# tsukumijima/libaribb25 architecture

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

- Interactive diagram: https://gitdiagram.com/tsukumijima/libaribb25
- Repository: https://github.com/tsukumijima/libaribb25

## Overview

1. **Repository purpose**
   - This repo is a unified codebase for ARIB STD-B1 and ARIB STD-B25 TS decryption libraries and tools.
   - It merges multiple historical forks into one buildable project for both **Windows** and **Linux**.

2. **Top-level architecture**
   - The code splits into:
     - a **transport-stream parser/orchestrator**
     - a **smart-card/CAS access layer**
     - a **MULTI2 cipher layer**
     - thin **public API / compatibility wrappers**
     - **CLI tools** and **stream-based test tools**
   - The same core logic is reused for both B1 and B25, with preprocessor switches selecting the target variant.

3. **Core orchestration layer**
   - `arib_std_b25.c/.h` is the main controller.
   - It owns the end-to-end workflow: packet sizing, PAT/PMT/ECM/CAT/EMM discovery, cipher setup, and packet pass-through or decryption.
   - Architecturally, this is the central hub that connects all other modules.

4. **Transport stream parsing boundary**
   - `ts_section_parser.c/.h` handles MPEG-2 TS section reconstruction.
   - It converts fragmented TS packets into usable PSI/SI sections such as PAT, PMT, ECM, CAT, and EMM.
   - This is the parsing boundary between raw TS input and higher-level CAS logic.

5. **CAS / smart-card integration**
   - `b_cas_card.c/.h` implements the card-facing part of the system.
   - On Windows it relies on Win32 smart-card APIs; on Linux the build is geared toward PC/SC through `libpcsclite`.
   - This module is responsible for card initialization and for sending ECM/EMM-related requests to the card to obtain scrambling keys.

6. **Cipher/decryption layer**
   - `multi2.c`, `multi2.cc`, and related headers implement the MULTI2 cipher used for broadcast TS scrambling.
   - The repo contains two main implementation lineages:
     - a C-based path closer to the HaijinW fork
     - a C++ rewrite from the stz2012 lineage
   - The orchestrator feeds system keys, CBC state, and scrambling keys into this layer, and receives decrypted payloads back.

7. **Platform-specific SIMD split**
   - SIMD support is not uniform across platforms.
   - Windows builds use the HaijinW-derived SIMD path with selectable instruction sets such as SSE2/SSSE3/AVX2.
   - Linux builds use the stz2012-derived SIMD path, selected automatically when available.
   - This is an important architectural fork because decryption performance and available code paths differ by platform.

8. **Public library interfaces**
   - `libaribb25.*` and `libaribb1.*` expose the reusable library surfaces.
   - `libaribb25` is the B-CAS/B25-facing API; `libaribb1` is the analogous B1-facing API for SkyPerfecTV Premium Service.
   - The README also notes DLL compatibility naming for existing software ecosystems, especially on Windows.

9. **Compatibility and legacy boundary**
   - `B25Decoder.cpp/.h` exists in the tree but is not part of the build; the functional compatibility layer lives in `libaribb25.cpp/.h`.
   - On Windows, the project can also emulate legacy `B25Decoder.dll` / `B1Decoder.dll` interfaces for external applications such as EDCB.
   - This is a boundary layer for drop-in compatibility rather than core decoding logic.

10. **Executable tools and data flow**
   - `b25` / `b1` are file-based command-line programs that read scrambled TS and write decrypted TS.
   - `arib-b25-stream-test` / `arib-b1-stream-test` are stream-based variants that use stdin/stdout instead of files.
   - The data flow is: input TS → parse PSI/sections → obtain keys from card → initialize MULTI2 → decrypt scrambled packets → emit TS.

11. **CAS workflow modeled in the code**
   - The core workflow is driven by TS metadata:
     - detect packet size
     - find PAT
     - resolve PMTs
     - identify ECMs and associated streams
     - optionally process CAT/EMM
   - Decryption is only enabled once the relevant ECM and card-derived keys are available.
   - This makes the architecture event-driven around broadcast table updates, not a simple linear file transform.

