poppler: add new axial and radialShadedFill parameters
[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 #define __USE_LARGEFILE64
32 #include <fcntl.h>
33 #include <errno.h>
34
35 #include "../config.h"
36
37 #ifdef HAVE_ZLIB
38 #include <zlib.h>
39 #define ZLIB_BUFFER_SIZE 16384
40 #endif
41 #include "./bitio.h"
42
43 /* ---------------------------- null reader ------------------------------- */
44
45 static int reader_nullread(reader_t*r, void* data, int len) 
46 {
47     memset(data, 0, len);
48     return len;
49 }
50 static void reader_nullread_dealloc(reader_t*r)
51 {
52     memset(r, 0, sizeof(reader_t));
53 }
54 static int reader_nullseek(reader_t*r, int pos)
55 {
56     return pos;
57 }
58 void reader_init_nullreader(reader_t*r)
59 {
60     r->read = reader_nullread;
61     r->seek = reader_nullseek;
62     r->dealloc = reader_nullread_dealloc;
63     r->internal = 0;
64     r->type = READER_TYPE_NULL;
65     r->mybyte = 0;
66     r->bitpos = 8;
67     r->pos = 0;
68 }
69 /* ---------------------------- file reader ------------------------------- */
70
71 static int reader_fileread(reader_t*reader, void* data, int len) 
72 {
73     int ret = read((int)reader->internal, data, len);
74     if(ret>=0)
75         reader->pos += ret;
76     return ret;
77 }
78 static void reader_fileread_dealloc(reader_t*r)
79 {
80     if(r->type == READER_TYPE_FILE2) {
81         close((ptroff_t)r->internal);
82     }
83     memset(r, 0, sizeof(reader_t));
84 }
85 static int reader_fileread_seek(reader_t*r, int pos)
86 {
87     return lseek((ptroff_t)r->internal, pos, SEEK_SET);
88 }
89 void reader_init_filereader(reader_t*r, int handle)
90 {
91     r->read = reader_fileread;
92     r->seek = reader_fileread_seek;
93     r->dealloc = reader_fileread_dealloc;
94     r->internal = (void*)handle;
95     r->type = READER_TYPE_FILE;
96     r->mybyte = 0;
97     r->bitpos = 8;
98     r->pos = 0;
99 }
100 void reader_init_filereader2(reader_t*r, const char*filename)
101 {
102 #ifdef HAVE_OPEN64
103     int fi = open64
104 #else
105     int fi = open
106 #endif
107     (filename,
108 #ifdef O_BINARY
109             O_BINARY|
110 #endif
111             O_RDONLY);
112     reader_init_filereader(r, fi);
113     r->type = READER_TYPE_FILE2;
114 }
115
116 /* ---------------------------- mem reader ------------------------------- */
117
118 typedef struct _memread
119 {
120     unsigned char*data;
121     int length;
122 } memread_t;
123
124 static int reader_memread(reader_t*reader, void* data, int len) 
125 {
126     memread_t*mr = (memread_t*)reader->internal;
127
128     if(mr->length - reader->pos < len) {
129         len = mr->length - reader->pos;
130     }
131     if(!len) return 0;
132     memcpy(data, &mr->data[reader->pos], len);
133     reader->pos += len;
134     return len;
135 }
136 static int reader_memseek(reader_t*reader, int pos)
137 {
138     memread_t*mr = (memread_t*)reader->internal;
139     if(pos>=0 && pos<=mr->length) {
140         reader->pos = pos;
141         return pos;
142     } else {
143         return -1;
144     }
145 }
146 static void reader_memread_dealloc(reader_t*reader)
147 {
148     if(reader->internal)
149         free(reader->internal);
150     memset(reader, 0, sizeof(reader_t));
151 }
152 void reader_init_memreader(reader_t*r, void*newdata, int newlength)
153 {
154     memread_t*mr = (memread_t*)malloc(sizeof(memread_t));
155     mr->data = (unsigned char*)newdata;
156     mr->length = newlength;
157     r->read = reader_memread;
158     r->seek = reader_memseek;
159     r->dealloc = reader_memread_dealloc;
160     r->internal = (void*)mr;
161     r->type = READER_TYPE_MEM;
162     r->mybyte = 0;
163     r->bitpos = 8;
164     r->pos = 0;
165
166
167 /* ---------------------------- zzip reader ------------------------------ */
168 #ifdef HAVE_ZZIP
169 static int reader_zzip_read(reader_t*reader, void* data, int len) 
170 {
171     return zzip_file_read((ZZIP_FILE*)reader->internal, data, len);
172 }
173 static void reader_zzip_dealloc(reader_t*reader)
174 {
175     memset(reader, 0, sizeof(reader_t));
176 }
177 static int reader_zzip_seek(reader_t*reader, int pos)
178 {
179     return zzip_seek((ZZIP_FILE*)reader->internal, pos, SEEK_SET);
180 }
181 void reader_init_zzipreader(reader_t*r,ZZIP_FILE*z)
182 {
183     r->read = reader_zzip_read;
184     r->seek = reader_zzip_seek;
185     r->dealloc = reader_zzip_dealloc;
186     r->internal = z;
187     r->type = READER_TYPE_ZZIP;
188     r->mybyte = 0;
189     r->bitpos = 8;
190     r->pos = 0;
191 }
192 #endif
193
194 /* ---------------------------- mem writer ------------------------------- */
195
196 typedef struct _memwrite
197 {
198     unsigned char*data;
199     int length;
200 } memwrite_t;
201
202 static int writer_memwrite_write(writer_t*w, void* data, int len) 
203 {
204     memwrite_t*mw = (memwrite_t*)w->internal;
205     if(mw->length - w->pos > len) {
206         memcpy(&mw->data[w->pos], data, len);
207         w->pos += len;
208         return len;
209     } else {
210         memcpy(&mw->data[w->pos], data, mw->length - w->pos);
211         w->pos = mw->length;
212         return mw->length - w->pos;
213     }
214 }
215 static void writer_memwrite_finish(writer_t*w)
216 {
217     if(w->internal) 
218         free(w->internal);
219     w->internal = 0;
220 }
221 static void dummy_flush(writer_t*w)
222 {
223 }
224 void writer_init_memwriter(writer_t*w, void*data, int len)
225 {
226     memwrite_t *mr;
227     mr = (memwrite_t*)malloc(sizeof(memwrite_t));
228     mr->data = (unsigned char *)data;
229     mr->length = len;
230     memset(w, 0, sizeof(writer_t));
231     w->write = writer_memwrite_write;
232     w->flush = dummy_flush;
233     w->finish = writer_memwrite_finish;
234     w->internal = (void*)mr;
235     w->type = WRITER_TYPE_MEM;
236     w->bitpos = 0;
237     w->mybyte = 0;
238     w->pos = 0;
239 }
240
241 /* ------------------------- growing mem writer ------------------------------- */
242
243 typedef struct _growmemwrite
244 {
245     unsigned char*data;
246     int length;
247     U32 grow;
248 } growmemwrite_t;
249 static int writer_growmemwrite_write(writer_t*w, void* data, int len) 
250 {
251     growmemwrite_t*mw = (growmemwrite_t*)w->internal;
252     if(!mw->data) {
253         fprintf(stderr, "Illegal write operation: data already given away");
254         exit(1);
255     }
256     if(mw->length - w->pos < len) {
257         int newlength = mw->length;
258         while(newlength - w->pos < len) {
259             newlength += mw->grow;
260         }
261 #ifdef NO_REALLOC
262         unsigned char*newmem = (unsigned char*)malloc(newlength);
263         memcpy(newmem, mw->data, mw->length);
264         free(mw->data);
265         mw->data = newmem;
266 #else
267         mw->data = (unsigned char*)realloc(mw->data, newlength);
268 #endif
269         mw->length = newlength;
270     }
271     memcpy(&mw->data[w->pos], data, len);
272     w->pos += len;
273     return len;
274 }
275 static void writer_growmemwrite_finish(writer_t*w)
276 {
277     growmemwrite_t*mw = (growmemwrite_t*)w->internal;
278     if(mw->data) {
279         free(mw->data);mw->data = 0;
280     }
281     mw->length = 0;
282     free(w->internal);mw=0;
283     memset(w, 0, sizeof(writer_t));
284 }
285 void* writer_growmemwrite_memptr(writer_t*w, int*len)
286 {
287     growmemwrite_t*mw = (growmemwrite_t*)w->internal;
288     if(len) {
289         *len = w->pos;
290     }
291     return mw->data;
292 }
293 void* writer_growmemwrite_getmem(writer_t*w)
294 {
295     growmemwrite_t*mw = (growmemwrite_t*)w->internal;
296     void*ret = mw->data;
297     /* remove own reference so that neither write() nor finish() can free it.
298        It's property of the caller now.
299     */
300     mw->data = 0;
301     return ret;
302 }
303 void writer_growmemwrite_reset(writer_t*w)
304 {
305     growmemwrite_t*mw = (growmemwrite_t*)w->internal;
306     w->pos = 0;
307     w->bitpos = 0;
308     w->mybyte = 0;
309 }
310 void writer_init_growingmemwriter(writer_t*w, U32 grow)
311 {
312     growmemwrite_t *mw = (growmemwrite_t *)malloc(sizeof(growmemwrite_t));
313     mw->length = 4096;
314     mw->data = (unsigned char *)malloc(mw->length);
315     mw->grow = grow;
316     memset(w, 0, sizeof(writer_t));
317     w->write = writer_growmemwrite_write;
318     w->flush = dummy_flush;
319     w->finish = writer_growmemwrite_finish;
320     w->internal = (void*)mw;
321     w->type = WRITER_TYPE_GROWING_MEM;
322     w->bitpos = 0;
323     w->mybyte = 0;
324     w->pos = 0;
325 }
326
327 /* ---------------------------- file writer ------------------------------- */
328
329 typedef struct _filewrite
330 {
331     int handle;
332     char free_handle;
333 } filewrite_t;
334
335 static int writer_filewrite_write(writer_t*w, void* data, int len) 
336 {
337     filewrite_t * fw= (filewrite_t*)w->internal;
338     w->pos += len;
339     return write(fw->handle, data, len);
340 }
341 static void writer_filewrite_finish(writer_t*w)
342 {
343     filewrite_t *mr = (filewrite_t*)w->internal;
344     if(mr->free_handle)
345         close(mr->handle);
346     free(w->internal);
347     memset(w, 0, sizeof(writer_t));
348 }
349 void writer_init_filewriter(writer_t*w, int handle)
350 {
351     filewrite_t *mr = (filewrite_t *)malloc(sizeof(filewrite_t));
352     mr->handle = handle;
353     mr->free_handle = 0;
354     memset(w, 0, sizeof(writer_t));
355     w->write = writer_filewrite_write;
356     w->finish = writer_filewrite_finish;
357     w->internal = mr;
358     w->type = WRITER_TYPE_FILE;
359     w->bitpos = 0;
360     w->mybyte = 0;
361     w->pos = 0;
362 }
363 void writer_init_filewriter2(writer_t*w, char*filename)
364 {
365 #ifdef HAVE_OPEN64
366     int fi = open64
367 #else
368     int fi = open
369 #endif
370     (filename,
371 #ifdef O_BINARY
372             O_BINARY|
373 #endif
374             O_WRONLY|O_CREAT|O_TRUNC, 0644);
375     writer_init_filewriter(w, fi);
376     ((filewrite_t*)w->internal)->free_handle = 1;
377 }
378
379 /* ---------------------------- null writer ------------------------------- */
380
381 static int writer_nullwrite_write(writer_t*w, void* data, int len) 
382 {
383     w->pos += len;
384     return len;
385 }
386 static void writer_nullwrite_finish(writer_t*w)
387 {
388     memset(w, 0, sizeof(writer_t));
389 }
390 void writer_init_nullwriter(writer_t*w)
391 {
392     memset(w, 0, sizeof(writer_t));
393     w->write = writer_nullwrite_write;
394     w->flush = dummy_flush;
395     w->finish = writer_nullwrite_finish;
396     w->internal = 0;
397     w->type = WRITER_TYPE_NULL;
398     w->bitpos = 0;
399     w->mybyte = 0;
400     w->pos = 0;
401 }
402 /* ---------------------------- zlibinflate reader -------------------------- */
403
404 typedef struct _zlibinflate
405 {
406 #ifdef HAVE_ZLIB
407     z_stream zs;
408     reader_t*input;
409     unsigned char readbuffer[ZLIB_BUFFER_SIZE];
410 #endif
411 } zlibinflate_t;
412
413 #ifdef HAVE_ZLIB
414 static void zlib_error(int ret, char* msg, z_stream*zs)
415 {
416     fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
417           msg,
418           ret,
419           zs->msg?zs->msg:"unknown");
420     if(errno) perror("errno:");
421     exit(1);
422 }
423 #endif
424
425 static int reader_zlibinflate(reader_t*reader, void* data, int len) 
426 {
427 #ifdef HAVE_ZLIB
428     zlibinflate_t*z = (zlibinflate_t*)reader->internal;
429     int ret;
430     if(!z) {
431         return 0;
432     }
433     if(!len)
434         return 0;
435     
436     z->zs.next_out = (Bytef *)data;
437     z->zs.avail_out = len;
438
439     while(1) {
440         if(!z->zs.avail_in) {
441             z->zs.avail_in = z->input->read(z->input, z->readbuffer, ZLIB_BUFFER_SIZE);
442             z->zs.next_in = z->readbuffer;
443         }
444         if(z->zs.avail_in)
445             ret = inflate(&z->zs, Z_NO_FLUSH);
446         else
447             ret = inflate(&z->zs, Z_FINISH);
448     
449         if (ret != Z_OK &&
450             ret != Z_STREAM_END) zlib_error(ret, "bitio:inflate_inflate", &z->zs);
451
452         if (ret == Z_STREAM_END) {
453                 int pos = z->zs.next_out - (Bytef*)data;
454                 ret = inflateEnd(&z->zs);
455                 if (ret != Z_OK) zlib_error(ret, "bitio:inflate_end", &z->zs);
456                 free(reader->internal);
457                 reader->internal = 0;
458                 reader->pos += pos;
459                 return pos;
460         }
461         if(!z->zs.avail_out) {
462             break;
463         }
464     }
465     reader->pos += len;
466     return len;
467 #else
468     fprintf(stderr, "Error: swftools was compiled without zlib support");
469     exit(1);
470 #endif
471 }
472 static int reader_zlibseek(reader_t*reader, int pos)
473 {
474     fprintf(stderr, "Erro: seeking not supported for zlib streams");
475     return -1;
476 }
477 static void reader_zlibinflate_dealloc(reader_t*reader)
478 {
479 #ifdef HAVE_ZLIB
480     zlibinflate_t*z = (zlibinflate_t*)reader->internal;
481     /* test whether read() already did basic deallocation */
482     if(reader->internal) {
483         inflateEnd(&z->zs);
484         free(reader->internal);
485     }
486     memset(reader, 0, sizeof(reader_t));
487 #endif
488 }
489 void reader_init_zlibinflate(reader_t*r, reader_t*input)
490 {
491 #ifdef HAVE_ZLIB
492     zlibinflate_t*z = (zlibinflate_t*)malloc(sizeof(zlibinflate_t));
493     memset(z, 0, sizeof(zlibinflate_t));
494     int ret;
495     memset(r, 0, sizeof(reader_t));
496     r->internal = z;
497     r->read = reader_zlibinflate;
498     r->seek = reader_zlibseek;
499     r->dealloc = reader_zlibinflate_dealloc;
500     r->type = READER_TYPE_ZLIB;
501     r->pos = 0;
502     z->input = input;
503     memset(&z->zs,0,sizeof(z_stream));
504     z->zs.zalloc = Z_NULL;
505     z->zs.zfree  = Z_NULL;
506     z->zs.opaque = Z_NULL;
507     ret = inflateInit(&z->zs);
508     if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs);
509     reader_resetbits(r);
510 #else
511     fprintf(stderr, "Error: swftools was compiled without zlib support");
512     exit(1);
513 #endif
514 }
515
516 /* ---------------------------- zlibdeflate writer -------------------------- */
517
518 typedef struct _zlibdeflate
519 {
520 #ifdef HAVE_ZLIB
521     z_stream zs;
522     writer_t*output;
523     unsigned char writebuffer[ZLIB_BUFFER_SIZE];
524 #endif
525 } zlibdeflate_t;
526
527 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len) 
528 {
529 #ifdef HAVE_ZLIB
530     zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
531     int ret;
532     if(writer->type != WRITER_TYPE_ZLIB) {
533         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
534         return 0;
535     }
536     if(!z) {
537         fprintf(stderr, "zlib not initialized!\n");
538         return 0;
539     }
540     if(!len)
541         return 0;
542     
543     z->zs.next_in = (Bytef *)data;
544     z->zs.avail_in = len;
545
546     while(1) {
547         ret = deflate(&z->zs, Z_NO_FLUSH);
548         
549         if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
550
551         if(z->zs.next_out != z->writebuffer) {
552             writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
553             z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
554             z->zs.next_out = z->writebuffer;
555             z->zs.avail_out = ZLIB_BUFFER_SIZE;
556         }
557
558         if(!z->zs.avail_in) {
559             break;
560         }
561     }
562     return len;
563 #else
564     fprintf(stderr, "Error: swftools was compiled without zlib support");
565     exit(1);
566 #endif
567 }
568
569 void writer_zlibdeflate_flush(writer_t*writer)
570 {
571 #ifdef HAVE_ZLIB
572     zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
573     int ret;
574     if(writer->type != WRITER_TYPE_ZLIB) {
575         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
576         return;
577     }
578     if(!z) {
579         fprintf(stderr, "zlib not initialized!\n");
580         return;
581     }
582
583     z->zs.next_in = 0;
584     z->zs.avail_in = 0;
585     while(1) {
586         ret = deflate(&z->zs, Z_SYNC_FLUSH);
587         if (ret != Z_OK) zlib_error(ret, "bitio:deflate_flush", &z->zs);
588         if(z->zs.next_out != z->writebuffer) {
589             writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
590             z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
591             z->zs.next_out = z->writebuffer;
592             z->zs.avail_out = ZLIB_BUFFER_SIZE;
593         } 
594         /* TODO: how will zlib let us know it needs more buffer space? */
595         break;
596     }
597     return;
598 #else
599     fprintf(stderr, "Error: swftools was compiled without zlib support");
600     exit(1);
601 #endif
602 }
603
604 static void writer_zlibdeflate_finish(writer_t*writer)
605 {
606 #ifdef HAVE_ZLIB
607     zlibdeflate_t*z = (zlibdeflate_t*)writer->internal;
608     writer_t*output;
609     int ret;
610     if(writer->type != WRITER_TYPE_ZLIB) {
611         fprintf(stderr, "Wrong writer ID (writer not initialized?)\n");
612         return;
613     }
614     if(!z)
615         return;
616     output= z->output;
617     while(1) {
618         ret = deflate(&z->zs, Z_FINISH);
619         if (ret != Z_OK &&
620             ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
621
622         if(z->zs.next_out != z->writebuffer) {
623             writer->pos += z->zs.next_out - (Bytef*)z->writebuffer;
624             z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
625             z->zs.next_out = z->writebuffer;
626             z->zs.avail_out = ZLIB_BUFFER_SIZE;
627         }
628
629         if (ret == Z_STREAM_END) {
630             break;
631
632         }
633     }
634     ret = deflateEnd(&z->zs);
635     if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
636     free(writer->internal);
637     memset(writer, 0, sizeof(writer_t));
638     //output->finish(output); 
639 #else
640     fprintf(stderr, "Error: swftools was compiled without zlib support");
641     exit(1);
642 #endif
643 }
644 void writer_init_zlibdeflate(writer_t*w, writer_t*output)
645 {
646 #ifdef HAVE_ZLIB
647     zlibdeflate_t*z;
648     int ret;
649     memset(w, 0, sizeof(writer_t));
650     z = (zlibdeflate_t*)malloc(sizeof(zlibdeflate_t));
651     memset(z, 0, sizeof(zlibdeflate_t));
652     w->internal = z;
653     w->write = writer_zlibdeflate_write;
654     w->flush = writer_zlibdeflate_flush;
655     w->finish = writer_zlibdeflate_finish;
656     w->type = WRITER_TYPE_ZLIB;
657     w->pos = 0;
658     z->output = output;
659     memset(&z->zs,0,sizeof(z_stream));
660     z->zs.zalloc = Z_NULL;
661     z->zs.zfree  = Z_NULL;
662     z->zs.opaque = Z_NULL;
663     ret = deflateInit(&z->zs, 9);
664     if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
665     w->bitpos = 0;
666     w->mybyte = 0;
667     z->zs.next_out = z->writebuffer;
668     z->zs.avail_out = ZLIB_BUFFER_SIZE;
669 #else
670     fprintf(stderr, "Error: swftools was compiled without zlib support");
671     exit(1);
672 #endif
673 }
674
675 /* ----------------------- bit handling routines -------------------------- */
676
677 void writer_writebit(writer_t*w, int bit)
678 {    
679     if(w->bitpos==8) 
680     {
681         w->write(w, &w->mybyte, 1);
682         w->bitpos = 0;
683         w->mybyte = 0;
684     }
685     if(bit&1)
686         w->mybyte |= 1 << (7 - w->bitpos);
687     w->bitpos ++;
688 }
689 void writer_writebits(writer_t*w, unsigned int data, int bits)
690 {
691     int t;
692     for(t=0;t<bits;t++)
693     {
694         writer_writebit(w, (data >> (bits-t-1))&1);
695     }
696 }
697 void writer_resetbits(writer_t*w)
698 {
699     if(w->bitpos)
700         w->write(w, &w->mybyte, 1);
701     w->bitpos = 0;
702     w->mybyte = 0;
703 }
704  
705 unsigned int reader_readbit(reader_t*r)
706 {
707     if(r->bitpos==8) 
708     {
709         r->bitpos=0;
710         r->read(r, &r->mybyte, 1);
711     }
712     return (r->mybyte>>(7-r->bitpos++))&1;
713 }
714 unsigned int reader_readbits(reader_t*r, int num)
715 {
716     int t;
717     int val = 0;
718     for(t=0;t<num;t++)
719     {
720         val<<=1;
721         val|=reader_readbit(r);
722     }
723     return val;
724 }
725 void reader_resetbits(reader_t*r)
726 {
727     r->mybyte = 0;
728     r->bitpos = 8;
729
730 }
731
732 U8 reader_readU8(reader_t*r)
733 {
734     U8 b = 0;
735     if(r->read(r, &b, 1)<1) {
736         fprintf(stderr, "bitio.c:reader_readU8: Read over end of memory region\n");
737     }
738     return b;
739 }
740 U16 reader_readU16(reader_t*r)
741 {
742     U8 b1=0,b2=0;
743     if(r->read(r, &b1, 1)<1) {
744         fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
745     }
746     if(r->read(r, &b2, 1)<1) {
747         fprintf(stderr, "bitio.c:reader_readU16: Read over end of memory region\n");
748     }
749     return b1|b2<<8;
750 }
751 U32 reader_readU32(reader_t*r)
752 {
753     U8 b1=0,b2=0,b3=0,b4=0;
754     if(r->read(r, &b1, 1)<1)
755         fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
756     if(r->read(r, &b2, 1)<1)
757         fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
758     if(r->read(r, &b3, 1)<1)
759         fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
760     if(r->read(r, &b4, 1)<1)
761         fprintf(stderr, "bitio.c:reader_readU32: Read over end of memory region\n");
762     return b1|b2<<8|b3<<16|b4<<24;
763 }
764
765 float reader_readFloat(reader_t*r)
766 {
767     float f;
768     r->read(r, &f, 4);
769     return f;
770
771     U8 b1=0,b2=0,b3=0,b4=0;
772     r->read(r, &b1, 1);
773     r->read(r, &b2, 1);
774     r->read(r, &b3, 1);
775     r->read(r, &b4, 1);
776     U32 w = (b1|b2<<8|b3<<16|b4<<24);
777     return *(float*)&w;
778 }
779 double reader_readDouble(reader_t*r)
780 {
781     double f;
782     r->read(r, &f, 8);
783     return f;
784
785     U8 b[8];
786     r->read(r, b, 8);
787     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);
788     return *(double*)&w;
789 }
790 char*reader_readString(reader_t*r)
791 {
792     writer_t g;
793     writer_init_growingmemwriter(&g, 16);
794     while(1) {
795         U8 b = reader_readU8(r);
796         writer_writeU8(&g, b);
797         if(!b)
798             break;
799     }
800     char*string = (char*)writer_growmemwrite_getmem(&g);
801     g.finish(&g);
802     return string;
803 }
804
805 void writer_writeString(writer_t*w, const char*s)
806 {
807     int l = strlen(s);
808     char zero = 0;
809     w->write(w, (void*)s, l);
810     w->write(w, &zero, 1);
811 }
812 void writer_writeU8(writer_t*w, unsigned char b)
813 {
814     w->write(w, &b, 1);
815 }
816 void writer_writeU16(writer_t*w, unsigned short v)
817 {
818     unsigned char b1 = v;
819     unsigned char b2 = v>>8;
820     w->write(w, &b1, 1);
821     w->write(w, &b2, 1);
822 }
823 void writer_writeU32(writer_t*w, unsigned long v)
824 {
825     unsigned char b1 = v;
826     unsigned char b2 = v>>8;
827     unsigned char b3 = v>>16;
828     unsigned char b4 = v>>24;
829     w->write(w, &b1, 1);
830     w->write(w, &b2, 1);
831     w->write(w, &b3, 1);
832     w->write(w, &b4, 1);
833 }
834 void writer_writeFloat(writer_t*w, float f)
835 {
836     w->write(w, &f, 4);
837     return;
838
839     unsigned v = *(unsigned*)&f;
840     unsigned char b1 = v;
841     unsigned char b2 = v>>8;
842     unsigned char b3 = v>>16;
843     unsigned char b4 = v>>24;
844     w->write(w, &b1, 1);
845     w->write(w, &b2, 1);
846     w->write(w, &b3, 1);
847     w->write(w, &b4, 1);
848 }
849 void writer_writeDouble(writer_t*w, double f)
850 {
851     w->write(w, &f, 8);
852     return;
853
854     unsigned long long v = *(unsigned long long*)&f;
855     unsigned char b1 = v;
856     unsigned char b2 = v>>8;
857     unsigned char b3 = v>>16;
858     unsigned char b4 = v>>24;
859     unsigned char b5 = v>>32;
860     unsigned char b6 = v>>40;
861     unsigned char b7 = v>>48;
862     unsigned char b8 = v>>56;
863     w->write(w, &b1, 1);
864     w->write(w, &b2, 1);
865     w->write(w, &b3, 1);
866     w->write(w, &b4, 1);
867     w->write(w, &b5, 1);
868     w->write(w, &b6, 1);
869     w->write(w, &b7, 1);
870     w->write(w, &b8, 1);
871 }