Rework LuCI build system
[project/luci.git] / modules / luci-base / src / template_lmo.c
1 /*
2  * lmo - Lua Machine Objects - Base functions
3  *
4  *   Copyright (C) 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "template_lmo.h"
20
21 /*
22  * Hash function from http://www.azillionmonkeys.com/qed/hash.html
23  * Copyright (C) 2004-2008 by Paul Hsieh
24  */
25
26 uint32_t sfh_hash(const char *data, int len)
27 {
28         uint32_t hash = len, tmp;
29         int rem;
30
31         if (len <= 0 || data == NULL) return 0;
32
33         rem = len & 3;
34         len >>= 2;
35
36         /* Main loop */
37         for (;len > 0; len--) {
38                 hash  += sfh_get16(data);
39                 tmp    = (sfh_get16(data+2) << 11) ^ hash;
40                 hash   = (hash << 16) ^ tmp;
41                 data  += 2*sizeof(uint16_t);
42                 hash  += hash >> 11;
43         }
44
45         /* Handle end cases */
46         switch (rem) {
47                 case 3: hash += sfh_get16(data);
48                         hash ^= hash << 16;
49                         hash ^= data[sizeof(uint16_t)] << 18;
50                         hash += hash >> 11;
51                         break;
52                 case 2: hash += sfh_get16(data);
53                         hash ^= hash << 11;
54                         hash += hash >> 17;
55                         break;
56                 case 1: hash += *data;
57                         hash ^= hash << 10;
58                         hash += hash >> 1;
59         }
60
61         /* Force "avalanching" of final 127 bits */
62         hash ^= hash << 3;
63         hash += hash >> 5;
64         hash ^= hash << 4;
65         hash += hash >> 17;
66         hash ^= hash << 25;
67         hash += hash >> 6;
68
69         return hash;
70 }
71
72 uint32_t lmo_canon_hash(const char *str, int len)
73 {
74         char res[4096];
75         char *ptr, prev;
76         int off;
77
78         if (!str || len >= sizeof(res))
79                 return 0;
80
81         for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++)
82         {
83                 if (isspace(*str))
84                 {
85                         if (!isspace(prev))
86                                 *ptr++ = ' ';
87                 }
88                 else
89                 {
90                         *ptr++ = *str;
91                 }
92         }
93
94         if ((ptr > res) && isspace(*(ptr-1)))
95                 ptr--;
96
97         return sfh_hash(res, ptr - res);
98 }
99
100 lmo_archive_t * lmo_open(const char *file)
101 {
102         int in = -1;
103         uint32_t idx_offset = 0;
104         struct stat s;
105
106         lmo_archive_t *ar = NULL;
107
108         if (stat(file, &s) == -1)
109                 goto err;
110
111         if ((in = open(file, O_RDONLY)) == -1)
112                 goto err;
113
114         if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL)
115         {
116                 memset(ar, 0, sizeof(*ar));
117
118                 ar->fd     = in;
119                 ar->size = s.st_size;
120
121                 fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
122
123                 if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
124                         goto err;
125
126                 idx_offset = ntohl(*((const uint32_t *)
127                                      (ar->mmap + ar->size - sizeof(uint32_t))));
128
129                 if (idx_offset >= ar->size)
130                         goto err;
131
132                 ar->index  = (lmo_entry_t *)(ar->mmap + idx_offset);
133                 ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
134                 ar->end    = ar->mmap + ar->size;
135
136                 return ar;
137         }
138
139 err:
140         if (in > -1)
141                 close(in);
142
143         if (ar != NULL)
144         {
145                 if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
146                         munmap(ar->mmap, ar->size);
147
148                 free(ar);
149         }
150
151         return NULL;
152 }
153
154 void lmo_close(lmo_archive_t *ar)
155 {
156         if (ar != NULL)
157         {
158                 if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
159                         munmap(ar->mmap, ar->size);
160
161                 close(ar->fd);
162                 free(ar);
163
164                 ar = NULL;
165         }
166 }
167
168
169 lmo_catalog_t *_lmo_catalogs = NULL;
170 lmo_catalog_t *_lmo_active_catalog = NULL;
171
172 int lmo_load_catalog(const char *lang, const char *dir)
173 {
174         DIR *dh = NULL;
175         char pattern[16];
176         char path[PATH_MAX];
177         struct dirent *de = NULL;
178
179         lmo_archive_t *ar = NULL;
180         lmo_catalog_t *cat = NULL;
181
182         if (!lmo_change_catalog(lang))
183                 return 0;
184
185         if (!dir || !(dh = opendir(dir)))
186                 goto err;
187
188         if (!(cat = malloc(sizeof(*cat))))
189                 goto err;
190
191         memset(cat, 0, sizeof(*cat));
192
193         snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
194         snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
195
196         while ((de = readdir(dh)) != NULL)
197         {
198                 if (!fnmatch(pattern, de->d_name, 0))
199                 {
200                         snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
201                         ar = lmo_open(path);
202
203                         if (ar)
204                         {
205                                 ar->next = cat->archives;
206                                 cat->archives = ar;
207                         }
208                 }
209         }
210
211         closedir(dh);
212
213         cat->next = _lmo_catalogs;
214         _lmo_catalogs = cat;
215
216         if (!_lmo_active_catalog)
217                 _lmo_active_catalog = cat;
218
219         return 0;
220
221 err:
222         if (dh) closedir(dh);
223         if (cat) free(cat);
224
225         return -1;
226 }
227
228 int lmo_change_catalog(const char *lang)
229 {
230         lmo_catalog_t *cat;
231
232         for (cat = _lmo_catalogs; cat; cat = cat->next)
233         {
234                 if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
235                 {
236                         _lmo_active_catalog = cat;
237                         return 0;
238                 }
239         }
240
241         return -1;
242 }
243
244 static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
245 {
246         unsigned int m, l, r;
247         uint32_t k;
248
249         l = 0;
250         r = ar->length - 1;
251
252         while (1)
253         {
254                 m = l + ((r - l) / 2);
255
256                 if (r < l)
257                         break;
258
259                 k = ntohl(ar->index[m].key_id);
260
261                 if (k == hash)
262                         return &ar->index[m];
263
264                 if (k > hash)
265                 {
266                         if (!m)
267                                 break;
268
269                         r = m - 1;
270                 }
271                 else
272                 {
273                         l = m + 1;
274                 }
275         }
276
277         return NULL;
278 }
279
280 int lmo_translate(const char *key, int keylen, char **out, int *outlen)
281 {
282         uint32_t hash;
283         lmo_entry_t *e;
284         lmo_archive_t *ar;
285
286         if (!key || !_lmo_active_catalog)
287                 return -2;
288
289         hash = lmo_canon_hash(key, keylen);
290
291         for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
292         {
293                 if ((e = lmo_find_entry(ar, hash)) != NULL)
294                 {
295                         *out = ar->mmap + ntohl(e->offset);
296                         *outlen = ntohl(e->length);
297                         return 0;
298                 }
299         }
300
301         return -1;
302 }
303
304 void lmo_close_catalog(const char *lang)
305 {
306         lmo_archive_t *ar, *next;
307         lmo_catalog_t *cat, *prev;
308
309         for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next)
310         {
311                 if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
312                 {
313                         if (prev)
314                                 prev->next = cat->next;
315                         else
316                                 _lmo_catalogs = cat->next;
317
318                         for (ar = cat->archives; ar; ar = next)
319                         {
320                                 next = ar->next;
321                                 lmo_close(ar);
322                         }
323
324                         free(cat);
325                         break;
326                 }
327         }
328 }