2 Various routines for reading and writing bit- and bytewise, from and to memory.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
13 static int datalength;
18 void reader_init(uchar*newdata, int newlength)
21 datalength = newlength;
29 static u8 bitpos=8,mybyte;
36 void input1(void*target)
38 *(uchar*)target = *(uchar*)&data[datapos];
41 void input2(void*target)
43 *(unsigned short int*)target = *(unsigned short int*)&data[datapos];
46 void input4(void*target)
48 *(unsigned int*)target = *(unsigned int*)&data[datapos];
53 return &data[datapos];
59 void setinputpos(uchar*pos)
71 return (mybyte>>(7-bitpos++))&1;
73 void readbits(u32*val,int num)
84 void readsbits(s32*val,int num)
102 s32 getsbits(int num)
121 return ((u16)b)*256+a;
131 return (((((u32)d)*256+(u32)c)*256+(u32)b)*256+(u32)a);
134 void writer_init(struct writer_t*w, u8*data, int maxlength)
137 w->maxlength = maxlength;
143 void writer_write(struct writer_t*w, void*data, int length)
145 memcpy(&w->data[w->pos], data, length);
149 void writer_writeu8(struct writer_t*w, u8 value)
152 writer_write(w, &value, 1);
155 void writer_writeu16(struct writer_t*w, u16 value)
158 writer_write(w, &value, 2);
161 void writer_writeu32(struct writer_t*w, u32 value)
164 writer_write(w, &value, 4);
167 void* writer_getpos(struct writer_t*w)
169 return &w->data[w->pos];
172 void writer_resetbits(struct writer_t*w)
175 writer_write(w, &w->mybyte, 1);
180 void writer_writebit(struct writer_t*w, int bit)
184 writer_write(w, &w->mybyte, 1);
189 w->mybyte |= 1 << (7 - w->bitpos);
193 void writer_writebits(struct writer_t*w, u32 data, int bits)
198 writer_writebit(w, (data >> (bits-t-1))&1);