# frank2/packer-tutorial architecture

> Architecture diagram of the GitHub repository frank2/packer-tutorial, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/frank2/packer-tutorial
- Repository: https://github.com/frank2/packer-tutorial

## Overview

##### Project type and purpose
- This is a native C++ “packer tutorial” project that demonstrates how to build a self-contained Windows packer/unpacker using CMake and zlib.
- It comprises three primary executables:
- dummy (a small test binary),
- stub (the unpacker runtime),
- packer (the compressor + resource-injector),
plus the zlib compression library.

##### File structure
- Top-level CMakeLists.txt orchestrates the build.
- Subdirectories:
- dummy/src/main.cpp – the target binary to be packed.
- stub/src/main.cpp – stub that unpacks and simulates the PE loader.
- src/main.cpp – packer logic (compress + embed stub).
- zlib-1.2.13/ – bundled zlib C library.
- CTest integration via add_test in root CMake to run test_pack and test_unpack.
- Build‐time flow: CMake → zlib build → stub build → packer build → dummy build.

##### Key insights from README
- Two-phase architecture:
a) Compression stage (packer.exe uses zlib to compress dummy.exe, injects stub.exe as a resource).
b) Unpacking stage (stub.exe at runtime extracts its “.packed” section, decompresses it, simulates Windows PE loader, resolves imports, relocations, transfers control to original entrypoint).
- Tutorial covers PE parsing/manipulation, resource embedding via Windows RC files generated by CMake.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build System
    CMake["CMake Build System"]:::build
    CTest["CTest Integration"]:::build

    %% Library
    ZLIB["zlib-1.2.13"]:::lib
    ZLIBCMake["zlib-1.2.13/CMakeLists.txt"]:::code

    %% Build Targets
    SUBTGT["stub.exe Target"]:::exe
    STUBCMake["stub/CMakeLists.txt"]:::code
    PACKTGT["packer.exe Target"]:::exe
    DUMMYTGT["dummy.exe Target"]:::exe
    DUMMYCMake["dummy/CMakeLists.txt"]:::code

    %% Source Files
    stubSrc["stub/src/main.cpp"]:::code
    PACKSRC["src/main.cpp"]:::code
    DUMMYSRC["dummy/src/main.cpp"]:::code

    %% Runtime Subsystem
    subgraph "stub.exe Runtime" 
        Rsrc["1.FindResource"]:::runtime
        Decomp["2.Decompress (zlib)"]:::runtime
        MapSec["3.MapSections"]:::runtime
        Imports["4.ResolveImports"]:::runtime
        Reloc["5.Perform Relocations"]:::runtime
        Dispatch["6.Dispatch Entry"]:::runtime
    end

    %% Build Dependencies
    CMake -->|"add_subdirectory(zlib)"| ZLIB
    CMake -->|"add_subdirectory(stub)"| SUBTGT
    CMake -->|"add_subdirectory(dummy)"| DUMMYTGT
    CMake -->|"built via root"| PACKTGT
    CMake -->|"add_test(test_pack & test_unpack)"| CTest

    ZLIB -->|"link"| SUBTGT
    ZLIB -->|"link"| PACKTGT

    SUBTGT -->|"embed as resource"| PACKTGT
    PACKTGT -->|"packs dummy.exe"| DUMMYTGT

    %% Source Links
    SUBTGT -->|"CMake target"| STUBCMake
    SUBTGT -->|"source code"| stubSrc
    PACKTGT -->|"source code"| PACKSRC
    DUMMYTGT -->|"CMake target"| DUMMYCMake
    DUMMYTGT -->|"source code"| DUMMYSRC
    ZLIB -->|"build scripts"| ZLIBCMake

    %% Runtime Flow
    SUBTGT --> Rsrc
    Rsrc --> Decomp
    Decomp --> MapSec
    MapSec --> Imports
    Imports --> Reloc
    Reloc --> Dispatch

    %% Click Events
    click CMake "https://github.com/frank2/packer-tutorial/blob/main/CMakeLists.txt"
    click STUBCMake "https://github.com/frank2/packer-tutorial/blob/main/stub/CMakeLists.txt"
    click stubSrc "https://github.com/frank2/packer-tutorial/blob/main/stub/src/main.cpp"
    click PACKSRC "https://github.com/frank2/packer-tutorial/blob/main/src/main.cpp"
    click DUMMYCMake "https://github.com/frank2/packer-tutorial/blob/main/dummy/CMakeLists.txt"
    click DUMMYSRC "https://github.com/frank2/packer-tutorial/blob/main/dummy/src/main.cpp"
    click ZLIB "https://github.com/frank2/packer-tutorial/tree/main/zlib-1.2.13/"
    click ZLIBCMake "https://github.com/frank2/packer-tutorial/blob/main/zlib-1.2.13/CMakeLists.txt"

    %% Styles
    classDef build fill:#D0E8FF,stroke:#0366D6,stroke-width:1px;
    classDef lib fill:#DFFFE0,stroke:#22863A,stroke-width:1px;
    classDef exe fill:#FFEDD5,stroke:#D15704,stroke-width:1px;
    classDef runtime fill:#FFF5B1,stroke:#B08800,stroke-width:1px;
    classDef code fill:#F0F0F0,stroke:#CCCCCC,stroke-width:1px;font-style:italic;
```
