Commits

Jeremy Rossi committed 63c381e

more waf build stuff

  • Participants
  • Parent commits 217b72f

Comments (0)

Files changed (20)

src/analysisd/cdb/Makefile

-# Makefile for analysisd alerts
-# Daniel B. Cid <dcid@sourcefire.com>||<daniel.cid@gmail.com>
-# http://www.ossec.net/hids/
-
-PT=../../
-NAME=cdb
-
-include ../../Config.Make
-
-SRCS = cdb.c cdb_hash.c cdb_make.c uint32_pack.c uint32_unpack.c
-OBJS = cdb.o cdb_hash.o cdb_make.o uint32_pack.o uint32_unpack.o
-
-
-response:
-		$(CC) -I../ $(CFLAGS) -c $(SRCS)
-		ar cru cdb.a $(OBJS)
-		ar cru cdb_make.a $(OBJS)
-		ranlib cdb.a
-		ranlib cdb_make.a
-
-clean:
-		${CLEAN}

src/analysisd/cdb/cdb.c

-/* Public domain. */
-/* Adapted from DJB's original cdb-0.75 package */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include "cdb.h"
-
-#ifndef EPROTO
-#define EPROTO -15  /* cdb 0.75's default for PROTOless systems */
-#endif
-
-void cdb_free(struct cdb *c)
-{
-  if (c->map) {
-    munmap(c->map,c->size);
-    c->map = 0;
-  }
-}
-
-void cdb_findstart(struct cdb *c)
-{
-  c->loop = 0;
-}
-
-void cdb_init(struct cdb *c,int fd)
-{
-  struct stat st;
-  char *x;
-
-  cdb_free(c);
-  cdb_findstart(c);
-  c->fd = fd;
-
-  if (fstat(fd,&st) == 0)
-    if (st.st_size <= 0xffffffff) {
-      x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
-      if (x + 1) {
-	c->size = st.st_size;
-	c->map = x;
-      }
-    }
-}
-
-int cdb_read(struct cdb *c,char *buf,unsigned int len,uint32 pos)
-{
-  if (c->map) {
-    if ((pos > c->size) || (c->size - pos < len)) goto FORMAT;
-    memcpy(buf,c->map + pos,len);
-  }
-  else {
-    if (lseek(c->fd,pos,SEEK_SET) == -1) return -1;
-    /* if (seek_set(c->fd,pos) == -1) return -1; */
-    while (len > 0) {
-      int r;
-      do
-        r = read(c->fd,buf,len);
-      while ((r == -1) && (errno == EINTR));
-      if (r == -1) return -1;
-      if (r == 0) goto FORMAT;
-      buf += r;
-      len -= r;
-    }
-  }
-  return 0;
-
-  FORMAT:
-  errno = EPROTO;
-  return -1;
-}
-
-static int match(struct cdb *c,char *key,unsigned int len,uint32 pos)
-{
-  char buf[32];
-  int n;
-
-  while (len > 0) {
-    n = sizeof buf;
-    if (n > len) n = len;
-    if (cdb_read(c,buf,n,pos) == -1) return -1;
-    if (memcmp(buf,key,n)) return 0;
-    pos += n;
-    key += n;
-    len -= n;
-  }
-  return 1;
-}
-
-int cdb_findnext(struct cdb *c,char *key,unsigned int len)
-{
-  char buf[8];
-  uint32 pos;
-  uint32 u;
-
-  if (!c->loop) {
-    u = cdb_hash(key,len);
-    if (cdb_read(c,buf,8,(u << 3) & 2047) == -1) return -1;
-    uint32_unpack(buf + 4,&c->hslots);
-    if (!c->hslots) return 0;
-    uint32_unpack(buf,&c->hpos);
-    c->khash = u;
-    u >>= 8;
-    u %= c->hslots;
-    u <<= 3;
-    c->kpos = c->hpos + u;
-  }
-
-  while (c->loop < c->hslots) {
-    if (cdb_read(c,buf,8,c->kpos) == -1) return -1;
-    uint32_unpack(buf + 4,&pos);
-    if (!pos) return 0;
-    c->loop += 1;
-    c->kpos += 8;
-    if (c->kpos == c->hpos + (c->hslots << 3)) c->kpos = c->hpos;
-    uint32_unpack(buf,&u);
-    if (u == c->khash) {
-      if (cdb_read(c,buf,8,pos) == -1) return -1;
-      uint32_unpack(buf,&u);
-      if (u == len)
-	switch(match(c,key,len,pos + 8)) {
-	  case -1:
-	    return -1;
-	  case 1:
-	    uint32_unpack(buf + 4,&c->dlen);
-	    c->dpos = pos + 8 + len;
-	    return 1;
-	}
-    }
-  }
-
-  return 0;
-}
-
-int cdb_find(struct cdb *c,char *key,unsigned int len)
-{
-  cdb_findstart(c);
-  return cdb_findnext(c,key,len);
-}

