Skip to content
Merged
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
38 changes: 29 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,44 @@ jobs:
- name: Test
run: meson test -C build --verbose

- name: Compare with the unmodified 2.4.0 codec
run: |
# Pin the pre-extension source. Never replace it with the PR head.
legacy_commit=fecf8778423c353a54b7387aa0954887a75fbe49
git fetch --no-tags --depth=1 origin "$legacy_commit"
mkdir -p "$RUNNER_TEMP/ppack-legacy"
git archive "$legacy_commit" include src | tar -x -C "$RUNNER_TEMP/ppack-legacy"
meson setup build_compat tests/compatibility \
--buildtype=debug \
"-Dlegacy_root=$RUNNER_TEMP/ppack-legacy" \
-Db_sanitize=address,undefined \
-Dc_args=-fsanitize=float-cast-overflow \
-Dc_link_args=-fsanitize=float-cast-overflow
meson test -C build_compat --verbose

- name: Upload test log
if: failure()
uses: actions/upload-artifact@v4
with:
name: testlog-${{ matrix.os }}
path: build/meson-logs/testlog.txt
path: |
build/meson-logs/testlog.txt
build_compat/meson-logs/testlog.txt

# ---------------------------------------------------------------------------
# Exercise the PPACK_PAYLOAD_BITS override path. The library and tests
# must build and pass at the documented 512-bit ceiling (CAN-FD frame
# data field). Locks in that no future change accidentally hard-codes
# an assumption tying the convenience-macro buffer size to 64 bits.
# Exercise the buffer-size override at CAN-FD size and at the codec's
# maximum. Runtime sizes must remain independent of this default.
# ---------------------------------------------------------------------------
override-test:
name: "Override test (PPACK_PAYLOAD_BITS=512)"
name: "Override test (PPACK_PAYLOAD_BITS=${{ matrix.bits }})"
runs-on: ubuntu-latest
needs: test

strategy:
fail-fast: false
matrix:
bits: [512, 65528]

steps:
- uses: actions/checkout@v4

Expand All @@ -87,13 +107,13 @@ jobs:
- name: Install meson
run: pip install meson

- name: Configure (debug + sanitizers + 512-bit override)
- name: Configure (debug + sanitizers + buffer-size override)
run: >
meson setup build
--buildtype=debug
-Dbuild_tests=true
-Db_sanitize=address,undefined
"-Dc_args=-DPPACK_PAYLOAD_BITS=512 -fsanitize=float-cast-overflow"
"-Dc_args=-DPPACK_PAYLOAD_BITS=${{ matrix.bits }} -fsanitize=float-cast-overflow"
-Dc_link_args=-fsanitize=float-cast-overflow

- name: Build
Expand All @@ -106,7 +126,7 @@ jobs:
if: failure()
uses: actions/upload-artifact@v4
with:
name: testlog-override
name: testlog-override-${{ matrix.bits }}
path: build/meson-logs/testlog.txt

# ---------------------------------------------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [2.5.0] - 2026-09-08

### Changed

- Raised the payload limit from 512 to 65,528 bits, or 8,191 logical octets.
The public `PPACK_MAX_PAYLOAD_BITS` constant defines the runtime and
compile-time maximum. The default buffer size remains 64 bits.
- Retained function signatures, field-descriptor layout, 32-bit field widths,
and the packed representation of existing fields.

Callers must provide enough payload storage and enforce transport-specific
limits. Valid sizes above 512 bits no longer fail before buffer access.
The codec still permits partial output on errors. CRC, record publication,
and format-version policy remain the consumer's responsibility.

### Fixed

- Changed field-range validation to ordered subtraction. This prevents
malformed high offsets from wrapping the check on 16-bit `size_t` targets.

### Added

- Independent bit-at-a-time reference tests for every legal start/width pair,
every payload size, high-offset mixed types, and boundary value patterns.
- Guard, clearing, input-preservation, and malformed-geometry tests for both
native and simulated 16-bit-MAU storage.
- Compile-time configuration checks and a maximum-size CI override build.
- A [verification record](docs/large-payload-verification.md) with the range
argument, compatibility checks, and target-compilation scope.

## [2.4.0] - 2026-07-03

### Fixed
Expand Down
12 changes: 8 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ meson setup build --buildtype=debug -Dbuild_tests=true \
meson compile -C build
meson test -C build --verbose

