CameraFileCopy (cfc)

repository·master·Indexed 21 days ago

https://github.com/sz3/cfc

An Android application and supporting library, libcimbar, that enables one-way air-gapped data transfer by reading animated cimbar codes via a device camera. It utilizes a high-density 2D barcode format with Reed Solomon error correction, zstd compression, and Wirehair fountain codes to achieve transfer speeds up to 850 kilobits/s. The project includes tools for encoding files into PNG images and animating them to a window for reception.

Tokens
12.9K
Snippets
46
Records
69
Agent score
75%

What's inside cfc

  1. What is libcimbar?

    master

    libcimbar is an experimental high-density 2D color matrix barcode designed for small data transfers. It encodes data into a grid of colored tiles, using Reed-Solomon error correction (expected error rate ~1%).

    An optimized version of the cimbar format, libcimbar implements a file encoding protocol using wirehair (fountain codes) and zstd compression. This allows files up to 33MB (compressed) to be encoded into a series of cimbar codes (images or video frames). Because it uses fountain codes, the file can be reconstructed even if some frames are corrupted or lost during transmission via a camera.

  2. Overview of Wirehair Fountain Codes

    master

    Wirehair is a C library providing fast and portable fountain codes (erasure codes). It produces a stream of error correction blocks from a data source. When a sufficient number of these blocks are received, the original data can be recovered.

    Key Characteristics:

    • Scalability: Supports an unlimited number of error correction blocks and very large block counts.
    • Performance: Complexity is $O(N)$ relative to input data size, making it well-suited for large datasets compared to $O(N \log N)$ or $O(N^2)$ alternatives.
    • Efficiency: While not a perfect MDS code (it may require $N+1$ or $N+2$ packets to recover $N$ original packets), the average overhead is very low (approx. $N + 0.02$ packets).
    • Dependencies: No external dependencies required.
  3. Overview of libcimbar barcode technology

    master

    libcimbar is an optimized implementation of the cimbar high-density 2D barcode format designed for air-gapped data transfer. It uses a grid of colored tiles to encode bits and applies Reed Solomon error correction to handle the lossy nature of video-to-digital decoding.

    Key features include:

    • High Speed: Can sustain up to 850 kilobits/s (~106 KB/s) using a monitor and smartphone camera.
    • Robustness: Uses fountain codes (wirehair) and zstd compression. Files up to 33MB can be encoded as a series of frames. The protocol is resilient to out-of-order frames, corruption, or missing frames.
    • Cross-Platform: The encoder can run in any modern web browser via WASM/asmjs (e.g., at cimbar.org), while the decoder is available as an Android app.
  4. What is CameraFileCopy (cfc)?

    master

    CameraFileCopy (cfc) is an Android application designed to receive data via a one-way channel using the device camera. It functions by reading animated cimbar codes.

    Key characteristics:

    • No wireless connectivity required: It does not use Wi-Fi, Bluetooth, NFC, or any antennas. It works effectively in airplane mode.
    • Data Source: To send data, use a cimbar encoder (such as cimbar.org) or the cimbar_send utility from libcimbar. The sender displays an animated barcode, which the cfc app reads via the camera.
  5. What is libcorrect and how does it work?

    master

    libcorrect is a performant, BSD-licensed library for Forward Error Correction (FEC). It allows you to encode redundancy into data packets to protect them against lossy channels, enabling the recovery of original data during decoding.

    It implements two primary algorithms:

    1. Convolutional codes: Robust against constant background noise. libcorrect uses a Viterbi algorithm decoder for these.
    2. Reed-Solomon: Effective at dealing with burst noise.

    It is intended as a drop-in, BSD-licensed replacement for libfec.

  6. Configure Error Correction (ECC) in Cimbar

    master

    Cimbar uses error correction to handle imperfect tile decoding. The encoder processes data in chunks.

    For a given ecc value, the encoder reads a chunk of 155-ecc bytes of real data, adds ecc bytes of error correction, and then encodes the resulting 155 byte block 6 bits at a time.

    Example: With ecc=30, the system provides 30 bytes of error correction for every 125 bytes of real data.

  7. Understand Interleaving and Fountain Encoding

    master

    Cimbar employs two strategies to handle data loss and localized errors:

    Interleaving

    Because physical obstructions (like a finger or pen) tend to affect adjacent cells, Cimbar interleaves ECC chunks across the image by skipping over $N$ cells. This spreads the impact of localized errors across different ECC blocks.

    Fountain (Wirehair) Encoding

    To handle files larger than the capacity of a single image or to improve robustness, Cimbar uses Fountain (wirehair) codes.

    • Overhead: Introduces a small amount of bookkeeping (e.g., 6 bytes per 744 bytes of real data in 6-bit Cimbar).
    • Reconstruction: Allows the decoder to reconstruct the file from multiple fountain frames received in any order.
    • Resilience: As long as $N+1$ frames are received (where $N$ is the number of frames required for the file size), the file can be recovered.
    • Constraints: The current implementation caps file size at 33.55MB because wirehair requires file contents to be stored in RAM.
  8. How the Cimbar decoder works

    master

    The decoder performs several complex steps to recover data from an image:

    1. Scan and Extract: Similar to QR codes, the decoder locates three square patterns at the corners of the image to triangulate the fourth corner. It then applies a perspective transform to extract the Cimbar grid.
    2. Symbol Decoding: The decoder iterates through cells, prioritizing those with higher confidence. It uses a distance metric (Hamming distance from the image hash) to determine confidence.
    3. Drift Tracking: The decoder tracks local distortion using a drift value (an $(x,y)$ offset, capped at 7px) to adjust for misalignments.
    4. Reassembly: It uses the deinterleave function to reverse the encoder's interleaving scheme and then applies error_correct to finalize the data.

    Decoding Loop Logic

    for i, bits, distance, drift in next_decode():
        results[deinterleave(i)] = bits
        position_tracker.update(i, drift, distance)
    
    decoded_data = error_correct(results)
  9. How Cimbar encoding works

    master

    Cimbar encodes data into a grid of colored tiles using an image hashing approach. The core concept is that each symbol (a set of bits) is represented by a tile whose 8x8 pixel grid is converted into a 64-bit number via a simple threshold (1 if pixel is set, 0 if not).

    To ensure reliability, symbols are chosen such that they have a high Hamming distance from one another. This allows the decoder to identify the most likely symbol even if the tile is blurry or corrupted.

    In a 6-bit Cimbar configuration (4 symbol bits and 2 color bits), a single image can contain approximately 12,400 tiles, resulting in roughly 9,300 bytes of data per image.

    // Conceptual encoding loop
    for bits in error_correction(file):
        for x, y in next_position():
            img.paste(cimbar_tile(bits), x, y)
  10. Build Zstandard DLLs on Windows using MinGW+MSYS

    master

    You can create a DLL using MinGW+MSYS by running make libzstd. This generates:

    • dll\libzstd.dll (The dynamic library)
    • dll\libzstd.lib (The import library, required for Visual C++)

    To compile a project using the generated DLL with gcc/MinGW, include the header and link against the DLL:

    gcc $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\libzstd.dll

    Note: The resulting executable will require dll\libzstd.dll to be present at runtime.

  11. Enable multithreading in Zstandard

    master

    To enable multithreading when building with make, you must satisfy two conditions:

    1. Set the build macro ZSTD_MULTITHREAD (e.g., -DZSTD_MULTITHREAD for gcc).
    2. For POSIX systems, compile with the pthread flag (e.g., -pthread for gcc).

    Important: When linking a POSIX program with a multithreaded version of libzstd, you must also invoke the -pthread flag during the link stage.

  12. Build the Zstandard library

    master

    Use the provided Makefile to generate static and dynamic libraries.

    • make: Generates both static and dynamic libraries. By default, the dynamic library is multithreaded and the static library is single-threaded.
    • make install: Installs libraries and headers into target system directories.

    To control multithreading behavior specifically:

    • make lib-mt: Forces multithreading for both static and dynamic libraries.
    • make lib-nomt: Forces single-threading for both static and dynamic libraries.
    • make lib: Default behavior (dynamic is MT, static is ST).
    make
    make install
    # Or specific threading modes
    make lib-mt
    make lib-nomt