Coverage Report

Created: 2026-06-05 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/oficonv/libsrc/citrus_db.c
Line
Count
Source
1
/*-
2
 * Copyright (c)2003 Citrus Project,
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include "dcmtk/config/osconfig.h"
28
#include "citrus_db.h"
29
30
#include <sys/types.h>
31
#include <errno.h>
32
#include <limits.h>
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
#include "citrus_bcs.h"
38
#include "citrus_region.h"
39
#include "citrus_memstream.h"
40
#include "citrus_mmap.h"
41
#include "citrus_db_factory.h"
42
#include "citrus_db_file.h"
43
44
struct _citrus_db {
45
    struct _citrus_region       db_region;
46
    _citrus_db_hash_func_t   db_hashfunc;
47
    void            *db_hashfunc_closure;
48
};
49
50
int
51
_citrus_db_open(struct _citrus_db **rdb, struct _citrus_region *r, const char *magic,
52
    _citrus_db_hash_func_t hashfunc, void *hashfunc_closure)
53
0
{
54
0
    struct _citrus_db *db;
55
0
    struct _citrus_db_header_x *dhx;
56
0
    struct _citrus_memory_stream ms;
57
58
0
    _citrus_memory_stream_bind(&ms, r);
59
60
    /* sanity check */
61
0
    dhx = _citrus_memory_stream_getregion(&ms, NULL, sizeof(*dhx));
62
0
    if (dhx == NULL)
63
0
        return (EFTYPE);
64
0
    if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0)
65
0
        return (EFTYPE);
66
0
    if (_citrus_memory_stream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET))
67
0
        return (EFTYPE);
68
69
0
    if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE >
70
0
        _citrus_memory_stream_remainder(&ms))
71
0
        return (EFTYPE);
72
73
0
    db = malloc(sizeof(*db));
74
0
    if (db == NULL)
75
0
        return (errno);
76
0
    db->db_region = *r;
77
0
    db->db_hashfunc = hashfunc;
78
0
    db->db_hashfunc_closure = hashfunc_closure;
79
0
    *rdb = db;
80
81
0
    return (0);
82
0
}
83
84
void
85
_citrus_db_close(struct _citrus_db *db)
86
0
{
87
88
0
    free(db);
89
0
}
90
91
int
92
_citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key,
93
    struct _citrus_region *data, struct _citrus_db_locator *dl)
94
0
{
95
0
    struct _citrus_db_entry_x *dex;
96
0
    struct _citrus_db_header_x *dhx;
97
0
    struct _citrus_region r;
98
0
    struct _citrus_memory_stream ms;
99
0
    uint32_t hashval, num_entries;
100
0
    size_t offset;
101
102
0
    _citrus_memory_stream_bind(&ms, &db->db_region);
103
104
0
    dhx = _citrus_memory_stream_getregion(&ms, NULL, sizeof(*dhx));
105
0
    num_entries = be32toh(dhx->dhx_num_entries);
106
0
    if (num_entries == 0)
107
0
        return (ENOENT);
108
109
0
    if (dl != NULL && dl->dl_offset>0) {
110
0
        hashval = dl->dl_hashval;
111
0
        offset = dl->dl_offset;
112
0
        if (offset >= _citrus_region_size(&db->db_region))
113
0
            return (ENOENT);
114
0
    } else {
115
0
        hashval = db->db_hashfunc(key)%num_entries;
116
0
        offset = be32toh(dhx->dhx_entry_offset) +
117
0
            hashval * _CITRUS_DB_ENTRY_SIZE;
118
0
        if (dl)
119
0
            dl->dl_hashval = hashval;
120
0
    }
121
0
    do {
122
        /* seek to the next entry */
123
0
        if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
124
0
            return (EFTYPE);
125
        /* get the entry record */
126
0
        dex = _citrus_memory_stream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
127
0
        if (dex == NULL)
128
0
            return (EFTYPE);
129
130
        /* jump to next entry having the same hash value. */
131
0
        offset = be32toh(dex->dex_next_offset);
132
133
        /* save the current position */
134
0
        if (dl) {
135
0
            dl->dl_offset = offset;
136
0
            if (offset == 0)
137
0
                dl->dl_offset = _citrus_region_size(&db->db_region);
138
0
        }
139
140
        /* compare hash value. */
141
0
        if (be32toh(dex->dex_hash_value) != hashval)
142
            /* not found */
143
0
            break;
144
        /* compare key length */
145
0
        if (be32toh(dex->dex_key_size) == _citrus_region_size(key)) {
146
            /* seek to the head of the key. */
147
0
            if (_citrus_memory_stream_seek(&ms, be32toh(dex->dex_key_offset),
148
0
                SEEK_SET))
149
0
                return (EFTYPE);
150
            /* get the region of the key */
151
0
            if (_citrus_memory_stream_getregion(&ms, &r,
152
0
                _citrus_region_size(key)) == NULL)
153
0
                return (EFTYPE);
154
            /* compare key byte stream */
155
0
            if (memcmp(_citrus_region_head(&r), _citrus_region_head(key),
156
0
                _citrus_region_size(key)) == 0) {
157
                /* match */
158
0
                if (_citrus_memory_stream_seek(
159
0
                    &ms, be32toh(dex->dex_data_offset),
160
0
                    SEEK_SET))
161
0
                    return (EFTYPE);
162
0
                if (_citrus_memory_stream_getregion(
163
0
                    &ms, data,
164
0
                    be32toh(dex->dex_data_size)) == NULL)
165
0
                    return (EFTYPE);
166
0
                return (0);
167
0
            }
168
0
        }
169
0
    } while (offset != 0);
170
171
0
    return (ENOENT);
172
0
}
173
174
int
175
_citrus_db_lookup_by_string(struct _citrus_db *db, const char *key,
176
    struct _citrus_region *data, struct _citrus_db_locator *dl)
