51 #define LOG(...) // none 54 #define SHL(x, y) ( ((x) << (y)) ) 55 #define SHR(x, y) ( ((y) & DPATHW) ? 0 : ((x) >> (y)) ) 61 #define SWAP2(x) be16toh(x) 62 #define SWAP4(x) be32toh(x) 63 #define SWAP8(x) be64toh(x) 65 #define SWAP2(x) ( (((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8) ) 66 #define SWAP4(x) (((x) >> 24)|(((x) & 0x00ff0000) >> 8)| \ 67 (((x) & 0x0000ff00) << 8)|(((x) & 0x000000ff) << 24)) 68 #define SWAP8(x) something... //TODO 76 #define IS_POW_OF_2(x) (\ 77 ((x)!= 0)&& (((x)& ((x)- 1))== 0)\ 84 register WORD_T word0;
85 int end_code= STAT_ERROR;
98 CHECK_DO(bitparser_ctx!= NULL,
goto end);
101 bitparser_ctx->
buf= (WORD_T*)buf;
103 bitparser_ctx->
bcnt= 0;
104 bitparser_ctx->
word0= word0= SWAPW(((WORD_T*)buf)[0]);
105 bitparser_ctx->
word1= SWAPW(((WORD_T*)buf)[1]);
106 bitparser_ctx->
top= word0;
108 end_code= STAT_SUCCESS;
110 if(end_code!= STAT_SUCCESS)
112 return bitparser_ctx;
120 if(ref_bitparser_ctx== NULL)
123 if((bitparser_ctx= *ref_bitparser_ctx)!= NULL) {
125 *ref_bitparser_ctx= NULL;
131 register WORD_T buf_size, bcnt, bcnt_new, wcnt, wcnt_new, word0, word1,
133 register const WORD_T *buf;
136 CHECK_DO(bitparser_ctx!= NULL,
return);
138 buf= bitparser_ctx->
buf;
140 bcnt= bitparser_ctx->
bcnt;
141 wcnt= bcnt>> DPATHW_SHIFTb;
143 bitparser_ctx->
bcnt= bcnt_new;
144 wcnt_new= bcnt_new>> DPATHW_SHIFTb;
145 word0= bitparser_ctx->
word0;
146 word1= bitparser_ctx->
word1;
147 wcnt_max= buf_size>> DPATHW_SHIFTB;
148 CHECK_DO(wcnt_new<= wcnt_max,
return);
150 if(wcnt_new!= wcnt) {
151 bitparser_ctx->
word0= word0= SWAPW(buf[wcnt_new% wcnt_max]);
152 bitparser_ctx->
word1= word1= SWAPW(buf[(wcnt_new+ 1)% wcnt_max]);
159 bitparser_ctx->
top= SHL(word0, bcnt_new&(DPATHW-1))+
160 SHR(word1, DPATHW- (bcnt_new&(DPATHW-1)));
168 CHECK_DO(bitparser_ctx!= NULL,
return 0);
170 bits_value= (bitparser_ctx->
top>> (DPATHW-n));
180 CHECK_DO(bitparser_ctx!= NULL,
return 0);
182 bits_value= (bitparser_ctx->
top>> (DPATHW-n));
188 register WORD_T bytecnt, buf_size, avail_size;
189 const uint8_t *pbuf= (uint8_t*)bitparser_ctx->
buf;
191 int end_code= STAT_ERROR;
194 CHECK_DO(bitparser_ctx!= NULL,
return NULL);
197 bytecnt= bitparser_ctx->
bcnt>> 3;
199 CHECK_DO(bytecnt< buf_size,
goto end);
200 avail_size= buf_size- bytecnt;
201 CHECK_DO(cnt<= avail_size,
goto end);
206 memcpy(p_ret, pbuf+ bytecnt, cnt);
209 end_code= STAT_SUCCESS;
211 if(end_code!= STAT_SUCCESS)
219 register WORD_T bits_unaligned, bits2flush;
222 CHECK_DO(bitparser_ctx!= NULL,
return);
224 bits_unaligned= bitparser_ctx->
bcnt& 7;
225 bits2flush= bits_unaligned? 8- bits_unaligned: 0;
227 if(bits_unaligned> 0)
void * bitparser_copy_bytes(bitparser_ctx_t *bitparser_ctx, size_t cnt)
void bitparser_flush(bitparser_ctx_t *bitparser_ctx, size_t n)
#define EXTEND_SIZE_TO_MULTIPLE(SIZE, MULTIPLE)
void bitparser_close(bitparser_ctx_t **ref_bitparser_ctx)
WORD_T bitparser_show(bitparser_ctx_t *bitparser_ctx, size_t n)
void bitparser_align_2byte(bitparser_ctx_t *bitparser_ctx)
General status codes enumeration.
#define CHECK_DO(COND, ACTION)
#define SIZE_IS_MULTIPLE(SIZE, MULTIPLE)
bitparser_ctx_t * bitparser_open(void *buf, size_t buf_size)
WORD_T bitparser_get(bitparser_ctx_t *bitparser_ctx, size_t n)
Bit parsing module utility.