From 15d76c3469dc6a98db1dab022aa2254bb8f5fbdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Sun, 22 Aug 2010 17:53:22 +0300 Subject: db: rename squarkdb to filterdb will need authentication db later too. --- Makefile | 4 +- filterdb.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ filterdb.h | 59 +++++++++++++++++++++ lua-squarkdb.c | 2 +- squark-filter.c | 2 +- squarkdb.c | 157 -------------------------------------------------------- squarkdb.h | 59 --------------------- 7 files changed, 220 insertions(+), 220 deletions(-) create mode 100644 filterdb.c create mode 100644 filterdb.h delete mode 100644 squarkdb.c delete mode 100644 squarkdb.h diff --git a/Makefile b/Makefile index 7c5633f..51d9e56 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,10 @@ all: $(TARGETS) squark-auth: squark-auth.o blob.o $(CC) -o $@ $^ $(NETSNMP_LIBS) -squark-filter: squark-filter.o squarkdb.o blob.o +squark-filter: squark-filter.o filterdb.o blob.o $(CC) -o $@ $^ $(CMPH_LIBS) -squarkdb.so: lua-squarkdb.o squarkdb.o blob.o +squarkdb.so: lua-squarkdb.o filterdb.o blob.o $(CC) -shared -o $@ $^ $(LUA_LIBS) $(CMPH_LIBS) clean: diff --git a/filterdb.c b/filterdb.c new file mode 100644 index 0000000..d3f4c6a --- /dev/null +++ b/filterdb.c @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include + +#include "filterdb.h" + +#define PAGE_SIZE 4096 +#define ALIGN(s,a) (((s) + a - 1) & ~(a - 1)) + +const char *sqdb_section_names[SQDB_SECTION_MAX] = { + [SQDB_SECTION_STRINGS] = "strings", + [SQDB_SECTION_CATEGORIES] = "categories", + [SQDB_SECTION_INDEX] = "index", + [SQDB_SECTION_INDEX_MPH] = "index_mph", + [SQDB_SECTION_KEYWORD] = "keyword", + [SQDB_SECTION_KEYWORD_MPH] = "keyword_mph", +}; + +static int sqdb_allocate(struct sqdb *db, size_t s, int wr) +{ + size_t old_size, new_size; + void *base; + int prot = PROT_READ; + + old_size = db->file_length; + new_size = ALIGN(db->file_length + s, PAGE_SIZE); + + if (new_size == ALIGN(db->file_length, PAGE_SIZE)) { + db->file_length += s; + return old_size; + } + + if (wr && ftruncate(db->fd, new_size) < 0) + return -1; + + if (db->mmap_base == NULL) { + if (wr) + prot |= PROT_WRITE; + base = mmap(NULL, new_size, prot, MAP_SHARED, db->fd, 0); + } else { + base = mremap(db->mmap_base, ALIGN(old_size, PAGE_SIZE), + new_size, MREMAP_MAYMOVE); + } + if (base == MAP_FAILED) + return -1; + + db->mmap_base = base; + db->file_length += ALIGN(s, 16); + + return old_size; +} + +int sqdb_open(struct sqdb *db, const char *fn) +{ + struct stat st; + + db->fd = open(fn, O_RDONLY); + if (db->fd < 0) + return -1; + + fstat(db->fd, &st); + + db->file_length = 0; + db->mmap_base = NULL; + sqdb_allocate(db, st.st_size, 0); + + return 0; +} + +int sqdb_create(struct sqdb *db, const char *fn) +{ + struct sqdb_header *hdr; + int rc; + + db->fd = open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666); + if (db->fd < 0) + return -1; + + db->file_length = 0; + db->mmap_base = NULL; + + rc = sqdb_allocate(db, sizeof(struct sqdb_header), 1); + if (rc < 0) { + close(db->fd); + return rc; + } + + hdr = db->mmap_base; + strcpy(hdr->description, "Squark Filtering Database"); + hdr->version = 1; + hdr->magic = 0xdbdbdbdb; + hdr->num_sections = SQDB_SECTION_MAX; + + return 0; +} + +int sqdb_open(struct sqdb *db, const char *fn); + +void sqdb_close(struct sqdb *db) +{ + if (db->mmap_base) + munmap(db->mmap_base, ALIGN(db->file_length, PAGE_SIZE)); + close(db->fd); +} + +void *sqdb_section_create(struct sqdb *db, int id, uint32_t size) +{ + struct sqdb_header *hdr; + size_t pos; + + hdr = db->mmap_base; + if (hdr->section[id].offset || hdr->section[id].length) + return NULL; + + pos = sqdb_allocate(db, size, 1); + if (pos < 0) + return NULL; + + /* sqdb_allocate can remap mmap_base */ + hdr = db->mmap_base; + hdr->section[id].offset = pos; + hdr->section[id].length = size; + + return db->mmap_base + pos; +} + +void *sqdb_section_get(struct sqdb *db, int id, uint32_t *size) +{ + struct sqdb_header *hdr = db->mmap_base; + + if (hdr->section[id].offset == 0) + return NULL; + + if (size) + *size = hdr->section[id].length; + + return db->mmap_base + hdr->section[id].offset; +} + +blob_t sqdb_get_string_literal(struct sqdb *db, uint32_t encoded_ptr) +{ + unsigned char *ptr; + unsigned int len, off; + + ptr = sqdb_section_get(db, SQDB_SECTION_STRINGS, NULL); + if (ptr == NULL) + return BLOB_NULL; + + off = encoded_ptr >> SQDB_LENGTH_BITS; + len = encoded_ptr & ((1 << SQDB_LENGTH_BITS) - 1); + if (len == 0) + len = ptr[off++]; + + return BLOB_PTR_LEN(ptr + off, len); +} diff --git a/filterdb.h b/filterdb.h new file mode 100644 index 0000000..68c1a2a --- /dev/null +++ b/filterdb.h @@ -0,0 +1,59 @@ +#ifndef SQUARKDB_H +#define SQUARKDB_H + +#include +#include +#include "blob.h" + +#define SQDB_LENGTH_BITS 5 + +#define SQDB_SECTION_STRINGS 0 +#define SQDB_SECTION_CATEGORIES 1 +#define SQDB_SECTION_INDEX 2 +#define SQDB_SECTION_INDEX_MPH 3 +#define SQDB_SECTION_KEYWORD 4 +#define SQDB_SECTION_KEYWORD_MPH 5 +#define SQDB_SECTION_MAX 8 + +struct sqdb { + int fd; + void * mmap_base; + size_t file_length; +}; + +struct sqdb_section { + uint32_t offset; + uint32_t length; +}; + +struct sqdb_header { + char description[116]; + uint32_t num_sections; + uint32_t version; + uint32_t magic; + struct sqdb_section section[SQDB_SECTION_MAX]; +}; + +#define SQDB_PARENT_ROOT 0xffffff +#define SQDB_PARENT_IPV4 0xfffffe + +struct sqdb_index_entry { + uint32_t has_subdomains : 1; + uint32_t has_paths : 1; + uint32_t category : 6; + uint32_t parent : 24; + uint32_t component; +}; + + +const char *sqdb_section_names[SQDB_SECTION_MAX]; + +int sqdb_create(struct sqdb *db, const char *fn); +int sqdb_open(struct sqdb *db, const char *fn); +void sqdb_close(struct sqdb *db); + +void *sqdb_section_create(struct sqdb *db, int id, uint32_t size); +void *sqdb_section_get(struct sqdb *db, int id, uint32_t *size); +blob_t sqdb_get_string_literal(struct sqdb *db, uint32_t encoded_ptr); + +#endif diff --git a/lua-squarkdb.c b/lua-squarkdb.c index 09d4afe..5a30848 100644 --- a/lua-squarkdb.c +++ b/lua-squarkdb.c @@ -6,7 +6,7 @@ #include -#include "squarkdb.h" +#include "filterdb.h" #define SQUARKDB_META "squarkdb" diff --git a/squark-filter.c b/squark-filter.c index 9bc6bb2..588eb73 100644 --- a/squark-filter.c +++ b/squark-filter.c @@ -17,7 +17,7 @@ #include -#include "squarkdb.h" +#include "filterdb.h" #include "blob.h" static int running = 1; diff --git a/squarkdb.c b/squarkdb.c deleted file mode 100644 index 0d661c9..0000000 --- a/squarkdb.c +++ /dev/null @@ -1,157 +0,0 @@ -#include -#include -#include -#include -#include - -#include "squarkdb.h" - -#define PAGE_SIZE 4096 -#define ALIGN(s,a) (((s) + a - 1) & ~(a - 1)) - -const char *sqdb_section_names[SQDB_SECTION_MAX] = { - [SQDB_SECTION_STRINGS] = "strings", - [SQDB_SECTION_CATEGORIES] = "categories", - [SQDB_SECTION_INDEX] = "index", - [SQDB_SECTION_INDEX_MPH] = "index_mph", - [SQDB_SECTION_KEYWORD] = "keyword", - [SQDB_SECTION_KEYWORD_MPH] = "keyword_mph", -}; - -static int sqdb_allocate(struct sqdb *db, size_t s, int wr) -{ - size_t old_size, new_size; - void *base; - int prot = PROT_READ; - - old_size = db->file_length; - new_size = ALIGN(db->file_length + s, PAGE_SIZE); - - if (new_size == ALIGN(db->file_length, PAGE_SIZE)) { - db->file_length += s; - return old_size; - } - - if (wr && ftruncate(db->fd, new_size) < 0) - return -1; - - if (db->mmap_base == NULL) { - if (wr) - prot |= PROT_WRITE; - base = mmap(NULL, new_size, prot, MAP_SHARED, db->fd, 0); - } else { - base = mremap(db->mmap_base, ALIGN(old_size, PAGE_SIZE), - new_size, MREMAP_MAYMOVE); - } - if (base == MAP_FAILED) - return -1; - - db->mmap_base = base; - db->file_length += ALIGN(s, 16); - - return old_size; -} - -int sqdb_open(struct sqdb *db, const char *fn) -{ - struct stat st; - - db->fd = open(fn, O_RDONLY); - if (db->fd < 0) - return -1; - - fstat(db->fd, &st); - - db->file_length = 0; - db->mmap_base = NULL; - sqdb_allocate(db, st.st_size, 0); - - return 0; -} - -int sqdb_create(struct sqdb *db, const char *fn) -{ - struct sqdb_header *hdr; - int rc; - - db->fd = open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666); - if (db->fd < 0) - return -1; - - db->file_length = 0; - db->mmap_base = NULL; - - rc = sqdb_allocate(db, sizeof(struct sqdb_header), 1); - if (rc < 0) { - close(db->fd); - return rc; - } - - hdr = db->mmap_base; - strcpy(hdr->description, "Squark Filtering Database"); - hdr->version = 1; - hdr->magic = 0xdbdbdbdb; - hdr->num_sections = SQDB_SECTION_MAX; - - return 0; -} - -int sqdb_open(struct sqdb *db, const char *fn); - -void sqdb_close(struct sqdb *db) -{ - if (db->mmap_base) - munmap(db->mmap_base, ALIGN(db->file_length, PAGE_SIZE)); - close(db->fd); -} - -void *sqdb_section_create(struct sqdb *db, int id, uint32_t size) -{ - struct sqdb_header *hdr; - size_t pos; - - hdr = db->mmap_base; - if (hdr->section[id].offset || hdr->section[id].length) - return NULL; - - pos = sqdb_allocate(db, size, 1); - if (pos < 0) - return NULL; - - /* sqdb_allocate can remap mmap_base */ - hdr = db->mmap_base; - hdr->section[id].offset = pos; - hdr->section[id].length = size; - - return db->mmap_base + pos; -} - -void *sqdb_section_get(struct sqdb *db, int id, uint32_t *size) -{ - struct sqdb_header *hdr = db->mmap_base; - - if (hdr->section[id].offset == 0) - return NULL; - - if (size) - *size = hdr->section[id].length; - - return db->mmap_base + hdr->section[id].offset; -} - -blob_t sqdb_get_string_literal(struct sqdb *db, uint32_t encoded_ptr) -{ - unsigned char *ptr; - unsigned int len, off; - - ptr = sqdb_section_get(db, SQDB_SECTION_STRINGS, NULL); - if (ptr == NULL) - return BLOB_NULL; - - off = encoded_ptr >> SQDB_LENGTH_BITS; - len = encoded_ptr & ((1 << SQDB_LENGTH_BITS) - 1); - if (len == 0) - len = ptr[off++]; - - return BLOB_PTR_LEN(ptr + off, len); -} diff --git a/squarkdb.h b/squarkdb.h deleted file mode 100644 index 68c1a2a..0000000 --- a/squarkdb.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef SQUARKDB_H -#define SQUARKDB_H - -#include -#include -#include "blob.h" - -#define SQDB_LENGTH_BITS 5 - -#define SQDB_SECTION_STRINGS 0 -#define SQDB_SECTION_CATEGORIES 1 -#define SQDB_SECTION_INDEX 2 -#define SQDB_SECTION_INDEX_MPH 3 -#define SQDB_SECTION_KEYWORD 4 -#define SQDB_SECTION_KEYWORD_MPH 5 -#define SQDB_SECTION_MAX 8 - -struct sqdb { - int fd; - void * mmap_base; - size_t file_length; -}; - -struct sqdb_section { - uint32_t offset; - uint32_t length; -}; - -struct sqdb_header { - char description[116]; - uint32_t num_sections; - uint32_t version; - uint32_t magic; - struct sqdb_section section[SQDB_SECTION_MAX]; -}; - -#define SQDB_PARENT_ROOT 0xffffff -#define SQDB_PARENT_IPV4 0xfffffe - -struct sqdb_index_entry { - uint32_t has_subdomains : 1; - uint32_t has_paths : 1; - uint32_t category : 6; - uint32_t parent : 24; - uint32_t component; -}; - - -const char *sqdb_section_names[SQDB_SECTION_MAX]; - -int sqdb_create(struct sqdb *db, const char *fn); -int sqdb_open(struct sqdb *db, const char *fn); -void sqdb_close(struct sqdb *db); - -void *sqdb_section_create(struct sqdb *db, int id, uint32_t size); -void *sqdb_section_get(struct sqdb *db, int id, uint32_t *size); -blob_t sqdb_get_string_literal(struct sqdb *db, uint32_t encoded_ptr); - -#endif -- cgit v1.2.3