1 #define READER_TYPE_FILE 0
2 #define READER_TYPE_MEM 1
3 #define READER_TYPE_ZLIB 2
4 #define WRITER_TYPE_FILE 0
5 #define WRITER_TYPE_MEM 1
6 #define WRITER_TYPE_ZLIB 2
10 int (*read)(struct reader_t*, void*data, int len);
18 int (*write)(struct writer_t*, void*data, int len);
19 void (*finish)(struct writer_t*);
25 static int reader_fileread(struct reader_t*reader, void* data, int len)
27 return read((int)reader->internal, data, len);
29 static void reader_resetbits(struct reader_t*r)
34 static void reader_init_filereader(struct reader_t*r, int handle)
36 r->read = reader_fileread;
37 r->internal = (void*)handle;
38 r->type = READER_TYPE_FILE;
42 #define ZLIB_BUFFER_SIZE 16384
46 struct reader_t*input;
47 U8 readbuffer[ZLIB_BUFFER_SIZE];
49 static void zlib_error(int ret, char* msg, z_stream*zs)
51 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
54 zs->msg?zs->msg:"unknown");
58 static int reader_zlibinflate(struct reader_t*reader, void* data, int len);
59 static void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
61 struct zlibinflate_t*z;
63 memset(r, 0, sizeof(struct reader_t));
64 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
65 memset(z, 0, sizeof(struct zlibinflate_t));
67 r->read = reader_zlibinflate;
68 r->type = READER_TYPE_ZLIB;
70 memset(&z->zs,0,sizeof(z_stream));
71 z->zs.zalloc = Z_NULL;
73 z->zs.opaque = Z_NULL;
74 ret = inflateInit(&z->zs);
75 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
78 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
80 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
85 z->zs.next_out = data;
86 z->zs.avail_out = len;
90 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
91 z->zs.next_in = z->readbuffer;
94 ret = inflate(&z->zs, Z_NO_FLUSH);
96 ret = inflate(&z->zs, Z_FINISH);
99 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
101 if (ret == Z_STREAM_END) {
102 int pos = z->zs.next_out - (Bytef*)data;
103 ret = inflateEnd(&z->zs);
104 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
105 free(reader->internal);
106 reader->internal = 0;
109 if(!z->zs.avail_out) {
115 static unsigned int reader_readbit(struct reader_t*r)
120 r->read(r, &r->mybyte, 1);
122 return (r->mybyte>>(7-r->bitpos++))&1;
124 static unsigned int reader_readbits(struct reader_t*r, int num)
131 val|=reader_readbit(r);
135 static int writer_filewrite_write(struct writer_t*w, void* data, int len)
137 return write((int)w->internal, data, len);
139 static void writer_filewrite_finish(struct writer_t*w)
142 static void writer_resetbits(struct writer_t*w)
145 w->write(w, &w->mybyte, 1);
149 static void writer_init_filewriter(struct writer_t*w, int handle)
151 memset(w, 0, sizeof(struct writer_t));
152 w->write = writer_filewrite_write;
153 w->finish = writer_filewrite_finish;
154 w->internal = (void*)handle;
155 w->type = WRITER_TYPE_FILE;
162 struct writer_t*output;
163 U8 writebuffer[ZLIB_BUFFER_SIZE];
165 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len);
166 static void writer_zlibdeflate_finish(struct writer_t*writer);
167 static void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
169 struct zlibdeflate_t*z;
171 memset(w, 0, sizeof(struct writer_t));
172 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
173 memset(z, 0, sizeof(struct zlibdeflate_t));
175 w->write = writer_zlibdeflate_write;
176 w->finish = writer_zlibdeflate_finish;
177 w->type = WRITER_TYPE_ZLIB;
179 memset(&z->zs,0,sizeof(z_stream));
180 z->zs.zalloc = Z_NULL;
181 z->zs.zfree = Z_NULL;
182 z->zs.opaque = Z_NULL;
183 ret = deflateInit(&z->zs, 9);
184 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
187 z->zs.next_out = z->writebuffer;
188 z->zs.avail_out = ZLIB_BUFFER_SIZE;
190 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
192 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
197 z->zs.next_in = data;
198 z->zs.avail_in = len;
201 ret = deflate(&z->zs, Z_NO_FLUSH);
203 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
205 if(z->zs.next_out != z->writebuffer) {
206 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
207 z->zs.next_out = z->writebuffer;
208 z->zs.avail_out = ZLIB_BUFFER_SIZE;
211 if(!z->zs.avail_in) {
217 static void writer_zlibdeflate_finish(struct writer_t*writer)
219 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
220 struct writer_t*output;
226 ret = deflate(&z->zs, Z_FINISH);
228 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
230 if(z->zs.next_out != z->writebuffer) {
231 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
232 z->zs.next_out = z->writebuffer;
233 z->zs.avail_out = ZLIB_BUFFER_SIZE;
236 if (ret == Z_STREAM_END) {
241 ret = deflateEnd(&z->zs);
242 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
243 free(writer->internal);
244 writer->internal = 0;
245 output->finish(output);
248 static void writer_writebit(struct writer_t*w, int bit)
252 w->write(w, &w->mybyte, 1);
257 w->mybyte |= 1 << (7 - w->bitpos);
260 static void writer_writebits(struct writer_t*w, U32 data, int bits)
265 writer_writebit(w, (data >> (bits-t-1))&1);