| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <ctype.h> |
| 13 |
#include <errno.h> |
| 14 |
#include "wad_config.h" |
| 15 |
#include "wad.h" |
| 16 |
#include "waderrno.h" |
| 17 |
|
| 18 |
#ifndef min |
| 19 |
#define min(x,y) ((x) < (y) ? (x) : (y)) |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifndef max |
| 23 |
#define max(x,y) ((x) > (y) ? (x) : (y)) |
| 24 |
#endif |
| 25 |
|
| 26 |
|
| 27 |
#define CBUF_LEN 16384 |
| 28 |
static unsigned char cbuf[CBUF_LEN]; |
| 29 |
|
| 30 |
|
| 31 |
extern int waderrno; |
| 32 |
#ifndef __MINGW32__ |
| 33 |
|
| 34 |
extern int errno; |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static wad_t* WAD_Init() |
| 43 |
{ |
| 44 |
wad_t *out = (wad_t*)WAD_MALLOC(sizeof(wad_t)); |
| 45 |
|
| 46 |
out->type = WI_UNKNOWN; |
| 47 |
|
| 48 |
out->header.type = WADTYPE_PWAD; |
| 49 |
out->header.entry_count = 0; |
| 50 |
out->header.entry_list_offset = 12; |
| 51 |
|
| 52 |
|
| 53 |
out->handle.buffer = NULL; |
| 54 |
out->buffer_size = 0; |
| 55 |
out->buffer_capacity = 0; |
| 56 |
|
| 57 |
|
| 58 |
out->entries = NULL; |
| 59 |
out->entries_capacity = 0; |
| 60 |
|
| 61 |
return out; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static int WAD_SetupOpenFileHandle(FILE *fp, wadheader_t *header) |
| 66 |
{ |
| 67 |
int buf; |
| 68 |
|
| 69 |
if (fseek(fp, 0, SEEK_SET)) |
| 70 |
{ |
| 71 |
fclose(fp); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
buf = fread(header, 1, sizeof(wadheader_t), fp); |
| 76 |
|
| 77 |
|
| 78 |
if (buf < sizeof(wadheader_t)) |
| 79 |
{ |
| 80 |
fclose(fp); |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
if (WADTYPE_PWAD != header->type && WADTYPE_IWAD != header->type) |
| 86 |
{ |
| 87 |
fclose(fp); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static int WAD_ExpandEntrylist(wad_t *wad, int newsize) |
| 96 |
{ |
| 97 |
int i; |
| 98 |
wadentry_t **oldarray; |
| 99 |
int oldsize = wad->entries_capacity; |
| 100 |
newsize = max(newsize, 1); |
| 101 |
|
| 102 |
if (newsize <= oldsize) |
| 103 |
return 0; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
oldarray = wad->entries; |
| 108 |
wad->entries = (wadentry_t**)WAD_MALLOC((sizeof (wadentry_t*)) * newsize); |
| 109 |
|
| 110 |
|
| 111 |
if (!(wad->entries)) |
| 112 |
{ |
| 113 |
wad->entries = oldarray; |
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
if (oldarray) |
| 118 |
{ |
| 119 |
|
| 120 |
memcpy(wad->entries, oldarray, (sizeof (wadentry_t*)) * oldsize); |
| 121 |
WAD_FREE(oldarray); |
| 122 |
} |
| 123 |
|
| 124 |
for (i = oldsize; i < newsize; i++) |
| 125 |
{ |
| 126 |
wad->entries[i] = (wadentry_t*)WAD_MALLOC(sizeof (wadentry_t)); |
| 127 |
|
| 128 |
if (!wad->entries[i]) |
| 129 |
return 1; |
| 130 |
} |
| 131 |
wad->entries_capacity = i; |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
static int WAD_ExpandBuffer(wad_t *wad, int newsize) |
| 138 |
{ |
| 139 |
unsigned char *oldarray; |
| 140 |
int oldsize = wad->buffer_capacity; |
| 141 |
|
| 142 |
if (newsize <= oldsize) |
| 143 |
return 0; |
| 144 |
|
| 145 |
|
| 146 |
oldarray = wad->handle.buffer; |
| 147 |
wad->handle.buffer = (unsigned char *)WAD_MALLOC((sizeof (unsigned char)) * newsize); |
| 148 |
if (!(wad->handle.buffer)) |
| 149 |
{ |
| 150 |
wad->handle.buffer = oldarray; |
| 151 |
return 1; |
| 152 |
} |
| 153 |
|
| 154 |
if (oldarray) |
| 155 |
{ |
| 156 |
memcpy(wad->handle.buffer, oldarray, (sizeof (unsigned char)) * oldsize); |
| 157 |
WAD_FREE(oldarray); |
| 158 |
} |
| 159 |
|
| 160 |
wad->buffer_capacity = newsize; |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
static int WAD_SetupBuildEntrylist(FILE *fp, wad_t *wad) |
| 168 |
{ |
| 169 |
int count = WAD_EntryCount(wad); |
| 170 |
|
| 171 |
if (WAD_ExpandEntrylist(wad, count)) |
| 172 |
return 1; |
| 173 |
|
| 174 |
|
| 175 |
if (fseek(fp, wad->header.entry_list_offset, SEEK_SET)) |
| 176 |
return 1; |
| 177 |
|
| 178 |
for (int i = 0; i < count; i++) |
| 179 |
if (!fread(wad->entries[i], sizeof(wadentry_t), 1, fp)) |
| 180 |
return 1; |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
static int WAD_SetupBuildBuffer(FILE *fp, wad_t *wad) |
| 187 |
{ |
| 188 |
int len = (wad->header.entry_list_offset) - sizeof(wadheader_t); |
| 189 |
int remain = len; |
| 190 |
int count; |
| 191 |
|
| 192 |
if (WAD_ExpandBuffer(wad, len)) |
| 193 |
return 1; |
| 194 |
|
| 195 |
unsigned char *bufptr = wad->handle.buffer; |
| 196 |
|
| 197 |
|
| 198 |
if (fseek(fp, sizeof(wadheader_t), SEEK_SET)) |
| 199 |
return 1; |
| 200 |
|
| 201 |
while (remain) |
| 202 |
{ |
| 203 |
count = min(CBUF_LEN, remain); |
| 204 |
if (!fread(bufptr, 1, count, fp)) |
| 205 |
return 1; |
| 206 |
remain -= count; |
| 207 |
bufptr += count; |
| 208 |
} |
| 209 |
|
| 210 |
wad->buffer_size = len; |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
static void WAD_FreeAllocated(wad_t *wad) |
| 217 |
{ |
| 218 |
int i; |
| 219 |
for (i = 0; i < wad->entries_capacity; i++) |
| 220 |
{ |
| 221 |
WAD_FREE(wad->entries[i]); |
| 222 |
} |
| 223 |
WAD_FREE(wad->entries); |
| 224 |
WAD_FREE(wad); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
static int WAD_EntryNameCopy(const char *src, char *dest) |
| 229 |
{ |
| 230 |
int i = 0; |
| 231 |
while (i < 8) |
| 232 |
{ |
| 233 |
char c = src[i]; |
| 234 |
if (!c) |
| 235 |
break; |
| 236 |
|
| 237 |
|
| 238 |
if (c > 0x20 && c < 0x7F) |
| 239 |
dest[i] = (char)toupper(c); |
| 240 |
else |
| 241 |
dest[i] = '_'; |
| 242 |
|
| 243 |
i++; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if (i < 8) |
| 248 |
dest[i] = '\0'; |
| 249 |
|
| 250 |
return i; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
static wadentry_t* WAD_AddEntryCommon(wad_t *wad, const char *name, int32_t length, int32_t offset, int index) |
| 255 |
{ |
| 256 |
index = min(wad->header.entry_count, index); |
| 257 |
|
| 258 |
if (wad->header.entry_count + 1 >= wad->entries_capacity) |
| 259 |
if (WAD_ExpandEntrylist(wad, wad->entries_capacity * 2)) |
| 260 |
return NULL; |
| 261 |
|
| 262 |
|
| 263 |
wadentry_t *newentry = wad->entries[wad->header.entry_count]; |
| 264 |
WAD_EntryNameCopy(name, newentry->name); |
| 265 |
newentry->length = length; |
| 266 |
newentry->offset = offset; |
| 267 |
|
| 268 |
int i = wad->header.entry_count; |
| 269 |
while (i > index) |
| 270 |
{ |
| 271 |
wadentry_t *e = wad->entries[i - 1]; |
| 272 |
wad->entries[i - 1] = wad->entries[i]; |
| 273 |
wad->entries[i] = e; |
| 274 |
i--; |
| 275 |
} |
| 276 |
|
| 277 |
wad->header.entry_count++; |
| 278 |
return newentry; |
| 279 |
} |
| 280 |
|
| 281 |
static int entrycmpr(const void *a, const void *b) |
| 282 |
{ |
| 283 |
wadentry_t *x = *(wadentry_t**)a; |
| 284 |
wadentry_t *y = *(wadentry_t**)b; |
| 285 |
int i = x->length < 0 && !x->name[0] ? 1 : 0; |
| 286 |
int j = y->length < 0 && !y->name[0] ? 1 : 0; |
| 287 |
return i - j; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
static int WAD_RemoveEntriesCommon(wad_t *wad, int *indices, int count) |
| 292 |
{ |
| 293 |
int i, index; |
| 294 |
for (i = 0; i < count; i++) |
| 295 |
{ |
| 296 |
index = indices[i]; |
| 297 |
if (index < 0 || index >= wad->header.entry_count) |
| 298 |
{ |
| 299 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 300 |
return 1; |
| 301 |
} |
| 302 |
wadentry_t *entry = wad->entries[index]; |
| 303 |
entry->length = -1; |
| 304 |
entry->name[0] = '\0'; |
| 305 |
} |
| 306 |
|
| 307 |
qsort(wad->entries, wad->header.entry_count, sizeof(wadentry_t*), entrycmpr); |
| 308 |
wad->header.entry_count = wad->header.entry_count - count; |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
static int WAD_RemoveEntryRangeCommon(wad_t *wad, int start, int count) |
| 314 |
{ |
| 315 |
if (start < 0 || start >= wad->header.entry_count) |
| 316 |
{ |
| 317 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
if (count < 0) |
| 322 |
{ |
| 323 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 324 |
return 1; |
| 325 |
} |
| 326 |
|
| 327 |
int end = min(start + count, wad->header.entry_count); |
| 328 |
|
| 329 |
wadentry_t **e = (wadentry_t**)WAD_MALLOC(sizeof(wadentry_t*) * count); |
| 330 |
if (!e) |
| 331 |
return 1; |
| 332 |
|
| 333 |
|
| 334 |
size_t size = sizeof(wadentry_t*) * count; |
| 335 |
size_t endsize = sizeof(wadentry_t*) * (wad->header.entry_count - end); |
| 336 |
memcpy(e, &(wad->entries[start]), size); |
| 337 |
memmove(&(wad->entries[start]), &(wad->entries[end]), endsize); |
| 338 |
memcpy(&(wad->entries[start + count - 1]), e, size); |
| 339 |
|
| 340 |
wad->header.entry_count = wad->header.entry_count - count; |
| 341 |
WAD_FREE(e); |
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
static int WAD_SwapEntryCommon(wad_t *wad, int a, int b) |
| 347 |
{ |
| 348 |
if (a < 0 || a >= wad->header.entry_count) |
| 349 |
{ |
| 350 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 351 |
return 1; |
| 352 |
} |
| 353 |
|
| 354 |
if (b < 0 || b >= wad->header.entry_count) |
| 355 |
{ |
| 356 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 357 |
return 1; |
| 358 |
} |
| 359 |
|
| 360 |
if (a == b) |
| 361 |
return 0; |
| 362 |
|
| 363 |
wadentry_t *temp = wad->entries[a]; |
| 364 |
wad->entries[a] = wad->entries[b]; |
| 365 |
wad->entries[b] = temp; |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
static int WAD_ShiftEntryCommon(wad_t *wad, int source, int count, int destination) |
| 371 |
{ |
| 372 |
if (source < 0 || source >= wad->header.entry_count) |
| 373 |
{ |
| 374 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 375 |
return 1; |
| 376 |
} |
| 377 |
|
| 378 |
if (destination < 0 || destination >= wad->header.entry_count) |
| 379 |
{ |
| 380 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 381 |
return 1; |
| 382 |
} |
| 383 |
|
| 384 |
if (source == destination) |
| 385 |
return 0; |
| 386 |
|
| 387 |
|
| 388 |
if (source + count > wad->header.entry_count) |
| 389 |
count = wad->header.entry_count - source; |
| 390 |
|
| 391 |
if (destination + count < 0 || destination + count > wad->header.entry_count) |
| 392 |
{ |
| 393 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 394 |
return 1; |
| 395 |
} |
| 396 |
|
| 397 |
size_t size = sizeof(wadentry_t*) * count; |
| 398 |
wadentry_t **copy = (wadentry_t**)WAD_MALLOC(size); |
| 399 |
memcpy(copy, &(wad->entries[source]), size); |
| 400 |
|
| 401 |
if (destination < source) |
| 402 |
{ |
| 403 |
int destcount = source - destination; |
| 404 |
size_t destsize = sizeof(wadentry_t*) * destcount; |
| 405 |
memmove(&(wad->entries[destination + count]), &(wad->entries[destination]), destsize); |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
int destcount = (destination + count) - (source + count); |
| 410 |
size_t destsize = sizeof(wadentry_t*) * destcount; |
| 411 |
memmove(&(wad->entries[source]), &(wad->entries[source + count]), destsize); |
| 412 |
} |
| 413 |
|
| 414 |
memcpy(&(wad->entries[destination]), copy, size); |
| 415 |
WAD_FREE(copy); |
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
typedef struct { |
| 424 |
|
| 425 |
int (*destroy)(wad_t*); |
| 426 |
int (*commit_entries)(wad_t*); |
| 427 |
wadentry_t* (*create_entry_at)(wad_t*, const char*, int); |
| 428 |
wadentry_t* (*add_entry_at)(wad_t*, const char*, int, unsigned char*, size_t); |
| 429 |
wadentry_t* (*add_entry_data_at)(wad_t*, const char*, int, FILE*); |
| 430 |
wadentry_t* (*add_entry_explicit_at)(wad_t*, const char*, int, size_t, int); |
| 431 |
int (*remove_entries_at)(wad_t*, int*, int); |
| 432 |
int (*remove_entry_range)(wad_t*, int, int); |
| 433 |
int (*swap_entries)(wad_t*, int, int); |
| 434 |
int (*shift_entries)(wad_t*, int, int, int); |
| 435 |
int (*get_data)(wad_t*, wadentry_t*, unsigned char*); |
| 436 |
int (*read_data)(wad_t*, wadentry_t*, void*, size_t, size_t); |
| 437 |
|
| 438 |
} wadfuncs_t; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static int wi_map_destroy(wad_t *wad) |
| 447 |
{ |
| 448 |
|
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
static int wi_map_commit_entries(wad_t *wad) |
| 454 |
{ |
| 455 |
|
| 456 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 457 |
return 0; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
static wadentry_t* wi_map_create_entry_at(wad_t *wad, const char *name, int index) |
| 462 |
{ |
| 463 |
|
| 464 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
static wadentry_t* wi_map_add_entry_at(wad_t *wad, const char *name, int index, unsigned char *buffer, size_t size) |
| 470 |
{ |
| 471 |
|
| 472 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 473 |
return NULL; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
static wadentry_t* wi_map_add_entry_data_at(wad_t *wad, const char *name, int index, FILE *stream) |
| 478 |
{ |
| 479 |
|
| 480 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 481 |
return NULL; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
static wadentry_t* wi_map_add_entry_explicit_at(wad_t *wad, const char *name, int index, size_t length, int offset) |
| 486 |
{ |
| 487 |
|
| 488 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 489 |
return NULL; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
static int wi_map_remove_entries_at(wad_t *wad, int *indices, int count) |
| 494 |
{ |
| 495 |
|
| 496 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 497 |
return 1; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
static int wi_map_remove_entry_range(wad_t *wad, int start, int count) |
| 502 |
{ |
| 503 |
|
| 504 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 505 |
return 1; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
static int wi_map_swap_entries(wad_t *wad, int a, int b) |
| 510 |
{ |
| 511 |
|
| 512 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
static int wi_map_shift_entries(wad_t *wad, int source, int count, int destination) |
| 518 |
{ |
| 519 |
|
| 520 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
static int wi_map_get_data(wad_t *wad, wadentry_t *entry, unsigned char *destination) |
| 526 |
{ |
| 527 |
|
| 528 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
static int wi_map_read_data(wad_t *wad, wadentry_t *entry, void *destination, size_t size, size_t count) |
| 534 |
{ |
| 535 |
|
| 536 |
waderrno = WADERROR_NOT_SUPPORTED; |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
static wadfuncs_t WI_MAP_WADFUNCS = { |
| 541 |
wi_map_destroy, |
| 542 |
wi_map_commit_entries, |
| 543 |
wi_map_create_entry_at, |
| 544 |
wi_map_add_entry_at, |
| 545 |
wi_map_add_entry_data_at, |
| 546 |
wi_map_add_entry_explicit_at, |
| 547 |
wi_map_remove_entries_at, |
| 548 |
wi_map_remove_entry_range, |
| 549 |
wi_map_swap_entries, |
| 550 |
wi_map_shift_entries, |
| 551 |
wi_map_get_data, |
| 552 |
wi_map_read_data, |
| 553 |
}; |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static int wi_file_destroy(wad_t *wad) |
| 561 |
{ |
| 562 |
|
| 563 |
fflush(wad->handle.file); |
| 564 |
fclose(wad->handle.file); |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
static int wi_file_commit_header(wad_t *wad) |
| 570 |
{ |
| 571 |
errno = 0; |
| 572 |
FILE *file = wad->handle.file; |
| 573 |
if (fseek(file, 0, SEEK_SET)) |
| 574 |
return 1; |
| 575 |
if (!fwrite(&(wad->header), sizeof(wadheader_t), 1, file)) |
| 576 |
return 1; |
| 577 |
|
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
static int wi_file_commit_entry(wad_t *wad, int index) |
| 582 |
{ |
| 583 |
errno = 0; |
| 584 |
FILE *file = wad->handle.file; |
| 585 |
if (fseek(file, wad->header.entry_list_offset + (sizeof(wadentry_t) * index), SEEK_SET)) |
| 586 |
return 1; |
| 587 |
if (!fwrite(wad->entries[index], sizeof(wadentry_t), 1, file)) |
| 588 |
return 1; |
| 589 |
|
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
static int wi_file_commit_entries(wad_t *wad) |
| 595 |
{ |
| 596 |
if (wi_file_commit_header(wad)) |
| 597 |
{ |
| 598 |
if (errno) |
| 599 |
waderrno = WADERROR_FILE_ERROR; |
| 600 |
else |
| 601 |
waderrno = WADERROR_CANNOT_COMMIT; |
| 602 |
return 1; |
| 603 |
} |
| 604 |
|
| 605 |
int i = 0; |
| 606 |
while (i < wad->header.entry_count) |
| 607 |
{ |
| 608 |
if (wi_file_commit_entry(wad, i++)) |
| 609 |
{ |
| 610 |
if (errno) |
| 611 |
waderrno = WADERROR_FILE_ERROR; |
| 612 |
else |
| 613 |
waderrno = WADERROR_CANNOT_COMMIT; |
| 614 |
return 1; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
static wadentry_t* wi_file_create_entry_at(wad_t *wad, const char *name, int index) |
| 623 |
{ |
| 624 |
wadentry_t* entry; |
| 625 |
if (!(entry = WAD_AddEntryCommon(wad, name, 0, 0, index))) |
| 626 |
{ |
| 627 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 628 |
return NULL; |
| 629 |
} |
| 630 |
if (wi_file_commit_entries(wad)) |
| 631 |
return NULL; |
| 632 |
return entry; |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
static wadentry_t* wi_file_add_entry_at(wad_t *wad, const char *name, int index, unsigned char *buffer, size_t size) |
| 637 |
{ |
| 638 |
int count; |
| 639 |
wadentry_t* entry; |
| 640 |
int pos = wad->header.entry_list_offset; |
| 641 |
if (!(entry = WAD_AddEntryCommon(wad, name, size, pos, index))) |
| 642 |
{ |
| 643 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 644 |
return NULL; |
| 645 |
} |
| 646 |
|
| 647 |
FILE *fp = wad->handle.file; |
| 648 |
if (fseek(fp, pos, SEEK_SET)) |
| 649 |
return NULL; |
| 650 |
|
| 651 |
size_t remain = size; |
| 652 |
|
| 653 |
while (remain) |
| 654 |
{ |
| 655 |
count = min(CBUF_LEN, remain); |
| 656 |
if (fwrite(buffer, 1, count, fp) < count) |
| 657 |
{ |
| 658 |
waderrno = WADERROR_FILE_ERROR; |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
remain -= count; |
| 662 |
buffer += count; |
| 663 |
} |
| 664 |
|
| 665 |
wad->header.entry_list_offset = pos + size; |
| 666 |
|
| 667 |
if (wi_file_commit_entries(wad)) |
| 668 |
return NULL; |
| 669 |
|
| 670 |
return entry; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
static wadentry_t* wi_file_add_entry_data_at(wad_t *wad, const char *name, int index, FILE *stream) |
| 675 |
{ |
| 676 |
wadentry_t* entry; |
| 677 |
int pos = wad->header.entry_list_offset; |
| 678 |
FILE *fp = wad->handle.file; |
| 679 |
if (fseek(fp, pos, SEEK_SET)) |
| 680 |
{ |
| 681 |
waderrno = WADERROR_FILE_ERROR; |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
|
| 685 |
int buf = 0; |
| 686 |
int count = 0; |
| 687 |
while ((buf = fread(cbuf, 1, CBUF_LEN, stream))) |
| 688 |
{ |
| 689 |
if (fwrite(cbuf, 1, buf, fp) < buf) |
| 690 |
{ |
| 691 |
waderrno = WADERROR_FILE_ERROR; |
| 692 |
return NULL; |
| 693 |
} |
| 694 |
fflush(stream); |
| 695 |
count += buf; |
| 696 |
} |
| 697 |
|
| 698 |
wad->header.entry_list_offset = pos + count; |
| 699 |
|
| 700 |
if (!(entry = WAD_AddEntryCommon(wad, name, count, pos, index))) |
| 701 |
{ |
| 702 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 703 |
return NULL; |
| 704 |
} |
| 705 |
|
| 706 |
if (wi_file_commit_entries(wad)) |
| 707 |
return NULL; |
| 708 |
|
| 709 |
return entry; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
static wadentry_t* wi_file_add_entry_explicit_at(wad_t *wad, const char *name, int index, size_t length, int offset) |
| 714 |
{ |
| 715 |
wadentry_t *entry; |
| 716 |
if (!(entry = WAD_AddEntryCommon(wad, name, length, offset, index))) |
| 717 |
{ |
| 718 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 719 |
return NULL; |
| 720 |
} |
| 721 |
|
| 722 |
if (wi_file_commit_entries(wad)) |
| 723 |
return NULL; |
| 724 |
|
| 725 |
return entry; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
static int wi_file_remove_entries_at(wad_t *wad, int *indices, int count) |
| 730 |
{ |
| 731 |
if (WAD_RemoveEntriesCommon(wad, indices, count)) |
| 732 |
return 1; |
| 733 |
if (wi_file_commit_entries(wad)) |
| 734 |
return 1; |
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
static int wi_file_remove_entry_range(wad_t *wad, int start, int count) |
| 740 |
{ |
| 741 |
if (WAD_RemoveEntryRangeCommon(wad, start, count)) |
| 742 |
return 1; |
| 743 |
if (wi_file_commit_entries(wad)) |
| 744 |
return 1; |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
static int wi_file_swap_entries(wad_t *wad, int a, int b) |
| 750 |
{ |
| 751 |
if (WAD_SwapEntryCommon(wad, a, b)) |
| 752 |
return 1; |
| 753 |
if (wi_file_commit_entries(wad)) |
| 754 |
return 1; |
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
static int wi_file_shift_entries(wad_t *wad, int source, int count, int destination) |
| 760 |
{ |
| 761 |
if (WAD_ShiftEntryCommon(wad, source, count, destination)) |
| 762 |
return 1; |
| 763 |
if (wi_file_commit_entries(wad)) |
| 764 |
return 1; |
| 765 |
return 0; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
static int wi_file_get_data(wad_t *wad, wadentry_t *entry, unsigned char *destination) |
| 770 |
{ |
| 771 |
if (fseek(wad->handle.file, entry->offset, SEEK_SET)) |
| 772 |
{ |
| 773 |
waderrno = WADERROR_FILE_ERROR; |
| 774 |
return -1; |
| 775 |
} |
| 776 |
|
| 777 |
return fread(destination, 1, entry->length, wad->handle.file); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
static int wi_file_read_data(wad_t *wad, wadentry_t *entry, void *destination, size_t size, size_t count) |
| 782 |
{ |
| 783 |
if (fseek(wad->handle.file, entry->offset, SEEK_SET)) |
| 784 |
{ |
| 785 |
waderrno = WADERROR_FILE_ERROR; |
| 786 |
return -1; |
| 787 |
} |
| 788 |
|
| 789 |
return fread(destination, size, count, wad->handle.file); |
| 790 |
} |
| 791 |
|
| 792 |
static wadfuncs_t WI_FILE_WADFUNCS = { |
| 793 |
wi_file_destroy, |
| 794 |
wi_file_commit_entries, |
| 795 |
wi_file_create_entry_at, |
| 796 |
wi_file_add_entry_at, |
| 797 |
wi_file_add_entry_data_at, |
| 798 |
wi_file_add_entry_explicit_at, |
| 799 |
wi_file_remove_entries_at, |
| 800 |
wi_file_remove_entry_range, |
| 801 |
wi_file_swap_entries, |
| 802 |
wi_file_shift_entries, |
| 803 |
wi_file_get_data, |
| 804 |
wi_file_read_data, |
| 805 |
}; |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
static int wi_buffer_destroy(wad_t *wad) |
| 813 |
{ |
| 814 |
|
| 815 |
WAD_FREE(wad->handle.buffer); |
| 816 |
wad->buffer_size = 0; |
| 817 |
wad->buffer_capacity = -1; |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
static int wi_buffer_commit_entries(wad_t *wad) |
| 823 |
{ |
| 824 |
|
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
static wadentry_t* wi_buffer_create_entry_at(wad_t *wad, const char *name, int index) |
| 830 |
{ |
| 831 |
wadentry_t* entry; |
| 832 |
if (!(entry = WAD_AddEntryCommon(wad, name, 0, 0, index))) |
| 833 |
{ |
| 834 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 835 |
return NULL; |
| 836 |
} |
| 837 |
return entry; |
| 838 |
} |
| 839 |
|
| 840 |
static int wi_buffer_attempt_expand(wad_t *wad, int add) |
| 841 |
{ |
| 842 |
|
| 843 |
if (wad->buffer_size + add >= wad->buffer_capacity) |
| 844 |
{ |
| 845 |
if (WAD_ExpandBuffer(wad, wad->buffer_capacity * 2)) |
| 846 |
return 1; |
| 847 |
} |
| 848 |
|
| 849 |
return 0; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
static wadentry_t* wi_buffer_add_entry_at(wad_t *wad, const char *name, int index, unsigned char *buffer, size_t size) |
| 854 |
{ |
| 855 |
if (wi_buffer_attempt_expand(wad, size)) |
| 856 |
{ |
| 857 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 858 |
return NULL; |
| 859 |
} |
| 860 |
|
| 861 |
unsigned char *dest = &(wad->handle.buffer[wad->buffer_size]); |
| 862 |
memcpy(dest, buffer, size); |
| 863 |
|
| 864 |
wadentry_t* entry; |
| 865 |
if (!(entry = WAD_AddEntryCommon(wad, name, size, wad->buffer_size, index))) |
| 866 |
{ |
| 867 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 868 |
return NULL; |
| 869 |
} |
| 870 |
|
| 871 |
wad->buffer_size += size; |
| 872 |
wad->header.entry_list_offset = wad->buffer_size + sizeof(wadheader_t); |
| 873 |
|
| 874 |
return entry; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
static wadentry_t* wi_buffer_add_entry_data_at(wad_t *wad, const char *name, int index, FILE *stream) |
| 879 |
{ |
| 880 |
int buf = 0; |
| 881 |
int count = 0; |
| 882 |
unsigned char *dest = NULL; |
| 883 |
while (buf = fread(cbuf, 1, CBUF_LEN, stream)) |
| 884 |
{ |
| 885 |
if (wi_buffer_attempt_expand(wad, buf)) |
| 886 |
{ |
| 887 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 888 |
return NULL; |
| 889 |
} |
| 890 |
|
| 891 |
dest = &(wad->handle.buffer[wad->buffer_size]); |
| 892 |
memcpy(dest, cbuf, buf); |
| 893 |
|
| 894 |
wad->buffer_size += buf; |
| 895 |
count += buf; |
| 896 |
} |
| 897 |
|
| 898 |
int pos = wad->header.entry_list_offset; |
| 899 |
wad->header.entry_list_offset = pos + count; |
| 900 |
|
| 901 |
wadentry_t* entry; |
| 902 |
if (!(entry = WAD_AddEntryCommon(wad, name, count, pos, index))) |
| 903 |
{ |
| 904 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 905 |
return NULL; |
| 906 |
} |
| 907 |
|
| 908 |
return entry; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
static wadentry_t* wi_buffer_add_entry_explicit_at(wad_t *wad, const char *name, int index, size_t length, int offset) |
| 913 |
{ |
| 914 |
wadentry_t *entry; |
| 915 |
if (!(entry = WAD_AddEntryCommon(wad, name, length, offset, index))) |
| 916 |
{ |
| 917 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 918 |
return NULL; |
| 919 |
} |
| 920 |
|
| 921 |
if (wi_buffer_commit_entries(wad)) |
| 922 |
return NULL; |
| 923 |
|
| 924 |
return entry; |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
static int wi_buffer_remove_entries_at(wad_t *wad, int *indices, int count) |
| 929 |
{ |
| 930 |
return WAD_RemoveEntriesCommon(wad, indices, count); |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
static int wi_buffer_remove_entry_range(wad_t *wad, int start, int count) |
| 935 |
{ |
| 936 |
return WAD_RemoveEntryRangeCommon(wad, start, count); |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
static int wi_buffer_swap_entries(wad_t *wad, int a, int b) |
| 941 |
{ |
| 942 |
if (WAD_SwapEntryCommon(wad, a, b)) |
| 943 |
return 1; |
| 944 |
if (wi_buffer_commit_entries(wad)) |
| 945 |
return 1; |
| 946 |
return 0; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
static int wi_buffer_shift_entries(wad_t *wad, int source, int count, int destination) |
| 951 |
{ |
| 952 |
if (WAD_ShiftEntryCommon(wad, source, count, destination)) |
| 953 |
return 1; |
| 954 |
if (wi_buffer_commit_entries(wad)) |
| 955 |
return 1; |
| 956 |
return 0; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
static int wi_buffer_get_data(wad_t *wad, wadentry_t *entry, unsigned char *destination) |
| 961 |
{ |
| 962 |
unsigned char *dest = destination; |
| 963 |
unsigned char *src; |
| 964 |
int32_t len = entry->length; |
| 965 |
|
| 966 |
if (len > 0) |
| 967 |
{ |
| 968 |
src = wad->handle.buffer + entry->offset - sizeof(wadheader_t); |
| 969 |
memcpy(dest, src, len); |
| 970 |
return len; |
| 971 |
} |
| 972 |
|
| 973 |
return 0; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
static int wi_buffer_read_data(wad_t *wad, wadentry_t *entry, void *destination, size_t size, size_t count) |
| 978 |
{ |
| 979 |
int out = 0; |
| 980 |
unsigned char *dest = destination; |
| 981 |
unsigned char *src; |
| 982 |
int32_t len = entry->length; |
| 983 |
|
| 984 |
|
| 985 |
if (len) |
| 986 |
src = wad->handle.buffer + entry->offset - sizeof(wadheader_t); |
| 987 |
else |
| 988 |
src = wad->handle.buffer; |
| 989 |
|
| 990 |
while (len >= size && count--) |
| 991 |
{ |
| 992 |
memcpy(dest, src, size); |
| 993 |
dest += size; |
| 994 |
src += size; |
| 995 |
len -= size; |
| 996 |
out++; |
| 997 |
} |
| 998 |
return out; |
| 999 |
} |
| 1000 |
|
| 1001 |
static wadfuncs_t WI_BUFFER_WADFUNCS = { |
| 1002 |
wi_buffer_destroy, |
| 1003 |
wi_buffer_commit_entries, |
| 1004 |
wi_buffer_create_entry_at, |
| 1005 |
wi_buffer_add_entry_at, |
| 1006 |
wi_buffer_add_entry_data_at, |
| 1007 |
wi_buffer_add_entry_explicit_at, |
| 1008 |
wi_buffer_remove_entries_at, |
| 1009 |
wi_buffer_remove_entry_range, |
| 1010 |
wi_buffer_swap_entries, |
| 1011 |
wi_buffer_shift_entries, |
| 1012 |
wi_buffer_get_data, |
| 1013 |
wi_buffer_read_data, |
| 1014 |
}; |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
static wadfuncs_t* WAD_funcs(wadimpl_t impl) |
| 1019 |
{ |
| 1020 |
switch (impl) |
| 1021 |
{ |
| 1022 |
case WI_MAP: return &WI_MAP_WADFUNCS; |
| 1023 |
case WI_FILE: return &WI_FILE_WADFUNCS; |
| 1024 |
case WI_BUFFER: return &WI_BUFFER_WADFUNCS; |
| 1025 |
} |
| 1026 |
|
| 1027 |
return NULL; |
| 1028 |
} |
| 1029 |
|
| 1030 |
#define WI_FUNC(w,f) (WAD_funcs((w)->type))->f |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
wad_t* WAD_Open(const char *filename) |
| 1041 |
{ |
| 1042 |
wad_t *out; |
| 1043 |
FILE *fp; |
| 1044 |
|
| 1045 |
|
| 1046 |
waderrno = WADERROR_NO_ERROR; |
| 1047 |
|
| 1048 |
fp = fopen(filename, "r+b"); |
| 1049 |
if (!fp) |
| 1050 |
{ |
| 1051 |
waderrno = WADERROR_FILE_ERROR; |
| 1052 |
return NULL; |
| 1053 |
} |
| 1054 |
|
| 1055 |
out = WAD_Init(); |
| 1056 |
if (!out) |
| 1057 |
{ |
| 1058 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1059 |
return NULL; |
| 1060 |
} |
| 1061 |
if (WAD_SetupOpenFileHandle(fp, &(out->header))) |
| 1062 |
{ |
| 1063 |
waderrno = WADERROR_FILE_NOT_A_WAD; |
| 1064 |
return NULL; |
| 1065 |
} |
| 1066 |
if (WAD_SetupBuildEntrylist(fp, out)) |
| 1067 |
{ |
| 1068 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1069 |
return NULL; |
| 1070 |
} |
| 1071 |
|
| 1072 |
out->type = WI_FILE; |
| 1073 |
out->handle.file = fp; |
| 1074 |
|
| 1075 |
return out; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
wad_t* WAD_Create(char *filename) |
| 1083 |
{ |
| 1084 |
wad_t *out; |
| 1085 |
FILE *fp; |
| 1086 |
|
| 1087 |
|
| 1088 |
waderrno = WADERROR_NO_ERROR; |
| 1089 |
|
| 1090 |
fp = fopen(filename, "w+b"); |
| 1091 |
if (!fp) |
| 1092 |
{ |
| 1093 |
waderrno = WADERROR_FILE_ERROR; |
| 1094 |
return NULL; |
| 1095 |
} |
| 1096 |
|
| 1097 |
out = WAD_Init(); |
| 1098 |
if (!out) |
| 1099 |
{ |
| 1100 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1101 |
return NULL; |
| 1102 |
} |
| 1103 |
if (WAD_ExpandEntrylist(out, 8)) |
| 1104 |
{ |
| 1105 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1106 |
return NULL; |
| 1107 |
} |
| 1108 |
|
| 1109 |
out->type = WI_FILE; |
| 1110 |
out->handle.file = fp; |
| 1111 |
|
| 1112 |
if (wi_file_commit_header(out)) |
| 1113 |
{ |
| 1114 |
waderrno = WADERROR_CANNOT_COMMIT; |
| 1115 |
fclose(fp); |
| 1116 |
WAD_FREE(out); |
| 1117 |
return NULL; |
| 1118 |
} |
| 1119 |
if (WAD_SetupOpenFileHandle(fp, &(out->header))) |
| 1120 |
{ |
| 1121 |
waderrno = WADERROR_FILE_NOT_A_WAD; |
| 1122 |
fclose(fp); |
| 1123 |
WAD_FREE(out); |
| 1124 |
return NULL; |
| 1125 |
} |
| 1126 |
|
| 1127 |
return out; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
wad_t* WAD_OpenMap(char *filename) |
| 1135 |
{ |
| 1136 |
wad_t *out; |
| 1137 |
FILE *fp; |
| 1138 |
|
| 1139 |
|
| 1140 |
waderrno = WADERROR_NO_ERROR; |
| 1141 |
|
| 1142 |
fp = fopen(filename, "rb"); |
| 1143 |
if (!fp) |
| 1144 |
{ |
| 1145 |
waderrno = WADERROR_FILE_ERROR; |
| 1146 |
return NULL; |
| 1147 |
} |
| 1148 |
|
| 1149 |
out = WAD_Init(); |
| 1150 |
if (!out) |
| 1151 |
{ |
| 1152 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1153 |
return NULL; |
| 1154 |
} |
| 1155 |
if (WAD_SetupOpenFileHandle(fp, &(out->header))) |
| 1156 |
{ |
| 1157 |
waderrno = WADERROR_FILE_NOT_A_WAD; |
| 1158 |
return NULL; |
| 1159 |
} |
| 1160 |
if (WAD_SetupBuildEntrylist(fp, out)) |
| 1161 |
{ |
| 1162 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1163 |
return NULL; |
| 1164 |
} |
| 1165 |
|
| 1166 |
fclose(fp); |
| 1167 |
out->type = WI_MAP; |
| 1168 |
|
| 1169 |
return out; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
wad_t* WAD_OpenBuffer(char *filename) |
| 1177 |
{ |
| 1178 |
wad_t *out; |
| 1179 |
FILE *fp; |
| 1180 |
|
| 1181 |
|
| 1182 |
waderrno = WADERROR_NO_ERROR; |
| 1183 |
|
| 1184 |
fp = fopen(filename, "rb"); |
| 1185 |
if (!fp) |
| 1186 |
{ |
| 1187 |
waderrno = WADERROR_FILE_ERROR; |
| 1188 |
return NULL; |
| 1189 |
} |
| 1190 |
|
| 1191 |
out = WAD_Init(); |
| 1192 |
if (!out) |
| 1193 |
{ |
| 1194 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1195 |
return NULL; |
| 1196 |
} |
| 1197 |
if (WAD_SetupOpenFileHandle(fp, &(out->header))) |
| 1198 |
{ |
| 1199 |
waderrno = WADERROR_FILE_NOT_A_WAD; |
| 1200 |
return NULL; |
| 1201 |
} |
| 1202 |
if (WAD_SetupBuildEntrylist(fp, out)) |
| 1203 |
{ |
| 1204 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1205 |
return NULL; |
| 1206 |
} |
| 1207 |
if (WAD_SetupBuildBuffer(fp, out)) |
| 1208 |
{ |
| 1209 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1210 |
return NULL; |
| 1211 |
} |
| 1212 |
|
| 1213 |
out->type = WI_BUFFER; |
| 1214 |
|
| 1215 |
return out; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
wad_t* WAD_CreateBuffer(void) |
| 1223 |
{ |
| 1224 |
return WAD_CreateBufferInit(WADBUFFER_INITSIZE); |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
wad_t* WAD_CreateBufferInit(int size) |
| 1232 |
{ |
| 1233 |
wad_t *out; |
| 1234 |
|
| 1235 |
|
| 1236 |
waderrno = WADERROR_NO_ERROR; |
| 1237 |
|
| 1238 |
out = WAD_Init(); |
| 1239 |
if (!out) |
| 1240 |
{ |
| 1241 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1242 |
return NULL; |
| 1243 |
} |
| 1244 |
if (WAD_ExpandEntrylist(out, WADENTRIES_INITSIZE)) |
| 1245 |
{ |
| 1246 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1247 |
return NULL; |
| 1248 |
} |
| 1249 |
if (WAD_ExpandBuffer(out, size)) |
| 1250 |
{ |
| 1251 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1252 |
return NULL; |
| 1253 |
} |
| 1254 |
|
| 1255 |
out->type = WI_BUFFER; |
| 1256 |
|
| 1257 |
return out; |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
int WAD_CommitEntries(wad_t *wad) |
| 1265 |
{ |
| 1266 |
|
| 1267 |
waderrno = WADERROR_NO_ERROR; |
| 1268 |
|
| 1269 |
if (wad == NULL) |
| 1270 |
{ |
| 1271 |
waderrno = WADERROR_WAD_INVALID; |
| 1272 |
return 1; |
| 1273 |
} |
| 1274 |
|
| 1275 |
if ((WI_FUNC(wad, commit_entries))(wad)) |
| 1276 |
{ |
| 1277 |
waderrno = WADERROR_CANNOT_COMMIT; |
| 1278 |
return 1; |
| 1279 |
} |
| 1280 |
|
| 1281 |
return 0; |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
wadentry_t* WAD_GetEntry(const wad_t *wad, int index) |
| 1289 |
{ |
| 1290 |
|
| 1291 |
waderrno = WADERROR_NO_ERROR; |
| 1292 |
|
| 1293 |
if (wad == NULL) |
| 1294 |
{ |
| 1295 |
waderrno = WADERROR_WAD_INVALID; |
| 1296 |
return NULL; |
| 1297 |
} |
| 1298 |
|
| 1299 |
if (index < 0 || index >= wad->header.entry_count) |
| 1300 |
{ |
| 1301 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 1302 |
return NULL; |
| 1303 |
} |
| 1304 |
|
| 1305 |
return wad->entries[index]; |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
wadentry_t* WAD_GetEntryByName(wad_t *wad, const char *name) |
| 1313 |
{ |
| 1314 |
return WAD_GetEntryByNameOffset(wad, name, 0); |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
wadentry_t* WAD_GetEntryByNameOffset(wad_t *wad, const char *name, int start) |
| 1322 |
{ |
| 1323 |
|
| 1324 |
waderrno = WADERROR_NO_ERROR; |
| 1325 |
|
| 1326 |
if (wad == NULL) |
| 1327 |
{ |
| 1328 |
waderrno = WADERROR_WAD_INVALID; |
| 1329 |
return NULL; |
| 1330 |
} |
| 1331 |
|
| 1332 |
if (start < 0) |
| 1333 |
{ |
| 1334 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 1335 |
return NULL; |
| 1336 |
} |
| 1337 |
|
| 1338 |
while (start < wad->header.entry_count) |
| 1339 |
{ |
| 1340 |
|
| 1341 |
if (!strncmp(name, wad->entries[start]->name, 8)) |
| 1342 |
return wad->entries[start]; |
| 1343 |
start++; |
| 1344 |
} |
| 1345 |
|
| 1346 |
return NULL; |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
wadentry_t* WAD_GetEntryByNameNth(wad_t *wad, const char *name, int nth) |
| 1354 |
{ |
| 1355 |
return WAD_GetEntryByNameOffsetNth(wad, name, 0, nth); |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
wadentry_t* WAD_GetEntryByNameOffsetNth(wad_t *wad, const char *name, int start, int nth) |
| 1363 |
{ |
| 1364 |
|
| 1365 |
waderrno = WADERROR_NO_ERROR; |
| 1366 |
|
| 1367 |
if (wad == NULL) |
| 1368 |
{ |
| 1369 |
waderrno = WADERROR_WAD_INVALID; |
| 1370 |
return NULL; |
| 1371 |
} |
| 1372 |
|
| 1373 |
if (start < 0) |
| 1374 |
{ |
| 1375 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 1376 |
return NULL; |
| 1377 |
} |
| 1378 |
|
| 1379 |
while (start < wad->header.entry_count) |
| 1380 |
{ |
| 1381 |
|
| 1382 |
if (!strncmp(name, wad->entries[start]->name, 8)) |
| 1383 |
if (--nth <= 0) |
| 1384 |
return wad->entries[start]; |
| 1385 |
start++; |
| 1386 |
} |
| 1387 |
|
| 1388 |
return NULL; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
wadentry_t* WAD_GetLastEntryByName(wad_t *wad, const char *name) |
| 1396 |
{ |
| 1397 |
|
| 1398 |
waderrno = WADERROR_NO_ERROR; |
| 1399 |
|
| 1400 |
if (wad == NULL) |
| 1401 |
{ |
| 1402 |
waderrno = WADERROR_WAD_INVALID; |
| 1403 |
return NULL; |
| 1404 |
} |
| 1405 |
|
| 1406 |
int i = wad->header.entry_count - 1; |
| 1407 |
while (i >= 0) |
| 1408 |
{ |
| 1409 |
|
| 1410 |
if (!strncmp(name, wad->entries[i]->name, 8)) |
| 1411 |
return wad->entries[i]; |
| 1412 |
i--; |
| 1413 |
} |
| 1414 |
|
| 1415 |
return NULL; |
| 1416 |
} |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
int WAD_GetEntryCount(wad_t *wad, const char *name) |
| 1423 |
{ |
| 1424 |
|
| 1425 |
waderrno = WADERROR_NO_ERROR; |
| 1426 |
|
| 1427 |
if (wad == NULL) |
| 1428 |
{ |
| 1429 |
waderrno = WADERROR_WAD_INVALID; |
| 1430 |
return -1; |
| 1431 |
} |
| 1432 |
|
| 1433 |
int i = 0; |
| 1434 |
int out = 0; |
| 1435 |
while (i < wad->header.entry_count) |
| 1436 |
{ |
| 1437 |
|
| 1438 |
if (!strncmp(name, wad->entries[i]->name, 8)) |
| 1439 |
out++; |
| 1440 |
i++; |
| 1441 |
} |
| 1442 |
|
| 1443 |
return out; |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
int WAD_GetEntryIndex(wad_t *wad, const char *name) |
| 1451 |
{ |
| 1452 |
return WAD_GetEntryIndexOffset(wad, name, 0); |
| 1453 |
} |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
int WAD_GetEntryIndexOffset(wad_t *wad, const char *name, int start) |
| 1460 |
{ |
| 1461 |
|
| 1462 |
waderrno = WADERROR_NO_ERROR; |
| 1463 |
|
| 1464 |
if (wad == NULL) |
| 1465 |
{ |
| 1466 |
waderrno = WADERROR_WAD_INVALID; |
| 1467 |
return -1; |
| 1468 |
} |
| 1469 |
|
| 1470 |
if (start < 0) |
| 1471 |
{ |
| 1472 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 1473 |
return -1; |
| 1474 |
} |
| 1475 |
|
| 1476 |
while (start < wad->header.entry_count) |
| 1477 |
{ |
| 1478 |
|
| 1479 |
if (!strncmp(name, wad->entries[start]->name, 8)) |
| 1480 |
return start; |
| 1481 |
start++; |
| 1482 |
} |
| 1483 |
|
| 1484 |
return -1; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
int WAD_GetEntryIndices(wad_t *wad, const char *name, int *out, int max) |
| 1492 |
{ |
| 1493 |
return WAD_GetEntryIndicesOffset(wad, name, 0, out, max); |
| 1494 |
} |
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
int WAD_GetEntryIndicesOffset(wad_t *wad, const char *name, int start, int *out, int max) |
| 1501 |
{ |
| 1502 |
|
| 1503 |
waderrno = WADERROR_NO_ERROR; |
| 1504 |
|
| 1505 |
if (wad == NULL) |
| 1506 |
{ |
| 1507 |
waderrno = WADERROR_WAD_INVALID; |
| 1508 |
return -1; |
| 1509 |
} |
| 1510 |
|
| 1511 |
if (start < 0) |
| 1512 |
{ |
| 1513 |
waderrno = WADERROR_INDEX_OUT_OF_RANGE; |
| 1514 |
return -1; |
| 1515 |
} |
| 1516 |
|
| 1517 |
int i = 0; |
| 1518 |
while (i < max && (*out = WAD_GetEntryIndexOffset(wad, name, start)) >= 0) |
| 1519 |
{ |
| 1520 |
start = (*out) + 1; |
| 1521 |
out += 1; |
| 1522 |
i++; |
| 1523 |
} |
| 1524 |
|
| 1525 |
return i; |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
int WAD_GetEntryLastIndex(wad_t *wad, const char *name) |
| 1533 |
{ |
| 1534 |
|
| 1535 |
waderrno = WADERROR_NO_ERROR; |
| 1536 |
|
| 1537 |
if (wad == NULL) |
| 1538 |
{ |
| 1539 |
waderrno = WADERROR_WAD_INVALID; |
| 1540 |
return -1; |
| 1541 |
} |
| 1542 |
|
| 1543 |
int i = wad->header.entry_count - 1; |
| 1544 |
while (i >= 0) |
| 1545 |
{ |
| 1546 |
|
| 1547 |
if (!strncmp(name, wad->entries[i]->name, 8)) |
| 1548 |
return i; |
| 1549 |
i--; |
| 1550 |
} |
| 1551 |
|
| 1552 |
return -1; |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
wadentry_t* WAD_CreateEntry(wad_t *wad, const char *name) |
| 1560 |
{ |
| 1561 |
return WAD_CreateEntryAt(wad, name, wad->header.entry_count); |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
wadentry_t* WAD_CreateEntryAt(wad_t *wad, const char *name, int index) |
| 1569 |
{ |
| 1570 |
|
| 1571 |
waderrno = WADERROR_NO_ERROR; |
| 1572 |
errno = 0; |
| 1573 |
|
| 1574 |
if (wad == NULL) |
| 1575 |
{ |
| 1576 |
waderrno = WADERROR_WAD_INVALID; |
| 1577 |
return NULL; |
| 1578 |
} |
| 1579 |
|
| 1580 |
wadentry_t *out; |
| 1581 |
if (!(out = ((WI_FUNC(wad, create_entry_at))(wad, name, index)))) |
| 1582 |
{ |
| 1583 |
|
| 1584 |
return NULL; |
| 1585 |
} |
| 1586 |
|
| 1587 |
return out; |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
wadentry_t* WAD_AddEntry(wad_t *wad, const char *name, unsigned char *buffer, size_t buffer_size) |
| 1595 |
{ |
| 1596 |
return WAD_AddEntryAt(wad, name, wad->header.entry_count, buffer, buffer_size); |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
wadentry_t* WAD_AddEntryAt(wad_t *wad, const char *name, int index, unsigned char *buffer, size_t buffer_size) |
| 1604 |
{ |
| 1605 |
|
| 1606 |
waderrno = WADERROR_NO_ERROR; |
| 1607 |
errno = 0; |
| 1608 |
|
| 1609 |
if (wad == NULL) |
| 1610 |
{ |
| 1611 |
waderrno = WADERROR_WAD_INVALID; |
| 1612 |
return NULL; |
| 1613 |
} |
| 1614 |
|
| 1615 |
wadentry_t *out; |
| 1616 |
if (!(out = ((WI_FUNC(wad, add_entry_at))(wad, name, index, buffer, buffer_size)))) |
| 1617 |
{ |
| 1618 |
|
| 1619 |
return NULL; |
| 1620 |
} |
| 1621 |
|
| 1622 |
return out; |
| 1623 |
} |
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
wadentry_t* WAD_AddEntryData(wad_t *wad, const char *name, FILE *stream) |
| 1630 |
{ |
| 1631 |
return WAD_AddEntryDataAt(wad, name, wad->header.entry_count, stream); |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
wadentry_t* WAD_AddEntryDataAt(wad_t *wad, const char *name, int index, FILE *stream) |
| 1639 |
{ |
| 1640 |
|
| 1641 |
waderrno = WADERROR_NO_ERROR; |
| 1642 |
errno = 0; |
| 1643 |
|
| 1644 |
if (wad == NULL) |
| 1645 |
{ |
| 1646 |
waderrno = WADERROR_WAD_INVALID; |
| 1647 |
return NULL; |
| 1648 |
} |
| 1649 |
|
| 1650 |
wadentry_t *out; |
| 1651 |
if (!(out = ((WI_FUNC(wad, add_entry_data_at))(wad, name, index, stream)))) |
| 1652 |
{ |
| 1653 |
|
| 1654 |
return NULL; |
| 1655 |
} |
| 1656 |
|
| 1657 |
return out; |
| 1658 |
} |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
wadentry_t* WAD_AddExplicitEntry(wad_t *wad, const char *name, size_t length, int offset) |
| 1665 |
{ |
| 1666 |
return WAD_AddExplicitEntryAt(wad, name, wad->header.entry_count, length, offset); |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
wadentry_t* WAD_AddExplicitEntryAt(wad_t *wad, const char *name, int index, size_t length, int offset) |
| 1674 |
{ |
| 1675 |
|
| 1676 |
waderrno = WADERROR_NO_ERROR; |
| 1677 |
errno = 0; |
| 1678 |
|
| 1679 |
if (wad == NULL) |
| 1680 |
{ |
| 1681 |
waderrno = WADERROR_WAD_INVALID; |
| 1682 |
return NULL; |
| 1683 |
} |
| 1684 |
|
| 1685 |
wadentry_t *out; |
| 1686 |
if (!(out = ((WI_FUNC(wad, add_entry_explicit_at))(wad, name, index, length, offset)))) |
| 1687 |
{ |
| 1688 |
|
| 1689 |
return NULL; |
| 1690 |
} |
| 1691 |
|
| 1692 |
return out; |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
wadentry_t* WAD_AddMarkerEntry(wad_t *wad, const char *name) |
| 1700 |
{ |
| 1701 |
return WAD_AddExplicitEntry(wad, name, 0, 0); |
| 1702 |
} |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
wadentry_t* WAD_AddMarkerEntryAt(wad_t *wad, const char *name, int index) |
| 1709 |
{ |
| 1710 |
return WAD_AddExplicitEntryAt(wad, name, index, 0, 0); |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
int WAD_RemoveEntryAt(wad_t *wad, int index) |
| 1718 |
{ |
| 1719 |
return WAD_RemoveEntryRange(wad, index, 1); |
| 1720 |
} |
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
int WAD_RemoveEntriesAt(wad_t *wad, int *indices, int count) |
| 1727 |
{ |
| 1728 |
|
| 1729 |
waderrno = WADERROR_NO_ERROR; |
| 1730 |
errno = 0; |
| 1731 |
|
| 1732 |
if (wad == NULL) |
| 1733 |
{ |
| 1734 |
waderrno = WADERROR_WAD_INVALID; |
| 1735 |
return 1; |
| 1736 |
} |
| 1737 |
|
| 1738 |
if ((WI_FUNC(wad, remove_entries_at))(wad, indices, count)) |
| 1739 |
{ |
| 1740 |
|
| 1741 |
return 1; |
| 1742 |
} |
| 1743 |
|
| 1744 |
return 0; |
| 1745 |
} |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
int WAD_RemoveEntryRange(wad_t *wad, int start, int count) |
| 1752 |
{ |
| 1753 |
|
| 1754 |
waderrno = WADERROR_NO_ERROR; |
| 1755 |
errno = 0; |
| 1756 |
|
| 1757 |
if (wad == NULL) |
| 1758 |
{ |
| 1759 |
waderrno = WADERROR_WAD_INVALID; |
| 1760 |
return 1; |
| 1761 |
} |
| 1762 |
|
| 1763 |
if ((WI_FUNC(wad, remove_entry_range))(wad, start, count)) |
| 1764 |
{ |
| 1765 |
|
| 1766 |
return 1; |
| 1767 |
} |
| 1768 |
|
| 1769 |
return 0; |
| 1770 |
} |
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
int WAD_SwapEntry(wad_t *wad, int a, int b) |
| 1777 |
{ |
| 1778 |
|
| 1779 |
waderrno = WADERROR_NO_ERROR; |
| 1780 |
errno = 0; |
| 1781 |
|
| 1782 |
if (wad == NULL) |
| 1783 |
{ |
| 1784 |
waderrno = WADERROR_WAD_INVALID; |
| 1785 |
return 1; |
| 1786 |
} |
| 1787 |
|
| 1788 |
if ((WI_FUNC(wad, swap_entries))(wad, a, b)) |
| 1789 |
{ |
| 1790 |
|
| 1791 |
return 1; |
| 1792 |
} |
| 1793 |
|
| 1794 |
return 0; |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
int WAD_ShiftEntry(wad_t *wad, int source, int destination) |
| 1802 |
{ |
| 1803 |
return WAD_ShiftEntries(wad, source, 1, destination); |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
int WAD_ShiftEntries(wad_t *wad, int start, int count, int destination) |
| 1811 |
{ |
| 1812 |
|
| 1813 |
waderrno = WADERROR_NO_ERROR; |
| 1814 |
errno = 0; |
| 1815 |
|
| 1816 |
if (wad == NULL) |
| 1817 |
{ |
| 1818 |
waderrno = WADERROR_WAD_INVALID; |
| 1819 |
return 1; |
| 1820 |
} |
| 1821 |
|
| 1822 |
if ((WI_FUNC(wad, shift_entries))(wad, start, count, destination)) |
| 1823 |
{ |
| 1824 |
|
| 1825 |
return 1; |
| 1826 |
} |
| 1827 |
|
| 1828 |
return 0; |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
int WAD_GetEntryData(wad_t *wad, wadentry_t *entry, unsigned char *destination) |
| 1836 |
{ |
| 1837 |
|
| 1838 |
waderrno = WADERROR_NO_ERROR; |
| 1839 |
errno = 0; |
| 1840 |
|
| 1841 |
if (wad == NULL) |
| 1842 |
{ |
| 1843 |
waderrno = WADERROR_WAD_INVALID; |
| 1844 |
return 1; |
| 1845 |
} |
| 1846 |
|
| 1847 |
if ((WI_FUNC(wad, get_data))(wad, entry, destination) < 0) |
| 1848 |
{ |
| 1849 |
|
| 1850 |
return -1; |
| 1851 |
} |
| 1852 |
|
| 1853 |
return 0; |
| 1854 |
} |
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
int WAD_ReadEntryData(wad_t *wad, wadentry_t *entry, void *destination, size_t size, size_t count) |
| 1861 |
{ |
| 1862 |
|
| 1863 |
waderrno = WADERROR_NO_ERROR; |
| 1864 |
errno = 0; |
| 1865 |
|
| 1866 |
if (wad == NULL) |
| 1867 |
{ |
| 1868 |
waderrno = WADERROR_WAD_INVALID; |
| 1869 |
return 1; |
| 1870 |
} |
| 1871 |
|
| 1872 |
if ((WI_FUNC(wad, read_data))(wad, entry, destination, size, count) < 0) |
| 1873 |
{ |
| 1874 |
|
| 1875 |
return -1; |
| 1876 |
} |
| 1877 |
|
| 1878 |
return 0; |
| 1879 |
} |
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
int WAD_Close(wad_t *wad) |
| 1886 |
{ |
| 1887 |
|
| 1888 |
waderrno = WADERROR_NO_ERROR; |
| 1889 |
|
| 1890 |
if (wad == NULL) |
| 1891 |
{ |
| 1892 |
waderrno = WADERROR_WAD_INVALID; |
| 1893 |
return 1; |
| 1894 |
} |
| 1895 |
|
| 1896 |
if ((WI_FUNC(wad, destroy))(wad)) |
| 1897 |
{ |
| 1898 |
waderrno = WADERROR_CANNOT_CLOSE; |
| 1899 |
return 1; |
| 1900 |
} |
| 1901 |
|
| 1902 |
WAD_FreeAllocated(wad); |
| 1903 |
|
| 1904 |
return 0; |
| 1905 |
} |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
waditerator_t* WAD_IteratorCreate(wad_t *wad, int start) |
| 1912 |
{ |
| 1913 |
|
| 1914 |
waderrno = WADERROR_NO_ERROR; |
| 1915 |
|
| 1916 |
waditerator_t *out = (waditerator_t*)WAD_MALLOC(sizeof(waditerator_t)); |
| 1917 |
|
| 1918 |
if (wad == NULL) |
| 1919 |
{ |
| 1920 |
waderrno = WADERROR_OUT_OF_MEMORY; |
| 1921 |
return NULL; |
| 1922 |
} |
| 1923 |
|
| 1924 |
out->wad = wad; |
| 1925 |
out->entry = NULL; |
| 1926 |
out->next = start; |
| 1927 |
out->count = (int)(wad->header.entry_count); |
| 1928 |
|
| 1929 |
return out; |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
void WAD_IteratorReset(waditerator_t *iter, int start) |
| 1937 |
{ |
| 1938 |
iter->entry = NULL; |
| 1939 |
iter->next = start; |
| 1940 |
iter->count = iter->wad->header.entry_count; |
| 1941 |
} |
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
wadentry_t* WAD_IteratorNext(waditerator_t *iter) |
| 1948 |
{ |
| 1949 |
if (iter->next < iter->count) |
| 1950 |
{ |
| 1951 |
iter->entry = iter->wad->entries[iter->next]; |
| 1952 |
iter->next++; |
| 1953 |
return iter->entry; |
| 1954 |
} |
| 1955 |
else |
| 1956 |
{ |
| 1957 |
return NULL; |
| 1958 |
} |
| 1959 |
} |
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
void WAD_IteratorClose(waditerator_t *iter) |
| 1966 |
{ |
| 1967 |
WAD_FREE(iter); |
| 1968 |
} |