Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ports/c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.o
prism
examples/encode_decode
hello.png
21 changes: 21 additions & 0 deletions ports/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Optional CMake build for the Comb Code C port (the Makefile is the primary
# build; this is an equivalent alternative).
cmake_minimum_required(VERSION 3.16)
project(CombCode VERSION 1.0.0 LANGUAGES C)
# Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit.

set(CMAKE_C_STANDARD 11)

find_package(ZLIB REQUIRED)

add_library(combcode STATIC
prism.c prism_crypto.c prism_ecdsa.c prism_secure.c
prism_parts.c prism_cards.c prism_issuers.c)
target_link_libraries(combcode PUBLIC ZLIB::ZLIB m)
target_include_directories(combcode PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_executable(prism cli.c)
target_link_libraries(prism PRIVATE combcode)

add_executable(encode_decode_example examples/encode_decode.c)
target_link_libraries(encode_decode_example PRIVATE combcode)
4 changes: 4 additions & 0 deletions ports/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ clean:
rm -f prism *.o

.PHONY: clean

example: examples/encode_decode.c prism.o
$(CC) $(CFLAGS) -I. -o examples/encode_decode examples/encode_decode.c prism.o -lz -lm
./examples/encode_decode
37 changes: 34 additions & 3 deletions ports/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,41 @@ Byte-compatible with comb-issuers.js, comb-parts.js and comb-cards.js:
PRISM-TICKET/1 payloads; built payloads are byte-identical to the JS
builders (escaping included).

## Not in this port (yet)
## Library use

The public C API is `prism.h` (encoder/decoder), `prism_secure.h` (Comb Code
Secure), `prism_parts.h` (multi-part files), `prism_cards.h` (Wi-Fi / vCard /
ticket payloads) and `prism_issuers.h` (ticket issuers). All functions are
documented at their declarations. Example (`examples/encode_decode.c`,
build and run with `make example`):

```c
#include "prism.h"

char err[256];
const char *text = "Hello from Comb Code";
prism_encode_png((const uint8_t *)text, strlen(text), -1, 1,
PRISM_MODE_COLOR, PRISM_TYPE_TEXT, 12, "hello.png", err, sizeof err);
uint8_t *out; size_t out_len; int out_type;
prism_decode_png("hello.png", &out, &out_len, &out_type, err, sizeof err);
printf("%.*s
", (int)out_len, out); /* Hello from Comb Code */
free(out);
```

### API reference (encoder/decoder, `prism.h`)

- `prism_encode_png(data, len, size, ecc, mode, type, scale, out_png, err, errlen)` —
encode payload bytes and write a PNG. `size` -1 = automatic or 0..5;
`ecc` 0..3 (10/20/30/40% repair data); `mode` color or black-and-white;
`type` text or binary; `scale` pixels per cell unit.
- `prism_encode_rgba(...)` — same, to an in-memory RGBA image.
- `prism_decode_png(in_png, &out, &out_len, &out_type, err, errlen)` —
decode a clean PNG; the payload is only returned when its CRC-32 passes.
- `prism_capacity_payload(size, ecc, mode)` — maximum payload bytes.

The phone-camera reading pipeline (glare, blur, perspective — that stays on
the website's reader pages) is being ported next.
The phone-camera reader (photos: glare, blur, perspective) is in `reader/`,
with its own README.

## License

Expand Down
32 changes: 32 additions & 0 deletions ports/c/examples/encode_decode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Comb Code C example: encode a text payload to a PNG, decode it back. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../prism.h"

int main(void) {
char err[256];
const char *text = "Hello from Comb Code";

/* Encode: automatic size, 20% repair data, color mode, 12 px per cell unit. */
if (prism_encode_png((const uint8_t *)text, strlen(text), -1, 1,
PRISM_MODE_COLOR, PRISM_TYPE_TEXT, 12,
"hello.png", err, sizeof err) != 0) {
fprintf(stderr, "encode failed: %s\n", err);
return 1;
}
printf("encoded %zu bytes -> hello.png\n", strlen(text));

/* Decode it back. */
uint8_t *out; size_t out_len; int out_type;
if (prism_decode_png("hello.png", &out, &out_len, &out_type, err, sizeof err) != 0) {
fprintf(stderr, "decode failed: %s\n", err);
return 1;
}
printf("decoded %zu bytes: %.*s\n", out_len, (int)out_len, out);

int ok = out_len == strlen(text) && memcmp(out, text, out_len) == 0;
free(out);
printf("round-trip %s\n", ok ? "OK" : "MISMATCH");
return ok ? 0 : 1;
}
6 changes: 6 additions & 0 deletions ports/c/reader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.o
*.a
obj/
bin/
cli
chain772*
16 changes: 16 additions & 0 deletions ports/c/reader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CC ?= cc
# -ffp-contract=off keeps floating point bit-identical to the JavaScript reader.
CFLAGS ?= -O2 -ffp-contract=off -std=gnu11 -Wall

SRC = prism_foundations.c prism_jsmath.c prism_detect.c prism_image.c prism_classify.c \
prism_decode.c prism_mix.c prism_read.c prism_distfit.c prism_chain.c

all: cli

cli: $(SRC) cli.c
$(CC) $(CFLAGS) -I. -o $@ cli.c $(SRC) -lm

clean:
rm -f cli

.PHONY: all clean
36 changes: 36 additions & 0 deletions ports/c/reader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Comb Code camera reader — C (C11) port

A C11 port of the live Comb Code photo/camera reader: detection, hypothesis
search, palette-mix recovery, distance-fit recovery and the full retry chain.
It reads real photos, not only clean PNGs. The clean-PNG encoder/decoder is in
the parent folder.

Build: `make` (produces `cli`). The Makefile passes `-ffp-contract=off` so
floating point stays bit-identical with the JavaScript reader.

Run: `./cli scene.raw`, where the file is 8 bytes little-endian width and
height followed by RGBA bytes. Prints `OK <text>`, `OKBIN <hex>` or `FAIL`.
A result is only returned when the header CRC and payload CRC-32 pass.

## Verified status

Method: 772 synthetic scenes from the bench kit, run sequentially, each result
compared to the live JavaScript reader's result for the same image
(`../../reader-verify/verify_vs_live.py`).

| Result | Scenes |
|---|---|
| Read, payload identical to live reader | 728 |
| Both fail | 44 |
| Live reader reads, port does not | 0 |
| Wrong payload | 0 |
| Port reads, live reader does not | 0 |

The live reader reads 728 of the 772 scenes; the port matches it on every
scene. Timing: the reader stops a page read after 6 seconds, as the live one
does. Run verification one process at a time; under heavy parallel load a few
borderline scenes time out. This is a synthetic-scene result, not a
physical-camera result.

Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit.
No warranty.
23 changes: 23 additions & 0 deletions ports/c/reader/cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* prism-reader-cli: read a Comb Code from a raw RGBA file (8-byte LE w,h + RGBA). */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "prism_chain.h"
int main(int argc, char **argv) {
(void)argc;
FILE *f = fopen(argv[1], "rb");
if (!f) return 2;
uint32_t w, h;
if (fread(&w, 4, 1, f) != 1 || fread(&h, 4, 1, f) != 1) return 2;
uint8_t *rgba = malloc((size_t)w * h * 4);
if (fread(rgba, 4, (size_t)w * h, f) != (size_t)w * h) return 2;
fclose(f);
PrismRead r;
if (prism_chain_read(rgba, (int)w, (int)h, &r) == 0 && r.ok) {
if (r.payload.text) { printf("OK %s\n", r.payload.text); }
else { printf("OKBIN "); for (int i = 0; i < r.payload.n_bytes; i++) printf("%02x", r.payload.bytes[i]); printf("\n"); }
return 0;
}
printf("FAIL\n");
return 0;
}
Loading