| 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 |
#define FORMAT blorb |
| 28 |
#define HOME_PAGE "http://eblong.com/zarf/blorb" |
| 29 |
#define FORMAT_EXT ".blorb,.blb,.zblorb,.zlb,.gblorb,.glb" |
| 30 |
#define CONTAINER_FORMAT |
| 31 |
#include "treaty_builder.h" |
| 32 |
#include <stdlib.h> |
| 33 |
#include <ctype.h> |
| 34 |
|
| 35 |
extern TREATY treaty_registry[]; |
| 36 |
|
| 37 |
|
| 38 |
static char *TranslateExec[] = { |
| 39 |
"ZCOD", "zcode", |
| 40 |
"GLUL", "glulx", |
| 41 |
"TAD2", "tads2", |
| 42 |
"TAD3", "tads3", |
| 43 |
NULL, NULL |
| 44 |
}; |
| 45 |
|
| 46 |
void *my_malloc(int32, char *); |
| 47 |
int32 ifiction_get_IFID(char *, char *, int32); |
| 48 |
|
| 49 |
static uint32 read_int(void *inp) |
| 50 |
{ |
| 51 |
unsigned char *mem=(unsigned char *)inp; |
| 52 |
uint32 i4 = mem[0], |
| 53 |
i3 = mem[1], |
| 54 |
i2 = mem[2], |
| 55 |
i1 = mem[3]; |
| 56 |
return i1 | (i2<<8) | (i3<<16) | (i4<<24); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static int32 blorb_get_chunk(void *blorb_file, int32 extent, char *id, uint32 *begin, uint32 *output_extent) |
| 61 |
{ |
| 62 |
uint32 i=12, j; |
| 63 |
while(i<(uint32) extent-8) |
| 64 |
{ |
| 65 |
if (memcmp(((char *)blorb_file)+i,id,4)==0) |
| 66 |
{ |
| 67 |
*output_extent=read_int((char *)blorb_file+i+4); |
| 68 |
if (*output_extent > (uint32) extent) return NO_REPLY_RV; |
| 69 |
*begin=i+8; |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
j=read_int((char *)blorb_file+i+4); |
| 74 |
if (j%2) j++; |
| 75 |
i+=j+8; |
| 76 |
|
| 77 |
} |
| 78 |
return NO_REPLY_RV; |
| 79 |
} |
| 80 |
static int32 blorb_get_resource(void *blorb_file, int32 extent, char *rid, int32 nnumber, uint32 *begin, uint32 *output_extent) |
| 81 |
{ |
| 82 |
uint32 ridx_len; |
| 83 |
uint32 i,j; |
| 84 |
uint32 number=(uint32) nnumber; |
| 85 |
void *ridx; |
| 86 |
if (blorb_get_chunk(blorb_file, extent,"RIdx",&i,&ridx_len)==NO_REPLY_RV) |
| 87 |
return NO_REPLY_RV; |
| 88 |
|
| 89 |
ridx=(char *)blorb_file+i+4; |
| 90 |
ridx_len=read_int((char *)blorb_file+i); |
| 91 |
for(i=0;i<ridx_len;i++) |
| 92 |
{ |
| 93 |
if (memcmp((char *)ridx+(i*12),rid,4)==0 && read_int((char *)ridx+(i*12)+4)==number) |
| 94 |
{ |
| 95 |
j=i; |
| 96 |
i=read_int((char *)ridx+(j*12)+8); |
| 97 |
*begin=i+8; |
| 98 |
*output_extent=read_int((char *)blorb_file+i+4); |
| 99 |
if (*begin > extent || *begin + *output_extent > extent) |
| 100 |
return NO_REPLY_RV; |
| 101 |
return 1; |
| 102 |
} |
| 103 |
} |
| 104 |
return NO_REPLY_RV; |
| 105 |
} |
| 106 |
|
| 107 |
#if 0 |
| 108 |
static int32 blorb_get_story_file(void *blorb_file, int32 extent, uint32 *begin, uint32 *output_extent) |
| 109 |
{ |
| 110 |
return blorb_get_resource(blorb_file, extent, "Exec", 0, begin, output_extent); |
| 111 |
|
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
static int32 get_story_extent(void *blorb_file, int32 extent) |
| 116 |
{ |
| 117 |
uint32 i,j; |
| 118 |
if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j)) |
| 119 |
{ |
| 120 |
return j; |
| 121 |
} |
| 122 |
return NO_REPLY_RV; |
| 123 |
|
| 124 |
} |
| 125 |
static int32 get_story_file(void *blorb_file, int32 extent, void *output, int32 output_extent) |
| 126 |
{ |
| 127 |
uint32 i,j; |
| 128 |
if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j)) |
| 129 |
{ |
| 130 |
ASSERT_OUTPUT_SIZE((int32) j); |
| 131 |
memcpy(output,(char *)blorb_file+i,j); |
| 132 |
return j; |
| 133 |
} |
| 134 |
return NO_REPLY_RV; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
char *blorb_chunk_for_name(char *name) |
| 139 |
{ |
| 140 |
static char buffer[5]; |
| 141 |
int j; |
| 142 |
for(j=0;TranslateExec[j];j+=2) |
| 143 |
if (strcmp(name,TranslateExec[j+1])==0) return TranslateExec[j]; |
| 144 |
for(j=0;j<4 && name[j];j++) buffer[j]=toupper(buffer[j]); |
| 145 |
while(j<4) buffer[j++]=' '; |
| 146 |
buffer[4]=0; |
| 147 |
return buffer; |
| 148 |
|
| 149 |
} |
| 150 |
static char *blorb_get_story_format(void *blorb_file, int32 extent) |
| 151 |
{ |
| 152 |
uint32 i, j; |
| 153 |
|
| 154 |
for(j=0;treaty_registry[j];j++) |
| 155 |
{ |
| 156 |
static char fn[512]; |
| 157 |
treaty_registry[j](GET_FORMAT_NAME_SEL,NULL,0,fn,512); |
| 158 |
if (blorb_get_chunk(blorb_file,extent,blorb_chunk_for_name(fn),&i, &i)) return fn; |
| 159 |
} |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
static int32 get_story_format(void *blorb_file, int32 extent, char *output, int32 output_extent) |
| 164 |
{ |
| 165 |
char *o; |
| 166 |
o=blorb_get_story_format(blorb_file, extent); |
| 167 |
if (!o) return NO_REPLY_RV; |
| 168 |
ASSERT_OUTPUT_SIZE((signed) strlen(o)+1); |
| 169 |
strcpy(output,o); |
| 170 |
return strlen(o)+1; |
| 171 |
} |
| 172 |
static int32 get_story_file_metadata_extent(void *blorb_file, int32 extent) |
| 173 |
{ |
| 174 |
uint32 i,j; |
| 175 |
if (blorb_get_chunk(blorb_file,extent,"IFmd",&i,&j)) return j+1; |
| 176 |
return NO_REPLY_RV; |
| 177 |
} |
| 178 |
static int32 blorb_get_cover(void *blorb_file, int32 extent, uint32 *begin, uint32 *output_extent) |
| 179 |
{ |
| 180 |
uint32 i,j; |
| 181 |
if (blorb_get_chunk(blorb_file,extent,"Fspc",&i,&j)) |
| 182 |
{ |
| 183 |
if (j<4) return NO_REPLY_RV; |
| 184 |
i=read_int((char *)blorb_file+i); |
| 185 |
if (!blorb_get_resource(blorb_file,extent,"Pict",i,&i,&j)) return NO_REPLY_RV; |
| 186 |
*begin=i; |
| 187 |
*output_extent=j; |
| 188 |
if (memcmp((char *)blorb_file+i-8,"PNG ",4)==0) return PNG_COVER_FORMAT; |
| 189 |
else if (memcmp((char *)blorb_file+i-8,"JPEG",4)==0) return JPEG_COVER_FORMAT; |
| 190 |
} |
| 191 |
return NO_REPLY_RV; |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
static int32 get_story_file_cover_extent(void *blorb_file, int32 extent) |
| 196 |
{ |
| 197 |
uint32 i,j; |
| 198 |
if (blorb_get_cover(blorb_file,extent,&i,&j)) return j; |
| 199 |
return NO_REPLY_RV; |
| 200 |
} |
| 201 |
|
| 202 |
static int32 get_story_file_cover_format(void *blorb_file, int32 extent) |
| 203 |
{ |
| 204 |
uint32 i,j; |
| 205 |
return blorb_get_cover(blorb_file, extent, &i,&j); |
| 206 |
} |
| 207 |
|
| 208 |
static int32 get_story_file_IFID(void *b, int32 e, char *output, int32 output_extent) |
| 209 |
{ |
| 210 |
int32 j; |
| 211 |
char *md; |
| 212 |
j=get_story_file_metadata_extent(b,e); |
| 213 |
if (j<=0) return NO_REPLY_RV; |
| 214 |
md=(char *)my_malloc(j, "Metadata buffer"); |
| 215 |
j=get_story_file_metadata(b,e,md,j); |
| 216 |
if (j<=0) return NO_REPLY_RV; |
| 217 |
|
| 218 |
j=ifiction_get_IFID(md,output,output_extent); |
| 219 |
free(md); |
| 220 |
return j; |
| 221 |
} |
| 222 |
|
| 223 |
static int32 get_story_file_metadata(void *blorb_file, int32 extent, char *output, int32 output_extent) |
| 224 |
{ |
| 225 |
uint32 i,j; |
| 226 |
if (!blorb_get_chunk(blorb_file, extent,"IFmd",&i,&j)) return NO_REPLY_RV; |
| 227 |
ASSERT_OUTPUT_SIZE((int32) j+1); |
| 228 |
memcpy(output,(char *)blorb_file+i,j); |
| 229 |
output[j]=0; |
| 230 |
return j+1; |
| 231 |
} |
| 232 |
static int32 get_story_file_cover(void *blorb_file, int32 extent, void *output, int32 output_extent) |
| 233 |
{ |
| 234 |
uint32 i,j; |
| 235 |
if (!blorb_get_cover(blorb_file, extent,&i,&j)) return NO_REPLY_RV; |
| 236 |
ASSERT_OUTPUT_SIZE((int32) j); |
| 237 |
memcpy(output,(char *)blorb_file+i,j); |
| 238 |
return j; |
| 239 |
} |
| 240 |
|
| 241 |
static int32 claim_story_file(void *story_file, int32 extent) |
| 242 |
{ |
| 243 |
int i; |
| 244 |
|
| 245 |
if (extent<16 || |
| 246 |
memcmp(story_file,"FORM",4) || |
| 247 |
memcmp((char *)story_file+8,"IFRS",4) |
| 248 |
) i= INVALID_STORY_FILE_RV; |
| 249 |
else i= NO_REPLY_RV; |
| 250 |
|
| 251 |
return i; |
| 252 |
|
| 253 |
} |