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

Function simplicity_decodeUptoMaxInt

src/simplicity/bitstream.c:187–202  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

185 * Precondition: NULL != stream
186 */
187int32_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.

Callers 3

test_decodeUptoMaxIntFunction · 0.85
decodeNodeFunction · 0.85

Calls 3

read1BitFunction · 0.85
decodeUpto65535Function · 0.85
simplicity_readNBitsFunction · 0.85

Tested by 1

test_decodeUptoMaxIntFunction · 0.68