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 static int reader_nullseek(reader_t*r, int pos)
57 void reader_init_nullreader(reader_t*r)
59 r->read = reader_nullread;
60 r->seek = reader_nullseek;
61 r->dealloc = reader_nullread_dealloc;
63 r->type = READER_TYPE_NULL;
68 /* ---------------------------- file reader ------------------------------- */
70 static int reader_fileread(reader_t*reader, void* data, int len)
72 int ret = read((int)reader->internal, data, len);
77 static void reader_fileread_dealloc(reader_t*r)
79 if(r->type == READER_TYPE_FILE2) {
80 close((ptroff_t)r->internal);
82 memset(r, 0, sizeof(reader_t));
84 static int reader_fileread_seek(reader_t*r, int pos)
86 return lseek((ptroff_t)r->internal, pos, SEEK_SET);
88 void reader_init_filereader(reader_t*r, int handle)
90 r->read = reader_fileread;
91 r->seek = reader_fileread_seek;
92 r->dealloc = reader_fileread_dealloc;
93 r->internal = (void*)handle;
94 r->type = READER_TYPE_FILE;
99 void reader_init_filereader2(reader_t*r, const char*filename)
101 int fi = open(filename,
106 reader_init_filereader(r, fi);
107 r->type = READER_TYPE_FILE2;
110 /* ---------------------------- mem reader ------------------------------- */
112 typedef struct _memread
118 static int reader_memread(reader_t*reader, void* data, int len)
120 memread_t*mr = (memread_t*)reader->internal;
122 if(mr->length - reader->pos < len) {
123 len = mr->length - reader->pos;
126 memcpy(data, &mr->data[reader->pos], len);
130 static int reader_memseek(reader_t*reader, int pos)
132 memread_t*mr = (memread_t*)reader->internal;
133 if(pos>=0 && pos<=mr->length) {
140 static void reader_memread_dealloc(reader_t*reader)
143 free(reader->internal);
144 memset(reader, 0, sizeof(reader_t));
146 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
148 memread_t*mr = (memread_t*)malloc(sizeof(memread_t));
149 mr->data = (unsigned char*)newdata;
150 mr->length = newlength;
151 r->read = reader_memread;
152 r->seek = reader_memseek;
153 r->dealloc = reader_memread_dealloc;
154 r->internal = (void*)mr;
155 r->type = READER_TYPE_MEM;
161 /* ---------------------------- zzip reader ------------------------------ */
163 static int reader_zzip_read(reader_t*reader, void* data, int len)
165 return zzip_file_read((ZZIP_FILE*)reader->internal, data, len);
167 static void reader_zzip_dealloc(reader_t*reader)
169 memset(reader, 0, sizeof(reader_t));
171 static int reader_zzip_seek(reader_t*reader, int pos)
173 return zzip_seek((ZZIP_FILE*)reader->internal, pos, SEEK_SET);
175 void reader_init_zzipreader(reader_t*r,ZZIP_FILE*z)
177 r->read = reader_zzip_read;
178 r->seek = reader_zzip_seek;
179 r->dealloc = reader_zzip_dealloc;
181 r->type = READER_TYPE_ZZIP;
188 /* ---------------------------- mem writer ------------------------------- */
190 typedef struct _memwrite
196 static int writer_memwrite_write(writer_t*w, void* data, int len)
198 memwrite_t*mw = (memwrite_t*)w->internal;
199 if(mw->length - w->pos > len) {
200 memcpy(&mw->data[w->pos], data, len);
204 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
206 return mw->length - w->pos;
209 static void writer_memwrite_finish(writer_t*w)
215 static void dummy_flush(writer_t*w)
218 void writer_init_memwriter(writer_t*w, void*data, int len)
221 mr = (memwrite_t*)malloc(sizeof(memwrite_t));
222 mr->data = (unsigned char *)data;
224 memset(w, 0, sizeof(writer_t));
225 w->write = writer_memwrite_write;
226 w->flush = dummy_flush;
227 w->finish = writer_memwrite_finish;
228 w->internal = (void*)mr;
229 w->type = WRITER_TYPE_MEM;
235 /* ------------------------- growing mem writer ------------------------------- */
237 typedef struct _growmemwrite
243 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
245 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
247 fprintf(stderr, "Illegal write operation: data already given away");
250 if(mw->length - w->pos < len) {
251 int newlength = mw->length;
252 while(newlength - w->pos < len) {
253 newlength += mw->grow;
256 unsigned char*newmem = (unsigned char*)malloc(newlength);
257 memcpy(newmem, mw->data, mw->length);
261 mw->data = (unsigned char*)realloc(mw->data, newlength);
263 mw->length = newlength;
265 memcpy(&mw->data[w->pos], data, len);
269 static void writer_growmemwrite_finish(writer_t*w)
271 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
273 free(mw->data);mw->data = 0;
276 free(w->internal);mw=0;
277 memset(w, 0, sizeof(writer_t));
279 void* writer_growmemwrite_memptr(writer_t*w, int*len)
281 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
287 void* writer_growmemwrite_getmem(writer_t*w)
289 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
291 /* remove own reference so that neither write() nor finish() can free it.
292 It's property of the caller now.
297 void writer_growmemwrite_reset(writer_t*w)
299 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
304 void writer_init_growingmemwriter(writer_t*w, U32 grow)
306 growmemwrite_t *mw = (growmemwrite_t *)malloc(sizeof(growmemwrite_t));
308 mw->data = (unsigned char *)malloc(mw->length);
310 memset(w, 0, sizeof(writer_t));
311 w->write = writer_growmemwrite_write;
312 w->flush = dummy_flush;
313 w->finish = writer_growmemwrite_finish;
314 w->internal = (void*)mw;
315 w->type = WRITER_TYPE_GROWING_MEM;
321 /* ---------------------------- file writer ------------------------------- */
323 typedef struct _filewrite
329 static int writer_filewrite_write(writer_t*w, void* data, int len)
331 filewrite_t * fw= (filewrite_t*)w->internal;
333 return write(fw->handle, data, len);
335 static void writer_filewrite_finish(writer_t*w)
337 filewrite_t *mr = (filewrite_t*)w->internal;
341 memset(w, 0, sizeof(writer_t));
343 void writer_init_filewriter(writer_t*w, int handle)
345 filewrite_t *mr = (filewrite_t *)malloc(sizeof(filewrite_t));
348 memset(w, 0, sizeof(writer_t));
349 w->write = writer_filewrite_write;
350 w->finish = writer_filewrite_finish;
352 w->type = WRITER_TYPE_FILE;
357 void writer_init_filewriter2(writer_t*w, char*filename)
359 int fi = open(filename,
363 O_WRONLY|O_CREAT|O_TRUNC, 0644);
364 writer_init_filewriter(w, fi);
365 ((filewrite_t*)w->internal)->free_handle = 1;
368 /* ---------------------------- null writer ------------------------------- */
370 static int writer_nullwrite_write(writer_t*w, void* data, int len)
375 static void writer_nullwrite_finish(writer_t*w)
377 memset(w, 0, sizeof(writer_t));
379 void writer_init_nullwriter(writer_t*w)
381 memset(w, 0, sizeof(writer_t));
382 w->write = writer_nullwrite_write;
383 w->flush = dummy_flush;
384 w->finish = writer_nullwrite_finish;
386 w->type = WRITER_TYPE_NULL;
391 /* ---------------------------- zlibinflate reader -------------------------- */
393 typedef struct _zlibinflate
398 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
403 static void zlib_error(int ret, char* msg, z_stream*zs)
405 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
408 zs->msg?zs->msg:"unknown");
409 if(errno) perror("errno:");
414 static int reader_zlibinflate(reader_t*reader, void* data, int len)
417 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
425 z->zs.next_out = (Bytef *)data;
426 z->zs.avail_out = len;
429 if(!z->zs.avail_in) {
430 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
431 z->zs.next_in = z->readbuffer;
434 ret = inflate(&z->zs, Z_NO_FLUSH);
436 ret = inflate(&z->zs, Z_FINISH);
439 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
441 if (ret == Z_STREAM_END) {
442 int pos = z->zs.next_out - (Bytef*)data;
443 ret = inflateEnd(&z->zs);
444 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
445 free(reader->internal);
446 reader->internal = 0;
450 if(!z->zs.avail_out) {
457 fprintf(stderr, "Error: swftools was compiled without zlib support");
461 static int reader_zlibseek(reader_t*reader, int pos)
463 fprintf(stderr, "Erro: seeking not supported for zlib streams");
466 static void reader_zlibinflate_dealloc(reader_t*reader)
469 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
470 /* test whether read() already did basic deallocation */
471 if(reader->internal) {
473 free(reader->internal);
475 memset(reader, 0, sizeof(reader_t));
478 void reader_init_zlibinflate(reader_t*r, reader_t*input)
481 zlibinflate_t*z = (zlibinflate_t*)malloc(sizeof(zlibinflate_t));
482 memset(z, 0, sizeof(zlibinflate_t));
484 memset(r, 0, sizeof(reader_t));
486 r->read = reader_zlibinflate;
487 r->seek = reader_zlibseek;
488 r->dealloc = reader_zlibinflate_dealloc;
489 r->type = READER_TYPE_ZLIB;
492 memset(&z->zs,0,sizeof(z_stream));
493 z->zs.zalloc = Z_NULL;
494 z->zs.zfree = Z_NULL;
495 z->zs.opaque = Z_NULL;
496 ret = inflateInit(&z->zs);
497 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
500 fprintf(stderr, "Error: swftools was compiled without zlib support");
505 /* ---------------------------- zlibdeflate writer -------------------------- */
507 typedef struct _zlibdeflate
512 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
516 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
519 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
521 if(writer->type != WRITER_TYPE_ZLIB) {
522 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
526 fprintf(stderr, "zlib not initialized!\n");
532 z->zs.next_in = (Bytef *)data;
533 z->zs.avail_in = len;
536 ret = deflate(&z->zs, Z_NO_FLUSH);
538 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
540 if(z->zs.next_out != z->writebuffer) {
541 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
542 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
543 z->zs.next_out = z->writebuffer;
544 z->zs.avail_out = ZLIB_BUFFER_SIZE;
547 if(!z->zs.avail_in) {
553 fprintf(stderr, "Error: swftools was compiled without zlib support");
558 void writer_zlibdeflate_flush(writer_t*writer)
561 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
563 if(writer->type != WRITER_TYPE_ZLIB) {
564 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
568 fprintf(stderr, "zlib not initialized!\n");
575 ret = deflate(&z->zs, Z_SYNC_FLUSH);
576 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_flush", &z->zs);
577 if(z->zs.next_out != z->writebuffer) {
578 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
579 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
580 z->zs.next_out = z->writebuffer;
581 z->zs.avail_out = ZLIB_BUFFER_SIZE;
583 /* TODO: how will zlib let us know it needs more buffer space? */
588 fprintf(stderr, "Error: swftools was compiled without zlib support");
593 static void writer_zlibdeflate_finish(writer_t*writer)
596 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
599 if(writer->type != WRITER_TYPE_ZLIB) {
600 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
607 ret = deflate(&z->zs, Z_FINISH);
609 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
611 if(z->zs.next_out != z->writebuffer) {
612 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
613 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
614 z->zs.next_out = z->writebuffer;
615 z->zs.avail_out = ZLIB_BUFFER_SIZE;
618 if (ret == Z_STREAM_END) {
623 ret = deflateEnd(&z->zs);
624 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
625 free(writer->internal);
626 memset(writer, 0, sizeof(writer_t));
627 //output->finish(output);
629 fprintf(stderr, "Error: swftools was compiled without zlib support");
633 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
638 memset(w, 0, sizeof(writer_t));
639 z = (zlibdeflate_t*)malloc(sizeof(zlibdeflate_t));
640 memset(z, 0, sizeof(zlibdeflate_t));
642 w->write = writer_zlibdeflate_write;
643 w->flush = writer_zlibdeflate_flush;
644 w->finish = writer_zlibdeflate_finish;
645 w->type = WRITER_TYPE_ZLIB;
648 memset(&z->zs,0,sizeof(z_stream));
649 z->zs.zalloc = Z_NULL;
650 z->zs.zfree = Z_NULL;
651 z->zs.opaque = Z_NULL;
652 ret = deflateInit(&z->zs, 9);
653 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
656 z->zs.next_out = z->writebuffer;
657 z->zs.avail_out = ZLIB_BUFFER_SIZE;
659 fprintf(stderr, "Error: swftools was compiled without zlib support");
664 /* ----------------------- bit handling routines -------------------------- */
666 void writer_writebit(writer_t*w, int bit)
670 w->write(w, &w->mybyte, 1);
675 w->mybyte |= 1 << (7 - w->bitpos);
678 void writer_writebits(writer_t*w, unsigned int data, int bits)
683 writer_writebit(w, (data >> (bits-t-1))&1);
686 void writer_resetbits(writer_t*w)
689 w->write(w, &w->mybyte, 1);
694 unsigned int reader_readbit(reader_t*r)
699 r->read(r, &r->mybyte, 1);
701 return (r->mybyte>>(7-r->bitpos++))&1;
703 unsigned int reader_readbits(reader_t*r, int num)
710 val|=reader_readbit(r);
714 void reader_resetbits(reader_t*r)
721 U8 reader_readU8(reader_t*r)
724 if(r->read(r, &b, 1)<1) {
725 fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
729 U16 reader_readU16(reader_t*r)
732 if(r->read(r, &b1, 1)<1) {
733 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
735 if(r->read(r, &b2, 1)<1) {
736 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
740 U32 reader_readU32(reader_t*r)
742 U8 b1=0,b2=0,b3=0,b4=0;
743 if(r->read(r, &b1, 1)<1)
744 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
745 if(r->read(r, &b2, 1)<1)
746 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
747 if(r->read(r, &b3, 1)<1)
748 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
749 if(r->read(r, &b4, 1)<1)
750 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
751 return b1|b2<<8|b3<<16|b4<<24;
754 float reader_readFloat(reader_t*r)
760 U8 b1=0,b2=0,b3=0,b4=0;
765 U32 w = (b1|b2<<8|b3<<16|b4<<24);
768 double reader_readDouble(reader_t*r)
776 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);
779 char*reader_readString(reader_t*r)
782 writer_init_growingmemwriter(&g, 16);
784 U8 b = reader_readU8(r);
785 writer_writeU8(&g, b);
789 char*string = (char*)writer_growmemwrite_getmem(&g);
794 void writer_writeString(writer_t*w, const char*s)
798 w->write(w, (void*)s, l);
799 w->write(w, &zero, 1);
801 void writer_writeU8(writer_t*w, unsigned char b)
805 void writer_writeU16(writer_t*w, unsigned short v)
807 unsigned char b1 = v;
808 unsigned char b2 = v>>8;
812 void writer_writeU32(writer_t*w, unsigned long v)
814 unsigned char b1 = v;
815 unsigned char b2 = v>>8;
816 unsigned char b3 = v>>16;
817 unsigned char b4 = v>>24;
823 void writer_writeFloat(writer_t*w, float f)
828 unsigned v = *(unsigned*)&f;
829 unsigned char b1 = v;
830 unsigned char b2 = v>>8;
831 unsigned char b3 = v>>16;
832 unsigned char b4 = v>>24;
838 void writer_writeDouble(writer_t*w, double f)
843 unsigned long long v = *(unsigned long long*)&f;
844 unsigned char b1 = v;
845 unsigned char b2 = v>>8;
846 unsigned char b3 = v>>16;
847 unsigned char b4 = v>>24;
848 unsigned char b5 = v>>32;
849 unsigned char b6 = v>>40;
850 unsigned char b7 = v>>48;
851 unsigned char b8 = v>>56;