1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: packing variable sized words into an octet stream
15 last mod: $Id: bitwise.c,v 1.1 2001/02/13 01:06:24 giles Exp $
17 ********************************************************************/
19 /* We're 'LSb' endian; if we write a word but read individual bits,
20 then we'll read the lsb first */
26 #define BUFFER_INCREMENT 256
28 static unsigned long mask
[]=
29 {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
30 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
31 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
32 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
33 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
34 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
35 0x3fffffff,0x7fffffff,0xffffffff };
37 void _oggpack_writeinit(oggpack_buffer
*b
){
38 memset(b
,0,sizeof(oggpack_buffer
));
39 b
->ptr
=b
->buffer
=malloc(BUFFER_INCREMENT
);
41 b
->storage
=BUFFER_INCREMENT
;
44 void _oggpack_reset(oggpack_buffer
*b
){
47 b
->endbit
=b
->endbyte
=0;
50 void _oggpack_writeclear(oggpack_buffer
*b
){
52 memset(b
,0,sizeof(oggpack_buffer
));
55 void _oggpack_readinit(oggpack_buffer
*b
,char *buf
,int bytes
){
56 memset(b
,0,sizeof(oggpack_buffer
));
61 /* Takes only up to 32 bits. */
62 void _oggpack_write(oggpack_buffer
*b
,unsigned long value
,int bits
){
63 if(b
->endbyte
+4>=b
->storage
){
64 b
->buffer
=realloc(b
->buffer
,b
->storage
+BUFFER_INCREMENT
);
65 b
->storage
+=BUFFER_INCREMENT
;
66 b
->ptr
=b
->buffer
+b
->endbyte
;
72 b
->ptr
[0]|=value
<<b
->endbit
;
75 b
->ptr
[1]=value
>>(8-b
->endbit
);
77 b
->ptr
[2]=value
>>(16-b
->endbit
);
79 b
->ptr
[3]=value
>>(24-b
->endbit
);
82 b
->ptr
[4]=value
>>(32-b
->endbit
);
95 /* Read in bits without advancing the bitptr; bits <= 32 */
96 long _oggpack_look(oggpack_buffer
*b
,int bits
){
98 unsigned long m
=mask
[bits
];
102 if(b
->endbyte
+4>=b
->storage
){
103 /* not the main path */
104 if(b
->endbyte
+(bits
-1)/8>=b
->storage
)return(-1);
107 ret
=b
->ptr
[0]>>b
->endbit
;
109 ret
|=b
->ptr
[1]<<(8-b
->endbit
);
111 ret
|=b
->ptr
[2]<<(16-b
->endbit
);
113 ret
|=b
->ptr
[3]<<(24-b
->endbit
);
114 if(bits
>32 && b
->endbit
)
115 ret
|=b
->ptr
[4]<<(32-b
->endbit
);
122 long _oggpack_look1(oggpack_buffer
*b
){
123 if(b
->endbyte
>=b
->storage
)return(-1);
124 return((b
->ptr
[0]>>b
->endbit
)&1);
127 void _oggpack_adv(oggpack_buffer
*b
,int bits
){
134 void _oggpack_adv1(oggpack_buffer
*b
){
143 long _oggpack_read(oggpack_buffer
*b
,int bits
){
145 unsigned long m
=mask
[bits
];
149 if(b
->endbyte
+4>=b
->storage
){
150 /* not the main path */
152 if(b
->endbyte
+(bits
-1)/8>=b
->storage
)goto overflow
;
155 ret
=b
->ptr
[0]>>b
->endbit
;
157 ret
|=b
->ptr
[1]<<(8-b
->endbit
);
159 ret
|=b
->ptr
[2]<<(16-b
->endbit
);
161 ret
|=b
->ptr
[3]<<(24-b
->endbit
);
162 if(bits
>32 && b
->endbit
){
163 ret
|=b
->ptr
[4]<<(32-b
->endbit
);
178 long _oggpack_read1(oggpack_buffer
*b
){
181 if(b
->endbyte
>=b
->storage
){
182 /* not the main path */
187 ret
=(b
->ptr
[0]>>b
->endbit
)&1;
200 long _oggpack_bytes(oggpack_buffer
*b
){
201 return(b
->endbyte
+(b
->endbit
+7)/8);
204 long _oggpack_bits(oggpack_buffer
*b
){
205 return(b
->endbyte
*8+b
->endbit
);
208 char *_oggpack_buffer(oggpack_buffer
*b
){
212 /* Self test of the bitwise routines; everything else is based on
213 them, so they damned well better be solid. */
218 static int ilog(unsigned int v
){
230 void report(char *in
){
235 void cliptest(unsigned long *b
,int vals
,int bits
,int *comp
,int compsize
){
237 unsigned char *buffer
;
241 _oggpack_write(&o
,b
[i
],bits
?bits
:ilog(b
[i
]));
242 buffer
=_oggpack_buffer(&o
);
243 bytes
=_oggpack_bytes(&o
);
244 if(bytes
!=compsize
)report("wrong number of bytes!\n");
245 for(i
=0;i
<bytes
;i
++)if(buffer
[i
]!=comp
[i
]){
246 for(i
=0;i
<bytes
;i
++)fprintf(stderr
,"%x %x\n",(int)buffer
[i
],(int)comp
[i
]);
247 report("wrote incorrect value!\n");
249 _oggpack_readinit(&r
,buffer
,bytes
);
251 int tbit
=bits
?bits
:ilog(b
[i
]);
252 if(_oggpack_look(&r
,tbit
)==-1)
253 report("out of data!\n");
254 if(_oggpack_look(&r
,tbit
)!=(b
[i
]&mask
[tbit
]))
255 report("looked at incorrect value!\n");
257 if(_oggpack_look1(&r
)!=(b
[i
]&mask
[tbit
]))
258 report("looked at single bit incorrect value!\n");
260 if(_oggpack_read1(&r
)!=(b
[i
]&mask
[tbit
]))
261 report("read incorrect single bit value!\n");
263 if(_oggpack_read(&r
,tbit
)!=(b
[i
]&mask
[tbit
]))
264 report("read incorrect value!\n");
267 if(_oggpack_bytes(&r
)!=bytes
)report("leftover bytes after read!\n");
271 unsigned char *buffer
;
273 static unsigned long testbuffer1
[]=
274 {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
275 567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
278 static unsigned long testbuffer2
[]=
279 {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
280 1233432,534,5,346435231,14436467,7869299,76326614,167548585,
281 85525151,0,12321,1,349528352};
284 static unsigned long large
[]=
285 {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
286 1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
287 85525151,0,12321,1,2146528352};
289 static unsigned long testbuffer3
[]=
290 {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,
291 0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
295 static int one
[]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
296 34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
300 static int two
[]={61,255,255,251,231,29};
303 static int three
[]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
304 142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
305 58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
306 100,52,4,14,18,86,77,1};
309 static int four
[]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
310 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
314 static int five
[]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
315 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
316 84,75,159,2,1,0,132,192,8,0,0,18,22};
319 static int six
[]={17,177,170,242,169,19,148};
321 /* Test read/write together */
322 /* Later we test against pregenerated bitstreams */
323 _oggpack_writeinit(&o
);
325 fprintf(stderr
,"\nSmall preclipped packing: ");
326 cliptest(testbuffer1
,test1size
,0,one
,onesize
);
327 fprintf(stderr
,"ok.");
329 fprintf(stderr
,"\nNull bit call: ");
330 cliptest(testbuffer3
,test3size
,0,two
,twosize
);
331 fprintf(stderr
,"ok.");
333 fprintf(stderr
,"\nLarge preclipped packing: ");
334 cliptest(testbuffer2
,test2size
,0,three
,threesize
);
335 fprintf(stderr
,"ok.");
337 fprintf(stderr
,"\n32 bit preclipped packing: ");
339 for(i
=0;i
<test2size
;i
++)
340 _oggpack_write(&o
,large
[i
],32);
341 buffer
=_oggpack_buffer(&o
);
342 bytes
=_oggpack_bytes(&o
);
343 _oggpack_readinit(&r
,buffer
,bytes
);
344 for(i
=0;i
<test2size
;i
++){
345 if(_oggpack_look(&r
,32)==-1)report("out of data. failed!");
346 if(_oggpack_look(&r
,32)!=large
[i
]){
347 fprintf(stderr
,"%ld != %ld (%lx!=%lx):",_oggpack_look(&r
,32),large
[i
],
348 _oggpack_look(&r
,32),large
[i
]);
349 report("read incorrect value!\n");
353 if(_oggpack_bytes(&r
)!=bytes
)report("leftover bytes after read!\n");
354 fprintf(stderr
,"ok.");
356 fprintf(stderr
,"\nSmall unclipped packing: ");
357 cliptest(testbuffer1
,test1size
,7,four
,foursize
);
358 fprintf(stderr
,"ok.");
360 fprintf(stderr
,"\nLarge unclipped packing: ");
361 cliptest(testbuffer2
,test2size
,17,five
,fivesize
);
362 fprintf(stderr
,"ok.");
364 fprintf(stderr
,"\nSingle bit unclicpped packing: ");
365 cliptest(testbuffer3
,test3size
,1,six
,sixsize
);
366 fprintf(stderr
,"ok.");
368 fprintf(stderr
,"\nTesting read past end: ");
369 _oggpack_readinit(&r
,"\0\0\0\0\0\0\0\0",8);
371 if(_oggpack_read(&r
,1)!=0){
372 fprintf(stderr
,"failed; got -1 prematurely.\n");
376 if(_oggpack_look(&r
,1)!=-1 ||
377 _oggpack_read(&r
,1)!=-1){
378 fprintf(stderr
,"failed; read past end without -1.\n");
381 _oggpack_readinit(&r
,"\0\0\0\0\0\0\0\0",8);
382 if(_oggpack_read(&r
,30)!=0 || _oggpack_read(&r
,16)!=0){
383 fprintf(stderr
,"failed 2; got -1 prematurely.\n");
387 if(_oggpack_look(&r
,18)!=0 ||
388 _oggpack_look(&r
,18)!=0){
389 fprintf(stderr
,"failed 3; got -1 prematurely.\n");
392 if(_oggpack_look(&r
,19)!=-1 ||
393 _oggpack_look(&r
,19)!=-1){
394 fprintf(stderr
,"failed; read past end without -1.\n");
397 if(_oggpack_look(&r
,32)!=-1 ||
398 _oggpack_look(&r
,32)!=-1){
399 fprintf(stderr
,"failed; read past end without -1.\n");
402 fprintf(stderr
,"ok.\n\n");
408 #undef BUFFER_INCREMENT