示例#1
0
int
_citrus_db_open(struct _citrus_db **rdb, struct _region *r, const char *magic,
                _citrus_db_hash_func_t hashfunc, void *hashfunc_closure)
{
    struct _citrus_db *db;
    struct _citrus_db_header_x *dhx;
    struct _memstream ms;

    _memstream_bind(&ms, r);

    /* sanity check */
    dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
    if (dhx == NULL)
        return (EFTYPE);
    if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0)
        return (EFTYPE);
    if (_memstream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET))
        return (EFTYPE);

    if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE >
            _memstream_remainder(&ms))
        return (EFTYPE);

    db = malloc(sizeof(*db));
    if (db == NULL)
        return (errno);
    db->db_region = *r;
    db->db_hashfunc = hashfunc;
    db->db_hashfunc_closure = hashfunc_closure;
    *rdb = db;

    return (0);
}
示例#2
0
int
_citrus_db_get_number_of_entries(struct _citrus_db *db)
{
    struct _citrus_db_header_x *dhx;
    struct _memstream ms;

    _memstream_bind(&ms, &db->db_region);

    dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
    return ((int)be32toh(dhx->dhx_num_entries));
}
static int
parse_file(struct _citrus_mapper_646 *m6, const char *path)
{
	struct _memstream ms;
	struct _region r;
	const char *p;
	char *pp;
	size_t len;
	char buf[PATH_MAX];
	int i, ret;

	ret = _map_file(&r, path);
	if (ret)
		return (ret);
	_memstream_bind(&ms, &r);
	for (i = 0; i < NUM_OF_SPECIALS; i++) {
retry:
		p = _memstream_getln(&ms, &len);
		if (p == NULL) {
			ret = EINVAL;
			break;
		}
		p = _bcs_skip_ws_len(p, &len);
		if (*p == T_COMM || len==0)
			goto retry;
		if (!_bcs_isdigit(*p)) {
			ret = EINVAL;
			break;
		}
		snprintf(buf, sizeof(buf), "%.*s", (int)len, p);
		pp = __DECONST(void *, p);
		m6->m6_map[i] = strtoul(buf, (char **)&pp, 0);
		p = _bcs_skip_ws(buf);
		if (*p != T_COMM && !*p) {
			ret = EINVAL;
			break;
		}
	}
	_unmap_file(&r);

	return (ret);
};
示例#4
0
int
_citrus_db_get_entry(struct _citrus_db *db, int idx,
		     struct _region *key, struct _region *data)
{
	uint32_t num_entries;
	size_t offset;
	struct _memstream ms;
	struct _citrus_db_header_x *dhx;
	struct _citrus_db_entry_x *dex;

	_memstream_bind(&ms, &db->db_region);

	dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
	_DIAGASSERT(dhx);
	num_entries = be32toh(dhx->dhx_num_entries);
	if (idx < 0 || (uint32_t)idx >= num_entries)
		return EINVAL;

	/* seek to the next entry */
	offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE;
	if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
		return EFTYPE;
	/* get the entry record */
	dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
	if (dex == NULL)
		return EFTYPE;
	/* seek to the head of the key. */
	if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET))
		return EFTYPE;
	/* get the region of the key. */
	if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL)
		return EFTYPE;
	/* seek to the head of the data. */
	if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET))
		return EFTYPE;
	/* get the region of the data. */
	if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL)
		return EFTYPE;

	return 0;
}
示例#5
0
static int
seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
		 struct _region *data)
{
	size_t len;
	const char *p;

	cl->cl_rewind = 0;
	free(cl->cl_key);
	cl->cl_key = strdup(key);
	if (cl->cl_ignore_case)
		_bcs_convert_to_lower(cl->cl_key);
	cl->cl_keylen = strlen(cl->cl_key);
	_memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
	p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0);
	if (p == NULL)
		return ENOENT;
	if (data)
		_region_init(data, __UNCONST(p), len);

	return 0;
}
示例#6
0
static int
seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
	       struct _region *data)
{
	const char *p, *q;
	size_t len;

	if (cl->cl_rewind)
		_memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
	cl->cl_rewind = 0;

retry:
	p = _memstream_getln(&cl->cl_plainms, &len);
	if (p == NULL)
		return ENOENT;
	/* ignore comment */
	q = memchr(p, T_COMM, len);
	if (q) {
		len = q-p;
	}
	/* ignore trailing spaces */
	_bcs_trunc_rws_len(p, &len);
	p = _bcs_skip_ws_len(p, &len);
	q = _bcs_skip_nonws_len(p, &len);
	if (p==q)
		goto retry;
	if (cl->cl_key && ((size_t)(q-p) != cl->cl_keylen ||
			   memcmp(p, cl->cl_key, (size_t)(q-p)) != 0))
		goto retry;

	/* found a entry */
	if (key)
		_region_init(key, __UNCONST(p), (size_t)(q-p));
	p = _bcs_skip_ws_len(q, &len);
	if (data)
		_region_init(data, len ? __UNCONST(p) : NULL, len);

	return 0;
}
示例#7
0
int
_citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key,
                  struct _citrus_region *data, struct _citrus_db_locator *dl)
{
    struct _citrus_db_entry_x *dex;
    struct _citrus_db_header_x *dhx;
    struct _citrus_region r;
    struct _memstream ms;
    uint32_t hashval, num_entries;
    size_t offset;

    _memstream_bind(&ms, &db->db_region);

    dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
    num_entries = be32toh(dhx->dhx_num_entries);
    if (num_entries == 0)
        return (ENOENT);

    if (dl != NULL && dl->dl_offset>0) {
        hashval = dl->dl_hashval;
        offset = dl->dl_offset;
        if (offset >= _region_size(&db->db_region))
            return (ENOENT);
    } else {
        hashval = db->db_hashfunc(key)%num_entries;
        offset = be32toh(dhx->dhx_entry_offset) +
                 hashval * _CITRUS_DB_ENTRY_SIZE;
        if (dl)
            dl->dl_hashval = hashval;
    }
    do {
        /* seek to the next entry */
        if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
            return (EFTYPE);
        /* get the entry record */
        dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
        if (dex == NULL)
            return (EFTYPE);

        /* jump to next entry having the same hash value. */
        offset = be32toh(dex->dex_next_offset);

        /* save the current position */
        if (dl) {
            dl->dl_offset = offset;
            if (offset == 0)
                dl->dl_offset = _region_size(&db->db_region);
        }

        /* compare hash value. */
        if (be32toh(dex->dex_hash_value) != hashval)
            /* not found */
            break;
        /* compare key length */
        if (be32toh(dex->dex_key_size) == _region_size(key)) {
            /* seek to the head of the key. */
            if (_memstream_seek(&ms, be32toh(dex->dex_key_offset),
                                SEEK_SET))
                return (EFTYPE);
            /* get the region of the key */
            if (_memstream_getregion(&ms, &r,
                                     _region_size(key)) == NULL)
                return (EFTYPE);
            /* compare key byte stream */
            if (memcmp(_region_head(&r), _region_head(key),
                       _region_size(key)) == 0) {
                /* match */
                if (_memstream_seek(
                            &ms, be32toh(dex->dex_data_offset),
                            SEEK_SET))
                    return (EFTYPE);
                if (_memstream_getregion(
                            &ms, data,
                            be32toh(dex->dex_data_size)) == NULL)
                    return (EFTYPE);
                return (0);
            }
        }
    } while (offset != 0);

    return (ENOENT);
}