forked from erkyrath/blorblib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blorblib.c
822 lines (653 loc) · 23.1 KB
/
blorblib.c
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* blorblib.c: Blorb file reader library, version 1.0.2.
Designed by Andrew Plotkin <[email protected]>
http://www.eblong.com/zarf/blorb/index.html
This is portable code to read a Blorb file. Add it to your
interpreter, #include "blorb.h", and you're ready to go.
*/
#include <stdio.h>
#include <stdlib.h>
#include "blorb.h"
#include "blorblow.h"
#ifdef BLORB_BIG_ENDIAN
static char contentsticker[] = "\nBlorb Library 1.0 (big-endian)\n";
#define bb_native2(v) (v)
#define bb_native4(v) (v)
#endif
#ifdef BLORB_LITTLE_ENDIAN
static char contentsticker[] = "\nBlorb Library 1.0 (little-endian)\n";
#define bb_native2(v) \
( (((uint16)(v) >> 8) & 0x00ff) \
| (((uint16)(v) << 8) & 0xff00))
#define bb_native4(v) \
( (((uint32)(v) >> 24) & 0x000000ff) \
| (((uint32)(v) >> 8) & 0x0000ff00) \
| (((uint32)(v) << 8) & 0x00ff0000) \
| (((uint32)(v) << 24) & 0xff000000))
#endif
#ifndef bb_native4
"You must define either BLORB_BIG_ENDIAN or BLORB_LITTLE_ENDIAN in blorb.h \
in order to compile this library.";
#endif
static int lib_inited = FALSE;
static bb_err_t bb_initialize_map(bb_map_t *map);
static bb_err_t bb_initialize(void);
static int sortsplot(bb_resdesc_t **p1, bb_resdesc_t **p2);
/* Do some one-time startup tests. */
static bb_err_t bb_initialize()
{
union {
uint32 val;
char ch[4];
} test;
uint32 val;
if (sizeof(uint32) != 4 || sizeof(uint16) != 2)
return bb_err_CompileTime; /* Basic types are the wrong size. */
test.ch[0] = 0x13;
test.ch[1] = 0x57;
test.ch[2] = 0x9a;
test.ch[3] = 0xce;
val = test.val;
if (bb_native4(val) != 0x13579ace)
return bb_err_CompileTime; /* Wrong endianness. */
return bb_err_None;
}
bb_err_t bb_create_map(FILE *file, bb_map_t **newmap)
{
bb_err_t err;
bb_map_t *map;
size_t readlen;
uint32 nextpos, totallength;
bb_chunkdesc_t *chunks;
int chunks_size, numchunks;
uint32 buffer[4];
*newmap = NULL;
if (!lib_inited) {
err = bb_initialize();
if (err)
return err;
lib_inited = TRUE;
}
/* First, chew through the file and index the chunks. */
err = fseek(file, 0, 0);
if (err)
return bb_err_Read;
readlen = fread(buffer, sizeof(uint32), 3, file);
if (readlen != 3)
return bb_err_Read;
if (bb_native4(buffer[0]) != bb_ID_FORM)
return bb_err_Format;
if (bb_native4(buffer[2]) != bb_ID_IFRS)
return bb_err_Format;
totallength = bb_native4(buffer[1]) + 8;
nextpos = 12;
chunks_size = 8;
numchunks = 0;
chunks = (bb_chunkdesc_t *)malloc(sizeof(bb_chunkdesc_t) * chunks_size);
while (nextpos < totallength) {
uint32 type, len;
int chunum;
bb_chunkdesc_t *chu;
err = fseek(file, nextpos, 0);
if (err)
return bb_err_Read;
readlen = fread(buffer, sizeof(uint32), 2, file);
if (readlen != 2)
return bb_err_Read;
type = bb_native4(buffer[0]);
len = bb_native4(buffer[1]);
if (numchunks >= chunks_size) {
chunks_size *= 2;
chunks = (bb_chunkdesc_t *)realloc(chunks,
sizeof(bb_chunkdesc_t) * chunks_size);
}
chunum = numchunks;
chu = &(chunks[chunum]);
numchunks++;
chu->type = type;
chu->startpos = nextpos;
if (type == bb_ID_FORM) {
chu->datpos = nextpos;
chu->len = len+8;
}
else {
chu->datpos = nextpos+8;
chu->len = len;
}
chu->ptr = NULL;
chu->auxdatnum = -1;
nextpos = nextpos + len + 8;
if (nextpos & 1)
nextpos++;
if (nextpos > totallength)
return bb_err_Format;
}
/* The basic IFF structure seems to be ok, and we have a list of
chunks. Now we allocate the map structure itself. */
map = (bb_map_t *)malloc(sizeof(bb_map_t));
if (!map) {
free(chunks);
return bb_err_Alloc;
}
map->inited = bb_Inited_Magic;
map->file = file;
map->chunks = chunks;
map->numchunks = numchunks;
map->resources = NULL;
map->ressorted = NULL;
map->numresources = 0;
map->releasenum = 0;
map->zheader = NULL;
map->resolution = NULL;
map->palettechunk = -1;
map->palette = NULL;
map->auxsound = NULL;
map->auxpict = NULL;
/* Now we do everything else involved in loading the Blorb file,
such as building resource lists. */
err = bb_initialize_map(map);
if (err) {
bb_destroy_map(map);
return err;
}
*newmap = map;
return bb_err_None;
}
static bb_err_t bb_initialize_map(bb_map_t *map)
{
/* It is important that the map structure be kept valid during this
function. If this returns an error, bb_destroy_map() will be called. */
int ix, jx;
bb_result_t chunkres;
bb_err_t err;
uint32 *ptr;
uint32 len;
uint32 val;
int numres;
int gotindex = FALSE;
for (ix=0; ix<map->numchunks; ix++) {
bb_chunkdesc_t *chu = &map->chunks[ix];
switch (chu->type) {
case bb_ID_RIdx:
/* Resource index chunk: build the resource list and sort it. */
if (gotindex)
return bb_err_Format; /* duplicate index chunk */
err = bb_load_chunk_by_number(map, bb_method_Memory,
&chunkres, ix);
if (err)
return err;
ptr = chunkres.data.ptr;
len = chunkres.length;
val = ptr[0];
numres = bb_native4(val);
if (numres) {
int ix2;
bb_resdesc_t *resources;
bb_resdesc_t **ressorted;
if (len != numres*12+4)
return bb_err_Format; /* bad length field */
resources = (bb_resdesc_t *)malloc(numres * sizeof(bb_resdesc_t));
ressorted = (bb_resdesc_t **)malloc(numres * sizeof(bb_resdesc_t *));
if (!ressorted || !resources)
return bb_err_Alloc;
ix2 = 0;
for (jx=0; jx<numres; jx++) {
bb_resdesc_t *res = &(resources[jx]);
uint32 respos;
val = ptr[1+jx*3];
res->usage = bb_native4(val);
val = ptr[2+jx*3];
res->resnum = bb_native4(val);
val = ptr[3+jx*3];
respos = bb_native4(val);
while (ix2 < map->numchunks && map->chunks[ix2].startpos < respos)
ix2++;
if (ix2 >= map->numchunks || map->chunks[ix2].startpos != respos)
return bb_err_Format; /* start pos does not match a real chunk */
res->chunknum = ix2;
ressorted[jx] = res;
}
/* Sort a resource list (actually a list of pointers to structures
in map->resources.) This makes it easy to find resources by
usage and resource number. */
qsort(ressorted, numres, sizeof(bb_resdesc_t *),
(int (*)())&sortsplot);
map->numresources = numres;
map->resources = resources;
map->ressorted = ressorted;
}
bb_unload_chunk(map, ix);
gotindex = TRUE;
break;
case bb_ID_RelN:
/* Release number chunk: Get the release number. */
err = bb_load_chunk_by_number(map, bb_method_Memory,
&chunkres, ix);
if (err)
return err;
if (chunkres.length != 2)
return bb_err_Format;
{
uint16 val = *((uint16 *)chunkres.data.ptr);
map->releasenum = bb_native2(val);
}
bb_unload_chunk(map, ix);
break;
case bb_ID_IFhd:
/* Z-header chunk: Get the header info. */
err = bb_load_chunk_by_number(map, bb_method_Memory,
&chunkres, ix);
if (err)
return err;
if (chunkres.length < 13)
return bb_err_Format;
{
uint16 val;
bb_zheader_t *head = (bb_zheader_t *)malloc(sizeof(bb_zheader_t));
if (!head)
return bb_err_Alloc;
val = ((uint16 *)(chunkres.data.ptr))[0];
head->releasenum = bb_native2(val);
val = ((uint16 *)(chunkres.data.ptr))[4];
head->checksum = bb_native2(val);
for (jx=0; jx<6; jx++) {
head->serialnum[jx] = ((char *)(chunkres.data.ptr))[2+jx];
}
map->zheader = head;
}
bb_unload_chunk(map, ix);
break;
case bb_ID_Reso:
/* Resolution chunk: Get the window size data, and resolution
ratios for images. */
err = bb_load_chunk_by_number(map, bb_method_Memory,
&chunkres, ix);
if (err)
return err;
if (chunkres.length < 24)
return bb_err_Format;
ptr = chunkres.data.ptr;
len = chunkres.length;
{
bb_resolution_t *reso = (bb_resolution_t *)malloc(sizeof(bb_resolution_t));
if (!reso)
return bb_err_Alloc;
reso->px = bb_native4(ptr[0]);
reso->py = bb_native4(ptr[1]);
reso->minx = bb_native4(ptr[2]);
reso->miny = bb_native4(ptr[3]);
reso->maxx = bb_native4(ptr[4]);
reso->maxy = bb_native4(ptr[5]);
map->resolution = reso;
}
ptr += 6;
len -= 6*4;
len = len / 28;
if (len) {
bb_aux_pict_t *aux = (bb_aux_pict_t *)malloc(len * sizeof(bb_aux_pict_t));
for (jx=0; jx<len; jx++, ptr += 7) {
bb_result_t res;
err = bb_load_resource(map, bb_method_DontLoad, &res,
bb_ID_Pict, bb_native4(ptr[0]));
if (!err) {
bb_chunkdesc_t *chu = &(map->chunks[res.chunknum]);
if (chu->auxdatnum != -1)
return bb_err_Format; /* two image entries for this resource */
chu->auxdatnum = jx;
aux[jx].ratnum = bb_native4(ptr[1]);
aux[jx].ratden = bb_native4(ptr[2]);
aux[jx].minnum = bb_native4(ptr[3]);
aux[jx].minden = bb_native4(ptr[4]);
aux[jx].maxnum = bb_native4(ptr[5]);
aux[jx].maxden = bb_native4(ptr[6]);
}
}
map->auxpict = aux;
}
bb_unload_chunk(map, ix);
break;
case bb_ID_Loop:
/* Looping chunk: Get looping data for sounds. */
err = bb_load_chunk_by_number(map, bb_method_Memory,
&chunkres, ix);
if (err)
return err;
ptr = chunkres.data.ptr;
len = chunkres.length;
len = len / 8;
if (len) {
bb_aux_sound_t *aux = (bb_aux_sound_t *)malloc(len * sizeof(bb_aux_sound_t));
for (jx=0; jx<len; jx++, ptr += 2) {
bb_result_t res;
err = bb_load_resource(map, bb_method_DontLoad, &res,
bb_ID_Snd, bb_native4(ptr[0]));
if (!err) {
bb_chunkdesc_t *chu = &(map->chunks[res.chunknum]);
if (chu->auxdatnum != -1)
return bb_err_Format; /* two looping entries for this resource */
chu->auxdatnum = jx;
aux[jx].repeats = bb_native4(ptr[1]);
}
}
map->auxsound = aux;
}
bb_unload_chunk(map, ix);
break;
case bb_ID_Plte:
/* Palette chunk: Don't get the palette info now, since it may
be large and the interpreter may not care. But remember
the chunk number in case the interpreter asks later. */
map->palettechunk = ix;
break;
}
}
return bb_err_None;
}
bb_err_t bb_destroy_map(bb_map_t *map)
{
int ix;
if (!map || !map->chunks || map->inited != bb_Inited_Magic)
return bb_err_NotAMap;
for (ix=0; ix<map->numchunks; ix++) {
bb_chunkdesc_t *chu = &(map->chunks[ix]);
if (chu->ptr) {
free(chu->ptr);
chu->ptr = NULL;
}
}
if (map->chunks) {
free(map->chunks);
map->chunks = NULL;
}
map->numchunks = 0;
if (map->resources) {
free(map->resources);
map->resources = NULL;
}
if (map->ressorted) {
free(map->ressorted);
map->ressorted = NULL;
}
map->numresources = 0;
if (map->zheader) {
free(map->zheader);
map->zheader = NULL;
}
if (map->resolution) {
free(map->resolution);
map->resolution = NULL;
}
if (map->palette) {
if (!map->palette->isdirect && map->palette->data.table.colors) {
free(map->palette->data.table.colors);
map->palette->data.table.colors = NULL;
}
free(map->palette);
map->palette = NULL;
}
if (map->auxsound) {
free(map->auxsound);
map->auxsound = NULL;
}
if (map->auxpict) {
free(map->auxpict);
map->auxpict = NULL;
}
map->file = NULL;
map->inited = 0;
free(map);
return bb_err_None;
}
/* Turn a four-byte constant into a string. This returns a static buffer,
so if you call it twice, the old value gets overwritten. */
char *bb_id_to_string(uint32 id)
{
static char buf[5];
buf[0] = (id >> 24) & 0xff;
buf[1] = (id >> 16) & 0xff;
buf[2] = (id >> 8) & 0xff;
buf[3] = (id) & 0xff;
buf[4] = '\0';
return buf;
}
/* Turn an error code into a string describing the error. */
char *bb_err_to_string(bb_err_t err)
{
switch (err) {
case bb_err_None:
return "ok";
case bb_err_CompileTime:
return "library compiled wrong";
case bb_err_Alloc:
return "cannot allocate memory";
case bb_err_Read:
return "cannot read from file";
case bb_err_NotAMap:
return "map structure is bad";
case bb_err_Format:
return "bad format in Blorb file";
case bb_err_NotFound:
return "data not found";
default:
return "unknown error";
}
}
/* This is used for binary searching and quicksorting the resource pointer list. */
static int sortsplot(bb_resdesc_t **p1, bb_resdesc_t **p2)
{
bb_resdesc_t *v1 = *p1;
bb_resdesc_t *v2 = *p2;
if (v1->usage < v2->usage)
return -1;
if (v1->usage > v2->usage)
return 1;
return v1->resnum - v2->resnum;
}
bb_err_t bb_load_chunk_by_type(bb_map_t *map, int method, bb_result_t *res,
uint32 type, int count)
{
int ix;
for (ix=0; ix < map->numchunks; ix++) {
if (map->chunks[ix].type == type) {
if (count == 0)
break;
count--;
}
}
if (ix >= map->numchunks) {
return bb_err_NotFound;
}
return bb_load_chunk_by_number(map, method, res, ix);
}
bb_err_t bb_load_chunk_by_number(bb_map_t *map, int method, bb_result_t *res,
int chunknum)
{
bb_chunkdesc_t *chu;
if (chunknum < 0 || chunknum >= map->numchunks)
return bb_err_NotFound;
chu = &(map->chunks[chunknum]);
switch (method) {
case bb_method_DontLoad:
/* do nothing */
break;
case bb_method_FilePos:
res->data.startpos = chu->datpos;
break;
case bb_method_Memory:
if (!chu->ptr) {
bb_err_t err;
size_t readlen;
void *dat = malloc(chu->len);
if (!dat)
return bb_err_Alloc;
err = fseek(map->file, chu->datpos, 0);
if (err)
return bb_err_Read;
readlen = fread(dat, 1, chu->len, map->file);
if (readlen != chu->len)
return bb_err_Read;
chu->ptr = dat;
}
res->data.ptr = chu->ptr;
break;
}
res->chunknum = chunknum;
res->length = chu->len;
return bb_err_None;
}
bb_err_t bb_load_resource(bb_map_t *map, int method, bb_result_t *res,
uint32 usage, int resnum)
{
bb_resdesc_t sample;
bb_resdesc_t *ptr;
bb_resdesc_t **found;
sample.usage = usage;
sample.resnum = resnum;
ptr = &sample;
found = bsearch(&ptr, map->ressorted, map->numresources, sizeof(bb_resdesc_t *),
(int (*)())&sortsplot);
if (!found)
return bb_err_NotFound;
return bb_load_chunk_by_number(map, method, res, (*found)->chunknum);
}
bb_err_t bb_unload_chunk(bb_map_t *map, int chunknum)
{
bb_chunkdesc_t *chu;
if (chunknum < 0 || chunknum >= map->numchunks)
return bb_err_NotFound;
chu = &(map->chunks[chunknum]);
if (chu->ptr) {
free(chu->ptr);
chu->ptr = NULL;
}
return bb_err_None;
}
bb_err_t bb_count_resources(bb_map_t *map, uint32 usage,
int *num, int *min, int *max)
{
int ix;
int count, minval, maxval, val;
count = 0;
minval = 0;
maxval = 0;
for (ix=0; ix<map->numresources; ix++) {
if (map->resources[ix].usage == usage) {
val = map->resources[ix].resnum;
if (count == 0) {
count++;
minval = val;
maxval = val;
}
else {
count++;
if (val < minval)
minval = val;
if (val > maxval)
maxval = val;
}
}
}
if (num)
*num = count;
if (min)
*min = minval;
if (max)
*max = maxval;
return bb_err_None;
}
uint16 bb_get_release_num(bb_map_t *map)
{
return map->releasenum;
}
bb_zheader_t *bb_get_zheader(bb_map_t *map)
{
return map->zheader;
}
bb_resolution_t *bb_get_resolution(bb_map_t *map)
{
return map->resolution;
}
bb_err_t bb_get_palette(bb_map_t *map, bb_palette_t **res)
{
int ix;
bb_err_t err;
if (res)
*res = NULL;
if (map->palettechunk < 0) {
return bb_err_None;
}
if (!map->palette) {
bb_result_t chunkres;
bb_palette_t *pal;
unsigned char *ptr;
pal = (bb_palette_t *)malloc(sizeof(bb_palette_t));
if (!pal)
return bb_err_Alloc;
err = bb_load_chunk_by_number(map, bb_method_Memory, &chunkres,
map->palettechunk);
if (err)
return err;
ptr = chunkres.data.ptr;
if (chunkres.length == 1) {
int val = ptr[0];
if (val != 16 && val != 32)
return bb_err_Format;
pal->isdirect = TRUE;
pal->data.depth = val;
}
else {
int size = chunkres.length / 3;
bb_color_t *colors = (bb_color_t *)malloc(size * sizeof(bb_color_t));
if (!colors)
return bb_err_Alloc;
if (size < 1 || size > 256)
return bb_err_Format;
for (ix=0; ix<size; ix++) {
colors[ix].red = ptr[ix*3+0];
colors[ix].green = ptr[ix*3+1];
colors[ix].blue = ptr[ix*3+2];
}
pal->isdirect = FALSE;
pal->data.table.numcolors = size;
pal->data.table.colors = colors;
}
bb_unload_chunk(map, map->palettechunk);
map->palette = pal;
}
if (res)
*res = map->palette;
return bb_err_None;
}
bb_err_t bb_load_resource_pict(bb_map_t *map, int method, bb_result_t *res,
int resnum, bb_aux_pict_t **auxdata)
{
bb_err_t err;
if (auxdata)
*auxdata = NULL;
err = bb_load_resource(map, method, res, bb_ID_Pict, resnum);
if (err)
return err;
if (auxdata) {
bb_chunkdesc_t *chu = &(map->chunks[res->chunknum]);
if (chu->auxdatnum >= 0 && map->auxpict) {
*auxdata = &(map->auxpict[chu->auxdatnum]);
}
}
return bb_err_None;
}
bb_err_t bb_load_resource_snd(bb_map_t *map, int method, bb_result_t *res,
int resnum, bb_aux_sound_t **auxdata)
{
bb_err_t err;
if (auxdata)
*auxdata = NULL;
err = bb_load_resource(map, method, res, bb_ID_Pict, resnum);
if (err)
return err;
if (auxdata) {
bb_chunkdesc_t *chu = &(map->chunks[res->chunknum]);
if (chu->auxdatnum >= 0 && map->auxsound) {
*auxdata = &(map->auxsound[chu->auxdatnum]);
}
}
return bb_err_None;
}