template_lmo.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include <sys/stat.h>
  21. #include <sys/mman.h>
  22. #include <arpa/inet.h>
  23. #include <dirent.h>
  24. #include <fcntl.h>
  25. #include <fnmatch.h>
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <limits.h>
  32. struct lmo_entry {
  33. uint32_t key_id;
  34. uint32_t val_id;
  35. uint32_t offset;
  36. uint32_t length;
  37. } __attribute__((packed));
  38. typedef struct lmo_entry lmo_entry_t;
  39. struct lmo_archive {
  40. size_t length;
  41. const lmo_entry_t *index;
  42. char *data;
  43. const char *end;
  44. struct lmo_archive *next;
  45. };
  46. typedef struct lmo_archive lmo_archive_t;
  47. struct lmo_catalog {
  48. char lang[6];
  49. struct lmo_archive *archives;
  50. struct lmo_catalog *next;
  51. };
  52. typedef struct lmo_catalog lmo_catalog_t;
  53. static inline uint16_t get_le16(const void *data) {
  54. const uint8_t *d = data;
  55. return (((uint16_t)d[1]) << 8) | d[0];
  56. }
  57. static inline uint32_t get_be32(const void *data) {
  58. const uint8_t *d = data;
  59. return (((uint32_t)d[0]) << 24)
  60. | (((uint32_t)d[1]) << 16)
  61. | (((uint32_t)d[2]) << 8)
  62. | d[3];
  63. }
  64. /*
  65. * Hash function from http://www.azillionmonkeys.com/qed/hash.html
  66. * Copyright (C) 2004-2008 by Paul Hsieh
  67. */
  68. static uint32_t sfh_hash(const void *input, size_t len)
  69. {
  70. const uint8_t *data = input;
  71. uint32_t hash = len, tmp;
  72. /* Main loop */
  73. for (; len > 3; len -= 4) {
  74. hash += get_le16(data);
  75. tmp = (get_le16(data+2) << 11) ^ hash;
  76. hash = (hash << 16) ^ tmp;
  77. data += 4;
  78. hash += hash >> 11;
  79. }
  80. /* Handle end cases */
  81. switch (len) {
  82. case 3: hash += get_le16(data);
  83. hash ^= hash << 16;
  84. hash ^= data[2] << 18;
  85. hash += hash >> 11;
  86. break;
  87. case 2: hash += get_le16(data);
  88. hash ^= hash << 11;
  89. hash += hash >> 17;
  90. break;
  91. case 1: hash += *data;
  92. hash ^= hash << 10;
  93. hash += hash >> 1;
  94. }
  95. /* Force "avalanching" of final 127 bits */
  96. hash ^= hash << 3;
  97. hash += hash >> 5;
  98. hash ^= hash << 4;
  99. hash += hash >> 17;
  100. hash ^= hash << 25;
  101. hash += hash >> 6;
  102. return hash;
  103. }
  104. static lmo_archive_t * lmo_open(const char *file)
  105. {
  106. int fd = -1;
  107. lmo_archive_t *ar = NULL;
  108. struct stat s;
  109. fd = open(file, O_RDONLY|O_CLOEXEC);
  110. if (fd < 0)
  111. goto err;
  112. if (fstat(fd, &s))
  113. goto err;
  114. if ((ar = calloc(1, sizeof(*ar))) != NULL) {
  115. ar->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
  116. close(fd);
  117. fd = -1;
  118. if (ar->data == MAP_FAILED)
  119. goto err;
  120. ar->end = ar->data + s.st_size;
  121. uint32_t idx_offset = get_be32(ar->end - sizeof(uint32_t));
  122. ar->index = (const lmo_entry_t *)(ar->data + idx_offset);
  123. if ((const char *)ar->index > (ar->end - sizeof(uint32_t)))
  124. goto err;
  125. ar->length = (ar->end - sizeof(uint32_t) - (const char *)ar->index) / sizeof(lmo_entry_t);
  126. return ar;
  127. }
  128. err:
  129. if (fd >= 0)
  130. close(fd);
  131. if (ar != NULL) {
  132. if ((ar->data != NULL) && (ar->data != MAP_FAILED))
  133. munmap(ar->data, ar->end - ar->data);
  134. free(ar);
  135. }
  136. return NULL;
  137. }
  138. static lmo_catalog_t *lmo_catalogs;
  139. static lmo_catalog_t *lmo_active_catalog;
  140. bool lmo_change_catalog(const char *lang)
  141. {
  142. lmo_catalog_t *cat;
  143. for (cat = lmo_catalogs; cat; cat = cat->next) {
  144. if (!strncmp(cat->lang, lang, sizeof(cat->lang))) {
  145. lmo_active_catalog = cat;
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. bool lmo_load_catalog(const char *lang, const char *dir)
  152. {
  153. DIR *dh = NULL;
  154. char pattern[16];
  155. char path[PATH_MAX];
  156. struct dirent *de = NULL;
  157. lmo_archive_t *ar = NULL;
  158. lmo_catalog_t *cat = NULL;
  159. if (lmo_change_catalog(lang))
  160. return true;
  161. if (!(dh = opendir(dir)))
  162. goto err;
  163. if (!(cat = calloc(1, sizeof(*cat))))
  164. goto err;
  165. snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
  166. snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
  167. while ((de = readdir(dh)) != NULL) {
  168. if (!fnmatch(pattern, de->d_name, 0)) {
  169. snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
  170. ar = lmo_open(path);
  171. if (ar) {
  172. ar->next = cat->archives;
  173. cat->archives = ar;
  174. }
  175. }
  176. }
  177. closedir(dh);
  178. cat->next = lmo_catalogs;
  179. lmo_catalogs = cat;
  180. lmo_active_catalog = cat;
  181. return true;
  182. err:
  183. if (dh)
  184. closedir(dh);
  185. free(cat);
  186. return false;
  187. }
  188. static int lmo_compare_entry(const void *a, const void *b)
  189. {
  190. const lmo_entry_t *ea = a, *eb = b;
  191. uint32_t ka = ntohl(ea->key_id), kb = ntohl(eb->key_id);
  192. if (ka < kb)
  193. return -1;
  194. else if (ka > kb)
  195. return 1;
  196. else
  197. return 0;
  198. }
  199. static const lmo_entry_t * lmo_find_entry(const lmo_archive_t *ar, uint32_t hash)
  200. {
  201. lmo_entry_t key;
  202. key.key_id = htonl(hash);
  203. return bsearch(&key, ar->index, ar->length, sizeof(lmo_entry_t), lmo_compare_entry);
  204. }
  205. bool lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen)
  206. {
  207. if (!lmo_active_catalog)
  208. return false;
  209. uint32_t hash = sfh_hash(key, keylen);
  210. for (const lmo_archive_t *ar = lmo_active_catalog->archives; ar; ar = ar->next) {
  211. const lmo_entry_t *e = lmo_find_entry(ar, hash);
  212. if (!e)
  213. continue;
  214. *out = ar->data + ntohl(e->offset);
  215. *outlen = ntohl(e->length);
  216. if (*out + *outlen > ar->end)
  217. continue;
  218. return true;
  219. }
  220. return false;
  221. }