added artsutils.o to object files
[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 = (struct 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         int newlength = mw->length;
182         while(newlength - w->pos < len) {
183             newlength += mw->grow;
184         }
185 #ifdef NO_REALLOC
186         unsigned char*newmem = (unsigned char*)malloc(newlength);
187         memcpy(newmem, mw->data, mw->length);
188         free(mw->data);
189         mw->data = newmem;
190 #else
191         mw->data = (unsigned char*)realloc(mw->data, newlength);
192 #endif
193         mw->length = newlength;
194     }
195     memcpy(&mw->data[w->pos], data, len);
196     w->pos += len;
197     return len;
198 }
199 static void writer_growmemwrite_finish(writer_t*w)
200 {
201     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
202     if(mw->data)
203         free(mw->data);
204     mw->data = 0;
205     mw->length = 0;
206     free(w->internal);mw=0;
207     memset(w, 0, sizeof(writer_t));
208 }
209 void* writer_growmemwrite_getmem(writer_t*w)
210 {
211     struct growmemwrite_t*mw = (struct growmemwrite_t*)w->internal;
212     void*ret = mw->data;
213     /* remove own reference so that neither write() nor finish() can free it.
214        It's property of the caller now.
215     */
216     mw->data = 0;
217     return ret;
218 }
219 void writer_init_growingmemwriter(writer_t*w, U32 grow)
220 {
221     struct growmemwrite_t *mr;
222     mr = (struct growmemwrite_t *)malloc(sizeof(struct growmemwrite_t));
223     mr->length = 4096;
224     mr->data = (unsigned char *)malloc(mr->length);
225     mr->grow = grow;
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;
231     w->bitpos = 0;
232     w->mybyte = 0;
233     w->pos = 0;
234 }
235
236 /* ---------------------------- file writer ------------------------------- */
237
238 struct filewrite_t
239 {
240     int handle;
241     char free_handle;
242 };
243
244 static int writer_filewrite_write(writer_t*w, void* data, int len) 
245 {
246     struct filewrite_t * fw= (struct filewrite_t*)w->internal;
247     return write(fw->handle, data, len);
248 }
249 static void writer_filewrite_finish(writer_t*w)
250 {
251     struct filewrite_t *mr = (struct filewrite_t*)w->internal;
252     if(mr->free_handle)
253         close(mr->handle);
254     free(w->internal);
255     memset(w, 0, sizeof(writer_t));
256 }
257 void writer_init_filewriter(writer_t*w, int handle)
258 {
259     struct filewrite_t *mr = (struct filewrite_t *)malloc(sizeof(struct filewrite_t));
260     mr->handle = handle;
261     mr->free_handle = 0;
262     memset(w, 0, sizeof(writer_t));
263     w->write = writer_filewrite_write;
264     w->finish = writer_filewrite_finish;
265     w->internal = mr;
266     w->type = WRITER_TYPE_FILE;
267     w->bitpos = 0;
268     w->mybyte = 0;
269     w->pos = 0;
270 }
271 void writer_init_filewriter2(writer_t*w, char*filename)
272 {
273     int fi = open("movie.swf",
274 #ifdef O_BINARY
275             O_BINARY|
276 #endif
277             O_WRONLY|O_CREAT|O_TRUNC, 0644);
278     writer_init_filewriter(w, fi);
279     ((struct filewrite_t*)w->internal)->free_handle = 1;
280 }
281
282 /* ---------------------------- null writer ------------------------------- */
283
284 static int writer_nullwrite_write(writer_t*w, void* data, int len) 
285 {
286     w->pos += len;
287     return len;
288 }
289 static void writer_nullwrite_finish(writer_t*w)
290 {
291     memset(w, 0, sizeof(writer_t));
292 }
293 void writer_init_nullwriter(writer_t*w)
294 {
295     memset(w, 0, sizeof(writer_t));
296     w->write = writer_nullwrite_write;
297     w->finish = writer_nullwrite_finish;
298     w->internal = 0;
299     w->type = WRITER_TYPE_NULL;
300     w->bitpos = 0;
301     w->mybyte = 0;
302     w->pos = 0;
303 }
304 /* ---------------------------- zlibinflate reader -------------------------- */
305
306 struct zlibinflate_t
307 {
308 #ifdef HAVE_ZLIB
309     z_stream zs;
310     reader_t*input;
311     unsigned char readbuffer[ZLIB_BUFFER_SIZE];
312 #endif
313 };
314
315 #ifdef HAVE_ZLIB
316 static void zlib_error(int ret, char* msg, z_stream*zs)
317 {
318     fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
319           msg,
320           ret,
321           zs->msg?zs->msg:"unknown");
322     perror("errno:");
323     exit(1);
324 }
325 #endif
326
327 static int reader_zlibinflate(reader_t*reader, void* data, int len) 
328 {
329 #ifdef HAVE_ZLIB
330     struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
331     int ret;
332     if(!z) {
333         return 0;
334     }
335     if(!len)
336         return 0;
337     
338     z->zs.next_out = (Bytef *)data;
339     z->zs.avail_out = len;
340
341     while(1) {
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;
345         }
346         if(z->zs.avail_in)
347             ret = inflate(&z->zs, Z_NO_FLUSH);
348         else
349             ret = inflate(&z->zs, Z_FINISH);
350     
351         if (ret != Z_OK &&
352             ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
353
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;
360                 reader->pos += pos;
361                 return pos;
362         }
363         if(!z->zs.avail_out) {
364             break;
365         }
366     }
367     reader->pos += len;
368     return len;
369 #else
370     fprintf(stderr, "Error: swftools was compiled without zlib support");
371     exit(1);
372 #endif
373 }
374 static void reader_zlibinflate_dealloc(reader_t*reader)
375 {
376 #ifdef HAVE_ZLIB
377     struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal;
378     /* test whether read() already did basic deallocation */
379     if(reader->internal) {
380         inflateEnd(&z->zs);
381         free(reader->internal);
382     }
383     memset(reader, 0, sizeof(reader_t));
384 #endif
385 }
386 void reader_init_zlibinflate(reader_t*r, reader_t*input)
387 {
388 #ifdef HAVE_ZLIB
389     struct zlibinflate_t*z;
390     int ret;
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));
394     r->internal = z;
395     r->read = reader_zlibinflate;
396     r->dealloc = reader_zlibinflate_dealloc;
397     r->type = READER_TYPE_ZLIB;
398     r->pos = 0;
399     z->input = input;
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);
406     reader_resetbits(r);
407 #else
408     fprintf(stderr, "Error: swftools was compiled without zlib support");
409     exit(1);
410 #endif
411 }
412
413 /* ---------------------------- zlibdeflate writer -------------------------- */
414
415 struct zlibdeflate_t
416 {
417 #ifdef HAVE_ZLIB
418     z_stream zs;
419     writer_t*output;
420     unsigned char writebuffer[ZLIB_BUFFER_SIZE];
421 #endif
422 };
423
424 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len) 
425 {
426 #ifdef HAVE_ZLIB
427     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
428     int ret;
429     if(writer->type != WRITER_TYPE_ZLIB) {
430         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
431         return 0;
432     }
433     if(!z) {
434         fprintf(stderr, "zlib not initialized!\n");
435         return 0;
436     }
437     if(!len)
438         return 0;
439     
440     z->zs.next_in = (Bytef *)data;
441     z->zs.avail_in = len;
442
443     while(1) {
444         ret = deflate(&z->zs, Z_NO_FLUSH);
445         
446         if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
447
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;
452         }
453
454         if(!z->zs.avail_in) {
455             break;
456         }
457     }
458     writer->pos += len;
459     return len;
460 #else
461     fprintf(stderr, "Error: swftools was compiled without zlib support");
462     exit(1);
463 #endif
464 }
465 static void writer_zlibdeflate_finish(writer_t*writer)
466 {
467 #ifdef HAVE_ZLIB
468     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
469     writer_t*output;
470     int ret;
471     if(writer->type != WRITER_TYPE_ZLIB) {
472         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
473         return;
474     }
475     if(!z)
476         return;
477     output= z->output;
478     while(1) {
479         ret = deflate(&z->zs, Z_FINISH);
480         if (ret != Z_OK &&
481             ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
482
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;
487         }
488
489         if (ret == Z_STREAM_END) {
490             break;
491
492         }
493     }
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); 
499 #else
500     fprintf(stderr, "Error: swftools was compiled without zlib support");
501     exit(1);
502 #endif
503 }
504 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
505 {
506 #ifdef HAVE_ZLIB
507     struct zlibdeflate_t*z;
508     int ret;
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));
512     w->internal = z;
513     w->write = writer_zlibdeflate_write;
514     w->finish = writer_zlibdeflate_finish;
515     w->type = WRITER_TYPE_ZLIB;
516     w->pos = 0;
517     z->output = output;
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);
524     w->bitpos = 0;
525     w->mybyte = 0;
526     z->zs.next_out = z->writebuffer;
527     z->zs.avail_out = ZLIB_BUFFER_SIZE;
528 #else
529     fprintf(stderr, "Error: swftools was compiled without zlib support");
530     exit(1);
531 #endif
532 }
533
534 /* ----------------------- bit handling routines -------------------------- */
535
536 void writer_writebit(writer_t*w, int bit)
537 {    
538     if(w->bitpos==8) 
539     {
540         w->write(w, &w->mybyte, 1);
541         w->bitpos = 0;
542         w->mybyte = 0;
543     }
544     if(bit&1)
545         w->mybyte |= 1 << (7 - w->bitpos);
546     w->bitpos ++;
547 }
548 void writer_writebits(writer_t*w, unsigned int data, int bits)
549 {
550     int t;
551     for(t=0;t<bits;t++)
552     {
553         writer_writebit(w, (data >> (bits-t-1))&1);
554     }
555 }
556 void writer_resetbits(writer_t*w)
557 {
558     if(w->bitpos)
559         w->write(w, &w->mybyte, 1);
560     w->bitpos = 0;
561     w->mybyte = 0;
562 }
563  
564 unsigned int reader_readbit(reader_t*r)
565 {
566     if(r->bitpos==8) 
567     {
568         r->bitpos=0;
569         r->read(r, &r->mybyte, 1);
570     }
571     return (r->mybyte>>(7-r->bitpos++))&1;
572 }
573 unsigned int reader_readbits(reader_t*r, int num)
574 {
575     int t;
576     int val = 0;
577     for(t=0;t<num;t++)
578     {
579         val<<=1;
580         val|=reader_readbit(r);
581     }
582     return val;
583 }
584 void reader_resetbits(reader_t*r)
585 {
586     r->mybyte = 0;
587     r->bitpos = 8;
588
589 }
590
591 U8 reader_readU8(reader_t*r)
592 {
593     U8 b;
594     r->read(r, &b, 1);
595     return b;
596 }
597 U16 reader_readU16(reader_t*r)
598 {
599     U8 b1,b2;
600     r->read(r, &b1, 1);
601     r->read(r, &b2, 1);
602     return b1|b2<<8;
603 }
604 U32 reader_readU32(reader_t*r)
605 {
606     U8 b1,b2,b3,b4;
607     r->read(r, &b1, 1);
608     r->read(r, &b2, 1);
609     r->read(r, &b3, 1);
610     r->read(r, &b4, 1);
611     return b1|b2<<8|b3<<16|b4<<24;
612 }
613 float reader_readFloat(reader_t*r)
614 {
615     U8 b1,b2,b3,b4;
616     r->read(r, &b1, 1);
617     r->read(r, &b2, 1);
618     r->read(r, &b3, 1);
619     r->read(r, &b4, 1);
620     U32 w = (b1|b2<<8|b3<<16|b4<<24);
621     return *(float*)&w;
622 }
623 double reader_readDouble(reader_t*r)
624 {
625     double f;
626     r->read(r, &f, 8);
627     return f;
628
629     U8 b[8];
630     r->read(r, b, 8);
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);
632     return *(double*)&w;
633 }
634 char*reader_readString(reader_t*r)
635 {
636     writer_t g;
637     writer_init_growingmemwriter(&g, 16);
638     while(1) {
639         U8 b = reader_readU8(r);
640         writer_writeU8(&g, b);
641         if(!b)
642             break;
643     }
644     char*string = (char*)writer_growmemwrite_getmem(&g);
645     writer_growmemwrite_finish(&g);
646     return string;
647 }
648
649 void writer_writeString(writer_t*w, const char*s)
650 {
651     int l = strlen(s);
652     char zero = 0;
653     w->write(w, (void*)s, l);
654     w->write(w, &zero, 1);
655 }
656 void writer_writeU8(writer_t*w, unsigned char b)
657 {
658     w->write(w, &b, 1);
659 }
660 void writer_writeU16(writer_t*w, unsigned short v)
661 {
662     unsigned char b1 = v;
663     unsigned char b2 = v>>8;
664     w->write(w, &b1, 1);
665     w->write(w, &b2, 1);
666 }
667 void writer_writeU32(writer_t*w, unsigned long v)
668 {
669     unsigned char b1 = v;
670     unsigned char b2 = v>>8;
671     unsigned char b3 = v>>16;
672     unsigned char b4 = v>>24;
673     w->write(w, &b1, 1);
674     w->write(w, &b2, 1);
675     w->write(w, &b3, 1);
676     w->write(w, &b4, 1);
677 }
678 void writer_writeFloat(writer_t*w, float f)
679 {
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;
685     w->write(w, &b1, 1);
686     w->write(w, &b2, 1);
687     w->write(w, &b3, 1);
688     w->write(w, &b4, 1);
689 }
690 void writer_writeDouble(writer_t*w, double f)
691 {
692     w->write(w, &f, 8);
693     return;
694
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;
704     w->write(w, &b1, 1);
705     w->write(w, &b2, 1);
706     w->write(w, &b3, 1);
707     w->write(w, &b4, 1);
708     w->write(w, &b5, 1);
709     w->write(w, &b6, 1);
710     w->write(w, &b7, 1);
711     w->write(w, &b8, 1);
712 }