1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg CONTAINER 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-2010 *
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 16993 2010-03-21 23:15:46Z xiphmont $
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 const 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 static const unsigned int mask8B[]=
37 {0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
39 void oggpack_writeinit(oggpack_buffer *b){
40 memset(b,0,sizeof(*b));
41 b->ptr=b->buffer=_ogg_malloc(BUFFER_INCREMENT);
43 b->storage=BUFFER_INCREMENT;
46 void oggpackB_writeinit(oggpack_buffer *b){
50 int oggpack_writecheck(oggpack_buffer *b){
51 if(!b->ptr || !b->storage)return -1;
55 int oggpackB_writecheck(oggpack_buffer *b){
56 return oggpack_writecheck(b);
59 void oggpack_writetrunc(oggpack_buffer *b,long bits){
63 b->ptr=b->buffer+bytes;
70 void oggpackB_writetrunc(oggpack_buffer *b,long bits){
74 b->ptr=b->buffer+bytes;
77 *b->ptr&=mask8B[bits];
81 /* Takes only up to 32 bits. */
82 void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
83 if(b->endbyte+4>=b->storage){
86 ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
88 oggpack_writeclear(b);
92 b->storage+=BUFFER_INCREMENT;
93 b->ptr=b->buffer+b->endbyte;
99 b->ptr[0]|=value<<b->endbit;
102 b->ptr[1]=(unsigned char)(value>>(8-b->endbit));
104 b->ptr[2]=(unsigned char)(value>>(16-b->endbit));
106 b->ptr[3]=(unsigned char)(value>>(24-b->endbit));
109 b->ptr[4]=(unsigned char)(value>>(32-b->endbit));
122 /* Takes only up to 32 bits. */
123 void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){
124 if(b->endbyte+4>=b->storage){
127 ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
129 oggpack_writeclear(b);
133 b->storage+=BUFFER_INCREMENT;
134 b->ptr=b->buffer+b->endbyte;
137 value=(value&mask[bits])<<(32-bits);
140 b->ptr[0]|=value>>(24+b->endbit);
143 b->ptr[1]=(unsigned char)(value>>(16+b->endbit));
145 b->ptr[2]=(unsigned char)(value>>(8+b->endbit));
147 b->ptr[3]=(unsigned char)(value>>(b->endbit));
150 b->ptr[4]=(unsigned char)(value<<(8-b->endbit));
163 void oggpack_writealign(oggpack_buffer *b){
164 int bits=8-b->endbit;
166 oggpack_write(b,0,bits);
169 void oggpackB_writealign(oggpack_buffer *b){
170 int bits=8-b->endbit;
172 oggpackB_write(b,0,bits);
175 static void oggpack_writecopy_helper(oggpack_buffer *b,
178 void (*w)(oggpack_buffer *,
182 unsigned char *ptr=(unsigned char *)source;
189 /* unaligned copy. Do it the hard way. */
191 w(b,(unsigned long)(ptr[i]),8);
193 /* aligned block copy */
194 if(b->endbyte+bytes+1>=b->storage){
197 b->storage=b->endbyte+bytes+BUFFER_INCREMENT;
198 ret=_ogg_realloc(b->buffer,b->storage);
200 oggpack_writeclear(b);
204 b->ptr=b->buffer+b->endbyte;
207 memmove(b->ptr,source,bytes);
215 w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits);
217 w(b,(unsigned long)(ptr[bytes]),bits);
221 void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
222 oggpack_writecopy_helper(b,source,bits,oggpack_write,0);
225 void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){
226 oggpack_writecopy_helper(b,source,bits,oggpackB_write,1);
229 void oggpack_reset(oggpack_buffer *b){
233 b->endbit=b->endbyte=0;
236 void oggpackB_reset(oggpack_buffer *b){
240 void oggpack_writeclear(oggpack_buffer *b){
241 if(b->buffer)_ogg_free(b->buffer);
242 memset(b,0,sizeof(*b));
245 void oggpackB_writeclear(oggpack_buffer *b){
246 oggpack_writeclear(b);
249 void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
250 memset(b,0,sizeof(*b));
251 b->buffer=b->ptr=buf;
255 void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
256 oggpack_readinit(b,buf,bytes);
259 /* Read in bits without advancing the bitptr; bits <= 32 */
260 long oggpack_look(oggpack_buffer *b,int bits){
262 unsigned long m=mask[bits];
266 if(b->endbyte+4>=b->storage){
267 /* not the main path */
268 if(b->endbyte*8+bits>b->storage*8)return(-1);
271 ret=b->ptr[0]>>b->endbit;
273 ret|=b->ptr[1]<<(8-b->endbit);
275 ret|=b->ptr[2]<<(16-b->endbit);
277 ret|=b->ptr[3]<<(24-b->endbit);
278 if(bits>32 && b->endbit)
279 ret|=b->ptr[4]<<(32-b->endbit);
286 /* Read in bits without advancing the bitptr; bits <= 32 */
287 long oggpackB_look(oggpack_buffer *b,int bits){
293 if(b->endbyte+4>=b->storage){
294 /* not the main path */
295 if(b->endbyte*8+bits>b->storage*8)return(-1);
298 ret=b->ptr[0]<<(24+b->endbit);
300 ret|=b->ptr[1]<<(16+b->endbit);
302 ret|=b->ptr[2]<<(8+b->endbit);
304 ret|=b->ptr[3]<<(b->endbit);
305 if(bits>32 && b->endbit)
306 ret|=b->ptr[4]>>(8-b->endbit);
310 return ((ret&0xffffffff)>>(m>>1))>>((m+1)>>1);
313 long oggpack_look1(oggpack_buffer *b){
314 if(b->endbyte>=b->storage)return(-1);
315 return((b->ptr[0]>>b->endbit)&1);
318 long oggpackB_look1(oggpack_buffer *b){
319 if(b->endbyte>=b->storage)return(-1);
320 return((b->ptr[0]>>(7-b->endbit))&1);
323 void oggpack_adv(oggpack_buffer *b,int bits){
330 void oggpackB_adv(oggpack_buffer *b,int bits){
334 void oggpack_adv1(oggpack_buffer *b){
342 void oggpackB_adv1(oggpack_buffer *b){
347 long oggpack_read(oggpack_buffer *b,int bits){
349 unsigned long m=mask[bits];
353 if(b->endbyte+4>=b->storage){
354 /* not the main path */
356 if(b->endbyte*8+bits>b->storage*8)goto overflow;
359 ret=b->ptr[0]>>b->endbit;
361 ret|=b->ptr[1]<<(8-b->endbit);
363 ret|=b->ptr[2]<<(16-b->endbit);
365 ret|=b->ptr[3]<<(24-b->endbit);
366 if(bits>32 && b->endbit){
367 ret|=b->ptr[4]<<(32-b->endbit);
383 long oggpackB_read(oggpack_buffer *b,int bits){
389 if(b->endbyte+4>=b->storage){
390 /* not the main path */
392 if(b->endbyte*8+bits>b->storage*8)goto overflow;
393 /* special case to avoid reading b->ptr[0], which might be past the end of
394 the buffer; also skips some useless accounting */
395 else if(!bits)return(0L);
398 ret=b->ptr[0]<<(24+b->endbit);
400 ret|=b->ptr[1]<<(16+b->endbit);
402 ret|=b->ptr[2]<<(8+b->endbit);
404 ret|=b->ptr[3]<<(b->endbit);
405 if(bits>32 && b->endbit)
406 ret|=b->ptr[4]>>(8-b->endbit);
410 ret=((ret&0xffffffffUL)>>(m>>1))>>((m+1)>>1);
420 long oggpack_read1(oggpack_buffer *b){
423 if(b->endbyte>=b->storage){
424 /* not the main path */
429 ret=(b->ptr[0]>>b->endbit)&1;
442 long oggpackB_read1(oggpack_buffer *b){
445 if(b->endbyte>=b->storage){
446 /* not the main path */
451 ret=(b->ptr[0]>>(7-b->endbit))&1;
464 long oggpack_bytes(oggpack_buffer *b){
465 return(b->endbyte+(b->endbit+7)/8);
468 long oggpack_bits(oggpack_buffer *b){
469 return(b->endbyte*8+b->endbit);
472 long oggpackB_bytes(oggpack_buffer *b){
473 return oggpack_bytes(b);
476 long oggpackB_bits(oggpack_buffer *b){
477 return oggpack_bits(b);
480 unsigned char *oggpack_get_buffer(oggpack_buffer *b){
484 unsigned char *oggpackB_get_buffer(oggpack_buffer *b){
485 return oggpack_get_buffer(b);
488 /* Self test of the bitwise routines; everything else is based on
489 them, so they damned well better be solid. */
494 static int ilog(unsigned int v){
506 void report(char *in){
507 fprintf(stderr,"%s",in);
511 void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
513 unsigned char *buffer;
517 oggpack_write(&o,b[i],bits?bits:ilog(b[i]));
518 buffer=oggpack_get_buffer(&o);
519 bytes=oggpack_bytes(&o);
520 if(bytes!=compsize)report("wrong number of bytes!\n");
521 for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
522 for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
523 report("wrote incorrect value!\n");
525 oggpack_readinit(&r,buffer,bytes);
527 int tbit=bits?bits:ilog(b[i]);
528 if(oggpack_look(&r,tbit)==-1)
529 report("out of data!\n");
530 if(oggpack_look(&r,tbit)!=(b[i]&mask[tbit]))
531 report("looked at incorrect value!\n");
533 if(oggpack_look1(&r)!=(b[i]&mask[tbit]))
534 report("looked at single bit incorrect value!\n");
536 if(oggpack_read1(&r)!=(b[i]&mask[tbit]))
537 report("read incorrect single bit value!\n");
539 if(oggpack_read(&r,tbit)!=(b[i]&mask[tbit]))
540 report("read incorrect value!\n");
543 if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
546 void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){
548 unsigned char *buffer;
552 oggpackB_write(&o,b[i],bits?bits:ilog(b[i]));
553 buffer=oggpackB_get_buffer(&o);
554 bytes=oggpackB_bytes(&o);
555 if(bytes!=compsize)report("wrong number of bytes!\n");
556 for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
557 for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
558 report("wrote incorrect value!\n");
560 oggpackB_readinit(&r,buffer,bytes);
562 int tbit=bits?bits:ilog(b[i]);
563 if(oggpackB_look(&r,tbit)==-1)
564 report("out of data!\n");
565 if(oggpackB_look(&r,tbit)!=(b[i]&mask[tbit]))
566 report("looked at incorrect value!\n");
568 if(oggpackB_look1(&r)!=(b[i]&mask[tbit]))
569 report("looked at single bit incorrect value!\n");
571 if(oggpackB_read1(&r)!=(b[i]&mask[tbit]))
572 report("read incorrect single bit value!\n");
574 if(oggpackB_read(&r,tbit)!=(b[i]&mask[tbit]))
575 report("read incorrect value!\n");
578 if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
582 unsigned char *buffer;
584 static unsigned long testbuffer1[]=
585 {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
586 567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
589 static unsigned long testbuffer2[]=
590 {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
591 1233432,534,5,346435231,14436467,7869299,76326614,167548585,
592 85525151,0,12321,1,349528352};
595 static unsigned long testbuffer3[]=
596 {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,
597 0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
600 static unsigned long large[]=
601 {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
602 1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
603 85525151,0,12321,1,2146528352};
606 static int one[33]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
607 34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
609 static int oneB[33]={150,101,131,33,203,15,204,216,105,193,156,65,84,85,222,
610 8,139,145,227,126,34,55,244,171,85,100,39,195,173,18,
614 static int two[6]={61,255,255,251,231,29};
615 static int twoB[6]={247,63,255,253,249,120};
618 static int three[54]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
619 142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
620 58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
621 100,52,4,14,18,86,77,1};
622 static int threeB[54]={206,128,42,153,57,8,183,251,13,89,36,30,32,144,183,
623 130,59,240,121,59,85,223,19,228,180,134,33,107,74,98,
624 233,253,196,135,63,2,110,114,50,155,90,127,37,170,104,
625 200,20,254,4,58,106,176,144,0};
628 static int four[38]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
629 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
631 static int fourB[38]={36,48,102,83,243,24,52,7,4,35,132,10,145,21,2,93,2,41,
632 1,219,184,16,33,184,54,149,170,132,18,30,29,98,229,67,
636 static int five[45]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
637 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
638 84,75,159,2,1,0,132,192,8,0,0,18,22};
639 static int fiveB[45]={1,84,145,111,245,100,128,8,56,36,40,71,126,78,213,226,
640 124,105,12,0,133,128,0,162,233,242,67,152,77,205,77,
641 172,150,169,129,79,128,0,6,4,32,0,27,9,0};
644 static int six[7]={17,177,170,242,169,19,148};
645 static int sixB[7]={136,141,85,79,149,200,41};
647 /* Test read/write together */
648 /* Later we test against pregenerated bitstreams */
649 oggpack_writeinit(&o);
651 fprintf(stderr,"\nSmall preclipped packing (LSb): ");
652 cliptest(testbuffer1,test1size,0,one,onesize);
653 fprintf(stderr,"ok.");
655 fprintf(stderr,"\nNull bit call (LSb): ");
656 cliptest(testbuffer3,test3size,0,two,twosize);
657 fprintf(stderr,"ok.");
659 fprintf(stderr,"\nLarge preclipped packing (LSb): ");
660 cliptest(testbuffer2,test2size,0,three,threesize);
661 fprintf(stderr,"ok.");
663 fprintf(stderr,"\n32 bit preclipped packing (LSb): ");
665 for(i=0;i<test2size;i++)
666 oggpack_write(&o,large[i],32);
667 buffer=oggpack_get_buffer(&o);
668 bytes=oggpack_bytes(&o);
669 oggpack_readinit(&r,buffer,bytes);
670 for(i=0;i<test2size;i++){
671 if(oggpack_look(&r,32)==-1)report("out of data. failed!");
672 if(oggpack_look(&r,32)!=large[i]){
673 fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpack_look(&r,32),large[i],
674 oggpack_look(&r,32),large[i]);
675 report("read incorrect value!\n");
679 if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
680 fprintf(stderr,"ok.");
682 fprintf(stderr,"\nSmall unclipped packing (LSb): ");
683 cliptest(testbuffer1,test1size,7,four,foursize);
684 fprintf(stderr,"ok.");
686 fprintf(stderr,"\nLarge unclipped packing (LSb): ");
687 cliptest(testbuffer2,test2size,17,five,fivesize);
688 fprintf(stderr,"ok.");
690 fprintf(stderr,"\nSingle bit unclipped packing (LSb): ");
691 cliptest(testbuffer3,test3size,1,six,sixsize);
692 fprintf(stderr,"ok.");
694 fprintf(stderr,"\nTesting read past end (LSb): ");
695 oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
697 if(oggpack_read(&r,1)!=0){
698 fprintf(stderr,"failed; got -1 prematurely.\n");
702 if(oggpack_look(&r,1)!=-1 ||
703 oggpack_read(&r,1)!=-1){
704 fprintf(stderr,"failed; read past end without -1.\n");
707 oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
708 if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){
709 fprintf(stderr,"failed 2; got -1 prematurely.\n");
713 if(oggpack_look(&r,18)!=0 ||
714 oggpack_look(&r,18)!=0){
715 fprintf(stderr,"failed 3; got -1 prematurely.\n");
718 if(oggpack_look(&r,19)!=-1 ||
719 oggpack_look(&r,19)!=-1){
720 fprintf(stderr,"failed; read past end without -1.\n");
723 if(oggpack_look(&r,32)!=-1 ||
724 oggpack_look(&r,32)!=-1){
725 fprintf(stderr,"failed; read past end without -1.\n");
728 oggpack_writeclear(&o);
729 fprintf(stderr,"ok.\n");
731 /********** lazy, cut-n-paste retest with MSb packing ***********/
733 /* Test read/write together */
734 /* Later we test against pregenerated bitstreams */
735 oggpackB_writeinit(&o);
737 fprintf(stderr,"\nSmall preclipped packing (MSb): ");
738 cliptestB(testbuffer1,test1size,0,oneB,onesize);
739 fprintf(stderr,"ok.");
741 fprintf(stderr,"\nNull bit call (MSb): ");
742 cliptestB(testbuffer3,test3size,0,twoB,twosize);
743 fprintf(stderr,"ok.");
745 fprintf(stderr,"\nLarge preclipped packing (MSb): ");
746 cliptestB(testbuffer2,test2size,0,threeB,threesize);
747 fprintf(stderr,"ok.");
749 fprintf(stderr,"\n32 bit preclipped packing (MSb): ");
751 for(i=0;i<test2size;i++)
752 oggpackB_write(&o,large[i],32);
753 buffer=oggpackB_get_buffer(&o);
754 bytes=oggpackB_bytes(&o);
755 oggpackB_readinit(&r,buffer,bytes);
756 for(i=0;i<test2size;i++){
757 if(oggpackB_look(&r,32)==-1)report("out of data. failed!");
758 if(oggpackB_look(&r,32)!=large[i]){
759 fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpackB_look(&r,32),large[i],
760 oggpackB_look(&r,32),large[i]);
761 report("read incorrect value!\n");
765 if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
766 fprintf(stderr,"ok.");
768 fprintf(stderr,"\nSmall unclipped packing (MSb): ");
769 cliptestB(testbuffer1,test1size,7,fourB,foursize);
770 fprintf(stderr,"ok.");
772 fprintf(stderr,"\nLarge unclipped packing (MSb): ");
773 cliptestB(testbuffer2,test2size,17,fiveB,fivesize);
774 fprintf(stderr,"ok.");
776 fprintf(stderr,"\nSingle bit unclipped packing (MSb): ");
777 cliptestB(testbuffer3,test3size,1,sixB,sixsize);
778 fprintf(stderr,"ok.");
780 fprintf(stderr,"\nTesting read past end (MSb): ");
781 oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8);
783 if(oggpackB_read(&r,1)!=0){
784 fprintf(stderr,"failed; got -1 prematurely.\n");
788 if(oggpackB_look(&r,1)!=-1 ||
789 oggpackB_read(&r,1)!=-1){
790 fprintf(stderr,"failed; read past end without -1.\n");
793 oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8);
794 if(oggpackB_read(&r,30)!=0 || oggpackB_read(&r,16)!=0){
795 fprintf(stderr,"failed 2; got -1 prematurely.\n");
799 if(oggpackB_look(&r,18)!=0 ||
800 oggpackB_look(&r,18)!=0){
801 fprintf(stderr,"failed 3; got -1 prematurely.\n");
804 if(oggpackB_look(&r,19)!=-1 ||
805 oggpackB_look(&r,19)!=-1){
806 fprintf(stderr,"failed; read past end without -1.\n");
809 if(oggpackB_look(&r,32)!=-1 ||
810 oggpackB_look(&r,32)!=-1){
811 fprintf(stderr,"failed; read past end without -1.\n");
814 oggpackB_writeclear(&o);
815 fprintf(stderr,"ok.\n\n");
820 #endif /* _V_SELFTEST */
822 #undef BUFFER_INCREMENT