blob: 1c6cd829fe57046c83fa578bcfcf618d0f0f644c [file] [log] [blame]
Josh Coalson423f8042007-01-28 17:40:26 +00001/* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
36#include <stdlib.h> /* for malloc() */
37#include <string.h> /* for memcpy(), memset() */
38#if defined(_MSC_VER) && _MSC_VER <= 1200
39#include <winsock.h> /* for ntohl() */
40#else
41#include <netinet/in.h> /* for ntohl() */
42#endif
43#include "private/bitmath.h"
44#include "private/bitreader.h"
45#include "private/crc.h"
46#include "FLAC/assert.h"
47
48/*
49 * Along the way you will see two versions of some functions, selected
50 * by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more
51 * readable, and slow version, and the other is the same function
52 * where crucial parts have been manually inlined and are much faster.
53 *
54 */
55
56/* Things should be fastest when this matches the machine word size */
57/* WATCHOUT: if you change this you must also change the following #defines down to ALIGNED_UNARY_BITS below to match */
58/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
59/* also, some sections currently only have fast versions for 4 or 8 bytes per word */
60typedef FLAC__uint32 brword;
61#define FLAC__BYTES_PER_WORD 4
62#define FLAC__BITS_PER_WORD 32
63#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
64#define FLAC__WORD_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
65/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
66#if WORDS_BIGENDIAN
67#define SWAP_BE_WORD_TO_HOST(x) (x)
68#else
Josh Coalsonc85056b2007-02-04 02:57:48 +000069#ifdef _MSC_VER
70#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
71#else
Josh Coalson423f8042007-01-28 17:40:26 +000072#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
73#endif
Josh Coalsonc85056b2007-02-04 02:57:48 +000074#endif
Josh Coalson423f8042007-01-28 17:40:26 +000075/* counts the # of zero MSBs in a word */
76#define ALIGNED_UNARY_BITS(word) ( \
77 (word) <= 0xffff ? \
78 ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
79 ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
80)
81/* this alternate might be slightly faster on some systems/compilers: */
82#define ALIGNED_UNARY_BITS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
83
84
85/*
86 * This should be at least twice as large as the largest number of words
87 * required to represent any 'number' (in any encoding) you are going to
88 * read. With FLAC this is on the order of maybe a few hundred bits.
89 * If the buffer is smaller than that, the decoder won't be able to read
90 * in a whole number that is in a variable length encoding (e.g. Rice).
91 * But to be practical it should be at least 1K bytes.
92 *
93 * Increase this number to decrease the number of read callbacks, at the
94 * expense of using more memory. Or decrease for the reverse effect,
95 * keeping in mind the limit from the first paragraph. The optimal size
96 * also depends on the CPU cache size and other factors; some twiddling
97 * may be necessary to squeeze out the best performance.
98 */
99static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
100
101static const unsigned char byte_to_unary_table[] = {
102 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
103 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
104 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
105 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
106 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
107 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
118};
119
120#ifdef min
121#undef min
122#endif
123#define min(x,y) ((x)<(y)?(x):(y))
124#ifdef max
125#undef max
126#endif
127#define max(x,y) ((x)>(y)?(x):(y))
128
129/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
130#ifdef _MSC_VER
131#define FLAC__U64L(x) x
132#else
133#define FLAC__U64L(x) x##LLU
134#endif
135
136#ifndef FLaC__INLINE
137#define FLaC__INLINE
138#endif
139
140struct FLAC__BitReader {
141 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
142 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
143 brword *buffer;
144 unsigned capacity; /* in words */
145 unsigned words; /* # of completed words in buffer */
146 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
147 unsigned consumed_words, consumed_bits; /* #words+(#bits of head word) already consumed from the front of buffer */
148 unsigned read_crc16; /* the running frame CRC */
149 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
150 FLAC__BitReaderReadCallback read_callback;
151 void *client_data;
152};
153
Josh Coalsonc85056b2007-02-04 02:57:48 +0000154#ifdef _MSC_VER
155/* OPT: an MSVC built-in would be better */
156static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
157{
158 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
159 return (x>>16) | (x<<16);
160}
161#endif
162
Josh Coalson423f8042007-01-28 17:40:26 +0000163static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
164{
165 register unsigned crc = br->read_crc16;
166#if FLAC__BYTES_PER_WORD == 4
167 switch(br->crc16_align) {
168 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
169 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
170 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
171 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
172 }
173#elif FLAC__BYTES_PER_WORD == 8
174 switch(br->crc16_align) {
175 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
176 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
177 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
178 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
179 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
180 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
181 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
182 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
183 }
184#else
185 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
186 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
187 br->read_crc16 = crc;
188#endif
189 br->crc16_align = 0;
190}
191
192static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
193{
194 unsigned start, end;
195 size_t bytes;
196 FLAC__byte *target;
197
198 /* first shift the unconsumed buffer data toward the front as much as possible */
199 if(br->consumed_words > 0) {
200 start = br->consumed_words;
201 end = br->words + (br->bytes? 1:0);
202 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
203
204 br->words -= start;
205 br->consumed_words = 0;
206 }
207
208 /*
209 * set the target for reading, taking into account word alignment and endianness
210 */
211 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
212 if(bytes == 0)
213 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
214 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
215
216 /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
217 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
218 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
219 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
220 * ^^-------target, bytes=3
221 * on LE machines, have to byteswap the odd tail word so nothing is
222 * overwritten:
223 */
224#if WORDS_BIGENDIAN
225#else
226 if(br->bytes)
227 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
228#endif
229
230 /* now it looks like:
231 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
232 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
233 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
234 * ^^-------target, bytes=3
235 */
236
237 /* read in the data; note that the callback may return a smaller number of bytes */
238 if(!br->read_callback(target, &bytes, br->client_data))
239 return false;
240
241 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
242 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
243 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
244 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
245 * now have to byteswap on LE machines:
246 */
247#if WORDS_BIGENDIAN
248#else
249 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
250 for(start = br->words; start < end; start++)
251 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
252#endif
253
254 /* now it looks like:
255 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
256 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
257 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
258 * finally we'll update the reader values:
259 */
260 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
261 br->words = end / FLAC__BYTES_PER_WORD;
262 br->bytes = end % FLAC__BYTES_PER_WORD;
263
264 return true;
265}
266
267/***********************************************************************
268 *
269 * Class constructor/destructor
270 *
271 ***********************************************************************/
272
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000273FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000274{
275 FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
276
277 /* calloc() implies:
278 memset(br, 0, sizeof(FLAC__BitReader));
279 br->buffer = 0;
280 br->capacity = 0;
281 br->words = br->bytes = 0;
282 br->consumed_words = br->consumed_bits = 0;
283 br->read_callback = 0;
284 br->client_data = 0;
285 */
286 return br;
287}
288
289void FLAC__bitreader_delete(FLAC__BitReader *br)
290{
291 FLAC__ASSERT(0 != br);
292
293 FLAC__bitreader_free(br);
294 free(br);
295}
296
297/***********************************************************************
298 *
299 * Public class methods
300 *
301 ***********************************************************************/
302
303FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
304{
305 FLAC__ASSERT(0 != br);
306
307 br->words = br->bytes = 0;
308 br->consumed_words = br->consumed_bits = 0;
309 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
310 br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
311 if(br->buffer == 0)
312 return false;
313 br->read_callback = rcb;
314 br->client_data = cd;
315
316 return true;
317}
318
319void FLAC__bitreader_free(FLAC__BitReader *br)
320{
321 FLAC__ASSERT(0 != br);
322
323 if(0 != br->buffer)
324 free(br->buffer);
325 br->buffer = 0;
326 br->capacity = 0;
327 br->words = br->bytes = 0;
328 br->consumed_words = br->consumed_bits = 0;
329 br->read_callback = 0;
330 br->client_data = 0;
331}
332
333FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
334{
335 br->words = br->bytes = 0;
336 br->consumed_words = br->consumed_bits = 0;
337 return true;
338}
339
340void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
341{
342 unsigned i, j;
343 if(br == 0) {
344 fprintf(out, "bitreader is NULL\n");
345 }
346 else {
347 fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
348
349 for(i = 0; i < br->words; i++) {
350 fprintf(out, "%08X: ", i);
351 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
352 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
353 fprintf(out, ".");
354 else
355 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
356 fprintf(out, "\n");
357 }
358 if(br->bytes > 0) {
359 fprintf(out, "%08X: ", i);
360 for(j = 0; j < br->bytes*8; j++)
361 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
362 fprintf(out, ".");
363 else
364 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
365 fprintf(out, "\n");
366 }
367 }
368}
369
370void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
371{
372 FLAC__ASSERT(0 != br);
373 FLAC__ASSERT(0 != br->buffer);
374 FLAC__ASSERT((br->consumed_bits & 7) == 0);
375
376 br->read_crc16 = (unsigned)seed;
377 br->crc16_align = br->consumed_bits;
378}
379
380FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
381{
382 FLAC__ASSERT(0 != br);
383 FLAC__ASSERT(0 != br->buffer);
384 FLAC__ASSERT((br->consumed_bits & 7) == 0);
385 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
386
387 /* CRC any tail bytes in a partially-consumed word */
388 if(br->consumed_bits) {
389 const brword tail = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000390 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
391 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
392 }
393 return br->read_crc16;
394}
395
396FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
397{
398 return ((br->consumed_bits & 7) == 0);
399}
400
401FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
402{
403 return 8 - (br->consumed_bits & 7);
404}
405
406FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
407{
408 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
409}
410
411FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
412{
413 FLAC__ASSERT(0 != br);
414 FLAC__ASSERT(0 != br->buffer);
415
416 FLAC__ASSERT(bits <= 32);
417 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
418 FLAC__ASSERT(br->consumed_words <= br->words);
419
420 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
421 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
422
423 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
424 *val = 0;
425 return true;
426 }
427
428 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
429 if(!bitreader_read_from_client_(br))
430 return false;
431 }
432 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
433 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
434 if(br->consumed_bits) {
435 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
436 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
437 const brword word = br->buffer[br->consumed_words];
438 if(bits < n) {
439 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
440 br->consumed_bits += bits;
441 return true;
442 }
443 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
444 bits -= n;
445 crc16_update_word_(br, word);
446 br->consumed_words++;
447 br->consumed_bits = 0;
448 if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
449 *val <<= bits;
450 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
451 br->consumed_bits = bits;
452 }
453 return true;
454 }
455 else {
456 const brword word = br->buffer[br->consumed_words];
457 if(bits < FLAC__BITS_PER_WORD) {
458 *val = word >> (FLAC__BITS_PER_WORD-bits);
459 br->consumed_bits = bits;
460 return true;
461 }
462 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
463 *val = word;
464 crc16_update_word_(br, word);
465 br->consumed_words++;
466 return true;
467 }
468 }
469 else {
470 /* in this case we're starting our read at a partial tail word;
471 * the reader has guaranteed that we have at least 'bits' bits
472 * available to read, which makes this case simpler.
473 */
474 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
475 if(br->consumed_bits) {
476 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
477 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
478 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
479 br->consumed_bits += bits;
480 return true;
481 }
482 else {
483 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
484 br->consumed_bits += bits;
485 return true;
486 }
487 }
488}
489
490FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
491{
492 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
493 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
494 return false;
495 /* sign-extend: */
496 *val <<= (32-bits);
497 *val >>= (32-bits);
498 return true;
499}
500
501FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
502{
503 FLAC__uint32 hi, lo;
504
505 if(bits > 32) {
506 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
507 return false;
508 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
509 return false;
510 *val = hi;
511 *val <<= 32;
512 *val |= lo;
513 }
514 else {
515 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
516 return false;
517 *val = lo;
518 }
519 return true;
520}
521
522FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
523{
524 FLAC__uint32 x8, x32 = 0;
525
526 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
527
528 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
529 return false;
530
531 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
532 return false;
533 x32 |= (x8 << 8);
534
535 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
536 return false;
537 x32 |= (x8 << 16);
538
539 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
540 return false;
541 x32 |= (x8 << 24);
542
543 *val = x32;
544 return true;
545}
546
547FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
548{
549 /*
550 * OPT: a faster implementation is possible but probably not that useful
551 * since this is only called a couple of times in the metadata readers.
552 */
553 FLAC__ASSERT(0 != br);
554 FLAC__ASSERT(0 != br->buffer);
555
556 if(bits > 0) {
557 const unsigned n = br->consumed_bits & 7;
558 unsigned m;
559 FLAC__uint32 x;
560
561 if(n != 0) {
562 m = min(8-n, bits);
563 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
564 return false;
565 bits -= m;
566 }
567 m = bits / 8;
568 if(m > 0) {
569 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
570 return false;
571 bits %= 8;
572 }
573 if(bits > 0) {
574 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
575 return false;
576 }
577 }
578
579 return true;
580}
581
582FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
583{
584 FLAC__uint32 x;
585
586 FLAC__ASSERT(0 != br);
587 FLAC__ASSERT(0 != br->buffer);
588 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
589
590 /* step 1: skip over partial head word to get word aligned */
591 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
592 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
593 return false;
594 nvals--;
595 }
596 if(0 == nvals)
597 return true;
598 /* step 2: skip whole words in chunks */
599 while(nvals >= FLAC__BYTES_PER_WORD) {
600 if(br->consumed_words < br->words) {
601 br->consumed_words++;
602 nvals -= FLAC__BYTES_PER_WORD;
603 }
604 else if(!bitreader_read_from_client_(br))
605 return false;
606 }
607 /* step 3: skip any remainder from partial tail bytes */
608 while(nvals) {
609 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
610 return false;
611 nvals--;
612 }
613
614 return true;
615}
616
617FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
618{
619 FLAC__uint32 x;
620
621 FLAC__ASSERT(0 != br);
622 FLAC__ASSERT(0 != br->buffer);
623 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
624
625 /* step 1: read from partial head word to get word aligned */
626 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
627 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
628 return false;
629 *val++ = (FLAC__byte)x;
630 nvals--;
631 }
632 if(0 == nvals)
633 return true;
634 /* step 2: read whole words in chunks */
635 while(nvals >= FLAC__BYTES_PER_WORD) {
636 if(br->consumed_words < br->words) {
637 const brword word = br->buffer[br->consumed_words++];
638#if FLAC__BYTES_PER_WORD == 4
639 val[0] = (FLAC__byte)(word >> 24);
640 val[1] = (FLAC__byte)(word >> 16);
641 val[2] = (FLAC__byte)(word >> 8);
642 val[3] = (FLAC__byte)word;
643#elif FLAC__BYTES_PER_WORD == 8
644 val[0] = (FLAC__byte)(word >> 56);
645 val[1] = (FLAC__byte)(word >> 48);
646 val[2] = (FLAC__byte)(word >> 40);
647 val[3] = (FLAC__byte)(word >> 32);
648 val[4] = (FLAC__byte)(word >> 24);
649 val[5] = (FLAC__byte)(word >> 16);
650 val[6] = (FLAC__byte)(word >> 8);
651 val[7] = (FLAC__byte)word;
652#else
653 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
654 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
655#endif
656 val += FLAC__BYTES_PER_WORD;
657 nvals -= FLAC__BYTES_PER_WORD;
658 }
659 else if(!bitreader_read_from_client_(br))
660 return false;
661 }
662 /* step 3: read any remainder from partial tail bytes */
663 while(nvals) {
664 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
665 return false;
666 *val++ = (FLAC__byte)x;
667 nvals--;
668 }
669
670 return true;
671}
672
673FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
674#ifdef FLAC__NO_MANUAL_INLINING
675{
676 unsigned bit;
677
678 FLAC__ASSERT(0 != br);
679 FLAC__ASSERT(0 != br->buffer);
680
681 *val = 0;
682 while(1) {
683 if(!FLAC__bitreader_read_bit(br, &bit))
684 return false;
685 if(bit)
686 break;
687 else
688 *val++;
689 }
690 return true;
691}
692#else
693{
694 unsigned i;
695
696 FLAC__ASSERT(0 != br);
697 FLAC__ASSERT(0 != br->buffer);
698
699 *val = 0;
700 while(1) {
701 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
702 brword b = br->buffer[br->consumed_words] << br->consumed_bits;
703 if(b) {
704#if 0 /* too slow, but this is the idea: */
705 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
706 b <<= 1;
707#else
708 i = ALIGNED_UNARY_BITS(b);
709#endif
710 *val += i;
711 i++;
712 br->consumed_bits += i;
713 if(br->consumed_bits == FLAC__BITS_PER_WORD) {
714 crc16_update_word_(br, br->buffer[br->consumed_words]);
715 br->consumed_words++;
716 br->consumed_bits = 0;
717 }
718 return true;
719 }
720 else {
721 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
722 crc16_update_word_(br, br->buffer[br->consumed_words]);
723 br->consumed_words++;
724 br->consumed_bits = 0;
725 /* didn't find stop bit yet, have to keep going... */
726 }
727 }
728 /* at this point we've eaten up all the whole words; have to try
729 * reading through any tail bytes before calling the read callback.
730 * this is a repeat of the above logic adjusted for the fact we
731 * don't have a whole word. note though if the client is feeding
732 * us data a byte at a time (unlikely), br->consumed_bits may not
733 * be zero.
734 */
735 if(br->bytes) {
736 const unsigned end = br->bytes * 8;
737 brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
738 if(b) {
739#if 0 /* too slow, but this is the idea: */
740 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
741 b <<= 1;
742#else
743 i = ALIGNED_UNARY_BITS(b);
744#endif
745 *val += i;
746 i++;
747 br->consumed_bits += i;
748 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
749 return true;
750 }
751 else {
752 *val += end - br->consumed_bits;
753 br->consumed_bits += end;
754 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
755 /* didn't find stop bit yet, have to keep going... */
756 }
757 }
758 if(!bitreader_read_from_client_(br))
759 return false;
760 }
761}
762#endif
763
764FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
765{
766 FLAC__uint32 lsbs = 0, msbs = 0;
767 unsigned uval;
768
769 FLAC__ASSERT(0 != br);
770 FLAC__ASSERT(0 != br->buffer);
771 FLAC__ASSERT(parameter <= 31);
772
773 /* read the unary MSBs and end bit */
774 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
775 return false;
776
777 /* read the binary LSBs */
778 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
779 return false;
780
781 /* compose the value */
782 uval = (msbs << parameter) | lsbs;
783 if(uval & 1)
784 *val = -((int)(uval >> 1)) - 1;
785 else
786 *val = (int)(uval >> 1);
787
788 return true;
789}
790
791/* this is by far the most heavily used reader call. it ain't pretty but it's fast */
792/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
793FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
794{
795 unsigned i;
796 unsigned uval = 0;
797 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
798
799 /* try and get br->consumed_words and br->consumed_bits into register;
800 * must remember to flush them back to *br before calling other
801 * bitwriter functions that use them, and before returning */
802 register unsigned cwords;
803 register unsigned cbits;
804
805 FLAC__ASSERT(0 != br);
806 FLAC__ASSERT(0 != br->buffer);
807 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
808 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
809 FLAC__ASSERT(parameter < 32);
810 /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
811
812 if(nvals == 0)
813 return true;
814
815 cbits = br->consumed_bits;
816 cwords = br->consumed_words;
817
818 while(1) {
819
820 /* read unary part */
821 while(1) {
822 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
823 brword b = br->buffer[cwords] << cbits;
824 if(b) {
825#if 0 /* too slow, but this is the idea: */
826 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
827 b <<= 1;
828#else
829 i = ALIGNED_UNARY_BITS(b);
830#endif
831 uval += i;
832 bits = parameter;
833 i++;
834 cbits += i;
835 if(cbits == FLAC__BITS_PER_WORD) {
836 crc16_update_word_(br, br->buffer[cwords]);
837 cwords++;
838 cbits = 0;
839 }
840 goto break1;
841 }
842 else {
843 uval += FLAC__BITS_PER_WORD - cbits;
844 crc16_update_word_(br, br->buffer[cwords]);
845 cwords++;
846 cbits = 0;
847 /* didn't find stop bit yet, have to keep going... */
848 }
849 }
850 /* at this point we've eaten up all the whole words; have to try
851 * reading through any tail bytes before calling the read callback.
852 * this is a repeat of the above logic adjusted for the fact we
853 * don't have a whole word. note though if the client is feeding
854 * us data a byte at a time (unlikely), br->consumed_bits may not
855 * be zero.
856 */
857 if(br->bytes) {
858 const unsigned end = br->bytes * 8;
859 brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
860 if(b) {
861#if 0 /* too slow, but this is the idea: */
862 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
863 b <<= 1;
864#else
865 i = ALIGNED_UNARY_BITS(b);
866#endif
867 uval += i;
868 bits = parameter;
869 i++;
870 cbits += i;
871 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
872 goto break1;
873 }
874 else {
875 uval += end - cbits;
876 cbits += end;
877 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
878 /* didn't find stop bit yet, have to keep going... */
879 }
880 }
881 /* flush registers and read; bitreader_read_from_client_() does
882 * not touch br->consumed_bits at all but we still need to set
883 * it in case it fails and we have to return false.
884 */
885 br->consumed_bits = cbits;
886 br->consumed_words = cwords;
887 if(!bitreader_read_from_client_(br))
888 return false;
889 cwords = br->consumed_words;
890 }
891break1:
892 /* read binary part */
893 FLAC__ASSERT(cwords <= br->words);
894
895 if(bits) {
896 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
897 /* flush registers and read; bitreader_read_from_client_() does
898 * not touch br->consumed_bits at all but we still need to set
899 * it in case it fails and we have to return false.
900 */
901 br->consumed_bits = cbits;
902 br->consumed_words = cwords;
903 if(!bitreader_read_from_client_(br))
904 return false;
905 cwords = br->consumed_words;
906 }
907 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
908 if(cbits) {
909 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
910 const unsigned n = FLAC__BITS_PER_WORD - cbits;
911 const brword word = br->buffer[cwords];
912 if(bits < n) {
913 uval <<= bits;
914 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
915 cbits += bits;
916 goto break2;
917 }
918 uval <<= n;
919 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
920 bits -= n;
921 crc16_update_word_(br, word);
922 cwords++;
923 cbits = 0;
924 if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
925 uval <<= bits;
926 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
927 cbits = bits;
928 }
929 goto break2;
930 }
931 else {
932 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
933 uval <<= bits;
934 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
935 cbits = bits;
936 goto break2;
937 }
938 }
939 else {
940 /* in this case we're starting our read at a partial tail word;
941 * the reader has guaranteed that we have at least 'bits' bits
942 * available to read, which makes this case simpler.
943 */
944 uval <<= bits;
945 if(cbits) {
946 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
947 FLAC__ASSERT(cbits + bits <= br->bytes*8);
948 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
949 cbits += bits;
950 goto break2;
951 }
952 else {
953 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
954 cbits += bits;
955 goto break2;
956 }
957 }
958 }
959break2:
960 /* compose the value */
961 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
962
963 /* are we done? */
964 --nvals;
965 if(nvals == 0) {
966 br->consumed_bits = cbits;
967 br->consumed_words = cwords;
968 return true;
969 }
970
971 uval = 0;
972 ++vals;
973
974 }
975}
976
977#if 0 /* UNUSED */
978FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
979{
980 FLAC__uint32 lsbs = 0, msbs = 0;
981 unsigned bit, uval, k;
982
983 FLAC__ASSERT(0 != br);
984 FLAC__ASSERT(0 != br->buffer);
985
986 k = FLAC__bitmath_ilog2(parameter);
987
988 /* read the unary MSBs and end bit */
989 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
990 return false;
991
992 /* read the binary LSBs */
993 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
994 return false;
995
996 if(parameter == 1u<<k) {
997 /* compose the value */
998 uval = (msbs << k) | lsbs;
999 }
1000 else {
1001 unsigned d = (1 << (k+1)) - parameter;
1002 if(lsbs >= d) {
1003 if(!FLAC__bitreader_read_bit(br, &bit))
1004 return false;
1005 lsbs <<= 1;
1006 lsbs |= bit;
1007 lsbs -= d;
1008 }
1009 /* compose the value */
1010 uval = msbs * parameter + lsbs;
1011 }
1012
1013 /* unfold unsigned to signed */
1014 if(uval & 1)
1015 *val = -((int)(uval >> 1)) - 1;
1016 else
1017 *val = (int)(uval >> 1);
1018
1019 return true;
1020}
1021
1022FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1023{
1024 FLAC__uint32 lsbs, msbs = 0;
1025 unsigned bit, k;
1026
1027 FLAC__ASSERT(0 != br);
1028 FLAC__ASSERT(0 != br->buffer);
1029
1030 k = FLAC__bitmath_ilog2(parameter);
1031
1032 /* read the unary MSBs and end bit */
1033 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1034 return false;
1035
1036 /* read the binary LSBs */
1037 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1038 return false;
1039
1040 if(parameter == 1u<<k) {
1041 /* compose the value */
1042 *val = (msbs << k) | lsbs;
1043 }
1044 else {
1045 unsigned d = (1 << (k+1)) - parameter;
1046 if(lsbs >= d) {
1047 if(!FLAC__bitreader_read_bit(br, &bit))
1048 return false;
1049 lsbs <<= 1;
1050 lsbs |= bit;
1051 lsbs -= d;
1052 }
1053 /* compose the value */
1054 *val = msbs * parameter + lsbs;
1055 }
1056
1057 return true;
1058}
1059#endif /* UNUSED */
1060
1061/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1062FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1063{
1064 FLAC__uint32 v = 0;
1065 FLAC__uint32 x;
1066 unsigned i;
1067
1068 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1069 return false;
1070 if(raw)
1071 raw[(*rawlen)++] = (FLAC__byte)x;
1072 if(!(x & 0x80)) { /* 0xxxxxxx */
1073 v = x;
1074 i = 0;
1075 }
1076 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1077 v = x & 0x1F;
1078 i = 1;
1079 }
1080 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1081 v = x & 0x0F;
1082 i = 2;
1083 }
1084 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1085 v = x & 0x07;
1086 i = 3;
1087 }
1088 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1089 v = x & 0x03;
1090 i = 4;
1091 }
1092 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1093 v = x & 0x01;
1094 i = 5;
1095 }
1096 else {
1097 *val = 0xffffffff;
1098 return true;
1099 }
1100 for( ; i; i--) {
1101 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1102 return false;
1103 if(raw)
1104 raw[(*rawlen)++] = (FLAC__byte)x;
1105 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1106 *val = 0xffffffff;
1107 return true;
1108 }
1109 v <<= 6;
1110 v |= (x & 0x3F);
1111 }
1112 *val = v;
1113 return true;
1114}
1115
1116/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1117FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1118{
1119 FLAC__uint64 v = 0;
1120 FLAC__uint32 x;
1121 unsigned i;
1122
1123 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1124 return false;
1125 if(raw)
1126 raw[(*rawlen)++] = (FLAC__byte)x;
1127 if(!(x & 0x80)) { /* 0xxxxxxx */
1128 v = x;
1129 i = 0;
1130 }
1131 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1132 v = x & 0x1F;
1133 i = 1;
1134 }
1135 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1136 v = x & 0x0F;
1137 i = 2;
1138 }
1139 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1140 v = x & 0x07;
1141 i = 3;
1142 }
1143 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1144 v = x & 0x03;
1145 i = 4;
1146 }
1147 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1148 v = x & 0x01;
1149 i = 5;
1150 }
1151 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1152 v = 0;
1153 i = 6;
1154 }
1155 else {
1156 *val = FLAC__U64L(0xffffffffffffffff);
1157 return true;
1158 }
1159 for( ; i; i--) {
1160 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1161 return false;
1162 if(raw)
1163 raw[(*rawlen)++] = (FLAC__byte)x;
1164 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1165 *val = FLAC__U64L(0xffffffffffffffff);
1166 return true;
1167 }
1168 v <<= 6;
1169 v |= (x & 0x3F);
1170 }
1171 *val = v;
1172 return true;
1173}