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 */
| 11 | * Precondition: NULL != stream |
| 12 | */ |
| 13 | simplicity_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. |
no outgoing calls