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 ------------------------------- */
88 static int reader_memread(reader_t*reader, void* data, int len)
90 struct memread_t*mr = (struct memread_t*)reader->internal;
92 if(mr->length - reader->pos < len) {
93 len = mr->length - reader->pos;
95 memcpy(data, &mr->data[reader->pos], len);
99 static void reader_memread_dealloc(reader_t*reader)
102 free(reader->internal);
103 memset(reader, 0, sizeof(reader_t));
105 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
107 struct memread_t*mr = (struct memread_t*)malloc(sizeof(struct memread_t));
108 mr->data = (unsigned char*)newdata;
109 mr->length = newlength;
110 r->read = reader_memread;
111 r->dealloc = reader_memread_dealloc;
112 r->internal = (void*)mr;
113 r->type = READER_TYPE_MEM;
119 /* ---------------------------- mem writer ------------------------------- */
127 static int writer_memwrite_write(writer_t*w, void* data, int len)
129 struct memwrite_t*mw = (struct memwrite_t*)w->internal;
130 if(mw->length - w->pos > len) {
131 memcpy(&mw->data[w->pos], data, len);
135 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
137 return mw->length - w->pos;
140 static void writer_memwrite_finish(writer_t*w)
146 void writer_init_memwriter(writer_t*w, void*data, int len)
148 struct memwrite_t *mr;
149 mr = (struct memwrite_t*)malloc(sizeof(struct memwrite_t));
150 mr->data = (unsigned char *)data;
152 memset(w, 0, sizeof(writer_t));
153 w->write = writer_memwrite_write;
154 w->finish = writer_memwrite_finish;
155 w->internal = (void*)mr;
156 w->type = WRITER_TYPE_MEM;
162 /* ------------------------- growing mem writer ------------------------------- */
164 struct growmemwrite_t
170 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
172 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
174 fprintf(stderr, "Illegal write operation: data already given away");
177 if(mw->length - w->pos < len) {
178 int newlength = mw->length;
179 while(newlength - w->pos < len) {
180 newlength += mw->grow;
183 unsigned char*newmem = (unsigned char*)malloc(newlength);
184 memcpy(newmem, mw->data, mw->length);
188 mw->data = (unsigned char*)realloc(mw->data, newlength);
190 mw->length = newlength;
192 memcpy(&mw->data[w->pos], data, len);
196 static void writer_growmemwrite_finish(writer_t*w)
198 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
200 free(mw->data);mw->data = 0;
203 free(w->internal);mw=0;
204 memset(w, 0, sizeof(writer_t));
206 void* writer_growmemwrite_memptr(writer_t*w, int*len)
208 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
214 void* writer_growmemwrite_getmem(writer_t*w)
216 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
218 /* remove own reference so that neither write() nor finish() can free it.
219 It's property of the caller now.
224 void writer_growmemwrite_reset(writer_t*w)
226 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
231 void writer_init_growingmemwriter(writer_t*w, U32 grow)
233 struct growmemwrite_t *mw;
234 mw = (struct growmemwrite_t *)malloc(sizeof(struct growmemwrite_t));
236 mw->data = (unsigned char *)malloc(mw->length);
238 memset(w, 0, sizeof(writer_t));
239 w->write = writer_growmemwrite_write;
240 w->finish = writer_growmemwrite_finish;
241 w->internal = (void*)mw;
242 w->type = WRITER_TYPE_GROWING_MEM;
248 /* ---------------------------- file writer ------------------------------- */
256 static int writer_filewrite_write(writer_t*w, void* data, int len)
258 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
259 return write(fw->handle, data, len);
261 static void writer_filewrite_finish(writer_t*w)
263 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
267 memset(w, 0, sizeof(writer_t));
269 void writer_init_filewriter(writer_t*w, int handle)
271 struct filewrite_t *mr = (struct filewrite_t *)malloc(sizeof(struct filewrite_t));
274 memset(w, 0, sizeof(writer_t));
275 w->write = writer_filewrite_write;
276 w->finish = writer_filewrite_finish;
278 w->type = WRITER_TYPE_FILE;
283 void writer_init_filewriter2(writer_t*w, char*filename)
285 int fi = open("movie.swf",
289 O_WRONLY|O_CREAT|O_TRUNC, 0644);
290 writer_init_filewriter(w, fi);
291 ((struct filewrite_t*)w->internal)->free_handle = 1;
294 /* ---------------------------- null writer ------------------------------- */
296 static int writer_nullwrite_write(writer_t*w, void* data, int len)
301 static void writer_nullwrite_finish(writer_t*w)
303 memset(w, 0, sizeof(writer_t));
305 void writer_init_nullwriter(writer_t*w)
307 memset(w, 0, sizeof(writer_t));
308 w->write = writer_nullwrite_write;
309 w->finish = writer_nullwrite_finish;
311 w->type = WRITER_TYPE_NULL;
316 /* ---------------------------- zlibinflate reader -------------------------- */
323 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
328 static void zlib_error(int ret, char* msg, z_stream*zs)
330 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
333 zs->msg?zs->msg:"unknown");
339 static int reader_zlibinflate(reader_t*reader, void* data, int len)
342 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
350 z->zs.next_out = (Bytef *)data;
351 z->zs.avail_out = len;
354 if(!z->zs.avail_in) {
355 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
356 z->zs.next_in = z->readbuffer;
359 ret = inflate(&z->zs, Z_NO_FLUSH);
361 ret = inflate(&z->zs, Z_FINISH);
364 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
366 if (ret == Z_STREAM_END) {
367 int pos = z->zs.next_out - (Bytef*)data;
368 ret = inflateEnd(&z->zs);
369 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
370 free(reader->internal);
371 reader->internal = 0;
375 if(!z->zs.avail_out) {
382 fprintf(stderr, "Error: swftools was compiled without zlib support");
386 static void reader_zlibinflate_dealloc(reader_t*reader)
389 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
390 /* test whether read() already did basic deallocation */
391 if(reader->internal) {
393 free(reader->internal);
395 memset(reader, 0, sizeof(reader_t));
398 void reader_init_zlibinflate(reader_t*r, reader_t*input)
401 struct zlibinflate_t*z;
403 memset(r, 0, sizeof(reader_t));
404 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
405 memset(z, 0, sizeof(struct zlibinflate_t));
407 r->read = reader_zlibinflate;
408 r->dealloc = reader_zlibinflate_dealloc;
409 r->type = READER_TYPE_ZLIB;
412 memset(&z->zs,0,sizeof(z_stream));
413 z->zs.zalloc = Z_NULL;
414 z->zs.zfree = Z_NULL;
415 z->zs.opaque = Z_NULL;
416 ret = inflateInit(&z->zs);
417 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
420 fprintf(stderr, "Error: swftools was compiled without zlib support");
425 /* ---------------------------- zlibdeflate writer -------------------------- */
432 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
436 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
439 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
441 if(writer->type != WRITER_TYPE_ZLIB) {
442 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
446 fprintf(stderr, "zlib not initialized!\n");
452 z->zs.next_in = (Bytef *)data;
453 z->zs.avail_in = len;
456 ret = deflate(&z->zs, Z_NO_FLUSH);
458 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
460 if(z->zs.next_out != z->writebuffer) {
461 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
462 z->zs.next_out = z->writebuffer;
463 z->zs.avail_out = ZLIB_BUFFER_SIZE;
466 if(!z->zs.avail_in) {
473 fprintf(stderr, "Error: swftools was compiled without zlib support");
477 static void writer_zlibdeflate_finish(writer_t*writer)
480 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
483 if(writer->type != WRITER_TYPE_ZLIB) {
484 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
491 ret = deflate(&z->zs, Z_FINISH);
493 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
495 if(z->zs.next_out != z->writebuffer) {
496 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
497 z->zs.next_out = z->writebuffer;
498 z->zs.avail_out = ZLIB_BUFFER_SIZE;
501 if (ret == Z_STREAM_END) {
506 ret = deflateEnd(&z->zs);
507 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
508 free(writer->internal);
509 memset(writer, 0, sizeof(writer_t));
510 //output->finish(output);
512 fprintf(stderr, "Error: swftools was compiled without zlib support");
516 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
519 struct zlibdeflate_t*z;
521 memset(w, 0, sizeof(writer_t));
522 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
523 memset(z, 0, sizeof(struct zlibdeflate_t));
525 w->write = writer_zlibdeflate_write;
526 w->finish = writer_zlibdeflate_finish;
527 w->type = WRITER_TYPE_ZLIB;
530 memset(&z->zs,0,sizeof(z_stream));
531 z->zs.zalloc = Z_NULL;
532 z->zs.zfree = Z_NULL;
533 z->zs.opaque = Z_NULL;
534 ret = deflateInit(&z->zs, 9);
535 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
538 z->zs.next_out = z->writebuffer;
539 z->zs.avail_out = ZLIB_BUFFER_SIZE;
541 fprintf(stderr, "Error: swftools was compiled without zlib support");
546 /* ----------------------- bit handling routines -------------------------- */
548 void writer_writebit(writer_t*w, int bit)
552 w->write(w, &w->mybyte, 1);
557 w->mybyte |= 1 << (7 - w->bitpos);
560 void writer_writebits(writer_t*w, unsigned int data, int bits)
565 writer_writebit(w, (data >> (bits-t-1))&1);
568 void writer_resetbits(writer_t*w)
571 w->write(w, &w->mybyte, 1);
576 unsigned int reader_readbit(reader_t*r)
581 r->read(r, &r->mybyte, 1);
583 return (r->mybyte>>(7-r->bitpos++))&1;
585 unsigned int reader_readbits(reader_t*r, int num)
592 val|=reader_readbit(r);
596 void reader_resetbits(reader_t*r)
603 U8 reader_readU8(reader_t*r)
606 if(r->read(r, &b, 1)<1) {
607 fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
611 U16 reader_readU16(reader_t*r)
614 if(r->read(r, &b1, 1)<1) {
615 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
617 if(r->read(r, &b2, 1)<1) {
618 fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
622 U32 reader_readU32(reader_t*r)
624 U8 b1=0,b2=0,b3=0,b4=0;
625 if(r->read(r, &b1, 1)<1)
626 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
627 if(r->read(r, &b2, 1)<1)
628 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
629 if(r->read(r, &b3, 1)<1)
630 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
631 if(r->read(r, &b4, 1)<1)
632 fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
633 return b1|b2<<8|b3<<16|b4<<24;
635 float reader_readFloat(reader_t*r)
637 U8 b1=0,b2=0,b3=0,b4=0;
642 U32 w = (b1|b2<<8|b3<<16|b4<<24);
645 double reader_readDouble(reader_t*r)
653 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);
656 char*reader_readString(reader_t*r)
659 writer_init_growingmemwriter(&g, 16);
661 U8 b = reader_readU8(r);
662 writer_writeU8(&g, b);
666 char*string = (char*)writer_growmemwrite_getmem(&g);
671 void writer_writeString(writer_t*w, const char*s)
675 w->write(w, (void*)s, l);
676 w->write(w, &zero, 1);
678 void writer_writeU8(writer_t*w, unsigned char b)
682 void writer_writeU16(writer_t*w, unsigned short v)
684 unsigned char b1 = v;
685 unsigned char b2 = v>>8;
689 void writer_writeU32(writer_t*w, unsigned long v)
691 unsigned char b1 = v;
692 unsigned char b2 = v>>8;
693 unsigned char b3 = v>>16;
694 unsigned char b4 = v>>24;
700 void writer_writeFloat(writer_t*w, float f)
702 unsigned long v = *(unsigned long*)&f;
703 unsigned char b1 = v;
704 unsigned char b2 = v>>8;
705 unsigned char b3 = v>>16;
706 unsigned char b4 = v>>24;
712 void writer_writeDouble(writer_t*w, double f)
717 unsigned long long v = *(unsigned long long*)&f;
718 unsigned char b1 = v;
719 unsigned char b2 = v>>8;
720 unsigned char b3 = v>>16;
721 unsigned char b4 = v>>24;
722 unsigned char b5 = v>>32;
723 unsigned char b6 = v>>40;
724 unsigned char b7 = v>>48;
725 unsigned char b8 = v>>56;