Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / media / libtheora / lib / dec / bitwise.c
blobbe118d1b08bc2d436a5a2bbd42938e633b200a5a
1 /********************************************************************
2 * *
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. *
7 * *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
10 * *
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 */
21 #include <string.h>
22 #include <stdlib.h>
23 #include "bitwise.h"
25 void theorapackB_reset(oggpack_buffer *b){
26 b->ptr=b->buffer;
27 b->buffer[0]=0;
28 b->endbit=b->endbyte=0;
31 void theorapackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
32 memset(b,0,sizeof(*b));
33 b->buffer=b->ptr=buf;
34 b->storage=bytes;
37 int theorapackB_look1(oggpack_buffer *b,long *_ret){
38 if(b->endbyte>=b->storage){
39 *_ret=0L;
40 return -1;
42 *_ret=((b->ptr[0]>>(7-b->endbit))&1);
43 return 0;
46 void theorapackB_adv1(oggpack_buffer *b){
47 if(++(b->endbit)>7){
48 b->endbit=0;
49 b->ptr++;
50 b->endbyte++;
54 /* bits <= 32 */
55 int theorapackB_read(oggpack_buffer *b,int bits,long *_ret){
56 long ret;
57 long m;
58 int fail;
59 m=32-bits;
60 bits+=b->endbit;
61 if(b->endbyte+4>=b->storage){
62 /* not the main path */
63 if(b->endbyte*8+bits>b->storage*8){
64 *_ret=0L;
65 fail=-1;
66 goto overflow;
68 /* special case to avoid reading b->ptr[0], which might be past the end of
69 the buffer; also skips some useless accounting */
70 else if(!bits){
71 *_ret=0L;
72 return 0;
75 ret=b->ptr[0]<<(24+b->endbit);
76 if(bits>8){
77 ret|=b->ptr[1]<<(16+b->endbit);
78 if(bits>16){
79 ret|=b->ptr[2]<<(8+b->endbit);
80 if(bits>24){
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);
88 fail=0;
89 overflow:
90 b->ptr+=bits/8;
91 b->endbyte+=bits/8;
92 b->endbit=bits&7;
93 return fail;
96 int theorapackB_read1(oggpack_buffer *b,long *_ret){
97 int fail;
98 if(b->endbyte>=b->storage){
99 /* not the main path */
100 *_ret=0L;
101 fail=-1;
102 goto overflow;
104 *_ret=(b->ptr[0]>>(7-b->endbit))&1;
105 fail=0;
106 overflow:
107 b->endbit++;
108 if(b->endbit>7){
109 b->endbit=0;
110 b->ptr++;
111 b->endbyte++;
113 return fail;
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){
125 return(b->buffer);