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
|
/*
ucdf - microlib for reading Compound Document Format files
Copyright (c) 2021 Tibor 'Igor2' Palinkas
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the Author nor the names of contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Source code: svn://svn.repo.hu/pcb-rnd/trunk
Contact the author: http://igor2.repo.hu/contact.html
*/
#include <stdlib.h>
#include <string.h>
#include "ucdf.h"
const char *ucdf_error_str[] = {
"success",
"failed to open file",
"failed to read",
"wrong file ID",
"broken file header",
"broken MSAT chain",
"broken SAT chain",
"broken short SAT chain",
"broken direcotry chain",
"failed to allocate memory",
NULL
};
/* master file header that identifies the file format */
static const unsigned char hdr_id[8] = { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 };
/* read len bytes to dst from ctx->f; set and return error on failure */
#define safe_read(dst, len) \
do { \
if (fread(dst, 1, len, ctx->f) != len) { \
ctx->error = UCDF_ERR_READ; \
return -1; \
} \
} while(0)
/* seek to offs in ctx->f; set and return error on failure */
#define safe_seek(offs) \
do { \
if (fseek(ctx->f, offs, SEEK_SET) != 0) { \
ctx->error = UCDF_ERR_READ; \
return -1; \
} \
} while(0)
/* shorthand: set and return error */
#define error(code) \
do { \
ctx->error = code; \
return -1; \
} while(0)
/* convert a sector ID to offset in ctx->f (long sectors) */
static long sect_id2offs(ucdf_ctx_t *ctx, long id)
{
return 512L + (id << ctx->ssz);
}
/* convert a file integer (len bytes wide) into a native integer */
static long load_int(ucdf_ctx_t *ctx, unsigned const char *src, int len)
{
int n, sh;
long tmp, res = 0;
if (ctx->litend) {
for(sh = n = 0; n < len; n++,sh += 8,src++) {
tmp = *src;
tmp = (tmp << sh);
res |= tmp;
}
}
else {
for(; len > 0; len--,src++) {
res <<= 8;
res |= *src;
}
}
/* sign extend */
if (((unsigned long)res) >> (len*8-1))
res |= ((unsigned long)-1) << (len*8-1);
return res;
}
/* Load the main file header. Return 0 on success. */
static int ucdf_read_hdr(ucdf_ctx_t *ctx)
{
unsigned char buff[16];
/* refuse the file on main header ID mismatch */
safe_read(buff, sizeof(hdr_id));
if (memcmp(buff, hdr_id, sizeof(hdr_id)) != 0)
error(UCDF_ERR_BAD_ID);
/* figure the byte order @28 */
safe_seek(28);
safe_read(buff, 2);
if ((buff[0] == 0xFE) || (buff[1] == 0xFF))
ctx->litend = 1;
else if ((buff[0] == 0xFF) || (buff[1] == 0xFE))
ctx->litend = 0;
else
error(UCDF_ERR_BAD_HDR);
/* sector sizes @30 */
safe_read(buff, 2);
ctx->ssz = load_int(ctx, buff, 2);
if (ctx->ssz < 7)
error(UCDF_ERR_BAD_HDR);
ctx->sect_size = 1 << ctx->ssz;
safe_read(buff, 2);
ctx->sssz = load_int(ctx, buff, 2);
if (ctx->sssz > ctx->ssz)
error(UCDF_ERR_BAD_HDR);
ctx->short_sect_size = 1 << ctx->sssz;
/* allocation table info @44 */
safe_seek(44);
safe_read(buff, 4);
ctx->sat_len = load_int(ctx, buff, 4);
safe_read(buff, 4);
ctx->dir_first = load_int(ctx, buff, 4);
safe_seek(56);
safe_read(buff, 4);
ctx->long_stream_min_size = load_int(ctx, buff, 4);
safe_read(buff, 4);
ctx->ssat_first = load_int(ctx, buff, 4);
safe_read(buff, 4);
ctx->ssat_len = load_int(ctx, buff, 4);
safe_read(buff, 4);
ctx->msat_first = load_int(ctx, buff, 4);
safe_read(buff, 4);
ctx->msat_len = load_int(ctx, buff, 4);
/* versions @24 */
safe_seek(24);
safe_read(buff, 2);
ctx->file_rev = load_int(ctx, buff, 2);
safe_read(buff, 2);
ctx->file_ver = load_int(ctx, buff, 2);
return 0;
}
/* Load a sector allocation table chunk from ctx->f into dst; the chunk is
one sector long; dst[] is indexed with *idx. Return 0 on success. */
static int ucdf_load_any_sat(ucdf_ctx_t *ctx, long *dst, long *idx)
{
int n;
long id_per_sect = ctx->sect_size >> 2;
unsigned char buff[4];
for(n = 0; n < id_per_sect; n++) {
safe_read(buff, 4);
dst[*idx] = load_int(ctx, buff, 4);
/* printf(" [%ld]: sect %ld (%02x %02x %02x %02x)\n", *idx, ctx->msat[*idx], buff[0], buff[1], buff[2], buff[3]);*/
(*idx)++;
}
return 0;
}
/* Load a master sector allocation table chunk from ctx->f into dst, total
num_ids slots. Store the result in ctx->msat[], indexed with *idx.
Return 0 on success. */
static int ucdf_load_msat_(ucdf_ctx_t *ctx, long num_ids, long *idx)
{
int n;
unsigned char buff[4];
for(n = 0; n < num_ids; n++) {
safe_read(buff, 4);
ctx->msat[*idx] = load_int(ctx, buff, 4);
/* printf(" [%ld]: sect %ld (%02x %02x %02x %02x)\n", *idx, ctx->msat[*idx], buff[0], buff[1], buff[2], buff[3]);*/
(*idx)++;
}
return 0;
}
/* Load the next sector of msat; return next sector ID or -2 (UCDF_SECT_EOC)
on end. */
static long ucdf_load_msat(ucdf_ctx_t *ctx, long num_ids, long *idx)
{
unsigned char buff[4];
long next;
/* load content */
if (ucdf_load_msat_(ctx, num_ids-1, idx) != 0)
return -1;
/* load next sector address in the chain */
safe_read(buff, 4);
next = load_int(ctx, buff, 4);
/* printf("msat next sect: %ld\n", next);*/
return next;
}
/* Load and build all sector allocation tables. Return 0 on succss. */
static int ucdf_read_sats(ucdf_ctx_t *ctx)
{
long next, n, idx = 0, id_per_sect = ctx->sect_size >> 2;
ctx->msat = malloc(sizeof(long) * (ctx->msat_len * id_per_sect + 109));
/* load first block of msat from the header @ 76*/
safe_seek(76);
ucdf_load_msat_(ctx, 109, &idx);
/* load further msat blocks */
next = ctx->msat_first;
for(n = 0; n < ctx->msat_len; n++) {
safe_seek(sect_id2offs(ctx, next));
next = ucdf_load_msat(ctx, id_per_sect, &idx);
if (next < 0) {
n++; /* acknowledge this block of msat readed */
break;
}
}
if ((n != ctx->msat_len) || (next != UCDF_SECT_EOC))
error(UCDF_ERR_BAD_MSAT);
/* load and build the sat */
ctx->sat = malloc(sizeof(long) * ctx->sat_len * id_per_sect);
idx = 0;
for(n = 0; n < ctx->sat_len; n++) {
next = ctx->msat[n];
if (next < 0)
error(UCDF_ERR_BAD_SAT);
safe_seek(sect_id2offs(ctx, next));
if (ucdf_load_any_sat(ctx, ctx->sat, &idx) != 0)
return -1;
}
/* load and build the short sat */
ctx->ssat = calloc(sizeof(long) * ctx->ssat_len * id_per_sect, 1);
next = ctx->ssat_first;
idx = 0;
for(n = 0; n < ctx->ssat_len; n++) {
/* printf("ssat next=%ld seek=%ld\n", next, sect_id2offs(ctx, next));*/
safe_seek(sect_id2offs(ctx, next));
if (ucdf_load_any_sat(ctx, ctx->ssat, &idx) != 0)
return -1;
next = ctx->sat[next];
}
if (next != UCDF_SECT_EOC)
error(UCDF_ERR_BAD_SSAT);
return 0;
}
/* Lazy convert utf16 to ASCII, keeping the low byte */
static void utf16_to_ascii(char *dst, int dstlen, const unsigned char *src)
{
for(dstlen--; (*src != '\0') && (dstlen > 0); dstlen--, dst++, src+=2)
*dst = *src;
*dst = '\0';
}
/* context used for directory mapping */
typedef struct {
long num_dirsects, dirs_per_sect;
long *diroffs;
} dir_ctx_t;
/* Read a raw directory entry from ctx->f and allocate and link in a
digested directory entry somewhere under ctx->root. Return 0 on success. */
static int ucdf_read_dir(ucdf_ctx_t *ctx, dir_ctx_t *dctx, long dirid, ucdf_direntry_t *parent, ucdf_direntry_t **de_out)
{
long page = dirid / dctx->dirs_per_sect;
long offs = dirid % dctx->dirs_per_sect;
long leftid, rightid, rootid;
unsigned char buf[128];
ucdf_direntry_t *de;
/* navigate to the record */
if ((page < 0) || (page >= dctx->num_dirsects))
error(UCDF_ERR_BAD_DIRCHAIN);
safe_seek(dctx->diroffs[page] + offs * 128);
safe_read(buf, 128);
/* read and parse */
de = malloc(sizeof(ucdf_direntry_t));
utf16_to_ascii(de->name, sizeof(de->name), buf);
de->size = load_int(ctx, buf+120, 4);
de->is_short = de->size < ctx->long_stream_min_size;
leftid = load_int(ctx, buf+68, 4);
rightid = load_int(ctx, buf+72, 4);
rootid = load_int(ctx, buf+76, 4);
de->first = load_int(ctx, buf+116, 4);
de->type = buf[66];
de->parent = parent;
de->children = NULL;
/* link in */
if (parent != NULL) {
de->next = parent->children;
parent->children = de;
}
else
de->next = NULL;
if (de_out != NULL)
*de_out = de;
/* printf("dir: %d:'%s' (%ld %ld r=%ld) sec=%ld\n", de->type, de->name, leftid, rightid, rootid, de->first);*/
/* read children */
if (leftid >= 0)
ucdf_read_dir(ctx, dctx, leftid, de->parent, NULL);
if (rightid >= 0)
ucdf_read_dir(ctx, dctx, rightid, de->parent, NULL);
if (rootid >= 0)
ucdf_read_dir(ctx, dctx, rootid, de, NULL);
return 0;
}
/* Read the directory tree; return 0 on success */
static int ucdf_read_dirs(ucdf_ctx_t *ctx)
{
int res;
long n, next;
dir_ctx_t dctx;
/* count directory sectors */
next = ctx->dir_first;
dctx.num_dirsects = 0;
dctx.dirs_per_sect = ctx->sect_size >> 7;
while(next >= 0) {
next = ctx->sat[next];
dctx.num_dirsects++;
}
if (next != UCDF_SECT_EOC)
error(UCDF_ERR_BAD_DIRCHAIN);
/* build a temporary offset list of dir pages */
dctx.diroffs = malloc(sizeof(long) * dctx.num_dirsects);
if (dctx.diroffs == NULL)
error(UCDF_ERR_BAD_MALLOC);
next = ctx->dir_first;
n = 0;
while(next >= 0) {
dctx.diroffs[n] = sect_id2offs(ctx, next);
next = ctx->sat[next];
n++;
}
/* start reading from the root dir */
res = ucdf_read_dir(ctx, &dctx, 0, NULL, &ctx->root);
free(dctx.diroffs);
return res;
}
/* Set up the virtual long file that holds short sector data */
static int ucdf_setup_ssd(ucdf_ctx_t *ctx)
{
long id_per_sect = ctx->sect_size >> 2;
if (ctx->root->type != UCDF_DE_ROOT)
error(UCDF_ERR_BAD_DIRCHAIN);
strcpy(ctx->ssd_de.name, "__short_sector_data__");
ctx->ssd_de.type = UCDF_DE_FILE;
ctx->ssd_de.size = ctx->ssat_len * id_per_sect * ctx->short_sect_size;
ctx->ssd_de.is_short = 0;
ctx->ssd_de.first = ctx->root->first;
ctx->ssd_de.parent = ctx->ssd_de.next = ctx->ssd_de.children = NULL;
ctx->ssd_f.ctx = ctx;
ctx->ssd_f.de = &ctx->ssd_de;
ctx->ssd_f.stream_offs = ctx->ssd_f.sect_id = ctx->ssd_f.sect_offs = 0;
return 0;
}
static int ucdf_open_(ucdf_ctx_t *ctx, const char *path)
{
ctx->f = fopen(path, "rb");
if (ctx->f == NULL) {
ctx->error = UCDF_ERR_OPEN;
return -1;
}
if (ucdf_read_hdr(ctx) != 0) {
fclose(ctx->f);
return -1;
}
return 0;
}
int ucdf_test_parse(const char *path)
{
ucdf_ctx_t ctx;
if (ucdf_open_(&ctx, path) == 0) {
fclose(ctx.f);
return 0;
}
return -1;
}
int ucdf_open(ucdf_ctx_t *ctx, const char *path)
{
if (ucdf_open_(ctx, path) != 0)
return -1;
if (ucdf_read_sats(ctx) != 0)
goto error;
if (ucdf_read_dirs(ctx) != 0)
goto error;
if (ucdf_setup_ssd(ctx) != 0)
goto error;
return 0;
error:;
fclose(ctx->f);
ctx->f = NULL;
return -1;
}
static void ucdf_free_dir(ucdf_direntry_t *dir)
{
ucdf_direntry_t *d, *next;
for(d = dir->children; d != NULL; d = next) {
next = d->next;
ucdf_free_dir(d);
}
free(dir);
}
void ucdf_close(ucdf_ctx_t *ctx)
{
if (ctx->root != NULL) {
ucdf_free_dir(ctx->root);
ctx->root = NULL;
}
if (ctx->f != NULL) {
fclose(ctx->f);
ctx->f = NULL;
}
if (ctx->msat != NULL) {
free(ctx->msat);
ctx->msat = NULL;
}
if (ctx->sat != NULL) {
free(ctx->sat);
ctx->sat = NULL;
}
if (ctx->ssat != NULL) {
free(ctx->ssat);
ctx->ssat = NULL;
}
}
int ucdf_fopen(ucdf_ctx_t *ctx, ucdf_file_t *fp, ucdf_direntry_t *de)
{
if (de->type != UCDF_DE_FILE)
return -1;
memset(fp, 0, sizeof(ucdf_file_t));
fp->ctx = ctx;
fp->de = de;
fp->sect_id = de->first;
return 0;
}
/* Read a chunk from a short file. Returns bytes read or 0 on EOF or -1 on error. */
static long ucdf_fread_short(ucdf_file_t *fp, char *dst, long len)
{
ucdf_ctx_t *ctx = fp->ctx;
long got = 0;
while(len > 0) {
long l, sect_remaining, file_remaining;
if ((fp->sect_id < 0) || (fp->stream_offs >= fp->de->size))
break;
/* read chunk from current short sector */
sect_remaining = ctx->short_sect_size - fp->sect_offs;
file_remaining = fp->de->size - fp->stream_offs;
l = (len < sect_remaining) ? len : sect_remaining;
l = (l < file_remaining) ? l : file_remaining;
if (ucdf_fseek(&ctx->ssd_f, fp->sect_id * ctx->short_sect_size + fp->sect_offs) != 0)
return -1;
if (ucdf_fread(&ctx->ssd_f, dst, l) != l)
return -1;
got += l;
dst += l;
len -= l;
fp->sect_offs += l;
fp->stream_offs += l;
/* move to next sector if needed */
if (fp->sect_offs == ctx->short_sect_size) {
fp->sect_offs = 0;
fp->sect_id = ctx->ssat[fp->sect_id];
}
}
return got;
}
/* Read a chunk from a long file. Returns bytes read or 0 on EOF or -1 on error. */
long ucdf_fread_long(ucdf_file_t *fp, char *dst, long len)
{
ucdf_ctx_t *ctx = fp->ctx;
long got = 0;
while(len > 0) {
long l, sect_remaining, file_remaining;
if ((fp->sect_id < 0) || (fp->stream_offs >= fp->de->size))
break;
/* read chunk from current sector */
sect_remaining = ctx->sect_size - fp->sect_offs;
file_remaining = fp->de->size - fp->stream_offs;
l = (len < sect_remaining) ? len : sect_remaining;
l = (l < file_remaining) ? l : file_remaining;
safe_seek(sect_id2offs(ctx, fp->sect_id) + fp->sect_offs);
safe_read(dst, l);
got += l;
dst += l;
len -= l;
fp->sect_offs += l;
fp->stream_offs += l;
/* move to next sector if needed */
if (fp->sect_offs == ctx->sect_size) {
fp->sect_offs = 0;
fp->sect_id = ctx->sat[fp->sect_id];
}
}
return got;
}
long ucdf_fread(ucdf_file_t *fp, char *dst, long len)
{
if (fp->de->is_short) return ucdf_fread_short(fp, dst, len);
return ucdf_fread_long(fp, dst, len);
}
int ucdf_fseek(ucdf_file_t *fp, long offs)
{
ucdf_ctx_t *ctx = fp->ctx;
long sect_min, sect_max;
long sect_idx, sect_offs, sect_id, n;
if (fp->de->is_short)
return -1;
if (offs == fp->stream_offs)
return 0; /* no need to move */
if ((offs < 0) || (offs >= fp->de->size))
return -1; /* do not seek out of the file */
/* fast lane: check if seek is within the current sector */
sect_min = fp->stream_offs - fp->sect_offs;
sect_max = sect_min + ctx->sect_size;
if ((offs >= sect_min) && (offs < sect_max)) {
long delta = offs - fp->stream_offs;
fp->stream_offs += delta;
fp->sect_offs += delta;
return 0;
}
/* seeking to another sector; since SAT is a singly linked list, we need
to recalculate this sector by sector */
sect_idx = offs / ctx->sect_size;
sect_offs = offs % ctx->sect_size;
sect_id = fp->de->first;
for(n = 0; n < sect_idx; n++) {
if (sect_id < 0)
return -1;
sect_id = ctx->sat[sect_id];
}
if (sect_id < 0)
return -1;
fp->stream_offs = offs;
fp->sect_offs = sect_offs;
fp->sect_id = sect_id;
return 0;
}
|