A C decoder receives an unsigned char buffer p containing n bytes. Its first two bytes encode an unsigned 16-bit payload length in big-endian order; the payload follows immediately. Assume 8-bit bytes, an available uint32_t type, and that n and the decoded length L have type size_t. Select every portable and valid claim.
After n >= 2, *(const uint16_t *)p portably reconstructs the length on every host.
After n >= 2, checking L <= n establishes that all declared payload bytes are present.
After n >= 2, ((uint32_t)p[0] << 8) | (uint32_t)p[1] reconstructs the length.
After n >= 2, checking L <= n - 2 establishes payload availability without addition overflow.
Unlock full course notes by signing up
Decoding counters from two byte orders
A migration tool reads unsigned 16-bit counters from two instruments. Instrument A stores counters in big-endian order, and instrument B uses little-endian order. Each byte has 8 bits. In the table, bytes are written in hexadecimal, and the first byte shown occurs first in the file. Complete the decoded decimal values using exact integers.
Publishing only complete decoded objects
A configuration decoder must preserve the current configuration if a serialized replacement is invalid. The fixed header contains a version and a payload length whose meaning depends on that version. The decoder's design requires header validation before length interpretation, length checks before allocation, and complete payload validation before publication. Arrange its stages in execution order. Do not publish a partly decoded object.
Escaping reserved payload bytes once
A device serializes binary messages using byte escaping, a reversible replacement of reserved payload bytes with special byte sequences. Every byte below is written in hexadecimal.
The format reserves byte 7E as a message delimiter and byte 7D as an escape marker. Apply these rules to each original payload byte exactly once:
Replace 7E with 7D 5E.