1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: packing variable sized words into an octet stream
14 last mod: $Id: bitwise.c,v 1.13 2002/07/11 09:09:08 xiphmont Exp $
16 ********************************************************************/
18 /* We're 'LSb' endian; if we write a word but read individual bits,
19 then we'll read the lsb first */
25 #define BUFFER_INCREMENT 256
27 static unsigned long mask[]=
28 {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
29 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
30 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
31 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
32 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
33 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
34 0x3fffffff,0x7fffffff,0xffffffff };
36 void oggpack_writeinit(oggpack_buffer *b){
37 memset(b,0,sizeof(*b));
38 b->ptr=b->buffer=_ogg_malloc(BUFFER_INCREMENT);
40 b->storage=BUFFER_INCREMENT;
43 void oggpack_writetrunc(oggpack_buffer *b,long bits){
46 b->ptr=b->buffer+bytes;
52 void oggpack_writealign(oggpack_buffer *b){
55 oggpack_write(b,0,bits);
58 void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
59 unsigned char *ptr=(unsigned char *)source;
66 /* unaligned copy. Do it the hard way. */
68 oggpack_write(b,(long)(ptr[i]),8);
70 /* aligned block copy */
71 if(b->endbyte+bytes+1>=b->storage){
72 b->storage=b->endbyte+bytes+BUFFER_INCREMENT;
73 b->buffer=_ogg_realloc(b->buffer,b->storage);
74 b->ptr=b->buffer+b->endbyte;
77 memmove(b->ptr,source,bytes);
84 oggpack_write(b,(long)(ptr[bytes]),bits);
87 void oggpack_reset(oggpack_buffer *b){
90 b->endbit=b->endbyte=0;
93 void oggpack_writeclear(oggpack_buffer *b){
95 memset(b,0,sizeof(*b));
98 void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
99 memset(b,0,sizeof(*b));
100 b->buffer=b->ptr=buf;
104 /* Takes only up to 32 bits. */
105 void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
106 if(b->endbyte+4>=b->storage){
107 b->buffer=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
108 b->storage+=BUFFER_INCREMENT;
109 b->ptr=b->buffer+b->endbyte;
115 b->ptr[0]|=value<<b->endbit;
118 b->ptr[1]=value>>(8-b->endbit);
120 b->ptr[2]=value>>(16-b->endbit);
122 b->ptr[3]=value>>(24-b->endbit);
125 b->ptr[4]=value>>(32-b->endbit);
138 /* Read in bits without advancing the bitptr; bits <= 32 */
139 long oggpack_look(oggpack_buffer *b,int bits){
141 unsigned long m=mask[bits];
145 if(b->endbyte+4>=b->storage){
146 /* not the main path */
147 if(b->endbyte*8+bits>b->storage*8)return(-1);
150 ret=b->ptr[0]>>b->endbit;
152 ret|=b->ptr[1]<<(8-b->endbit);
154 ret|=b->ptr[2]<<(16-b->endbit);
156 ret|=b->ptr[3]<<(24-b->endbit);
157 if(bits>32 && b->endbit)
158 ret|=b->ptr[4]<<(32-b->endbit);
165 long oggpack_look1(oggpack_buffer *b){
166 if(b->endbyte>=b->storage)return(-1);
167 return((b->ptr[0]>>b->endbit)&1);
170 void oggpack_adv(oggpack_buffer *b,int bits){
177 void oggpack_adv1(oggpack_buffer *b){
186 long oggpack_read(oggpack_buffer *b,int bits){
188 unsigned long m=mask[bits];
192 if(b->endbyte+4>=b->storage){
193 /* not the main path */
195 if(b->endbyte*8+bits>b->storage*8)goto overflow;
198 ret=b->ptr[0]>>b->endbit;
200 ret|=b->ptr[1]<<(8-b->endbit);
202 ret|=b->ptr[2]<<(16-b->endbit);
204 ret|=b->ptr[3]<<(24-b->endbit);
205 if(bits>32 && b->endbit){
206 ret|=b->ptr[4]<<(32-b->endbit);
221 long oggpack_read1(oggpack_buffer *b){
224 if(b->endbyte>=b->storage){
225 /* not the main path */
230 ret=(b->ptr[0]>>b->endbit)&1;
243 long oggpack_bytes(oggpack_buffer *b){
244 return(b->endbyte+(b->endbit+7)/8);
247 long oggpack_bits(oggpack_buffer *b){
248 return(b->endbyte*8+b->endbit);
251 unsigned char *oggpack_get_buffer(oggpack_buffer *b){
255 /* Self test of the bitwise routines; everything else is based on
256 them, so they damned well better be solid. */
261 static int ilog(unsigned int v){
273 void report(char *in){
274 fprintf(stderr,"%s",in);
278 void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
280 unsigned char *buffer;
284 oggpack_write(&o,b[i],bits?bits:ilog(b[i]));
285 buffer=oggpack_get_buffer(&o);
286 bytes=oggpack_bytes(&o);
287 if(bytes!=compsize)report("wrong number of bytes!\n");
288 for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
289 for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
290 report("wrote incorrect value!\n");
292 oggpack_readinit(&r,buffer,bytes);
294 int tbit=bits?bits:ilog(b[i]);
295 if(oggpack_look(&r,tbit)==-1)
296 report("out of data!\n");
297 if(oggpack_look(&r,tbit)!=(b[i]&mask[tbit]))
298 report("looked at incorrect value!\n");
300 if(oggpack_look1(&r)!=(b[i]&mask[tbit]))
301 report("looked at single bit incorrect value!\n");
303 if(oggpack_read1(&r)!=(b[i]&mask[tbit]))
304 report("read incorrect single bit value!\n");
306 if(oggpack_read(&r,tbit)!=(b[i]&mask[tbit]))
307 report("read incorrect value!\n");
310 if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
314 unsigned char *buffer;
316 static unsigned long testbuffer1[]=
317 {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
318 567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
321 static unsigned long testbuffer2[]=
322 {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
323 1233432,534,5,346435231,14436467,7869299,76326614,167548585,
324 85525151,0,12321,1,349528352};
327 static unsigned long large[]=
328 {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
329 1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
330 85525151,0,12321,1,2146528352};
332 static unsigned long testbuffer3[]=
333 {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,
334 0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
338 static int one[]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
339 34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
343 static int two[]={61,255,255,251,231,29};
346 static int three[]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
347 142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
348 58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
349 100,52,4,14,18,86,77,1};
352 static int four[]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
353 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
357 static int five[]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
358 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
359 84,75,159,2,1,0,132,192,8,0,0,18,22};
362 static int six[]={17,177,170,242,169,19,148};
364 /* Test read/write together */
365 /* Later we test against pregenerated bitstreams */
366 oggpack_writeinit(&o);
368 fprintf(stderr,"\nSmall preclipped packing: ");
369 cliptest(testbuffer1,test1size,0,one,onesize);
370 fprintf(stderr,"ok.");
372 fprintf(stderr,"\nNull bit call: ");
373 cliptest(testbuffer3,test3size,0,two,twosize);
374 fprintf(stderr,"ok.");
376 fprintf(stderr,"\nLarge preclipped packing: ");
377 cliptest(testbuffer2,test2size,0,three,threesize);
378 fprintf(stderr,"ok.");
380 fprintf(stderr,"\n32 bit preclipped packing: ");
382 for(i=0;i<test2size;i++)
383 oggpack_write(&o,large[i],32);
384 buffer=oggpack_get_buffer(&o);
385 bytes=oggpack_bytes(&o);
386 oggpack_readinit(&r,buffer,bytes);
387 for(i=0;i<test2size;i++){
388 if(oggpack_look(&r,32)==-1)report("out of data. failed!");
389 if(oggpack_look(&r,32)!=large[i]){
390 fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpack_look(&r,32),large[i],
391 oggpack_look(&r,32),large[i]);
392 report("read incorrect value!\n");
396 if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
397 fprintf(stderr,"ok.");
399 fprintf(stderr,"\nSmall unclipped packing: ");
400 cliptest(testbuffer1,test1size,7,four,foursize);
401 fprintf(stderr,"ok.");
403 fprintf(stderr,"\nLarge unclipped packing: ");
404 cliptest(testbuffer2,test2size,17,five,fivesize);
405 fprintf(stderr,"ok.");
407 fprintf(stderr,"\nSingle bit unclipped packing: ");
408 cliptest(testbuffer3,test3size,1,six,sixsize);
409 fprintf(stderr,"ok.");
411 fprintf(stderr,"\nTesting read past end: ");
412 oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
414 if(oggpack_read(&r,1)!=0){
415 fprintf(stderr,"failed; got -1 prematurely.\n");
419 if(oggpack_look(&r,1)!=-1 ||
420 oggpack_read(&r,1)!=-1){
421 fprintf(stderr,"failed; read past end without -1.\n");
424 oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
425 if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){
426 fprintf(stderr,"failed 2; got -1 prematurely.\n");
430 if(oggpack_look(&r,18)!=0 ||
431 oggpack_look(&r,18)!=0){
432 fprintf(stderr,"failed 3; got -1 prematurely.\n");
435 if(oggpack_look(&r,19)!=-1 ||
436 oggpack_look(&r,19)!=-1){
437 fprintf(stderr,"failed; read past end without -1.\n");
440 if(oggpack_look(&r,32)!=-1 ||
441 oggpack_look(&r,32)!=-1){
442 fprintf(stderr,"failed; read past end without -1.\n");
445 fprintf(stderr,"ok.\n\n");
450 #endif /* _V_SELFTEST */
452 #undef BUFFER_INCREMENT