template_lmo.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. if ((fd = open(file, O_RDONLY|O_CLOEXEC)) == -1)
  110. goto err;
  111. if (fstat(fd, &s) == -1)
  112. goto err;
  113. if ((ar = calloc(1, sizeof(*ar))) != NULL) {
  114. if ((ar->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
  115. goto err;
  116. ar->end = ar->data + s.st_size;
  117. uint32_t idx_offset = get_be32(ar->end - sizeof(uint32_t));
  118. ar->index = (const lmo_entry_t *)(ar->data + idx_offset);
  119. if ((const char *)ar->index > (ar->end - sizeof(uint32_t)))
  120. goto err;
  121. ar->length = (ar->end - sizeof(uint32_t) - (const char *)ar->index) / sizeof(lmo_entry_t);
  122. return ar;
  123. }
  124. err:
  125. if (fd >= 0)
  126. close(fd);
  127. if (ar != NULL) {
  128. if ((ar->data != NULL) && (ar->data != MAP_FAILED))
  129. munmap(ar->data, ar->end - ar->data);
  130. free(ar);
  131. }
  132. return NULL;
  133. }
  134. static lmo_catalog_t *lmo_catalogs;
  135. static lmo_catalog_t *lmo_active_catalog;
  136. bool lmo_change_catalog(const char *lang)
  137. {
  138. lmo_catalog_t *cat;
  139. for (cat = lmo_catalogs; cat; cat = cat->next) {
  140. if (!strncmp(cat->lang, lang, sizeof(cat->lang))) {
  141. lmo_active_catalog = cat;
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. bool lmo_load_catalog(const char *lang, const char *dir)
  148. {
  149. DIR *dh = NULL;
  150. char pattern[16];
  151. char path[PATH_MAX];
  152. struct dirent *de = NULL;
  153. lmo_archive_t *ar = NULL;
  154. lmo_catalog_t *cat = NULL;
  155. if (lmo_change_catalog(lang))
  156. return true;
  157. if (!(dh = opendir(dir)))
  158. goto err;
  159. if (!(cat = calloc(1, sizeof(*cat))))
  160. goto err;
  161. snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
  162. snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
  163. while ((de = readdir(dh)) != NULL) {
  164. if (!fnmatch(pattern, de->d_name, 0)) {
  165. snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
  166. ar = lmo_open(path);
  167. if (ar) {
  168. ar->next = cat->archives;
  169. cat->archives = ar;
  170. }
  171. }
  172. }
  173. closedir(dh);
  174. cat->next = lmo_catalogs;
  175. lmo_catalogs = cat;
  176. lmo_active_catalog = cat;
  177. return true;
  178. err:
  179. if (dh)
  180. closedir(dh);
  181. free(cat);
  182. return false;
  183. }
  184. static int lmo_compare_entry(const void *a, const void *b)
  185. {
  186. const lmo_entry_t *ea = a, *eb = b;
  187. uint32_t ka = ntohl(ea->key_id), kb = ntohl(eb->key_id);
  188. if (ka < kb)
  189. return -1;
  190. else if (ka > kb)
  191. return 1;
  192. else
  193. return 0;
  194. }
  195. static const lmo_entry_t * lmo_find_entry(const lmo_archive_t *ar, uint32_t hash)
  196. {
  197. lmo_entry_t key;
  198. key.key_id = htonl(hash);
  199. return bsearch(&key, ar->index, ar->length, sizeof(lmo_entry_t), lmo_compare_entry);
  200. }
  201. bool lmo_translate(const char *key, size_t keylen, char **out, size_t *outlen)
  202. {
  203. if (!lmo_active_catalog)
  204. return false;
  205. uint32_t hash = sfh_hash(key, keylen);
  206. for (const lmo_archive_t *ar = lmo_active_catalog->archives; ar; ar = ar->next) {
  207. const lmo_entry_t *e = lmo_find_entry(ar, hash);
  208. if (!e)
  209. continue;
  210. *out = ar->data + ntohl(e->offset);
  211. *outlen = ntohl(e->length);
  212. if (*out + *outlen > ar->end)
  213. continue;
  214. return true;
  215. }
  216. return false;
  217. }