177
0
{
178
0
    struct _citrus_region r;
179
180
0
    _citrus_region_init(&r, CITRUS_DECONST(void *, key), strlen(key));
181
182
0
    return (_citrus_db_lookup(db, &r, data, dl));
183
0
}
184
185
int
186
_citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key,
187
    uint8_t *rval, struct _citrus_db_locator *dl)
188
0
{
189
0
    struct _citrus_region r;
190
0
    int ret;
191
192
0
    ret = _citrus_db_lookup_by_string(db, key, &r, dl);
193
0
    if (ret)
194
0
        return (ret);
195
196
0
    if (_citrus_region_size(&r) != 1)
197
0
        return (EFTYPE);
198
199
0
    if (rval)
200
0
        memcpy(rval, _citrus_region_head(&r), 1);
201
202
0
    return (0);
203
0
}
204
205
int
206
_citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key,
207
    uint16_t *rval, struct _citrus_db_locator *dl)
208
0
{
209
0
    struct _citrus_region r;
210
0
    int ret;
211
0
    uint16_t val;
212
213
0
    ret = _citrus_db_lookup_by_string(db, key, &r, dl);
214
0
    if (ret)
215
0
        return (ret);
216
217
0
    if (_citrus_region_size(&r) != 2)
218
0
        return (EFTYPE);
219
220
0
    if (rval) {
221
0
        memcpy(&val, _citrus_region_head(&r), 2);
222
0
        *rval = be16toh(val);
223
0
    }
224
225
0
    return (0);
226
0
}
227
228
int
229
_citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key,
230
    uint32_t *rval, struct _citrus_db_locator *dl)
231
0
{
232
0
    struct _citrus_region r;
233
0
    uint32_t val;
234
0
    int ret;
235
236
0
    ret = _citrus_db_lookup_by_string(db, key, &r, dl);
237
0
    if (ret)
238
0
        return (ret);
239
240
0
    if (_citrus_region_size(&r) != 4)
241
0
        return (EFTYPE);
242
243
0
    if (rval) {
244
0
        memcpy(&val, _citrus_region_head(&r), 4);
245
0
        *rval = be32toh(val);
246
0
    }
247
248
0
    return (0);
249
0
}
250
251
int
252
_citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key,
253
    const char **rdata, struct _citrus_db_locator *dl)
254
0
{
255
0
    struct _citrus_region r;
256
0
    int ret;
257
258
0
    ret = _citrus_db_lookup_by_string(db, key, &r, dl);
259
0
    if (ret)
260
0
        return (ret);
261
262
    /* check whether the string is null terminated */
263
0
    if (_citrus_region_size(&r) == 0)
264
0
        return (EFTYPE);
265
0
    if (*((const char*)_citrus_region_head(&r)+_citrus_region_size(&r)-1) != '\0')
266
0
        return (EFTYPE);
267
268
0
    if (rdata)
269
0
        *rdata = _citrus_region_head(&r);
270
271
0
    return (0);
272
0
}
273
274
int
275
_citrus_db_get_number_of_entries(struct _citrus_db *db)
276
0
{
277
0
    struct _citrus_db_header_x *dhx;
278
0
    struct _citrus_memory_stream ms;
279
280
0
    _citrus_memory_stream_bind(&ms, &db->db_region);
281
282
0
    dhx = _citrus_memory_stream_getregion(&ms, NULL, sizeof(*dhx));
283
0
    return ((int)be32toh(dhx->dhx_num_entries));
284
0
}
285
286
int
287
_citrus_db_get_entry(struct _citrus_db *db, int idx, struct _citrus_region *key,
288
    struct _citrus_region *data)
289
0
{
290
0
    struct _citrus_db_entry_x *dex;
291
0
    struct _citrus_db_header_x *dhx;
292
0
    struct _citrus_memory_stream ms;
293
0
    uint32_t num_entries;
294
0
    size_t offset;
295
296
0
    _citrus_memory_stream_bind(&ms, &db->db_region);
297
298
0
    dhx = _citrus_memory_stream_getregion(&ms, NULL, sizeof(*dhx));
299
0
    num_entries = be32toh(dhx->dhx_num_entries);
300
0
    if (idx < 0 || (uint32_t)idx >= num_entries)
301
0
        return (EINVAL);
302
303
    /* seek to the next entry */
304
0
    offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE;
305
0
    if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
306
0
        return (EFTYPE);
307
    /* get the entry record */
308
0
    dex = _citrus_memory_stream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
309
0
    if (dex == NULL)
310
0
        return (EFTYPE);
311
    /* seek to the head of the key. */
312
0
    if (_citrus_memory_stream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET))
313
0
        return (EFTYPE);
314
    /* get the region of the key. */
315
0
    if (_citrus_memory_stream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL)
316
0
        return (EFTYPE);
317
    /* seek to the head of the data. */
318
0
    if (_citrus_memory_stream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET))
319
0
        return (EFTYPE);
320
    /* get the region of the data. */
321
0
    if (_citrus_memory_stream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL)
322
0
        return (EFTYPE);
323
324
0
    return (0);
325
0
}