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