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 memcpy(data, &mr->data[reader->pos], len);
97 memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
98 reader->pos += mr->length;
99 return mr->length - reader->pos;
102 static void reader_memread_dealloc(reader_t*reader)
105 free(reader->internal);
106 memset(reader, 0, sizeof(reader_t));
108 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
110 struct memread_t*mr = (struct memread_t*)malloc(sizeof(struct memread_t));
111 mr->data = (unsigned char*)newdata;
112 mr->length = newlength;
113 r->read = reader_memread;
114 r->dealloc = reader_memread_dealloc;
115 r->internal = (void*)mr;
116 r->type = READER_TYPE_MEM;
122 /* ---------------------------- mem writer ------------------------------- */
130 static int writer_memwrite_write(writer_t*w, void* data, int len)
132 struct memwrite_t*mw = (struct memwrite_t*)w->internal;
133 if(mw->length - w->pos > len) {
134 memcpy(&mw->data[w->pos], data, len);
138 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
140 return mw->length - w->pos;
143 static void writer_memwrite_finish(writer_t*w)
149 void writer_init_memwriter(writer_t*w, void*data, int len)
151 struct memwrite_t *mr;
152 mr = (memwrite_t *)malloc(sizeof(struct memwrite_t));
153 mr->data = (unsigned char *)data;
155 memset(w, 0, sizeof(writer_t));
156 w->write = writer_memwrite_write;
157 w->finish = writer_memwrite_finish;
158 w->internal = (void*)mr;
159 w->type = WRITER_TYPE_MEM;
165 /* ------------------------- growing mem writer ------------------------------- */
167 struct growmemwrite_t
173 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
175 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
177 fprintf(stderr, "Illegal write operation: data already given away");
180 if(mw->length - w->pos < len) {
181 unsigned char*newmem;
182 int newlength = mw->length;
183 while(newlength - w->pos < len) {
184 newlength += mw->grow;
187 newmem = (unsigned char*)malloc(newlength);
188 memcpy(newmem, mw->data, mw->length);
192 mw->data = (unsigned char*)realloc(mw->data, newlength);
194 mw->length = newlength;
196 memcpy(&mw->data[w->pos], data, len);
200 static void writer_growmemwrite_finish(writer_t*w)
202 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
207 free(w->internal);mw=0;
208 memset(w, 0, sizeof(writer_t));
210 void* writer_growmemwrite_getmem(writer_t*w)
212 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
214 /* remove own reference so that neither write() nor finish() can free it.
215 It's property of the caller now.
220 void writer_init_growingmemwriter(writer_t*w, U32 grow)
222 struct growmemwrite_t *mr;
223 mr = (struct growmemwrite_t *)malloc(sizeof(struct growmemwrite_t));
225 mr->data = (unsigned char *)malloc(mr->length);
227 memset(w, 0, sizeof(writer_t));
228 w->write = writer_growmemwrite_write;
229 w->finish = writer_growmemwrite_finish;
230 w->internal = (void*)mr;
231 w->type = WRITER_TYPE_GROWING_MEM;
237 /* ---------------------------- file writer ------------------------------- */
245 static int writer_filewrite_write(writer_t*w, void* data, int len)
247 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
248 return write(fw->handle, data, len);
250 static void writer_filewrite_finish(writer_t*w)
252 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
256 memset(w, 0, sizeof(writer_t));
258 void writer_init_filewriter(writer_t*w, int handle)
260 struct filewrite_t *mr = (struct filewrite_t *)malloc(sizeof(struct filewrite_t));
263 memset(w, 0, sizeof(writer_t));
264 w->write = writer_filewrite_write;
265 w->finish = writer_filewrite_finish;
267 w->type = WRITER_TYPE_FILE;
272 void writer_init_filewriter2(writer_t*w, char*filename)
274 int fi = open("movie.swf",
278 O_WRONLY|O_CREAT|O_TRUNC, 0644);
279 writer_init_filewriter(w, fi);
280 ((struct filewrite_t*)w->internal)->free_handle = 1;
283 /* ---------------------------- null writer ------------------------------- */
285 static int writer_nullwrite_write(writer_t*w, void* data, int len)
290 static void writer_nullwrite_finish(writer_t*w)
292 memset(w, 0, sizeof(writer_t));
294 void writer_init_nullwriter(writer_t*w)
296 memset(w, 0, sizeof(writer_t));
297 w->write = writer_nullwrite_write;
298 w->finish = writer_nullwrite_finish;
300 w->type = WRITER_TYPE_NULL;
305 /* ---------------------------- zlibinflate reader -------------------------- */
312 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
317 static void zlib_error(int ret, char* msg, z_stream*zs)
319 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
322 zs->msg?zs->msg:"unknown");
328 static int reader_zlibinflate(reader_t*reader, void* data, int len)
331 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
339 z->zs.next_out = (Bytef *)data;
340 z->zs.avail_out = len;
343 if(!z->zs.avail_in) {
344 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
345 z->zs.next_in = z->readbuffer;
348 ret = inflate(&z->zs, Z_NO_FLUSH);
350 ret = inflate(&z->zs, Z_FINISH);
353 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
355 if (ret == Z_STREAM_END) {
356 int pos = z->zs.next_out - (Bytef*)data;
357 ret = inflateEnd(&z->zs);
358 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
359 free(reader->internal);
360 reader->internal = 0;
364 if(!z->zs.avail_out) {
371 fprintf(stderr, "Error: swftools was compiled without zlib support");
375 static void reader_zlibinflate_dealloc(reader_t*reader)
378 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
379 /* test whether read() already did basic deallocation */
380 if(reader->internal) {
382 free(reader->internal);
384 memset(reader, 0, sizeof(reader_t));
387 void reader_init_zlibinflate(reader_t*r, reader_t*input)
390 struct zlibinflate_t*z;
392 memset(r, 0, sizeof(reader_t));
393 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
394 memset(z, 0, sizeof(struct zlibinflate_t));
396 r->read = reader_zlibinflate;
397 r->dealloc = reader_zlibinflate_dealloc;
398 r->type = READER_TYPE_ZLIB;
401 memset(&z->zs,0,sizeof(z_stream));
402 z->zs.zalloc = Z_NULL;
403 z->zs.zfree = Z_NULL;
404 z->zs.opaque = Z_NULL;
405 ret = inflateInit(&z->zs);
406 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
409 fprintf(stderr, "Error: swftools was compiled without zlib support");
414 /* ---------------------------- zlibdeflate writer -------------------------- */
421 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
425 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
428 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
430 if(writer->type != WRITER_TYPE_ZLIB) {
431 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
435 fprintf(stderr, "zlib not initialized!\n");
441 z->zs.next_in = (Bytef *)data;
442 z->zs.avail_in = len;
445 ret = deflate(&z->zs, Z_NO_FLUSH);
447 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
449 if(z->zs.next_out != z->writebuffer) {
450 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
451 z->zs.next_out = z->writebuffer;
452 z->zs.avail_out = ZLIB_BUFFER_SIZE;
455 if(!z->zs.avail_in) {
462 fprintf(stderr, "Error: swftools was compiled without zlib support");
466 static void writer_zlibdeflate_finish(writer_t*writer)
469 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
472 if(writer->type != WRITER_TYPE_ZLIB) {
473 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
480 ret = deflate(&z->zs, Z_FINISH);
482 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
484 if(z->zs.next_out != z->writebuffer) {
485 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
486 z->zs.next_out = z->writebuffer;
487 z->zs.avail_out = ZLIB_BUFFER_SIZE;
490 if (ret == Z_STREAM_END) {
495 ret = deflateEnd(&z->zs);
496 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
497 free(writer->internal);
498 memset(writer, 0, sizeof(writer_t));
499 //output->finish(output);
501 fprintf(stderr, "Error: swftools was compiled without zlib support");
505 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
508 struct zlibdeflate_t*z;
510 memset(w, 0, sizeof(writer_t));
511 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
512 memset(z, 0, sizeof(struct zlibdeflate_t));
514 w->write = writer_zlibdeflate_write;
515 w->finish = writer_zlibdeflate_finish;
516 w->type = WRITER_TYPE_ZLIB;
519 memset(&z->zs,0,sizeof(z_stream));
520 z->zs.zalloc = Z_NULL;
521 z->zs.zfree = Z_NULL;
522 z->zs.opaque = Z_NULL;
523 ret = deflateInit(&z->zs, 9);
524 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
527 z->zs.next_out = z->writebuffer;
528 z->zs.avail_out = ZLIB_BUFFER_SIZE;
530 fprintf(stderr, "Error: swftools was compiled without zlib support");
535 /* ----------------------- bit handling routines -------------------------- */
537 void writer_writebit(writer_t*w, int bit)
541 w->write(w, &w->mybyte, 1);
546 w->mybyte |= 1 << (7 - w->bitpos);
549 void writer_writebits(writer_t*w, unsigned int data, int bits)
554 writer_writebit(w, (data >> (bits-t-1))&1);
557 void writer_resetbits(writer_t*w)
560 w->write(w, &w->mybyte, 1);
565 unsigned int reader_readbit(reader_t*r)
570 r->read(r, &r->mybyte, 1);
572 return (r->mybyte>>(7-r->bitpos++))&1;
574 unsigned int reader_readbits(reader_t*r, int num)
581 val|=reader_readbit(r);
585 void reader_resetbits(reader_t*r)
592 U8 reader_readU8(reader_t*r)
598 U16 reader_readU16(reader_t*r)
605 U32 reader_readU32(reader_t*r)
612 return b1|b2<<8|b3<<16|b4<<24;
614 float reader_readFloat(reader_t*r)
621 U32 w = (b1|b2<<8|b3<<16|b4<<24);
624 double reader_readDouble(reader_t*r)
632 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);
635 char*reader_readString(reader_t*r)
638 writer_init_growingmemwriter(&g, 16);
640 U8 b = reader_readU8(r);
641 writer_writeU8(&g, b);
645 char*string = (char*)writer_growmemwrite_getmem(&g);
646 writer_growmemwrite_finish(&g);
650 void writer_writeString(writer_t*w, const char*s)
654 w->write(w, (void*)s, l);
655 w->write(w, &zero, 1);
657 void writer_writeU8(writer_t*w, unsigned char b)
661 void writer_writeU16(writer_t*w, unsigned short v)
663 unsigned char b1 = v;
664 unsigned char b2 = v>>8;
668 void writer_writeU32(writer_t*w, unsigned long v)
670 unsigned char b1 = v;
671 unsigned char b2 = v>>8;
672 unsigned char b3 = v>>16;
673 unsigned char b4 = v>>24;
679 void writer_writeFloat(writer_t*w, float f)
681 unsigned long v = *(unsigned long*)&f;
682 unsigned char b1 = v;
683 unsigned char b2 = v>>8;
684 unsigned char b3 = v>>16;
685 unsigned char b4 = v>>24;
691 void writer_writeDouble(writer_t*w, double f)
696 unsigned long long v = *(unsigned long long*)&f;
697 unsigned char b1 = v;
698 unsigned char b2 = v>>8;
699 unsigned char b3 = v>>16;
700 unsigned char b4 = v>>24;
701 unsigned char b5 = v>>32;
702 unsigned char b6 = v>>40;
703 unsigned char b7 = v>>48;
704 unsigned char b8 = v>>56;