gluon-web: clean up LMO code

This commit is contained in:
Matthias Schiffer 2018-02-22 22:47:27 +01:00
parent 3203970969
commit 99b4d2eaf0
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
4 changed files with 123 additions and 150 deletions

View File

@ -19,19 +19,18 @@
#include "template_lmo.h" #include "template_lmo.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fnmatch.h>
#include <dirent.h> #include <dirent.h>
#include <ctype.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> #include <limits.h>
@ -46,12 +45,10 @@ typedef struct lmo_entry lmo_entry_t;
struct lmo_archive { struct lmo_archive {
int fd; size_t length;
int length; const lmo_entry_t *index;
uint32_t size; char *data;
lmo_entry_t *index; const char *end;
char *mmap;
char *end;
struct lmo_archive *next; struct lmo_archive *next;
}; };
@ -66,14 +63,24 @@ struct lmo_catalog {
typedef struct lmo_catalog lmo_catalog_t; typedef struct lmo_catalog lmo_catalog_t;
static inline uint16_t get_le16(const void *data) {
const uint8_t *d = data;
return (((uint16_t)d[1]) << 8) | d[0];
}
static inline uint32_t get_be32(const void *data) {
const uint8_t *d = data;
return (((uint32_t)d[0]) << 24)
| (((uint32_t)d[1]) << 16)
| (((uint32_t)d[2]) << 8)
| d[3];
}
/* /*
* Hash function from http://www.azillionmonkeys.com/qed/hash.html * Hash function from http://www.azillionmonkeys.com/qed/hash.html
* Copyright (C) 2004-2008 by Paul Hsieh * Copyright (C) 2004-2008 by Paul Hsieh
*/ */
static inline uint16_t get_le16(const uint8_t *d) {
return (((uint16_t)d[1]) << 8) | d[0];
}
static uint32_t sfh_hash(const void *input, size_t len) static uint32_t sfh_hash(const void *input, size_t len)
{ {
const uint8_t *data = input; const uint8_t *data = input;
@ -117,47 +124,40 @@ static uint32_t sfh_hash(const void *input, size_t len)
static lmo_archive_t * lmo_open(const char *file) static lmo_archive_t * lmo_open(const char *file)
{ {
int in = -1; int fd = -1;
uint32_t idx_offset = 0; lmo_archive_t *ar = NULL;
struct stat s; struct stat s;
lmo_archive_t *ar = NULL; if ((fd = open(file, O_RDONLY|O_CLOEXEC)) == -1)
if ((in = open(file, O_RDONLY|O_CLOEXEC)) == -1)
goto err; goto err;
if (fstat(in, &s) == -1) if (fstat(fd, &s) == -1)
goto err; goto err;
if ((ar = calloc(1, sizeof(*ar))) != NULL) { if ((ar = calloc(1, sizeof(*ar))) != NULL) {
if ((ar->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
ar->fd = in;
ar->size = s.st_size;
if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
goto err; goto err;
idx_offset = ntohl(*((const uint32_t *) ar->end = ar->data + s.st_size;
(ar->mmap + ar->size - sizeof(uint32_t))));
if (idx_offset >= ar->size) uint32_t idx_offset = get_be32(ar->end - sizeof(uint32_t));
ar->index = (const lmo_entry_t *)(ar->data + idx_offset);
if ((const char *)ar->index > (ar->end - sizeof(uint32_t)))
goto err; goto err;
ar->index = (lmo_entry_t *)(ar->mmap + idx_offset); ar->length = (ar->end - sizeof(uint32_t) - (const char *)ar->index) / sizeof(lmo_entry_t);
ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
ar->end = ar->mmap + ar->size;
return ar; return ar;
} }
err: err:
if (in > -1) if (fd >= 0)
close(in); close(fd);
if (ar != NULL) if (ar != NULL) {
{ if ((ar->data != NULL) && (ar->data != MAP_FAILED))
if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) munmap(ar->data, ar->end - ar->data);
munmap(ar->mmap, ar->size);
free(ar); free(ar);
} }
@ -166,10 +166,24 @@ err:
} }
static lmo_catalog_t *_lmo_catalogs; static lmo_catalog_t *lmo_catalogs;
static lmo_catalog_t *_lmo_active_catalog; static lmo_catalog_t *lmo_active_catalog;
int lmo_load_catalog(const char *lang, const char *dir) 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;
}
}
return false;
}
bool lmo_load_catalog(const char *lang, const char *dir)
{ {
DIR *dh = NULL; DIR *dh = NULL;
char pattern[16]; char pattern[16];
@ -179,10 +193,10 @@ int lmo_load_catalog(const char *lang, const char *dir)
lmo_archive_t *ar = NULL; lmo_archive_t *ar = NULL;
lmo_catalog_t *cat = NULL; lmo_catalog_t *cat = NULL;
if (!lmo_change_catalog(lang)) if (lmo_change_catalog(lang))
return 0; return true;
if (!dir || !(dh = opendir(dir))) if (!(dh = opendir(dir)))
goto err; goto err;
if (!(cat = calloc(1, sizeof(*cat)))) if (!(cat = calloc(1, sizeof(*cat))))
@ -191,15 +205,12 @@ int lmo_load_catalog(const char *lang, const char *dir)
snprintf(cat->lang, sizeof(cat->lang), "%s", lang); snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang); snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
while ((de = readdir(dh)) != NULL) while ((de = readdir(dh)) != NULL) {
{ if (!fnmatch(pattern, de->d_name, 0)) {
if (!fnmatch(pattern, de->d_name, 0))
{
snprintf(path, sizeof(path), "%s/%s", dir, de->d_name); snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
ar = lmo_open(path); ar = lmo_open(path);
if (ar) if (ar) {
{
ar->next = cat->archives; ar->next = cat->archives;
cat->archives = ar; cat->archives = ar;
} }
@ -208,93 +219,62 @@ int lmo_load_catalog(const char *lang, const char *dir)
closedir(dh); closedir(dh);
cat->next = _lmo_catalogs; cat->next = lmo_catalogs;
_lmo_catalogs = cat; lmo_catalogs = cat;
if (!_lmo_active_catalog) lmo_active_catalog = cat;
_lmo_active_catalog = cat;
return 0; return true;
err: err:
if (dh) closedir(dh); if (dh)
if (cat) free(cat); closedir(dh);
free(cat);
return -1; return false;
} }
int lmo_change_catalog(const char *lang) static int lmo_compare_entry(const void *a, const void *b)
{ {
lmo_catalog_t *cat; const lmo_entry_t *ea = a, *eb = b;
uint32_t ka = ntohl(ea->key_id), kb = ntohl(eb->key_id);
for (cat = _lmo_catalogs; cat; cat = cat->next) if (ka < kb)
{ return -1;
if (!strncmp(cat->lang, lang, sizeof(cat->lang))) else if (ka > kb)
{ return 1;
_lmo_active_catalog = cat; else
return 0; return 0;
} }
static const lmo_entry_t * lmo_find_entry(const lmo_archive_t *ar, 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);
}
bool lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen)
{
if (!lmo_active_catalog)
return false;
uint32_t hash = sfh_hash(key, keylen);
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;
return true;
} }
return -1; return false;
}
static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
{
unsigned int m, l, r;
uint32_t k;
l = 0;
r = ar->length - 1;
while (1)
{
m = l + ((r - l) / 2);
if (r < l)
break;
k = ntohl(ar->index[m].key_id);
if (k == hash)
return &ar->index[m];
if (k > hash)
{
if (!m)
break;
r = m - 1;
}
else
{
l = m + 1;
}
}
return NULL;
}
int lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen)
{
uint32_t hash;
lmo_entry_t *e;
lmo_archive_t *ar;
if (!key || !_lmo_active_catalog)
return -2;
hash = sfh_hash(key, keylen);
for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
{
if ((e = lmo_find_entry(ar, hash)) != NULL)
{
*out = ar->mmap + ntohl(e->offset);
*outlen = ntohl(e->length);
return 0;
}
}
return -1;
} }