src/analysisd/cdb/cdb.h

-/* Public domain. */
-/* Adapted from DJB's original cdb-0.75 package */
-
-#ifndef CDB_H
-#define CDB_H
-
-#include "uint32.h"
-
-#define CDB_HASHSTART 5381
-extern uint32 cdb_hashadd(uint32,unsigned char);
-extern uint32 cdb_hash(char *,unsigned int);
-
-struct cdb {
-  char *map; /* 0 if no map is available */
-  int fd;
-  uint32 size; /* initialized if map is nonzero */
-  uint32 loop; /* number of hash slots searched under this key */
-  uint32 khash; /* initialized if loop is nonzero */
-  uint32 kpos; /* initialized if loop is nonzero */
-  uint32 hpos; /* initialized if loop is nonzero */
-  uint32 hslots; /* initialized if loop is nonzero */
-  uint32 dpos; /* initialized if cdb_findnext() returns 1 */
-  uint32 dlen; /* initialized if cdb_findnext() returns 1 */
-} ;
-
-extern void cdb_free(struct cdb *);
-extern void cdb_init(struct cdb *,int fd);
-
-extern int cdb_read(struct cdb *,char *,unsigned int,uint32);
-
-extern void cdb_findstart(struct cdb *);
-extern int cdb_findnext(struct cdb *,char *,unsigned int);
-extern int cdb_find(struct cdb *,char *,unsigned int);
-
-#define cdb_datapos(c) ((c)->dpos)
-#define cdb_datalen(c) ((c)->dlen)
-
-#endif

src/analysisd/cdb/cdb_hash.c

-/* Public domain. */
-/* Adapted from DJB's original cdb-0.75 package */
-
-#include "cdb.h"
-
-uint32 cdb_hashadd(uint32 h,unsigned char c)
-{
-  h += (h << 5);
-  return h ^ c;
-}
-
-uint32 cdb_hash(char *buf,unsigned int len)
-{
-  uint32 h;
-
-  h = CDB_HASHSTART;
-  while (len) {
-    h = cdb_hashadd(h,*buf++);
-    --len;
-  }
-  return h;
-}

src/analysisd/cdb/cdb_make.c

