header file for artsutils.c
[swftools.git] / lib / bitio.c
1 /*  bitio.c 
2     part of swftools
3     implementation of bitio.h.
4
5     Copyright (C) 2003 Matthias Kramm <kramm@quiss.org>
6
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.
11
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.
16
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 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_IO_H
27 #include <io.h>
28 #endif
29 #include <string.h>
30 #include <memory.h>
31 #include <fcntl.h>
32
33 #include "../config.h"
34
35 #ifdef HAVE_ZLIB
36 #include <zlib.h>
37 #define ZLIB_BUFFER_SIZE 16384
38 #endif
39 #include "./bitio.h"
40
41 /* ---------------------------- null reader ------------------------------- */
42
43 static int reader_nullread(reader_t*r, void* data, int len) 
44 {
45     memset(data, 0, len);
46     return len;
47 }
48 static void reader_nullread_dealloc(reader_t*r)
49 {
50     memset(r, 0, sizeof(reader_t));
51 }
52 void reader_init_nullreader(reader_t*r)
53 {
54     r->read = reader_nullread;
55     r->dealloc = reader_nullread_dealloc;
56     r->internal = 0;
57     r->type = READER_TYPE_NULL;
58     r->mybyte = 0;
59     r->bitpos = 8;
60     r->pos = 0;
61 }
62 /* ---------------------------- file reader ------------------------------- */
63
64 static int reader_fileread(reader_t*reader, void* data, int len) 
65 {
66     int ret = read((int)reader->internal, data, len);
67     if(ret>=0)
68         reader->pos += ret;
69     return ret;
70 }
71 void reader_init_filereader(reader_t*r, int handle)
72 {
73     r->read = reader_fileread;
74     r->internal = (void*)handle;
75     r->type = READER_TYPE_FILE;
76     r->mybyte = 0;
77     r->bitpos = 8;
78     r->pos = 0;
79 }
80
81 /* ---------------------------- mem reader ------------------------------- */
82
83 struct memread_t
84 {
85     unsigned char*data;
86     int length;
87 };
88 static int reader_memread(reader_t*reader, void* data, int len) 
89 {
90     struct memread_t*mr = (struct memread_t*)reader->internal;
91
92     if(mr->length - reader->pos > len) {
93         memcpy(data, &mr->data[reader->pos], len);
94         reader->pos += len;
95         return len;
96     } else {
97         memcpy(data, &mr->data[reader->pos], mr->length - reader->pos);
98         reader->pos += mr->length;
99         return mr->length - reader->pos;
100     }
101 }
102 static void reader_memread_dealloc(reader_t*reader)
103 {
104     if(reader->internal)
105         free(reader->internal);
106     memset(reader, 0, sizeof(reader_t));
107 }
108 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
109 {
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;
117     r->mybyte = 0;
118     r->bitpos = 8;
119     r->pos = 0;
120
121
122 /* ---------------------------- mem writer ------------------------------- */
123
124 struct memwrite_t
125 {
126     unsigned char*data;
127     int length;
128 };
129
130 static int writer_memwrite_write(writer_t*w, void* data, int len) 
131 {
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);
135         w->pos += len;
136         return len;
137     } else {
138         memcpy(&mw->data[w->pos], data, mw->length - w->pos);
139         w->pos = mw->length;
140         return mw->length - w->pos;
141     }
142 }
143 static void writer_memwrite_finish(writer_t*w)
144 {
145     if(w->internal) 
146         free(w->internal);
147     w->internal = 0;
148 }
149 void writer_init_memwriter(writer_t*w, void*data, int len)
150 {
151     struct memwrite_t *mr;
152     mr = (memwrite_t *)malloc(sizeof(struct memwrite_t));
153     mr->data = (unsigned char *)data;
154     mr->length = len;
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;
160     w->bitpos = 0;
161     w->mybyte = 0;
162     w->pos = 0;
163 }
164
165 /* ------------------------- growing mem writer ------------------------------- */
166
167 struct growmemwrite_t
168 {
169     unsigned char*data;
170     int length;
171     U32 grow;
172 };
173 static int writer_growmemwrite_write(writer_t*w, void* data, int len) 
174 {
175     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
176     if(!mw->data) {
177         fprintf(stderr, "Illegal write operation: data already given away");
178         exit(1);
179     }
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;
185         }
186 #ifdef NO_REALLOC
187         newmem = (unsigned char*)malloc(newlength);
188         memcpy(newmem, mw->data, mw->length);
189         free(mw->data);
190         mw->data = newmem;
191 #else
192         mw->data = (unsigned char*)realloc(mw->data, newlength);
193 #endif
194         mw->length = newlength;
195     }
196     memcpy(&mw->data[w->pos], data, len);
197     w->pos += len;
198     return len;
199 }
200 static void writer_growmemwrite_finish(writer_t*w)
201 {
202     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
203     if(mw->data)
204         free(mw->data);
205     mw->data = 0;
206     mw->length = 0;
207     free(w->internal);mw=0;
208     memset(w, 0, sizeof(writer_t));
209 }
210 void* writer_growmemwrite_getmem(writer_t*w)
211 {
212     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
213     void*ret = mw->data;
214     /* remove own reference so that neither write() nor finish() can free it.
215        It's property of the caller now.
216     */
217     mw->data = 0;
218     return ret;
219 }
220 void writer_init_growingmemwriter(writer_t*w, U32 grow)
221 {
222     struct growmemwrite_t *mr;
223     mr = (struct growmemwrite_t *)malloc(sizeof(struct growmemwrite_t));
224     mr->length = 4096;
225     mr->data = (unsigned char *)malloc(mr->length);
226     mr->grow = grow;
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;
232     w->bitpos = 0;
233     w->mybyte = 0;
234     w->pos = 0;
235 }
236
237 /* ---------------------------- file writer ------------------------------- */
238
239 struct filewrite_t
240 {
241     int handle;
242     char free_handle;
243 };
244
245 static int writer_filewrite_write(writer_t*w, void* data, int len) 
246 {
247     struct filewrite_t * fw= (struct filewrite_t*)w->internal;
248     return write(fw->handle, data, len);
249 }
250 static void writer_filewrite_finish(writer_t*w)
251 {
252     struct filewrite_t *mr = (struct filewrite_t*)w->internal;
253     if(mr->free_handle)
254         close(mr->handle);
255     free(w->internal);
256     memset(w, 0, sizeof(writer_t));
257 }
258 void writer_init_filewriter(writer_t*w, int handle)
259 {
260     struct filewrite_t *mr = (struct filewrite_t *)malloc(sizeof(struct filewrite_t));
261     mr->handle = handle;
262     mr->free_handle = 0;
263     memset(w, 0, sizeof(writer_t));
264     w->write = writer_filewrite_write;
265     w->finish = writer_filewrite_finish;
266     w->internal = mr;
267     w->type = WRITER_TYPE_FILE;
268     w->bitpos = 0;
269     w->mybyte = 0;
270     w->pos = 0;
271 }
272 void writer_init_filewriter2(writer_t*w, char*filename)
273 {
274     int fi = open("movie.swf",
275 #ifdef O_BINARY
276             O_BINARY|
277 #endif
278             O_WRONLY|O_CREAT|O_TRUNC, 0644);
279     writer_init_filewriter(w, fi);
280     ((struct filewrite_t*)w->internal)->free_handle = 1;
281 }
282
283 /* ---------------------------- null writer ------------------------------- */
284
285 static int writer_nullwrite_write(writer_t*w, void* data, int len) 
286 {
287     w->pos += len;
288     return len;
289 }
290 static void writer_nullwrite_finish(writer_t*w)
291 {
292     memset(w, 0, sizeof(writer_t));
293 }
294 void writer_init_nullwriter(writer_t*w)
295 {
296     memset(w, 0, sizeof(writer_t));
297     w->write = writer_nullwrite_write;
298     w->finish = writer_nullwrite_finish;
299     w->internal = 0;
300     w->type = WRITER_TYPE_NULL;
301     w->bitpos = 0;
302     w->mybyte = 0;
303     w->pos = 0;
304 }
305 /* ---------------------------- zlibinflate reader -------------------------- */
306
307 struct zlibinflate_t
308 {
309 #ifdef HAVE_ZLIB
310     z_stream zs;
311     reader_t*input;
312     unsigned char readbuffer[ZLIB_BUFFER_SIZE];
313 #endif
314 };
315
316 #ifdef HAVE_ZLIB
317 static void zlib_error(int ret, char* msg, z_stream*zs)
318 {
319     fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
320           msg,
321           ret,
322           zs->msg?zs->msg:"unknown");
323     perror("errno:");
324     exit(1);
325 }
326 #endif
327
328 static int reader_zlibinflate(reader_t*reader, void* data, int len) 
329 {
330 #ifdef HAVE_ZLIB
331     struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
332     int ret;
333     if(!z) {
334         return 0;
335     }
336     if(!len)
337         return 0;
338     
339     z->zs.next_out = (Bytef *)data;
340     z->zs.avail_out = len;
341
342     while(1) {
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;
346         }
347         if(z->zs.avail_in)
348             ret = inflate(&z->zs, Z_NO_FLUSH);
349         else
350             ret = inflate(&z->zs, Z_FINISH);
351     
352         if (ret != Z_OK &&
353             ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
354
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;
361                 reader->pos += pos;
362                 return pos;
363         }
364         if(!z->zs.avail_out) {
365             break;
366         }
367     }
368     reader->pos += len;
369     return len;
370 #else
371     fprintf(stderr, "Error: swftools was compiled without zlib support");
372     exit(1);
373 #endif
374 }
375 static void reader_zlibinflate_dealloc(reader_t*reader)
376 {
377 #ifdef HAVE_ZLIB
378     struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
379     /* test whether read() already did basic deallocation */
380     if(reader->internal) {
381         inflateEnd(&z->zs);
382         free(reader->internal);
383     }
384     memset(reader, 0, sizeof(reader_t));
385 #endif
386 }
387 void reader_init_zlibinflate(reader_t*r, reader_t*input)
388 {
389 #ifdef HAVE_ZLIB
390     struct zlibinflate_t*z;
391     int ret;
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));
395     r->internal = z;
396     r->read = reader_zlibinflate;
397     r->dealloc = reader_zlibinflate_dealloc;
398     r->type = READER_TYPE_ZLIB;
399     r->pos = 0;
400     z->input = input;
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);
407     reader_resetbits(r);
408 #else
409     fprintf(stderr, "Error: swftools was compiled without zlib support");
410     exit(1);
411 #endif
412 }
413
414 /* ---------------------------- zlibdeflate writer -------------------------- */
415
416 struct zlibdeflate_t
417 {
418 #ifdef HAVE_ZLIB
419     z_stream zs;
420     writer_t*output;
421     unsigned char writebuffer[ZLIB_BUFFER_SIZE];
422 #endif
423 };
424
425 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len) 
426 {
427 #ifdef HAVE_ZLIB
428     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
429     int ret;
430     if(writer->type != WRITER_TYPE_ZLIB) {
431         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
432         return 0;
433     }
434     if(!z) {
435         fprintf(stderr, "zlib not initialized!\n");
436         return 0;
437     }
438     if(!len)
439         return 0;
440     
441     z->zs.next_in = (Bytef *)data;
442     z->zs.avail_in = len;
443
444     while(1) {
445         ret = deflate(&z->zs, Z_NO_FLUSH);
446         
447         if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
448
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;
453         }
454
455         if(!z->zs.avail_in) {
456             break;
457         }
458     }
459     writer->pos += len;
460     return len;
461 #else
462     fprintf(stderr, "Error: swftools was compiled without zlib support");
463     exit(1);
464 #endif
465 }
466 static void writer_zlibdeflate_finish(writer_t*writer)
467 {
468 #ifdef HAVE_ZLIB
469     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
470     writer_t*output;
471     int ret;
472     if(writer->type != WRITER_TYPE_ZLIB) {
473         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
474         return;
475     }
476     if(!z)
477         return;
478     output= z->output;
479     while(1) {
480         ret = deflate(&z->zs, Z_FINISH);
481         if (ret != Z_OK &&
482             ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
483
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;
488         }
489
490         if (ret == Z_STREAM_END) {
491             break;
492
493         }
494     }
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); 
500 #else
501     fprintf(stderr, "Error: swftools was compiled without zlib support");
502     exit(1);
503 #endif
504 }
505 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
506 {
507 #ifdef HAVE_ZLIB
508     struct zlibdeflate_t*z;
509     int ret;
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));
513     w->internal = z;
514     w->write = writer_zlibdeflate_write;
515     w->finish = writer_zlibdeflate_finish;
516     w->type = WRITER_TYPE_ZLIB;
517     w->pos = 0;
518     z->output = output;
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);
525     w->bitpos = 0;
526     w->mybyte = 0;
527     z->zs.next_out = z->writebuffer;
528     z->zs.avail_out = ZLIB_BUFFER_SIZE;
529 #else
530     fprintf(stderr, "Error: swftools was compiled without zlib support");
531     exit(1);
532 #endif
533 }
534
535 /* ----------------------- bit handling routines -------------------------- */
536
537 void writer_writebit(writer_t*w, int bit)
538 {    
539     if(w->bitpos==8) 
540     {
541         w->write(w, &w->mybyte, 1);
542         w->bitpos = 0;
543         w->mybyte = 0;
544     }
545     if(bit&1)
546         w->mybyte |= 1 << (7 - w->bitpos);
547     w->bitpos ++;
548 }
549 void writer_writebits(writer_t*w, unsigned int data, int bits)
550 {
551     int t;
552     for(t=0;t<bits;t++)
553     {
554         writer_writebit(w, (data >> (bits-t-1))&1);
555     }
556 }
557 void writer_resetbits(writer_t*w)
558 {
559     if(w->bitpos)
560         w->write(w, &w->mybyte, 1);
561     w->bitpos = 0;
562     w->mybyte = 0;
563 }
564  
565 unsigned int reader_readbit(reader_t*r)
566 {
567     if(r->bitpos==8) 
568     {
569         r->bitpos=0;
570         r->read(r, &r->mybyte, 1);
571     }
572     return (r->mybyte>>(7-r->bitpos++))&1;
573 }
574 unsigned int reader_readbits(reader_t*r, int num)
575 {
576     int t;
577     int val = 0;
578     for(t=0;t<num;t++)
579     {
580         val<<=1;
581         val|=reader_readbit(r);
582     }
583     return val;
584 }
585 void reader_resetbits(reader_t*r)
586 {
587     r->mybyte = 0;
588     r->bitpos = 8;
589
590 }
591
592 U8 reader_readU8(reader_t*r)
593 {
594     U8 b;
595     r->read(r, &b, 1);
596     return b;
597 }
598 U16 reader_readU16(reader_t*r)
599 {
600     U8 b1,b2;
601     r->read(r, &b1, 1);
602     r->read(r, &b2, 1);
603     return b1|b2<<8;
604 }
605 U32 reader_readU32(reader_t*r)
606 {
607     U8 b1,b2,b3,b4;
608     r->read(r, &b1, 1);
609     r->read(r, &b2, 1);
610     r->read(r, &b3, 1);
611     r->read(r, &b4, 1);
612     return b1|b2<<8|b3<<16|b4<<24;
613 }
614 float reader_readFloat(reader_t*r)
615 {
616     U8 b1,b2,b3,b4;
617     r->read(r, &b1, 1);
618     r->read(r, &b2, 1);
619     r->read(r, &b3, 1);
620     r->read(r, &b4, 1);
621     U32 w = (b1|b2<<8|b3<<16|b4<<24);
622     return *(float*)&w;
623 }
624 double reader_readDouble(reader_t*r)
625 {
626     double f;
627     r->read(r, &f, 8);
628     return f;
629
630     U8 b[8];
631     r->read(r, b, 8);
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);
633     return *(double*)&w;
634 }
635 char*reader_readString(reader_t*r)
636 {
637     writer_t g;
638     writer_init_growingmemwriter(&g, 16);
639     while(1) {
640         U8 b = reader_readU8(r);
641         writer_writeU8(&g, b);
642         if(!b)
643             break;
644     }
645     char*string = (char*)writer_growmemwrite_getmem(&g);
646     writer_growmemwrite_finish(&g);
647     return string;
648 }
649
650 void writer_writeString(writer_t*w, const char*s)
651 {
652     int l = strlen(s);
653     char zero = 0;
654     w->write(w, (void*)s, l);
655     w->write(w, &zero, 1);
656 }
657 void writer_writeU8(writer_t*w, unsigned char b)
658 {
659     w->write(w, &b, 1);
660 }
661 void writer_writeU16(writer_t*w, unsigned short v)
662 {
663     unsigned char b1 = v;
664     unsigned char b2 = v>>8;
665     w->write(w, &b1, 1);
666     w->write(w, &b2, 1);
667 }
668 void writer_writeU32(writer_t*w, unsigned long v)
669 {
670     unsigned char b1 = v;
671     unsigned char b2 = v>>8;
672     unsigned char b3 = v>>16;
673     unsigned char b4 = v>>24;
674     w->write(w, &b1, 1);
675     w->write(w, &b2, 1);
676     w->write(w, &b3, 1);
677     w->write(w, &b4, 1);
678 }
679 void writer_writeFloat(writer_t*w, float f)
680 {
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;
686     w->write(w, &b1, 1);
687     w->write(w, &b2, 1);
688     w->write(w, &b3, 1);
689     w->write(w, &b4, 1);
690 }
691 void writer_writeDouble(writer_t*w, double f)
692 {
693     w->write(w, &f, 8);
694     return;
695
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;
705     w->write(w, &b1, 1);
706     w->write(w, &b2, 1);
707     w->write(w, &b3, 1);
708     w->write(w, &b4, 1);
709     w->write(w, &b5, 1);
710     w->write(w, &b6, 1);
711     w->write(w, &b7, 1);
712     w->write(w, &b8, 1);
713 }