2017-02-08 21:14:30 +00:00
|
|
|
/*
|
|
|
|
* lmo - Lua Machine Objects - Base functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
|
2018-02-22 13:23:33 +00:00
|
|
|
* Copyright (C) 2018 Matthias Schiffer <mschiffer@universe-factory.net>
|
2017-02-08 21:14:30 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "template_lmo.h"
|
|
|
|
|
2018-02-22 20:09:35 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
2018-02-22 21:47:27 +00:00
|
|
|
|
2018-02-22 20:09:35 +00:00
|
|
|
#include <arpa/inet.h>
|
2018-02-22 21:47:27 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-02-22 20:09:35 +00:00
|
|
|
|
|
|
|
|
2018-02-22 21:47:27 +00:00
|
|
|
static inline uint16_t get_le16(const void *data) {
|
|
|
|
const uint8_t *d = data;
|
|
|
|
return (((uint16_t)d[1]) << 8) | d[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t get_be32(const void *data) {
|
|
|
|
const uint8_t *d = data;
|
|
|
|
return (((uint32_t)d[0]) << 24)
|
|
|
|
| (((uint32_t)d[1]) << 16)
|
|
|
|
| (((uint32_t)d[2]) << 8)
|
|
|
|
| d[3];
|
|
|
|
}
|
|
|
|
|
2017-02-08 21:14:30 +00:00
|
|
|
/*
|
|
|
|
* Hash function from http://www.azillionmonkeys.com/qed/hash.html
|
|
|
|
* Copyright (C) 2004-2008 by Paul Hsieh
|
|
|
|
*/
|
2019-11-23 13:35:45 +00:00
|
|
|
uint32_t sfh_hash(const void *input, size_t len)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 13:33:47 +00:00
|
|
|
const uint8_t *data = input;
|
2017-02-08 21:14:30 +00:00
|
|
|
uint32_t hash = len, tmp;
|
|
|
|
|
|
|
|
/* Main loop */
|
2018-02-22 13:06:12 +00:00
|
|
|
for (; len > 3; len -= 4) {
|
|
|
|
hash += get_le16(data);
|
|
|
|
tmp = (get_le16(data+2) << 11) ^ hash;
|
2017-02-08 21:14:30 +00:00
|
|
|
hash = (hash << 16) ^ tmp;
|
2018-02-22 13:06:12 +00:00
|
|
|
data += 4;
|
2017-02-08 21:14:30 +00:00
|
|
|
hash += hash >> 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle end cases */
|
2018-02-22 13:06:12 +00:00
|
|
|
switch (len) {
|
|
|
|
case 3: hash += get_le16(data);
|
|
|
|
hash ^= hash << 16;
|
|
|
|
hash ^= data[2] << 18;
|
|
|
|
hash += hash >> 11;
|
|
|
|
break;
|
|
|
|
case 2: hash += get_le16(data);
|
|
|
|
hash ^= hash << 11;
|
|
|
|
hash += hash >> 17;
|
|
|
|
break;
|
|
|
|
case 1: hash += *data;
|
|
|
|
hash ^= hash << 10;
|
|
|
|
hash += hash >> 1;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Force "avalanching" of final 127 bits */
|
|
|
|
hash ^= hash << 3;
|
|
|
|
hash += hash >> 5;
|
|
|
|
hash ^= hash << 4;
|
|
|
|
hash += hash >> 17;
|
|
|
|
hash ^= hash << 25;
|
|
|
|
hash += hash >> 6;
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
bool lmo_load(lmo_catalog_t *cat, const char *file)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 21:47:27 +00:00
|
|
|
int fd = -1;
|
|
|
|
struct stat s;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
cat->data = MAP_FAILED;
|
|
|
|
|
2018-02-22 23:03:57 +00:00
|
|
|
fd = open(file, O_RDONLY|O_CLOEXEC);
|
|
|
|
if (fd < 0)
|
2017-02-08 21:14:30 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-02-22 23:03:57 +00:00
|
|
|
if (fstat(fd, &s))
|
2017-02-08 21:14:30 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
cat->data = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
2018-02-22 23:03:57 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2018-02-22 23:03:57 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
if (cat->data == MAP_FAILED)
|
|
|
|
goto err;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
cat->end = cat->data + s.st_size;
|
2018-02-22 21:47:27 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
uint32_t idx_offset = get_be32(cat->end - sizeof(uint32_t));
|
|
|
|
cat->index = (const lmo_entry_t *)(cat->data + idx_offset);
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
if ((const char *)cat->index > (cat->end - sizeof(uint32_t)))
|
|
|
|
goto err;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
cat->length = (cat->end - sizeof(uint32_t) - (const char *)cat->index) / sizeof(lmo_entry_t);
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
|
|
|
err:
|
2018-02-22 21:47:27 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
if (cat->data != MAP_FAILED)
|
|
|
|
munmap(cat->data, cat->end - cat->data);
|
2018-02-22 21:47:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
void lmo_unload(lmo_catalog_t *cat)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-23 05:26:10 +00:00
|
|
|
if (cat->data != MAP_FAILED)
|
|
|
|
munmap(cat->data, cat->end - cat->data);
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
|
2018-02-22 21:47:27 +00:00
|
|
|
static int lmo_compare_entry(const void *a, const void *b)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 21:47:27 +00:00
|
|
|
const lmo_entry_t *ea = a, *eb = b;
|
|
|
|
uint32_t ka = ntohl(ea->key_id), kb = ntohl(eb->key_id);
|
|
|
|
|
|
|
|
if (ka < kb)
|
|
|
|
return -1;
|
|
|
|
else if (ka > kb)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
static const lmo_entry_t * lmo_find_entry(const lmo_catalog_t *cat, uint32_t hash)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 21:47:27 +00:00
|
|
|
lmo_entry_t key;
|
|
|
|
key.key_id = htonl(hash);
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
return bsearch(&key, cat->index, cat->length, sizeof(lmo_entry_t), lmo_compare_entry);
|
2018-02-22 21:47:27 +00:00
|
|
|
}
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
bool lmo_translate(const lmo_catalog_t *cat, const char *key, size_t keylen, const char **out, size_t *outlen)
|
2018-02-22 21:47:27 +00:00
|
|
|
{
|
|
|
|
uint32_t hash = sfh_hash(key, keylen);
|
2018-02-23 05:26:10 +00:00
|
|
|
const lmo_entry_t *e = lmo_find_entry(cat, hash);
|
|
|
|
if (!e)
|
|
|
|
return false;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
*out = cat->data + ntohl(e->offset);
|
|
|
|
*outlen = ntohl(e->length);
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
if (*out + *outlen > cat->end)
|
|
|
|
return false;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-23 05:26:10 +00:00
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|