-/* Public domain. */
-/* Adapted from DJB's original cdb-0.75 package */
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-#include "cdb.h"
-#include "cdb_make.h"
-#include "uint32.h"
-
-static int cdb_make_write(struct cdb_make *c, char *buf, uint32 sz) {
-  fwrite(buf, sz, 1, c->fp);
-  return ferror(c->fp);
-}
-
-int cdb_make_start(struct cdb_make *c, FILE * f)
-{
-  c->head = 0;
-  c->split = 0;
-  c->hash = 0;
-  c->numentries = 0;
-  c->fp = f;
-  c->pos = sizeof c->final;
-  if (fseek(f,c->pos,SEEK_SET) == -1) {
-    perror("fseek failed");
-    return -1;
-  }
-  return ftell(c->fp);
-}
-
-static int posplus(struct cdb_make *c,uint32 len)
-{
-  uint32 newpos = c->pos + len;
-  if (newpos < len) { errno = ENOMEM; return -1; }
-  c->pos = newpos;
-  return 0;
-}
-
-int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h)
-{
-  struct cdb_hplist *head;
-
-  head = c->head;
-  if (!head || (head->num >= CDB_HPLIST)) {
-    head = (struct cdb_hplist *) malloc(sizeof(struct cdb_hplist));
-    if (!head) return -1;
-    head->num = 0;
-    head->next = c->head;
-    c->head = head;
-  }
-  head->hp[head->num].h = h;
-  head->hp[head->num].p = c->pos;
-  ++head->num;
-  ++c->numentries;
-  if (posplus(c,8) == -1) return -1;
-  if (posplus(c,keylen) == -1) return -1;
-  if (posplus(c,datalen) == -1) return -1;
-  return 0;
-}
-
-int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen)
-{
-  char buf[8];
-
-  if (keylen > 0xffffffff) { errno = ENOMEM; return -1; }
-  if (datalen > 0xffffffff) { errno = ENOMEM; return -1; }
-
-  uint32_pack(buf,keylen);
-  uint32_pack(buf + 4,datalen);
-  if (cdb_make_write(c,buf,8) != 0) return -1;
-  /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
-  return 0;
-}
-
-int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen)
-{
-  if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1;
-  if (cdb_make_write(c,key,keylen) != 0) return -1;
-  if (cdb_make_write(c,data,datalen) != 0) return -1;
-  /* if (buffer_putalign(&c->b,key,keylen) == -1) return -1; */
-  /* if (buffer_putalign(&c->b,data,datalen) == -1) return -1; */
-  return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen));
-}
-
-int cdb_make_finish(struct cdb_make *c)
-{
-  char buf[8];
-  int i;
-  uint32 len;
-  uint32 u;
-  uint32 memsize;
-  uint32 count;
-  uint32 where;
-  struct cdb_hplist *x;
-  struct cdb_hp *hp;
-
-  for (i = 0;i < 256;++i)
-    c->count[i] = 0;
-
-  for (x = c->head;x;x = x->next) {
-    i = x->num;
-    while (i--)
-      ++c->count[255 & x->hp[i].h];
-  }
-
-  memsize = 1;
-  for (i = 0;i < 256;++i) {
-    u = c->count[i] * 2;
-    if (u > memsize)
-      memsize = u;
-  }
-
-  memsize += c->numentries; /* no overflow possible up to now */
-  u = (uint32) 0 - (uint32) 1;
-  u /= sizeof(struct cdb_hp);
-  if (memsize > u) { errno = ENOMEM; return -1; }
-
-  c->split = (struct cdb_hp *) malloc(memsize * sizeof(struct cdb_hp));
-  if (!c->split) return -1;
-
-  c->hash = c->split + c->numentries;
-
-  u = 0;
-  for (i = 0;i < 256;++i) {
-    u += c->count[i]; /* bounded by numentries, so no overflow */
-    c->start[i] = u;
-  }
-
-  for (x = c->head;x;x = x->next) {
-    i = x->num;
-    while (i--)
-      c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
-  }
-
-  for (i = 0;i < 256;++i) {
-    count = c->count[i];
-
-    len = count + count; /* no overflow possible */
-    uint32_pack(c->final + 8 * i,c->pos);
-    uint32_pack(c->final + 8 * i + 4,len);
-
-    for (u = 0;u < len;++u)
-      c->hash[u].h = c->hash[u].p = 0;
-
-    hp = c->split + c->start[i];
-    for (u = 0;u < count;++u) {
-      where = (hp->h >> 8) % len;
-      while (c->hash[where].p)
-	if (++where == len)
-	  where = 0;
-      c->hash[where] = *hp++;
-    }
-
-    for (u = 0;u < len;++u) {
-      uint32_pack(buf,c->hash[u].h);
-      uint32_pack(buf + 4,c->hash[u].p);
-      if (cdb_make_write(c,buf,8) != 0) return -1;
-      /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
-      if (posplus(c,8) == -1) return -1;
-    }
-  }
-
-  if (c->split) free(c->split);
-
-  for (x = c->head;x;c->head = x) {
-    x = x->next;
-    free(c->head);
-  }
-
-  if (fflush(c->fp) != 0) return -1;
-  /* if (buffer_flush(&c->b) == -1) return -1; */
-  rewind(c->fp);
-  if (ftell(c->fp) != 0) return -1;
-  /* if (seek_begin(c->fd) == -1) return -1; */
-  if (cdb_make_write(c,c->final,sizeof c->final) != 0) return -1;
-  return fflush(c->fp);
-  /* return buffer_putflush(&c->b,c->final,sizeof c->final); */
-}

