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