template_lmo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL)
  72. {
  73. memset(ar, 0, sizeof(*ar));
  74. ar->fd = in;
  75. ar->size = s.st_size;
  76. fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
  77. if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
  78. goto err;
  79. idx_offset = ntohl(*((const uint32_t *)
  80. (ar->mmap + ar->size - sizeof(uint32_t))));
  81. if (idx_offset >= ar->size)
  82. goto err;
  83. ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
  84. ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
  85. ar->end = ar->mmap + ar->size;
  86. return ar;
  87. }
  88. err:
  89. if (in > -1)
  90. close(in);
  91. if (ar != NULL)
  92. {
  93. if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
  94. munmap(ar->mmap, ar->size);
  95. free(ar);
  96. }
  97. return NULL;
  98. }
  99. static lmo_catalog_t *_lmo_catalogs;
  100. static lmo_catalog_t *_lmo_active_catalog;
  101. int lmo_load_catalog(const char *lang, const char *dir)
  102. {
  103. DIR *dh = NULL;
  104. char pattern[16];
  105. char path[PATH_MAX];
  106. struct dirent *de = NULL;
  107. lmo_archive_t *ar = NULL;
  108. lmo_catalog_t *cat = NULL;
  109. if (!lmo_change_catalog(lang))
  110. return 0;
  111. if (!dir || !(dh = opendir(dir)))
  112. goto err;
  113. if (!(cat = malloc(sizeof(*cat))))
  114. goto err;
  115. memset(cat, 0, sizeof(*cat));
  116. snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
  117. snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
  118. while ((de = readdir(dh)) != NULL)
  119. {
  120. if (!fnmatch(pattern, de->d_name, 0))
  121. {
  122. snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
  123. ar = lmo_open(path);
  124. if (ar)
  125. {
  126. ar->next = cat->archives;
  127. cat->archives = ar;
  128. }
  129. }
  130. }
  131. closedir(dh);
  132. cat->next = _lmo_catalogs;
  133. _lmo_catalogs = cat;
  134. if (!_lmo_active_catalog)
  135. _lmo_active_catalog = cat;
  136. return 0;
  137. err:
  138. if (dh) closedir(dh);
  139. if (cat) free(cat);
  140. return -1;
  141. }
  142. int lmo_change_catalog(const char *lang)
  143. {
  144. lmo_catalog_t *cat;
  145. for (cat = _lmo_catalogs; cat; cat = cat->next)
  146. {
  147. if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
  148. {
  149. _lmo_active_catalog = cat;
  150. return 0;
  151. }
  152. }
  153. return -1;
  154. }
  155. static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
  156. {
  157. unsigned int m, l, r;
  158. uint32_t k;
  159. l = 0;
  160. r = ar->length - 1;
  161. while (1)
  162. {
  163. m = l + ((r - l) / 2);
  164. if (r < l)
  165. break;
  166. k = ntohl(ar->index[m].key_id);
  167. if (k == hash)
  168. return &ar->index[m];
  169. if (k > hash)
  170. {
  171. if (!m)
  172. break;
  173. r = m - 1;
  174. }
  175. else
  176. {
  177. l = m + 1;
  178. }
  179. }
  180. return NULL;
  181. }
  182. int lmo_translate(const char *key, int keylen, char **out, int *outlen)
  183. {
  184. uint32_t hash;
  185. lmo_entry_t *e;
  186. lmo_archive_t *ar;
  187. if (!key || !_lmo_active_catalog)
  188. return -2;
  189. hash = sfh_hash(key, keylen);
  190. for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
  191. {
  192. if ((e = lmo_find_entry(ar, hash)) != NULL)
  193. {
  194. *out = ar->mmap + ntohl(e->offset);
  195. *outlen = ntohl(e->length);
  196. return 0;
  197. }
  198. }
  199. return -1;
  200. }