template_lmo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * lmo - Lua Machine Objects - Base functions
  3. *
  4. * Copyright (C) 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2018 Matthias Schiffer <mschiffer@universe-factory.net>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "template_lmo.h"
  20. /*
  21. * Hash function from http://www.azillionmonkeys.com/qed/hash.html
  22. * Copyright (C) 2004-2008 by Paul Hsieh
  23. */
  24. static inline uint16_t get_le16(const uint8_t *d) {
  25. return (((uint16_t)d[1]) << 8) | d[0];
  26. }
  27. static uint32_t sfh_hash(const void *input, int len)
  28. {
  29. const uint8_t *data = input;
  30. uint32_t hash = len, tmp;
  31. /* Main loop */
  32. for (; len > 3; len -= 4) {
  33. hash += get_le16(data);
  34. tmp = (get_le16(data+2) << 11) ^ hash;
  35. hash = (hash << 16) ^ tmp;
  36. data += 4;
  37. hash += hash >> 11;
  38. }
  39. /* Handle end cases */
  40. switch (len) {
  41. case 3: hash += get_le16(data);
  42. hash ^= hash << 16;
  43. hash ^= data[2] << 18;
  44. hash += hash >> 11;
  45. break;
  46. case 2: hash += get_le16(data);
  47. hash ^= hash << 11;
  48. hash += hash >> 17;
  49. break;
  50. case 1: hash += *data;
  51. hash ^= hash << 10;
  52. hash += hash >> 1;
  53. }
  54. /* Force "avalanching" of final 127 bits */
  55. hash ^= hash << 3;
  56. hash += hash >> 5;
  57. hash ^= hash << 4;
  58. hash += hash >> 17;
  59. hash ^= hash << 25;
  60. hash += hash >> 6;
  61. return hash;
  62. }
  63. static lmo_archive_t * lmo_open(const char *file)
  64. {
  65. int in = -1;
  66. uint32_t idx_offset = 0;
  67. struct stat s;
  68. lmo_archive_t *ar = NULL;
  69. if ((in = open(file, O_RDONLY|O_CLOEXEC)) == -1)
  70. goto err;
  71. if (fstat(in, &s) == -1)
  72. goto err;
  73. if ((ar = calloc(1, sizeof(*ar))) != NULL) {
  74. ar->fd = in;
  75. ar->size = s.st_size;
  76. if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
  77. goto err;
  78. idx_offset = ntohl(*((const uint32_t *)
  79. (ar->mmap + ar->size - sizeof(uint32_t))));
  80. if (idx_offset >= ar->size)
  81. goto err;
  82. ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
  83. ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
  84. ar->end = ar->mmap + ar->size;
  85. return ar;
  86. }
  87. err:
  88. if (in > -1)
  89. close(in);
  90. if (ar != NULL)
  91. {
  92. if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
  93. munmap(ar->mmap, ar->size);
  94. free(ar);
  95. }
  96. return NULL;
  97. }
  98. static lmo_catalog_t *_lmo_catalogs;
  99. static lmo_catalog_t *_lmo_active_catalog;
  100. int lmo_load_catalog(const char *lang, const char *dir)
  101. {
  102. DIR *dh = NULL;
  103. char pattern[16];
  104. char path[PATH_MAX];
  105. struct dirent *de = NULL;
  106. lmo_archive_t *ar = NULL;
  107. lmo_catalog_t *cat = NULL;
  108. if (!lmo_change_catalog(lang))
  109. return 0;
  110. if (!dir || !(dh = opendir(dir)))
  111. goto err;
  112. if (!(cat = calloc(1, sizeof(*cat))))
  113. goto err;
  114. snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
  115. snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
  116. while ((de = readdir(dh)) != NULL)
  117. {
  118. if (!fnmatch(pattern, de->d_name, 0))
  119. {
  120. snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
  121. ar = lmo_open(path);
  122. if (ar)
  123. {
  124. ar->next = cat->archives;
  125. cat->archives = ar;
  126. }
  127. }
  128. }
  129. closedir(dh);
  130. cat->next = _lmo_catalogs;
  131. _lmo_catalogs = cat;
  132. if (!_lmo_active_catalog)
  133. _lmo_active_catalog = cat;
  134. return 0;
  135. err:
  136. if (dh) closedir(dh);
  137. if (cat) free(cat);
  138. return -1;
  139. }
  140. int lmo_change_catalog(const char *lang)
  141. {
  142. lmo_catalog_t *cat;
  143. for (cat = _lmo_catalogs; cat; cat = cat->next)
  144. {
  145. if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
  146. {
  147. _lmo_active_catalog = cat;
  148. return 0;
  149. }
  150. }
  151. return -1;
  152. }
  153. static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
  154. {
  155. unsigned int m, l, r;
  156. uint32_t k;
  157. l = 0;
  158. r = ar->length - 1;
  159. while (1)
  160. {
  161. m = l + ((r - l) / 2);
  162. if (r < l)
  163. break;
  164. k = ntohl(ar->index[m].key_id);
  165. if (k == hash)
  166. return &ar->index[m];
  167. if (k > hash)
  168. {
  169. if (!m)
  170. break;
  171. r = m - 1;
  172. }
  173. else
  174. {
  175. l = m + 1;
  176. }
  177. }
  178. return NULL;
  179. }
  180. int lmo_translate(const char *key, int keylen, char **out, int *outlen)
  181. {
  182. uint32_t hash;
  183. lmo_entry_t *e;
  184. lmo_archive_t *ar;
  185. if (!key || !_lmo_active_catalog)
  186. return -2;
  187. hash = sfh_hash(key, keylen);
  188. for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
  189. {
  190. if ((e = lmo_find_entry(ar, hash)) != NULL)
  191. {
  192. *out = ar->mmap + ntohl(e->offset);
  193. *outlen = ntohl(e->length);
  194. return 0;
  195. }
  196. }
  197. return -1;
  198. }