template_lmo.c 4.7 KB

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