/*
* 2ooM: The Master of Orion II Reverse Engineering Project
* Library for working with LBX archive files.
- * Copyright (C) 2006-2010 Nick Bowler
+ * Copyright © 2006-2010, 2013-2014 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
#define LBX_MAGIC 0x0000fead
#define LBX_HDR_SIZE 8
-struct lbx_state {
+struct lbx_priv {
+ struct lbx pub;
+
char *name;
const struct lbx_file_ops *fops;
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;
+ struct lbx_priv *lbx;
int eof;
};
-static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
+static struct lbx_priv *lbx_init(unsigned char *hdr)
{
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;
+ struct lbx_priv *lbx;
if (magic != LBX_MAGIC) {
lbx_error_raise(LBX_EMAGIC);
return NULL;
}
- *lbx = (struct lbx_state) {
- .nfiles = nfiles,
+ *lbx = (struct lbx_priv) {
+ .pub = { .nfiles = nfiles, },
};
return lbx;
return buf;
}
-struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
- int (*destructor)(void *), const char *name)
+struct lbx *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;
+ struct lbx_priv *lbx = NULL;
char *dupname = NULL;
dupname = str_dup(name);
lbx->fops = fops;
lbx->f = f;
- for (unsigned i = 0; i <= lbx->nfiles; i++) {
+ for (unsigned i = 0; i <= lbx->pub.nfiles; i++) {
unsigned char buf[4];
if (fops->read(buf, sizeof buf, f) != sizeof buf) {
lbx->offsets[i] = unpack_32_le(buf);
}
- return lbx;
+ return &lbx->pub;
err:
free(dupname);
free(lbx);
return c+1;
}
-struct lbx_state *lbx_fopen(const char *file)
+struct lbx *lbx_fopen(const char *file)
{
const char *name = last_component(file);
struct lbx_pipe_state *p;
FILE *f;
f = fopen(file, "rb");
- if (!f)
+ if (!f) {
+ lbx_error_raise(-errno);
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) {
+ lbx_error_raise(LBX_ENOMEM);
fclose(f);
return NULL;
}
return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
}
-size_t lbx_numfiles(struct lbx_state *lbx)
-{
- return lbx->nfiles;
-}
-
-int
-lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
+int lbx_file_stat(struct lbx *pub, unsigned fileno, struct lbx_statbuf *buf)
{
+ struct lbx_priv *lbx = (struct lbx_priv *)pub;
static char str[256]; /* FIXME */
- if (fileno >= lbx->nfiles) {
- lbx_error_raise(LBX_ENOENT);
+ if (fileno >= lbx->pub.nfiles) {
+ lbx_error_raise(LBX_EINVAL);
buf->name = NULL;
return -1;
}
return 0;
}
-int lbx_close(struct lbx_state *lbx)
+int lbx_close(struct lbx *pub)
{
+ struct lbx_priv *lbx = (struct lbx_priv *)pub;
int rc = 0;
if (lbx && lbx->dtor)
return rc;
}
-struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
+struct lbx_file_state *lbx_file_open(struct lbx *pub, unsigned fileno)
{
+ struct lbx_priv *lbx = (struct lbx_priv *)pub;
struct lbx_file_state *state;
- if (fileno >= lbx->nfiles) {
- lbx_error_raise(LBX_ENOENT);
+ if (fileno >= lbx->pub.nfiles) {
+ lbx_error_raise(LBX_EINVAL);
return NULL;
}
rc = fops->read(buf, want, f->lbx->f);
f->offset += rc;
- if (want < n || (rc < want && fops->eof(f->lbx->f)))
+ if (rc < want) {
+ if (fops->eof(f->lbx->f))
+ lbx_error_raise(LBX_EEOF);
+ } else if (rc < n) {
f->eof = 1;
+ }
+
return rc;
}
case SEEK_END:
pos = f->limit + offset;
break;
+ default:
+ lbx_error_raise(LBX_EINVAL);
+ return -1;
}
- if (pos > f->limit)
+ if (pos > f->limit) {
+ lbx_error_raise(LBX_EINVAL);
return -1;
+ }
f->lbx->last_file = NULL;
if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)