# Coverage (CI gate is 80% line + 70% branch)
# Coverage (CI gate is 100% line + 100% branch)
meson setup build_cov --buildtype=debug -Dbuild_tests=true \
-Db_coverage=true
meson compile -C build_cov && meson test -C build_cov
Expand Down Expand Up @@ -58,11 +58,15 @@ The library is analysed with `misch` (cppcheck-backed MISRA C:2023 analysis), co
- Add a test for every new feature.
- All tests must pass on both build configurations: 8-bit native and
16-bit MAU simulated (`-DPPACK_SIMULATE_16BIT_MAU`).
- CI enforces an 80% line / 70% branch coverage gate. New code without
- CI enforces a 100% line / 100% branch coverage gate. New code without
tests will fail the coverage gate.
- Tests live in `tests/test_*.c`.
- Fuzz seeds are fixed (`0xBEEF000N`) so failures are reproducible from
the test name alone. Do not introduce non-deterministic tests.
- Fuzz seeds are fixed and recorded in each test. Failures must be
reproducible from the test name alone. Do not use non-deterministic tests.

For payload-size changes, also run the independent geometry sweep and the
legacy comparison. See [large-payload verification](docs/large-payload-verification.md)
for commands and the limits of these checks.

## Wire format and API stability

Expand Down
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A generic payload serialisation library for bit-aligned data fields in C.
## Features

- **Bit-aligned fields** - Fields can start at any bit position and span arbitrary bit ranges
- **Variable payload size** - Caller supplies the payload size in bits (multiple of 8, up to 512); supports CAN classic (64) and CAN-FD (up to 512)
- **Variable payload size** - Caller supplies a multiple of 8 bits, up to 65,528 bits. CAN classic and CAN-FD sizes remain supported.
- **No dynamic memory** - Fixed-size operations, no `malloc` / `free`
- **Deterministic WCET** - All operations have bounded execution time
- **Scaled fields** - Linear scale/offset transformations for physical-unit encoding
Expand Down Expand Up @@ -170,6 +170,37 @@ Or, if every payload in your project is 512 bits, set
`ppack_byte_t payload[PPACK_PAYLOAD_UNITS]`. The runtime API still
takes the size explicitly.

### Larger stored records

Stored records can use up to `PPACK_MAX_PAYLOAD_BITS`, which is 65,528 bits
or 8,191 logical octets. Individual fields remain limited to 32 bits.
For example, a 1,024-bit record needs 128 payload storage units:

```c
ppack_byte_t record[1024u / PPACK_ADDR_UNIT_BITS];
int ret = ppack_pack(&parameters, record, 1024u,
parameter_fields, parameter_field_count);
```

Increasing `payload_bits` does not resize a buffer. Allocate at least
`payload_bits / 8` elements of `ppack_byte_t` before calling the codec.
Each logical octet occupies one 16-bit storage unit on a 16-bit MAU target.
The default `PPACK_PAYLOAD_BITS` remains 64. Do not allocate the maximum
on a small stack unless that space is available.

The codec no longer rejects valid sizes above 512 bits. Callers must enforce
CAN-FD and other transport limits before calling it. Audit callers that relied
on the old rejection to protect a smaller buffer.

The packed representation of existing fields does not change. The codec does
not provide CRC, schema versioning, or atomic record updates. Decode into a
temporary structure and publish it only after a successful return. On failure,
pack and unpack can leave partial output. Record-format changes still require
the consumer's version and recovery policy.

See [large-payload verification](docs/large-payload-verification.md) for the
range argument, test coverage, and remaining caller obligations.

## Building

```sh
Expand Down Expand Up @@ -212,7 +243,10 @@ int ppack_unpack(void *base_ptr, const void *payload, size_t payload_bits,
const struct ppack_field *fields, size_t field_count);
```

`payload_bits` is the payload size in bits. Must be a positive multiple of `PPACK_ADDR_UNIT_BITS` (always 8) and no greater than 512 (CAN-FD frame data field). Common values: `64` for CAN classic, `512` for full CAN-FD frames.
`payload_bits` is the payload size in bits. It must be a positive multiple
of `PPACK_ADDR_UNIT_BITS`, which is always 8. The maximum is
`PPACK_MAX_PAYLOAD_BITS`, or 65,528. Common values are `64` for CAN classic
and `512` for full CAN-FD frames. The caller must enforce transport limits.

### Field Descriptor

