X-Git-Url: https://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/a6ae4dd126e7b2b678e0db31b68bba51311282cc..c02dcc9610e66ebe6154e60dcd2f109c90eb09df:/src/lbx.c diff --git a/src/lbx.c b/src/lbx.c index 4b5d7f3..0e26315 100644 --- a/src/lbx.c +++ b/src/lbx.c @@ -1,7 +1,7 @@ /* * 2ooM: The Master of Orion II Reverse Engineering Project * Library for working with LBX archive files. - * Copyright (C) 2006-2008 Nick Bowler + * Copyright (C) 2006-2010 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,13 +23,9 @@ #include #include #include -#include #include #include -#include -#include - #include "pack.h" #include "misc.h" #include "lbx.h" @@ -40,14 +36,22 @@ int lbx_errno = 0; struct lbx_state { - const char *name; + char *name; const struct lbx_file_ops *fops; int (*dtor)(void *handle); void *f; - uint16_t nfiles; - uint32_t offsets[]; + struct lbx_file_state *last_file; + + unsigned short nfiles; + unsigned long offsets[]; +}; + +struct lbx_file_state { + unsigned long base, limit, offset; + struct lbx_state *lbx; + int eof; }; static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE]) @@ -75,23 +79,40 @@ static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE]) return lbx; } +static char *str_dup(const char *s) +{ + char *buf; + + buf = malloc(strlen(s)+1); + if (buf) + strcpy(buf, s); + return buf; +} + struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops, int (*destructor)(void *), const char *name) { unsigned char hdr_buf[LBX_HDR_SIZE]; - struct lbx_state *lbx; + struct lbx_state *lbx = NULL; + char *dupname = NULL; + + dupname = str_dup(name); + if (!dupname) { + lbx_errno = -errno; + goto err; + } if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) { lbx_errno = -errno; - return NULL; + goto err; } lbx = lbx_init(hdr_buf); if (!lbx) - return NULL; + goto err; + lbx->name = dupname; lbx->dtor = destructor; - lbx->name = name; lbx->fops = fops; lbx->f = f; @@ -102,19 +123,66 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops, lbx_errno = -errno; if (fops->eof(f)) lbx_errno = LBX_EEOF; - free(lbx); - return NULL; + goto err; } lbx->offsets[i] = unpack_32_le(buf); } return lbx; +err: + free(dupname); + free(lbx); + return NULL; } -struct lbx_state *lbx_fopen(FILE *f, const char *name) +static int file_close(void *f) { - return lbx_open(f, &lbx_default_fops, NULL, name); + return fclose((FILE *)f); +} + +static int pipe_close(void *f) +{ + struct lbx_pipe_state *p = f; + int rc; + + rc = fclose(p->f); + free(p); + return rc; +} + +static char *last_component(const char *name) +{ + char *c; + + /* TODO: Handle other path separators. */ + c = strrchr(name, '/'); + if (!c) + return (char *)name; + return c+1; +} + +struct lbx_state *lbx_fopen(const char *file) +{ + const char *name = last_component(file); + struct lbx_pipe_state *p; + FILE *f; + + f = fopen(file, "rb"); + if (!f) + return NULL; + + if (fseek(f, 0, SEEK_CUR) == 0) + return lbx_open(f, &lbx_default_fops, file_close, name); + + p = malloc(sizeof *p); + if (!p) { + fclose(f); + return NULL; + } + + *p = (struct lbx_pipe_state) { .f = f }; + return lbx_open(p, &lbx_pipe_fops, pipe_close, name); } size_t lbx_numfiles(struct lbx_state *lbx) @@ -122,80 +190,133 @@ size_t lbx_numfiles(struct lbx_state *lbx) return lbx->nfiles; } -int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf) +int +lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf) { static char str[256]; /* FIXME */ - if (index >= lbx->nfiles) { + if (fileno >= lbx->nfiles) { buf->name = NULL; lbx_errno = LBX_ERANGE; return -1; } - snprintf(str, sizeof str, "%s.%03zu", lbx->name, index); + snprintf(str, sizeof str, "%s.%03u", lbx->name, fileno); buf->name = str; - buf->size = lbx->offsets[index+1] - lbx->offsets[index]; + buf->size = lbx->offsets[fileno+1] - lbx->offsets[fileno]; return 0; } -static size_t -_lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of) +int lbx_close(struct lbx_state *lbx) { - unsigned char buf[1024]; - size_t rc, written = 0; + int rc = 0; - assert(lbx->f); + if (lbx && lbx->dtor) + rc = lbx->dtor(lbx->f); + free(lbx->name); + free(lbx); - if (lbx->fops->seek(lbx->f, base, SEEK_SET) != 0) { + return rc; +} + +struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno) +{ + struct lbx_file_state *state; + + if (fileno >= lbx->nfiles) { + lbx_errno = LBX_ERANGE; + return NULL; + } + + lbx->last_file = NULL; + if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) { lbx_errno = -errno; - return 0; + return NULL; } - - while (len) { - size_t amt = MIN(len, sizeof buf); - - rc = lbx->fops->read(buf, amt, lbx->f); - len -= rc; - if (rc < amt) { - if (lbx->fops->eof(lbx->f)) lbx_errno = LBX_EEOF; - else lbx_errno = -errno; - break; - } - rc = fwrite(buf, 1, amt, of); - written += rc; - if (rc < amt) { - lbx_errno = -errno; - break; - } + state = malloc(sizeof *state); + if (!state) { + lbx_errno = -errno; + return NULL; } - return written; + *state = (struct lbx_file_state) { + .base = lbx->offsets[fileno], + .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno], + .lbx = lbx, + }; + + lbx->last_file = state; + return state; } -size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of) +size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n) { - size_t base, len; + const struct lbx_file_ops *fops = f->lbx->fops; + size_t want = MIN(n, f->limit - f->offset); + size_t rc; + + if (f != f->lbx->last_file) { + f->lbx->last_file = NULL; + if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0) + return 0; + f->lbx->last_file = f; + } - if (index >= lbx->nfiles) { - lbx_errno = LBX_ERANGE; - return 0; + rc = fops->read(buf, want, f->lbx->f); + f->offset += rc; + + if (want < n || (rc < want && fops->eof(f->lbx->f))) + f->eof = 1; + return rc; +} + +int lbx_file_seek(struct lbx_file_state *f, long offset, int whence) +{ + const struct lbx_file_ops *fops = f->lbx->fops; + unsigned long pos; + + switch (whence) { + case SEEK_CUR: + pos = f->offset + offset; + break; + case SEEK_SET: + pos = offset; + break; + case SEEK_END: + pos = f->limit + offset; + break; } - - base = lbx->offsets[index]; - len = lbx->offsets[index+1] - lbx->offsets[index]; - return _lbx_fextract(lbx, base, len, of); + + if (pos > f->limit) + return -1; + + f->lbx->last_file = NULL; + if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0) + return -1; + + f->offset = pos; + f->lbx->last_file = f; + f->eof = 0; + + return 0; } -int lbx_close(struct lbx_state *lbx) +long lbx_file_tell(struct lbx_file_state *f) { - int rc = 0; + return f->offset; +} - if (lbx && lbx->dtor) - rc = lbx->dtor(lbx->f); - free(lbx); +int lbx_file_eof(struct lbx_file_state *f) +{ + return f->eof; +} - return rc; +void lbx_file_close(struct lbx_file_state *f) +{ + if (f->lbx->last_file == f) + f->lbx->last_file = NULL; + free(f); } const char *lbx_strerror(void)