]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbx.c
liblbx: Rename lbx_stat to lbx_file_stat.
[liblbx.git] / src / lbx.c
index ef4589d5c61440df89205b4cff17a933d9dc57f4..0e26315fc8652acdc922a7f526140d5288463ab9 100644 (file)
--- 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
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdint.h>
 #include <errno.h>
 #include <assert.h>
 
-#include <sys/stat.h>
-#include <sys/mman.h>
-
-#include "byteorder.h"
+#include "pack.h"
 #include "misc.h"
 #include "lbx.h"
 
-#define LBX_MAGIC 0x0000fead
+#define LBX_MAGIC    0x0000fead
+#define LBX_HDR_SIZE 8
 
 int lbx_errno = 0;
 
 struct lbx_state {
-       const char *name;
+       char *name;
 
-       unsigned char *mem;
-       size_t memsize;
-       FILE *f;
-       long foff;
+       const struct lbx_file_ops *fops;
+       int (*dtor)(void *handle);
+       void *f;
 
-       uint16_t nfiles;
-       uint32_t offsets[];
-};
+       struct lbx_file_state *last_file;
 
-struct lbx_state *lbx_fopen(FILE *f, const char *name)
-{
-       struct lbx_state *new = NULL;
-       uint16_t nfiles, version;
-       uint32_t magic;
+       unsigned short nfiles;
+       unsigned long offsets[];
+};
 
-       if (fread(&nfiles,  sizeof nfiles,  1, f) != 1) goto readerr;
-       if (fread(&magic,   sizeof magic,   1, f) != 1) goto readerr;
-       if (fread(&version, sizeof version, 1, f) != 1) goto readerr;
+struct lbx_file_state {
+       unsigned long base, limit, offset;
+       struct lbx_state *lbx;
+       int eof;
+};
 
