--- /dev/null
+AC_PREREQ(2.60)
+AC_INIT([liblbx], [0.1], [toom-devel@lists.sourceforge.net])
+AC_CONFIG_SRCDIR([src/lbx.c])
+AC_CONFIG_HEADER([config.h])
+
+AM_INIT_AUTOMAKE([-Wall -Werror foreign])
+
+AC_PROG_CC
+AC_C_BIGENDIAN
+
+AC_PROG_LIBTOOL
+
+AC_CONFIG_FILES([
+ Makefile
+ src/Makefile
+])
+
+AC_OUTPUT
--- /dev/null
+#include <stdint.h>
+#include "byteorder.h"
+
+uint16_t letohs(uint16_t val)
+{
+ uint16_t hostval;
+ int i;
+
+ for (i = 0; i < sizeof val; i++)
+ ((uint8_t *)&hostval)[i] = ((uint8_t *)&val)[sizeof val - i];
+}
+
+uint32_t letohl(uint32_t val)
+{
+ uint32_t hostval;
+ int i;
+
+ for (i = 0; i < sizeof val; i++)
+ ((uint8_t *)&hostval)[i] = ((uint8_t *)&val)[sizeof val - i];
+}
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#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);
+}