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 */
27 #include "../config.h"
31 #define ZLIB_BUFFER_SIZE 16384
35 /* ---------------------------- null reader ------------------------------- */
37 static int reader_nullread(reader_t*r, void* data, int len)
42 static void reader_nullread_dealloc(reader_t*r)
44 memset(r, 0, sizeof(reader_t));
46 void reader_init_nullreader(reader_t*r)
48 r->read = reader_nullread;
49 r->dealloc = reader_nullread_dealloc;
51 r->type = READER_TYPE_NULL;
56 /* ---------------------------- file reader ------------------------------- */
58 static int reader_fileread(reader_t*reader, void* data, int len)
60 int ret = read((int)reader->internal, data, len);
65 void reader_init_filereader(reader_t*r, int handle)
67 r->read = reader_fileread;
68 r->internal = (void*)handle;
69 r->type = READER_TYPE_FILE;
75 /* ---------------------------- mem reader ------------------------------- */
82 static int reader_memread(reader_t*reader, void* data, int len)
84 struct memread_t*mr = (struct memread_t*)reader->internal;
86 if(mr->length - reader->pos > len) {
87 memcpy(data, &mr->data[reader->pos], len);
91 memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
92 reader->pos += mr->length;
93 return mr->length - reader->pos;
96 static void reader_memread_dealloc(reader_t*reader)
99 free(reader->internal);
100 memset(reader, 0, sizeof(reader_t));
102 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
104 struct memread_t*mr = malloc(sizeof(struct memread_t));
106 mr->length = newlength;
107 r->read = reader_memread;
108 r->dealloc = reader_memread_dealloc;
109 r->internal = (void*)mr;
110 r->type = READER_TYPE_MEM;
116 /* ---------------------------- mem writer ------------------------------- */
124 static int writer_memwrite_write(writer_t*w, void* data, int len)
126 struct memwrite_t*mw = (struct memwrite_t*)w->internal;
127 if(mw->length - w->pos > len) {
128 memcpy(&mw->data[w->pos], data, len);
132 memcpy(&mw->data[w->pos], data, mw->length - w->pos);
134 return mw->length - w->pos;
137 static void writer_memwrite_finish(writer_t*w)
143 void writer_init_memwriter(writer_t*w, void*data, int len)
145 struct memwrite_t *mr;
146 mr = malloc(sizeof(struct memwrite_t));
149 memset(w, 0, sizeof(writer_t));
150 w->write = writer_memwrite_write;
151 w->finish = writer_memwrite_finish;
152 w->internal = (void*)mr;
153 w->type = WRITER_TYPE_MEM;
159 /* ------------------------- growing mem writer ------------------------------- */
161 struct growmemwrite_t
167 static int writer_growmemwrite_write(writer_t*w, void* data, int len)
169 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
171 fprintf(stderr, "Illegal write operation: data already given away");
174 if(mw->length - w->pos < len) {
175 unsigned char*newmem;
176 int newlength = mw->length;
177 while(newlength - w->pos < len) {
178 newlength += mw->grow;
181 newmem = malloc(newlength);
182 memcpy(newmem, mw->data, mw->length);
186 mw->data = realloc(mw->data, newlength);
188 mw->length = newlength;
190 memcpy(&mw->data[w->pos], data, len);
194 static void writer_growmemwrite_finish(writer_t*w)
196 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
201 free(w->internal);mw=0;
202 memset(w, 0, sizeof(writer_t));
204 void* writer_growmemwrite_getmem(writer_t*w)
206 struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
208 /* remove own reference so that neither write() nor finish() can free it.
209 It's property of the caller now.
214 void writer_init_growingmemwriter(writer_t*w, U32 grow)
216 struct growmemwrite_t *mr;
217 mr = malloc(sizeof(struct growmemwrite_t));
219 mr->data = malloc(mr->length);
221 memset(w, 0, sizeof(writer_t));
222 w->write = writer_growmemwrite_write;
223 w->finish = writer_growmemwrite_finish;
224 w->internal = (void*)mr;
225 w->type = WRITER_TYPE_GROWING_MEM;
231 /* ---------------------------- file writer ------------------------------- */
239 static int writer_filewrite_write(writer_t*w, void* data, int len)
241 struct filewrite_t * fw= (struct filewrite_t*)w->internal;
242 return write(fw->handle, data, len);
244 static void writer_filewrite_finish(writer_t*w)
246 struct filewrite_t *mr = (struct filewrite_t*)w->internal;
250 memset(w, 0, sizeof(writer_t));
252 void writer_init_filewriter(writer_t*w, int handle)
254 struct filewrite_t *mr = malloc(sizeof(struct filewrite_t));
257 memset(w, 0, sizeof(writer_t));
258 w->write = writer_filewrite_write;
259 w->finish = writer_filewrite_finish;
261 w->type = WRITER_TYPE_FILE;
266 void writer_init_filewriter2(writer_t*w, char*filename)
268 int fi = open("movie.swf",
272 O_WRONLY|O_CREAT|O_TRUNC, 0644);
273 writer_init_filewriter(w, fi);
274 ((struct filewrite_t*)w->internal)->free_handle = 1;
277 /* ---------------------------- null writer ------------------------------- */
279 static int writer_nullwrite_write(writer_t*w, void* data, int len)
284 static void writer_nullwrite_finish(writer_t*w)
286 memset(w, 0, sizeof(writer_t));
288 void writer_init_nullwriter(writer_t*w)
290 memset(w, 0, sizeof(writer_t));
291 w->write = writer_nullwrite_write;
292 w->finish = writer_nullwrite_finish;
294 w->type = WRITER_TYPE_NULL;
299 /* ---------------------------- zlibinflate reader -------------------------- */
306 unsigned char readbuffer[ZLIB_BUFFER_SIZE];
311 static void zlib_error(int ret, char* msg, z_stream*zs)
313 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
316 zs->msg?zs->msg:"unknown");
322 static int reader_zlibinflate(reader_t*reader, void* data, int len)
325 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
333 z->zs.next_out = data;
334 z->zs.avail_out = len;
337 if(!z->zs.avail_in) {
338 z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
339 z->zs.next_in = z->readbuffer;
342 ret = inflate(&z->zs, Z_NO_FLUSH);
344 ret = inflate(&z->zs, Z_FINISH);
347 ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
349 if (ret == Z_STREAM_END) {
350 int pos = z->zs.next_out - (Bytef*)data;
351 ret = inflateEnd(&z->zs);
352 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
353 free(reader->internal);
354 reader->internal = 0;
358 if(!z->zs.avail_out) {
365 fprintf(stderr, "Error: swftools was compiled without zlib support");
369 static void reader_zlibinflate_dealloc(reader_t*reader)
372 struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
373 /* test whether read() already did basic deallocation */
374 if(reader->internal) {
376 free(reader->internal);
378 memset(reader, 0, sizeof(reader_t));
381 void reader_init_zlibinflate(reader_t*r, reader_t*input)
384 struct zlibinflate_t*z;
386 memset(r, 0, sizeof(reader_t));
387 z = (struct zlibinflate_t*)malloc(sizeof(struct zlibinflate_t));
388 memset(z, 0, sizeof(struct zlibinflate_t));
390 r->read = reader_zlibinflate;
391 r->dealloc = reader_zlibinflate_dealloc;
392 r->type = READER_TYPE_ZLIB;
395 memset(&z->zs,0,sizeof(z_stream));
396 z->zs.zalloc = Z_NULL;
397 z->zs.zfree = Z_NULL;
398 z->zs.opaque = Z_NULL;
399 ret = inflateInit(&z->zs);
400 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
403 fprintf(stderr, "Error: swftools was compiled without zlib support");
408 /* ---------------------------- zlibdeflate writer -------------------------- */
415 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
419 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
422 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
424 if(writer->type != WRITER_TYPE_ZLIB) {
425 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
429 fprintf(stderr, "zlib not initialized!\n");
435 z->zs.next_in = data;
436 z->zs.avail_in = len;
439 ret = deflate(&z->zs, Z_NO_FLUSH);
441 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
443 if(z->zs.next_out != z->writebuffer) {
444 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
445 z->zs.next_out = z->writebuffer;
446 z->zs.avail_out = ZLIB_BUFFER_SIZE;
449 if(!z->zs.avail_in) {
456 fprintf(stderr, "Error: swftools was compiled without zlib support");
460 static void writer_zlibdeflate_finish(writer_t*writer)
463 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
466 if(writer->type != WRITER_TYPE_ZLIB) {
467 fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
474 ret = deflate(&z->zs, Z_FINISH);
476 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
478 if(z->zs.next_out != z->writebuffer) {
479 z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
480 z->zs.next_out = z->writebuffer;
481 z->zs.avail_out = ZLIB_BUFFER_SIZE;
484 if (ret == Z_STREAM_END) {
489 ret = deflateEnd(&z->zs);
490 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
491 free(writer->internal);
492 memset(writer, 0, sizeof(writer_t));
493 //output->finish(output);
495 fprintf(stderr, "Error: swftools was compiled without zlib support");
499 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
502 struct zlibdeflate_t*z;
504 memset(w, 0, sizeof(writer_t));
505 z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
506 memset(z, 0, sizeof(struct zlibdeflate_t));
508 w->write = writer_zlibdeflate_write;
509 w->finish = writer_zlibdeflate_finish;
510 w->type = WRITER_TYPE_ZLIB;
513 memset(&z->zs,0,sizeof(z_stream));
514 z->zs.zalloc = Z_NULL;
515 z->zs.zfree = Z_NULL;
516 z->zs.opaque = Z_NULL;
517 ret = deflateInit(&z->zs, 9);
518 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
521 z->zs.next_out = z->writebuffer;
522 z->zs.avail_out = ZLIB_BUFFER_SIZE;
524 fprintf(stderr, "Error: swftools was compiled without zlib support");
529 /* ----------------------- bit handling routines -------------------------- */
531 void writer_writebit(writer_t*w, int bit)
535 w->write(w, &w->mybyte, 1);
540 w->mybyte |= 1 << (7 - w->bitpos);
543 void writer_writebits(writer_t*w, unsigned int data, int bits)
548 writer_writebit(w, (data >> (bits-t-1))&1);
551 void writer_resetbits(writer_t*w)
554 w->write(w, &w->mybyte, 1);
559 unsigned int reader_readbit(reader_t*r)
564 r->read(r, &r->mybyte, 1);
566 return (r->mybyte>>(7-r->bitpos++))&1;
568 unsigned int reader_readbits(reader_t*r, int num)
575 val|=reader_readbit(r);
579 void reader_resetbits(reader_t*r)
586 U8 reader_readU8(reader_t*r)
592 U16 reader_readU16(reader_t*r)
599 U32 reader_readU32(reader_t*r)
606 return b1|b2<<8|b3<<16|b4<<24;
608 float reader_readFloat(reader_t*r)
615 U32 w = (b1|b2<<8|b3<<16|b4<<24);
618 double reader_readDouble(reader_t*r)
626 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);
629 char*reader_readString(reader_t*r)
632 writer_init_growingmemwriter(&g, 16);
634 U8 b = reader_readU8(r);
635 writer_writeU8(&g, b);
639 char*string = writer_growmemwrite_getmem(&g);
640 writer_growmemwrite_finish(&g);
644 void writer_writeString(writer_t*w, const char*s)
648 w->write(w, (void*)s, l);
649 w->write(w, &zero, 1);
651 void writer_writeU8(writer_t*w, unsigned char b)
655 void writer_writeU16(writer_t*w, unsigned short v)
657 unsigned char b1 = v;
658 unsigned char b2 = v>>8;
662 void writer_writeU32(writer_t*w, unsigned long v)
664 unsigned char b1 = v;
665 unsigned char b2 = v>>8;
666 unsigned char b3 = v>>16;
667 unsigned char b4 = v>>24;
673 void writer_writeFloat(writer_t*w, float f)
675 unsigned long v = *(unsigned long*)&f;
676 unsigned char b1 = v;
677 unsigned char b2 = v>>8;
678 unsigned char b3 = v>>16;
679 unsigned char b4 = v>>24;
685 void writer_writeDouble(writer_t*w, double f)
690 unsigned long long v = *(unsigned long long*)&f;
691 unsigned char b1 = v;
692 unsigned char b2 = v>>8;
693 unsigned char b3 = v>>16;
694 unsigned char b4 = v>>24;
695 unsigned char b5 = v>>32;
696 unsigned char b6 = v>>40;
697 unsigned char b7 = v>>48;
698 unsigned char b8 = v>>56;