summaryrefslogtreecommitdiff
path: root/apps/codecs/libtremor/bitwise.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libtremor/bitwise.c')
-rw-r--r--apps/codecs/libtremor/bitwise.c949
1 files changed, 800 insertions, 149 deletions
diff --git a/apps/codecs/libtremor/bitwise.c b/apps/codecs/libtremor/bitwise.c
index dabba468b9..050fb67c60 100644
--- a/apps/codecs/libtremor/bitwise.c
+++ b/apps/codecs/libtremor/bitwise.c
@@ -1,28 +1,31 @@
1/******************************************************************** 1/********************************************************************
2 * * 2 * *
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. * 3 * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. *
4 * *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * * 7 * *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 * 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ * 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 * * 10 * *
12 ******************************************************************** 11 ********************************************************************
13 12
14 function: packing variable sized words into an octet stream 13 function: packing variable sized words into an octet stream
14 last mod: $Id$
15 15
16 ********************************************************************/ 16 ********************************************************************/
17 17
18/* We're 'LSb' endian; if we write a word but read individual bits, 18/* We're 'LSb' endian; if we write a word but read individual bits,
19 then we'll read the lsb first */ 19 then we'll read the lsb first */
20 20
21#include "config-tremor.h"
22#include <string.h> 21#include <string.h>
22#include <stdlib.h>
23#include <limits.h>
23#include "ogg.h" 24#include "ogg.h"
24 25
25const unsigned long oggpack_mask[] ICONST_ATTR = 26#define BUFFER_INCREMENT 256
27
28const unsigned long mask[] ICONST_ATTR =
26{0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f, 29{0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
27 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff, 30 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
28 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff, 31 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
@@ -31,184 +34,832 @@ const unsigned long oggpack_mask[] ICONST_ATTR =
31 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff, 34 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
32 0x3fffffff,0x7fffffff,0xffffffff }; 35 0x3fffffff,0x7fffffff,0xffffffff };
33 36
34void oggpack_readinit(oggpack_buffer *b,ogg_reference *r){ 37#if 0
38static const unsigned int mask8B[]=
39{0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
40
41void oggpack_writeinit(oggpack_buffer *b){
35 memset(b,0,sizeof(*b)); 42 memset(b,0,sizeof(*b));
43 b->ptr=b->buffer=_ogg_malloc(BUFFER_INCREMENT);
44 b->buffer[0]='\0';
45 b->storage=BUFFER_INCREMENT;
46}
47
48void oggpackB_writeinit(oggpack_buffer *b){
49 oggpack_writeinit(b);
50}
36 51
37 b->tail=b->head=r; 52int oggpack_writecheck(oggpack_buffer *b){
38 b->count=0; 53 if(!b->ptr || !b->storage)return -1;
39 b->headptr=b->head->buffer->data+b->head->begin; 54 return 0;
40 b->headend=b->head->length;
41 _span(b);
42} 55}
43 56
44#define _lookspan() while(!end){\ 57int oggpackB_writecheck(oggpack_buffer *b){
45 head=head->next;\ 58 return oggpack_writecheck(b);
46 if(!head) return -1;\ 59}
47 ptr=head->buffer->data + head->begin;\
48 end=head->length;\
49 }
50 60
51/* Read in bits without advancing the bitptr; bits <= 32 */ 61void oggpack_writetrunc(oggpack_buffer *b,long bits){
52long oggpack_look_full(oggpack_buffer *b,int bits) ICODE_ATTR_TREMOR_NOT_MDCT; 62 long bytes=bits>>3;
53long oggpack_look_full(oggpack_buffer *b,int bits){ 63 if(b->ptr){
54 unsigned long m=oggpack_mask[bits]; 64 bits-=bytes*8;
55 unsigned long ret=-1; 65 b->ptr=b->buffer+bytes;
56 66 b->endbit=bits;
57 bits+=b->headbit; 67 b->endbyte=bytes;
58 68 *b->ptr&=mask[bits];
59 if(bits >= b->headend<<3){ 69 }
60 int end=b->headend; 70}
61 unsigned char *ptr=b->headptr; 71
62 ogg_reference *head=b->head; 72void oggpackB_writetrunc(oggpack_buffer *b,long bits){
63 73 long bytes=bits>>3;
64 if(end<0)return -1; 74 if(b->ptr){
65 75 bits-=bytes*8;
66 if(bits){ 76 b->ptr=b->buffer+bytes;
67 _lookspan(); 77 b->endbit=bits;
68 ret=*ptr++>>b->headbit; 78 b->endbyte=bytes;
69 if(bits>8){ 79 *b->ptr&=mask8B[bits];
70 --end; 80 }
71 _lookspan(); 81}
72 ret|=*ptr++<<(8-b->headbit); 82
73 if(bits>16){ 83/* Takes only up to 32 bits. */
74 --end; 84void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
75 _lookspan(); 85 if(bits<0 || bits>32) goto err;
76 ret|=*ptr++<<(16-b->headbit); 86 if(b->endbyte>=b->storage-4){
77 if(bits>24){ 87 void *ret;
78 --end; 88 if(!b->ptr)return;
79 _lookspan(); 89 if(b->storage>LONG_MAX-BUFFER_INCREMENT) goto err;
80 ret|=*ptr++<<(24-b->headbit); 90 ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
81 if(bits>32 && b->headbit){ 91 if(!ret) goto err;
82 --end; 92 b->buffer=ret;
83 _lookspan(); 93 b->storage+=BUFFER_INCREMENT;
84 ret|=*ptr<<(32-b->headbit); 94 b->ptr=b->buffer+b->endbyte;
85 } 95 }
86 } 96
97 value&=mask[bits];
98 bits+=b->endbit;
99
100 b->ptr[0]|=value<<b->endbit;
101
102 if(bits>=8){
103 b->ptr[1]=(unsigned char)(value>>(8-b->endbit));
104 if(bits>=16){
105 b->ptr[2]=(unsigned char)(value>>(16-b->endbit));
106 if(bits>=24){
107 b->ptr[3]=(unsigned char)(value>>(24-b->endbit));
108 if(bits>=32){
109 if(b->endbit)
110 b->ptr[4]=(unsigned char)(value>>(32-b->endbit));
111 else
112 b->ptr[4]=0;
87 } 113 }
88 } 114 }
89 } 115 }
116 }
90 117
91 }else{ 118 b->endbyte+=bits/8;
119 b->ptr+=bits/8;
120 b->endbit=bits&7;
121 return;
122 err:
123 oggpack_writeclear(b);
124}
125
126/* Takes only up to 32 bits. */
127void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){
128 if(bits<0 || bits>32) goto err;
129 if(b->endbyte>=b->storage-4){
130 void *ret;
131 if(!b->ptr)return;
132 if(b->storage>LONG_MAX-BUFFER_INCREMENT) goto err;
133 ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
134 if(!ret) goto err;
135 b->buffer=ret;
136 b->storage+=BUFFER_INCREMENT;
137 b->ptr=b->buffer+b->endbyte;
138 }
139
140 value=(value&mask[bits])<<(32-bits);
141 bits+=b->endbit;
92 142
93 /* make this a switch jump-table */ 143 b->ptr[0]|=value>>(24+b->endbit);
94 ret=b->headptr[0]>>b->headbit; 144
95 if(bits>8){ 145 if(bits>=8){
96 ret|=b->headptr[1]<<(8-b->headbit); 146 b->ptr[1]=(unsigned char)(value>>(16+b->endbit));
97 if(bits>16){ 147 if(bits>=16){
98 ret|=b->headptr[2]<<(16-b->headbit); 148 b->ptr[2]=(unsigned char)(value>>(8+b->endbit));
99 if(bits>24){ 149 if(bits>=24){
100 ret|=b->headptr[3]<<(24-b->headbit); 150 b->ptr[3]=(unsigned char)(value>>(b->endbit));
101 if(bits>32 && b->headbit) 151 if(bits>=32){
102 ret|=b->headptr[4]<<(32-b->headbit); 152 if(b->endbit)
153 b->ptr[4]=(unsigned char)(value<<(8-b->endbit));
154 else
155 b->ptr[4]=0;
103 } 156 }
104 } 157 }
105 } 158 }
106 } 159 }
107 160
108 ret&=m; 161 b->endbyte+=bits/8;
109 return ret; 162 b->ptr+=bits/8;
163 b->endbit=bits&7;
164 return;
165 err:
166 oggpack_writeclear(b);
167}
168
169void oggpack_writealign(oggpack_buffer *b){
170 int bits=8-b->endbit;
171 if(bits<8)
172 oggpack_write(b,0,bits);
173}
174
175void oggpackB_writealign(oggpack_buffer *b){
176 int bits=8-b->endbit;
177 if(bits<8)
178 oggpackB_write(b,0,bits);
179}
180
181static void oggpack_writecopy_helper(oggpack_buffer *b,
182 void *source,
183 long bits,
184 void (*w)(oggpack_buffer *,
185 unsigned long,
186 int),
187 int msb){
188 unsigned char *ptr=(unsigned char *)source;
189
190 long bytes=bits/8;
191 bits-=bytes*8;
192
193 if(b->endbit){
194 int i;
195 /* unaligned copy. Do it the hard way. */
196 for(i=0;i<bytes;i++)
197 w(b,(unsigned long)(ptr[i]),8);
198 }else{
199 /* aligned block copy */
200 if(b->endbyte+bytes+1>=b->storage){
201 void *ret;
202 if(!b->ptr) goto err;
203 if(b->endbyte+bytes+BUFFER_INCREMENT>b->storage) goto err;
204 b->storage=b->endbyte+bytes+BUFFER_INCREMENT;
205 ret=_ogg_realloc(b->buffer,b->storage);
206 if(!ret) goto err;
207 b->buffer=ret;
208 b->ptr=b->buffer+b->endbyte;
209 }
210
211 memmove(b->ptr,source,bytes);
212 b->ptr+=bytes;
213 b->endbyte+=bytes;
214 *b->ptr=0;
215
216 }
217 if(bits){
218 if(msb)
219 w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits);
220 else
221 w(b,(unsigned long)(ptr[bytes]),bits);
222 }
223 return;
224 err:
225 oggpack_writeclear(b);
226}
227
228void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
229 oggpack_writecopy_helper(b,source,bits,oggpack_write,0);
230}
231
232void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){
233 oggpack_writecopy_helper(b,source,bits,oggpackB_write,1);
234}
235
236void oggpack_reset(oggpack_buffer *b){
237 if(!b->ptr)return;
238 b->ptr=b->buffer;
239 b->buffer[0]=0;
240 b->endbit=b->endbyte=0;
241}
242
243void oggpackB_reset(oggpack_buffer *b){
244 oggpack_reset(b);
245}
246
247void oggpack_writeclear(oggpack_buffer *b){
248 if(b->buffer)_ogg_free(b->buffer);
249 memset(b,0,sizeof(*b));
250}
251
252void oggpackB_writeclear(oggpack_buffer *b){
253 oggpack_writeclear(b);
110} 254}
255#endif
256void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
257 memset(b,0,sizeof(*b));
258 b->buffer=b->ptr=buf;
259 b->storage=bytes;
260}
261#if 0
262void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
263 oggpack_readinit(b,buf,bytes);
264}
265#endif
266/* Read in bits without advancing the bitptr; bits <= 32 */
267/* moved to ogg.h for inlining */
268#if 0
269long oggpack_look(oggpack_buffer *b,int bits){
270 unsigned long ret;
271 unsigned long m;
272
273 if(bits<0 || bits>32) return -1;
274 m=mask[bits];
275 bits+=b->endbit;
276
277 if(b->endbyte >= b->storage-4){
278 /* not the main path */
279 if(b->endbyte > b->storage-((bits+7)>>3)) return -1;
280 /* special case to avoid reading b->ptr[0], which might be past the end of
281 the buffer; also skips some useless accounting */
282 else if(!bits)return(0L);
283 }
111 284
112/* spans forward and finds next byte. Never halts */ 285 ret=b->ptr[0]>>b->endbit;
113static void _span_one(oggpack_buffer *b){ 286 if(bits>8){
114 while(b->headend<1){ 287 ret|=b->ptr[1]<<(8-b->endbit);
115 if(b->head->next){ 288 if(bits>16){
116 b->count+=b->head->length; 289 ret|=b->ptr[2]<<(16-b->endbit);
117 b->head=b->head->next; 290 if(bits>24){
118 b->headptr=b->head->buffer->data+b->head->begin; 291 ret|=b->ptr[3]<<(24-b->endbit);
119 b->headend=b->head->length; 292 if(bits>32 && b->endbit)
120 }else 293 ret|=b->ptr[4]<<(32-b->endbit);
121 break; 294 }
295 }
122 } 296 }
297 return(m&ret);
123} 298}
299#endif
300
301#if 0
302/* Read in bits without advancing the bitptr; bits <= 32 */
303long oggpackB_look(oggpack_buffer *b,int bits){
304 unsigned long ret;
305 int m=32-bits;
306
307 if(m<0 || m>32) return -1;
308 bits+=b->endbit;
124 309
125static int _halt_one(oggpack_buffer *b){ 310 if(b->endbyte >= b->storage-4){
126 if(b->headend<1){ 311 /* not the main path */
127 _adv_halt(b); 312 if(b->endbyte > b->storage-((bits+7)>>3)) return -1;
128 return -1; 313 /* special case to avoid reading b->ptr[0], which might be past the end of
314 the buffer; also skips some useless accounting */
315 else if(!bits)return(0L);
129 } 316 }
130 return 0; 317
318 ret=b->ptr[0]<<(24+b->endbit);
319 if(bits>8){
320 ret|=b->ptr[1]<<(16+b->endbit);
321 if(bits>16){
322 ret|=b->ptr[2]<<(8+b->endbit);
323 if(bits>24){
324 ret|=b->ptr[3]<<(b->endbit);
325 if(bits>32 && b->endbit)
326 ret|=b->ptr[4]>>(8-b->endbit);
327 }
328 }
329 }
330 return ((ret&0xffffffff)>>(m>>1))>>((m+1)>>1);
331}
332#endif
333long oggpack_look1(oggpack_buffer *b){
334 if(b->endbyte>=b->storage)return(-1);
335 return((b->ptr[0]>>b->endbit)&1);
131} 336}
337#if 0
338long oggpackB_look1(oggpack_buffer *b){
339 if(b->endbyte>=b->storage)return(-1);
340 return((b->ptr[0]>>(7-b->endbit))&1);
341}
342#endif
343/* moved to ogg.h for inlining
344void oggpack_adv(oggpack_buffer *b,int bits){
345 bits+=b->endbit;
346
347 if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow;
348
349 b->ptr+=bits/8;
350 b->endbyte+=bits/8;
351 b->endbit=bits&7;
352 return;
132 353
354 overflow:
355 b->ptr=NULL;
356 b->endbyte=b->storage;
357 b->endbit=1;
358}
359*/
360#if 0
361void oggpackB_adv(oggpack_buffer *b,int bits){
362 oggpack_adv(b,bits);
363}
364#endif
365void oggpack_adv1(oggpack_buffer *b){
366 if(++(b->endbit)>7){
367 b->endbit=0;
368 b->ptr++;
369 b->endbyte++;
370 }
371}
372#if 0
373void oggpackB_adv1(oggpack_buffer *b){
374 oggpack_adv1(b);
375}
376#endif
133/* bits <= 32 */ 377/* bits <= 32 */
134long oggpack_read(oggpack_buffer *b,register int bits) ICODE_ATTR_TREMOR_NOT_MDCT; 378long oggpack_read(oggpack_buffer *b,int bits) ICODE_ATTR_TREMOR_NOT_MDCT;
135long oggpack_read(oggpack_buffer *b,register int bits){ 379long oggpack_read(oggpack_buffer *b,int bits){
136 unsigned long m=oggpack_mask[bits]; 380 long ret;
137 ogg_uint32_t ret=-1; 381 unsigned long m;
138 382
139 bits+=b->headbit; 383 if(bits<0 || bits>32) goto err;
384 m=mask[bits];
385 bits+=b->endbit;
140 386
141 if(bits >= b->headend<<3){ 387 if(b->endbyte >= b->storage-4){
388 /* not the main path */
389 if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow;
390 /* special case to avoid reading b->ptr[0], which might be past the end of
391 the buffer; also skips some useless accounting */
392 else if(!bits)return(0L);
393 }
142 394
143 if(b->headend<0)return -1; 395 ret=b->ptr[0]>>b->endbit;
144 396 if(bits>8){
145 if(bits){ 397 ret|=b->ptr[1]<<(8-b->endbit);
146 if (_halt_one(b)) return -1; 398 if(bits>16){
147 ret=*b->headptr>>b->headbit; 399 ret|=b->ptr[2]<<(16-b->endbit);
148 400 if(bits>24){
149 if(bits>=8){ 401 ret|=b->ptr[3]<<(24-b->endbit);
150 ++b->headptr; 402 if(bits>32 && b->endbit){
151 --b->headend; 403 ret|=b->ptr[4]<<(32-b->endbit);
152 _span_one(b);
153 if(bits>8){
154 if (_halt_one(b)) return -1;
155 ret|=*b->headptr<<(8-b->headbit);
156
157 if(bits>=16){
158 ++b->headptr;
159 --b->headend;
160 _span_one(b);
161 if(bits>16){
162 if (_halt_one(b)) return -1;
163 ret|=*b->headptr<<(16-b->headbit);
164
165 if(bits>=24){
166 ++b->headptr;
167 --b->headend;
168 _span_one(b);
169 if(bits>24){
170 if (_halt_one(b)) return -1;
171 ret|=*b->headptr<<(24-b->headbit);
172
173 if(bits>=32){
174 ++b->headptr;
175 --b->headend;
176 _span_one(b);
177 if(bits>32){
178 if (_halt_one(b)) return -1;
179 if(b->headbit)ret|=*b->headptr<<(32-b->headbit);
180
181 }
182 }
183 }
184 }
185 }
186 }
187 } 404 }
188 } 405 }
189 } 406 }
190 }else{ 407 }
408 ret&=m;
409 b->ptr+=bits/8;
410 b->endbyte+=bits/8;
411 b->endbit=bits&7;
412 return ret;
191 413
192 ret=b->headptr[0]>>b->headbit; 414 overflow:
193 if(bits>8){ 415 err:
194 ret|=b->headptr[1]<<(8-b->headbit); 416 b->ptr=NULL;
195 if(bits>16){ 417 b->endbyte=b->storage;
196 ret|=b->headptr[2]<<(16-b->headbit); 418 b->endbit=1;
197 if(bits>24){ 419 return -1L;
198 ret|=b->headptr[3]<<(24-b->headbit); 420}
199 if(bits>32 && b->headbit){ 421#if 0
200 ret|=b->headptr[4]<<(32-b->headbit); 422/* bits <= 32 */
201 } 423long oggpackB_read(oggpack_buffer *b,int bits){
202 } 424 long ret;
425 long m=32-bits;
426
427 if(m<0 || m>32) goto err;
428 bits+=b->endbit;
429
430 if(b->endbyte+4>=b->storage){
431 /* not the main path */
432 if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow;
433 /* special case to avoid reading b->ptr[0], which might be past the end of
434 the buffer; also skips some useless accounting */
435 else if(!bits)return(0L);
436 }
437
438 ret=b->ptr[0]<<(24+b->endbit);
439 if(bits>8){
440 ret|=b->ptr[1]<<(16+b->endbit);
441 if(bits>16){
442 ret|=b->ptr[2]<<(8+b->endbit);
443 if(bits>24){
444 ret|=b->ptr[3]<<(b->endbit);
445 if(bits>32 && b->endbit)
446 ret|=b->ptr[4]>>(8-b->endbit);
203 } 447 }
204 } 448 }
205
206 b->headptr+=((unsigned)bits)/8;
207 b->headend-=((unsigned)bits)/8;
208 } 449 }
450 ret=((ret&0xffffffffUL)>>(m>>1))>>((m+1)>>1);
209 451
210 ret&=m; 452 b->ptr+=bits/8;
211 b->headbit=bits&7; 453 b->endbyte+=bits/8;
454 b->endbit=bits&7;
212 return ret; 455 return ret;
456
457 overflow:
458 err:
459 b->ptr=NULL;
460 b->endbyte=b->storage;
461 b->endbit=1;
462 return -1L;
213} 463}
464#endif
465long oggpack_read1(oggpack_buffer *b){
466 long ret;
467
468 if(b->endbyte >= b->storage) goto overflow;
469 ret=(b->ptr[0]>>b->endbit)&1;
470
471 b->endbit++;
472 if(b->endbit>7){
473 b->endbit=0;
474 b->ptr++;
475 b->endbyte++;
476 }
477 return ret;
478
479 overflow:
480 b->ptr=NULL;
481 b->endbyte=b->storage;
482 b->endbit=1;
483 return -1L;
484}
485#if 0
486long oggpackB_read1(oggpack_buffer *b){
487 long ret;
488
489 if(b->endbyte >= b->storage) goto overflow;
490 ret=(b->ptr[0]>>(7-b->endbit))&1;
491
492 b->endbit++;
493 if(b->endbit>7){
494 b->endbit=0;
495 b->ptr++;
496 b->endbyte++;
497 }
498 return ret;
499
500 overflow:
501 b->ptr=NULL;
502 b->endbyte=b->storage;
503 b->endbit=1;
504 return -1L;
505}
506
507long oggpack_bytes(oggpack_buffer *b){
508 return(b->endbyte+(b->endbit+7)/8);
509}
510
511long oggpack_bits(oggpack_buffer *b){
512 return(b->endbyte*8+b->endbit);
513}
514
515long oggpackB_bytes(oggpack_buffer *b){
516 return oggpack_bytes(b);
517}
518
519long oggpackB_bits(oggpack_buffer *b){
520 return oggpack_bits(b);
521}
522
523unsigned char *oggpack_get_buffer(oggpack_buffer *b){
524 return(b->buffer);
525}
526
527unsigned char *oggpackB_get_buffer(oggpack_buffer *b){
528 return oggpack_get_buffer(b);
529}
530#endif
531/* Self test of the bitwise routines; everything else is based on
532 them, so they damned well better be solid. */
533
534#ifdef _V_SELFTEST
535#include <stdio.h>
536
537static int ilog(unsigned int v){
538 int ret=0;
539 while(v){
540 ret++;
541 v>>=1;
542 }
543 return(ret);
544}
545
546oggpack_buffer o;
547oggpack_buffer r;
548
549void report(char *in){
550 fprintf(stderr,"%s",in);
551 exit(1);
552}
553
554void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
555 long bytes,i;
556 unsigned char *buffer;
557
558 oggpack_reset(&o);
559 for(i=0;i<vals;i++)
560 oggpack_write(&o,b[i],bits?bits:ilog(b[i]));
561 buffer=oggpack_get_buffer(&o);
562 bytes=oggpack_bytes(&o);
563 if(bytes!=compsize)report("wrong number of bytes!\n");
564 for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
565 for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
566 report("wrote incorrect value!\n");
567 }
568 oggpack_readinit(&r,buffer,bytes);
569 for(i=0;i<vals;i++){
570 int tbit=bits?bits:ilog(b[i]);
571 if(oggpack_look(&r,tbit)==-1)
572 report("out of data!\n");
573 if(oggpack_look(&r,tbit)!=(b[i]&mask[tbit]))
574 report("looked at incorrect value!\n");
575 if(tbit==1)
576 if(oggpack_look1(&r)!=(b[i]&mask[tbit]))
577 report("looked at single bit incorrect value!\n");
578 if(tbit==1){
579 if(oggpack_read1(&r)!=(b[i]&mask[tbit]))
580 report("read incorrect single bit value!\n");
581 }else{
582 if(oggpack_read(&r,tbit)!=(b[i]&mask[tbit]))
583 report("read incorrect value!\n");
584 }
585 }
586 if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
587}
588
589void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){
590 long bytes,i;
591 unsigned char *buffer;
592
593 oggpackB_reset(&o);
594 for(i=0;i<vals;i++)
595 oggpackB_write(&o,b[i],bits?bits:ilog(b[i]));
596 buffer=oggpackB_get_buffer(&o);
597 bytes=oggpackB_bytes(&o);
598 if(bytes!=compsize)report("wrong number of bytes!\n");
599 for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
600 for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
601 report("wrote incorrect value!\n");
602 }
603 oggpackB_readinit(&r,buffer,bytes);
604 for(i=0;i<vals;i++){
605 int tbit=bits?bits:ilog(b[i]);
606 if(oggpackB_look(&r,tbit)==-1)
607 report("out of data!\n");
608 if(oggpackB_look(&r,tbit)!=(b[i]&mask[tbit]))
609 report("looked at incorrect value!\n");
610 if(tbit==1)
611 if(oggpackB_look1(&r)!=(b[i]&mask[tbit]))
612 report("looked at single bit incorrect value!\n");
613 if(tbit==1){
614 if(oggpackB_read1(&r)!=(b[i]&mask[tbit]))
615 report("read incorrect single bit value!\n");
616 }else{
617 if(oggpackB_read(&r,tbit)!=(b[i]&mask[tbit]))
618 report("read incorrect value!\n");
619 }
620 }
621 if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
622}
623
624int main(void){
625 unsigned char *buffer;
626 long bytes,i;
627 static unsigned long testbuffer1[]=
628 {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
629 567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
630 int test1size=43;
631
632 static unsigned long testbuffer2[]=
633 {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
634 1233432,534,5,346435231,14436467,7869299,76326614,167548585,
635 85525151,0,12321,1,349528352};
636 int test2size=21;
637
638 static unsigned long testbuffer3[]=
639 {1,0,14,0,1,0,12,0,1,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0,1,
640 0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
641 int test3size=56;
642
643 static unsigned long large[]=
644 {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
645 1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
646 85525151,0,12321,1,2146528352};
647
648 int onesize=33;
649 static int one[33]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
650 34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
651 223,4};
652 static int oneB[33]={150,101,131,33,203,15,204,216,105,193,156,65,84,85,222,
653 8,139,145,227,126,34,55,244,171,85,100,39,195,173,18,
654 245,251,128};
655
656 int twosize=6;
657 static int two[6]={61,255,255,251,231,29};
658 static int twoB[6]={247,63,255,253,249,120};
659
660 int threesize=54;
661 static int three[54]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
662 142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
663 58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
664 100,52,4,14,18,86,77,1};
665 static int threeB[54]={206,128,42,153,57,8,183,251,13,89,36,30,32,144,183,
666 130,59,240,121,59,85,223,19,228,180,134,33,107,74,98,
667 233,253,196,135,63,2,110,114,50,155,90,127,37,170,104,
668 200,20,254,4,58,106,176,144,0};
669
670 int foursize=38;
671 static int four[38]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
672 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
673 28,2,133,0,1};
674 static int fourB[38]={36,48,102,83,243,24,52,7,4,35,132,10,145,21,2,93,2,41,
675 1,219,184,16,33,184,54,149,170,132,18,30,29,98,229,67,
676 129,10,4,32};
677
678 int fivesize=45;
679 static int five[45]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
680 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
681 84,75,159,2,1,0,132,192,8,0,0,18,22};
682 static int fiveB[45]={1,84,145,111,245,100,128,8,56,36,40,71,126,78,213,226,
683 124,105,12,0,133,128,0,162,233,242,67,152,77,205,77,
684 172,150,169,129,79,128,0,6,4,32,0,27,9,0};
685
686 int sixsize=7;
687 static int six[7]={17,177,170,242,169,19,148};
688 static int sixB[7]={136,141,85,79,149,200,41};
689
690 /* Test read/write together */
691 /* Later we test against pregenerated bitstreams */
692 oggpack_writeinit(&o);
693
694 fprintf(stderr,"\nSmall preclipped packing (LSb): ");
695 cliptest(testbuffer1,test1size,0,one,onesize);
696 fprintf(stderr,"ok.");
697
698 fprintf(stderr,"\nNull bit call (LSb): ");
699 cliptest(testbuffer3,test3size,0,two,twosize);
700 fprintf(stderr,"ok.");
701
702 fprintf(stderr,"\nLarge preclipped packing (LSb): ");
703 cliptest(testbuffer2,test2size,0,three,threesize);
704 fprintf(stderr,"ok.");
705
706 fprintf(stderr,"\n32 bit preclipped packing (LSb): ");
707 oggpack_reset(&o);
708 for(i=0;i<test2size;i++)
709 oggpack_write(&o,large[i],32);
710 buffer=oggpack_get_buffer(&o);
711 bytes=oggpack_bytes(&o);
712 oggpack_readinit(&r,buffer,bytes);
713 for(i=0;i<test2size;i++){
714 if(oggpack_look(&r,32)==-1)report("out of data. failed!");
715 if(oggpack_look(&r,32)!=large[i]){
716 fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpack_look(&r,32),large[i],
717 oggpack_look(&r,32),large[i]);
718 report("read incorrect value!\n");
719 }
720 oggpack_adv(&r,32);
721 }
722 if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
723 fprintf(stderr,"ok.");
724
725 fprintf(stderr,"\nSmall unclipped packing (LSb): ");
726 cliptest(testbuffer1,test1size,7,four,foursize);
727 fprintf(stderr,"ok.");
728
729 fprintf(stderr,"\nLarge unclipped packing (LSb): ");
730 cliptest(testbuffer2,test2size,17,five,fivesize);
731 fprintf(stderr,"ok.");
732
733 fprintf(stderr,"\nSingle bit unclipped packing (LSb): ");
734 cliptest(testbuffer3,test3size,1,six,sixsize);
735 fprintf(stderr,"ok.");
736
737 fprintf(stderr,"\nTesting read past end (LSb): ");
738 oggpack_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
739 for(i=0;i<64;i++){
740 if(oggpack_read(&r,1)!=0){
741 fprintf(stderr,"failed; got -1 prematurely.\n");
742 exit(1);
743 }
744 }
745 if(oggpack_look(&r,1)!=-1 ||
746 oggpack_read(&r,1)!=-1){
747 fprintf(stderr,"failed; read past end without -1.\n");
748 exit(1);
749 }
750 oggpack_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
751 if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){
752 fprintf(stderr,"failed 2; got -1 prematurely.\n");
753 exit(1);
754 }
755
756 if(oggpack_look(&r,18)!=0 ||
757 oggpack_look(&r,18)!=0){
758 fprintf(stderr,"failed 3; got -1 prematurely.\n");
759 exit(1);
760 }
761 if(oggpack_look(&r,19)!=-1 ||
762 oggpack_look(&r,19)!=-1){
763 fprintf(stderr,"failed; read past end without -1.\n");
764 exit(1);
765 }
766 if(oggpack_look(&r,32)!=-1 ||
767 oggpack_look(&r,32)!=-1){
768 fprintf(stderr,"failed; read past end without -1.\n");
769 exit(1);
770 }
771 oggpack_writeclear(&o);
772 fprintf(stderr,"ok.\n");
773
774 /********** lazy, cut-n-paste retest with MSb packing ***********/
775
776 /* Test read/write together */
777 /* Later we test against pregenerated bitstreams */
778 oggpackB_writeinit(&o);
779
780 fprintf(stderr,"\nSmall preclipped packing (MSb): ");
781 cliptestB(testbuffer1,test1size,0,oneB,onesize);
782 fprintf(stderr,"ok.");
783
784 fprintf(stderr,"\nNull bit call (MSb): ");
785 cliptestB(testbuffer3,test3size,0,twoB,twosize);
786 fprintf(stderr,"ok.");
787
788 fprintf(stderr,"\nLarge preclipped packing (MSb): ");
789 cliptestB(testbuffer2,test2size,0,threeB,threesize);
790 fprintf(stderr,"ok.");
791
792 fprintf(stderr,"\n32 bit preclipped packing (MSb): ");
793 oggpackB_reset(&o);
794 for(i=0;i<test2size;i++)
795 oggpackB_write(&o,large[i],32);
796 buffer=oggpackB_get_buffer(&o);
797 bytes=oggpackB_bytes(&o);
798 oggpackB_readinit(&r,buffer,bytes);
799 for(i=0;i<test2size;i++){
800 if(oggpackB_look(&r,32)==-1)report("out of data. failed!");
801 if(oggpackB_look(&r,32)!=large[i]){
802 fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpackB_look(&r,32),large[i],
803 oggpackB_look(&r,32),large[i]);
804 report("read incorrect value!\n");
805 }
806 oggpackB_adv(&r,32);
807 }
808 if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
809 fprintf(stderr,"ok.");
810
811 fprintf(stderr,"\nSmall unclipped packing (MSb): ");
812 cliptestB(testbuffer1,test1size,7,fourB,foursize);
813 fprintf(stderr,"ok.");
814
815 fprintf(stderr,"\nLarge unclipped packing (MSb): ");
816 cliptestB(testbuffer2,test2size,17,fiveB,fivesize);
817 fprintf(stderr,"ok.");
818
819 fprintf(stderr,"\nSingle bit unclipped packing (MSb): ");
820 cliptestB(testbuffer3,test3size,1,sixB,sixsize);
821 fprintf(stderr,"ok.");
822
823 fprintf(stderr,"\nTesting read past end (MSb): ");
824 oggpackB_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
825 for(i=0;i<64;i++){
826 if(oggpackB_read(&r,1)!=0){
827 fprintf(stderr,"failed; got -1 prematurely.\n");
828 exit(1);
829 }
830 }
831 if(oggpackB_look(&r,1)!=-1 ||
832 oggpackB_read(&r,1)!=-1){
833 fprintf(stderr,"failed; read past end without -1.\n");
834 exit(1);
835 }
836 oggpackB_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
837 if(oggpackB_read(&r,30)!=0 || oggpackB_read(&r,16)!=0){
838 fprintf(stderr,"failed 2; got -1 prematurely.\n");
839 exit(1);
840 }
841
842 if(oggpackB_look(&r,18)!=0 ||
843 oggpackB_look(&r,18)!=0){
844 fprintf(stderr,"failed 3; got -1 prematurely.\n");
845 exit(1);
846 }
847 if(oggpackB_look(&r,19)!=-1 ||
848 oggpackB_look(&r,19)!=-1){
849 fprintf(stderr,"failed; read past end without -1.\n");
850 exit(1);
851 }
852 if(oggpackB_look(&r,32)!=-1 ||
853 oggpackB_look(&r,32)!=-1){
854 fprintf(stderr,"failed; read past end without -1.\n");
855 exit(1);
856 }
857 oggpackB_writeclear(&o);
858 fprintf(stderr,"ok.\n\n");
859
860
861 return(0);
862}
863#endif /* _V_SELFTEST */
214 864
865#undef BUFFER_INCREMENT