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 */
34 #include "../config.h"
38 #define ZLIB_BUFFER_SIZE 16384
42 /* ---------------------------- null reader ------------------------------- */
44 static int reader_nullread(reader_t*r, void* data, int len)
49 static void reader_nullread_dealloc(reader_t*r)
51 memset(r, 0, sizeof(reader_t));
53 void reader_init_nullreader(reader_t*r)
55 r->read = reader_nullread;
56 r->dealloc = reader_nullread_dealloc;
58 r->type = READER_TYPE_NULL;
63 /* ---------------------------- file reader ------------------------------- */
65 static int reader_fileread(reader_t*reader, void* data, int len)
67 int ret = read((int)reader->internal, data, len);
72 static void reader_fileread_dealloc(reader_t*r)
74 memset(r, 0, sizeof(reader_t));
76 void reader_init_filereader(reader_t*r, int handle)
78 r->read = reader_fileread;
79 r->dealloc = reader_fileread_dealloc;
80 r->internal = (void*)handle;
81 r->type = READER_TYPE_FILE;
87 /* ---------------------------- mem reader ------------------------------- */
89 typedef struct _memread
95 static int reader_memread(reader_t*reader, void* data, int len)
97 memread_t*mr = (memread_t*)reader->internal;
99 if(mr->length - reader->pos < len) {
100 len = mr->length - reader->pos;
102 memcpy(data, &mr->data[reader->pos], len);
106 static void reader_memread_dealloc(reader_t*reader)
109 free(reader->internal);
110 memset(reader, 0, sizeof(reader_t));
112 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
114 memread_t*mr = (memread_t*)malloc(sizeof(memread_t));
115 mr->data = (unsigned char*)newdata;
116 mr->length = newlength;
117 r->read = reader_memread;
118 r->dealloc = reader_memread_dealloc;
119 r->internal = (void*)mr;
120 r->type = READER_TYPE_MEM;
126 /* ---------------------------- mem writer ------------------------------- */
128 typedef struct _memwrite
134 static int writer_memwrite_write(writer_t*w, void* data, int len)
136 memwrite_t*mw = (memwrite_t*)w->internal;
137 if(mw->length - w->pos > len) {
138 memcpy(&mw->data[w->pos], data, len);
142 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
144 return mw->length - w->pos;
147 static void writer_memwrite_finish(writer_t*w)
153 static void dummy_flush(writer_t*w)
156 void writer_init_memwriter(writer_t*w, void*data, int len)
159 mr = (memwrite_t*)malloc(sizeof(memwrite_t));
160 mr->data = (unsigned char *)data;
162 memset(w, 0, sizeof(writer_t));
163 w->write = writer_memwrite_write;
164 w->flush = dummy_flush;
165 w->finish = writer_memwrite_finish;
166 w->internal = (void*)mr;
167 w->type = WRITER_TYPE_MEM;
173 /* ------------------------- growing mem writer ------------------------------- */
175 typedef struct _growmemwrite
181 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
183 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
185 fprintf(stderr, "Illegal write operation: data already given away");
188 if(mw->length - w->pos < len) {
189 int newlength = mw->length;
190 while(newlength - w->pos < len) {
191 newlength += mw->grow;
194 unsigned char*newmem = (unsigned char*)malloc(newlength);
195 memcpy(newmem, mw->data, mw->length);
199 mw->data = (unsigned char*)realloc(mw->data, newlength);
201 mw->length = newlength;
203 memcpy(&mw->data[w->pos], data, len);
207 static void writer_growmemwrite_finish(writer_t*w)
209 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
211 free(mw->data);mw->data = 0;
214 free(w->internal);mw=0;
215 memset(w, 0, sizeof(writer_t));
217 void* writer_growmemwrite_memptr(writer_t*w, int*len)
219 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
225 void* writer_growmemwrite_getmem(writer_t*w)
227 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
229 /* remove own reference so that neither write() nor finish() can free it.
230 It's property of the caller now.
235 void writer_growmemwrite_reset(writer_t*w)
237 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
242 void writer_init_growingmemwriter(writer_t*w, U32 grow)
244 growmemwrite_t *mw = (growmemwrite_t *)malloc(sizeof(growmemwrite_t));
246 mw->data = (unsigned char *)malloc(mw->length);
248 memset(w, 0, sizeof(writer_t));
249 w->write = writer_growmemwrite_write;
250 w->flush = dummy_flush;
251 w->finish = writer_growmemwrite_finish;
252 w->internal = (void*)mw;
253 w->type = WRITER_TYPE_GROWING_MEM;
259 /* ---------------------------- file writer ------------------------------- */
261 typedef struct _filewrite
267 static int writer_filewrite_write(writer_t*w, void* data, int len)
269 filewrite_t * fw= (filewrite_t*)w->internal;
271 return write(fw->handle, data, len);
273 static void writer_filewrite_finish(writer_t*w)
275 filewrite_t *mr = (filewrite_t*)w->internal;
279 memset(w, 0, sizeof(writer_t));
281 void writer_init_filewriter(writer_t*w, int handle)
283 filewrite_t *mr = (filewrite_t *)malloc(sizeof(filewrite_t));
286 memset(w, 0, sizeof(writer_t));
287 w->write = writer_filewrite_write;
288 w->finish = writer_filewrite_finish;
290 w->type = WRITER_TYPE_FILE;
295 void writer_init_filewriter2(writer_t*w, char*filename)
297 int fi = open("movie.swf",
301 O_WRONLY|O_CREAT|O_TRUNC, 0644);
302 writer_init_filewriter(w, fi);
303 ((filewrite_t*)w->internal)->free_handle = 1;
306 /* ---------------------------- null writer ------------------------------- */
308 static int writer_nullwrite_write(writer_t*w, void* data, int len)
313 static void writer_nullwrite_finish(writer_t*w)
315 memset(w, 0, sizeof(writer_t));
317 void writer_init_nullwriter(writer_t*w)
319 memset(w, 0, sizeof(writer_t));
320 w->write = writer_nullwrite_write;
321 w->flush = dummy_flush;
322 w->finish = writer_nullwrite_finish;
324 w->type = WRITER_TYPE_NULL;
329 /* ---------------------------- zlibinflate reader -------------------------- */
331 typedef struct _zlibinflate
336 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
341 static void zlib_error(int ret, char* msg, z_stream*zs)
343 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
346 zs->msg?zs->msg:"unknown");
347 if(errno) perror("errno:");
352 static int reader_zlibinflate(reader_t*reader, void* data, int len)
355 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
363 z->zs.next_out = (Bytef *)data;
364 z->zs.avail_out = len;
367 if(!z->zs.avail_in) {
368 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
369 z->zs.next_in = z->readbuffer;
372 ret = inflate(&z->zs, Z_NO_FLUSH);
374 ret = inflate(&z->zs, Z_FINISH);
377 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
379 if (ret == Z_STREAM_END) {
380 int pos = z->zs.next_out - (Bytef*)data;
381 ret = inflateEnd(&z->zs);
382 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
383 free(reader->internal);
384 reader->internal = 0;
388 if(!z->zs.avail_out) {
395 fprintf(stderr, "Error: swftools was compiled without zlib support");
399 static void reader_zlibinflate_dealloc(reader_t*reader)
402 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
403 /* test whether read() already did basic deallocation */
404 if(reader->internal) {
406 free(reader->internal);
408 memset(reader, 0, sizeof(reader_t));
411 void reader_init_zlibinflate(reader_t*r, reader_t*input)
414 zlibinflate_t*z = (zlibinflate_t*)malloc(sizeof(zlibinflate_t));
415 memset(z, 0, sizeof(zlibinflate_t));
417 memset(r, 0, sizeof(reader_t));
419 r->read = reader_zlibinflate;
420 r->dealloc = reader_zlibinflate_dealloc;
421 r->type = READER_TYPE_ZLIB;
424 memset(&z->zs,0,sizeof(z_stream));
425 z->zs.zalloc = Z_NULL;
426 z->zs.zfree = Z_NULL;
427 z->zs.opaque = Z_NULL;
428 ret = inflateInit(&z->zs);
429 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
432 fprintf(stderr, "Error: swftools was compiled without zlib support");
437 /* ---------------------------- zlibdeflate writer -------------------------- */
439 typedef struct _zlibdeflate
444 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
448 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
451 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
453 if(writer->type != WRITER_TYPE_ZLIB) {
454 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
458 fprintf(stderr, "zlib not initialized!\n");
464 z->zs.next_in = (Bytef *)data;
465 z->zs.avail_in = len;
468 ret = deflate(&z->zs, Z_NO_FLUSH);
470 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
472 if(z->zs.next_out != z->writebuffer) {
473 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
474 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
475 z->zs.next_out = z->writebuffer;
476 z->zs.avail_out = ZLIB_BUFFER_SIZE;
479 if(!z->zs.avail_in) {
485 fprintf(stderr, "Error: swftools was compiled without zlib support");
490 void writer_zlibdeflate_flush(writer_t*writer)
493 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
495 if(writer->type != WRITER_TYPE_ZLIB) {
496 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
500 fprintf(stderr, "zlib not initialized!\n");
507 ret = deflate(&z->zs, Z_SYNC_FLUSH);
508 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_flush", &z->zs);
509 if(z->zs.next_out != z->writebuffer) {
510 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
511 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
512 z->zs.next_out = z->writebuffer;
513 z->zs.avail_out = ZLIB_BUFFER_SIZE;
515 /* TODO: how will zlib let us know it needs more buffer space? */
520 fprintf(stderr, "Error: swftools was compiled without zlib support");
525 static void writer_zlibdeflate_finish(writer_t*writer)
528 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
531 if(writer->type != WRITER_TYPE_ZLIB) {
532 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
539 ret = deflate(&z->zs, Z_FINISH);
541 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
543 if(z->zs.next_out != z->writebuffer) {
544 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
545 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
546 z->zs.next_out = z->writebuffer;
547 z->zs.avail_out = ZLIB_BUFFER_SIZE;
550 if (ret == Z_STREAM_END) {
555 ret = deflateEnd(&z->zs);
556 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
557 free(writer->internal);
558 memset(writer, 0, sizeof(writer_t));
559 //output->finish(output);
561 fprintf(stderr, "Error: swftools was compiled without zlib support");
565 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
570 memset(w, 0, sizeof(writer_t));
571 z = (zlibdeflate_t*)malloc(sizeof(zlibdeflate_t));
572 memset(z, 0, sizeof(zlibdeflate_t));
574 w->write = writer_zlibdeflate_write;
575 w->flush = writer_zlibdeflate_flush;
576 w->finish = writer_zlibdeflate_finish;
577 w->type = WRITER_TYPE_ZLIB;
580 memset(&z->zs,0,sizeof(z_stream));
581 z->zs.zalloc = Z_NULL;
582 z->zs.zfree = Z_NULL;
583 z->zs.opaque = Z_NULL;
584 ret = deflateInit(&z->zs, 9);
585 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
588 z->zs.next_out = z->writebuffer;
589 z->zs.avail_out = ZLIB_BUFFER_SIZE;
591 fprintf(stderr, "Error: swftools was compiled without zlib support");
596 /* ----------------------- bit handling routines -------------------------- */
598 void writer_writebit(writer_t*w, int bit)
602 w->write(w, &w->mybyte, 1);
607 w->mybyte |= 1 << (7 - w->bitpos);
610 void writer_writebits(writer_t*w, unsigned int data, int bits)
615 writer_writebit(w, (data >> (bits-t-1))&1);
618 void writer_resetbits(writer_t*w)
621 w->write(w, &w->mybyte, 1);
626 unsigned int reader_readbit(reader_t*r)
631 r->read(r, &r->mybyte, 1);
633 return (r->mybyte>>(7-r->bitpos++))&1;
635 unsigned int reader_readbits(reader_t*r, int num)
642 val|=reader_readbit(r);
646 void reader_resetbits(reader_t*r)
653 U8 reader_readU8(reader_t*r)
656 if(r->read(r, &b, 1)<1) {
657 fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
661 U16 reader_readU16(reader_t*r)
664 if(r->read(r, &b1, 1)<1) {
665 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
667 if(r->read(r, &b2, 1)<1) {
668 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
672 U32 reader_readU32(reader_t*r)
674 U8 b1=0,b2=0,b3=0,b4=0;
675 if(r->read(r, &b1, 1)<1)
676 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
677 if(r->read(r, &b2, 1)<1)
678 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
679 if(r->read(r, &b3, 1)<1)
680 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
681 if(r->read(r, &b4, 1)<1)
682 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
683 return b1|b2<<8|b3<<16|b4<<24;
685 float reader_readFloat(reader_t*r)
691 U8 b1=0,b2=0,b3=0,b4=0;
696 U32 w = (b1|b2<<8|b3<<16|b4<<24);
699 double reader_readDouble(reader_t*r)
707 U64 w = ((U64)b[0]|(U64)b[1]<<8|(U64)b[2]<<16|(U64)b[3]<<24|(U64)b[4]<<32|(U64)b[5]<<40|(U64)b[6]<<48|(U64)b[7]<<56);
710 char*reader_readString(reader_t*r)
713 writer_init_growingmemwriter(&g, 16);
715 U8 b = reader_readU8(r);
716 writer_writeU8(&g, b);
720 char*string = (char*)writer_growmemwrite_getmem(&g);
725 void writer_writeString(writer_t*w, const char*s)
729 w->write(w, (void*)s, l);
730 w->write(w, &zero, 1);
732 void writer_writeU8(writer_t*w, unsigned char b)
736 void writer_writeU16(writer_t*w, unsigned short v)
738 unsigned char b1 = v;
739 unsigned char b2 = v>>8;
743 void writer_writeU32(writer_t*w, unsigned long v)
745 unsigned char b1 = v;
746 unsigned char b2 = v>>8;
747 unsigned char b3 = v>>16;
748 unsigned char b4 = v>>24;
754 void writer_writeFloat(writer_t*w, float f)
759 unsigned v = *(unsigned*)&f;
760 unsigned char b1 = v;
761 unsigned char b2 = v>>8;
762 unsigned char b3 = v>>16;
763 unsigned char b4 = v>>24;
769 void writer_writeDouble(writer_t*w, double f)
774 unsigned long long v = *(unsigned long long*)&f;
775 unsigned char b1 = v;
776 unsigned char b2 = v>>8;
777 unsigned char b3 = v>>16;
778 unsigned char b4 = v>>24;
779 unsigned char b5 = v>>32;
780 unsigned char b6 = v>>40;
781 unsigned char b7 = v>>48;
782 unsigned char b8 = v>>56;