12. **Windows-only configuration path**
   - The README describes `.ini`-based card-reader selection for Windows builds.
   - This is a deployment-time override layer, useful when multiple readers or virtual readers are present.
   - It affects how the smart-card boundary is resolved, but not the TS parsing or cipher layers.

13. **Build and packaging architecture**
   - The repo supports two build systems:
     - **Visual Studio solution/project files** for Windows
     - **CMake** for Linux
   - CMake helper modules handle installation, uninstall, revision stamping, and PC/SC discovery.
   - pkg-config templates indicate the libraries are intended to be installed and consumed by other software, not only run as standalone binaries.

14. **External dependencies and runtime assumptions**
   - The key runtime dependency is a smart-card environment: B-CAS/ACAS/SPHD cards and compatible readers.
   - Linux builds depend on PC/SC development packages.
   - The project is fundamentally a **broadcast TS decryption library/tooling stack**, not a generic multimedia application.

Main entry points: [`aribb25/arib_std_b25.c`](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib_std_b25.c) (Orchestrator), [`aribb25/libaribb25.h`](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/libaribb25.h) (B25 API), [`arib_std_b25.sln`](https://github.com/tsukumijima/libaribb25/blob/master/arib_std_b25.sln) (VS solution), [`aribb25/multi2_simd.c`](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2_simd.c) (SIMD layer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_core["Core decryption"]
  node_n_arib_std_b25["Orchestrator<br/>TS decrypt controller<br/>[arib_std_b25.c]"]
  node_n_ts_section_parser["TS parser<br/>PSI/section reconstructor"]
  node_n_b_cas_card["Card access<br/>CAS/smart-card layer<br/>[b_cas_card.c]"]
  node_n_multi2_c["MULTI2 C<br/>cipher impl<br/>[multi2.c]"]
  node_n_multi2_cc["MULTI2 C++<br/>cipher impl<br/>[multi2.cc]"]
  node_n_multi2_simd["SIMD layer<br/>platform SIMD<br/>[multi2_simd.c]"]
end

subgraph group_group_api["Public APIs"]
  node_n_libaribb25["B25 API<br/>public library<br/>[libaribb25.h]"]
  node_n_libaribb1["B1 API<br/>public library<br/>[libaribb1.vcxproj]"]
  node_n_b25decoder["Legacy API<br/>compat boundary<br/>[B25Decoder.h]"]
end

subgraph group_group_tools["CLI tools"]
  node_n_b25_cli["b25 tool<br/>CLI decryptor<br/>[b25.vcxproj]"]
  node_n_b1_cli["b1 tool<br/>CLI decryptor<br/>[b1.vcxproj]"]
  node_n_b25_stream["B25 stream test<br/>stream tool"]
  node_n_b1_stream["B1 stream test<br/>stream tool"]
end

subgraph group_group_build["Build system"]
  node_n_cmake["CMake build<br/>build system<br/>[CMakeLists.txt]"]
  node_n_vs_solution["VS solution<br/>windows build<br/>[arib_std_b25.sln]"]
  node_n_find_pcsc["PC/SC find<br/>cmake module<br/>[FindPCSC.cmake]"]
  node_n_git_revision["Revision stamp<br/>cmake module<br/>[GitRevision.cmake]"]
end

node_n_arib_std_b25 -->|"parses"| node_n_ts_section_parser
node_n_arib_std_b25 -->|"requests keys"| node_n_b_cas_card
node_n_arib_std_b25 -->|"decrypts via"| node_n_multi2_c
node_n_arib_std_b25 -->|"decrypts via"| node_n_multi2_cc
node_n_arib_std_b25 -->|"selects"| node_n_multi2_simd
node_n_ts_section_parser -->|"feeds tables"| node_n_arib_std_b25
node_n_b_cas_card -->|"returns keys"| node_n_arib_std_b25
node_n_multi2_simd -->|"accelerates"| node_n_multi2_c
node_n_multi2_simd -->|"accelerates"| node_n_multi2_cc
node_n_libaribb25 -->|"wraps"| node_n_arib_std_b25
node_n_libaribb1 -->|"wraps"| node_n_arib_std_b25
node_n_b25decoder -.->|"compatibility"| node_n_libaribb25
node_n_b25_cli -->|"uses"| node_n_libaribb25
node_n_b1_cli -->|"uses"| node_n_libaribb1
node_n_b25_stream -->|"uses"| node_n_libaribb25
node_n_b1_stream -->|"uses"| node_n_libaribb1
node_n_cmake -->|"includes"| node_n_find_pcsc
node_n_cmake -->|"includes"| node_n_git_revision
node_n_vs_solution -->|"builds"| node_n_libaribb25
node_n_vs_solution -->|"builds"| node_n_b25_cli
node_n_vs_solution -->|"builds"| node_n_b25_stream

click node_n_arib_std_b25 "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib_std_b25.c"
click node_n_ts_section_parser "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/ts_section_parser.c"
click node_n_b_cas_card "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/b_cas_card.c"
click node_n_multi2_c "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2.c"
click node_n_multi2_cc "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2.cc"
click node_n_multi2_simd "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2_simd.c"
click node_n_libaribb25 "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/libaribb25.h"
click node_n_libaribb1 "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/libaribb1.vcxproj"
click node_n_b25decoder "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/B25Decoder.h"
click node_n_b25_cli "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/b25.vcxproj"
click node_n_b1_cli "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/b1.vcxproj"
click node_n_b25_stream "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib-b25-stream-test.vcxproj"
click node_n_b1_stream "https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib-b1-stream-test.vcxproj"
click node_n_cmake "https://github.com/tsukumijima/libaribb25/blob/master/CMakeLists.txt"
click node_n_vs_solution "https://github.com/tsukumijima/libaribb25/blob/master/arib_std_b25.sln"
click node_n_find_pcsc "https://github.com/tsukumijima/libaribb25/blob/master/cmake/FindPCSC.cmake"
click node_n_git_revision "https://github.com/tsukumijima/libaribb25/blob/master/cmake/GitRevision.cmake"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_arib_std_b25,node_n_ts_section_parser,node_n_b_cas_card,node_n_multi2_c,node_n_multi2_cc,node_n_multi2_simd toneBlue
class node_n_libaribb25,node_n_libaribb1,node_n_b25decoder toneAmber
class node_n_b25_cli,node_n_b1_cli,node_n_b25_stream,node_n_b1_stream toneMint
class node_n_cmake,node_n_vs_solution,node_n_find_pcsc,node_n_git_revision toneRose
```

## Components

### Core decryption

End-to-end TS control, parsing, card access, and cipher execution.

- [Orchestrator](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib_std_b25.c): TS decrypt controller · Coordinates packet sizing, PSI/section discovery, card workflow, and MULTI2 setup for B1/B25 variants. · `aribb25/arib_std_b25.c`
- [TS parser](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/ts_section_parser.c): PSI/section reconstructor · Reassembles fragmented MPEG-2 TS packets into PAT, PMT, ECM, CAT, and EMM sections. · `aribb25/ts_section_parser.c`
- [Card access](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/b_cas_card.c): CAS/smart-card layer · Handles reader selection, card initialization, and ECM/EMM requests to obtain scrambling keys. · `aribb25/b_cas_card.c`
- [MULTI2 C](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2.c): cipher impl · C-based MULTI2 implementation lineage used for broadcast TS decryption. · `aribb25/multi2.c`
- [MULTI2 C++](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2.cc): cipher impl · C++ MULTI2 implementation lineage and rewrite path used by the shared cipher layer. · `aribb25/multi2.cc`
- [SIMD layer](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/multi2_simd.c): platform SIMD · Instruction-set dispatch and optimized cipher paths, differing between Windows and Linux builds. · `aribb25/multi2_simd.c`

### Public APIs

Reusable library surfaces and compatibility boundaries.

- [B25 API](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/libaribb25.h): public library · Primary reusable API surface for B-CAS/B25 decryption consumers. · `aribb25/libaribb25.h`
- [B1 API](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/libaribb1.vcxproj): public library · Analogous API surface for B1/SkyPerfecTV Premium Service consumers. · `aribb25/libaribb1.vcxproj`
- [Legacy API](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/B25Decoder.h): compat boundary · Legacy B25Decoder compatibility boundary present in-tree but not part of the main build. · `aribb25/B25Decoder.h`

### CLI tools

File-based and stream-based executables built on the core library.

- [b25 tool](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/b25.vcxproj): CLI decryptor · File-based command-line decryptor that reads scrambled TS and writes decrypted TS. · `aribb25/b25.vcxproj`
- [b1 tool](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/b1.vcxproj): CLI decryptor · File-based command-line decryptor for the B1 variant. · `aribb25/b1.vcxproj`
- [B25 stream test](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib-b25-stream-test.vcxproj): stream tool · stdin/stdout variant of the B25 workflow for pipeline use. · `aribb25/arib-b25-stream-test.vcxproj`
- [B1 stream test](https://github.com/tsukumijima/libaribb25/blob/master/aribb25/arib-b1-stream-test.vcxproj): stream tool · stdin/stdout variant of the B1 workflow for pipeline use. · `aribb25/arib-b1-stream-test.vcxproj`

### Build system

Cross-platform build, packaging, and installation support.

- [CMake build](https://github.com/tsukumijima/libaribb25/blob/master/CMakeLists.txt): build system · Cross-platform build and install system for Linux and packaging workflows. · `CMakeLists.txt`
- [VS solution](https://github.com/tsukumijima/libaribb25/blob/master/arib_std_b25.sln): windows build · Visual Studio solution and project graph for Windows builds. · `arib_std_b25.sln`
- [PC/SC find](https://github.com/tsukumijima/libaribb25/blob/master/cmake/FindPCSC.cmake): cmake module · CMake helper for discovering PC/SC development dependencies on Linux. · `cmake/FindPCSC.cmake`
- [Revision stamp](https://github.com/tsukumijima/libaribb25/blob/master/cmake/GitRevision.cmake): cmake module · Build metadata helper for revision stamping and packaging support. · `cmake/GitRevision.cmake`

## Connections

- Orchestrator → TS parser: parses · Reconstructs TS sections before higher-level CAS decisions.
- Orchestrator → Card access: requests keys · Uses card access to obtain scrambling keys from ECM/EMM workflow.
- Orchestrator → MULTI2 C: decrypts via · Feeds system and scrambling keys into the cipher layer.
- Orchestrator → MULTI2 C++: decrypts via · Alternative MULTI2 implementation lineage used by the shared core.
- Orchestrator → SIMD layer: selects · Dispatches to optimized SIMD implementations when available.
- TS parser → Orchestrator: feeds tables · Supplies PAT/PMT/ECM/CAT/EMM discovery results to the orchestrator.
- Card access → Orchestrator: returns keys · Provides card-derived keys used to enable decryption.
- SIMD layer → MULTI2 C: accelerates · Optimized execution path for the C cipher implementation.
- SIMD layer → MULTI2 C++: accelerates · Optimized execution path for the C++ cipher implementation.
- B25 API → Orchestrator: wraps · Public B25 API delegates core work to the orchestrator.
- B1 API → Orchestrator: wraps · Public B1 API reuses the same controller with variant selection.
- Legacy API → B25 API: compatibility · Legacy interface boundary replaced in practice by libaribb25.
- b25 tool → B25 API: uses · File-based tool is built on the reusable B25 API.
- b1 tool → B1 API: uses · File-based tool is built on the reusable B1 API.
- B25 stream test → B25 API: uses · Stream-based B25 tool uses the same library surface.
- B1 stream test → B1 API: uses · Stream-based B1 tool uses the same library surface.
- CMake build → PC/SC find: includes · Build configuration locates PC/SC on Linux.
- CMake build → Revision stamp: includes · Build configuration stamps revision and packaging metadata.
- VS solution → B25 API: builds · Windows solution builds the main B25 library and related targets.
- VS solution → b25 tool: builds · Windows solution builds the command-line tools.
- VS solution → B25 stream test: builds · Windows solution builds stream-based test executables.
