template_lmo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * lmo - Lua Machine Objects - Base functions
  3. *
  4. * Copyright (C) 2009-2010 Jo-Philipp Wich <jow@openwrt.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. #include "template_lmo.h"
  19. /*
  20. * Hash function from http://www.azillionmonkeys.com/qed/hash.html
  21. * Copyright (C) 2004-2008 by Paul Hsieh
  22. */
  23. static uint32_t sfh_hash(const char *data, int len)
  24. {
  25. uint32_t hash = len, tmp;
  26. int rem;
  27. if (len <= 0 || data == NULL) return 0;
  28. rem = len & 3;
  29. len >>= 2;
  30. /* Main loop */
  31. for (;len > 0; len--) {
  32. hash += sfh_get16(data);
  33. tmp = (sfh_get16(data+2) << 11) ^ hash;
  34. hash = (hash << 16) ^ tmp;
  35. data += 2*sizeof(uint16_t);
  36. hash += hash >> 11;
  37. }
  38. /* Handle end cases */
  39. switch (rem) {
  40. case 3: hash += sfh_get16(data);
  41. hash ^= hash << 16;
  42. hash ^= data[sizeof(uint16_t)] << 18;
  43. hash += hash >> 11;
  44. break;
  45. case 2: hash += sfh_get16(data);
  46. hash ^= hash << 11;
  47. hash += hash >> 17;
  48. break;
  49. case 1: hash += *data;
  50. hash ^= hash << 10;
  51. hash += hash >> 1;
  52. }
  53. /* Force "avalanching" of final 127 bits */
  54. hash ^= hash << 3;
  55. hash += hash >> 5;
  56. hash ^= hash << 4;
  57. hash += hash >> 17;
  58. hash ^= hash << 25;
  59. hash += hash >> 6;
  60. return hash;
  61. }
  62. static uint32_t lmo_canon_hash(const char *str, int len)
  63. {
  64. char res[4096];
  65. char *ptr, prev;
  66. int off;
  67. if (!str || len >= sizeof(res))
  68. return 0;
  69. for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++)
  70. {
  71. if (isspace(*str))
  72. {
  73. if (!isspace(prev))
  74. *ptr++ = ' ';
  75. }
  76. else
  77. {
  78. *ptr++ = *str;
  79. }
  80. }
  81. if ((ptr > res) && isspace(*(ptr-1)))
  82. ptr--;
  83. return sfh_hash(res, ptr - res);
  84. }
  85. static lmo_archive_t * lmo_open(const char *file)
  86. {
  87. int in = -1;
  88. uint32_t idx_offset = 0;
  89. struct stat s;
  90. lmo_archive_t *ar = NULL;
  91. if (stat(file, &s) == -1)
  92. goto err;
  93. if ((in = open(file, O_RDONLY)) == -1)
  94. goto err;
  95. if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL)
  96. {
  97. memset(ar, 0, sizeof(*ar));
  98. ar->fd = in;
  99. ar->size = s.st_size;
  100. fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
  101. if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
  102. goto err;
  103. idx_offset = ntohl(*((const uint32_t *)
  104. (ar->mmap + ar->size - sizeof(uint32_t))));
  105. if (idx_offset >= ar->size)
  106. goto err;
  107. ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
  108. ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
  109. ar->end = ar->mmap + ar->size;
  110. return ar;
  111. }
  112. err:
  113. if (in > -1)
  114. close(in);
  115. if (ar != NULL)
  116. {
  117. if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
  118. munmap(ar->mmap, ar->size);
  119. free(ar);
  120. }
  121. return NULL;
  122. }
  123. static lmo_catalog_t *_lmo_catalogs;
  124. static lmo_catalog_t *_lmo_active_catalog;
  125. int lmo_load_catalog(const char *lang, const char *dir)
  126. {
  127. DIR *dh = NULL;
  128. char pattern[16];
  129. char path[PATH_MAX];
  130. struct dirent *de = NULL;
  131. lmo_archive_t *ar = NULL;
  132. lmo_catalog_t *cat = NULL;
  133. if (!lmo_change_catalog(lang))
  134. return 0;
  135. if (!dir || !(dh = opendir(dir)))
  136. goto err;
  137. if (!(cat = malloc(sizeof(*cat))))
  138. goto err;
  139. memset(cat, 0, sizeof(*cat));
  140. snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
  141. snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
  142. while ((de = readdir(dh)) != NULL)
  143. {
  144. if (!fnmatch(pattern, de->d_name, 0))
  145. {
  146. snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
  147. ar = lmo_open(path);
  148. if (ar)
  149. {
  150. ar->next = cat->archives;
  151. cat->archives = ar;
  152. }
  153. }
  154. }
  155. closedir(dh);
  156. cat->next = _lmo_catalogs;
  157. _lmo_catalogs = cat;
  158. if (!_lmo_active_catalog)
  159. _lmo_active_catalog = cat;
  160. return 0;
  161. err:
  162. if (dh) closedir(dh);
  163. if (cat) free(cat);
  164. return -1;
  165. }
  166. int lmo_change_catalog(const char *lang)
  167. {
  168. lmo_catalog_t *cat;
  169. for (cat = _lmo_catalogs; cat; cat = cat->next)
  170. {
  171. if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
  172. {
  173. _lmo_active_catalog = cat;
  174. return 0;
  175. }
  176. }
  177. return -1;
  178. }
  179. static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
  180. {
  181. unsigned int m, l, r;
  182. uint32_t k;
  183. l = 0;
  184. r = ar->length - 1;
  185. while (1)
  186. {
  187. m = l + ((r - l) / 2);
  188. if (r < l)
  189. break;
  190. k = ntohl(ar->index[m].key_id);
  191. if (k == hash)
  192. return &ar->index[m];
  193. if (k > hash)
  194. {
  195. if (!m)
  196. break;
  197. r = m - 1;
  198. }
  199. else
  200. {
  201. l = m + 1;
  202. }
  203. }
  204. return NULL;
  205. }
  206. int lmo_translate(const char *key, int keylen, char **out, int *outlen)
  207. {
  208. uint32_t hash;
  209. lmo_entry_t *e;
  210. lmo_archive_t *ar;
  211. if (!key || !_lmo_active_catalog)
  212. return -2;
  213. hash = lmo_canon_hash(key, keylen);
  214. for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
  215. {
  216. if ((e = lmo_find_entry(ar, hash)) != NULL)
  217. {
  218. *out = ar->mmap + ntohl(e->offset);
  219. *outlen = ntohl(e->length);
  220. return 0;
  221. }
  222. }
  223. return -1;
  224. }