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 if(r->type == READER_TYPE_FILE2) {
75 close((ptroff_t)r->internal);
77 memset(r, 0, sizeof(reader_t));
79 void reader_init_filereader(reader_t*r, int handle)
81 r->read = reader_fileread;
82 r->dealloc = reader_fileread_dealloc;
83 r->internal = (void*)handle;
84 r->type = READER_TYPE_FILE;
89 void reader_init_filereader2(reader_t*r, const char*filename)
91 int fi = open(filename,
96 reader_init_filereader(r, fi);
97 r->type = READER_TYPE_FILE2;
100 /* ---------------------------- mem reader ------------------------------- */
102 typedef struct _memread
108 static int reader_memread(reader_t*reader, void* data, int len)
110 memread_t*mr = (memread_t*)reader->internal;
112 if(mr->length - reader->pos < len) {
113 len = mr->length - reader->pos;
115 memcpy(data, &mr->data[reader->pos], len);
119 static void reader_memread_dealloc(reader_t*reader)
122 free(reader->internal);
123 memset(reader, 0, sizeof(reader_t));
125 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
127 memread_t*mr = (memread_t*)malloc(sizeof(memread_t));
128 mr->data = (unsigned char*)newdata;
129 mr->length = newlength;
130 r->read = reader_memread;
131 r->dealloc = reader_memread_dealloc;
132 r->internal = (void*)mr;
133 r->type = READER_TYPE_MEM;
139 /* ---------------------------- zzip reader ------------------------------ */
141 static int reader_zzip_read(reader_t*reader, void* data, int len)
143 return zzip_file_read((ZZIP_FILE*)reader->internal, data, len);
145 static void reader_zzip_dealloc(reader_t*reader)
147 memset(reader, 0, sizeof(reader_t));
149 void reader_init_zzipreader(reader_t*r,ZZIP_FILE*z)
151 r->read = reader_zzip_read;
152 r->dealloc = reader_zzip_dealloc;
154 r->type = READER_TYPE_ZZIP;
161 /* ---------------------------- mem writer ------------------------------- */
163 typedef struct _memwrite
169 static int writer_memwrite_write(writer_t*w, void* data, int len)
171 memwrite_t*mw = (memwrite_t*)w->internal;
172 if(mw->length - w->pos > len) {
173 memcpy(&mw->data[w->pos], data, len);
177 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
179 return mw->length - w->pos;
182 static void writer_memwrite_finish(writer_t*w)
188 static void dummy_flush(writer_t*w)
191 void writer_init_memwriter(writer_t*w, void*data, int len)
194 mr = (memwrite_t*)malloc(sizeof(memwrite_t));
195 mr->data = (unsigned char *)data;
197 memset(w, 0, sizeof(writer_t));
198 w->write = writer_memwrite_write;
199 w->flush = dummy_flush;
200 w->finish = writer_memwrite_finish;
201 w->internal = (void*)mr;
202 w->type = WRITER_TYPE_MEM;
208 /* ------------------------- growing mem writer ------------------------------- */
210 typedef struct _growmemwrite
216 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
218 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
220 fprintf(stderr, "Illegal write operation: data already given away");
223 if(mw->length - w->pos < len) {
224 int newlength = mw->length;
225 while(newlength - w->pos < len) {
226 newlength += mw->grow;
229 unsigned char*newmem = (unsigned char*)malloc(newlength);
230 memcpy(newmem, mw->data, mw->length);
234 mw->data = (unsigned char*)realloc(mw->data, newlength);
236 mw->length = newlength;
238 memcpy(&mw->data[w->pos], data, len);
242 static void writer_growmemwrite_finish(writer_t*w)
244 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
246 free(mw->data);mw->data = 0;
249 free(w->internal);mw=0;
250 memset(w, 0, sizeof(writer_t));
252 void* writer_growmemwrite_memptr(writer_t*w, int*len)
254 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
260 void* writer_growmemwrite_getmem(writer_t*w)
262 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
264 /* remove own reference so that neither write() nor finish() can free it.
265 It's property of the caller now.
270 void writer_growmemwrite_reset(writer_t*w)
272 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
277 void writer_init_growingmemwriter(writer_t*w, U32 grow)
279 growmemwrite_t *mw = (growmemwrite_t *)malloc(sizeof(growmemwrite_t));
281 mw->data = (unsigned char *)malloc(mw->length);
283 memset(w, 0, sizeof(writer_t));
284 w->write = writer_growmemwrite_write;
285 w->flush = dummy_flush;
286 w->finish = writer_growmemwrite_finish;
287 w->internal = (void*)mw;
288 w->type = WRITER_TYPE_GROWING_MEM;
294 /* ---------------------------- file writer ------------------------------- */
296 typedef struct _filewrite
302 static int writer_filewrite_write(writer_t*w, void* data, int len)
304 filewrite_t * fw= (filewrite_t*)w->internal;
306 return write(fw->handle, data, len);
308 static void writer_filewrite_finish(writer_t*w)
310 filewrite_t *mr = (filewrite_t*)w->internal;
314 memset(w, 0, sizeof(writer_t));
316 void writer_init_filewriter(writer_t*w, int handle)
318 filewrite_t *mr = (filewrite_t *)malloc(sizeof(filewrite_t));
321 memset(w, 0, sizeof(writer_t));
322 w->write = writer_filewrite_write;
323 w->finish = writer_filewrite_finish;
325 w->type = WRITER_TYPE_FILE;
330 void writer_init_filewriter2(writer_t*w, char*filename)
332 int fi = open(filename,
336 O_WRONLY|O_CREAT|O_TRUNC, 0644);
337 writer_init_filewriter(w, fi);
338 ((filewrite_t*)w->internal)->free_handle = 1;
341 /* ---------------------------- null writer ------------------------------- */
343 static int writer_nullwrite_write(writer_t*w, void* data, int len)
348 static void writer_nullwrite_finish(writer_t*w)
350 memset(w, 0, sizeof(writer_t));
352 void writer_init_nullwriter(writer_t*w)
354 memset(w, 0, sizeof(writer_t));
355 w->write = writer_nullwrite_write;
356 w->flush = dummy_flush;
357 w->finish = writer_nullwrite_finish;
359 w->type = WRITER_TYPE_NULL;
364 /* ---------------------------- zlibinflate reader -------------------------- */
366 typedef struct _zlibinflate
371 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
376 static void zlib_error(int ret, char* msg, z_stream*zs)
378 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
381 zs->msg?zs->msg:"unknown");
382 if(errno) perror("errno:");
387 static int reader_zlibinflate(reader_t*reader, void* data, int len)
390 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
398 z->zs.next_out = (Bytef *)data;
399 z->zs.avail_out = len;
402 if(!z->zs.avail_in) {
403 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
404 z->zs.next_in = z->readbuffer;
407 ret = inflate(&z->zs, Z_NO_FLUSH);
409 ret = inflate(&z->zs, Z_FINISH);
412 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
414 if (ret == Z_STREAM_END) {
415 int pos = z->zs.next_out - (Bytef*)data;
416 ret = inflateEnd(&z->zs);
417 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
418 free(reader->internal);
419 reader->internal = 0;
423 if(!z->zs.avail_out) {
430 fprintf(stderr, "Error: swftools was compiled without zlib support");
434 static void reader_zlibinflate_dealloc(reader_t*reader)
437 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
438 /* test whether read() already did basic deallocation */
439 if(reader->internal) {
441 free(reader->internal);
443 memset(reader, 0, sizeof(reader_t));
446 void reader_init_zlibinflate(reader_t*r, reader_t*input)
449 zlibinflate_t*z = (zlibinflate_t*)malloc(sizeof(zlibinflate_t));
450 memset(z, 0, sizeof(zlibinflate_t));
452 memset(r, 0, sizeof(reader_t));
454 r->read = reader_zlibinflate;
455 r->dealloc = reader_zlibinflate_dealloc;
456 r->type = READER_TYPE_ZLIB;
459 memset(&z->zs,0,sizeof(z_stream));
460 z->zs.zalloc = Z_NULL;
461 z->zs.zfree = Z_NULL;
462 z->zs.opaque = Z_NULL;
463 ret = inflateInit(&z->zs);
464 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
467 fprintf(stderr, "Error: swftools was compiled without zlib support");
472 /* ---------------------------- zlibdeflate writer -------------------------- */
474 typedef struct _zlibdeflate
479 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
483 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
486 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
488 if(writer->type != WRITER_TYPE_ZLIB) {
489 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
493 fprintf(stderr, "zlib not initialized!\n");
499 z->zs.next_in = (Bytef *)data;
500 z->zs.avail_in = len;
503 ret = deflate(&z->zs, Z_NO_FLUSH);
505 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
507 if(z->zs.next_out != z->writebuffer) {
508 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
509 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
510 z->zs.next_out = z->writebuffer;
511 z->zs.avail_out = ZLIB_BUFFER_SIZE;
514 if(!z->zs.avail_in) {
520 fprintf(stderr, "Error: swftools was compiled without zlib support");
525 void writer_zlibdeflate_flush(writer_t*writer)
528 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
530 if(writer->type != WRITER_TYPE_ZLIB) {
531 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
535 fprintf(stderr, "zlib not initialized!\n");
542 ret = deflate(&z->zs, Z_SYNC_FLUSH);
543 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_flush", &z->zs);
544 if(z->zs.next_out != z->writebuffer) {
545 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
546 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
547 z->zs.next_out = z->writebuffer;
548 z->zs.avail_out = ZLIB_BUFFER_SIZE;
550 /* TODO: how will zlib let us know it needs more buffer space? */
555 fprintf(stderr, "Error: swftools was compiled without zlib support");
560 static void writer_zlibdeflate_finish(writer_t*writer)
563 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
566 if(writer->type != WRITER_TYPE_ZLIB) {
567 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
574 ret = deflate(&z->zs, Z_FINISH);
576 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
578 if(z->zs.next_out != z->writebuffer) {
579 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
580 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
581 z->zs.next_out = z->writebuffer;
582 z->zs.avail_out = ZLIB_BUFFER_SIZE;
585 if (ret == Z_STREAM_END) {
590 ret = deflateEnd(&z->zs);
591 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
592 free(writer->internal);
593 memset(writer, 0, sizeof(writer_t));
594 //output->finish(output);
596 fprintf(stderr, "Error: swftools was compiled without zlib support");
600 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
605 memset(w, 0, sizeof(writer_t));
606 z = (zlibdeflate_t*)malloc(sizeof(zlibdeflate_t));
607 memset(z, 0, sizeof(zlibdeflate_t));
609 w->write = writer_zlibdeflate_write;
610 w->flush = writer_zlibdeflate_flush;
611 w->finish = writer_zlibdeflate_finish;
612 w->type = WRITER_TYPE_ZLIB;
615 memset(&z->zs,0,sizeof(z_stream));
616 z->zs.zalloc = Z_NULL;
617 z->zs.zfree = Z_NULL;
618 z->zs.opaque = Z_NULL;
619 ret = deflateInit(&z->zs, 9);
620 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
623 z->zs.next_out = z->writebuffer;
624 z->zs.avail_out = ZLIB_BUFFER_SIZE;
626 fprintf(stderr, "Error: swftools was compiled without zlib support");
631 /* ----------------------- bit handling routines -------------------------- */
633 void writer_writebit(writer_t*w, int bit)
637 w->write(w, &w->mybyte, 1);
642 w->mybyte |= 1 << (7 - w->bitpos);
645 void writer_writebits(writer_t*w, unsigned int data, int bits)
650 writer_writebit(w, (data >> (bits-t-1))&1);
653 void writer_resetbits(writer_t*w)
656 w->write(w, &w->mybyte, 1);
661 unsigned int reader_readbit(reader_t*r)
666 r->read(r, &r->mybyte, 1);
668 return (r->mybyte>>(7-r->bitpos++))&1;
670 unsigned int reader_readbits(reader_t*r, int num)
677 val|=reader_readbit(r);
681 void reader_resetbits(reader_t*r)
688 U8 reader_readU8(reader_t*r)
691 if(r->read(r, &b, 1)<1) {
692 fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
696 U16 reader_readU16(reader_t*r)
699 if(r->read(r, &b1, 1)<1) {
700 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
702 if(r->read(r, &b2, 1)<1) {
703 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
707 U32 reader_readU32(reader_t*r)
709 U8 b1=0,b2=0,b3=0,b4=0;
710 if(r->read(r, &b1, 1)<1)
711 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
712 if(r->read(r, &b2, 1)<1)
713 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
714 if(r->read(r, &b3, 1)<1)
715 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
716 if(r->read(r, &b4, 1)<1)
717 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
718 return b1|b2<<8|b3<<16|b4<<24;
720 float reader_readFloat(reader_t*r)
726 U8 b1=0,b2=0,b3=0,b4=0;
731 U32 w = (b1|b2<<8|b3<<16|b4<<24);
734 double reader_readDouble(reader_t*r)
742 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);
745 char*reader_readString(reader_t*r)
748 writer_init_growingmemwriter(&g, 16);
750 U8 b = reader_readU8(r);
751 writer_writeU8(&g, b);
755 char*string = (char*)writer_growmemwrite_getmem(&g);
760 void writer_writeString(writer_t*w, const char*s)
764 w->write(w, (void*)s, l);
765 w->write(w, &zero, 1);
767 void writer_writeU8(writer_t*w, unsigned char b)
771 void writer_writeU16(writer_t*w, unsigned short v)
773 unsigned char b1 = v;
774 unsigned char b2 = v>>8;
778 void writer_writeU32(writer_t*w, unsigned long v)
780 unsigned char b1 = v;
781 unsigned char b2 = v>>8;
782 unsigned char b3 = v>>16;
783 unsigned char b4 = v>>24;
789 void writer_writeFloat(writer_t*w, float f)
794 unsigned v = *(unsigned*)&f;
795 unsigned char b1 = v;
796 unsigned char b2 = v>>8;
797 unsigned char b3 = v>>16;
798 unsigned char b4 = v>>24;
804 void writer_writeDouble(writer_t*w, double f)
809 unsigned long long v = *(unsigned long long*)&f;
810 unsigned char b1 = v;
811 unsigned char b2 = v>>8;
812 unsigned char b3 = v>>16;
813 unsigned char b4 = v>>24;
814 unsigned char b5 = v>>32;
815 unsigned char b6 = v>>40;
816 unsigned char b7 = v>>48;
817 unsigned char b8 = v>>56;