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