|
@@ -23,15 +23,12 @@
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
-#include <dirent.h>
|
|
|
#include <fcntl.h>
|
|
|
-#include <fnmatch.h>
|
|
|
#include <stdio.h>
|
|
|
#include <stdint.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <unistd.h>
|
|
|
-#include <limits.h>
|
|
|
|
|
|
|
|
|
struct lmo_entry {
|
|
@@ -41,28 +38,6 @@ struct lmo_entry {
|
|
|
uint32_t length;
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
-typedef struct lmo_entry lmo_entry_t;
|
|
|
-
|
|
|
-
|
|
|
-struct lmo_archive {
|
|
|
- size_t length;
|
|
|
- const lmo_entry_t *index;
|
|
|
- char *data;
|
|
|
- const char *end;
|
|
|
- struct lmo_archive *next;
|
|
|
-};
|
|
|
-
|
|
|
-typedef struct lmo_archive lmo_archive_t;
|
|
|
-
|
|
|
-
|
|
|
-struct lmo_catalog {
|
|
|
- char lang[6];
|
|
|
- struct lmo_archive *archives;
|
|
|
- struct lmo_catalog *next;
|
|
|
-};
|
|
|
-
|
|
|
-typedef struct lmo_catalog lmo_catalog_t;
|
|
|
-
|
|
|
|
|
|
static inline uint16_t get_le16(const void *data) {
|
|
|
const uint8_t *d = data;
|
|
@@ -122,12 +97,13 @@ static uint32_t sfh_hash(const void *input, size_t len)
|
|
|
return hash;
|
|
|
}
|
|
|
|
|
|
-static lmo_archive_t * lmo_open(const char *file)
|
|
|
+bool lmo_load(lmo_catalog_t *cat, const char *file)
|
|
|
{
|
|
|
int fd = -1;
|
|
|
- lmo_archive_t *ar = NULL;
|
|
|
struct stat s;
|
|
|
|
|
|
+ cat->data = MAP_FAILED;
|
|
|
+
|
|
|
fd = open(file, O_RDONLY|O_CLOEXEC);
|
|
|
if (fd < 0)
|
|
|
goto err;
|
|
@@ -135,111 +111,43 @@ static lmo_archive_t * lmo_open(const char *file)
|
|
|
if (fstat(fd, &s))
|
|
|
goto err;
|
|
|
|
|
|
- if ((ar = calloc(1, sizeof(*ar))) != NULL) {
|
|
|
- ar->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
+ cat->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
|
|
- close(fd);
|
|
|
- fd = -1;
|
|
|
+ close(fd);
|
|
|
+ fd = -1;
|
|
|
|
|
|
- if (ar->data == MAP_FAILED)
|
|
|
- goto err;
|
|
|
+ if (cat->data == MAP_FAILED)
|
|
|
+ goto err;
|
|
|
|
|
|
- ar->end = ar->data + s.st_size;
|
|
|
+ cat->end = cat->data + s.st_size;
|
|
|
|
|
|
- uint32_t idx_offset = get_be32(ar->end - sizeof(uint32_t));
|
|
|
- ar->index = (const lmo_entry_t *)(ar->data + idx_offset);
|
|
|
+ uint32_t idx_offset = get_be32(cat->end - sizeof(uint32_t));
|
|
|
+ cat->index = (const lmo_entry_t *)(cat->data + idx_offset);
|
|
|
|
|
|
- if ((const char *)ar->index > (ar->end - sizeof(uint32_t)))
|
|
|
- goto err;
|
|
|
+ if ((const char *)cat->index > (cat->end - sizeof(uint32_t)))
|
|
|
+ goto err;
|
|
|
|
|
|
- ar->length = (ar->end - sizeof(uint32_t) - (const char *)ar->index) / sizeof(lmo_entry_t);
|
|
|
+ cat->length = (cat->end - sizeof(uint32_t) - (const char *)cat->index) / sizeof(lmo_entry_t);
|
|
|
|
|
|
- return ar;
|
|
|
- }
|
|
|
+ return true;
|
|
|
|
|
|
err:
|
|
|
if (fd >= 0)
|
|
|
close(fd);
|
|
|
|
|
|
- if (ar != NULL) {
|
|
|
- if ((ar->data != NULL) && (ar->data != MAP_FAILED))
|
|
|
- munmap(ar->data, ar->end - ar->data);
|
|
|
-
|
|
|
- free(ar);
|
|
|
- }
|
|
|
-
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static lmo_catalog_t *lmo_catalogs;
|
|
|
-static lmo_catalog_t *lmo_active_catalog;
|
|
|
-
|
|
|
-bool lmo_change_catalog(const char *lang)
|
|
|
-{
|
|
|
- lmo_catalog_t *cat;
|
|
|
-
|
|
|
- for (cat = lmo_catalogs; cat; cat = cat->next) {
|
|
|
- if (!strncmp(cat->lang, lang, sizeof(cat->lang))) {
|
|
|
- lmo_active_catalog = cat;
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
+ if (cat->data != MAP_FAILED)
|
|
|
+ munmap(cat->data, cat->end - cat->data);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-bool lmo_load_catalog(const char *lang, const char *dir)
|
|
|
+void lmo_unload(lmo_catalog_t *cat)
|
|
|
{
|
|
|
- DIR *dh = NULL;
|
|
|
- char pattern[16];
|
|
|
- char path[PATH_MAX];
|
|
|
- struct dirent *de = NULL;
|
|
|
-
|
|
|
- lmo_archive_t *ar = NULL;
|
|
|
- lmo_catalog_t *cat = NULL;
|
|
|
-
|
|
|
- if (lmo_change_catalog(lang))
|
|
|
- return true;
|
|
|
-
|
|
|
- if (!(dh = opendir(dir)))
|
|
|
- goto err;
|
|
|
-
|
|
|
- if (!(cat = calloc(1, sizeof(*cat))))
|
|
|
- goto err;
|
|
|
-
|
|
|
- snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
|
|
|
- snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
|
|
|
-
|
|
|
- while ((de = readdir(dh)) != NULL) {
|
|
|
- if (!fnmatch(pattern, de->d_name, 0)) {
|
|
|
- snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
|
|
|
- ar = lmo_open(path);
|
|
|
-
|
|
|
- if (ar) {
|
|
|
- ar->next = cat->archives;
|
|
|
- cat->archives = ar;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- closedir(dh);
|
|
|
-
|
|
|
- cat->next = lmo_catalogs;
|
|
|
- lmo_catalogs = cat;
|
|
|
-
|
|
|
- lmo_active_catalog = cat;
|
|
|
-
|
|
|
- return true;
|
|
|
-
|
|
|
-err:
|
|
|
- if (dh)
|
|
|
- closedir(dh);
|
|
|
- free(cat);
|
|
|
-
|
|
|
- return false;
|
|
|
+ if (cat->data != MAP_FAILED)
|
|
|
+ munmap(cat->data, cat->end - cat->data);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
static int lmo_compare_entry(const void *a, const void *b)
|
|
|
{
|
|
|
const lmo_entry_t *ea = a, *eb = b;
|
|
@@ -253,34 +161,26 @@ static int lmo_compare_entry(const void *a, const void *b)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static const lmo_entry_t * lmo_find_entry(const lmo_archive_t *ar, uint32_t hash)
|
|
|
+static const lmo_entry_t * lmo_find_entry(const lmo_catalog_t *cat, uint32_t hash)
|
|
|
{
|
|
|
lmo_entry_t key;
|
|
|
key.key_id = htonl(hash);
|
|
|
|
|
|
- return bsearch(&key, ar->index, ar->length, sizeof(lmo_entry_t), lmo_compare_entry);
|
|
|
+ return bsearch(&key, cat->index, cat->length, sizeof(lmo_entry_t), lmo_compare_entry);
|
|
|
}
|
|
|
|
|
|
-bool lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen)
|
|
|
+bool lmo_translate(const lmo_catalog_t *cat, const char *key, size_t keylen, const char **out, size_t *outlen)
|
|
|
{
|
|
|
- if (!lmo_active_catalog)
|
|
|
- return false;
|
|
|
-
|
|
|
uint32_t hash = sfh_hash(key, keylen);
|
|
|
+ const lmo_entry_t *e = lmo_find_entry(cat, hash);
|
|
|
+ if (!e)
|
|
|
+ return false;
|
|
|
|
|
|
- for (const lmo_archive_t *ar = lmo_active_catalog->archives; ar; ar = ar->next) {
|
|
|
- const lmo_entry_t *e = lmo_find_entry(ar, hash);
|
|
|
- if (!e)
|
|
|
- continue;
|
|
|
-
|
|
|
- *out = ar->data + ntohl(e->offset);
|
|
|
- *outlen = ntohl(e->length);
|
|
|
-
|
|
|
- if (*out + *outlen > ar->end)
|
|
|
- continue;
|
|
|
+ *out = cat->data + ntohl(e->offset);
|
|
|
+ *outlen = ntohl(e->length);
|
|
|
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if (*out + *outlen > cat->end)
|
|
|
+ return false;
|
|
|
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|