src/analysisd/cdb/cdb_make.h

-/* Public domain. */
-/* Adapted from DJB's original cdb-0.75 package */
-
-#ifndef CDB_MAKE_H
-#define CDB_MAKE_H
-
-#include <stdio.h>
-#include "uint32.h"
-
-#define CDB_HPLIST 1000
-
-struct cdb_hp { uint32 h; uint32 p; } ;
-
-struct cdb_hplist {
-  struct cdb_hp hp[CDB_HPLIST];
-  struct cdb_hplist *next;
-  int num;
-} ;
-
-struct cdb_make {
-  /* char bspace[8192]; */
-  char final[2048];
-  uint32 count[256];
-  uint32 start[256];
-  struct cdb_hplist *head;
-  struct cdb_hp *split; /* includes space for hash */
-  struct cdb_hp *hash;
-  uint32 numentries;
-  /* buffer b; */
-  uint32 pos;
-  /* int fd; */
-  FILE * fp;
-} ;
-
-extern int cdb_make_start(struct cdb_make *, FILE *);
-extern int cdb_make_addbegin(struct cdb_make *,unsigned int,unsigned int);
-extern int cdb_make_addend(struct cdb_make *,unsigned int,unsigned int,uint32);
-extern int cdb_make_add(struct cdb_make *,char *,unsigned int,char *,unsigned int);
-extern int cdb_make_finish(struct cdb_make *);
-
-#endif

src/analysisd/cdb/uint32.h

-#ifndef UINT32_H
-#define UINT32_H
-
-/* adopted from libowfat 0.9 (GPL) */
-
-typedef unsigned int uint32;
-
-extern void uint32_pack(char *out,uint32 in);
-extern void uint32_unpack(const char *in,uint32 *out);
-
-#endif

src/analysisd/cdb/uint32_pack.c

-#define NO_UINT32_MACROS
-#include "uint32.h"
-
-/* adopted from libowfat 0.9 (GPL) */
-
-void uint32_pack(char *out,uint32 in) {
-  *out=in&0xff; in>>=8;
-  *++out=in&0xff; in>>=8;
-  *++out=in&0xff; in>>=8;
-  *++out=in&0xff;
-}

src/analysisd/cdb/uint32_unpack.c

-#define NO_UINT32_MACROS
-#include "uint32.h"
-
-/* adopted from libowfat 0.9 (GPL) */
-
-void uint32_unpack(const char *in,uint32 *out) {
-  *out = (((uint32)(unsigned char)in[3])<<24) |
-         (((uint32)(unsigned char)in[2])<<16) |
-         (((uint32)(unsigned char)in[1])<<8) |
-          (uint32)(unsigned char)in[0];
-}

src/external/tinycdb-0.77/Makefile

+# Makefile for analysisd alerts
+# Daniel B. Cid <dcid@sourcefire.com>||<daniel.cid@gmail.com>
+# http://www.ossec.net/hids/
+
+PT=../../
+NAME=cdb
+
+include ../../Config.Make
+
+SRCS = cdb.c cdb_hash.c cdb_make.c uint32_pack.c uint32_unpack.c
+OBJS = cdb.o cdb_hash.o cdb_make.o uint32_pack.o uint32_unpack.o
+
+
+response:
+		$(CC) -I../ $(CFLAGS) -c $(SRCS)
+		ar cru cdb.a $(OBJS)
+		ar cru cdb_make.a $(OBJS)
+		ranlib cdb.a
+		ranlib cdb_make.a
+
+clean:
+		${CLEAN}

src/external/tinycdb-0.77/cdb.c

