10 #define LBX_MAGIC 0x0000fead
24 struct lbx_state *lbx_fopen(FILE *f, const char *name)
26 struct lbx_state *new = NULL;
27 uint16_t nfiles, version;
30 if (fread(&nfiles, sizeof nfiles, 1, f) != 1) goto readerr;
31 if (fread(&magic, sizeof magic, 1, f) != 1) goto readerr;
32 if (fread(&version, sizeof version, 1, f) != 1) goto readerr;
34 nfiles = letohs(nfiles);
35 magic = letohl(magic);
36 version = letohs(version);
38 if (magic != LBX_MAGIC) {
39 lbx_errno = LBX_EMAGIC;
43 new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
49 *new = (struct lbx_state){
53 .foff = sizeof nfiles + sizeof magic + sizeof version,
56 if (fread(new->offsets, sizeof *new->offsets, nfiles+1, f) != nfiles+1)
58 new->foff += sizeof *new->offsets * (nfiles+1);
72 struct lbx_state *lbx_open(const char *path)
74 struct lbx_state *new = NULL;
77 if ((f = fopen(path, "rb"))) {
78 const char *name = strrchr(path, '/');
79 new = lbx_fopen(f, name ? name+1 : path);
87 size_t lbx_numfiles(struct lbx_state *lbx)
92 void lbx_close(struct lbx_state *lbx)
100 const char *lbx_strerror(void)
103 return strerror(-lbx_errno);
106 case LBX_ESUCCESS: return "Success";
107 case LBX_EMAGIC: return "Bad magic number";
108 case LBX_EEOF: return "Unexpected end-of-file";
111 return "Unknown error";