]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbx.c
Update Makefile.am so that lbx.h is installed.
[liblbx.git] / src / lbx.c
index 6e7b9eef790af843107e4a316163895d404e9df9..8ddcf1860ec51b6665767cb0d9bfc6738a900a2a 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
@@ -3,17 +3,24 @@
 #include <string.h>
 #include <stdint.h>
 #include <errno.h>
+#include <assert.h>
+
+#include <sys/stat.h>
+#include <sys/mman.h>
 
 #include "byteorder.h"
 #include "lbx.h"
 
 #define LBX_MAGIC 0x0000fead
+#define MIN(a,b) (((a)<(b))?(a):(b))
 
 int lbx_errno = 0;
 
 struct lbx_state {
        const char *name;
 
+       unsigned char *mem;
+       size_t memsize;
        FILE *f;
        long foff;
 
@@ -69,6 +76,59 @@ readerr:
        return NULL;
 }
 
+static int _lbx_memcpy(void *dest, struct lbx_state *src, size_t size)
+{
+       if (src->foff + size > src->memsize)
+               return -1;
+       memcpy(dest, src->mem + src->foff, size);
+       src->foff += size;
+       return 0;
+}
+
+struct lbx_state *lbx_mopen(void *_mem, size_t size, const char *name)
+{
+       struct lbx_state *new = NULL;
+       struct lbx_state tmp = { .mem = _mem, .memsize = size };
+       uint16_t nfiles, version;
+       uint32_t magic;
+
+       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;
+
+       nfiles  = letohs(nfiles);
+       magic   = letohl(magic);
+       version = letohs(version);
+
+       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;
+}
+
 struct lbx_state *lbx_open(const char *path)
 {
        struct lbx_state *new = NULL;
@@ -89,11 +149,169 @@ 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)
+{
+       static char str[256]; /* FIXME */
+
+       if (index >= lbx->nfiles) {
+               buf->name = NULL;
+               lbx_errno = LBX_ERANGE;
+               return -1;
+       }
+
+       snprintf(str, sizeof str, "%s.%03d", lbx->name, index);
+       buf->name = str;
+       buf->size = lbx->offsets[index+1] - lbx->offsets[index];
+       return 0;
+}
+
+/* Advance to the beginning of the index'th file by either fseek or reading. */
+static int _lbx_fseek(struct lbx_state *lbx, size_t base)
+{
+       static unsigned char oblivion[1024];
+       long dist;
+
+       if (lbx->foff < base) {
+               dist = base - lbx->foff;
+       } else if (lbx->foff > base) {
+               dist = -(long)(lbx->foff - base);
+       } else {
+               return 0;
+       }
+
+       if (fseek(lbx->f, dist, SEEK_CUR) == 0) {
+               lbx->foff += dist;
+       } else if (lbx->foff < base) {
+               while (dist) {
+                       size_t rc, amt = MIN(sizeof oblivion, dist);
+                       rc = fread(oblivion, 1, amt, lbx->f);
+                       lbx->foff += rc;
+                       dist -= rc;
+                       if (rc < amt) {
+                               if (feof(lbx->f))
+                                       lbx_errno = LBX_EEOF;
+                               else
+                                       lbx_errno = -errno;
+                               return -1;
+                       }
+               }
+       } else {
+               lbx_errno = -errno;
+               return -1;
+       }
+       return 0;
+}
+
+static size_t
+_lbx_mextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
+{
+       size_t rc;
+
+       assert(lbx->mem);
+       assert(base + len <= lbx->memsize);
+
+       rc = fwrite(lbx->mem + base, 1, len, of);
+       if (rc < len)
+               lbx_errno = -errno;
+
+       return rc;
+}
+
+static size_t
+_lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
+{
+       unsigned char buf[1024];
+       size_t rc, written = 0;
+
+       assert(lbx->f);
+
+       if (_lbx_fseek(lbx, 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;
+               }
+
+               rc = fwrite(buf, 1, amt, of);
+               written += rc;
+               if (rc < amt) {
+                       lbx_errno = -errno;
+                       break;
+               }
+       }
+
+       return written;
+}
+
+size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of)
+{
+       size_t base, len;
+
+       if (index >= lbx->nfiles) {
+               lbx_errno = LBX_ERANGE;
+               return 0;
+       }
+       
+       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);
+}
+
+void *lbx_mmap(struct lbx_state *lbx, size_t index, size_t *len)
+{
+       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;
+       }
+
+       mapping = mmap(NULL, statbuf.st_size, PROT_READ, 0, fileno(lbx->f), 0);
+       if (mapping == MAP_FAILED) {
+               lbx_errno = -errno;
+               return NULL;
+       }
+
+       lbx->mem     = mapping;
+       lbx->memsize = statbuf.st_size;
+       return mapping + base;
+}
+
 void lbx_close(struct lbx_state *lbx)
 {
        if (!lbx) return;
 
-       fclose(lbx->f);
+       if (lbx->f) {
+               fclose(lbx->f);
+               if (lbx->mem) {
+                       munmap(lbx->mem, lbx->memsize);
+               }
+       }
+
        free(lbx);
 }
 
@@ -106,6 +324,7 @@ const char *lbx_strerror(void)
        case LBX_ESUCCESS: return "Success";
        case LBX_EMAGIC:   return "Bad magic number";
        case LBX_EEOF:     return "Unexpected end-of-file";
+       case LBX_ERANGE:   return "Index out of range";
        }
 
        return "Unknown error";