+/* Public domain. */
+/* Adapted from DJB's original cdb-0.75 package */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include "cdb.h"
+
+#ifndef EPROTO
+#define EPROTO -15  /* cdb 0.75's default for PROTOless systems */
+#endif
+
+void cdb_free(struct cdb *c)
+{
+  if (c->map) {
+    munmap(c->map,c->size);
+    c->map = 0;
+  }
+}
+
+void cdb_findstart(struct cdb *c)
+{
+  c->loop = 0;
+}
+
+void cdb_init(struct cdb *c,int fd)
+{
+  struct stat st;
+  char *x;
+
+  cdb_free(c);
+  cdb_findstart(c);
+  c->fd = fd;
+
+  if (fstat(fd,&st) == 0)
+    if (st.st_size <= 0xffffffff) {
+      x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
+      if (x + 1) {
+	c->size = st.st_size;
+	c->map = x;
+      }
+    }
+}
+
+int cdb_read(struct cdb *c,char *buf,unsigned int len,uint32 pos)
+{
+  if (c->map) {
+    if ((pos > c->size) || (c->size - pos < len)) goto FORMAT;
+    memcpy(buf,c->map + pos,len);
+  }
+  else {
+    if (lseek(c->fd,pos,SEEK_SET) == -1) return -1;
+    /* if (seek_set(c->fd,pos) == -1) return -1; */
+    while (len > 0) {
+      int r;
+      do
+        r = read(c->fd,buf,len);
+      while ((r == -1) && (errno == EINTR));
+      if (r == -1) return -1;
+      if (r == 0) goto FORMAT;
+      buf += r;
+      len -= r;
+    }
+  }
+  return 0;
+
+  FORMAT:
+  errno = EPROTO;
+  return -1;
+}
+
+static int match(struct cdb *c,char *key,unsigned int len,uint32 pos)
+{
+  char buf[32];
+  int n;
+
+  while (len > 0) {
+    n = sizeof buf;
+    if (n > len) n = len;
+    if (cdb_read(c,buf,n,pos) == -1) return -1;
+    if (memcmp(buf,key,n)) return 0;
+    pos += n;
+    key += n;
+    len -= n;
+  }
+  return 1;
+}
+
+int cdb_findnext(struct cdb *c,char *key,unsigned int len)
+{
+  char buf[8];
+  uint32 pos;
+  uint32 u;
+
+  if (!c->loop) {
+    u = cdb_hash(key,len);
+    if (cdb_read(c,buf,8,(u << 3) & 2047) == -1) return -1;
+    uint32_unpack(buf + 4,&c->hslots);
+    if (!c->hslots) return 0;
+    uint32_unpack(buf,&c->hpos);
+    c->khash = u;
+    u >>= 8;
+    u %= c->hslots;
+    u <<= 3;
+    c->kpos = c->hpos + u;
+  }
+
+  while (c->loop < c->hslots) {
+    if (cdb_read(c,buf,8,c->kpos) == -1) return -1;
+    uint32_unpack(buf + 4,&pos);
+    if (!pos) return 0;
+    c->loop += 1;
+    c->kpos += 8;
+    if (c->kpos == c->hpos + (c->hslots << 3)) c->kpos = c->hpos;
+    uint32_unpack(buf,&u);
+    if (u == c->khash) {
+      if (cdb_read(c,buf,8,pos) == -1) return -1;
+      uint32_unpack(buf,&u);
+      if (u == len)
+	switch(match(c,key,len,pos + 8)) {
+	  case -1:
+	    return -1;
+	  case 1:
+	    uint32_unpack(buf + 4,&c->dlen);
+	    c->dpos = pos + 8 + len;
+	    return 1;
+	}
+    }
+  }
+
+  return 0;
+}
+
+int cdb_find(struct cdb *c,char *key,unsigned int len)
+{
+  cdb_findstart(c);
+  return cdb_findnext(c,key,len);
+}

src/external/tinycdb-0.77/cdb.h

