template_lmo.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 inline uint16_t get_le16(const uint8_t *d) {
  24. return (((uint16_t)d[1]) << 8) | d[0];
  25. }
  26. static uint32_t sfh_hash(const uint8_t *data, int len)
  27. {
  28. uint32_t hash = len, tmp;
  29. /* Main loop */
  30. for (; len > 3; len -= 4) {
  31. hash += get_le16(data);
  32. tmp = (get_le16(data+2) << 11) ^ hash;
  33. hash = (hash << 16) ^ tmp;
  34. data += 4;
  35. hash += hash >> 11;
  36. }
  37. /* Handle end cases */
  38. switch (len) {
  39. case 3: hash += get_le16(data);
  40. hash ^= hash << 16;
  41. hash ^= data[2] << 18;
  42. hash += hash >> 11;
  43. break;
  44. case 2: hash += get_le16(data);
  45. hash ^= hash << 11;
  46. hash += hash >> 17;
  47. break;
  48. case 1: hash += *data;
  49. hash ^= hash << 10;
  50. hash += hash >> 1;
  51. }
  52. /* Force "avalanching" of final 127 bits */
  53. hash ^= hash << 3;
  54. hash += hash >> 5;
  55. hash ^= hash << 4;
  56. hash += hash >> 17;
  57. hash ^= hash << 25;
  58. hash += hash >> 6;
  59. return hash;
  60. }
  61. static lmo_archive_t * lmo_open(const char *file)
  62. {
  63. int in = -1;
  64. uint32_t idx_offset = 0;
  65. struct stat s;
  66. lmo_archive_t *ar = NULL;
  67. if (stat(file, &s) == -1)
  68. goto err;
  69. if ((in = open(file, O_RDONLY)) == -1)
  70. goto err;
  71. if ((ar = calloc(1, sizeof(*ar))) != NULL) {
  72. ar->fd = in;
  73. ar->size = s.st_size;
  74. fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
  75. if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
  76. goto err;
  77. idx_offset = ntohl(*((const uint32_t *)
  78. (ar->mmap + ar->size - sizeof(uint32_t))));
  79. if (idx_offset >= ar->size)
  80. goto err;
  81. ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
  82. ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
  83. ar->end = ar->mmap + ar->size;
  84. return ar;
  85. }
  86. err:
  87. if (in > -1)
  88. close(in);
  89. if (ar != NULL)
  90. {
  91. if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
  92. munmap(ar->mmap, ar->size);
  93. free(ar);
  94. }
  95. return NULL;
  96. }
  97. static lmo_catalog_t *_lmo_catalogs;
  98. static lmo_catalog_t *_lmo_active_catalog;
  99. int lmo_load_catalog(const char *lang, const char *dir)
  100. {
  101. DIR *dh = NULL;
  102. char pattern[16];
  103. char path[PATH_MAX];
  104. struct dirent *de = NULL;
  105. lmo_archive_t *ar = NULL;
  106. lmo_catalog_t *cat = NULL;
  107. if (!lmo_change_catalog(lang))
  108. return 0;
  109. if (!dir || !(dh = opendir(dir)))
  110. goto err;
  111. if (!(cat = calloc(1, sizeof(*cat))))
  112. goto err;
  113. snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
  114. snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
  115. while ((de = readdir(dh)) != NULL)
  116. {
  117. if (!fnmatch(pattern, de->d_name, 0))
  118. {
  119. snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
  120. ar = lmo_open(path);
  121. if (ar)
  122. {
  123. ar->next = cat->archives;
  124. cat->archives = ar;
  125. }
  126. }
  127. }
  128. closedir(dh);
  129. cat->next = _lmo_catalogs;
  130. _lmo_catalogs = cat;
  131. if (!_lmo_active_catalog)
  132. _lmo_active_catalog = cat;
  133. return 0;
  134. err:
  135. if (dh) closedir(dh);
  136. if (cat) free(cat);
  137. return -1;
  138. }
  139. int lmo_change_catalog(const char *lang)
  140. {
  141. lmo_catalog_t *cat;
  142. for (cat = _lmo_catalogs; cat; cat = cat->next)
  143. {
  144. if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
  145. {
  146. _lmo_active_catalog = cat;
  147. return 0;
  148. }
  149. }
  150. return -1;
  151. }
  152. static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
  153. {
  154. unsigned int m, l, r;
  155. uint32_t k;
  156. l = 0;
  157. r = ar->length - 1;
  158. while (1)
  159. {
  160. m = l + ((r - l) / 2);
  161. if (r < l)
  162. break;
  163. k = ntohl(ar->index[m].key_id);
  164. if (k == hash)
  165. return &ar->index[m];
  166. if (k > hash)
  167. {
  168. if (!m)
  169. break;
  170. r = m - 1;
  171. }
  172. else
  173. {
  174. l = m + 1;
  175. }
  176. }
  177. return NULL;
  178. }
  179. int lmo_translate(const char *key, int keylen, char **out, int *outlen)
  180. {
  181. uint32_t hash;
  182. lmo_entry_t *e;
  183. lmo_archive_t *ar;
  184. if (!key || !_lmo_active_catalog)
  185. return -2;
  186. hash = sfh_hash(key, keylen);
  187. for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
  188. {
  189. if ((e = lmo_find_entry(ar, hash)) != NULL)
  190. {
  191. *out = ar->mmap + ntohl(e->offset);
  192. *outlen = ntohl(e->length);
  193. return 0;
  194. }
  195. }
  196. return -1;
  197. }