3 implementation of bitio.h.
5 Copyright (C) 2003 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
27 #include "../config.h"
31 #define ZLIB_BUFFER_SIZE 16384
35 /* ---------------------------- file reader ------------------------------- */
37 static int reader_fileread(struct reader_t*reader, void* data, int len)
39 int ret = read((int)reader->internal, data, len);
44 void reader_init_filereader(struct reader_t*r, int handle)
46 r->read = reader_fileread;
47 r->internal = (void*)handle;
48 r->type = READER_TYPE_FILE;
54 /* ---------------------------- mem reader ------------------------------- */
61 static int reader_memread(struct reader_t*reader, void* data, int len)
63 struct memread_t*mr = (struct memread_t*)reader->internal;
65 if(mr->length - reader->pos > len) {
66 memcpy(data, &mr->data[reader->pos], len);
70 memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
71 reader->pos = mr->length;
72 return mr->length - reader->pos;
75 void reader_init_memreader(struct reader_t*r, void*newdata, int newlength)
77 struct memread_t*mr = malloc(sizeof(struct memread_t));
79 mr->length = newlength;
80 r->read = reader_memread;
81 r->internal = (void*)mr;
82 r->type = READER_TYPE_MEM;
88 /* ---------------------------- mem writer ------------------------------- */
96 static int writer_memwrite_write(struct writer_t*w, void* data, int len)
98 struct memread_t*mw = (struct memread_t*)w->internal;
99 if(mw->length - w->pos > len) {
100 memcpy(&mw->data[w->pos], data, len);
104 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
106 return mw->length - w->pos;
109 static void writer_memwrite_finish(struct writer_t*w)
113 void writer_init_memwriter(struct writer_t*w, void*data, int len)
115 struct memwrite_t *mr;
116 mr = malloc(sizeof(struct memwrite_t));
119 memset(w, 0, sizeof(struct writer_t));
120 w->write = writer_memwrite_write;
121 w->finish = writer_memwrite_finish;
122 w->internal = (void*)mr;
123 w->type = WRITER_TYPE_FILE;
129 /* ---------------------------- file writer ------------------------------- */
137 static int writer_filewrite_write(struct writer_t*w, void* data, int len)
139 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
140 return write(fw->handle, data, len);
142 static void writer_filewrite_finish(struct writer_t*w)
144 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
149 void writer_init_filewriter(struct writer_t*w, int handle)
151 struct filewrite_t *mr = malloc(sizeof(struct filewrite_t));
154 memset(w, 0, sizeof(struct writer_t));
155 w->write = writer_filewrite_write;
156 w->finish = writer_filewrite_finish;
158 w->type = WRITER_TYPE_FILE;
163 void writer_init_filewriter2(struct writer_t*w, char*filename)
165 int fi = open("movie.swf",
169 O_WRONLY|O_CREAT|O_TRUNC, 0644);
170 writer_init_filewriter(w, fi);
171 ((struct filewrite_t*)w->internal)->free_handle = 1;
174 /* ---------------------------- null writer ------------------------------- */
176 static int writer_nullwrite_write(struct writer_t*w, void* data, int len)
181 static void writer_nullwrite_finish(struct writer_t*w)
184 void writer_init_nullwriter(struct writer_t*w)
186 memset(w, 0, sizeof(struct writer_t));
187 w->write = writer_nullwrite_write;
188 w->finish = writer_nullwrite_finish;
190 w->type = WRITER_TYPE_NULL;
195 /* ---------------------------- zlibinflate writer -------------------------- */
201 struct reader_t*input;
202 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
207 static void zlib_error(int ret, char* msg, z_stream*zs)
209 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
212 zs->msg?zs->msg:"unknown");
218 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
221 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
229 z->zs.next_out = data;
230 z->zs.avail_out = len;
233 if(!z->zs.avail_in) {
234 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
235 z->zs.next_in = z->readbuffer;
238 ret = inflate(&z->zs, Z_NO_FLUSH);
240 ret = inflate(&z->zs, Z_FINISH);
243 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
245 if (ret == Z_STREAM_END) {
246 int pos = z->zs.next_out - (Bytef*)data;
247 ret = inflateEnd(&z->zs);
248 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
249 free(reader->internal);
250 reader->internal = 0;
254 if(!z->zs.avail_out) {
261 fprintf(stderr, "Error: swftools was compiled without zlib support");
265 void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
268 struct zlibinflate_t*z;
270 memset(r, 0, sizeof(struct reader_t));
271 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
272 memset(z, 0, sizeof(struct zlibinflate_t));
274 r->read = reader_zlibinflate;
275 r->type = READER_TYPE_ZLIB;
278 memset(&z->zs,0,sizeof(z_stream));
279 z->zs.zalloc = Z_NULL;
280 z->zs.zfree = Z_NULL;
281 z->zs.opaque = Z_NULL;
282 ret = inflateInit(&z->zs);
283 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
286 fprintf(stderr, "Error: swftools was compiled without zlib support");
290 /* ---------------------------- zlibdeflate writer -------------------------- */
296 struct writer_t*output;
297 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
301 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
304 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
307 fprintf(stderr, "zlib not initialized!\n");
313 z->zs.next_in = data;
314 z->zs.avail_in = len;
317 ret = deflate(&z->zs, Z_NO_FLUSH);
319 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
321 if(z->zs.next_out != z->writebuffer) {
322 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
323 z->zs.next_out = z->writebuffer;
324 z->zs.avail_out = ZLIB_BUFFER_SIZE;
327 if(!z->zs.avail_in) {
334 fprintf(stderr, "Error: swftools was compiled without zlib support");
338 static void writer_zlibdeflate_finish(struct writer_t*writer)
341 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
342 struct writer_t*output;
348 ret = deflate(&z->zs, Z_FINISH);
350 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
352 if(z->zs.next_out != z->writebuffer) {
353 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
354 z->zs.next_out = z->writebuffer;
355 z->zs.avail_out = ZLIB_BUFFER_SIZE;
358 if (ret == Z_STREAM_END) {
363 ret = deflateEnd(&z->zs);
364 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
365 free(writer->internal);
366 writer->internal = 0;
367 output->finish(output);
369 fprintf(stderr, "Error: swftools was compiled without zlib support");
373 void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
376 struct zlibdeflate_t*z;
378 memset(w, 0, sizeof(struct writer_t));
379 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
380 memset(z, 0, sizeof(struct zlibdeflate_t));
382 w->write = writer_zlibdeflate_write;
383 w->finish = writer_zlibdeflate_finish;
384 w->type = WRITER_TYPE_ZLIB;
387 memset(&z->zs,0,sizeof(z_stream));
388 z->zs.zalloc = Z_NULL;
389 z->zs.zfree = Z_NULL;
390 z->zs.opaque = Z_NULL;
391 ret = deflateInit(&z->zs, 9);
392 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
395 z->zs.next_out = z->writebuffer;
396 z->zs.avail_out = ZLIB_BUFFER_SIZE;
398 fprintf(stderr, "Error: swftools was compiled without zlib support");
403 /* ----------------------- bit handling routines -------------------------- */
405 void writer_writebit(struct writer_t*w, int bit)
409 w->write(w, &w->mybyte, 1);
414 w->mybyte |= 1 << (7 - w->bitpos);
417 void writer_writebits(struct writer_t*w, unsigned int data, int bits)
422 writer_writebit(w, (data >> (bits-t-1))&1);
425 void writer_resetbits(struct writer_t*w)
428 w->write(w, &w->mybyte, 1);
433 unsigned int reader_readbit(struct reader_t*r)
438 r->read(r, &r->mybyte, 1);
440 return (r->mybyte>>(7-r->bitpos++))&1;
442 unsigned int reader_readbits(struct reader_t*r, int num)
449 val|=reader_readbit(r);
453 void reader_resetbits(struct reader_t*r)