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 */
33 #include "../config.h"
37 #define ZLIB_BUFFER_SIZE 16384
41 /* ---------------------------- null reader ------------------------------- */
43 static int reader_nullread(reader_t*r, void* data, int len)
48 static void reader_nullread_dealloc(reader_t*r)
50 memset(r, 0, sizeof(reader_t));
52 void reader_init_nullreader(reader_t*r)
54 r->read = reader_nullread;
55 r->dealloc = reader_nullread_dealloc;
57 r->type = READER_TYPE_NULL;
62 /* ---------------------------- file reader ------------------------------- */
64 static int reader_fileread(reader_t*reader, void* data, int len)
66 int ret = read((int)reader->internal, data, len);
71 void reader_init_filereader(reader_t*r, int handle)
73 r->read = reader_fileread;
74 r->internal = (void*)handle;
75 r->type = READER_TYPE_FILE;
81 /* ---------------------------- mem reader ------------------------------- */
83 typedef struct _memread
89 static int reader_memread(reader_t*reader, void* data, int len)
91 memread_t*mr = (memread_t*)reader->internal;
93 if(mr->length - reader->pos < len) {
94 len = mr->length - reader->pos;
96 memcpy(data, &mr->data[reader->pos], len);
100 static void reader_memread_dealloc(reader_t*reader)
103 free(reader->internal);
104 memset(reader, 0, sizeof(reader_t));
106 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
108 memread_t*mr = (memread_t*)malloc(sizeof(memread_t));
109 mr->data = (unsigned char*)newdata;
110 mr->length = newlength;
111 r->read = reader_memread;
112 r->dealloc = reader_memread_dealloc;
113 r->internal = (void*)mr;
114 r->type = READER_TYPE_MEM;
120 /* ---------------------------- mem writer ------------------------------- */
122 typedef struct _memwrite
128 static int writer_memwrite_write(writer_t*w, void* data, int len)
130 memwrite_t*mw = (memwrite_t*)w->internal;
131 if(mw->length - w->pos > len) {
132 memcpy(&mw->data[w->pos], data, len);
136 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
138 return mw->length - w->pos;
141 static void writer_memwrite_finish(writer_t*w)
147 static void dummy_flush(writer_t*w)
150 void writer_init_memwriter(writer_t*w, void*data, int len)
153 mr = (memwrite_t*)malloc(sizeof(memwrite_t));
154 mr->data = (unsigned char *)data;
156 memset(w, 0, sizeof(writer_t));
157 w->write = writer_memwrite_write;
158 w->flush = dummy_flush;
159 w->finish = writer_memwrite_finish;
160 w->internal = (void*)mr;
161 w->type = WRITER_TYPE_MEM;
167 /* ------------------------- growing mem writer ------------------------------- */
169 typedef struct _growmemwrite
175 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
177 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
179 fprintf(stderr, "Illegal write operation: data already given away");
182 if(mw->length - w->pos < len) {
183 int newlength = mw->length;
184 while(newlength - w->pos < len) {
185 newlength += mw->grow;
188 unsigned char*newmem = (unsigned char*)malloc(newlength);
189 memcpy(newmem, mw->data, mw->length);
193 mw->data = (unsigned char*)realloc(mw->data, newlength);
195 mw->length = newlength;
197 memcpy(&mw->data[w->pos], data, len);
201 static void writer_growmemwrite_finish(writer_t*w)
203 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
205 free(mw->data);mw->data = 0;
208 free(w->internal);mw=0;
209 memset(w, 0, sizeof(writer_t));
211 void* writer_growmemwrite_memptr(writer_t*w, int*len)
213 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
219 void* writer_growmemwrite_getmem(writer_t*w)
221 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
223 /* remove own reference so that neither write() nor finish() can free it.
224 It's property of the caller now.
229 void writer_growmemwrite_reset(writer_t*w)
231 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
236 void writer_init_growingmemwriter(writer_t*w, U32 grow)
238 growmemwrite_t *mw = (growmemwrite_t *)malloc(sizeof(growmemwrite_t));
240 mw->data = (unsigned char *)malloc(mw->length);
242 memset(w, 0, sizeof(writer_t));
243 w->write = writer_growmemwrite_write;
244 w->flush = dummy_flush;
245 w->finish = writer_growmemwrite_finish;
246 w->internal = (void*)mw;
247 w->type = WRITER_TYPE_GROWING_MEM;
253 /* ---------------------------- file writer ------------------------------- */
255 typedef struct _filewrite
261 static int writer_filewrite_write(writer_t*w, void* data, int len)
263 filewrite_t * fw= (filewrite_t*)w->internal;
265 return write(fw->handle, data, len);
267 static void writer_filewrite_finish(writer_t*w)
269 filewrite_t *mr = (filewrite_t*)w->internal;
273 memset(w, 0, sizeof(writer_t));
275 void writer_init_filewriter(writer_t*w, int handle)
277 filewrite_t *mr = (filewrite_t *)malloc(sizeof(filewrite_t));
280 memset(w, 0, sizeof(writer_t));
281 w->write = writer_filewrite_write;
282 w->finish = writer_filewrite_finish;
284 w->type = WRITER_TYPE_FILE;
289 void writer_init_filewriter2(writer_t*w, char*filename)
291 int fi = open("movie.swf",
295 O_WRONLY|O_CREAT|O_TRUNC, 0644);
296 writer_init_filewriter(w, fi);
297 ((filewrite_t*)w->internal)->free_handle = 1;
300 /* ---------------------------- null writer ------------------------------- */
302 static int writer_nullwrite_write(writer_t*w, void* data, int len)
307 static void writer_nullwrite_finish(writer_t*w)
309 memset(w, 0, sizeof(writer_t));
311 void writer_init_nullwriter(writer_t*w)
313 memset(w, 0, sizeof(writer_t));
314 w->write = writer_nullwrite_write;
315 w->flush = dummy_flush;
316 w->finish = writer_nullwrite_finish;
318 w->type = WRITER_TYPE_NULL;
323 /* ---------------------------- zlibinflate reader -------------------------- */
325 typedef struct _zlibinflate
330 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
335 static void zlib_error(int ret, char* msg, z_stream*zs)
337 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
340 zs->msg?zs->msg:"unknown");
346 static int reader_zlibinflate(reader_t*reader, void* data, int len)
349 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
357 z->zs.next_out = (Bytef *)data;
358 z->zs.avail_out = len;
361 if(!z->zs.avail_in) {
362 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
363 z->zs.next_in = z->readbuffer;
366 ret = inflate(&z->zs, Z_NO_FLUSH);
368 ret = inflate(&z->zs, Z_FINISH);
371 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
373 if (ret == Z_STREAM_END) {
374 int pos = z->zs.next_out - (Bytef*)data;
375 ret = inflateEnd(&z->zs);
376 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
377 free(reader->internal);
378 reader->internal = 0;
382 if(!z->zs.avail_out) {
389 fprintf(stderr, "Error: swftools was compiled without zlib support");
393 static void reader_zlibinflate_dealloc(reader_t*reader)
396 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
397 /* test whether read() already did basic deallocation */
398 if(reader->internal) {
400 free(reader->internal);
402 memset(reader, 0, sizeof(reader_t));
405 void reader_init_zlibinflate(reader_t*r, reader_t*input)
408 zlibinflate_t*z = (zlibinflate_t*)malloc(sizeof(zlibinflate_t));
409 memset(z, 0, sizeof(zlibinflate_t));
411 memset(r, 0, sizeof(reader_t));
413 r->read = reader_zlibinflate;
414 r->dealloc = reader_zlibinflate_dealloc;
415 r->type = READER_TYPE_ZLIB;
418 memset(&z->zs,0,sizeof(z_stream));
419 z->zs.zalloc = Z_NULL;
420 z->zs.zfree = Z_NULL;
421 z->zs.opaque = Z_NULL;
422 ret = inflateInit(&z->zs);
423 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
426 fprintf(stderr, "Error: swftools was compiled without zlib support");
431 /* ---------------------------- zlibdeflate writer -------------------------- */
433 typedef struct _zlibdeflate
438 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
442 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
445 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
447 if(writer->type != WRITER_TYPE_ZLIB) {
448 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
452 fprintf(stderr, "zlib not initialized!\n");
458 z->zs.next_in = (Bytef *)data;
459 z->zs.avail_in = len;
462 ret = deflate(&z->zs, Z_NO_FLUSH);
464 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
466 if(z->zs.next_out != z->writebuffer) {
467 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
468 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
469 z->zs.next_out = z->writebuffer;
470 z->zs.avail_out = ZLIB_BUFFER_SIZE;
473 if(!z->zs.avail_in) {
479 fprintf(stderr, "Error: swftools was compiled without zlib support");
484 void writer_zlibdeflate_flush(writer_t*writer)
487 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
489 if(writer->type != WRITER_TYPE_ZLIB) {
490 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
494 fprintf(stderr, "zlib not initialized!\n");
501 ret = deflate(&z->zs, Z_SYNC_FLUSH);
502 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_flush", &z->zs);
503 if(z->zs.next_out != z->writebuffer) {
504 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
505 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
506 z->zs.next_out = z->writebuffer;
507 z->zs.avail_out = ZLIB_BUFFER_SIZE;
509 /* TODO: how will zlib let us know it needs more buffer space? */
514 fprintf(stderr, "Error: swftools was compiled without zlib support");
519 static void writer_zlibdeflate_finish(writer_t*writer)
522 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
525 if(writer->type != WRITER_TYPE_ZLIB) {
526 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
533 ret = deflate(&z->zs, Z_FINISH);
535 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
537 if(z->zs.next_out != z->writebuffer) {
538 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
539 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
540 z->zs.next_out = z->writebuffer;
541 z->zs.avail_out = ZLIB_BUFFER_SIZE;
544 if (ret == Z_STREAM_END) {
549 ret = deflateEnd(&z->zs);
550 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
551 free(writer->internal);
552 memset(writer, 0, sizeof(writer_t));
553 //output->finish(output);
555 fprintf(stderr, "Error: swftools was compiled without zlib support");
559 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
564 memset(w, 0, sizeof(writer_t));
565 z = (zlibdeflate_t*)malloc(sizeof(zlibdeflate_t));
566 memset(z, 0, sizeof(zlibdeflate_t));
568 w->write = writer_zlibdeflate_write;
569 w->flush = writer_zlibdeflate_flush;
570 w->finish = writer_zlibdeflate_finish;
571 w->type = WRITER_TYPE_ZLIB;
574 memset(&z->zs,0,sizeof(z_stream));
575 z->zs.zalloc = Z_NULL;
576 z->zs.zfree = Z_NULL;
577 z->zs.opaque = Z_NULL;
578 ret = deflateInit(&z->zs, 9);
579 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
582 z->zs.next_out = z->writebuffer;
583 z->zs.avail_out = ZLIB_BUFFER_SIZE;
585 fprintf(stderr, "Error: swftools was compiled without zlib support");
590 /* ----------------------- bit handling routines -------------------------- */
592 void writer_writebit(writer_t*w, int bit)
596 w->write(w, &w->mybyte, 1);
601 w->mybyte |= 1 << (7 - w->bitpos);
604 void writer_writebits(writer_t*w, unsigned int data, int bits)
609 writer_writebit(w, (data >> (bits-t-1))&1);
612 void writer_resetbits(writer_t*w)
615 w->write(w, &w->mybyte, 1);
620 unsigned int reader_readbit(reader_t*r)
625 r->read(r, &r->mybyte, 1);
627 return (r->mybyte>>(7-r->bitpos++))&1;
629 unsigned int reader_readbits(reader_t*r, int num)
636 val|=reader_readbit(r);
640 void reader_resetbits(reader_t*r)
647 U8 reader_readU8(reader_t*r)
650 if(r->read(r, &b, 1)<1) {
651 fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
655 U16 reader_readU16(reader_t*r)
658 if(r->read(r, &b1, 1)<1) {
659 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
661 if(r->read(r, &b2, 1)<1) {
662 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
666 U32 reader_readU32(reader_t*r)
668 U8 b1=0,b2=0,b3=0,b4=0;
669 if(r->read(r, &b1, 1)<1)
670 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
671 if(r->read(r, &b2, 1)<1)
672 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
673 if(r->read(r, &b3, 1)<1)
674 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
675 if(r->read(r, &b4, 1)<1)
676 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
677 return b1|b2<<8|b3<<16|b4<<24;
679 float reader_readFloat(reader_t*r)
681 U8 b1=0,b2=0,b3=0,b4=0;
686 U32 w = (b1|b2<<8|b3<<16|b4<<24);
689 double reader_readDouble(reader_t*r)
697 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);
700 char*reader_readString(reader_t*r)
703 writer_init_growingmemwriter(&g, 16);
705 U8 b = reader_readU8(r);
706 writer_writeU8(&g, b);
710 char*string = (char*)writer_growmemwrite_getmem(&g);
715 void writer_writeString(writer_t*w, const char*s)
719 w->write(w, (void*)s, l);
720 w->write(w, &zero, 1);
722 void writer_writeU8(writer_t*w, unsigned char b)
726 void writer_writeU16(writer_t*w, unsigned short v)
728 unsigned char b1 = v;
729 unsigned char b2 = v>>8;
733 void writer_writeU32(writer_t*w, unsigned long v)
735 unsigned char b1 = v;
736 unsigned char b2 = v>>8;
737 unsigned char b3 = v>>16;
738 unsigned char b4 = v>>24;
744 void writer_writeFloat(writer_t*w, float f)
746 unsigned long v = *(unsigned long*)&f;
747 unsigned char b1 = v;
748 unsigned char b2 = v>>8;
749 unsigned char b3 = v>>16;
750 unsigned char b4 = v>>24;
756 void writer_writeDouble(writer_t*w, double f)
761 unsigned long long v = *(unsigned long long*)&f;
762 unsigned char b1 = v;
763 unsigned char b2 = v>>8;
764 unsigned char b3 = v>>16;
765 unsigned char b4 = v>>24;
766 unsigned char b5 = v>>32;
767 unsigned char b6 = v>>40;
768 unsigned char b7 = v>>48;
769 unsigned char b8 = v>>56;