1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
/*
* lmo - Lua Machine Objects - Base functions
*
* Copyright (C) 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "template_lmo.h"
#include "plural_formula.h"
/*
* Hash function from http://www.azillionmonkeys.com/qed/hash.html
* Copyright (C) 2004-2008 by Paul Hsieh
*/
uint32_t sfh_hash(const char *data, int len)
{
uint32_t hash = len, tmp;
int rem;
if (len <= 0 || data == NULL) return 0;
rem = len & 3;
len >>= 2;
/* Main loop */
for (;len > 0; len--) {
hash += sfh_get16(data);
tmp = (sfh_get16(data+2) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2*sizeof(uint16_t);
hash += hash >> 11;
}
/* Handle end cases */
switch (rem) {
case 3: hash += sfh_get16(data);
hash ^= hash << 16;
hash ^= (signed char)data[sizeof(uint16_t)] << 18;
hash += hash >> 11;
break;
case 2: hash += sfh_get16(data);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1: hash += (signed char)*data;
hash ^= hash << 10;
hash += hash >> 1;
}
/* Force "avalanching" of final 127 bits */
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
uint32_t lmo_canon_hash(const char *str, int len,
const char *ctx, int ctxlen, int plural)
{
char res[4096];
char *ptr, *end, prev;
int off;
if (!str)
return 0;
ptr = res;
end = res + sizeof(res);
if (ctx)
{
for (prev = ' ', off = 0; off < ctxlen; prev = *ctx, off++, ctx++)
{
if (ptr >= end)
return 0;
if (isspace(*ctx))
{
if (!isspace(prev))
*ptr++ = ' ';
}
else
{
*ptr++ = *ctx;
}
}
if ((ptr > res) && isspace(*(ptr-1)))
ptr--;
if (ptr >= end)
return 0;
*ptr++ = '\1';
}
for (prev = ' ', off = 0; off < len; prev = *str, off++, str++)
{
if (ptr >= end)
return 0;
if (isspace(*str))
{
if (!isspace(prev))
*ptr++ = ' ';
}
else
{
*ptr++ = *str;
}
}
if ((ptr > res) && isspace(*(ptr-1)))
ptr--;
if (plural > -1)
{
if (plural >= 100 || ptr + 3 >= end)
return 0;
ptr += snprintf(ptr, 3, "\2%d", plural);
}
return sfh_hash(res, ptr - res);
}
lmo_archive_t * lmo_open(const char *file)
{
int in = -1;
uint32_t idx_offset = 0;
struct stat s;
lmo_archive_t *ar = NULL;
if (stat(file, &s) == -1)
goto err;
if ((in = open(file, O_RDONLY)) == -1)
goto err;
if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL)
{
memset(ar, 0, sizeof(*ar));
ar->fd = in;
ar->size = s.st_size;
fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
goto err;
idx_offset = ntohl(*((const uint32_t *)
(ar->mmap + ar->size - sizeof(uint32_t))));
if (idx_offset >= ar->size)
goto err;
ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
ar->end = ar->mmap + ar->size;
return ar;
}
err:
if (in > -1)
close(in);
if (ar != NULL)
{
if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
munmap(ar->mmap, ar->size);
free(ar);
}
return NULL;
}
void lmo_close(lmo_archive_t *ar)
{
if (ar != NULL)
{
if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
munmap(ar->mmap, ar->size);
close(ar->fd);
free(ar);
ar = NULL;
}
}
lmo_catalog_t *_lmo_catalogs = NULL;
lmo_catalog_t *_lmo_active_catalog = NULL;
int lmo_load_catalog(const char *lang, const char *dir)
{
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 0;
if (!dir || !(dh = opendir(dir)))
goto err;
if (!(cat = malloc(sizeof(*cat))))
goto err;
memset(cat, 0, sizeof(*cat));
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;
if (!_lmo_active_catalog)
_lmo_active_catalog = cat;
return cat->archives ? 0 : -1;
err:
if (dh) closedir(dh);
if (cat) free(cat);
return -1;
}
int 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 0;
}
}
return -1;
}
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;
}
void *pluralParseAlloc(void *(*)(size_t));
void pluralParse(void *, int, int, void *);
void pluralParseFree(void *, void (*)(void *));
static int lmo_eval_plural(const char *expr, int len, int val)
{
struct { int num; int res; } s = { .num = val, .res = -1 };
const char *p = NULL;
void *pParser = NULL;
int t, n;
char c;
while (len > 7) {
if (*expr == 'p') {
if (!strncmp(expr, "plural=", 7)) {
p = expr + 7;
len -= 7;
break;
}
}
expr++;
len--;
}
if (!p)
goto out;
pParser = pluralParseAlloc(malloc);
if (!pParser)
goto out;
while (len-- > 0) {
c = *p++;
t = -1;
n = 0;
switch (c) {
case ' ':
case '\t':
continue;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
t = T_NUM;
n = c - '0';
while (*p >= '0' && *p <= '9') {
n *= 10;
n += *p - '0';
p++;
}
break;
case '=':
if (*p == '=') {
t = T_EQ;
p++;
}
break;
case '!':
if (*p == '=') {
t = T_NE;
p++;
}
else {
t = T_NOT;
}
break;
case '&':
if (*p == '&') {
t = T_AND;
p++;
}
break;
case '|':
if (*p == '|') {
t = T_OR;
p++;
}
break;
case '<':
if (*p == '=') {
t = T_LE;
p++;
}
else {
t = T_LT;
}
break;
case '>':
if (*p == '=') {
t = T_GE;
p++;
}
else {
t = T_GT;
}
break;
case '*':
t = T_MUL;
break;
case '/':
t = T_DIV;
break;
case '%':
t = T_MOD;
break;
case '+':
t = T_ADD;
break;
case '-':
t = T_SUB;
break;
case 'n':
t = T_N;
break;
case '?':
t = T_QMARK;
break;
case ':':
t = T_COLON;
break;
case '(':
t = T_LPAREN;
break;
case ')':
t = T_RPAREN;
break;
case ';':
case '\n':
case '\0':
t = 0;
break;
}
/* syntax error */
if (t < 0)
goto out;
pluralParse(pParser, t, n, &s);
/* eof */
if (t == 0)
break;
}
pluralParse(pParser, 0, 0, &s);
out:
pluralParseFree(pParser, free);
return s.res;
}
int lmo_translate(const char *key, int keylen, char **out, int *outlen)
{
return lmo_translate_ctxt(key, keylen, NULL, 0, out, outlen);
}
int lmo_translate_ctxt(const char *key, int keylen,
const char *ctx, int ctxlen,
char **out, int *outlen)
{
uint32_t hash;
lmo_entry_t *e;
lmo_archive_t *ar;
if (!key || !_lmo_active_catalog)
return -2;
hash = lmo_canon_hash(key, keylen, ctx, ctxlen, -1);
if (hash > 0)
{
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;
}
int lmo_translate_plural(int n, const char *skey, int skeylen,
const char *pkey, int pkeylen,
char **out, int *outlen)
{
return lmo_translate_plural_ctxt(n, skey, skeylen, pkey, pkeylen,
NULL, 0, out, outlen);
}
int lmo_translate_plural_ctxt(int n, const char *skey, int skeylen,
const char *pkey, int pkeylen,
const char *ctx, int ctxlen,
char **out, int *outlen)
{
int pid = -1;
uint32_t hash;
lmo_entry_t *e;
lmo_archive_t *ar;
const char *plural_formula;
if (!skey || !pkey || !_lmo_active_catalog)
return -2;
for (ar = _lmo_active_catalog->archives; ar; ar = ar->next) {
e = lmo_find_entry(ar, 0);
if (e != NULL) {
pid = lmo_eval_plural(ar->mmap + ntohl(e->offset), ntohl(e->length), n);
break;
}
}
if (pid == -1)
pid = (n != 1);
hash = lmo_canon_hash(skey, skeylen, ctx, ctxlen, pid);
if (hash == 0)
return -1;
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;
}
}
if (n != 1)
{
*out = (char *)pkey;
*outlen = pkeylen;
}
else
{
*out = (char *)skey;
*outlen = skeylen;
}
return 0;
}
void lmo_iterate(lmo_iterate_cb_t cb, void *priv)
{
unsigned int i;
lmo_entry_t *e;
lmo_archive_t *ar;
if (!_lmo_active_catalog)
return;
for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
for (i = 0, e = &ar->index[0]; i < ar->length; e = &ar->index[++i])
cb(ntohl(e->key_id), ar->mmap + ntohl(e->offset), ntohl(e->length), priv);
}
void lmo_close_catalog(const char *lang)
{
lmo_archive_t *ar, *next;
lmo_catalog_t *cat, *prev;
for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next)
{
if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
{
if (prev)
prev->next = cat->next;
else
_lmo_catalogs = cat->next;
for (ar = cat->archives; ar; ar = next)
{
next = ar->next;
lmo_close(ar);
}
free(cat);
break;
}
}
}
|