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