MCPcopy Create free account
hub / github.com/ElementsProject/elements / simplicity_closeBitstream

Function simplicity_closeBitstream

src/simplicity/bitstream.c:13–24  ·  view source on GitHub ↗

Closes a bitstream by consuming all remaining bits. * Returns 'SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES' if CHAR_BIT or more bits remain in the stream. * Otherwise, returns 'SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING' if any remaining bits are non-zero. * Otherwise returns 'SIMPLICITY_NO_ERROR'. * * Precondition: NULL != stream */

Source from the content-addressed store, hash-verified

11 * Precondition: NULL != stream
12 */
13simplicity_err simplicity_closeBitstream(bitstream* stream) {
14 if (1 < stream->len) return SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES; /* If there is more than one byte remaining. */
15 if (1 == stream->len) {
16 if (0 == stream->offset) return SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES; /* If there is one byte remaining */
17 if (0 != (*stream->arr & (UCHAR_MAX >> stream->offset))) { /* If any of the unconsumed bits are non-zero */
18 return SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING;
19 }
20 }
21 /* Otherwise there are either 0 bits remaining or there are between 1 and CHAR_BITS-1 bits remaining and they are all zero. */
22 *stream = (bitstream){0};
23 return SIMPLICITY_NO_ERROR;
24}
25
26/* Fetches up to 31 bits from 'stream' as the 'n' least significant bits of return value.
27 * The 'n' bits are set from the MSB to the LSB.

Callers 6

test_hashBlockFunction · 0.85
test_programFunction · 0.85
exactBudget_testFunction · 0.85

Calls

no outgoing calls

Tested by 4

test_hashBlockFunction · 0.68
test_programFunction · 0.68
exactBudget_testFunction · 0.68