Decode an encoded number between 1 and 2^31 - 1 inclusive. * When successful returns the decoded result. * If the decoded value would be too large, 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' is returned. * If more bits are needed than available in the 'stream', 'SIMPLICITY_ERR_BITSTRING_EOF' is returned. * If an I/O error occurs when reading from the 'stream', 'SIMPLICITY_ERR_BISTRING_ERROR' is return
| 185 | * Precondition: NULL != stream |
| 186 | */ |
| 187 | int32_t simplicity_decodeUptoMaxInt(bitstream* stream) { |
| 188 | int32_t bit = read1Bit(stream); |
| 189 | if (bit < 0) return bit; |
| 190 | if (0 == bit) { |
| 191 | return 1; |
| 192 | } else { |
| 193 | int32_t n = decodeUpto65535(stream); |
| 194 | if (n < 0) return n; |
| 195 | if (30 < n) return SIMPLICITY_ERR_DATA_OUT_OF_RANGE; |
| 196 | { |
| 197 | int32_t result = simplicity_readNBits(n, stream); |
| 198 | if (result < 0) return result; |
| 199 | return ((1 << n) | result); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* Fills a 'bitstring' containing 'n' bits from 'stream'. |
| 205 | * Returns 'SIMPLICITY_ERR_BITSTREAM_EOF' if not enough bits are available. |