The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
dbuff.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 /** A generic data buffer structure for encoding and decoding
18 *
19 * Because doing manual length checks is error prone and a waste of everyone's time.
20 *
21 * @file src/lib/util/dbuff.c
22 *
23 * @copyright 2020 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
24 */
25
26RCSID("$Id: 9720e8cbe78dace6acd4fdd2c4e6e8e0b157471e $")
27
28#include <freeradius-devel/util/dbuff.h>
29#include <freeradius-devel/util/syserror.h>
30
31#if defined(STATIC_ANALYZER) || !defined(NDEBUG)
32# define CHECK_DBUFF_INIT(_sbuff) do { if (!(_sbuff)->extend && (unlikely(!(_sbuff)->buff) || unlikely(!(_sbuff)->start) || unlikely(!(_sbuff)->end) || unlikely(!(_sbuff)->p))) return 0; } while (0)
33#else
34# define CHECK_DBUFF_INIT(_sbuff)
35#endif
36
37DIAG_OFF(overlength-strings) /* Seems broken */
38/** Internal macro for defining dbuff move functions
39 * @private
40 */
41#define FR_DBUFF_MOVE_DEF(_out_type, _in_type) \
42size_t _fr_dbuff_move_##_in_type##_to_##_out_type(fr_##_out_type##_t *out, fr_##_in_type##_t *in, size_t len) \
43{ \
44 size_t ext_len, to_copy, remaining = len; \
45 while (remaining > 0) { \
46 to_copy = remaining; \
47 ext_len = fr_dbuff_extend_lowat(NULL, in, to_copy); \
48 if (ext_len < to_copy) to_copy = ext_len; \
49 ext_len = fr_dbuff_extend_lowat(NULL, out, to_copy); \
50 if (ext_len < to_copy) to_copy = ext_len; \
51 if (to_copy == 0) break; \
52 to_copy = _fr_dbuff_safecpy(fr_dbuff_current(out), fr_dbuff_end(out), \
53 fr_dbuff_current(in), fr_dbuff_current(in) + to_copy); \
54 fr_dbuff_advance(out, (size_t)fr_dbuff_advance(in, to_copy)); \
55 remaining -= to_copy; \
56 } \
57 return len - remaining; \
58}
59
60FR_DBUFF_MOVE_DEF(dbuff, dbuff)
61FR_DBUFF_MOVE_DEF(dbuff, dbuff_marker)
62FR_DBUFF_MOVE_DEF(dbuff_marker, dbuff)
63FR_DBUFF_MOVE_DEF(dbuff_marker, dbuff_marker)
64DIAG_ON(overlength-strings)
65
66/** Iterate over the parents of a dbuff
67 *
68 * The iterator cannot be modified.
69 *
70 * @param[in] _dbuff to iterate over.
71 * @param[in] _iter Name of iteration variable.
72 * Will be declared in the scope of the loop.
73 */
74#define fr_dbuff_foreach_parent(_dbuff, _iter) \
75 for (fr_dbuff_t *_iter = (_dbuff); _iter != NULL; _iter = (_iter)->parent)
76
77
78static inline CC_HINT(always_inline) size_t min(size_t x, size_t y)
79{
80 return x < y ? x : y;
81}
82
83/** Update all markers and pointers in the set of dbuffs to point to new_buff
84 *
85 * This function should be used if the underlying buffer is realloced.
86 *
87 * @param[in] dbuff to update.
88 * @param[in] new_buff to assign to to sbuff.
89 * @param[in] new_len Length of the new buffer.
90 */
91void fr_dbuff_update(fr_dbuff_t *dbuff, uint8_t *new_buff, size_t new_len)
92{
93 uint8_t *old_buff; /* Current buff */
94
95 old_buff = dbuff->buff;
96
97 /*
98 * Update pointers to point to positions
99 * in new buffer based on their relative
100 * offsets in the old buffer... but not
101 * past the end of the new buffer.
102 */
103 fr_dbuff_foreach_parent(dbuff, dbuff_i) {
105
106 dbuff_i->buff = new_buff;
107 dbuff_i->start = new_buff + min(new_len, dbuff_i->start - old_buff);
108 dbuff_i->end = dbuff_i->buff + new_len;
109 dbuff_i->p = new_buff + min(new_len, dbuff_i->p - old_buff);
110
111 for (m_i = dbuff_i->m; m_i; m_i = m_i->next) m_i->p = new_buff + min(new_len, m_i->p - old_buff);
112 }
113}
114
115/** Shift the contents of the dbuff, returning the number of bytes we managed to shift
116 *
117 * @param[in] dbuff to shift.
118 * @param[in] shift the number of bytes to shift
119 * (not necessarily the amount actually shifted, because
120 * one can't move pointers outside the buffer)
121 *
122 * @return
123 * - 0 the shift failed due to constraining pointers.
124 * - >0 the number of bytes we managed to shift pointers
125 * in the dbuff.
126 */
127size_t fr_dbuff_shift(fr_dbuff_t *dbuff, size_t shift)
128{
129 uint8_t *buff, *end; /* Current start */
130 size_t max_shift = shift;
131
132 CHECK_DBUFF_INIT(dbuff);
133
134 buff = dbuff->buff;
135 end = dbuff->end;
136
137 /*
138 * First pass: find the maximum shift, which is the minimum
139 * of the distances from buff to any of the current pointers
140 * or current pointers of markers of dbuff and its ancestors.
141 * (We're also constrained by the requested shift count.)
142 */
143 fr_dbuff_foreach_parent(dbuff, dbuff_i) {
145
146 max_shift = min(max_shift, dbuff_i->p - buff);
147 if (!max_shift) return 0;
148
149 for (m_i = dbuff_i->m; m_i; m_i = m_i->next) {
150 max_shift = min(max_shift, m_i->p - buff);
151 if (!max_shift) return 0;
152 }
153 }
154
155 /*
156 * Second pass: adjust pointers.
157 * The first pass means we need only subtract shift from current pointers.
158 * Start pointers can't constrain shift, or we'd never free any space, so
159 * they require the added check.
160 * For d->end, d->buff <= d->p - shift, and d->p shouldn't exceed d->end,
161 * so d->p - shift <= d->end - shift, hence d->buff <= d->end - shift.
162 */
163 fr_dbuff_foreach_parent(dbuff, dbuff_i) {
165 uint8_t *start = dbuff_i->start;
166
167 dbuff_i->start -= min(max_shift, dbuff_i->start - buff);
168 dbuff_i->p -= max_shift;
169 dbuff_i->end -= max_shift;
170 dbuff_i->shifted += (max_shift - (start - dbuff_i->start));
171 for (m_i = dbuff_i->m; m_i; m_i = m_i->next) m_i->p -= max_shift;
172 }
173
174 /*
175 * Move data to track moved pointers.
176 */
177 if ((buff + max_shift) < end) memmove(buff, buff + max_shift, end - (buff + max_shift));
178
179 return max_shift;
180}
181
182/** Refresh the buffer with more data from the file
183 *
184 */
185size_t _fr_dbuff_extend_fd(fr_dbuff_t *dbuff, size_t extension)
186{
187 ssize_t bytes_read;
188 size_t available, total_read;
189 fr_dbuff_uctx_fd_t *fctx;
190
191 CHECK_DBUFF_INIT(dbuff);
192
193 fctx = dbuff->uctx;
194
195 if (extension == SIZE_MAX) extension = 0;
196
197 total_read = dbuff->shifted + (dbuff->end - dbuff->buff);
198 if (total_read >= fctx->max) {
199 fr_strerror_printf("Can't satisfy extension request for %zu bytes", extension);
200 return 0; /* There's no way we could satisfy the extension request */
201 }
202
203 if (fr_dbuff_used(dbuff)) {
204 /*
205 * Try and shift as much as we can out
206 * of the buffer to make space.
207 *
208 * Note: p and markers are constraints here.
209 */
210 fr_dbuff_shift(dbuff, fr_dbuff_used(dbuff));
211 }
212
213 available = min(fctx->buff_end - dbuff->end, fctx->max - total_read);
214 if (available < extension) {
215 fr_strerror_printf("Can't satisfy extension request for %zu bytes", extension);
216 return 0; /* There's no way we could satisfy the extension request */
217 }
218
219 bytes_read = read(fctx->fd, dbuff->end, available);
220
221 /** Check for errors
222 */
223 if (bytes_read < 0) {
224 fr_strerror_printf("Error extending buffer: %s", fr_syserror(errno));
225 return 0;
226 }
227
228 /* Question: should extensible ancestors have their ends adjusted? */
229 dbuff->end += bytes_read; /* Advance end, which increases fr_dbuff_remaining() */
230
231 return bytes_read;
232}
233
234/** Reallocate the current buffer
235 *
236 * @private
237 *
238 * @param[in] dbuff to be extended.
239 * @param[in] extension How many additional bytes should be allocated
240 * in the buffer.
241 * @return
242 * - 0 the extension operation failed.
243 * - >0 the number of bytes the buffer was extended by.
244 */
245size_t _fr_dbuff_extend_talloc(fr_dbuff_t *dbuff, size_t extension)
246{
247 fr_dbuff_uctx_talloc_t *tctx = dbuff->uctx;
248 size_t clen, nlen, elen = extension, reclaimed = 0;
249 uint8_t *new_buff;
250
251 CHECK_DBUFF_INIT(dbuff);
252
253 clen = dbuff->buff ? talloc_array_length(dbuff->buff) : 0;
254
255 /*
256 * fr_dbuff_shift() moves `end` back, but does not reallocate the buffer. That shift leaves the space
257 * between `end` and the end of the buffer as available. Use that space before growing the buffer.
258 * Without this step, a buffer which is already at `max` never sees the space which was shifted out.
259 *
260 * When extending the dbuff, we need to update `end` in this dbuff and in each ancestor which shares the
261 * same `end`. The loop stops after a dbuff that is bounded with FR_DBUFF_ADV_PARENT_END. The parent of
262 * that dbuff uses `end` to mark where the in-buffer data stops, and the data has not changed.
263 */
264 if (dbuff->buff && ((dbuff->buff + clen) > dbuff->end)) {
265 uint8_t *old_end = dbuff->end;
266 fr_dbuff_t *dbuff_i;
267
268 reclaimed = (dbuff->buff + clen) - old_end;
269
270 for (dbuff_i = dbuff; dbuff_i && (dbuff_i->end == old_end); dbuff_i = dbuff_i->parent) {
271 dbuff_i->end = dbuff_i->buff + clen;
272 if (dbuff_i->adv_parent & FR_DBUFF_ADV_PARENT_END) break;
273 }
274
275 if (reclaimed >= extension) return reclaimed;
276
277 elen = extension - reclaimed;
278 }
279
280 /*
281 * If the current buffer size + the extension
282 * is less than init, extend the buffer to init.
283 *
284 * This can happen if the buffer has been
285 * trimmed, and then additional data is added.
286 */
287 if ((clen + elen) < tctx->init) {
288 elen = tctx->init - clen;
289 /*
290 * Double the buffer size if it's more than the
291 * requested amount.
292 */
293 } else if (elen < clen){
294 elen = clen;
295 }
296
297 /*
298 * Check we don't exceed the maximum buffer
299 * length.
300 */
301 if (tctx->max && ((clen + elen) > tctx->max)) {
302 elen = tctx->max - clen;
303 if (elen == 0) {
304 if (reclaimed) return reclaimed;
305
306 fr_strerror_printf("Failed extending buffer by %zu bytes to "
307 "%zu bytes, max is %zu bytes",
308 extension, clen + extension, tctx->max);
309 return 0;
310 }
311 }
312 nlen = clen + elen;
313
314 new_buff = talloc_realloc(tctx->ctx, dbuff->buff, uint8_t, nlen);
315 if (unlikely(!new_buff)) {
316 if (reclaimed) return reclaimed;
317
318 fr_strerror_printf("Failed extending buffer by %zu bytes to %zu bytes", elen, nlen);
319 return 0;
320 }
321
322 (void)fr_dbuff_update(dbuff, new_buff, nlen); /* Shouldn't fail as we're extending */
323
324 /*
325 * When dbuffs are linked in a producer / consumer pair, we can't just change the size of this dbuff.
326 *
327 * After fr_dbuff_update() sets `end` in every ancestor to the end of the new buffer, we have to
328 * check for FR_DBUFF_ADV_PARENT_END, and if so, reset the parents `end` to the childs `p`.
329 */
330 fr_dbuff_foreach_parent(dbuff, dbuff_i) {
331 if (!dbuff_i->parent || !(dbuff_i->adv_parent & FR_DBUFF_ADV_PARENT_END)) continue;
332
333 _fr_dbuff_set_recurse(dbuff_i->parent, dbuff_i->adv_parent, dbuff_i->p);
334 }
335
336 return reclaimed + elen;
337}
338
339/** Trim a talloced dbuff to the minimum length required to represent the contained string
340 *
341 * @param[in] dbuff to trim.
342 * @param[in] len Length to trim to. Passing SIZE_MAX will
343 * result in the buffer being trimmed to the
344 * length of the content.
345 * @return
346 * - 0 on success.
347 * - -1 on failure - markers present pointing past the end of string data.
348 */
350{
351 size_t clen = 0, nlen = 0;
352 uint8_t *new_buff;
353 fr_dbuff_uctx_talloc_t *tctx = dbuff->uctx;
354
355 CHECK_DBUFF_INIT(dbuff);
356
357 if (dbuff->buff) clen = talloc_array_length(dbuff->buff);
358
359 if (len != SIZE_MAX) {
360 nlen += len;
361 } else if (dbuff->buff){
362 nlen += (dbuff->p - dbuff->start);
363 }
364
365 if (nlen != clen) {
366 new_buff = talloc_realloc(tctx->ctx, dbuff->buff, uint8_t, nlen);
367 if (!new_buff) {
368 fr_strerror_printf("Failed trimming buffer from %zu to %zu", clen, nlen);
369 return -1;
370 }
371 fr_dbuff_update(dbuff, new_buff, nlen);
372 }
373
374 return 0;
375}
376
377/** Reset a talloced buffer to its initial length, clearing any data stored
378 *
379 * @param[in] dbuff to reset.
380 * @return
381 * - 0 on success.
382 * - -1 on failure - markers present pointing past the end of string data.
383 */
385{
386 fr_dbuff_uctx_talloc_t *tctx = dbuff->uctx;
387
388 CHECK_DBUFF_INIT(dbuff);
389
390 fr_dbuff_set_to_start(dbuff); /* Clear data */
391
392 /*
393 * If someone trimmed the buffer, then increase its size to the minimum required.
394 */
395 if (fr_dbuff_len(dbuff) < tctx->init) {
396 uint8_t *new_buff;
397
398 new_buff = talloc_realloc(tctx->ctx, dbuff->buff, uint8_t, tctx->init);
399 if (!new_buff) {
400 fr_strerror_printf("Failed reallocing from %zu to %zu",
401 talloc_array_length(dbuff->buff), tctx->init);
402 return -1;
403 }
404 fr_dbuff_update(dbuff, new_buff, tctx->init);
405 }
406
407 return 0;
408}
#define RCSID(id)
Definition build.h:609
#define DIAG_ON(_x)
Definition build.h:582
#define unlikely(_x)
Definition build.h:492
#define DIAG_OFF(_x)
Definition build.h:581
int fr_dbuff_reset_talloc(fr_dbuff_t *dbuff)
Reset a talloced buffer to its initial length, clearing any data stored.
Definition dbuff.c:384
static size_t min(size_t x, size_t y)
Definition dbuff.c:78
int fr_dbuff_trim_talloc(fr_dbuff_t *dbuff, size_t len)
Trim a talloced dbuff to the minimum length required to represent the contained string.
Definition dbuff.c:349
#define CHECK_DBUFF_INIT(_sbuff)
Definition dbuff.c:32
size_t _fr_dbuff_extend_fd(fr_dbuff_t *dbuff, size_t extension)
Refresh the buffer with more data from the file.
Definition dbuff.c:185
void fr_dbuff_update(fr_dbuff_t *dbuff, uint8_t *new_buff, size_t new_len)
Update all markers and pointers in the set of dbuffs to point to new_buff.
Definition dbuff.c:91
#define fr_dbuff_foreach_parent(_dbuff, _iter)
Iterate over the parents of a dbuff.
Definition dbuff.c:74
#define FR_DBUFF_MOVE_DEF(_out_type, _in_type)
Internal macro for defining dbuff move functions.
Definition dbuff.c:41
size_t fr_dbuff_shift(fr_dbuff_t *dbuff, size_t shift)
Shift the contents of the dbuff, returning the number of bytes we managed to shift.
Definition dbuff.c:127
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:819
#define fr_dbuff_len(_dbuff_or_marker)
The length of the underlying buffer.
Definition dbuff.h:828
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_set_to_start(_dbuff_or_marker)
Reset the 'current' position of the dbuff or marker to the 'start' of the buffer.
Definition dbuff.h:1208
#define FR_DBUFF_ADV_PARENT_END
Advance end pointer of parent.
Definition dbuff.h:126
long int ssize_t
unsigned char uint8_t
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
PRIVATE void strings()
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
static fr_sbuff_err_t char size_t * len
Definition value.h:1061