// Writes a corpus for the app's GnUnwrapTest: frames from recorded captures, cut and corrupted copies of // them, and what gn_unwrap_its (the firmware's own code) makes of each. The Kotlin port must give the // same verdict and the same fields for every line. // // dump_corpus ... // // One line per frame, tab separated: // frame hex | A (accepted) or R (rejected) | port | flags (bit 0 GBC area, bit 1 signed) | // area lat | area lon | area distance A | truncated 0/1 | payload hex (empty when rejected) // // Selection is deterministic (fixed counts per category and a fixed-seed generator), so the file is // reproducible. Build and run: `make corpus` in this folder. #include #include #include #include #include #include "gn_unwrap.h" #include "test_util.h" #define MAX_PER_CATEGORY 20 #define MAX_REJECTED 60 #define MAX_MUTATED 250 static FILE *out; static unsigned rng_state = 12345u; static int accepted_total, rejected_total, mutated_total, cut_total; static int category_count[5 * 2 * 2 + 5]; // port index * 4 + signed * 2 + gbc static int seen_accepted; static unsigned rng(void) { rng_state = rng_state * 1664525u + 1013904223u; return rng_state >> 8; } static int port_index(int port) { switch (port) { case 2001: return 0; case 2002: return 1; case 2003: return 2; case 2004: return 3; default: return 4; } } static void emit(const uint8_t *frame, int len) { gn_rx_t rx; const bool ok = gn_unwrap_its(frame, len, &rx); for (int i = 0; i < len; i++) fprintf(out, "%02x", frame[i]); if (!ok) { fprintf(out, "\tR\t0\t0\t0\t0\t0\t0\t\n"); return; } fprintf(out, "\tA\t%u\t%d\t%d\t%d\t%u\t%d\t", rx.btp_dest_port, (rx.has_geo_area ? 1 : 0) | (rx.signed_unverified ? 2 : 0), (int)rx.geo_area_lat_tenmicrodeg, (int)rx.geo_area_lon_tenmicrodeg, (unsigned)rx.geo_area_distance_a_m, rx.truncated ? 1 : 0); for (int i = 0; i < rx.payload_len; i++) fprintf(out, "%02x", rx.payload[i]); fprintf(out, "\n"); } static void on_frame(const uint8_t *frame, int len, int index, void *ctx) { (void)ctx; (void)index; gn_rx_t rx; if (!gn_unwrap_its(frame, len, &rx)) { if (rejected_total < MAX_REJECTED && (rng() % 7) == 0) { emit(frame, len); ++rejected_total; } return; } ++seen_accepted; const int cat = port_index(rx.btp_dest_port) * 4 + (rx.signed_unverified ? 2 : 0) + (rx.has_geo_area ? 1 : 0); if (category_count[cat] < MAX_PER_CATEGORY) { ++category_count[cat]; emit(frame, len); ++accepted_total; // a few cut copies: the frame ends inside the payload, inside the headers, and at odd places const int cuts[] = {len - 3, len - 17, len / 2, len / 3, 40, 70}; for (unsigned c = 0; c < sizeof cuts / sizeof cuts[0]; c++) if (cuts[c] > 0 && cuts[c] < len && (rng() % 3) == 0) { emit(frame, cuts[c]); ++cut_total; } } // corrupted copies of frames taken at a steady rate: a few flipped bytes, anywhere if (mutated_total < MAX_MUTATED && (seen_accepted % 40) == 0) { uint8_t *copy = malloc((size_t)len); if (!copy) return; memcpy(copy, frame, (size_t)len); const int flips = 1 + (int)(rng() % 4); for (int k = 0; k < flips; k++) copy[rng() % (unsigned)len] ^= (uint8_t)(1u << (rng() % 8)); emit(copy, len); free(copy); ++mutated_total; } } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: dump_corpus ...\n"); return 2; } out = fopen(argv[1], "w"); if (!out) { perror(argv[1]); return 2; } for (int i = 2; i < argc; i++) { const int n = tu_pcap_foreach(argv[i], on_frame, NULL); if (n < 0) { fprintf(stderr, "cannot read %s\n", argv[i]); return 2; } } fclose(out); printf("corpus: %d accepted, %d cut, %d corrupted, %d rejected (of %d accepted frames seen)\n", accepted_total, cut_total, mutated_total, rejected_total, seen_accepted); for (int p = 0; p < 5; p++) printf(" port index %d: unsigned SHB %d, unsigned GBC %d, signed SHB %d, signed GBC %d\n", p, category_count[p * 4], category_count[p * 4 + 1], category_count[p * 4 + 2], category_count[p * 4 + 3]); return 0; }