diff --git a/README.md b/README.md index 0ea78c2..7e1fa40 100644 --- a/README.md +++ b/README.md @@ -729,9 +729,9 @@ measured ±0 B or worse. | Architecture | .text | .data | .bss | | - | - | - | - | -| ARMv6-m | ~3.9KB | 0.0KB | 0.0KB | -| ARMv7-m+fp.dp | ~3.9KB | 0.0KB | 0.0KB | -| RV32IMC | ~5.0KB | 0.0KB | 0.0KB | +| ARMv6-m | ~3.8KB | 0.0KB | 0.0KB | +| ARMv7-m+fp.dp | ~3.8KB | 0.0KB | 0.0KB | +| RV32IMC | ~4.9KB | 0.0KB | 0.0KB | | atmega8 | ~7.4KB | 0.0KB | 0.0KB | **Full configuration, strict UTF-8 on** — the only rows where the validator @@ -741,14 +741,14 @@ measured ±0 B or worse. | Architecture | .text | .data | .bss | | - | - | - | - | | ARMv6-m | ~4.1KB | 0.0KB | 0.0KB | -| ARMv7-m+fp.dp | ~4.1KB | 0.0KB | 0.0KB | +| ARMv7-m+fp.dp | ~4.0KB | 0.0KB | 0.0KB | | RV32IMC | ~5.2KB | 0.0KB | 0.0KB | | atmega8 | ~7.8KB | 0.0KB | 0.0KB | The [hold-back framing](#sequence-framing-and-the-hold-back-window) is part of those *Full* rows: 276 B on ARMv6-m, 398 B on atmega8. A pure-C consumer encoding only through `sofab_object_encode()` takes it back out with -`SOFAB_DISABLE_LAZY_SEQ_SUPPORT` — ARMv6-m returns to 3684 B, `sofab_ostream_t` +`SOFAB_DISABLE_LAZY_SEQ_SUPPORT` — ARMv6-m returns to 3662 B, `sofab_ostream_t` shrinks from 56 B to 20 B per stream, and a typical encode drops 6 Ir/op. The *Minimal* rows below disable sequences outright and are unaffected. @@ -785,19 +785,19 @@ same [`tools/footprint.sh`](tools/footprint.sh): | Switch | `.text` | delta | | - | -: | -: | -| *(full, the baseline)* | 3960 B | — | -| `SOFAB_DISABLE_OBJECT_API` | 2282 B | **−1678 B** | -| `SOFAB_DISABLE_ARRAY_SUPPORT` | 2918 B | **−1042 B** | -| `SOFAB_DISABLE_SEQUENCE_SUPPORT` | 3026 B | −934 B | -| `SOFAB_DISABLE_FIXLEN_SUPPORT` | 3148 B | −812 B | -| `SOFAB_DISABLE_INT64_SUPPORT` | 3646 B | −314 B | -| `SOFAB_DISABLE_LAZY_SEQ_SUPPORT` | 3684 B | −276 B | -| `SOFAB_DISABLE_INTEGER_OVERFLOW_CHECK` | 3886 B | −74 B | -| `SOFAB_DISABLE_FP64_SUPPORT` | 3918 B | −42 B | -| `SOFAB_OBJECT_DESCR_PROFILE=…_BIG` | 3962 B | +2 B | -| `SOFAB_ENABLE_SKIP_COUNTER` | 3978 B | +18 B | -| `SOFAB_OBJECT_DESCR_PROFILE=…_SMALL` | 3982 B | +22 B | -| `SOFAB_ENABLE_STRICT_UTF8` | 4206 B | +246 B | +| *(full, the baseline)* | 3938 B | — | +| `SOFAB_DISABLE_OBJECT_API` | 2282 B | **−1656 B** | +| `SOFAB_DISABLE_ARRAY_SUPPORT` | 2972 B | **−966 B** | +| `SOFAB_DISABLE_SEQUENCE_SUPPORT` | 3002 B | −936 B | +| `SOFAB_DISABLE_FIXLEN_SUPPORT` | 3152 B | −786 B | +| `SOFAB_DISABLE_INT64_SUPPORT` | 3624 B | −314 B | +| `SOFAB_DISABLE_LAZY_SEQ_SUPPORT` | 3662 B | −276 B | +| `SOFAB_DISABLE_INTEGER_OVERFLOW_CHECK` | 3864 B | −74 B | +| `SOFAB_DISABLE_FP64_SUPPORT` | 3896 B | −42 B | +| `SOFAB_OBJECT_DESCR_PROFILE=…_BIG` | 3940 B | +2 B | +| `SOFAB_OBJECT_DESCR_PROFILE=…_SMALL` | 3954 B | +16 B | +| `SOFAB_ENABLE_SKIP_COUNTER` | 3956 B | +18 B | +| `SOFAB_ENABLE_STRICT_UTF8` | 4184 B | +246 B | Three rows need a word: diff --git a/src/object.c b/src/object.c index 2cc8bb0..9ac922e 100644 --- a/src/object.c +++ b/src/object.c @@ -608,29 +608,35 @@ static int _field_is_default ( /* No default image: the implicit default is the empty string. */ return field->size == 0 || s[0] == '\0'; } - - if (field->type == SOFAB_OBJECT_FIELDTYPE_BLOB && field->nested_idx != 0) - { - /* Sized blob: the logical default is an empty blob (used_len == 0), - * mirroring the empty-string rule above. The buffer bytes are - * indeterminate and must not influence the decision. used_len sits - * immediately before the buffer (nested_idx bytes wide). */ - return _load_uint(CAST_TO(const void *, src, field->offset - field->nested_idx), - field->nested_idx) == 0; - } #endif -#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT) +#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) || !defined(SOFAB_DISABLE_ARRAY_SUPPORT) { uint8_t width = _sized_width(field); - if (width != 0 && field->type != SOFAB_OBJECT_FIELDTYPE_BLOB) + if (width != 0) { - /* Sized array: length first (§3 -- the length is the value), then the - * used prefix against the default image. Without a default image the - * logical default is the empty array. */ + /* Sized blob / sized array: length first (§3 -- the length is the + * value), then the used prefix against the default image. A blob is + * an array of one-byte elements here. Without a default image the + * logical default is the empty value. The buffer past the used length + * is indeterminate and never influences the decision. + * + * The 1 a blob keeps is load-bearing, not shorthand: for a BLOB the + * descriptor macros record sizeof(buffer) & 0xF in element_size -- the + * CAPACITY, not an element width -- which is 0 at a capacity of 16, so + * reading it here would divide by zero. An #if rather than a ternary + * because it also pays: with arrays disabled the blob is the only kind + * that reaches this block, esz folds to a constant, and neither the + * division (a libgcc call on a core without one) nor the multiply is + * emitted at all. */ + size_t esz = 1; +#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT) + if (field->type != SOFAB_OBJECT_FIELDTYPE_BLOB) + esz = field->element_size; +#endif uint64_t used = _load_uint( CAST_TO(const void *, src, field->offset - width), width); - size_t cap = field->size / field->element_size; + size_t cap = field->size / esz; if (used > (uint64_t)cap) used = (uint64_t)cap; if (defaults == NULL) return used == 0; @@ -639,10 +645,10 @@ static int _field_is_default ( width) != used) return 0; return memcmp(CAST_TO(const void *, defaults, field->offset), val, - (size_t)used * field->element_size) == 0; + (size_t)used * esz) == 0; } } -#endif /* !defined(SOFAB_DISABLE_ARRAY_SUPPORT) */ +#endif /* fixlen or array support */ if (defaults != NULL) { diff --git a/test/c/test_object.c b/test/c/test_object.c index 59532c0..d393dda 100644 --- a/test/c/test_object.c +++ b/test/c/test_object.c @@ -1420,6 +1420,194 @@ static size_t blobsized_encode (const blobsized_t *in, uint8_t *buf, size_t bufl return sofab_ostream_flush(&octx); } +/* + * A sized blob WITH a non-empty declared default: the used length is compared + * against the default image's length and the used prefix against its bytes, like + * a sized array (MESSAGE_SPEC §2). The default is {'H','i'}. + */ +static const blobsized_t _info_defaults_blobsized = + { .used_len = 2, .data = { 'H', 'i' } }; + +static const sofab_object_descr_t _info_blobsized_with_default = + SOFAB_OBJECT_DESCR_WITH_DEFAULTS(_info_fields_blobsized, 1, NULL, 0, + &_info_defaults_blobsized); + +/* Descriptor-driven, so the two blob layouts below (capacity 8 and 16) share it; + * sofab_object_encode takes the object as a void * for the same reason. */ +static size_t blobsized_def_encode (const sofab_object_descr_t *info, + const void *in, uint8_t *buf, size_t n) +{ + sofab_ostream_t octx; + sofab_ostream_init(&octx, buf, n, 0, NULL, NULL); + TEST_ASSERT_EQUAL_MESSAGE(SOFAB_RET_OK, + sofab_object_encode(&octx, info, in), "encode failed"); + return sofab_ostream_flush(&octx); +} + +static void blobsized_def_decode (const sofab_object_descr_t *info, + void *out, const uint8_t *buf, size_t n) +{ + sofab_istream_t ictx; + sofab_object_decoder_t dec; + memset(&dec, 0, sizeof(dec)); + dec.info = info; + dec.dst = (uint8_t *)out; + sofab_istream_init(&ictx, sofab_object_field_cb, &dec); + TEST_ASSERT_EQUAL_MESSAGE(SOFAB_RET_OK, sofab_istream_feed(&ictx, buf, n), + "decode failed"); +} + +static void test_object_blob_sized_default_nonempty (void) +{ + uint8_t buf[32]; + blobsized_t in, out; + + /* 1. left at its default: omitted. */ + sofab_object_init(&_info_blobsized_with_default, &in); + TEST_ASSERT_EQUAL_UINT8(2, in.used_len); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, + blobsized_def_encode(&_info_blobsized_with_default, &in, buf, sizeof(buf)), + "a sized blob equal to its non-empty default must be omitted"); + + /* 2. same length, different bytes: written. */ + in.data[1] = 'o'; + size_t n = blobsized_def_encode(&_info_blobsized_with_default, &in, buf, sizeof(buf)); + const uint8_t exp_ho[] = { 0x02, 0x13, 'H', 'o' }; + TEST_ASSERT_EQUAL_size_t(sizeof(exp_ho), n); + TEST_ASSERT_EQUAL_UINT8_ARRAY(exp_ho, buf, n); + + /* 3. a prefix of the default -- same bytes, shorter length: written, because + * the length is part of the value (§3). */ + sofab_object_init(&_info_blobsized_with_default, &in); + in.used_len = 1; /* "H": a prefix of the default */ + n = blobsized_def_encode(&_info_blobsized_with_default, &in, buf, sizeof(buf)); + const uint8_t exp_h[] = { 0x02, 0x0B, 'H' }; + TEST_ASSERT_EQUAL_size_t(sizeof(exp_h), n); + TEST_ASSERT_EQUAL_UINT8_ARRAY(exp_h, buf, n); + + /* 4. absent decodes to the default; a written value round-trips. */ + sofab_object_init(&_info_blobsized_with_default, &out); + blobsized_def_decode(&_info_blobsized_with_default, &out, buf, 0); + TEST_ASSERT_EQUAL_UINT8(2, out.used_len); + TEST_ASSERT_EQUAL_UINT8_ARRAY("Hi", out.data, 2); + + sofab_object_init(&_info_blobsized_with_default, &in); + in.data[1] = 'o'; + n = blobsized_def_encode(&_info_blobsized_with_default, &in, buf, sizeof(buf)); + sofab_object_init(&_info_blobsized_with_default, &out); + blobsized_def_decode(&_info_blobsized_with_default, &out, buf, n); + TEST_ASSERT_EQUAL_UINT8(2, out.used_len); + TEST_ASSERT_EQUAL_UINT8_ARRAY("Ho", out.data, 2); +} + +static void test_object_blob_sized_default_explicit_empty (void) +{ + uint8_t buf[32]; + blobsized_t in, out; + size_t n; + + /* explicit empty over a non-empty default: written as an empty blob, and + * it decodes back as empty (not as the default). */ + sofab_object_init(&_info_blobsized_with_default, &in); + in.used_len = 0; + n = blobsized_def_encode(&_info_blobsized_with_default, &in, buf, sizeof(buf)); + const uint8_t exp_empty[] = { 0x02, 0x03 }; + TEST_ASSERT_EQUAL_size_t_MESSAGE(sizeof(exp_empty), n, + "an explicit empty blob over a non-empty default must be written"); + TEST_ASSERT_EQUAL_UINT8_ARRAY(exp_empty, buf, n); + + sofab_object_init(&_info_blobsized_with_default, &out); + blobsized_def_decode(&_info_blobsized_with_default, &out, buf, n); + TEST_ASSERT_EQUAL_UINT8_MESSAGE(0, out.used_len, "empty must decode as empty"); +} + +static void test_object_blob_sized_no_default_image_unchanged (void) +{ + uint8_t buf[32]; + blobsized_t in; + + /* No default image: the logical default stays the empty blob. */ + sofab_object_init(&_info_blobsized, &in); + in.data[0] = 0x55; /* dirty buffer, length 0 */ + TEST_ASSERT_EQUAL_size_t(0, blobsized_encode(&in, buf, sizeof(buf))); + + in.used_len = 1; + const uint8_t exp[] = { 0x02, 0x0B, 0x55 }; + size_t n = blobsized_encode(&in, buf, sizeof(buf)); + TEST_ASSERT_EQUAL_size_t(sizeof(exp), n); + TEST_ASSERT_EQUAL_UINT8_ARRAY(exp, buf, n); + + /* A default image whose blob is empty behaves the same as none. */ + static const blobsized_t empty_def = { .used_len = 0 }; + static const sofab_object_descr_t info_empty_def = + SOFAB_OBJECT_DESCR_WITH_DEFAULTS(_info_fields_blobsized, 1, NULL, 0, &empty_def); + sofab_object_init(&info_empty_def, &in); + TEST_ASSERT_EQUAL_size_t(0, + blobsized_def_encode(&info_empty_def, &in, buf, sizeof(buf))); + in.used_len = 1; in.data[0] = 0x55; + TEST_ASSERT_EQUAL_size_t(sizeof(exp), + blobsized_def_encode(&info_empty_def, &in, buf, sizeof(buf))); +} + +/* + * Capacity 16: the descriptor records sizeof(data) & 0xF == 0 in element_size, so + * a sized blob that read element_size to derive its capacity would divide by zero + * here. The default-image comparison must stay defined and byte-exact. + */ +typedef struct +{ + uint8_t used_len; + uint8_t data[16]; +} blobsized_cap16_t; + +static const sofab_object_descr_field_t _info_fields_blobsized_cap16[] = +{ + SOFAB_OBJECT_FIELD_BLOB_SIZED(0, blobsized_cap16_t, data, used_len), +}; + +static const blobsized_cap16_t _info_defaults_blobsized_cap16 = + { .used_len = 3, .data = { 'a', 'b', 'c' } }; + +static const sofab_object_descr_t _info_blobsized_cap16 = + SOFAB_OBJECT_DESCR_WITH_DEFAULTS(_info_fields_blobsized_cap16, 1, NULL, 0, + &_info_defaults_blobsized_cap16); + +static void test_object_blob_sized_default_capacity_16 (void) +{ + uint8_t buf[32]; + blobsized_cap16_t in; + size_t n; + + /* at its default: omitted, and the capacity division is well-defined. */ + sofab_object_init(&_info_blobsized_cap16, &in); + TEST_ASSERT_EQUAL_UINT8(3, in.used_len); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, + blobsized_def_encode(&_info_blobsized_cap16, &in, buf, sizeof(buf)), + "a capacity-16 blob at its default must be omitted"); + + /* explicit empty over that default: written. */ + in.used_len = 0; + n = blobsized_def_encode(&_info_blobsized_cap16, &in, buf, sizeof(buf)); + const uint8_t exp_empty[] = { 0x02, 0x03 }; + TEST_ASSERT_EQUAL_size_t(sizeof(exp_empty), n); + TEST_ASSERT_EQUAL_UINT8_ARRAY(exp_empty, buf, n); + + /* a full-capacity value is compared -- and carried -- over all 16 bytes, not + * over 16 % 16 == 0 of them. Round-tripped rather than matched against a + * literal, so the test does not restate the length varint's width. */ + blobsized_cap16_t out; + sofab_object_init(&_info_blobsized_cap16, &in); + for (uint8_t i = 0; i < 16; i++) in.data[i] = (uint8_t)(0x40 + i); + in.used_len = 16; + n = blobsized_def_encode(&_info_blobsized_cap16, &in, buf, sizeof(buf)); + TEST_ASSERT_GREATER_THAN_size_t(16, n); + + sofab_object_init(&_info_blobsized_cap16, &out); + blobsized_def_decode(&_info_blobsized_cap16, &out, buf, n); + TEST_ASSERT_EQUAL_UINT8(16, out.used_len); + TEST_ASSERT_EQUAL_UINT8_ARRAY(in.data, out.data, 16); +} + static void test_object_blob_sized (void) { uint8_t buf[32]; @@ -3950,6 +4138,10 @@ int test_object_main (void) RUN_TEST(test_object_roundtrip_empty_sequence_before_sequence); RUN_TEST(test_object_string_default_omission); RUN_TEST(test_object_blob_sized); + RUN_TEST(test_object_blob_sized_default_nonempty); + RUN_TEST(test_object_blob_sized_default_explicit_empty); + RUN_TEST(test_object_blob_sized_no_default_image_unchanged); + RUN_TEST(test_object_blob_sized_default_capacity_16); RUN_TEST(test_object_boolean_tolerant_decode); RUN_TEST(test_object_boolean_roundtrip);