Debian Codegraph @

src/OVAL/probes/SEAP/generic/spb.h

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 #pragma once
24 #ifndef SPB_H
25 #define SPB_H
26
27 #include <stddef.h>
28 #include <inttypes.h>
29 #include <common/util.h>
30
31 #define __XC(a, b) OSCAP_CONCAT(a, b)
32 #define _sym(n) __XC(__XC(_sym, __LINE__), n)
33
34 typedef uint32_t spb_flags_t;
35
36 #define SPB_FLAG_FREE 0x00000001
37 #define SPB_FLAG_JOIN 0x00000002
38 #define SPB_FLAG_FILE 0x00000004
39
40 typedef uint64_t spb_size_t;
41 #define SPB_SZ_FMT "%"PRIo64
42
43 typedef struct {
44 void *base; /* buffer base address */
45 spb_size_t gend; /* sparse buffer index of the last byte of this buffer */
46 } spb_item_t;
47
48 typedef struct {
49 spb_item_t *buffer; /* array of buffers representing the sparse buffer */
50 uint32_t btotal; /* number of buffer */
51 uint32_t balloc; /* number of allocated slots */
52 uint32_t bflags; /* flags - not used at the moment */
53 } spb_t;
54
55 #define SPB_DEFAULT_BALLOC 32 /* default number of pre-allocated slots */
56 #define SPB_BALLOC_HIGHTRESH 512 /* the number of new slots is doubled until this limit is reached */
57 #define SPB_BALLOC_ADD 32 /* number of slot to add when we reached the high threshold */
58
59 /**
60 * Create a new sparse buffer.
61 * @param buffer first member of the sparse buffer
62 * @param buflen length of the first buffer
63 * @param balloc allocate space for `balloc' buffers (if more
64 * buffers will be added, the space will be automaticaly
65 * increased). If 0, default value will be used (SPB_DEFAULT_BALLOC).
66 */
67 spb_t *spb_new (void *buffer, size_t buflen, uint32_t balloc);
68
69 void spb_free (spb_t *spb, spb_flags_t flags);
70
71 /**
72 * Given the index of a byte in the sparse buffer, find the index
73 * of the buffer that contains that byte.
74 * @param spb sparse buffer
75 * @param index index of the requested byte in the sparse buffer
76 */
77 uint32_t spb_bindex (spb_t *spb, spb_size_t index);
78
79 /**
80 * Get the size of the sparse buffer
81 * @param spb sparse buffer
82 */
83 spb_size_t spb_size (spb_t *spb);
84
85 /**
86 * Iterate over octets starting at index start and ending at end.
87 * The current octet is available in the `spb_oct' variable during
88 * execution of each iteration.
89 * @param spb sparse buffer
90 * @param start starting index
91 * @param end ending index (-1 means until End-Of-Buffer)
92 */
93 #define spb_iterate_oct(spb, start, end, name) /* TODO */ while(0)
94
95 #define spb_iterate(spb, start, name, icode) \
96 do { \
97 spb_size_t _sym(istart) = (start); \
98 spb_t *_sym(ispb) = (spb); \
99 uint32_t _sym(idx) = spb_bindex(_sym(ispb), _sym(istart)); \
100 size_t _sym(l_off) = (size_t)(_sym(idx) > 0 ? start - _sym(ispb)->buffer[_sym(idx) - 1].gend - 1 : start); \
101 \
102 for (; _sym(idx) < _sym(ispb)->btotal; ++_sym(idx)) { \
103 register size_t _sym(l); \
104 register uint8_t *_sym(b); \
105 \
106 _sym(l) = (size_t)(_sym(idx) > 0 ? \
107 _sym(ispb)->buffer[_sym(idx)].gend - _sym(ispb)->buffer[_sym(idx) - 1].gend : \
108 _sym(ispb)->buffer[_sym(idx)].gend + 1) - _sym(l_off); \
109 _sym(b) = ((uint8_t *)(_sym(ispb)->buffer[_sym(idx)].base)) + _sym(l_off); \
110 \
111 for (; _sym(l) > 0; --_sym(l), ++_sym(b)) { \
112 (name) = *_sym(b); \
113 icode; \
114 } \
115 \
116 if (_sym(l) > 0) \
117 break; \
118 \
119 _sym(l_off) = 0; \
120 } \
121 } while (0)
122
123 /**
124 * Add new buffer to the sparse buffer
125 * @param spb sparse buffer
126 * @param buffer
127 * @param buflen
128 */
129 int spb_add (spb_t *spb, void *buffer, size_t buflen);
130
131 /**
132 * Pick a buffer region from the sparse buffer and copy it.
133 * @param spb sparse buffer
134 * @param start starting index
135 * @param size size of the region
136 * @param dst destination memory
137 */
138 int spb_pick (spb_t *spb, spb_size_t start, spb_size_t size, void *dst);
139
140 /**
141 * Pick a buffer region from the sparse buffer, starting with buffer
142 * at index `bindex' and copy it. If the starting index isn't located
143 * in the given starting buffer, signal an error.
144 * @param spb sparse buffer
145 * @param bindex index of the first buffer where the specified region begins
146 * @param start starting index
147 * @param size size of the region
148 * @param dst destination memory
149 */
150 int spb_pick_raw (spb_t *spb, uint32_t bindex, spb_size_t start, spb_size_t size, void *dst);
151
152 /**
153 * Pick a buffer region from the sparse buffer, starting with buffer
154 * at index `bindex' and copy it. If the starting index isn't located
155 * in the given starting buffer, signal an error.
156 * @param spb sparse buffer
157 * @param start starting index
158 * @param size size of the region
159 * @param cb callback function
160 * @param cbarg argument that will be passed (as the first argument) to
161 * the callback function among with the pointer to the memory
162 * block holding the requested data and the size of this block
163 */
164 int spb_pick_cb (spb_t *spb, spb_size_t start, spb_size_t size, void *cb (void *, void *, size_t), void *cbarg);
165
166 spb_size_t spb_drop_head (spb_t *spb, spb_size_t size, spb_flags_t flags);
167
168 uint8_t spb_octet (spb_t *spb, spb_size_t idx);
169 const uint8_t *spb_direct (spb_t *spb, spb_size_t start, spb_size_t size);
170
171 #endif /* SPB_H */