2 * lmo - Lua Machine Objects - Base functions
4 * Copyright (C) 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "template_lmo.h"
20 #include "plural_formula.h"
23 * Hash function from http://www.azillionmonkeys.com/qed/hash.html
24 * Copyright (C) 2004-2008 by Paul Hsieh
27 uint32_t sfh_hash(const char *data, int len)
29 uint32_t hash = len, tmp;
32 if (len <= 0 || data == NULL) return 0;
38 for (;len > 0; len--) {
39 hash += sfh_get16(data);
40 tmp = (sfh_get16(data+2) << 11) ^ hash;
41 hash = (hash << 16) ^ tmp;
42 data += 2*sizeof(uint16_t);
46 /* Handle end cases */
48 case 3: hash += sfh_get16(data);
50 hash ^= (signed char)data[sizeof(uint16_t)] << 18;
53 case 2: hash += sfh_get16(data);
57 case 1: hash += (signed char)*data;
62 /* Force "avalanching" of final 127 bits */
73 uint32_t lmo_canon_hash(const char *str, int len,
74 const char *ctx, int ctxlen, int plural)
77 char *ptr, *end, prev;
84 end = res + sizeof(res);
88 for (prev = ' ', off = 0; off < ctxlen; prev = *ctx, off++, ctx++)
104 if ((ptr > res) && isspace(*(ptr-1)))
113 for (prev = ' ', off = 0; off < len; prev = *str, off++, str++)
129 if ((ptr > res) && isspace(*(ptr-1)))
134 if (plural >= 100 || ptr + 3 >= end)
137 ptr += snprintf(ptr, 3, "\2%d", plural);
140 return sfh_hash(res, ptr - res);
143 lmo_archive_t * lmo_open(const char *file)
146 uint32_t idx_offset = 0;
149 lmo_archive_t *ar = NULL;
151 if (stat(file, &s) == -1)
154 if ((in = open(file, O_RDONLY)) == -1)
157 if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL)
159 memset(ar, 0, sizeof(*ar));
162 ar->size = s.st_size;
164 fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
166 if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
169 idx_offset = ntohl(*((const uint32_t *)
170 (ar->mmap + ar->size - sizeof(uint32_t))));
172 if (idx_offset >= ar->size)
175 ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
176 ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
177 ar->end = ar->mmap + ar->size;
188 if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
189 munmap(ar->mmap, ar->size);
197 void lmo_close(lmo_archive_t *ar)
201 if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
202 munmap(ar->mmap, ar->size);
212 lmo_catalog_t *_lmo_catalogs = NULL;
213 lmo_catalog_t *_lmo_active_catalog = NULL;
215 int lmo_load_catalog(const char *lang, const char *dir)
220 struct dirent *de = NULL;
222 lmo_archive_t *ar = NULL;
223 lmo_catalog_t *cat = NULL;
225 if (!lmo_change_catalog(lang))
228 if (!dir || !(dh = opendir(dir)))
231 if (!(cat = malloc(sizeof(*cat))))
234 memset(cat, 0, sizeof(*cat));
236 snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
237 snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
239 while ((de = readdir(dh)) != NULL)
241 if (!fnmatch(pattern, de->d_name, 0))
243 snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
248 ar->next = cat->archives;
256 cat->next = _lmo_catalogs;
259 if (!_lmo_active_catalog)
260 _lmo_active_catalog = cat;
262 return cat->archives ? 0 : -1;
265 if (dh) closedir(dh);
271 int lmo_change_catalog(const char *lang)
275 for (cat = _lmo_catalogs; cat; cat = cat->next)
277 if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
279 _lmo_active_catalog = cat;
287 static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
289 unsigned int m, l, r;
297 m = l + ((r - l) / 2);
302 k = ntohl(ar->index[m].key_id);
305 return &ar->index[m];
323 void *pluralParseAlloc(void *(*)(size_t));
324 void pluralParse(void *, int, int, void *);
325 void pluralParseFree(void *, void (*)(void *));
327 static int lmo_eval_plural(const char *expr, int len, int val)
329 struct { int num; int res; } s = { .num = val, .res = -1 };
330 const char *p = NULL;
331 void *pParser = NULL;
337 if (!strncmp(expr, "plural=", 7)) {
351 pParser = pluralParseAlloc(malloc);
366 case '0': case '1': case '2': case '3': case '4':
367 case '5': case '6': case '7': case '8': case '9':
371 while (*p >= '0' && *p <= '9') {
487 pluralParse(pParser, t, n, &s);
494 pluralParse(pParser, 0, 0, &s);
497 pluralParseFree(pParser, free);
502 int lmo_translate(const char *key, int keylen, char **out, int *outlen)
504 return lmo_translate_ctxt(key, keylen, NULL, 0, out, outlen);
507 int lmo_translate_ctxt(const char *key, int keylen,
508 const char *ctx, int ctxlen,
509 char **out, int *outlen)
515 if (!key || !_lmo_active_catalog)
518 hash = lmo_canon_hash(key, keylen, ctx, ctxlen, -1);
522 for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
524 if ((e = lmo_find_entry(ar, hash)) != NULL)
526 *out = ar->mmap + ntohl(e->offset);
527 *outlen = ntohl(e->length);
536 int lmo_translate_plural(int n, const char *skey, int skeylen,
537 const char *pkey, int pkeylen,
538 char **out, int *outlen)
540 return lmo_translate_plural_ctxt(n, skey, skeylen, pkey, pkeylen,
541 NULL, 0, out, outlen);
544 int lmo_translate_plural_ctxt(int n, const char *skey, int skeylen,
545 const char *pkey, int pkeylen,
546 const char *ctx, int ctxlen,
547 char **out, int *outlen)
553 const char *plural_formula;
555 if (!skey || !pkey || !_lmo_active_catalog)
558 for (ar = _lmo_active_catalog->archives; ar; ar = ar->next) {
559 e = lmo_find_entry(ar, 0);
562 pid = lmo_eval_plural(ar->mmap + ntohl(e->offset), ntohl(e->length), n);
570 hash = lmo_canon_hash(skey, skeylen, ctx, ctxlen, pid);
575 for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
577 if ((e = lmo_find_entry(ar, hash)) != NULL)
579 *out = ar->mmap + ntohl(e->offset);
580 *outlen = ntohl(e->length);
599 void lmo_iterate(lmo_iterate_cb_t cb, void *priv)
605 if (!_lmo_active_catalog)
608 for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
609 for (i = 0, e = &ar->index[0]; i < ar->length; e = &ar->index[++i])
610 cb(ntohl(e->key_id), ar->mmap + ntohl(e->offset), ntohl(e->length), priv);
613 void lmo_close_catalog(const char *lang)
615 lmo_archive_t *ar, *next;
616 lmo_catalog_t *cat, *prev;
618 for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next)
620 if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
623 prev->next = cat->next;
625 _lmo_catalogs = cat->next;
627 for (ar = cat->archives; ar; ar = next)