From b42e3c4eadf05be2506e1a1f8deb2cf24064c18b Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 22 Dec 2007 14:02:40 -0500 Subject: [PATCH] Initial commit --- .gitignore | 22 +++++++++++++++++ Makefile.am | 1 + configure.ac | 18 ++++++++++++++ src/.gitignore | 7 ++++++ src/Makefile.am | 3 +++ src/byteorder.c | 20 ++++++++++++++++ src/byteorder.h | 16 +++++++++++++ src/lbx.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lbx.h | 10 ++++++++ 9 files changed, 160 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile.am create mode 100644 configure.ac create mode 100644 src/.gitignore create mode 100644 src/Makefile.am create mode 100644 src/byteorder.c create mode 100644 src/byteorder.h create mode 100644 src/lbx.c create mode 100644 src/lbx.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a39608 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +config.status +config.cache +config.guess +config.log +config.sub +config.h.in +config.h +configure + +Makefile.in +Makefile + +libtool +ltmain.sh + +aclocal.m4 +autom4te.cache + +missing +depcomp +install-sh +stamp-h1 diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..af437a6 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..b3bd60c --- /dev/null +++ b/configure.ac @@ -0,0 +1,18 @@ +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 diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..f299a28 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,7 @@ +Makefile +Makefile.in +.deps +.libs +*.o +*.lo +*.la diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..0292af7 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,3 @@ +lib_LTLIBRARIES = liblbx.la + +liblbx_la_SOURCES = lbx.c diff --git a/src/byteorder.c b/src/byteorder.c new file mode 100644 index 0000000..76340a4 --- /dev/null +++ b/src/byteorder.c @@ -0,0 +1,20 @@ +#include +#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]; +} diff --git a/src/byteorder.h b/src/byteorder.h new file mode 100644 index 0000000..c997b10 --- /dev/null +++ b/src/byteorder.h @@ -0,0 +1,16 @@ +#ifndef BYTEORDER_H_ +#define BYTEORDER_H_ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef WORDS_BIGENDIAN + uint16_t letohs(uint16_t); + uint32_t letohl(uint32_t); +#else +# define letohs(x) (x) +# define letohl(x) (x) +#endif + +#endif diff --git a/src/lbx.c b/src/lbx.c new file mode 100644 index 0000000..481123b --- /dev/null +++ b/src/lbx.c @@ -0,0 +1,63 @@ +#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); +} diff --git a/src/lbx.h b/src/lbx.h new file mode 100644 index 0000000..4232b9f --- /dev/null +++ b/src/lbx.h @@ -0,0 +1,10 @@ +#ifndef LBX_H_ +#define LBX_H_ + +typedef struct lbx_state LBX; + +LBX *lbx_fopen(FILE *); +LBX *lbx_open(const char *); +void lbx_close(LBX *); + +#endif -- 2.43.2