+/* Public domain. */
+/* Adapted from DJB's original cdb-0.75 package */
+
+#ifndef CDB_H
+#define CDB_H
+
+#include "uint32.h"
+
+#define CDB_HASHSTART 5381
+extern uint32 cdb_hashadd(uint32,unsigned char);
+extern uint32 cdb_hash(char *,unsigned int);
+
+struct cdb {
+  char *map; /* 0 if no map is available */
+  int fd;
+  uint32 size; /* initialized if map is nonzero */
+  uint32 loop; /* number of hash slots searched under this key */
+  uint32 khash; /* initialized if loop is nonzero */
+  uint32 kpos; /* initialized if loop is nonzero */
+  uint32 hpos; /* initialized if loop is nonzero */
+  uint32 hslots; /* initialized if loop is nonzero */
+  uint32 dpos; /* initialized if cdb_findnext() returns 1 */
+  uint32 dlen; /* initialized if cdb_findnext() returns 1 */
+} ;
+
+extern void cdb_free(struct cdb *);
+extern void cdb_init(struct cdb *,int fd);
+
+extern int cdb_read(struct cdb *,char *,unsigned int,uint32);
+
+extern void cdb_findstart(struct cdb *);
+extern int cdb_findnext(struct cdb *,char *,unsigned int);
+extern int cdb_find(struct cdb *,char *,unsigned int);
+
+#define cdb_datapos(c) ((c)->dpos)
+#define cdb_datalen(c) ((c)->dlen)
+
+#endif

src/external/tinycdb-0.77/cdb_hash.c

+/* Public domain. */
+/* Adapted from DJB's original cdb-0.75 package */
+
+#include "cdb.h"
+
+uint32 cdb_hashadd(uint32 h,unsigned char c)
+{
+  h += (h << 5);
+  return h ^ c;
+}
+
+uint32 cdb_hash(char *buf,unsigned int len)
+{
+  uint32 h;
+
+  h = CDB_HASHSTART;
+  while (len) {
+    h = cdb_hashadd(h,*buf++);
+    --len;
+  }
+  return h;
+}

src/external/tinycdb-0.77/cdb_make.c