View File

@ -20,11 +20,11 @@
#ifndef _TEMPLATE_LMO_H_ #ifndef _TEMPLATE_LMO_H_
#define _TEMPLATE_LMO_H_ #define _TEMPLATE_LMO_H_
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
int lmo_load_catalog(const char *lang, const char *dir); bool lmo_load_catalog(const char *lang, const char *dir);
int lmo_change_catalog(const char *lang); bool lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen);
int lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen);
#endif #endif

View File

@ -92,8 +92,8 @@ static int template_L_pcdata(lua_State *L)
static int template_L_load_catalog(lua_State *L) { static int template_L_load_catalog(lua_State *L) {
const char *lang = luaL_optstring(L, 1, "en"); const char *lang = luaL_optstring(L, 1, "en");
const char *dir = luaL_optstring(L, 2, NULL); const char *dir = luaL_checkstring(L, 2);
lua_pushboolean(L, !lmo_load_catalog(lang, dir)); lua_pushboolean(L, lmo_load_catalog(lang, dir));
return 1; return 1;
} }
@ -103,19 +103,12 @@ static int template_L_translate(lua_State *L) {
size_t trlen; size_t trlen;
const char *key = luaL_checklstring(L, 1, &len); const char *key = luaL_checklstring(L, 1, &len);
switch (lmo_translate(key, len, &tr, &trlen)) if (lmo_translate(key, len, &tr, &trlen))
{ lua_pushlstring(L, tr, trlen);
case 0: else
lua_pushlstring(L, tr, trlen); lua_pushnil(L);
return 1;
case -1: return 1;
return 0;
}
lua_pushnil(L);
lua_pushstring(L, "no catalog loaded");
return 2;
} }

View File

@ -359,7 +359,7 @@ void luastr_translate(struct template_buffer *out, const char *s, size_t l, bool
char *tr; char *tr;
size_t trlen; size_t trlen;
if (!lmo_translate(s, l, &tr, &trlen)) if (lmo_translate(s, l, &tr, &trlen))
luastr_escape(out, tr, trlen, escape_xml); luastr_escape(out, tr, trlen, escape_xml);
else else
luastr_escape(out, s, l, escape_xml); luastr_escape(out, s, l, escape_xml);