]> git.draconx.ca Git - upkg.git/blobdiff - src/libupkg.c
Begin work on Engine.Texture.
[upkg.git] / src / libupkg.c
index 0dc2adeca76aab05336acddff6e655197c272044..4bb13a3fa7b0f85c55197ead75af52ebc9f77795 100644 (file)
@@ -124,6 +124,34 @@ static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
        return &alloc->pkg;
 }
 
+static int pkg_init_guid(struct upkg *pkg)
+{
+       size_t rc;
+
+       if (pkg->version < 68) {
+               unsigned long heritage_count, heritage_offset;
+               unsigned char buf[8];
+
+               rc = fread(buf, 1, sizeof buf, pkg->priv->f);
+               if (rc < 8)
+                       return -1;
+
+               heritage_count  = unpack_32_le(buf+0);
+               heritage_offset = unpack_32_le(buf+4);
+
+               if (heritage_count == 0)
+                       return -1;
+               if (fseek(pkg->priv->f, heritage_offset, SEEK_SET) != 0)
+                       return -1;
+       }
+
+       rc = fread(pkg->guid, 1, 16, pkg->priv->f);
+       if (rc < 16)
+               return -1;
+
+       return 0;
+}
+
 static int pkg_init_names(struct upkg *pkg)
 {
        size_t rc, len, nbuf = 0;
@@ -335,6 +363,10 @@ struct upkg *upkg_fopen(const char *path)
        }
        pkg->priv->f = f;
 
+       if (pkg_init_guid(pkg) != 0) {
+               goto err2;
+       }
+
        if (pkg_init_names(pkg) != 0) {
                goto err2;
        }
@@ -403,6 +435,7 @@ struct upkg_file *upkg_export_open(struct upkg *pkg, unsigned long idx)
                .pkg  = pkg,
                .base = pkg->priv->exports[idx].offset,
                .len  = pkg->priv->exports[idx].size,
+               .name = pkg->priv->exports[idx].name,
        };
 
        return f;
@@ -415,6 +448,43 @@ void upkg_export_close(struct upkg_file *f)
        free(f);
 }
 
+long upkg_export_tell(struct upkg_file *f)
+{
+       return f->offset;
+}
+
+int upkg_export_seek(struct upkg_file *f, long offset, int whence)
+{
+       int rc = EOF;
+
+       switch (whence) {
+       case SEEK_CUR:
+               offset = f->offset + offset;
+       case SEEK_SET:
+               if (offset < 0 || offset > f->len)
+                       return EOF;
+               rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
+               break;
+       case SEEK_END:
+               offset = -offset;
+               if (offset < 0 || offset > f->len)
+                       return EOF;
+               offset = f->len - offset;
+               rc = fseek(f->pkg->priv->f, f->base + offset, SEEK_SET);
+               break;
+       }
+
+       if (rc == 0) {
+               f->pkg->priv->last_file = f;
+               f->offset = offset;
+               f->eof = 0;
+       } else if (f->pkg->priv->last_file == f) {
+               f->pkg->priv->last_file = NULL;
+       }
+
+       return rc;
+}
+
 size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
 {
        size_t want = MIN(n, f->len - f->offset);
@@ -431,5 +501,8 @@ size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n)
 
        rc = fread(buf, 1, want, f->pkg->priv->f);
        f->offset += rc;
+
+       if (want < n || rc < want && feof(f->pkg->priv->f))
+               f->eof = 1;
        return rc;
 }