From acf6bc48b2696163803477a67c24a319c41441f0 Mon Sep 17 00:00:00 2001 From: K0NEO <243623884+k0neo@users.noreply.github.com> Date: Thu, 3 Dec 2026 21:58:14 -0600 Subject: [PATCH 1/5] Preserve unchanged AnyTone channel records Avoid reconstructing existing channel records when decoded fields are unchanged. D878UV2 offline regression eliminates 6800 of 7436 unintended byte changes. Binary serialization remains lossless. --- backends/anytone/codeplug.c | 49 ++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/backends/anytone/codeplug.c b/backends/anytone/codeplug.c index 126389e..9e57bb1 100644 --- a/backends/anytone/codeplug.c +++ b/backends/anytone/codeplug.c @@ -454,19 +454,66 @@ int at_channel_to_image(struct at_image *img, const struct at_channel *ch) return 0; } +/* + * Compare decoded channel fields explicitly. + * Structure padding and unmodeled image bytes are not compared. + */ +static int channel_fields_equal(const struct at_channel *a, + const struct at_channel *b) +{ + return a->index == b->index + && strcmp(a->name, b->name) == 0 + && a->rx_hz == b->rx_hz + && a->tx_hz == b->tx_hz + && a->mode == b->mode + && a->power == b->power + && a->bandwidth_wide == b->bandwidth_wide + && a->color_code == b->color_code + && a->timeslot == b->timeslot + && a->contact_index == b->contact_index + && a->radio_id_index == b->radio_id_index + && a->scan_list_index == b->scan_list_index + && a->rx_group_index == b->rx_group_index + && a->rx_only == b->rx_only + && a->admit == b->admit + && a->rx_ctcss == b->rx_ctcss + && a->tx_ctcss == b->tx_ctcss + && a->rx_dcs == b->rx_dcs + && a->tx_dcs == b->tx_dcs; +} + int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) { uint8_t *ch_bmp = at_image_find_mut(img, OFF_CH_BMP, CH_BMP_SIZE); if (ch_bmp) { + uint8_t original_bitmap[CH_BMP_SIZE]; + + memcpy(original_bitmap, ch_bmp, CH_BMP_SIZE); memset(ch_bmp, 0, CH_BMP_SIZE); + for (int i = 0; i < cp->nchannels; i++) { const struct at_channel *ch = &cp->channels[i]; if (ch->index < 0 || ch->index >= AT_MAX_CHANNELS) continue; at_bitmap_set(ch_bmp, (unsigned)ch->index, 1); uint8_t *raw = at_image_find_mut(img, channel_addr((unsigned)ch->index), CH_SIZE); - if (raw) + if (raw) { + /* + * Preserve the complete original record when + * none of its decoded fields has changed. + */ + if (at_bitmap_get(original_bitmap, + (unsigned)ch->index)) { + struct at_channel existing; + + decode_channel(&existing, raw, ch->index); + + if (channel_fields_equal(&existing, ch)) + continue; + } + encode_channel(raw, ch); + } } } From 359fe987b5d4bed0a7c1d657cbf12a551987a9f3 Mon Sep 17 00:00:00 2001 From: K0NEO <243623884+k0neo@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:38:42 -0600 Subject: [PATCH 2/5] Preserve AnyTone channel and contact bitmap padding Restrict bitmap initialization to valid record indices. D878UV2 offline regression reduces unintended byte modifications from 636 to 622 without additional observed changes. --- backends/anytone/codeplug.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backends/anytone/codeplug.c b/backends/anytone/codeplug.c index 9e57bb1..98aae3b 100644 --- a/backends/anytone/codeplug.c +++ b/backends/anytone/codeplug.c @@ -489,7 +489,10 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) uint8_t original_bitmap[CH_BMP_SIZE]; memcpy(original_bitmap, ch_bmp, CH_BMP_SIZE); - memset(ch_bmp, 0, CH_BMP_SIZE); + /* Clear valid channel bits, preserving bitmap padding. */ + for (unsigned i = 0; i < AT_MAX_CHANNELS; i++) + at_bitmap_set(ch_bmp, i, 0); + for (int i = 0; i < cp->nchannels; i++) { const struct at_channel *ch = &cp->channels[i]; @@ -519,7 +522,10 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) uint8_t *ct_bmp = at_image_find_mut(img, OFF_CONTACT_BMP, CONTACT_BMP_SIZE); if (ct_bmp) { - memset(ct_bmp, 0xff, CONTACT_BMP_SIZE); /* inverted: all disabled */ + /* Clear valid contact bits, preserving bitmap padding. */ + for (unsigned i = 0; i < AT_MAX_CONTACTS; i++) + at_inv_bitmap_set(ct_bmp, i, 0); + for (int i = 0; i < cp->ncontacts; i++) { const struct at_contact *c = &cp->contacts[i]; if (c->index < 0 || c->index >= AT_MAX_CONTACTS) From 00bdb752ce55ba49646ae45bb8156ba714f0742a Mon Sep 17 00:00:00 2001 From: K0NEO <243623884+k0neo@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:48:53 -0600 Subject: [PATCH 3/5] Preserve inactive AnyTone zone selection entries Update VFO A/B selection entries only for programmed zones. D878UV2 offline regression restores 576 bytes in the zone selection segment with zero collateral changes. --- backends/anytone/codeplug.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/backends/anytone/codeplug.c b/backends/anytone/codeplug.c index 98aae3b..b30c8bf 100644 --- a/backends/anytone/codeplug.c +++ b/backends/anytone/codeplug.c @@ -567,10 +567,7 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) uint8_t *zb = at_image_find_mut(img, OFF_ZONE_B_CH, ZONE_AB_CH_BYTES); memset(z_bmp, 0, ZONE_BMP_SIZE); - if (za) - memset(za, 0xff, ZONE_AB_CH_BYTES); - if (zb) - memset(zb, 0xff, ZONE_AB_CH_BYTES); + /* Preserve selection entries for inactive zones. */ for (int i = 0; i < cp->nzones; i++) { const struct at_zone *z = &cp->zones[i]; if (z->index < 0 || z->index >= AT_MAX_ZONES) @@ -586,10 +583,16 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) memset(chans, 0xff, ZONE_CHAN_STRIDE); for (int j = 0; j < z->nchannels && j < AT_ZONE_CHANS; j++) at_put_u16_le(chans + j * 2, (uint16_t)z->channels[j]); - if (za && z->channel_a >= 0) - at_put_u16_le(za + (unsigned)z->index * 2, (uint16_t)z->channel_a); - if (zb && z->channel_b >= 0) - at_put_u16_le(zb + (unsigned)z->index * 2, (uint16_t)z->channel_b); + if (za) + at_put_u16_le( + za + (unsigned)z->index * 2, + z->channel_a >= 0 + ? (uint16_t)z->channel_a : UINT16_MAX); + if (zb) + at_put_u16_le( + zb + (unsigned)z->index * 2, + z->channel_b >= 0 + ? (uint16_t)z->channel_b : UINT16_MAX); } } From da0219a093a1ebb9c9c30e4c66b605c7351ab158 Mon Sep 17 00:00:00 2001 From: K0NEO <243623884+k0neo@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:58:18 -0600 Subject: [PATCH 4/5] Preserve unmodeled AnyTone scan-list configuration Retain existing scan-list settings and restrict updates to modeled fields. D878UV2 regression now passes binary, structured, and JSON no-edit round trips with zero changed bytes across the complete captured ATCP image. --- backends/anytone/codeplug.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/backends/anytone/codeplug.c b/backends/anytone/codeplug.c index b30c8bf..bd19777 100644 --- a/backends/anytone/codeplug.c +++ b/backends/anytone/codeplug.c @@ -598,7 +598,14 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) uint8_t *s_bmp = at_image_find_mut(img, OFF_SCAN_BMP, SCAN_BMP_SIZE); if (s_bmp) { - memset(s_bmp, 0, SCAN_BMP_SIZE); + uint8_t original_bitmap[SCAN_BMP_SIZE]; + + memcpy(original_bitmap, s_bmp, SCAN_BMP_SIZE); + + /* Clear valid scan-list bits without altering padding. */ + for (unsigned i = 0; i < AT_MAX_SCANLISTS; i++) + at_bitmap_set(s_bmp, i, 0); + for (int i = 0; i < cp->nscan_lists; i++) { const struct at_scan_list *s = &cp->scan_lists[i]; if (s->index < 0 || s->index >= AT_MAX_SCANLISTS) @@ -607,9 +614,20 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img) uint8_t *raw = at_image_find_mut(img, scan_addr((unsigned)s->index), SCAN_SIZE); if (!raw) continue; - memset(raw, 0xff, SCAN_SIZE); - raw[0] = 0; + /* + * Initialize newly allocated records only. + * Preserve unmodeled settings in existing scan lists. + */ + if (!at_bitmap_get(original_bitmap, + (unsigned)s->index)) { + memset(raw, 0xff, SCAN_SIZE); + raw[0] = 0; + } + at_write_ascii(raw + 0x0f, 16, s->name); + + /* Reset only the channel membership field. */ + memset(raw + 0x20, 0xff, AT_SCAN_MEMBERS * 2); for (int j = 0; j < s->nmembers && j < AT_SCAN_MEMBERS; j++) at_put_u16_le(raw + 0x20 + j * 2, (uint16_t)s->members[j]); } From b1f511aa649d374ec2cfa48449ab8a79ae36c189 Mon Sep 17 00:00:00 2001 From: K0NEO <243623884+k0neo@users.noreply.github.com> Date: Thu, 3 Dec 2026 23:41:24 -0600 Subject: [PATCH 5/5] Add offline AnyTone lossless round-trip regression tests Generate a deterministic synthetic ATCP fixture without personal radio data. Verify byte-identical binary, structured, and JSON round trips. The original implementation modifies 1006 synthetic fixture bytes; the corrected implementation modifies none. --- tests/anytone/README.md | 132 ++++++++++++++++++++++++++++++ tests/anytone/generate-fixture.pl | 102 +++++++++++++++++++++++ tests/anytone/roundtrip.c | 98 ++++++++++++++++++++++ tests/anytone/run-roundtrip.sh | 68 +++++++++++++++ 4 files changed, 400 insertions(+) create mode 100644 tests/anytone/README.md create mode 100755 tests/anytone/generate-fixture.pl create mode 100644 tests/anytone/roundtrip.c create mode 100755 tests/anytone/run-roundtrip.sh diff --git a/tests/anytone/README.md b/tests/anytone/README.md new file mode 100644 index 0000000..ffa1d78 --- /dev/null +++ b/tests/anytone/README.md @@ -0,0 +1,132 @@ +# AnyTone Lossless Round-Trip Regression + +## Purpose + +Verify that an unmodified AnyTone ATCP image survives +binary, structured, and JSON conversion without unintended +changes to its contents. + +The regression was developed against CodeplugStudio 0.0.2, +upstream revision b229a85. + +## Defects addressed + +The original structured encoder reconstructs records and +initializes entire memory regions even when the corresponding +configuration has not changed. + +The corrections address four observed problems: + +1. Unchanged channel records are unnecessarily reconstructed, + discarding fields not represented in the structured model. +2. Channel and digital contact bitmap initialization overwrites + padding outside the valid record-index range. +3. Initialization of VFO A/B zone selection tables overwrites + entries belonging to inactive zones. +4. Scan-list encoding erases configuration fields not represented + in the structured model. + +## Running the regression + +Requirements: + +- Standard Linux build environment +- C11 compiler +- GNU Make +- Perl +- pkg-config +- libusb development libraries + +From the repository root: + + ./tests/anytone/run-roundtrip.sh + +The runner builds the application, generates a synthetic ATCP +fixture, compiles the regression harness, and executes three +independent conversion paths. + +Test A: + + ATCP -> ATCP + +Test B: + + ATCP -> C structures -> ATCP + +Test C: + + ATCP -> C structures -> JSON -> C structures -> ATCP + +All three resulting images must be byte-identical to the +original synthetic fixture. + +A failed comparison produces a nonzero exit status. + +## Synthetic fixture + +The fixture generator constructs a deterministic, sparse +19,468-byte ATCP image containing seven memory segments. + +No captured radio data, personal configuration, callsigns, +or subscriber databases are included. + +The fixture is generated at runtime and removed with the +temporary regression output when the runner exits. + +## Verified regression results + +Synthetic fixture: + +| Test | Original | Corrected | +| --- | ---: | ---: | +| Binary changed bytes | 0 | 0 | +| Structured changed bytes | 1006 | 0 | +| JSON changed bytes | 1006 | 0 | +| Changed memory segments | 5 | 0 | + +Original implementation differences: + +| Address | Changed bytes | +| --- | ---: | +| 0x00800000 | 3 | +| 0x01080000 | 1 | +| 0x024C1500 | 1 | +| 0x02500100 | 1000 | +| 0x02640000 | 1 | + +The corrections were additionally verified against a private +D878UV2 acquisition containing 102 memory segments and +4,118,912 total bytes. + +The original implementation introduced 7,436 unintended byte +changes during an unedited structured round trip. The corrected +implementation produced byte-identical output through all three +conversion paths. + +The private acquisition is not distributed. + +## Scope and limitations + +This regression establishes no-edit preservation for the +tested fixtures only. + +It does not establish: + +- Lossless intentional channel editing. +- Correct initialization of newly allocated records. +- Correct record deletion and reference maintenance. +- Compatibility with every AnyTone model or firmware revision. +- Safe programming of physical radios. + +Further validation is required for those operations. + +## Hardware safety + +All tests operate exclusively on local files. + +The synthetic fixture is deliberately incomplete and is not +an operational radio codeplug. + +NEVER upload the synthetic fixture to a physical radio. + +No radio connection is required. diff --git a/tests/anytone/generate-fixture.pl b/tests/anytone/generate-fixture.pl new file mode 100755 index 0000000..d9924dc --- /dev/null +++ b/tests/anytone/generate-fixture.pl @@ -0,0 +1,102 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $path = shift or die "Output filename required\n"; + +my %segments; + +# Synthetic channel bank. +my $bank = "\xff" x 16384; +my $channel = "\x00" x 64; + +# Simplex frequency representation. +substr($channel, 0x00, 4) = pack('H8', '14550000'); +substr($channel, 0x04, 4) = pack('H8', '14550000'); + +# Synthetic channel name. +substr($channel, 0x23, 16) = + pack('a16', 'TEST CHANNEL'); + +# Unmodeled field preservation marker. +substr($channel, 0x35, 1) = "\x01"; + +substr($bank, 0, 64) = $channel; + +# Corresponding channel extension. +my $extension = "\x00" x 64; +substr($extension, 3, 1) = "\x01"; +substr($bank, 8192, 64) = $extension; + +$segments{0x00800000} = $bank; + +# Channel bitmap with deliberate padding marker. +my $channel_bitmap = "\x00" x 512; + +vec($channel_bitmap, 0, 1) = 1; +substr($channel_bitmap, 500, 1) = "\xa5"; + +$segments{0x024c1500} = $channel_bitmap; + +# Inverted contact bitmap with padding marker. +my $contact_bitmap = "\xff" x 1264; + +substr($contact_bitmap, 1250, 1) = "\x5a"; + +$segments{0x02640000} = $contact_bitmap; + +# No active zones. +$segments{0x024c1300} = "\x00" x 32; + +# Inactive VFO A/B zone selection tables. +$segments{0x02500100} = "\x00" x 1012; + +# One active scan list. +my $scan_bitmap = "\x00" x 32; +vec($scan_bitmap, 0, 1) = 1; + +$segments{0x024c1340} = $scan_bitmap; + +# Synthetic scan-list record. +my $scan = "\xff" x 144; + +substr($scan, 0x00, 1) = "\x00"; +substr($scan, 0x01, 1) = "\x12"; + +substr($scan, 0x0f, 16) = + pack('a16', 'TEST SCAN'); + +substr($scan, 0x20, 2) = pack('v', 0); + +$segments{0x01080000} = $scan; + +# Serialize the synthetic ATCP container. +open my $fh, '>:raw', $path or die "$path: $!\n"; + +print {$fh} pack( + 'a4 V a8 a8 C x3 V', + 'ATCP', + 1, + 'D878UV2', + 'TEST', + 7, + scalar keys %segments +) or die "Header write failed\n"; + +for my $addr (sort {$a <=> $b} keys %segments) { + my $data = $segments{$addr}; + + print {$fh} pack('V2', $addr, length($data)), + $data or die "Segment write failed\n"; + + printf "Segment 0x%08X: %u bytes\n", + $addr, length($data); +} + +close $fh or die "Close failed: $!\n"; + +printf "\nSynthetic segments generated: %u\n", + scalar keys %segments; + +print "No original radio data used.\n"; diff --git a/tests/anytone/roundtrip.c b/tests/anytone/roundtrip.c new file mode 100644 index 0000000..5528eb9 --- /dev/null +++ b/tests/anytone/roundtrip.c @@ -0,0 +1,98 @@ +#include +#include + +#include "image.h" +#include "codeplug.h" + +int main(int argc, char **argv) +{ + struct at_image img; + struct at_codeplug cp; + struct at_codeplug parsed; + char *json = NULL; + int rc = EXIT_FAILURE; + + if (argc != 5) { + fprintf(stderr, + "Usage: %s original copy direct json\n", + argv[0]); + return EXIT_FAILURE; + } + + at_image_init(&img); + at_codeplug_init(&cp); + at_codeplug_init(&parsed); + + if (at_image_load(&img, argv[1]) != 0) + goto done; + + /* Test A: binary load and save. */ + + if (at_image_save(&img, argv[2]) != 0) + goto done; + + puts("Test A: output generated"); + + /* Decode the original image. */ + + if (at_codeplug_from_image(&cp, &img) != 0) + goto done; + + printf("Decoded channels: %d\n", cp.nchannels); + printf("Decoded contacts: %d\n", cp.ncontacts); + printf("Decoded radio IDs: %d\n", cp.nradio_ids); + printf("Decoded zones: %d\n", cp.nzones); + printf("Decoded scans: %d\n", cp.nscan_lists); + printf("Decoded RX groups: %d\n", cp.nrx_groups); + + /* Prepare JSON representation. */ + + json = at_codeplug_to_json(&cp); + + if (!json) + goto done; + + if (at_codeplug_from_json(&parsed, json) != 0) + goto done; + + /* Test B: direct structured round trip. */ + + if (at_codeplug_to_image(&cp, &img) != 0) + goto done; + + if (at_image_save(&img, argv[3]) != 0) + goto done; + + puts("Test B: output generated"); + + /* Reload the unmodified original for Test C. */ + + at_image_free(&img); + at_image_init(&img); + + if (at_image_load(&img, argv[1]) != 0) + goto done; + + /* Test C: JSON round trip. */ + + if (at_codeplug_to_image(&parsed, &img) != 0) + goto done; + + if (at_image_save(&img, argv[4]) != 0) + goto done; + + puts("Test C: output generated"); + + rc = EXIT_SUCCESS; + +done: + free(json); + at_codeplug_free(&parsed); + at_codeplug_free(&cp); + at_image_free(&img); + + if (rc != EXIT_SUCCESS) + fprintf(stderr, "Regression harness failed\n"); + + return rc; +} diff --git a/tests/anytone/run-roundtrip.sh b/tests/anytone/run-roundtrip.sh new file mode 100755 index 0000000..7b8aa5b --- /dev/null +++ b/tests/anytone/run-roundtrip.sh @@ -0,0 +1,68 @@ +#!/bin/sh + +set -eu +umask 077 + +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) +cd "$ROOT" + +WORK=$(mktemp -d) +trap 'rm -rf -- "$WORK"' EXIT +trap 'exit 1' HUP INT TERM + +printf '\n=== Build CodeplugStudio ===\n' + +make -j2 + +printf '\n=== Generate synthetic ATCP fixture ===\n' + +perl tests/anytone/generate-fixture.pl \ + "$WORK/synthetic.atcp" + +chmod 400 "$WORK/synthetic.atcp" + +printf '\n=== Compile regression harness ===\n' + +${CC:-cc} \ + -O2 -g -Wall -Wextra -Wpedantic -std=c11 \ + -Icommon/include \ + -Ithird_party \ + -Ibackends/anytone/include \ + -Ibackends/anytone \ + tests/anytone/roundtrip.c \ + common/src/*.o \ + backends/anytone/*.o \ + backends/nicfw/*.o \ + backends/tyt/*.o \ + third_party/cJSON.o \ + -lm -pthread \ + $(pkg-config --libs libusb-1.0) \ + -o "$WORK/roundtrip" + +printf '\n=== Execute offline regression ===\n' + +"$WORK/roundtrip" \ + "$WORK/synthetic.atcp" \ + "$WORK/binary.atcp" \ + "$WORK/direct.atcp" \ + "$WORK/json.atcp" + +printf '\n=== Verify byte-identical round trips ===\n' + +FAILED=0 + +for MODE in binary direct json; do + if cmp -s "$WORK/synthetic.atcp" "$WORK/$MODE.atcp"; then + printf '%s: PASS\n' "$MODE" + else + printf '%s: FAIL\n' "$MODE" + FAILED=1 + fi +done + +if [ "$FAILED" -ne 0 ]; then + printf '\nAnyTone round-trip regression: FAIL\n' + exit 1 +fi + +printf '\nAnyTone round-trip regression: PASS\n'