template_lmo.c 4.6 KB

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