template_lmo.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. struct lmo_entry {
  30. uint32_t key_id;
  31. uint32_t val_id;
  32. uint32_t offset;
  33. uint32_t length;
  34. } __attribute__((packed));
  35. static inline uint16_t get_le16(const void *data) {
  36. const uint8_t *d = data;
  37. return (((uint16_t)d[1]) << 8) | d[0];
  38. }
  39. static inline uint32_t get_be32(const void *data) {
  40. const uint8_t *d = data;
  41. return (((uint32_t)d[0]) << 24)
  42. | (((uint32_t)d[1]) << 16)
  43. | (((uint32_t)d[2]) << 8)
  44. | d[3];
  45. }
  46. /*
  47. * Hash function from http://www.azillionmonkeys.com/qed/hash.html
  48. * Copyright (C) 2004-2008 by Paul Hsieh
  49. */
  50. static uint32_t sfh_hash(const void *input, size_t len)
  51. {
  52. const uint8_t *data = input;
  53. uint32_t hash = len, tmp;
  54. /* Main loop */
  55. for (; len > 3; len -= 4) {
  56. hash += get_le16(data);
  57. tmp = (get_le16(data+2) << 11) ^ hash;
  58. hash = (hash << 16) ^ tmp;
  59. data += 4;
  60. hash += hash >> 11;
  61. }
  62. /* Handle end cases */
  63. switch (len) {
  64. case 3: hash += get_le16(data);
  65. hash ^= hash << 16;
  66. hash ^= data[2] << 18;
  67. hash += hash >> 11;
  68. break;
  69. case 2: hash += get_le16(data);
  70. hash ^= hash << 11;
  71. hash += hash >> 17;
  72. break;
  73. case 1: hash += *data;
  74. hash ^= hash << 10;
  75. hash += hash >> 1;
  76. }
  77. /* Force "avalanching" of final 127 bits */
  78. hash ^= hash << 3;
  79. hash += hash >> 5;
  80. hash ^= hash << 4;
  81. hash += hash >> 17;
  82. hash ^= hash << 25;
  83. hash += hash >> 6;
  84. return hash;
  85. }
  86. bool lmo_load(lmo_catalog_t *cat, const char *file)
  87. {
  88. int fd = -1;
  89. struct stat s;
  90. cat->data = MAP_FAILED;
  91. fd = open(file, O_RDONLY|O_CLOEXEC);
  92. if (fd < 0)
  93. goto err;
  94. if (fstat(fd, &s))
  95. goto err;
  96. cat->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
  97. close(fd);
  98. fd = -1;
  99. if (cat->data == MAP_FAILED)
  100. goto err;
  101. cat->end = cat->data + s.st_size;
  102. uint32_t idx_offset = get_be32(cat->end - sizeof(uint32_t));
  103. cat->index = (const lmo_entry_t *)(cat->data + idx_offset);
  104. if ((const char *)cat->index > (cat->end - sizeof(uint32_t)))
  105. goto err;
  106. cat->length = (cat->end - sizeof(uint32_t) - (const char *)cat->index) / sizeof(lmo_entry_t);
  107. return true;
  108. err:
  109. if (fd >= 0)
  110. close(fd);
  111. if (cat->data != MAP_FAILED)
  112. munmap(cat->data, cat->end - cat->data);
  113. return false;
  114. }
  115. void lmo_unload(lmo_catalog_t *cat)
  116. {
  117. if (cat->data != MAP_FAILED)
  118. munmap(cat->data, cat->end - cat->data);
  119. }
  120. static int lmo_compare_entry(const void *a, const void *b)
  121. {
  122. const lmo_entry_t *ea = a, *eb = b;
  123. uint32_t ka = ntohl(ea->key_id), kb = ntohl(eb->key_id);
  124. if (ka < kb)
  125. return -1;
  126. else if (ka > kb)
  127. return 1;
  128. else
  129. return 0;
  130. }
  131. static const lmo_entry_t * lmo_find_entry(const lmo_catalog_t *cat, uint32_t hash)
  132. {
  133. lmo_entry_t key;
  134. key.key_id = htonl(hash);
  135. return bsearch(&key, cat->index, cat->length, sizeof(lmo_entry_t), lmo_compare_entry);
  136. }
  137. bool lmo_translate(const lmo_catalog_t *cat, const char *key, size_t keylen, const char **out, size_t *outlen)
  138. {
  139. uint32_t hash = sfh_hash(key, keylen);
  140. const lmo_entry_t *e = lmo_find_entry(cat, hash);
  141. if (!e)
  142. return false;
  143. *out = cat->data + ntohl(e->offset);
  144. *outlen = ntohl(e->length);
  145. if (*out + *outlen > cat->end)
  146. return false;
  147. return true;
  148. }