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 = (struct 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 int newlength = mw->length;
182 while(newlength - w->pos < len) {
183 newlength += mw->grow;
186 unsigned char*newmem = (unsigned char*)malloc(newlength);
187 memcpy(newmem, mw->data, mw->length);
191 mw->data = (unsigned char*)realloc(mw->data, newlength);
193 mw->length = newlength;
195 memcpy(&mw->data[w->pos], data, len);
199 static void writer_growmemwrite_finish(writer_t*w)
201 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
206 free(w->internal);mw=0;
207 memset(w, 0, sizeof(writer_t));
209 void* writer_growmemwrite_getmem(writer_t*w)
211 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
213 /* remove own reference so that neither write() nor finish() can free it.
214 It's property of the caller now.
219 void writer_init_growingmemwriter(writer_t*w, U32 grow)
221 struct growmemwrite_t *mr;
222 mr = (struct growmemwrite_t *)malloc(sizeof(struct growmemwrite_t));
224 mr->data = (unsigned char *)malloc(mr->length);
226 memset(w, 0, sizeof(writer_t));
227 w->write = writer_growmemwrite_write;
228 w->finish = writer_growmemwrite_finish;
229 w->internal = (void*)mr;
230 w->type = WRITER_TYPE_GROWING_MEM;
236 /* ---------------------------- file writer ------------------------------- */
244 static int writer_filewrite_write(writer_t*w, void* data, int len)
246 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
247 return write(fw->handle, data, len);
249 static void writer_filewrite_finish(writer_t*w)
251 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
255 memset(w, 0, sizeof(writer_t));
257 void writer_init_filewriter(writer_t*w, int handle)
259 struct filewrite_t *mr = (struct filewrite_t *)malloc(sizeof(struct filewrite_t));
262 memset(w, 0, sizeof(writer_t));
263 w->write = writer_filewrite_write;
264 w->finish = writer_filewrite_finish;
266 w->type = WRITER_TYPE_FILE;
271 void writer_init_filewriter2(writer_t*w, char*filename)
273 int fi = open("movie.swf",
277 O_WRONLY|O_CREAT|O_TRUNC, 0644);
278 writer_init_filewriter(w, fi);
279 ((struct filewrite_t*)w->internal)->free_handle = 1;
282 /* ---------------------------- null writer ------------------------------- */
284 static int writer_nullwrite_write(writer_t*w, void* data, int len)
289 static void writer_nullwrite_finish(writer_t*w)
291 memset(w, 0, sizeof(writer_t));
293 void writer_init_nullwriter(writer_t*w)
295 memset(w, 0, sizeof(writer_t));
296 w->write = writer_nullwrite_write;
297 w->finish = writer_nullwrite_finish;
299 w->type = WRITER_TYPE_NULL;
304 /* ---------------------------- zlibinflate reader -------------------------- */
311 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
316 static void zlib_error(int ret, char* msg, z_stream*zs)
318 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
321 zs->msg?zs->msg:"unknown");
327 static int reader_zlibinflate(reader_t*reader, void* data, int len)
330 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
338 z->zs.next_out = (Bytef *)data;
339 z->zs.avail_out = len;
342 if(!z->zs.avail_in) {
343 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
344 z->zs.next_in = z->readbuffer;
347 ret = inflate(&z->zs, Z_NO_FLUSH);
349 ret = inflate(&z->zs, Z_FINISH);
352 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
354 if (ret == Z_STREAM_END) {
355 int pos = z->zs.next_out - (Bytef*)data;
356 ret = inflateEnd(&z->zs);
357 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
358 free(reader->internal);
359 reader->internal = 0;
363 if(!z->zs.avail_out) {
370 fprintf(stderr, "Error: swftools was compiled without zlib support");
374 static void reader_zlibinflate_dealloc(reader_t*reader)
377 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
378 /* test whether read() already did basic deallocation */
379 if(reader->internal) {
381 free(reader->internal);
383 memset(reader, 0, sizeof(reader_t));
386 void reader_init_zlibinflate(reader_t*r, reader_t*input)
389 struct zlibinflate_t*z;
391 memset(r, 0, sizeof(reader_t));
392 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
393 memset(z, 0, sizeof(struct zlibinflate_t));
395 r->read = reader_zlibinflate;
396 r->dealloc = reader_zlibinflate_dealloc;
397 r->type = READER_TYPE_ZLIB;
400 memset(&z->zs,0,sizeof(z_stream));
401 z->zs.zalloc = Z_NULL;
402 z->zs.zfree = Z_NULL;
403 z->zs.opaque = Z_NULL;
404 ret = inflateInit(&z->zs);
405 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
408 fprintf(stderr, "Error: swftools was compiled without zlib support");
413 /* ---------------------------- zlibdeflate writer -------------------------- */
420 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
424 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
427 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
429 if(writer->type != WRITER_TYPE_ZLIB) {
430 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
434 fprintf(stderr, "zlib not initialized!\n");
440 z->zs.next_in = (Bytef *)data;
441 z->zs.avail_in = len;
444 ret = deflate(&z->zs, Z_NO_FLUSH);
446 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
448 if(z->zs.next_out != z->writebuffer) {
449 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
450 z->zs.next_out = z->writebuffer;
451 z->zs.avail_out = ZLIB_BUFFER_SIZE;
454 if(!z->zs.avail_in) {
461 fprintf(stderr, "Error: swftools was compiled without zlib support");
465 static void writer_zlibdeflate_finish(writer_t*writer)
468 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
471 if(writer->type != WRITER_TYPE_ZLIB) {
472 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
479 ret = deflate(&z->zs, Z_FINISH);
481 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
483 if(z->zs.next_out != z->writebuffer) {
484 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
485 z->zs.next_out = z->writebuffer;
486 z->zs.avail_out = ZLIB_BUFFER_SIZE;
489 if (ret == Z_STREAM_END) {
494 ret = deflateEnd(&z->zs);
495 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
496 free(writer->internal);
497 memset(writer, 0, sizeof(writer_t));
498 //output->finish(output);
500 fprintf(stderr, "Error: swftools was compiled without zlib support");
504 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
507 struct zlibdeflate_t*z;
509 memset(w, 0, sizeof(writer_t));
510 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
511 memset(z, 0, sizeof(struct zlibdeflate_t));
513 w->write = writer_zlibdeflate_write;
514 w->finish = writer_zlibdeflate_finish;
515 w->type = WRITER_TYPE_ZLIB;
518 memset(&z->zs,0,sizeof(z_stream));
519 z->zs.zalloc = Z_NULL;
520 z->zs.zfree = Z_NULL;
521 z->zs.opaque = Z_NULL;
522 ret = deflateInit(&z->zs, 9);
523 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
526 z->zs.next_out = z->writebuffer;
527 z->zs.avail_out = ZLIB_BUFFER_SIZE;
529 fprintf(stderr, "Error: swftools was compiled without zlib support");
534 /* ----------------------- bit handling routines -------------------------- */
536 void writer_writebit(writer_t*w, int bit)
540 w->write(w, &w->mybyte, 1);
545 w->mybyte |= 1 << (7 - w->bitpos);
548 void writer_writebits(writer_t*w, unsigned int data, int bits)
553 writer_writebit(w, (data >> (bits-t-1))&1);
556 void writer_resetbits(writer_t*w)
559 w->write(w, &w->mybyte, 1);
564 unsigned int reader_readbit(reader_t*r)
569 r->read(r, &r->mybyte, 1);
571 return (r->mybyte>>(7-r->bitpos++))&1;
573 unsigned int reader_readbits(reader_t*r, int num)
580 val|=reader_readbit(r);
584 void reader_resetbits(reader_t*r)
591 U8 reader_readU8(reader_t*r)
597 U16 reader_readU16(reader_t*r)
604 U32 reader_readU32(reader_t*r)
611 return b1|b2<<8|b3<<16|b4<<24;
613 float reader_readFloat(reader_t*r)
620 U32 w = (b1|b2<<8|b3<<16|b4<<24);
623 double reader_readDouble(reader_t*r)
631 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);
634 char*reader_readString(reader_t*r)
637 writer_init_growingmemwriter(&g, 16);
639 U8 b = reader_readU8(r);
640 writer_writeU8(&g, b);
644 char*string = (char*)writer_growmemwrite_getmem(&g);
645 writer_growmemwrite_finish(&g);
649 void writer_writeString(writer_t*w, const char*s)
653 w->write(w, (void*)s, l);
654 w->write(w, &zero, 1);
656 void writer_writeU8(writer_t*w, unsigned char b)
660 void writer_writeU16(writer_t*w, unsigned short v)
662 unsigned char b1 = v;
663 unsigned char b2 = v>>8;
667 void writer_writeU32(writer_t*w, unsigned long v)
669 unsigned char b1 = v;
670 unsigned char b2 = v>>8;
671 unsigned char b3 = v>>16;
672 unsigned char b4 = v>>24;
678 void writer_writeFloat(writer_t*w, float f)
680 unsigned long v = *(unsigned long*)&f;
681 unsigned char b1 = v;
682 unsigned char b2 = v>>8;
683 unsigned char b3 = v>>16;
684 unsigned char b4 = v>>24;
690 void writer_writeDouble(writer_t*w, double f)
695 unsigned long long v = *(unsigned long long*)&f;
696 unsigned char b1 = v;
697 unsigned char b2 = v>>8;
698 unsigned char b3 = v>>16;
699 unsigned char b4 = v>>24;
700 unsigned char b5 = v>>32;
701 unsigned char b6 = v>>40;
702 unsigned char b7 = v>>48;
703 unsigned char b8 = v>>56;