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 */
31 #define __USE_LARGEFILE64
35 #include "../config.h"
39 #define ZLIB_BUFFER_SIZE 16384
43 /* ---------------------------- null reader ------------------------------- */
45 static int reader_nullread(reader_t*r, void* data, int len)
50 static void reader_nullread_dealloc(reader_t*r)
52 memset(r, 0, sizeof(reader_t));
54 static int reader_nullseek(reader_t*r, int pos)
58 void reader_init_nullreader(reader_t*r)
60 r->read = reader_nullread;
61 r->seek = reader_nullseek;
62 r->dealloc = reader_nullread_dealloc;
64 r->type = READER_TYPE_NULL;
69 /* ---------------------------- file reader ------------------------------- */
71 static int reader_fileread(reader_t*reader, void* data, int len)
73 int ret = read((int)reader->internal, data, len);
78 static void reader_fileread_dealloc(reader_t*r)
80 if(r->type == READER_TYPE_FILE2) {
81 close((ptroff_t)r->internal);
83 memset(r, 0, sizeof(reader_t));
85 static int reader_fileread_seek(reader_t*r, int pos)
87 return lseek((ptroff_t)r->internal, pos, SEEK_SET);
89 void reader_init_filereader(reader_t*r, int handle)
91 r->read = reader_fileread;
92 r->seek = reader_fileread_seek;
93 r->dealloc = reader_fileread_dealloc;
94 r->internal = (void*)handle;
95 r->type = READER_TYPE_FILE;
100 void reader_init_filereader2(reader_t*r, const char*filename)
112 reader_init_filereader(r, fi);
113 r->type = READER_TYPE_FILE2;
116 /* ---------------------------- mem reader ------------------------------- */
118 typedef struct _memread
124 static int reader_memread(reader_t*reader, void* data, int len)
126 memread_t*mr = (memread_t*)reader->internal;
128 if(mr->length - reader->pos < len) {
129 len = mr->length - reader->pos;
132 memcpy(data, &mr->data[reader->pos], len);
136 static int reader_memseek(reader_t*reader, int pos)
138 memread_t*mr = (memread_t*)reader->internal;
139 if(pos>=0 && pos<=mr->length) {
146 static void reader_memread_dealloc(reader_t*reader)
149 free(reader->internal);
150 memset(reader, 0, sizeof(reader_t));
152 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
154 memread_t*mr = (memread_t*)malloc(sizeof(memread_t));
155 mr->data = (unsigned char*)newdata;
156 mr->length = newlength;
157 r->read = reader_memread;
158 r->seek = reader_memseek;
159 r->dealloc = reader_memread_dealloc;
160 r->internal = (void*)mr;
161 r->type = READER_TYPE_MEM;
167 /* ---------------------------- zzip reader ------------------------------ */
169 static int reader_zzip_read(reader_t*reader, void* data, int len)
171 return zzip_file_read((ZZIP_FILE*)reader->internal, data, len);
173 static void reader_zzip_dealloc(reader_t*reader)
175 memset(reader, 0, sizeof(reader_t));
177 static int reader_zzip_seek(reader_t*reader, int pos)
179 return zzip_seek((ZZIP_FILE*)reader->internal, pos, SEEK_SET);
181 void reader_init_zzipreader(reader_t*r,ZZIP_FILE*z)
183 r->read = reader_zzip_read;
184 r->seek = reader_zzip_seek;
185 r->dealloc = reader_zzip_dealloc;
187 r->type = READER_TYPE_ZZIP;
194 /* ---------------------------- mem writer ------------------------------- */
196 typedef struct _memwrite
202 static int writer_memwrite_write(writer_t*w, void* data, int len)
204 memwrite_t*mw = (memwrite_t*)w->internal;
205 if(mw->length - w->pos > len) {
206 memcpy(&mw->data[w->pos], data, len);
210 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
212 return mw->length - w->pos;
215 static void writer_memwrite_finish(writer_t*w)
221 static void dummy_flush(writer_t*w)
224 void writer_init_memwriter(writer_t*w, void*data, int len)
227 mr = (memwrite_t*)malloc(sizeof(memwrite_t));
228 mr->data = (unsigned char *)data;
230 memset(w, 0, sizeof(writer_t));
231 w->write = writer_memwrite_write;
232 w->flush = dummy_flush;
233 w->finish = writer_memwrite_finish;
234 w->internal = (void*)mr;
235 w->type = WRITER_TYPE_MEM;
241 /* ------------------------- growing mem writer ------------------------------- */
243 typedef struct _growmemwrite
249 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
251 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
253 fprintf(stderr, "Illegal write operation: data already given away");
256 if(mw->length - w->pos < len) {
257 int newlength = mw->length;
258 while(newlength - w->pos < len) {
259 newlength += mw->grow;
262 unsigned char*newmem = (unsigned char*)malloc(newlength);
263 memcpy(newmem, mw->data, mw->length);
267 mw->data = (unsigned char*)realloc(mw->data, newlength);
269 mw->length = newlength;
271 memcpy(&mw->data[w->pos], data, len);
275 static void writer_growmemwrite_finish(writer_t*w)
277 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
279 free(mw->data);mw->data = 0;
282 free(w->internal);mw=0;
283 memset(w, 0, sizeof(writer_t));
285 void* writer_growmemwrite_memptr(writer_t*w, int*len)
287 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
293 void* writer_growmemwrite_getmem(writer_t*w)
295 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
297 /* remove own reference so that neither write() nor finish() can free it.
298 It's property of the caller now.
303 void writer_growmemwrite_reset(writer_t*w)
305 growmemwrite_t*mw = (growmemwrite_t*)w->internal;
310 void writer_init_growingmemwriter(writer_t*w, U32 grow)
312 growmemwrite_t *mw = (growmemwrite_t *)malloc(sizeof(growmemwrite_t));
314 mw->data = (unsigned char *)malloc(mw->length);
316 memset(w, 0, sizeof(writer_t));
317 w->write = writer_growmemwrite_write;
318 w->flush = dummy_flush;
319 w->finish = writer_growmemwrite_finish;
320 w->internal = (void*)mw;
321 w->type = WRITER_TYPE_GROWING_MEM;
327 /* ---------------------------- file writer ------------------------------- */
329 typedef struct _filewrite
335 static int writer_filewrite_write(writer_t*w, void* data, int len)
337 filewrite_t * fw= (filewrite_t*)w->internal;
339 return write(fw->handle, data, len);
341 static void writer_filewrite_finish(writer_t*w)
343 filewrite_t *mr = (filewrite_t*)w->internal;
347 memset(w, 0, sizeof(writer_t));
349 void writer_init_filewriter(writer_t*w, int handle)
351 filewrite_t *mr = (filewrite_t *)malloc(sizeof(filewrite_t));
354 memset(w, 0, sizeof(writer_t));
355 w->write = writer_filewrite_write;
356 w->finish = writer_filewrite_finish;
358 w->type = WRITER_TYPE_FILE;
363 void writer_init_filewriter2(writer_t*w, char*filename)
374 O_WRONLY|O_CREAT|O_TRUNC, 0644);
375 writer_init_filewriter(w, fi);
376 ((filewrite_t*)w->internal)->free_handle = 1;
379 /* ---------------------------- null writer ------------------------------- */
381 static int writer_nullwrite_write(writer_t*w, void* data, int len)
386 static void writer_nullwrite_finish(writer_t*w)
388 memset(w, 0, sizeof(writer_t));
390 void writer_init_nullwriter(writer_t*w)
392 memset(w, 0, sizeof(writer_t));
393 w->write = writer_nullwrite_write;
394 w->flush = dummy_flush;
395 w->finish = writer_nullwrite_finish;
397 w->type = WRITER_TYPE_NULL;
402 /* ---------------------------- zlibinflate reader -------------------------- */
404 typedef struct _zlibinflate
409 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
414 static void zlib_error(int ret, char* msg, z_stream*zs)
416 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
419 zs->msg?zs->msg:"unknown");
420 if(errno) perror("errno:");
425 static int reader_zlibinflate(reader_t*reader, void* data, int len)
428 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
436 z->zs.next_out = (Bytef *)data;
437 z->zs.avail_out = len;
440 if(!z->zs.avail_in) {
441 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
442 z->zs.next_in = z->readbuffer;
445 ret = inflate(&z->zs, Z_NO_FLUSH);
447 ret = inflate(&z->zs, Z_FINISH);
450 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
452 if (ret == Z_STREAM_END) {
453 int pos = z->zs.next_out - (Bytef*)data;
454 ret = inflateEnd(&z->zs);
455 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
456 free(reader->internal);
457 reader->internal = 0;
461 if(!z->zs.avail_out) {
468 fprintf(stderr, "Error: swftools was compiled without zlib support");
472 static int reader_zlibseek(reader_t*reader, int pos)
474 fprintf(stderr, "Erro: seeking not supported for zlib streams");
477 static void reader_zlibinflate_dealloc(reader_t*reader)
480 zlibinflate_t*z = (zlibinflate_t*)reader->internal;
481 /* test whether read() already did basic deallocation */
482 if(reader->internal) {
484 free(reader->internal);
486 memset(reader, 0, sizeof(reader_t));
489 void reader_init_zlibinflate(reader_t*r, reader_t*input)
492 zlibinflate_t*z = (zlibinflate_t*)malloc(sizeof(zlibinflate_t));
493 memset(z, 0, sizeof(zlibinflate_t));
495 memset(r, 0, sizeof(reader_t));
497 r->read = reader_zlibinflate;
498 r->seek = reader_zlibseek;
499 r->dealloc = reader_zlibinflate_dealloc;
500 r->type = READER_TYPE_ZLIB;
503 memset(&z->zs,0,sizeof(z_stream));
504 z->zs.zalloc = Z_NULL;
505 z->zs.zfree = Z_NULL;
506 z->zs.opaque = Z_NULL;
507 ret = inflateInit(&z->zs);
508 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
511 fprintf(stderr, "Error: swftools was compiled without zlib support");
516 /* ---------------------------- zlibdeflate writer -------------------------- */
518 typedef struct _zlibdeflate
523 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
527 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
530 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
532 if(writer->type != WRITER_TYPE_ZLIB) {
533 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
537 fprintf(stderr, "zlib not initialized!\n");
543 z->zs.next_in = (Bytef *)data;
544 z->zs.avail_in = len;
547 ret = deflate(&z->zs, Z_NO_FLUSH);
549 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
551 if(z->zs.next_out != z->writebuffer) {
552 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
553 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
554 z->zs.next_out = z->writebuffer;
555 z->zs.avail_out = ZLIB_BUFFER_SIZE;
558 if(!z->zs.avail_in) {
564 fprintf(stderr, "Error: swftools was compiled without zlib support");
569 void writer_zlibdeflate_flush(writer_t*writer)
572 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
574 if(writer->type != WRITER_TYPE_ZLIB) {
575 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
579 fprintf(stderr, "zlib not initialized!\n");
586 ret = deflate(&z->zs, Z_SYNC_FLUSH);
587 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_flush", &z->zs);
588 if(z->zs.next_out != z->writebuffer) {
589 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
590 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
591 z->zs.next_out = z->writebuffer;
592 z->zs.avail_out = ZLIB_BUFFER_SIZE;
594 /* TODO: how will zlib let us know it needs more buffer space? */
599 fprintf(stderr, "Error: swftools was compiled without zlib support");
604 static void writer_zlibdeflate_finish(writer_t*writer)
607 zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
610 if(writer->type != WRITER_TYPE_ZLIB) {
611 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
618 ret = deflate(&z->zs, Z_FINISH);
620 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
622 if(z->zs.next_out != z->writebuffer) {
623 writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
624 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
625 z->zs.next_out = z->writebuffer;
626 z->zs.avail_out = ZLIB_BUFFER_SIZE;
629 if (ret == Z_STREAM_END) {
634 ret = deflateEnd(&z->zs);
635 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
636 free(writer->internal);
637 memset(writer, 0, sizeof(writer_t));
638 //output->finish(output);
640 fprintf(stderr, "Error: swftools was compiled without zlib support");
644 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
649 memset(w, 0, sizeof(writer_t));
650 z = (zlibdeflate_t*)malloc(sizeof(zlibdeflate_t));
651 memset(z, 0, sizeof(zlibdeflate_t));
653 w->write = writer_zlibdeflate_write;
654 w->flush = writer_zlibdeflate_flush;
655 w->finish = writer_zlibdeflate_finish;
656 w->type = WRITER_TYPE_ZLIB;
659 memset(&z->zs,0,sizeof(z_stream));
660 z->zs.zalloc = Z_NULL;
661 z->zs.zfree = Z_NULL;
662 z->zs.opaque = Z_NULL;
663 ret = deflateInit(&z->zs, 9);
664 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
667 z->zs.next_out = z->writebuffer;
668 z->zs.avail_out = ZLIB_BUFFER_SIZE;
670 fprintf(stderr, "Error: swftools was compiled without zlib support");
675 /* ----------------------- bit handling routines -------------------------- */
677 void writer_writebit(writer_t*w, int bit)
681 w->write(w, &w->mybyte, 1);
686 w->mybyte |= 1 << (7 - w->bitpos);
689 void writer_writebits(writer_t*w, unsigned int data, int bits)
694 writer_writebit(w, (data >> (bits-t-1))&1);
697 void writer_resetbits(writer_t*w)
700 w->write(w, &w->mybyte, 1);
705 unsigned int reader_readbit(reader_t*r)
710 r->read(r, &r->mybyte, 1);
712 return (r->mybyte>>(7-r->bitpos++))&1;
714 unsigned int reader_readbits(reader_t*r, int num)
721 val|=reader_readbit(r);
725 void reader_resetbits(reader_t*r)
732 U8 reader_readU8(reader_t*r)
735 if(r->read(r, &b, 1)<1) {
736 fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
740 U16 reader_readU16(reader_t*r)
743 if(r->read(r, &b1, 1)<1) {
744 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
746 if(r->read(r, &b2, 1)<1) {
747 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
751 U32 reader_readU32(reader_t*r)
753 U8 b1=0,b2=0,b3=0,b4=0;
754 if(r->read(r, &b1, 1)<1)
755 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
756 if(r->read(r, &b2, 1)<1)
757 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
758 if(r->read(r, &b3, 1)<1)
759 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
760 if(r->read(r, &b4, 1)<1)
761 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
762 return b1|b2<<8|b3<<16|b4<<24;
765 float reader_readFloat(reader_t*r)
771 U8 b1=0,b2=0,b3=0,b4=0;
776 U32 w = (b1|b2<<8|b3<<16|b4<<24);
779 double reader_readDouble(reader_t*r)
787 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);
790 char*reader_readString(reader_t*r)
793 writer_init_growingmemwriter(&g, 16);
795 U8 b = reader_readU8(r);
796 writer_writeU8(&g, b);
800 char*string = (char*)writer_growmemwrite_getmem(&g);
805 void writer_writeString(writer_t*w, const char*s)
809 w->write(w, (void*)s, l);
810 w->write(w, &zero, 1);
812 void writer_writeU8(writer_t*w, unsigned char b)
816 void writer_writeU16(writer_t*w, unsigned short v)
818 unsigned char b1 = v;
819 unsigned char b2 = v>>8;
823 void writer_writeU32(writer_t*w, unsigned long v)
825 unsigned char b1 = v;
826 unsigned char b2 = v>>8;
827 unsigned char b3 = v>>16;
828 unsigned char b4 = v>>24;
834 void writer_writeFloat(writer_t*w, float f)
839 unsigned v = *(unsigned*)&f;
840 unsigned char b1 = v;
841 unsigned char b2 = v>>8;
842 unsigned char b3 = v>>16;
843 unsigned char b4 = v>>24;
849 void writer_writeDouble(writer_t*w, double f)
854 unsigned long long v = *(unsigned long long*)&f;
855 unsigned char b1 = v;
856 unsigned char b2 = v>>8;
857 unsigned char b3 = v>>16;
858 unsigned char b4 = v>>24;
859 unsigned char b5 = v>>32;
860 unsigned char b6 = v>>40;
861 unsigned char b7 = v>>48;
862 unsigned char b8 = v>>56;