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)
180 static void writer_nullwrite_finish(struct writer_t*w)
183 void writer_init_nullwriter(struct writer_t*w)
185 memset(w, 0, sizeof(struct writer_t));
186 w->write = writer_nullwrite_write;
187 w->finish = writer_nullwrite_finish;
189 w->type = WRITER_TYPE_NULL;
194 /* ---------------------------- zlibinflate writer -------------------------- */
200 struct reader_t*input;
201 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
206 static void zlib_error(int ret, char* msg, z_stream*zs)
208 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
211 zs->msg?zs->msg:"unknown");
217 static int reader_zlibinflate(struct reader_t*reader, void* data, int len)
220 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
228 z->zs.next_out = data;
229 z->zs.avail_out = len;
232 if(!z->zs.avail_in) {
233 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
234 z->zs.next_in = z->readbuffer;
237 ret = inflate(&z->zs, Z_NO_FLUSH);
239 ret = inflate(&z->zs, Z_FINISH);
242 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
244 if (ret == Z_STREAM_END) {
245 int pos = z->zs.next_out - (Bytef*)data;
246 ret = inflateEnd(&z->zs);
247 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
248 free(reader->internal);
249 reader->internal = 0;
253 if(!z->zs.avail_out) {
260 fprintf(stderr, "Error: swftools was compiled without zlib support");
264 void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input)
267 struct zlibinflate_t*z;
269 memset(r, 0, sizeof(struct reader_t));
270 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
271 memset(z, 0, sizeof(struct zlibinflate_t));
273 r->read = reader_zlibinflate;
274 r->type = READER_TYPE_ZLIB;
277 memset(&z->zs,0,sizeof(z_stream));
278 z->zs.zalloc = Z_NULL;
279 z->zs.zfree = Z_NULL;
280 z->zs.opaque = Z_NULL;
281 ret = inflateInit(&z->zs);
282 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
285 fprintf(stderr, "Error: swftools was compiled without zlib support");
289 /* ---------------------------- zlibdeflate writer -------------------------- */
295 struct writer_t*output;
296 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
300 static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len)
303 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
306 fprintf(stderr, "zlib not initialized!\n");
312 z->zs.next_in = data;
313 z->zs.avail_in = len;
316 ret = deflate(&z->zs, Z_NO_FLUSH);
318 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
320 if(z->zs.next_out != z->writebuffer) {
321 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
322 z->zs.next_out = z->writebuffer;
323 z->zs.avail_out = ZLIB_BUFFER_SIZE;
326 if(!z->zs.avail_in) {
333 fprintf(stderr, "Error: swftools was compiled without zlib support");
337 static void writer_zlibdeflate_finish(struct writer_t*writer)
340 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
341 struct writer_t*output;
347 ret = deflate(&z->zs, Z_FINISH);
349 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
351 if(z->zs.next_out != z->writebuffer) {
352 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
353 z->zs.next_out = z->writebuffer;
354 z->zs.avail_out = ZLIB_BUFFER_SIZE;
357 if (ret == Z_STREAM_END) {
362 ret = deflateEnd(&z->zs);
363 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
364 free(writer->internal);
365 writer->internal = 0;
366 output->finish(output);
368 fprintf(stderr, "Error: swftools was compiled without zlib support");
372 void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output)
375 struct zlibdeflate_t*z;
377 memset(w, 0, sizeof(struct writer_t));
378 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
379 memset(z, 0, sizeof(struct zlibdeflate_t));
381 w->write = writer_zlibdeflate_write;
382 w->finish = writer_zlibdeflate_finish;
383 w->type = WRITER_TYPE_ZLIB;
386 memset(&z->zs,0,sizeof(z_stream));
387 z->zs.zalloc = Z_NULL;
388 z->zs.zfree = Z_NULL;
389 z->zs.opaque = Z_NULL;
390 ret = deflateInit(&z->zs, 9);
391 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
394 z->zs.next_out = z->writebuffer;
395 z->zs.avail_out = ZLIB_BUFFER_SIZE;
397 fprintf(stderr, "Error: swftools was compiled without zlib support");
402 /* ----------------------- bit handling routines -------------------------- */
404 void writer_writebit(struct writer_t*w, int bit)
408 w->write(w, &w->mybyte, 1);
413 w->mybyte |= 1 << (7 - w->bitpos);
416 void writer_writebits(struct writer_t*w, unsigned int data, int bits)
421 writer_writebit(w, (data >> (bits-t-1))&1);
424 void writer_resetbits(struct writer_t*w)
427 w->write(w, &w->mybyte, 1);
432 unsigned int reader_readbit(struct reader_t*r)
437 r->read(r, &r->mybyte, 1);
439 return (r->mybyte>>(7-r->bitpos++))&1;
441 unsigned int reader_readbits(struct reader_t*r, int num)
448 val|=reader_readbit(r);
452 void reader_resetbits(struct reader_t*r)