Expand Down Expand Up @@ -247,7 +281,7 @@ For `PPACK_BEHAVIOUR_SCALED` fields the struct member must always be `float`, re
| Code | Value | Meaning |
| -------------------- | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `PPACK_SUCCESS` | 0 | Operation succeeded |
| `PPACK_ERR_INVALARG` | 1 | Invalid argument (NULL fields, zero field count, `bit_length` out of range, `payload_bits` zero or not a multiple of `PPACK_ADDR_UNIT_BITS` or > 512, scaling requested for `PPACK_TYPE_UINT8`) |
| `PPACK_ERR_INVALARG` | 1 | Invalid argument (NULL fields, zero field count, `bit_length` out of range, `payload_bits` zero or not a multiple of `PPACK_ADDR_UNIT_BITS` or > `PPACK_MAX_PAYLOAD_BITS`, scaling requested for `PPACK_TYPE_UINT8`) |
| `PPACK_ERR_NOTFOUND` | 3 | Unknown field type |
| `PPACK_ERR_NULLPTR` | 4 | NULL pointer passed for `base_ptr` or `payload` |
| `PPACK_ERR_OVERFLOW` | 5 | `start_bit + bit_length > payload_bits`, or `scale == 0` on a `SCALED` field |
Expand All @@ -256,7 +290,9 @@ Functions return the negated error code on failure (e.g. `-PPACK_ERR_NULLPTR`).

## Wire Format

The payload is `payload_bits` bits long (a multiple of 8, between 8 and 512), addressed as `payload_bits / 8` logical bytes numbered 0 to `payload_bits / 8 - 1`.
The payload contains `payload_bits` bits, a multiple of 8 between 8 and 65,528.
Its `payload_bits / 8` logical bytes are numbered from 0 to
`payload_bits / 8 - 1`.

- **Bit numbering**: payload bit `N` lives in logical byte `N / 8`, at position `N mod 8` within that byte.
- **Within a byte**: bit 0 is the least significant bit.
Expand Down Expand Up @@ -338,7 +374,7 @@ All public APIs validate arguments at the function boundary.

- Passing `NULL` for any pointer returns `-PPACK_ERR_NULLPTR`.
- Passing `NULL` or an empty `fields` array returns `-PPACK_ERR_INVALARG`.
- A `payload_bits` argument that is zero, not a multiple of `PPACK_ADDR_UNIT_BITS`, or greater than 512 returns `-PPACK_ERR_INVALARG`.
- A `payload_bits` argument that is zero, not a multiple of 8, or greater than `PPACK_MAX_PAYLOAD_BITS` returns `-PPACK_ERR_INVALARG`.
- A field with `bit_length == 0` or `bit_length > 32` returns `-PPACK_ERR_INVALARG`.
- A field where `start_bit + bit_length > payload_bits` returns `-PPACK_ERR_OVERFLOW`.
- A `PPACK_BEHAVIOUR_SCALED` field with `scale == 0.0` returns `-PPACK_ERR_OVERFLOW`.
Expand All @@ -360,7 +396,7 @@ ppack is a serialisation primitive only. The following are explicitly out of sco

- **No integrity checks**: no CRC, checksum, or framing. Caller (or the transport, e.g. the CAN frame CRC) is responsible for detecting bit errors.
- **No schema versioning**: the field-descriptor layout is the schema. Maintain it in shared headers across nodes.
- **Payload size capped at 512 bits**: matches a full CAN-FD frame data field. Larger payloads require multiple ppack calls on chunks.
- **Payload size capped at 65,528 bits**: the bound retains 16-bit bit indices and supports targets with 16-bit `size_t`.
- **No multi-buffer / streaming API**: a single payload is processed in one call.
- **No runtime endianness adaptation**: the wire format is little-endian. Big-endian hosts would need explicit byte swapping at the boundary (no such host is currently a target).

Expand All @@ -370,7 +406,7 @@ ppack is a serialisation primitive only. The following are explicitly out of sco
| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Memory** | All operations use stack memory; no dynamic allocation |
| **MISRA C:2023** | Audited with `misch` (cppcheck-backed MISRA C:2023 analysis; configuration in `misra.toml`); the audit reports zero findings. Deviations are justified at point of use with `cppcheck-suppress ... @deviation` comments, or project-wide in `misra-deviations.txt`. The only required-rule deviations are three Rule 21.15 sites: the type-erased struct-member copies centralised in two internal helpers, and the deliberate F32 `float`/`uint32_t` pun. Design follows MISRA principles throughout: no dynamic allocation, no UB shifts, explicit error codes, `memcpy`-based type punning. |
| **Payload size** | Caller-supplied via `payload_bits` (multiple of 8, between 8 and 512). 64 matches CAN classic; 512 matches full CAN-FD. |
| **Payload size** | Caller-supplied via `payload_bits`, a multiple of 8 between 8 and 65,528. The default buffer size remains 64 bits. |
| **Bit ordering** | LSB-first within each byte; multi-byte fields little-endian; fields may span byte boundaries |
| **Field size** | 1-32 bits per field |
| **Thread safety** | Not thread-safe; caller must provide mutual exclusion when `base_ptr` or `payload` is shared across threads or ISRs |
Expand Down
Loading
Loading