+/* Public domain. */
+/* Adapted from DJB's original cdb-0.75 package */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include "cdb.h"
+#include "cdb_make.h"
+#include "uint32.h"
+
+static int cdb_make_write(struct cdb_make *c, char *buf, uint32 sz) {
+  fwrite(buf, sz, 1, c->fp);
+  return ferror(c->fp);
+}
+
+int cdb_make_start(struct cdb_make *c, FILE * f)
+{
+  c->head = 0;
+  c->split = 0;
+  c->hash = 0;
+  c->numentries = 0;
+  c->fp = f;
+  c->pos = sizeof c->final;
+  if (fseek(f,c->pos,SEEK_SET) == -1) {
+    perror("fseek failed");
+    return -1;
+  }
+  return ftell(c->fp);
+}
+
+static int posplus(struct cdb_make *c,uint32 len)
+{
+  uint32 newpos = c->pos + len;
+  if (newpos < len) { errno = ENOMEM; return -1; }
+  c->pos = newpos;
+  return 0;
+}
+
+int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h)
+{
+  struct cdb_hplist *head;
+
+  head = c->head;
+  if (!head || (head->num >= CDB_HPLIST)) {
+    head = (struct cdb_hplist *) malloc(sizeof(struct cdb_hplist));
+    if (!head) return -1;
+    head->num = 0;
+    head->next = c->head;
+    c->head = head;
+  }
+  head->hp[head->num].h = h;
+  head->hp[head->num].p = c->pos;
+  ++head->num;
+  ++c->numentries;
+  if (posplus(c,8) == -1) return -1;
+  if (posplus(c,keylen) == -1) return -1;
+  if (posplus(c,datalen) == -1) return -1;
+  return 0;
+}
+
+int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen)
+{
+  char buf[8];
+
+  if (keylen > 0xffffffff) { errno = ENOMEM; return -1; }
+  if (datalen > 0xffffffff) { errno = ENOMEM; return -1; }
+
+  uint32_pack(buf,keylen);
+  uint32_pack(buf + 4,datalen);
+  if (cdb_make_write(c,buf,8) != 0) return -1;
+  /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
+  return 0;
+}
+
+int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen)
+{
+  if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1;
+  if (cdb_make_write(c,key,keylen) != 0) return -1;
+  if (cdb_make_write(c,data,datalen) != 0) return -1;
+  /* if (buffer_putalign(&c->b,key,keylen) == -1) return -1; */
+  /* if (buffer_putalign(&c->b,data,datalen) == -1) return -1; */
+  return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen));
+}
+
+int cdb_make_finish(struct cdb_make *c)
+{
+  char buf[8];
+  int i;
+  uint32 len;
+  uint32 u;
+  uint32 memsize;
+  uint32 count;
+  uint32 where;
+  struct cdb_hplist *x;
+  struct cdb_hp *hp;
+
+  for (i = 0;i < 256;++i)
+    c->count[i] = 0;
+
+  for (x = c->head;x;x = x->next) {
+    i = x->num;
+    while (i--)
+      ++c->count[255 & x->hp[i].h];
+  }
+
+  memsize = 1;
+  for (i = 0;i < 256;++i) {
+    u = c->count[i] * 2;
+    if (u > memsize)
+      memsize = u;
+  }
+
+  memsize += c->numentries; /* no overflow possible up to now */
+  u = (uint32) 0 - (uint32) 1;
+  u /= sizeof(struct cdb_hp);
+  if (memsize > u) { errno = ENOMEM; return -1; }
+
+  c->split = (struct cdb_hp *) malloc(memsize * sizeof(struct cdb_hp));
+  if (!c->split) return -1;
+
+  c->hash = c->split + c->numentries;
+
+  u = 0;
+  for (i = 0;i < 256;++i) {
+    u += c->count[i]; /* bounded by numentries, so no overflow */
+    c->start[i] = u;
+  }
+
+  for (x = c->head;x;x = x->next) {
+    i = x->num;
+    while (i--)
+      c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
+  }
+
+  for (i = 0;i < 256;++i) {
+    count = c->count[i];
+
+    len = count + count; /* no overflow possible */
+    uint32_pack(c->final + 8 * i,c->pos);
+    uint32_pack(c->final + 8 * i + 4,len);
+
+    for (u = 0;u < len;++u)
+      c->hash[u].h = c->hash[u].p = 0;
+
+    hp = c->split + c->start[i];
+    for (u = 0;u < count;++u) {
+      where = (hp->h >> 8) % len;
+      while (c->hash[where].p)
+	if (++where == len)
+	  where = 0;
+      c->hash[where] = *hp++;
+    }
+
+    for (u = 0;u < len;++u) {
+      uint32_pack(buf,c->hash[u].h);
+      uint32_pack(buf + 4,c->hash[u].p);
+      if (cdb_make_write(c,buf,8) != 0) return -1;
+      /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
+      if (posplus(c,8) == -1) return -1;
+    }
+  }
+
+  if (c->split) free(c->split);
+
+  for (x = c->head;x;c->head = x) {
+    x = x->next;
+    free(c->head);
+  }
+
+  if (fflush(c->fp) != 0) return -1;
+  /* if (buffer_flush(&c->b) == -1) return -1; */
+  rewind(c->fp);
+  if (ftell(c->fp) != 0) return -1;
+  /* if (seek_begin(c->fd) == -1) return -1; */
+  if (cdb_make_write(c,c->final,sizeof c->final) != 0) return -1;
+  return fflush(c->fp);
+  /* return buffer_putflush(&c->b,c->final,sizeof c->final); */
+}

src/external/tinycdb-0.77/cdb_make.h

