Debian Codegraph @

src/OVAL/probes/SEAP/generic/bfind.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #ifdef OS_WINDOWS
29 #include <io.h>
30 #else
31 #include <unistd.h>
32 #endif
33 #include <stdint.h>
34
35 #include "bfind.h"
36
37 void *bfind (void *ptr, size_t nmemb, size_t size, void *key, int cmpfn (void *, void *))
38 {
39 size_t w, s;
40 int cmp;
41 uint8_t *p;
42
43 w = nmemb;
44 s = 0;
45 p = (uint8_t *)ptr;
46
47 while (w > 0) {
48 cmp = cmpfn (key, p + (size * (s + w/2)));
49 if (cmp > 0) {
50 s += w/2 + 1;
51 w = w - w/2 - 1;
52 } else if (cmp < 0) {
53 w = w/2;
54 } else {
55 return ((void *)(p + (size * (s + w/2))));
56 }
57 }
58
59 return (NULL);
60 }
61
62 ssize_t bfind_i (void *ptr, size_t nmemb, size_t size, void *key, int cmpfn (void *, void *))
63 {
64 size_t w, s;
65 int cmp;
66 uint8_t *p;
67
68 w = nmemb;
69 s = 0;
70 p = (uint8_t *)ptr;
71
72 while (w > 0) {
73 cmp = cmpfn (key, p + (size * (s + w/2)));
74 if (cmp > 0) {
75 s += w/2 + 1;
76 w = w - w/2 - 1;
77 } else if (cmp < 0) {
78 w = w/2;
79 } else {
80 return (s + w/2);
81 }
82 }
83
84 return (-1);
85 }