#include #include #include #include "byteorder.h" #include "lbx.h" #define LBX_MAGIC 0x0000fead struct lbx_state { FILE *f; uint16_t nfiles; uint32_t offsets[]; }; struct lbx_state *lbx_fopen(FILE *f) { struct lbx_state *new = NULL; uint16_t nfiles, version; uint32_t magic; if (fread(&nfiles, sizeof nfiles, 1, f) != 1) return NULL; if (fread(&magic, sizeof magic, 1, f) != 1) return NULL; if (fread(&version, sizeof version, 1, f) != 1) return NULL; nfiles = letohs(nfiles); magic = letohl(magic); version = letohs(version); if (magic != LBX_MAGIC) return NULL; new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets)); if (!new) return NULL; *new = (struct lbx_state){ .nfiles = nfiles, .f = f, }; if (fread(new->offsets, sizeof *new->offsets, nfiles+1, f) != nfiles+1) return free(new), NULL; return new; } struct lbx_state *lbx_open(const char *path) { struct lbx_state *new = NULL; FILE *f; if ((f = fopen(path, "rb"))) new = lbx_fopen(f); return new; } void lbx_close(struct lbx_state *lbx) { fclose(lbx->f); free(lbx); }