-       nfiles  = letohs(nfiles);
-       magic   = letohl(magic);
-       version = letohs(version);
+static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
+{
+       unsigned short nfiles  = unpack_16_le(hdr+0);
+       unsigned long  magic   = unpack_32_le(hdr+2);
+       unsigned short version = unpack_16_le(hdr+6);
+       struct lbx_state *lbx;
 
        if (magic != LBX_MAGIC) {
-               lbx_errno = LBX_EMAGIC;
+               lbx_errno = -LBX_EMAGIC;
                return NULL;
        }
-       
-       new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
-       if (!new) {
+
+       lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
+       if (!lbx) {
                lbx_errno = -errno;
                return NULL;
        }
-       
-       *new = (struct lbx_state){
-               .name   = name,
+
+       *lbx = (struct lbx_state) {
                .nfiles = nfiles,
-               .f      = f,
-               .foff   = sizeof nfiles + sizeof magic + sizeof version,
        };
-       
-       if (fread(new->offsets, sizeof *new->offsets, nfiles+1, f) != nfiles+1)
-               goto readerr;
-       new->foff += sizeof *new->offsets * (nfiles+1);
-
-       return new;
-readerr:
-       if (feof(f)) {
-               lbx_errno = LBX_EEOF;
-       } else {
+
+       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 = NULL;
+       char *dupname = NULL;
+
+       dupname = str_dup(name);
+       if (!dupname) {
                lbx_errno = -errno;
+               goto err;
        }
 
-       free(new);
+       if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
+               lbx_errno = -errno;
+               goto err;
+       }
+
+       lbx = lbx_init(hdr_buf);
+       if (!lbx)
+               goto err;
+
+       lbx->name = dupname;
+       lbx->dtor = destructor;
+       lbx->fops = fops;
+       lbx->f    = f;
+
+       for (unsigned i = 0; i <= lbx->nfiles; i++) {
+               unsigned char buf[4];
+
+               if (fops->read(buf, sizeof buf, f) != sizeof buf) {
+                       lbx_errno = -errno;
+                       if (fops->eof(f))
+                               lbx_errno = LBX_EEOF;
+                       goto err;
+               }
+
+               lbx->offsets[i] = unpack_32_le(buf);
+       }
+
+       return lbx;
+err:
+       free(dupname);
+       free(lbx);
        return NULL;
 }
 
-static int _lbx_memcpy(void *dest, struct lbx_state *src, size_t size)
+static int file_close(void *f)
 {
-       if (src->foff + size > src->memsize)
-               return -1;
-       memcpy(dest, src->mem + src->foff, size);
-       src->foff += size;
-       return 0;
+       return fclose((FILE *)f);
 }
 
-struct lbx_state *lbx_mopen(void *_mem, size_t size, const char *name)
+static int pipe_close(void *f)
 {
-       struct lbx_state *new = NULL;
-       struct lbx_state tmp = { .mem = _mem, .memsize = size };
-       uint16_t nfiles, version;
-       uint32_t magic;
+       struct lbx_pipe_state *p = f;
+       int rc;
 
-       if (_lbx_memcpy(&nfiles,  &tmp, sizeof nfiles)  == -1) goto eof;
-       if (_lbx_memcpy(&magic,   &tmp, sizeof magic)   == -1) goto eof;
-       if (_lbx_memcpy(&version, &tmp, sizeof version) == -1) goto eof;
+       rc = fclose(p->f);
+       free(p);
+       return rc;
+}
 
-       nfiles  = letohs(nfiles);
-       magic   = letohl(magic);
-       version = letohs(version);
+static char *last_component(const char *name)
+{
+       char *c;
 
-       if (magic != LBX_MAGIC) {
-               lbx_errno = LBX_EMAGIC;
-               return NULL;
-       }
-       
-       new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
-       if (!new) {
-               lbx_errno = -errno;
-               return NULL;
-       }
-       
-       *new = (struct lbx_state){
-               .name    = name,
-               .nfiles  = nfiles,
-               .mem     = _mem,
-               .memsize = size,
-               .foff    = tmp.foff,
-       };
-       
-       if (_lbx_memcpy(new->offsets, new, (nfiles+1)*(sizeof *new->offsets)))
-               goto eof;
-
-       return new;
-eof:
-       free(new);
-       lbx_errno = LBX_EEOF;
-       return NULL;
+       /* TODO: Handle other path separators. */
+       c = strrchr(name, '/');
+       if (!c)
+               return (char *)name;
+       return c+1;
 }
 
-struct lbx_state *lbx_open(const char *path)
+struct lbx_state *lbx_fopen(const char *file)
 {
-       struct lbx_state *new = NULL;
+       const char *name = last_component(file);
+       struct lbx_pipe_state *p;
        FILE *f;
-       
-       if ((f = fopen(path, "rb"))) {
-               const char *name = strrchr(path, '/');
-               new = lbx_fopen(f, name ? name+1 : path);
-       } else {
-               lbx_errno = -errno;
+
+       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;
        }
 
-       return new;
+       *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)
@@ -171,133 +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.%03d", 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_mextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
+int lbx_close(struct lbx_state *lbx)
 {
-       size_t rc;
+       int rc = 0;
 
-       assert(lbx->mem);
-       assert(base + len <= lbx->memsize);
-
-       rc = fwrite(lbx->mem + base, 1, len, of);
-       if (rc < len)
-               lbx_errno = -errno;
+       if (lbx && lbx->dtor)
+               rc = lbx->dtor(lbx->f);
+       free(lbx->name);
+       free(lbx);
 
        return rc;
 }
 
-static size_t
-_lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
+struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
 {
-       unsigned char buf[1024];
-       size_t rc, written = 0;
-
-       assert(lbx->f);
-
-       if (_lbx_fseek(lbx->f, &lbx->foff, base) == -1)
-               return 0;
-       
-       while (len) {
-               size_t amt = MIN(len, sizeof buf);
-
-               rc = fread(buf, 1, amt, lbx->f);
-               lbx->foff += rc;
-               len -= rc;
-               if (rc < amt) {
-                       if (feof(lbx->f)) lbx_errno = LBX_EEOF;
-                       else lbx_errno = -errno;
-                       break;
-               }
+       struct lbx_file_state *state;
 
-               rc = fwrite(buf, 1, amt, of);
-               written += rc;
-               if (rc < amt) {
-                       lbx_errno = -errno;
-                       break;
-               }
+       if (fileno >= lbx->nfiles) {
+               lbx_errno = LBX_ERANGE;
+               return NULL;
        }
 
-       return written;
+       lbx->last_file = NULL;
+       if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
+               lbx_errno = -errno;
+               return NULL;
+       }
+
+       state = malloc(sizeof *state);
+       if (!state) {
+               lbx_errno = -errno;
+               return NULL;
+       }
+
+       *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 (index >= lbx->nfiles) {
-               lbx_errno = LBX_ERANGE;
-               return 0;
+       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;
        }
-       
-       base = lbx->offsets[index];
-       len  = lbx->offsets[index+1] - lbx->offsets[index];
 
-       if (lbx->mem)
-               return _lbx_mextract(lbx, base, len, of);
-       return _lbx_fextract(lbx, base, len, of);
+       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;
 }
 
-void *lbx_mmap(struct lbx_state *lbx, size_t index, size_t *len)
+int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
 {
-       unsigned char *mapping;
-       struct stat statbuf;
-       size_t base;
-
-       if (index >= lbx->nfiles) {
-               lbx_errno = LBX_ERANGE;
-               return NULL;
-       }
-       
-       base = lbx->offsets[index];
-       *len = lbx->offsets[index+1] - lbx->offsets[index];
-
-       if (lbx->mem)
-               return lbx->mem + base;
-       
-       if (fstat(fileno(lbx->f), &statbuf) == -1) {
-               lbx_errno = -errno;
-               return NULL;
+       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;
        }
 
-       mapping = mmap(NULL, statbuf.st_size, PROT_READ, 0, fileno(lbx->f), 0);
-       if (mapping == MAP_FAILED) {
-               lbx_errno = -errno;
-               return NULL;
-       }
+       if (pos > f->limit)
+               return -1;
 
-       lbx->mem     = mapping;
-       lbx->memsize = statbuf.st_size;
-       return mapping + base;
+       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;
 }
 
-void lbx_close(struct lbx_state *lbx)
+long lbx_file_tell(struct lbx_file_state *f)
 {
-       if (!lbx) return;
+       return f->offset;
+}
 
-       if (lbx->f) {
-               fclose(lbx->f);
-               if (lbx->mem) {
-                       munmap(lbx->mem, lbx->memsize);
-               }
-       }
+int lbx_file_eof(struct lbx_file_state *f)
+{
+       return f->eof;
+}
 
-       free(lbx);
+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)