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)
119 // I'm not using input2(&a) here because our input is
123 return ((u16)b)*256+a;
126 void writer_init(struct writer_t*w, u8*data, int maxlength)
129 w->maxlength = maxlength;
135 void writer_write(struct writer_t*w, void*data, int length)
137 memcpy(&w->data[w->pos], data, length);
141 void writer_writeu8(struct writer_t*w, u8 value)
144 writer_write(w, &value, 1);
147 void writer_writeu16(struct writer_t*w, u16 value)
150 writer_write(w, &value, 2);
153 void* writer_getpos(struct writer_t*w)
155 return &w->data[w->pos];
158 void writer_resetbits(struct writer_t*w)
161 writer_write(w, &w->mybyte, 1);
166 void writer_writebit(struct writer_t*w, int bit)
170 writer_write(w, &w->mybyte, 1);
175 w->mybyte |= 1 << (7 - w->bitpos);
179 void writer_writebits(struct writer_t*w, u32 data, int bits)
184 writer_writebit(w, (data >> (bits-t-1))&1);