]> git.draconx.ca Git - liblbx.git/commitdiff
Initial commit
authorNick Bowler <draconx@gmail.com>
Sat, 22 Dec 2007 19:02:40 +0000 (14:02 -0500)
committerNick Bowler <draconx@gmail.com>
Sat, 22 Dec 2007 19:02:40 +0000 (14:02 -0500)
.gitignore [new file with mode: 0644]
Makefile.am [new file with mode: 0644]
configure.ac [new file with mode: 0644]
src/.gitignore [new file with mode: 0644]
src/Makefile.am [new file with mode: 0644]
src/byteorder.c [new file with mode: 0644]
src/byteorder.h [new file with mode: 0644]
src/lbx.c [new file with mode: 0644]
src/lbx.h [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..7a39608
--- /dev/null
@@ -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 (file)
index 0000000..af437a6
--- /dev/null
@@ -0,0 +1 @@
+SUBDIRS = src
diff --git a/configure.ac b/configure.ac
new file mode 100644 (file)
index 0000000..b3bd60c
--- /dev/null
@@ -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 (file)
index 0000000..f299a28
--- /dev/null
@@ -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 (file)
index 0000000..0292af7
--- /dev/null
@@ -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 (file)
index 0000000..76340a4
--- /dev/null
@@ -0,0 +1,20 @@
+#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];
+}
diff --git a/src/byteorder.h b/src/byteorder.h
new file mode 100644 (file)
index 0000000..c997b10
--- /dev/null
@@ -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 (file)
index 0000000..481123b
--- /dev/null
+++ b/src/lbx.c
@@ -0,0 +1,63 @@
+#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);
+}
diff --git a/src/lbx.h b/src/lbx.h
new file mode 100644 (file)
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