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 14546 2008-02-29 01:14:05Z tterribe $
16 ********************************************************************/
18 /* We're 'MSb' endian; if we write a word but read individual bits,
19 then we'll read the msb first */
25 void theorapackB_reset(oggpack_buffer
*b
){
28 b
->endbit
=b
->endbyte
=0;
31 void theorapackB_readinit(oggpack_buffer
*b
,unsigned char *buf
,int bytes
){
32 memset(b
,0,sizeof(*b
));
37 int theorapackB_look1(oggpack_buffer
*b
,long *_ret
){
38 if(b
->endbyte
>=b
->storage
){
42 *_ret
=((b
->ptr
[0]>>(7-b
->endbit
))&1);
46 void theorapackB_adv1(oggpack_buffer
*b
){
55 int theorapackB_read(oggpack_buffer
*b
,int bits
,long *_ret
){
61 if(b
->endbyte
+4>=b
->storage
){
62 /* not the main path */
63 if(b
->endbyte
*8+bits
>b
->storage
*8){
68 /* special case to avoid reading b->ptr[0], which might be past the end of
69 the buffer; also skips some useless accounting */
75 ret
=b
->ptr
[0]<<(24+b
->endbit
);
77 ret
|=b
->ptr
[1]<<(16+b
->endbit
);
79 ret
|=b
->ptr
[2]<<(8+b
->endbit
);
81 ret
|=b
->ptr
[3]<<(b
->endbit
);
82 if(bits
>32 && b
->endbit
)
83 ret
|=b
->ptr
[4]>>(8-b
->endbit
);
87 *_ret
=((ret
&0xffffffffUL
)>>(m
>>1))>>((m
+1)>>1);
96 int theorapackB_read1(oggpack_buffer
*b
,long *_ret
){
98 if(b
->endbyte
>=b
->storage
){
99 /* not the main path */
104 *_ret
=(b
->ptr
[0]>>(7-b
->endbit
))&1;
116 long theorapackB_bytes(oggpack_buffer
*b
){
117 return(b
->endbyte
+(b
->endbit
+7)/8);
120 long theorapackB_bits(oggpack_buffer
*b
){
121 return(b
->endbyte
*8+b
->endbit
);
124 unsigned char *theorapackB_get_buffer(oggpack_buffer
*b
){