+/* Public domain. */
+/* Adapted from DJB's original cdb-0.75 package */
+
+#ifndef CDB_MAKE_H
+#define CDB_MAKE_H
+
+#include <stdio.h>
+#include "uint32.h"
+
+#define CDB_HPLIST 1000
+
+struct cdb_hp { uint32 h; uint32 p; } ;
+
+struct cdb_hplist {
+  struct cdb_hp hp[CDB_HPLIST];
+  struct cdb_hplist *next;
+  int num;
+} ;
+
+struct cdb_make {
+  /* char bspace[8192]; */
+  char final[2048];
+  uint32 count[256];
+  uint32 start[256];
+  struct cdb_hplist *head;
+  struct cdb_hp *split; /* includes space for hash */
+  struct cdb_hp *hash;
+  uint32 numentries;
+  /* buffer b; */
+  uint32 pos;
+  /* int fd; */
+  FILE * fp;
+} ;
+
+extern int cdb_make_start(struct cdb_make *, FILE *);
+extern int cdb_make_addbegin(struct cdb_make *,unsigned int,unsigned int);
+extern int cdb_make_addend(struct cdb_make *,unsigned int,unsigned int,uint32);
+extern int cdb_make_add(struct cdb_make *,char *,unsigned int,char *,unsigned int);
+extern int cdb_make_finish(struct cdb_make *);
+
+#endif

src/external/tinycdb-0.77/uint32.h

+#ifndef UINT32_H
+#define UINT32_H
+
+/* adopted from libowfat 0.9 (GPL) */
+
+typedef unsigned int uint32;
+
+extern void uint32_pack(char *out,uint32 in);
+extern void uint32_unpack(const char *in,uint32 *out);
+
+#endif

src/external/tinycdb-0.77/uint32_pack.c

+#define NO_UINT32_MACROS
+#include "uint32.h"
+
+/* adopted from libowfat 0.9 (GPL) */
+
+void uint32_pack(char *out,uint32 in) {
+  *out=in&0xff; in>>=8;
+  *++out=in&0xff; in>>=8;
+  *++out=in&0xff; in>>=8;
+  *++out=in&0xff;
+}

src/external/tinycdb-0.77/uint32_unpack.c

+#define NO_UINT32_MACROS
+#include "uint32.h"
+
+/* adopted from libowfat 0.9 (GPL) */
+
+void uint32_unpack(const char *in,uint32 *out) {
+  *out = (((uint32)(unsigned char)in[3])<<24) |
+         (((uint32)(unsigned char)in[2])<<16) |
+         (((uint32)(unsigned char)in[1])<<8) |
+          (uint32)(unsigned char)in[0];
+}

src/external/tinycdb-0.77/wscript

+#!/usr/bin/env python
+#from waflib.Configure import conf
+
+
+def build(ctx):
+    ctx.objects(
+        source = "cdb_hash.c uint32_pack.c uint32_unpack.c",
+        target = "cdb_common",
+        includes = ".",
+    )
+    ctx.stlib(
+        source = "cdb.c",
+        target = "cdb_read",
+        export_includes =".",
+        use="os_headers cdb_common",
+    )
+    ctx.stlib(
+        source = "cdb_make.c",
+        target = "cdb_make",
+        export_includes = ".",
+        use="os_headers cdb_common",
+    )
           pthread_mutex_init (&m, NULL);
              return 0;
 }
+
 """
 
 @conf 
         ctx.env.append_value('CXXFLAGS', '-pthread')
         ctx.env.append_value('CCFLAGS', '-pthread')
                                    
+    ctx.check_cc(header_name="inotify.h", compile_mode="c", 
+                var="USEINOTIFY", mandatory=False,)
     have_pthread = ctx.check(header_name='pthread.h', 
                              define_name='HAVE_PTHREAD_H',
                              fragment=pthread_fragment,
                                  uselib_store='OPENSSL')
         if libcrypto and libssl:
             ctx.env["USE_OPENSSL"] = True 
+    print ctx.env
 
     ctx.write_config_header('headers/build_config.h')
 
     ctx.add_group()
     ctx.recurse("external/zlib-1.2.3/")
     ctx.recurse("external/bson-0.1")
+    ctx.recurse("external/tinycdb-0.77")
     #ctx.recurse("src/headers")
     ctx.recurse("libos_core/os_zlib libos_core/os_xml libos_core/os_regex libos_core/os_net libos_core/os_crypto libos_core/config")
     ctx.recurse("libos_core/shared")