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