| 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 |
#include "treaty.h" |
| 52 |
#include <stdlib.h> |
| 53 |
#include <string.h> |
| 54 |
#include <stdio.h> |
| 55 |
#include <ctype.h> |
| 56 |
#include "md5.h" |
| 57 |
|
| 58 |
void *my_malloc(int, char *); |
| 59 |
|
| 60 |
struct babel_handler |
| 61 |
{ |
| 62 |
TREATY treaty_handler; |
| 63 |
TREATY treaty_backup; |
| 64 |
void *story_file; |
| 65 |
uint32 story_file_extent; |
| 66 |
void *story_file_blorbed; |
| 67 |
uint32 story_file_blorbed_extent; |
| 68 |
char blorb_mode; |
| 69 |
char *format_name; |
| 70 |
char auth; |
| 71 |
}; |
| 72 |
|
| 73 |
static struct babel_handler default_ctx; |
| 74 |
|
| 75 |
extern TREATY treaty_registry[]; |
| 76 |
extern TREATY container_registry[]; |
| 77 |
|
| 78 |
static char *deeper_babel_init(char *story_name, void *bhp) |
| 79 |
{ |
| 80 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 81 |
int i; |
| 82 |
char *ext; |
| 83 |
|
| 84 |
static char buffer[TREATY_MINIMUM_EXTENT]; |
| 85 |
int best_candidate; |
| 86 |
char buffert[TREATY_MINIMUM_EXTENT]; |
| 87 |
|
| 88 |
if (story_name) |
| 89 |
{ |
| 90 |
ext=strrchr(story_name,'.'); |
| 91 |
if (ext) for(i=0;ext[i];i++) ext[i]=tolower(ext[i]); |
| 92 |
} |
| 93 |
else ext=NULL; |
| 94 |
best_candidate=-1; |
| 95 |
if (ext) |
| 96 |
for(i=0;container_registry[i];i++) |
| 97 |
if (container_registry[i](GET_FILE_EXTENSIONS_SEL,NULL,0,buffer,TREATY_MINIMUM_EXTENT) >=0 && |
| 98 |
strstr(buffer,ext) && |
| 99 |
container_registry[i](CLAIM_STORY_FILE_SEL,bh->story_file,bh->story_file_extent,NULL,0)>=NO_REPLY_RV) |
| 100 |
break; |
| 101 |
if (!ext || !container_registry[i]) |
| 102 |
{ |
| 103 |
|
| 104 |
for(i=0;container_registry[i];i++) |
| 105 |
{int l=container_registry[i](CLAIM_STORY_FILE_SEL,bh->story_file,bh->story_file_extent,NULL,0); |
| 106 |
|
| 107 |
if (l==VALID_STORY_FILE_RV) |
| 108 |
break; |
| 109 |
else if (l==NO_REPLY_RV && best_candidate < 0) best_candidate=i; |
| 110 |
} |
| 111 |
} |
| 112 |
if (!container_registry[i] && best_candidate >=0) { bh->auth=0; i=best_candidate; } |
| 113 |
if (container_registry[i]) |
| 114 |
{ |
| 115 |
char buffer2[TREATY_MINIMUM_EXTENT]; |
| 116 |
|
| 117 |
bh->treaty_handler=container_registry[i]; |
| 118 |
container_registry[i](GET_FORMAT_NAME_SEL,NULL,0,buffert,TREATY_MINIMUM_EXTENT); |
| 119 |
bh->blorb_mode=1; |
| 120 |
|
| 121 |
bh->story_file_blorbed_extent=container_registry[i](CONTAINER_GET_STORY_EXTENT_SEL,bh->story_file,bh->story_file_extent,NULL,0); |
| 122 |
if (bh->story_file_blorbed_extent>0) bh->story_file_blorbed=my_malloc(bh->story_file_blorbed_extent, "contained story file"); |
| 123 |
if (bh->story_file_blorbed_extent<=0 || |
| 124 |
container_registry[i](CONTAINER_GET_STORY_FORMAT_SEL,bh->story_file,bh->story_file_extent,buffer2,TREATY_MINIMUM_EXTENT)<0 || |
| 125 |
container_registry[i](CONTAINER_GET_STORY_FILE_SEL,bh->story_file,bh->story_file_extent,bh->story_file_blorbed,bh->story_file_blorbed_extent)<=0 |
| 126 |
) |
| 127 |
return NULL; |
| 128 |
|
| 129 |
for(i=0;treaty_registry[i];i++) |
| 130 |
if (treaty_registry[i](GET_FORMAT_NAME_SEL,NULL,0,buffer,TREATY_MINIMUM_EXTENT)>=0 && |
| 131 |
strcmp(buffer,buffer2)==0 && |
| 132 |
treaty_registry[i](CLAIM_STORY_FILE_SEL,bh->story_file_blorbed,bh->story_file_blorbed_extent,NULL,0)>=NO_REPLY_RV) |
| 133 |
break; |
| 134 |
if (!treaty_registry[i]) |
| 135 |
return NULL; |
| 136 |
bh->treaty_backup=treaty_registry[i]; |
| 137 |
snprintf(buffer,sizeof buffer,"%sed %s",buffert,buffer2); |
| 138 |
return buffer; |
| 139 |
} |
| 140 |
|
| 141 |
bh->blorb_mode=0; |
| 142 |
best_candidate=-1; |
| 143 |
|
| 144 |
if (ext) |
| 145 |
for(i=0;treaty_registry[i];i++) |
| 146 |
if (treaty_registry[i](GET_FILE_EXTENSIONS_SEL,NULL,0,buffer,TREATY_MINIMUM_EXTENT) >=0 && |
| 147 |
strstr(buffer,ext) && |
| 148 |
treaty_registry[i](CLAIM_STORY_FILE_SEL,bh->story_file,bh->story_file_extent,NULL,0)>=NO_REPLY_RV) |
| 149 |
break; |
| 150 |
if (!ext || !treaty_registry[i]) |
| 151 |
{ |
| 152 |
|
| 153 |
for(i=0;treaty_registry[i];i++) |
| 154 |
{int l; |
| 155 |
l=treaty_registry[i](CLAIM_STORY_FILE_SEL,bh->story_file,bh->story_file_extent,NULL,0); |
| 156 |
|
| 157 |
if (l==VALID_STORY_FILE_RV) |
| 158 |
break; |
| 159 |
else if (l==NO_REPLY_RV && best_candidate < 0) best_candidate=i; |
| 160 |
} |
| 161 |
} |
| 162 |
if (!treaty_registry[i]) { |
| 163 |
if (best_candidate>0) { i=best_candidate; bh->auth=0; } |
| 164 |
else return NULL; |
| 165 |
} |
| 166 |
bh->treaty_handler=treaty_registry[i]; |
| 167 |
|
| 168 |
if (bh->treaty_handler(GET_FORMAT_NAME_SEL,NULL,0,buffer,TREATY_MINIMUM_EXTENT)>=0) |
| 169 |
return buffer; |
| 170 |
return NULL; |
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
static char *deep_babel_init(char *story_name, void *bhp) |
| 176 |
{ |
| 177 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 178 |
FILE *file; |
| 179 |
|
| 180 |
bh->treaty_handler=NULL; |
| 181 |
bh->treaty_backup=NULL; |
| 182 |
bh->story_file=NULL; |
| 183 |
bh->story_file_extent=0; |
| 184 |
bh->story_file_blorbed=NULL; |
| 185 |
bh->story_file_blorbed_extent=0; |
| 186 |
bh->format_name=NULL; |
| 187 |
file=fopen(story_name, "rb"); |
| 188 |
if (!file) return NULL; |
| 189 |
fseek(file,0,SEEK_END); |
| 190 |
bh->story_file_extent=ftell(file); |
| 191 |
fseek(file,0,SEEK_SET); |
| 192 |
bh->auth=1; |
| 193 |
bh->story_file=my_malloc(bh->story_file_extent,"story file storage"); |
| 194 |
fread(bh->story_file,1,bh->story_file_extent,file); |
| 195 |
fclose(file); |
| 196 |
|
| 197 |
return deeper_babel_init(story_name, bhp); |
| 198 |
} |
| 199 |
|
| 200 |
char *babel_init_ctx(char *sf, void *bhp) |
| 201 |
{ |
| 202 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 203 |
char *b; |
| 204 |
b=deep_babel_init(sf,bh); |
| 205 |
if (b) bh->format_name=strdup(b); |
| 206 |
return b; |
| 207 |
} |
| 208 |
char *babel_init(char *sf) |
| 209 |
{ |
| 210 |
return babel_init_ctx(sf, &default_ctx); |
| 211 |
} |
| 212 |
|
| 213 |
char *babel_init_raw_ctx(void *sf, int32 extent, void *bhp) |
| 214 |
{ |
| 215 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 216 |
char *b; |
| 217 |
bh->treaty_handler=NULL; |
| 218 |
bh->treaty_backup=NULL; |
| 219 |
bh->story_file=NULL; |
| 220 |
bh->story_file_extent=0; |
| 221 |
bh->story_file_blorbed=NULL; |
| 222 |
bh->story_file_blorbed_extent=0; |
| 223 |
bh->format_name=NULL; |
| 224 |
bh->story_file_extent=extent; |
| 225 |
bh->auth=1; |
| 226 |
bh->story_file=my_malloc(bh->story_file_extent,"story file storage"); |
| 227 |
memcpy(bh->story_file,sf,extent); |
| 228 |
|
| 229 |
b=deeper_babel_init(NULL, bhp); |
| 230 |
if (b) bh->format_name=strdup(b); |
| 231 |
return b; |
| 232 |
} |
| 233 |
char *babel_init_raw(void *sf, int32 extent) |
| 234 |
{ |
| 235 |
return babel_init_raw_ctx(sf, extent, &default_ctx); |
| 236 |
} |
| 237 |
|
| 238 |
void babel_release_ctx(void *bhp) |
| 239 |
{ |
| 240 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 241 |
if (bh->story_file) free(bh->story_file); |
| 242 |
bh->story_file=NULL; |
| 243 |
if (bh->story_file_blorbed) free(bh->story_file_blorbed); |
| 244 |
bh->story_file_blorbed=NULL; |
| 245 |
if (bh->format_name) free(bh->format_name); |
| 246 |
bh->format_name=NULL; |
| 247 |
} |
| 248 |
void babel_release() |
| 249 |
{ |
| 250 |
babel_release_ctx(&default_ctx); |
| 251 |
} |
| 252 |
int32 babel_md5_ifid_ctx(char *buffer, int32 extent, void *bhp) |
| 253 |
{ |
| 254 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 255 |
md5_state_t md5; |
| 256 |
int i; |
| 257 |
unsigned char ob[16]; |
| 258 |
if (extent <33 || bh->story_file==NULL) |
| 259 |
return 0; |
| 260 |
md5_init(&md5); |
| 261 |
md5_append(&md5,bh->story_file,bh->story_file_extent); |
| 262 |
md5_finish(&md5,ob); |
| 263 |
for(i=0;i<16;i++) |
| 264 |
sprintf(buffer+(2*i),"%02X",ob[i]); |
| 265 |
buffer[32]=0; |
| 266 |
return 1; |
| 267 |
|
| 268 |
} |
| 269 |
int32 babel_md5_ifid(char *buffer, int32 extent) |
| 270 |
{ |
| 271 |
return babel_md5_ifid_ctx(buffer, extent, |
| 272 |
&default_ctx); |
| 273 |
} |
| 274 |
|
| 275 |
int32 babel_treaty_ctx(int32 sel, void *output, int32 output_extent,void *bhp) |
| 276 |
{ |
| 277 |
int32 rv; |
| 278 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 279 |
if (!(sel & TREATY_SELECTOR_INPUT) && bh->blorb_mode) |
| 280 |
rv=bh->treaty_backup(sel,bh->story_file_blorbed,bh->story_file_blorbed_extent,output, output_extent); |
| 281 |
else |
| 282 |
{ |
| 283 |
rv=bh->treaty_handler(sel,bh->story_file,bh->story_file_extent,output,output_extent); |
| 284 |
if ((!rv|| rv==UNAVAILABLE_RV) && bh->blorb_mode) |
| 285 |
rv=bh->treaty_backup(sel,bh->story_file_blorbed,bh->story_file_blorbed_extent,output, output_extent); |
| 286 |
} |
| 287 |
if (!rv && sel==GET_STORY_FILE_IFID_SEL) |
| 288 |
return babel_md5_ifid_ctx(output,output_extent, bh); |
| 289 |
if (rv==INCOMPLETE_REPLY_RV && sel==GET_STORY_FILE_IFID_SEL) |
| 290 |
return babel_md5_ifid_ctx((void *)((char *) output+strlen((char *)output)), |
| 291 |
output_extent-strlen((char *)output), |
| 292 |
bh); |
| 293 |
|
| 294 |
return rv; |
| 295 |
} |
| 296 |
int32 babel_treaty(int32 sel, void *output, int32 output_extent) |
| 297 |
{ |
| 298 |
return babel_treaty_ctx(sel, output, output_extent, &default_ctx); |
| 299 |
} |
| 300 |
char *babel_get_format_ctx(void *bhp) |
| 301 |
{ |
| 302 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 303 |
return bh->format_name; |
| 304 |
} |
| 305 |
char *babel_get_format() |
| 306 |
{ |
| 307 |
return babel_get_format_ctx(&default_ctx); |
| 308 |
} |
| 309 |
void *get_babel_ctx() |
| 310 |
{ |
| 311 |
return my_malloc(sizeof(struct babel_handler), "babel handler context"); |
| 312 |
} |
| 313 |
void release_babel_ctx(void *b) |
| 314 |
{ |
| 315 |
free(b); |
| 316 |
} |
| 317 |
|
| 318 |
uint32 babel_get_length_ctx(void *bhp) |
| 319 |
{ |
| 320 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 321 |
return bh->story_file_extent; |
| 322 |
} |
| 323 |
uint32 babel_get_length() |
| 324 |
{ |
| 325 |
return babel_get_length_ctx(&default_ctx); |
| 326 |
} |
| 327 |
|
| 328 |
int32 babel_get_authoritative_ctx(void *bhp) |
| 329 |
{ |
| 330 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 331 |
return bh->auth; |
| 332 |
} |
| 333 |
int32 babel_get_authoritative() |
| 334 |
{ |
| 335 |
return babel_get_authoritative_ctx(&default_ctx); |
| 336 |
} |
| 337 |
void *babel_get_file_ctx(void *bhp) |
| 338 |
{ |
| 339 |
struct babel_handler *bh=(struct babel_handler *) bhp; |
| 340 |
return bh->story_file; |
| 341 |
} |
| 342 |
void *babel_get_file() |
| 343 |
{ |
| 344 |
return babel_get_file_ctx(&default_ctx); |
| 345 |
} |
| 346 |
|
| 347 |
uint32 babel_get_story_length_ctx(void *ctx) |
| 348 |
{ |
| 349 |
struct babel_handler *bh=(struct babel_handler *) ctx; |
| 350 |
if (bh->blorb_mode) return bh->story_file_blorbed_extent; |
| 351 |
return bh->story_file_extent; |
| 352 |
} |
| 353 |
uint32 babel_get_story_length() |
| 354 |
{ |
| 355 |
|
| 356 |
return babel_get_story_length_ctx(&default_ctx); |
| 357 |
} |
| 358 |
void *babel_get_story_file_ctx(void *ctx) |
| 359 |
{ |
| 360 |
struct babel_handler *bh=(struct babel_handler *) ctx; |
| 361 |
if (bh->blorb_mode) return bh->story_file_blorbed; |
| 362 |
return bh->story_file; |
| 363 |
} |
| 364 |
void *babel_get_story_file() |
| 365 |
{ |
| 366 |
return babel_get_story_file_ctx(&default_ctx); |
| 367 |
} |