suricata
util-streaming-buffer.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2023 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program 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
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "suricata-common.h"
19 #include "util-streaming-buffer.h"
20 #include "util-unittest.h"
21 #include "util-print.h"
22 #include "util-validate.h"
23 #include "util-debug.h"
24 #include "util-error.h"
25 
26 #include "app-layer-htp-mem.h"
27 #include "conf-yaml-loader.h"
28 #include "app-layer-htp.h"
29 
30 static void ListRegions(StreamingBuffer *sb);
31 
32 #define DUMP_REGIONS 0 // set to 1 to dump a visual representation of the regions list and sbb tree.
33 
34 /**
35  * \file
36  *
37  * \author Victor Julien <victor@inliniac.net>
38  *
39  * \brief Streaming Buffer API
40  */
41 
42 static void *ReallocFunc(void *ptr, const size_t size)
43 {
44  void *ptrmem = SCRealloc(ptr, size);
45  if (unlikely(ptrmem == NULL)) {
47  }
48  return ptrmem;
49 }
50 
51 static void *CallocFunc(const size_t nm, const size_t sz)
52 {
53  void *ptrmem = SCCalloc(nm, sz);
54  if (unlikely(ptrmem == NULL)) {
56  }
57  return ptrmem;
58 }
59 
60 /* memory handling wrappers. If config doesn't define it's own set of
61  * functions, use the defaults */
62 // TODO the default allocators don't set `sc_errno` yet.
63 #define CALLOC(cfg, n, s) (cfg)->Calloc ? (cfg)->Calloc((n), (s)) : CallocFunc((n), (s))
64 #define REALLOC(cfg, ptr, orig_s, s) \
65  (cfg)->Realloc ? (cfg)->Realloc((ptr), (orig_s), (s)) : ReallocFunc((ptr), (s))
66 #define FREE(cfg, ptr, s) \
67  (cfg)->Free ? (cfg)->Free((ptr), (s)) : SCFree((ptr))
68 
69 static void SBBFree(StreamingBuffer *sb, const StreamingBufferConfig *cfg);
70 
72 
74 {
75  SCLogDebug("a %" PRIu64 " len %u, b %" PRIu64 " len %u", a->offset, a->len, b->offset, b->len);
76 
77  if (a->offset > b->offset)
78  SCReturnInt(1);
79  else if (a->offset < b->offset)
80  SCReturnInt(-1);
81  else {
82  if (a->len == 0 || b->len == 0 || a->len == b->len)
83  SCReturnInt(0);
84  else if (a->len > b->len)
85  SCReturnInt(1);
86  else
87  SCReturnInt(-1);
88  }
89 }
90 
91 /* inclusive compare function that also considers the right edge,
92  * not just the offset. */
93 static inline int InclusiveCompare(StreamingBufferBlock *lookup, StreamingBufferBlock *intree) {
94  const uint64_t lre = lookup->offset + lookup->len;
95  const uint64_t tre = intree->offset + intree->len;
96  if (lre <= intree->offset) // entirely before
97  return -1;
98  else if (lookup->offset < tre && lre <= tre) // (some) overlap
99  return 0;
100  else
101  return 1; // entirely after
102 }
103 
105 {
106  SCLogDebug("looking up %" PRIu64, elm->offset);
107 
108  struct StreamingBufferBlock *tmp = RB_ROOT(head);
109  struct StreamingBufferBlock *res = NULL;
110  while (tmp) {
111  SCLogDebug("compare with %" PRIu64 "/%u", tmp->offset, tmp->len);
112  const int comp = InclusiveCompare(elm, tmp);
113  SCLogDebug("compare result: %d", comp);
114  if (comp < 0) {
115  res = tmp;
116  tmp = RB_LEFT(tmp, rb);
117  } else if (comp > 0) {
118  tmp = RB_RIGHT(tmp, rb);
119  } else {
120  return tmp;
121  }
122  }
123  return res;
124 }
125 
126 /** \interal
127  * \brief does data region intersect with list region 'r'
128  * Takes the max gap into account.
129  */
130 static inline bool RegionsIntersect(const StreamingBufferConfig *cfg,
131  const StreamingBufferRegion *r, const uint64_t offset, const uint64_t re)
132 {
133  /* create the data range for the region, adding the max gap */
134  const uint64_t reg_o =
135  r->stream_offset > cfg->region_gap ? (r->stream_offset - cfg->region_gap) : 0;
136  const uint64_t reg_re = r->stream_offset + r->buf_size + cfg->region_gap;
137  SCLogDebug("r %p: %" PRIu64 "/%" PRIu64 " - adjusted %" PRIu64 "/%" PRIu64, r, r->stream_offset,
138  r->stream_offset + r->buf_size, reg_o, reg_re);
139  /* check if data range intersects with region range */
140  /* [offset:re] and [reg_o:reg_re] do not intersect if and only if
141  * re < reg_o or if reg_re < offset (one segment is strictly before the other)
142  * trusting that offset<=re and reg_o<=reg_re
143  */
144  return re >= reg_o && reg_re >= offset;
145 }
146 
147 /** \internal
148  * \brief find the first region for merging.
149  */
150 static StreamingBufferRegion *FindFirstRegionForOffset(const StreamingBufferConfig *cfg,
151  StreamingBufferRegion *r, const uint64_t offset, const uint32_t len,
152  StreamingBufferRegion **prev)
153 {
154  const uint64_t data_re = offset + len;
155  SCLogDebug("looking for first region matching %" PRIu64 "/%" PRIu64, offset, data_re);
156 
157  StreamingBufferRegion *p = NULL;
158  for (; r != NULL; r = r->next) {
159  if (RegionsIntersect(cfg, r, offset, data_re)) {
160  *prev = p;
161  return r;
162  }
163  p = r;
164  }
165  *prev = NULL;
166  return NULL;
167 }
168 
169 static StreamingBufferRegion *FindLargestRegionForOffset(const StreamingBufferConfig *cfg,
170  StreamingBufferRegion *r, const uint64_t offset, const uint32_t len)
171 {
172  const uint64_t data_re = offset + len;
173  SCLogDebug("starting at %p/%" PRIu64 ", offset %" PRIu64 ", data_re %" PRIu64, r,
174  r->stream_offset, offset, data_re);
175  StreamingBufferRegion *candidate = r;
176  for (; r != NULL; r = r->next) {
177 #ifdef DEBUG
178  const uint64_t reg_re = r->stream_offset + r->buf_size;
179  SCLogDebug("checking: %p/%" PRIu64 "/%" PRIu64 ", offset %" PRIu64 "/%" PRIu64, r,
180  r->stream_offset, reg_re, offset, data_re);
181 #endif
182  if (!RegionsIntersect(cfg, r, offset, data_re))
183  return candidate;
184 
185  if (r->buf_size > candidate->buf_size) {
186  SCLogDebug("candidate %p as size %u > %u", candidate, r->buf_size, candidate->buf_size);
187  candidate = r;
188  }
189  }
190  return candidate;
191 }
192 
193 static StreamingBufferRegion *FindRightEdge(const StreamingBufferConfig *cfg,
194  StreamingBufferRegion *r, const uint64_t offset, const uint32_t len)
195 {
196  const uint64_t data_re = offset + len;
197  StreamingBufferRegion *candidate = r;
198  for (; r != NULL; r = r->next) {
199  if (!RegionsIntersect(cfg, r, offset, data_re)) {
200  SCLogDebug(
201  "r %p is out of scope: %" PRIu64 "/%u/%" PRIu64, r, offset, len, offset + len);
202  return candidate;
203  }
204  candidate = r;
205  }
206  return candidate;
207 }
208 
209 /** \note uses sc_errno to give error details */
210 static inline StreamingBufferRegion *InitBufferRegion(
211  StreamingBuffer *sb, const StreamingBufferConfig *cfg, const uint32_t min_size)
212 {
213  if ((cfg->max_regions != 0 && sb->regions >= cfg->max_regions) || (sb->regions == UINT16_MAX)) {
214  SCLogDebug("max regions reached");
216  return NULL;
217  }
218 
219  StreamingBufferRegion *aux_r = CALLOC(cfg, 1, sizeof(*aux_r));
220  if (aux_r == NULL) {
221  return NULL;
222  }
223 
224  aux_r->buf = CALLOC(cfg, 1, MAX(cfg->buf_size, min_size));
225  if (aux_r->buf == NULL) {
226  FREE(cfg, aux_r, sizeof(*aux_r));
227  return NULL;
228  }
229  aux_r->buf_size = MAX(cfg->buf_size, min_size);
230  sb->regions++;
231  sb->max_regions = MAX(sb->regions, sb->max_regions);
232  return aux_r;
233 }
234 
235 static inline int InitBuffer(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
236 {
237  sb->region.buf = CALLOC(cfg, 1, cfg->buf_size);
238  if (sb->region.buf == NULL) {
239  return sc_errno;
240  }
241  sb->region.buf_size = cfg->buf_size;
242  return SC_OK;
243 }
244 
246 {
247  StreamingBuffer *sb = CALLOC(cfg, 1, sizeof(StreamingBuffer));
248  if (sb == NULL) {
249  return NULL;
250  }
251 
252  sb->region.buf_size = cfg->buf_size;
253  sb->regions = sb->max_regions = 1;
254 
255  if (cfg->buf_size == 0) {
256  return sb;
257  }
258 
259  int r = InitBuffer(sb, cfg);
260  if (r != SC_OK) {
261  FREE(cfg, sb, sizeof(StreamingBuffer));
262  sc_errno = r;
263  return NULL;
264  }
265  return sb;
266 }
267 
269 {
270  if (sb != NULL) {
271  SCLogDebug("sb->region.buf_size %u max %u", sb->region.buf_size, sb->buf_size_max);
272 
273  SBBFree(sb, cfg);
274  ListRegions(sb);
275  if (sb->region.buf != NULL) {
276  FREE(cfg, sb->region.buf, sb->region.buf_size);
277  sb->region.buf = NULL;
278  }
279 
280  for (StreamingBufferRegion *r = sb->region.next; r != NULL;) {
282  FREE(cfg, r->buf, r->buf_size);
283  FREE(cfg, r, sizeof(*r));
284  r = next;
285  }
286  sb->region.next = NULL;
287  sb->regions = sb->max_regions = 1;
288  }
289 }
290 
292 {
293  if (sb != NULL) {
294  StreamingBufferClear(sb, cfg);
295  FREE(cfg, sb, sizeof(StreamingBuffer));
296  }
297 }
298 
299 #ifdef DEBUG
300 static void SBBPrintList(StreamingBuffer *sb)
301 {
302  StreamingBufferBlock *sbb = NULL;
303  RB_FOREACH(sbb, SBB, &sb->sbb_tree) {
304  SCLogDebug("sbb: offset %" PRIu64 ", len %u", sbb->offset, sbb->len);
305  StreamingBufferBlock *next = SBB_RB_NEXT(sbb);
306  if (next) {
307  if ((sbb->offset + sbb->len) != next->offset) {
308  SCLogDebug("gap: offset %" PRIu64 ", len %" PRIu64, (sbb->offset + sbb->len),
309  next->offset - (sbb->offset + sbb->len));
310  }
311  }
312  }
313 }
314 #endif
315 
316 /* setup with gap between 2 blocks
317  *
318  * [block][gap][block]
319  **/
320 static int WARN_UNUSED SBBInit(StreamingBuffer *sb, const StreamingBufferConfig *cfg,
321  StreamingBufferRegion *region, uint32_t rel_offset, uint32_t data_len)
322 {
324  DEBUG_VALIDATE_BUG_ON(region->buf_offset > region->stream_offset + rel_offset);
325 
326  /* need to set up 2: existing data block and new data block */
327  StreamingBufferBlock *sbb = CALLOC(cfg, 1, sizeof(*sbb));
328  if (sbb == NULL) {
329  return sc_errno;
330  }
331  sbb->offset = sb->region.stream_offset;
332  sbb->len = sb->region.buf_offset;
333  (void)SBB_RB_INSERT(&sb->sbb_tree, sbb);
334  sb->head = sbb;
335  sb->sbb_size = sbb->len;
336 
337  StreamingBufferBlock *sbb2 = CALLOC(cfg, 1, sizeof(*sbb2));
338  if (sbb2 == NULL) {
339  return sc_errno;
340  }
341  sbb2->offset = region->stream_offset + rel_offset;
342  sbb2->len = data_len;
343  DEBUG_VALIDATE_BUG_ON(sbb2->offset < sbb->len);
344  SCLogDebug("sbb1 %" PRIu64 ", len %u, sbb2 %" PRIu64 ", len %u", sbb->offset, sbb->len,
345  sbb2->offset, sbb2->len);
346 
347  sb->sbb_size += sbb2->len;
348  if (SBB_RB_INSERT(&sb->sbb_tree, sbb2) != NULL) {
349  FREE(cfg, sbb2, sizeof(*sbb2));
350  return SC_EINVAL;
351  }
352 
353 #ifdef DEBUG
354  SBBPrintList(sb);
355 #endif
356  return SC_OK;
357 }
358 
359 /* setup with leading gap
360  *
361  * [gap][block]
362  **/
363 static int WARN_UNUSED SBBInitLeadingGap(
364  StreamingBuffer *sb, const StreamingBufferConfig *cfg, uint64_t offset, uint32_t data_len)
365 {
367 
368  StreamingBufferBlock *sbb = CALLOC(cfg, 1, sizeof(*sbb));
369  if (sbb == NULL) {
370  return sc_errno;
371  }
372  sbb->offset = offset;
373  sbb->len = data_len;
374 
375  sb->head = sbb;
376  sb->sbb_size = sbb->len;
377  (void)SBB_RB_INSERT(&sb->sbb_tree, sbb);
378 
379  SCLogDebug("sbb %" PRIu64 ", len %u", sbb->offset, sbb->len);
380 #ifdef DEBUG
381  SBBPrintList(sb);
382 #endif
383  return SC_OK;
384 }
385 
386 static inline void ConsolidateFwd(StreamingBuffer *sb, const StreamingBufferConfig *cfg,
387  StreamingBufferRegion *region, struct SBB *tree, StreamingBufferBlock *sa)
388 {
389  uint64_t sa_re = sa->offset + sa->len;
390  StreamingBufferBlock *tr, *s = sa;
391  RB_FOREACH_FROM(tr, SBB, s) {
392  if (sa == tr)
393  continue;
394 
395  const uint64_t tr_re = tr->offset + tr->len;
396  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u re %" PRIu64, tr, tr->offset, tr->len, tr_re);
397 
398  if (sa_re < tr->offset) {
399  SCLogDebug("entirely before: %" PRIu64 " < %" PRIu64, sa_re, tr->offset);
400  break; // entirely before
401  }
402 
403  /* new block (sa) entirely eclipsed by in tree block (tr)
404  sa: [ ]
405  tr: [ ]
406  sa: [ ]
407  tr: [ ]
408  sa: [ ]
409  tr: [ ]
410  */
411  if (sa->offset >= tr->offset && sa_re <= tr_re) {
412  sb->sbb_size -= sa->len;
413  sa->len = tr->len;
414  sa->offset = tr->offset;
415  sa_re = sa->offset + sa->len;
416  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u REMOVED ECLIPSED (sa overlapped by tr)", tr,
417  tr->offset, tr->len);
418  SBB_RB_REMOVE(tree, tr);
419  FREE(cfg, tr, sizeof(StreamingBufferBlock));
420  /* new block (sa) entire eclipses in tree block (tr)
421  sa: [ ]
422  tr: [ ]
423  sa: [ ]
424  tr: [ ]
425  sa: [ ]
426  tr: [ ]
427  */
428  } else if (sa->offset <= tr->offset && sa_re >= tr_re) {
429  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u REMOVED ECLIPSED (tr overlapped by sa)", tr,
430  tr->offset, tr->len);
431  SBB_RB_REMOVE(tree, tr);
432  sb->sbb_size -= tr->len;
433  FREE(cfg, tr, sizeof(StreamingBufferBlock));
434 
435  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u", sa,
436  sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
437  region->buf_size);
438  if (sa->offset == region->stream_offset &&
439  sa_re > (region->stream_offset + region->buf_offset)) {
440  DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset);
441  DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX);
442  region->buf_offset = (uint32_t)(sa_re - region->stream_offset);
443  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64
444  " bo %u sz %u BUF_OFFSET UPDATED",
445  sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
446  region->buf_size);
447  }
448  /* new block (sa) extended by in tree block (tr)
449  sa: [ ]
450  tr: [ ]
451  sa: [ ]
452  tr: [ ]
453  */
454  } else if (sa->offset < tr->offset && // starts before
455  sa_re >= tr->offset && sa_re < tr_re) // ends inside
456  {
457  // merge. sb->sbb_size includes both so we need to adjust that too.
458  DEBUG_VALIDATE_BUG_ON(sa->len + tr->len > UINT32_MAX);
459  uint32_t combined_len = (uint32_t)(sa->len + tr->len);
460  DEBUG_VALIDATE_BUG_ON(tr_re - sa->offset > UINT32_MAX);
461  sa->len = (uint32_t)(tr_re - sa->offset);
462  sa_re = sa->offset + sa->len;
463  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u REMOVED MERGED", tr, tr->offset, tr->len);
464  SBB_RB_REMOVE(tree, tr);
465  sb->sbb_size -= (combined_len - sa->len); // remove what we added twice
466  FREE(cfg, tr, sizeof(StreamingBufferBlock));
467  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u RESULT", sa, sa->offset, sa->len);
468  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u", sa,
469  sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
470  region->buf_size);
471  if (sa->offset == region->stream_offset &&
472  sa_re > (region->stream_offset + region->buf_offset)) {
473  DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset);
474  DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX);
475  region->buf_offset = (uint32_t)(sa_re - region->stream_offset);
476  SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64
477  " bo %u sz %u BUF_OFFSET UPDATED",
478  sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
479  region->buf_size);
480  }
481  }
482  }
483 }
484 
485 static inline void ConsolidateBackward(StreamingBuffer *sb, const StreamingBufferConfig *cfg,
486  StreamingBufferRegion *region, struct SBB *tree, StreamingBufferBlock *sa)
487 {
488  uint64_t sa_re = sa->offset + sa->len;
489  StreamingBufferBlock *tr, *s = sa;
490  RB_FOREACH_REVERSE_FROM(tr, SBB, s) {
491  if (sa == tr)
492  continue;
493  const uint64_t tr_re = tr->offset + tr->len;
494  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u", tr, tr->offset, tr->len);
495 
496  if (sa->offset > tr_re)
497  break; // entirely after
498 
499  /* new block (sa) entirely eclipsed by in tree block (tr)
500  sa: [ ]
501  tr: [ ]
502  sa: [ ]
503  tr: [ ]
504  sa: [ ]
505  tr: [ ]
506  */
507  if (sa->offset >= tr->offset && sa_re <= tr_re) {
508  sb->sbb_size -= sa->len; // sa entirely eclipsed so remove double accounting
509  sa->len = tr->len;
510  sa->offset = tr->offset;
511  sa_re = sa->offset + sa->len;
512  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u REMOVED ECLIPSED (sa overlapped by tr)", tr,
513  tr->offset, tr->len);
514  if (sb->head == tr)
515  sb->head = sa;
516  SBB_RB_REMOVE(tree, tr);
517  FREE(cfg, tr, sizeof(StreamingBufferBlock));
518  /* new block (sa) entire eclipses in tree block (tr)
519  sa: [ ]
520  tr: [ ]
521  sa: [ ]
522  tr: [ ]
523  sa: [ ]
524  tr: [ ]
525  */
526  } else if (sa->offset <= tr->offset && sa_re >= tr_re) {
527  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u REMOVED ECLIPSED (tr overlapped by sa)", tr,
528  tr->offset, tr->len);
529  if (sb->head == tr)
530  sb->head = sa;
531  SBB_RB_REMOVE(tree, tr);
532  sb->sbb_size -= tr->len; // tr entirely eclipsed so remove double accounting
533  FREE(cfg, tr, sizeof(StreamingBufferBlock));
534 
535  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u", sa,
536  sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
537  region->buf_size);
538 
539  if (sa->offset == region->stream_offset &&
540  sa_re > (region->stream_offset + region->buf_offset)) {
541  DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset);
542  DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX);
543  region->buf_offset = (uint32_t)(sa_re - region->stream_offset);
544  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64
545  " bo %u sz %u BUF_OFFSET UPDATED",
546  sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
547  region->buf_size);
548  }
549 
550  /* new block (sa) extends in tree block (tr)
551  sa: [ ]
552  tr: [ ]
553  sa: [ ]
554  tr: [ ]
555  */
556  } else if (sa->offset > tr->offset && sa_re > tr_re && sa->offset <= tr_re) {
557  // merge. sb->sbb_size includes both so we need to adjust that too.
558  DEBUG_VALIDATE_BUG_ON(sa->len + tr->len > UINT32_MAX);
559  uint32_t combined_len = (uint32_t)(sa->len + tr->len);
560  DEBUG_VALIDATE_BUG_ON(sa_re - tr->offset > UINT32_MAX);
561  sa->len = (uint32_t)(sa_re - tr->offset);
562  sa->offset = tr->offset;
563  sa_re = sa->offset + sa->len;
564  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u REMOVED MERGED", tr, tr->offset, tr->len);
565  if (sb->head == tr)
566  sb->head = sa;
567  SBB_RB_REMOVE(tree, tr);
568  sb->sbb_size -= (combined_len - sa->len); // remove what we added twice
569  FREE(cfg, tr, sizeof(StreamingBufferBlock));
570 
571  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u", sa,
572  sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
573  region->buf_size);
574  if (sa->offset == region->stream_offset &&
575  sa_re > (region->stream_offset + region->buf_offset)) {
576  DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset);
577  DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX);
578  region->buf_offset = (uint32_t)(sa_re - region->stream_offset);
579  SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64
580  " bo %u sz %u BUF_OFFSET UPDATED",
581  sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset,
582  region->buf_size);
583  }
584  }
585  }
586 }
587 
588 /** \internal
589  * \param region the region that holds the new data
590  */
591 static int SBBUpdate(StreamingBuffer *sb, const StreamingBufferConfig *cfg,
592  StreamingBufferRegion *region, uint32_t rel_offset, uint32_t len)
593 {
594  struct SBB *tree = &sb->sbb_tree;
595  SCLogDebug("* inserting: %u/%u", rel_offset, len);
596 
597  StreamingBufferBlock *sbb = CALLOC(cfg, 1, sizeof(*sbb));
598  if (sbb == NULL) {
599  return sc_errno;
600  }
601  sbb->offset = region->stream_offset + rel_offset;
602  sbb->len = len;
603 
604  StreamingBufferBlock *res = SBB_RB_INSERT(tree, sbb);
605  if (res) {
606  // exact overlap
607  SCLogDebug("* insert failed: exact match in tree with %p %" PRIu64 "/%u", res, res->offset,
608  res->len);
609  FREE(cfg, sbb, sizeof(StreamingBufferBlock));
610  return SC_OK;
611  }
612  sb->sbb_size += len; // may adjust based on consolidation below
613 
614  /* handle backwards and forwards overlaps */
615  if (SBB_RB_PREV(sbb) == NULL) {
616  sb->head = sbb;
617  } else {
618  ConsolidateBackward(sb, cfg, region, tree, sbb);
619  }
620  ConsolidateFwd(sb, cfg, region, tree, sbb);
621 #ifdef DEBUG
622  SBBPrintList(sb);
623 #endif
624  if (sbb->offset == region->stream_offset) {
625  SCLogDebug("insert at region head");
626  region->buf_offset = sbb->len;
627  }
628  return SC_OK;
629 }
630 
631 static void SBBFree(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
632 {
633  StreamingBufferBlock *sbb = NULL, *safe = NULL;
634  RB_FOREACH_SAFE(sbb, SBB, &sb->sbb_tree, safe) {
635  SBB_RB_REMOVE(&sb->sbb_tree, sbb);
636  sb->sbb_size -= sbb->len;
637  FREE(cfg, sbb, sizeof(StreamingBufferBlock));
638  }
639  sb->head = NULL;
640 }
641 
642 static void SBBPrune(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
643 {
644  SCLogDebug("pruning %p to %" PRIu64, sb, sb->region.stream_offset);
645  StreamingBufferBlock *sbb = NULL, *safe = NULL;
646  RB_FOREACH_SAFE(sbb, SBB, &sb->sbb_tree, safe) {
647  /* completely beyond window, we're done */
648  if (sbb->offset >= sb->region.stream_offset) {
649  sb->head = sbb;
650  if (sbb->offset == sb->region.stream_offset) {
651  SCLogDebug("set buf_offset to first sbb len %u", sbb->len);
653  sb->region.buf_offset = sbb->len;
654  }
655  break;
656  }
657 
658  /* partly before, partly beyond. Adjust */
659  if (sbb->offset < sb->region.stream_offset &&
660  sbb->offset + sbb->len > sb->region.stream_offset) {
661  DEBUG_VALIDATE_BUG_ON(sb->region.stream_offset - sbb->offset > UINT32_MAX);
662  uint32_t shrink_by = (uint32_t)(sb->region.stream_offset - sbb->offset);
663  DEBUG_VALIDATE_BUG_ON(shrink_by > sbb->len);
664  if (sbb->len >= shrink_by) {
665  sbb->len -= shrink_by;
666  sbb->offset += shrink_by;
667  sb->sbb_size -= shrink_by;
668  SCLogDebug("shrunk by %u", shrink_by);
670  }
671  sb->head = sbb;
672  if (sbb->offset == sb->region.stream_offset) {
673  SCLogDebug("set buf_offset to first sbb len %u", sbb->len);
675  sb->region.buf_offset = sbb->len;
676  }
677  break;
678  }
679 
680  SBB_RB_REMOVE(&sb->sbb_tree, sbb);
681  /* either we set it again for the next sbb, or there isn't any */
682  sb->head = NULL;
683  sb->sbb_size -= sbb->len;
684  SCLogDebug("sb %p removed %p %" PRIu64 ", %u", sb, sbb, sbb->offset, sbb->len);
685  FREE(cfg, sbb, sizeof(StreamingBufferBlock));
686  }
687 #ifdef DEBUG
688  SBBPrintList(sb);
689 #endif
690 }
691 
692 static inline uint32_t ToNextMultipleOf(const uint32_t in, const uint32_t m)
693 {
694  uint32_t r = in;
695  if (m > 0) {
696  const uint32_t x = in % m;
697  if (x != 0) {
698  r = (in - x) + m;
699  }
700  }
701  return r;
702 }
703 
704 static thread_local bool g2s_warn_once = false;
705 
706 static inline int WARN_UNUSED GrowRegionToSize(StreamingBuffer *sb,
707  const StreamingBufferConfig *cfg, StreamingBufferRegion *region, const uint32_t size)
708 {
709  DEBUG_VALIDATE_BUG_ON(region->buf_size > BIT_U32(30));
710  if (size > BIT_U32(30)) { // 1GiB
711  if (!g2s_warn_once) {
712  SCLogWarning("StreamingBuffer::GrowRegionToSize() tried to alloc %u bytes, exceeds "
713  "limit of %" PRIu32,
714  size, BIT_U32(30));
715  g2s_warn_once = true;
716  }
717  return SC_ELIMIT;
718  }
719 
720  /* try to grow in multiples of cfg->buf_size */
721  const uint32_t grow = ToNextMultipleOf(size, cfg->buf_size);
722  SCLogDebug("grow %u", grow);
723  if (grow <= region->buf_size) {
724  // do not try to shrink, and do not memset with diff having unsigned underflow
725  return SC_OK;
726  }
727 
728  void *ptr = REALLOC(cfg, region->buf, region->buf_size, grow);
729  if (ptr == NULL) {
730  if (sc_errno == SC_OK)
732  return sc_errno;
733  }
734  /* for safe printing and general caution, lets memset the
735  * new data to 0 */
736  size_t diff = grow - region->buf_size;
737  void *new_mem = ((char *)ptr) + region->buf_size;
738  memset(new_mem, 0, diff);
739 
740  region->buf = ptr;
741  region->buf_size = grow;
742  SCLogDebug("grown buffer to %u", grow);
743 #ifdef DEBUG
744  if (region->buf_size > sb->buf_size_max) {
745  sb->buf_size_max = region->buf_size;
746  }
747 #endif
748  return SC_OK;
749 }
750 
751 static int WARN_UNUSED GrowToSize(
752  StreamingBuffer *sb, const StreamingBufferConfig *cfg, uint32_t size)
753 {
754  return GrowRegionToSize(sb, cfg, &sb->region, size);
755 }
756 
757 static inline bool RegionBeforeOffset(const StreamingBufferRegion *r, const uint64_t o)
758 {
759  return (r->stream_offset + r->buf_size <= o);
760 }
761 
762 static inline bool RegionContainsOffset(const StreamingBufferRegion *r, const uint64_t o)
763 {
764  return (o >= r->stream_offset && (r->stream_offset + r->buf_size) >= o);
765 }
766 
767 /** \internal
768  * \brief slide to offset for regions
769  *
770  *
771  * [ main ] [ gap ] [ aux ] [ gap ] [ aux ]
772  * ^
773  * - main reset to 0
774  *
775  *
776  * [ main ] [ gap ] [ aux ] [ gap ] [ aux ]
777  * ^
778  * - main shift
779  *
780  * [ main ] [ gap ] [ aux ] [ gap ] [ aux ]
781  * ^
782  * - main reset
783  * - move aux into main
784  * - free aux
785  * - shift
786  *
787  * [ main ] [ gap ] [ aux ] [ gap ] [ aux ]
788  * ^
789  * - main reset
790  * - move aux into main
791  * - free aux
792  * - no shift
793  */
794 static inline void StreamingBufferSlideToOffsetWithRegions(
795  StreamingBuffer *sb, const StreamingBufferConfig *cfg, const uint64_t slide_offset)
796 {
797  ListRegions(sb);
798  DEBUG_VALIDATE_BUG_ON(slide_offset == sb->region.stream_offset);
799 
800  SCLogDebug("slide_offset %" PRIu64, slide_offset);
801  SCLogDebug("main: offset %" PRIu64 " buf %p size %u offset %u", sb->region.stream_offset,
802  sb->region.buf, sb->region.buf_size, sb->region.buf_offset);
803 
804  StreamingBufferRegion *to_shift = NULL;
805  const bool main_is_oow = RegionBeforeOffset(&sb->region, slide_offset);
806  if (main_is_oow) {
807  SCLogDebug("main_is_oow");
808  if (sb->region.buf != NULL) {
809  SCLogDebug("clearing main");
810  FREE(cfg, sb->region.buf, sb->region.buf_size);
811  sb->region.buf = NULL;
812  sb->region.buf_size = 0;
813  sb->region.buf_offset = 0;
814  sb->region.stream_offset = slide_offset;
815  } else {
816  sb->region.stream_offset = slide_offset;
817  }
818 
819  /* remove regions that are out of window & select the region to
820  * become the new main */
821  StreamingBufferRegion *prev = &sb->region;
822  for (StreamingBufferRegion *r = sb->region.next; r != NULL;) {
823  SCLogDebug("r %p so %" PRIu64 ", re %" PRIu64, r, r->stream_offset,
824  r->stream_offset + r->buf_offset);
826  if (RegionBeforeOffset(r, slide_offset)) {
827  SCLogDebug("r %p so %" PRIu64 ", re %" PRIu64 " -> before", r, r->stream_offset,
828  r->stream_offset + r->buf_offset);
829  DEBUG_VALIDATE_BUG_ON(r == &sb->region);
830  prev->next = next;
831 
832  FREE(cfg, r->buf, r->buf_size);
833  FREE(cfg, r, sizeof(*r));
834  sb->regions--;
835  DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
836  } else if (RegionContainsOffset(r, slide_offset)) {
837  SCLogDebug("r %p so %" PRIu64 ", re %" PRIu64 " -> within", r, r->stream_offset,
838  r->stream_offset + r->buf_offset);
839  /* remove from list, we will xfer contents to main below */
840  prev->next = next;
841  to_shift = r;
842  break;
843  } else {
844  SCLogDebug("r %p so %" PRIu64 ", re %" PRIu64 " -> post", r, r->stream_offset,
845  r->stream_offset + r->buf_offset);
846  /* implied beyond slide offset */
847  DEBUG_VALIDATE_BUG_ON(r->stream_offset < slide_offset);
848  break;
849  }
850  r = next;
851  }
852  SCLogDebug("to_shift %p", to_shift);
853 
854  // this region is main, or will xfer its buffer to main
855  if (to_shift && to_shift != &sb->region) {
856  SCLogDebug("main: offset %" PRIu64 " buf %p size %u offset %u", to_shift->stream_offset,
857  to_shift->buf, to_shift->buf_size, to_shift->buf_offset);
858  DEBUG_VALIDATE_BUG_ON(sb->region.buf != NULL);
859 
860  sb->region.buf = to_shift->buf;
861  sb->region.stream_offset = to_shift->stream_offset;
862  sb->region.buf_offset = to_shift->buf_offset;
863  sb->region.buf_size = to_shift->buf_size;
864  sb->region.next = to_shift->next;
865 
866  BUG_ON(to_shift == &sb->region);
867  FREE(cfg, to_shift, sizeof(*to_shift));
868  to_shift = &sb->region;
869  sb->regions--;
870  DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
871  }
872 
873  } else {
874  to_shift = &sb->region;
875  SCLogDebug("shift start region %p", to_shift);
876  }
877 
878  // this region is main, or will xfer its buffer to main
879  if (to_shift) {
880  // Do the shift. If new region is exactly at the slide offset we can skip this.
881  DEBUG_VALIDATE_BUG_ON(to_shift->stream_offset > slide_offset);
882  DEBUG_VALIDATE_BUG_ON(slide_offset - to_shift->stream_offset > UINT32_MAX);
883  const uint32_t s = (uint32_t)(slide_offset - to_shift->stream_offset);
884  if (s > 0) {
885  const uint32_t new_data_size = to_shift->buf_size - s;
886  uint32_t new_mem_size = ToNextMultipleOf(new_data_size, cfg->buf_size);
887 
888  /* see if after the slide we'd overlap with the next region. If so, we need
889  * to consolidate them into one. Error handling is a bit peculiar. We need
890  * to grow a region, move data from another region into it, then free the
891  * other region. This could fail if we're close to the memcap. In this case
892  * we relax our rounding up logic so we only shrink and don't merge the 2
893  * regions after all. */
894  if (to_shift->next && slide_offset + new_mem_size >= to_shift->next->stream_offset) {
895  StreamingBufferRegion *start = to_shift;
896  StreamingBufferRegion *next = start->next;
897  const uint64_t next_re = next->stream_offset + next->buf_size;
898  DEBUG_VALIDATE_BUG_ON(next_re - slide_offset > UINT32_MAX);
899  const uint32_t mem_size =
900  ToNextMultipleOf((uint32_t)(next_re - slide_offset), cfg->buf_size);
901 
902  /* using next as the new main */
903  if (start->buf_size < next->buf_size) {
904  SCLogDebug("replace main with the next bigger region");
905 
906  DEBUG_VALIDATE_BUG_ON(next->stream_offset - slide_offset > UINT32_MAX);
907  const uint32_t next_data_offset =
908  (uint32_t)(next->stream_offset - slide_offset);
909  const uint32_t prev_buf_size = next->buf_size;
910  DEBUG_VALIDATE_BUG_ON(slide_offset - start->stream_offset > UINT32_MAX);
911  const uint32_t start_data_offset =
912  (uint32_t)(slide_offset - start->stream_offset);
913  DEBUG_VALIDATE_BUG_ON(start_data_offset > start->buf_size);
914  if (start_data_offset > start->buf_size) {
915  new_mem_size = new_data_size;
916  goto just_main;
917  }
918  /* expand "next" to include relevant part of "start" */
919  if (GrowRegionToSize(sb, cfg, next, mem_size) != 0) {
920  new_mem_size = new_data_size;
921  goto just_main;
922  }
923  SCLogDebug("region now sized %u", mem_size);
924 
925  // slide "next":
926  // pre-grow: [nextnextnext]
927  // post-grow [nextnextnextXXX]
928  // post-move [XXXnextnextnext]
929  memmove(next->buf + next_data_offset, next->buf, prev_buf_size);
930 
931  // move portion of "start" into "next"
932  //
933  // start: [ooooookkkkk] (o: old, k: keep)
934  // pre-next [xxxxxnextnextnext]
935  // post-next [kkkkknextnextnext]
936  const uint32_t start_data_size = start->buf_size - start_data_offset;
937  memcpy(next->buf, start->buf + start_data_offset, start_data_size);
938 
939  // free "start"s buffer, we will use the one from "next"
940  FREE(cfg, start->buf, start->buf_size);
941 
942  // update "main" to use "next"
943  start->stream_offset = slide_offset;
944  start->buf = next->buf;
945  start->buf_size = next->buf_size;
946  start->next = next->next;
947 
948  // free "next"
949  FREE(cfg, next, sizeof(*next));
950  sb->regions--;
951  DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
952  goto done;
953  } else {
954  /* using "main", expand to include "next" */
955  if (mem_size > start->buf_size) {
956  // Check that start->buf_size is actually not big enough
957  // As mem_size computation and earlier checks do not make it clear.
958  if (GrowRegionToSize(sb, cfg, start, mem_size) != 0) {
959  new_mem_size = new_data_size;
960  goto just_main;
961  }
962  }
963  SCLogDebug("start->buf now size %u", mem_size);
964 
965  // slide "start"
966  // pre: [xxxxxxxAAA]
967  // post: [AAAxxxxxxx]
968  SCLogDebug("s %u new_data_size %u", s, new_data_size);
969  memmove(start->buf, start->buf + s, new_data_size);
970 
971  // copy in "next"
972  // pre: [AAAxxxxxxx]
973  // [BBBBBBB]
974  // post: [AAABBBBBBB]
975  SCLogDebug("copy next->buf %p/%u to start->buf offset %u", next->buf,
976  next->buf_size, new_data_size);
977  memcpy(start->buf + new_data_size, next->buf, next->buf_size);
978 
979  start->stream_offset = slide_offset;
980  start->next = next->next;
981 
982  // free "next"
983  FREE(cfg, next->buf, next->buf_size);
984  FREE(cfg, next, sizeof(*next));
985  sb->regions--;
986  DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
987  goto done;
988  }
989  }
990 
991  just_main:
992  SCLogDebug("s %u new_data_size %u", s, new_data_size);
993  memmove(to_shift->buf, to_shift->buf + s, new_data_size);
994  /* shrink memory region. If this fails we keep the old */
995  void *ptr = REALLOC(cfg, to_shift->buf, to_shift->buf_size, new_mem_size);
996  if (ptr != NULL) {
997  to_shift->buf = ptr;
998  to_shift->buf_size = new_mem_size;
999  SCLogDebug("new buf_size %u", to_shift->buf_size);
1000  }
1001  if (s < to_shift->buf_offset)
1002  to_shift->buf_offset -= s;
1003  else
1004  to_shift->buf_offset = 0;
1005  to_shift->stream_offset = slide_offset;
1006  }
1007  }
1008 
1009 done:
1010  SCLogDebug("main: offset %" PRIu64 " buf %p size %u offset %u", sb->region.stream_offset,
1011  sb->region.buf, sb->region.buf_size, sb->region.buf_offset);
1012  SCLogDebug("end of slide");
1013 }
1014 
1015 /**
1016  * \brief slide to absolute offset
1017  * \todo if sliding beyond window, we could perhaps reset?
1018  */
1020  StreamingBuffer *sb, const StreamingBufferConfig *cfg, uint64_t offset)
1021 {
1022  SCLogDebug("sliding to offset %" PRIu64, offset);
1023  ListRegions(sb);
1024 #ifdef DEBUG
1025  SBBPrintList(sb);
1026 #endif
1027 
1028  if (sb->region.next) {
1029  StreamingBufferSlideToOffsetWithRegions(sb, cfg, offset);
1030  SBBPrune(sb, cfg);
1031  SCLogDebug("post SBBPrune");
1032  ListRegions(sb);
1033 #ifdef DEBUG
1034  SBBPrintList(sb);
1035 #endif
1036  DEBUG_VALIDATE_BUG_ON(sb->region.buf != NULL && sb->region.buf_size == 0);
1040  sb->head->len > sb->region.buf_offset);
1042  return;
1043  }
1044 
1045  if (offset > sb->region.stream_offset) {
1046  DEBUG_VALIDATE_BUG_ON(offset - sb->region.stream_offset > UINT32_MAX);
1047  const uint32_t slide = (uint32_t)(offset - sb->region.stream_offset);
1048  if (sb->head != NULL) {
1049  /* have sbb's, so can't rely on buf_offset for the slide */
1050  if (slide < sb->region.buf_size) {
1051  const uint32_t size = sb->region.buf_size - slide;
1052  SCLogDebug("sliding %u forward, size of original buffer left after slide %u", slide,
1053  size);
1054  memmove(sb->region.buf, sb->region.buf + slide, size);
1055  if (sb->region.buf_offset > slide) {
1056  sb->region.buf_offset -= slide;
1057  } else {
1058  sb->region.buf_offset = 0;
1059  }
1060  } else {
1061  sb->region.buf_offset = 0;
1062  }
1063  sb->region.stream_offset = offset;
1064  } else {
1065  /* no sbb's, so we can use buf_offset */
1066  if (offset <= sb->region.stream_offset + sb->region.buf_offset) {
1067  const uint32_t size = sb->region.buf_offset - slide;
1068  SCLogDebug("sliding %u forward, size of original buffer left after slide %u", slide,
1069  size);
1070  memmove(sb->region.buf, sb->region.buf + slide, size);
1071  sb->region.stream_offset = offset;
1072  sb->region.buf_offset = size;
1073  } else {
1074  /* moved past all data */
1075  sb->region.stream_offset = offset;
1076  sb->region.buf_offset = 0;
1077  }
1078  }
1079  SBBPrune(sb, cfg);
1080  }
1081 #ifdef DEBUG
1082  SBBPrintList(sb);
1083 #endif
1085 }
1086 
1087 static int DataFits(const StreamingBuffer *sb, const uint32_t len)
1088 {
1089  uint64_t buf_offset64 = sb->region.buf_offset;
1090  uint64_t len64 = len;
1091  if (len64 + buf_offset64 > UINT32_MAX) {
1092  return -1;
1093  }
1094  return sb->region.buf_offset + len <= sb->region.buf_size;
1095 }
1096 
1098  StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
1099 {
1100  DEBUG_VALIDATE_BUG_ON(seg == NULL);
1101  DEBUG_VALIDATE_BUG_ON(data_len > BIT_U32(27)); // 128MiB is excessive already
1102 
1103  if (sb->region.buf == NULL) {
1104  if (InitBuffer(sb, cfg) == -1)
1105  return -1;
1106  }
1107 
1108  int r = DataFits(sb, data_len);
1109  if (r < 0) {
1111  return -1;
1112  } else if (r == 0) {
1113  if (sb->region.buf_size == 0) {
1114  if (GrowToSize(sb, cfg, data_len) != SC_OK)
1115  return -1;
1116  } else {
1117  if (GrowToSize(sb, cfg, sb->region.buf_offset + data_len) != SC_OK)
1118  return -1;
1119  }
1120  }
1121  DEBUG_VALIDATE_BUG_ON(DataFits(sb, data_len) != 1);
1122  if (DataFits(sb, data_len) != 1)
1123  return -1;
1124 
1125  memcpy(sb->region.buf + sb->region.buf_offset, data, data_len);
1126  seg->stream_offset = sb->region.stream_offset + sb->region.buf_offset;
1127  seg->segment_len = data_len;
1128  uint32_t rel_offset = sb->region.buf_offset;
1129  sb->region.buf_offset += data_len;
1130 
1131  if (!RB_EMPTY(&sb->sbb_tree)) {
1132  return SBBUpdate(sb, cfg, &sb->region, rel_offset, data_len);
1133  } else {
1134  return 0;
1135  }
1136 }
1137 
1138 /**
1139  * \brief add data w/o tracking a segment
1140  */
1142  const uint8_t *data, uint32_t data_len)
1143 {
1144  DEBUG_VALIDATE_BUG_ON(data_len > BIT_U32(27)); // 128MiB is excessive already
1145 
1146  if (sb->region.buf == NULL) {
1147  if (InitBuffer(sb, cfg) == -1)
1148  return -1;
1149  }
1150 
1151  int r = DataFits(sb, data_len);
1152  if (r < 0) {
1154  return -1;
1155  } else if (r == 0) {
1156  if (sb->region.buf_size == 0) {
1157  if (GrowToSize(sb, cfg, data_len) != SC_OK)
1158  return -1;
1159  } else {
1160  if (GrowToSize(sb, cfg, sb->region.buf_offset + data_len) != SC_OK)
1161  return -1;
1162  }
1163  }
1164  DEBUG_VALIDATE_BUG_ON(DataFits(sb, data_len) != 1);
1165 
1166  memcpy(sb->region.buf + sb->region.buf_offset, data, data_len);
1167  uint32_t rel_offset = sb->region.buf_offset;
1168  sb->region.buf_offset += data_len;
1169 
1170  if (!RB_EMPTY(&sb->sbb_tree)) {
1171  return SBBUpdate(sb, cfg, &sb->region, rel_offset, data_len);
1172  } else {
1173  return 0;
1174  }
1175 }
1176 
1177 static int DataFitsAtOffset(
1178  const StreamingBufferRegion *region, const uint32_t len, const uint32_t offset)
1179 {
1180  const uint64_t offset64 = offset;
1181  const uint64_t len64 = len;
1182  if (offset64 + len64 > UINT32_MAX)
1183  return -1;
1184  return (offset + len <= region->buf_size);
1185 }
1186 
1187 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
1188 static void Validate(const StreamingBuffer *sb)
1189 {
1190  bool bail = false;
1191  uint32_t cnt = 0;
1192  for (const StreamingBufferRegion *r = &sb->region; r != NULL; r = r->next) {
1193  cnt++;
1194  if (r->next) {
1195  bail |= ((r->stream_offset + r->buf_size) > r->next->stream_offset);
1196  }
1197 
1198  bail |= (r->buf != NULL && r->buf_size == 0);
1199  bail |= (r->buf_offset > r->buf_size);
1200  }
1201  // leading gap, so buf_offset should be 0?
1202  if (sb->head && sb->head->offset > sb->region.stream_offset) {
1203  SCLogDebug("leading gap of size %" PRIu64, sb->head->offset - sb->region.stream_offset);
1204  BUG_ON(sb->region.buf_offset != 0);
1205  }
1206 
1207  if (sb->head && sb->head->offset == sb->region.stream_offset) {
1208  BUG_ON(sb->head->len > sb->region.buf_offset);
1209  BUG_ON(sb->head->len < sb->region.buf_offset);
1210  }
1211  BUG_ON(sb->regions != cnt);
1212  BUG_ON(bail);
1213 }
1214 #endif
1215 
1216 static void ListRegions(StreamingBuffer *sb)
1217 {
1218  if (sb->region.buf == NULL && sb->region.buf_offset == 0 && sb->region.next == NULL)
1219  return;
1220 #if defined(DEBUG) && DUMP_REGIONS == 1
1221  uint32_t cnt = 0;
1222  for (StreamingBufferRegion *r = &sb->region; r != NULL; r = r->next) {
1223  cnt++;
1224  char gap[64] = "";
1225  if (r->next) {
1226  snprintf(gap, sizeof(gap), "[ gap:%" PRIu64 " ]",
1227  r->next->stream_offset - (r->stream_offset + r->buf_size));
1228  }
1229 
1230  printf("[ %s offset:%" PRIu64 " size:%u offset:%u ]%s", r == &sb->region ? "main" : "aux",
1231  r->stream_offset, r->buf_size, r->buf_offset, gap);
1232  }
1233  printf("(max %u, cnt %u, sb->regions %u)\n", sb->max_regions, cnt, sb->regions);
1234  bool at_least_one = false;
1235  uint64_t last_re = sb->region.stream_offset;
1236  StreamingBufferBlock *sbb = NULL;
1237  RB_FOREACH(sbb, SBB, &sb->sbb_tree)
1238  {
1239  if (last_re != sbb->offset) {
1240  printf("[ gap:%" PRIu64 " ]", sbb->offset - last_re);
1241  }
1242  printf("[ sbb offset:%" PRIu64 " len:%u ]", sbb->offset, sbb->len);
1243  at_least_one = true;
1244  last_re = sbb->offset + sbb->len;
1245  }
1246  if (at_least_one)
1247  printf("\n");
1248 #endif
1249 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
1250  StreamingBufferBlock *sbb2 = NULL;
1251  RB_FOREACH(sbb2, SBB, &sb->sbb_tree)
1252  {
1253  const uint8_t *_data = NULL;
1254  uint32_t _data_len = 0;
1255  (void)StreamingBufferSBBGetData(sb, sbb2, &_data, &_data_len);
1256  }
1257  Validate(sb);
1258 #endif
1259 }
1260 
1261 /** \internal
1262  * \brief process insert by consolidating the affected regions into one
1263  * \note sets sc_errno
1264  */
1265 static StreamingBufferRegion *BufferInsertAtRegionConsolidate(StreamingBuffer *sb,
1267  StreamingBufferRegion *src_start, StreamingBufferRegion *src_end,
1268  const uint64_t data_offset, const uint32_t data_len, StreamingBufferRegion *prev,
1269  uint32_t dst_buf_size)
1270 {
1271  int retval;
1272 #ifdef DEBUG
1273  const uint64_t data_re = data_offset + data_len;
1274  SCLogDebug("sb %p dst %p src_start %p src_end %p data_offset %" PRIu64
1275  "/data_len %u/data_re %" PRIu64,
1276  sb, dst, src_start, src_end, data_offset, data_len, data_re);
1277 #endif
1278 
1279  // 1. determine size and offset for dst.
1280  const uint64_t dst_offset = MIN(src_start->stream_offset, data_offset);
1281  DEBUG_VALIDATE_BUG_ON(dst_offset < sb->region.stream_offset);
1282  const uint32_t dst_size = dst_buf_size;
1283  SCLogDebug("dst_size %u", dst_size);
1284 
1285  // 2. resize dst
1286  const uint32_t old_size = dst->buf_size;
1287  DEBUG_VALIDATE_BUG_ON(dst->stream_offset - dst_offset > UINT32_MAX);
1288  const uint32_t dst_copy_offset = (uint32_t)(dst->stream_offset - dst_offset);
1289 #ifdef DEBUG
1290  const uint32_t old_offset = dst->buf_offset;
1291  SCLogDebug("old_size %u, old_offset %u, dst_copy_offset %u", old_size, old_offset,
1292  dst_copy_offset);
1293 #endif
1294  if ((retval = GrowRegionToSize(sb, cfg, dst, dst_size)) != SC_OK) {
1295  sc_errno = retval;
1296  return NULL;
1297  }
1298  SCLogDebug("resized to %u -> %u", dst_size, dst->buf_size);
1299  /* validate that the size is exactly what we asked for */
1300  DEBUG_VALIDATE_BUG_ON(dst_size != dst->buf_size);
1301  if (dst_copy_offset != 0)
1302  memmove(dst->buf + dst_copy_offset, dst->buf, old_size);
1303  if (dst_offset != dst->stream_offset) {
1304  dst->stream_offset = dst_offset;
1305  // buf_offset no longer valid, reset.
1306  dst->buf_offset = 0;
1307  }
1308 
1309  uint32_t new_offset = src_start->buf_offset;
1310  if (data_offset == src_start->stream_offset + src_start->buf_offset) {
1311  new_offset += data_len;
1312  }
1313 
1314  bool start_is_main = false;
1315  if (src_start == &sb->region) {
1316  DEBUG_VALIDATE_BUG_ON(src_start->stream_offset != dst_offset);
1317 
1318  start_is_main = true;
1319  SCLogDebug("src_start is main region");
1320  if (src_start != dst)
1321  memcpy(dst->buf, src_start->buf, src_start->buf_offset);
1322  if (src_start == src_end) {
1323  SCLogDebug("src_start == src_end == main, we're done");
1324  DEBUG_VALIDATE_BUG_ON(src_start != dst);
1325  return src_start;
1326  }
1327  prev = src_start;
1328  src_start = src_start->next; // skip in the loop below
1329  }
1330 
1331  // 3. copy all regions from src_start to dst_start into the new region
1332  for (StreamingBufferRegion *r = src_start; r != NULL;) {
1333  SCLogDebug("r %p %" PRIu64 ", offset %u, len %u, %s, last %s", r, r->stream_offset,
1334  r->buf_offset, r->buf_size, r == &sb->region ? "main" : "aux",
1335  BOOL2STR(r == src_end));
1336  // skip dst
1337  if (r == dst) {
1338  SCLogDebug("skipping r %p as it is 'dst'", r);
1339  if (r == src_end)
1340  break;
1341  prev = r;
1342  r = r->next;
1343  continue;
1344  }
1345  DEBUG_VALIDATE_BUG_ON(r->stream_offset - dst_offset > UINT32_MAX);
1346  const uint32_t target_offset = (uint32_t)(r->stream_offset - dst_offset);
1347  SCLogDebug("r %p: target_offset %u", r, target_offset);
1348  DEBUG_VALIDATE_BUG_ON(target_offset > dst->buf_size);
1349  DEBUG_VALIDATE_BUG_ON(target_offset + r->buf_size > dst->buf_size);
1350  memcpy(dst->buf + target_offset, r->buf, r->buf_size);
1351 
1353  FREE(cfg, r->buf, r->buf_size);
1354  FREE(cfg, r, sizeof(*r));
1355  sb->regions--;
1356  DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
1357 
1358  DEBUG_VALIDATE_BUG_ON(prev == NULL && src_start != &sb->region);
1359  if (prev != NULL) {
1360  SCLogDebug("setting prev %p next to %p (was %p)", prev, next, prev->next);
1361  prev->next = next;
1362  } else {
1363  SCLogDebug("no prev yet");
1364  }
1365 
1366  if (r == src_end)
1367  break;
1368  r = next;
1369  }
1370 
1371  /* special handling of main region being the start, but not the
1372  * region we expand. In this case we'll have main and dst. We will
1373  * move the buffer from dst into main and free dst. */
1374  if (start_is_main && dst != &sb->region) {
1376  SCLogDebug("start_is_main && dst != main region");
1377  FREE(cfg, sb->region.buf, sb->region.buf_size);
1378  sb->region.buf = dst->buf;
1379  sb->region.buf_size = dst->buf_size;
1380  sb->region.buf_offset = new_offset;
1381  SCLogDebug("sb->region.buf_offset set to %u", sb->region.buf_offset);
1382  sb->region.next = dst->next;
1383  FREE(cfg, dst, sizeof(*dst));
1384  dst = &sb->region;
1385  sb->regions--;
1386  DEBUG_VALIDATE_BUG_ON(sb->regions == 0);
1387  } else {
1388  SCLogDebug("dst: %p next %p", dst, dst->next);
1389  }
1390 
1391  SCLogDebug("returning dst %p stream_offset %" PRIu64 " buf_offset %u buf_size %u", dst,
1392  dst->stream_offset, dst->buf_offset, dst->buf_size);
1393  return dst;
1394 }
1395 
1396 /** \note sets sc_errno */
1397 static StreamingBufferRegion *BufferInsertAtRegionDo(StreamingBuffer *sb,
1398  const StreamingBufferConfig *cfg, const uint64_t offset, const uint32_t len)
1399 {
1400  SCLogDebug("offset %" PRIu64 ", len %u", offset, len);
1401  StreamingBufferRegion *start_prev = NULL;
1402  StreamingBufferRegion *start =
1403  FindFirstRegionForOffset(cfg, &sb->region, offset, len, &start_prev);
1404  if (start) {
1405  const uint64_t insert_re = offset + len;
1406  const uint64_t insert_start_offset = MIN(start->stream_offset, offset);
1407  uint64_t insert_adjusted_re = insert_re;
1408 
1409  SCLogDebug("start region %p/%" PRIu64 "/%u", start, start->stream_offset, start->buf_size);
1410  StreamingBufferRegion *big = FindLargestRegionForOffset(cfg, start, offset, len);
1411  DEBUG_VALIDATE_BUG_ON(big == NULL);
1412  if (big == NULL) {
1413  sc_errno = SC_EINVAL;
1414  return NULL;
1415  }
1416  SCLogDebug("big region %p/%" PRIu64 "/%u", big, big->stream_offset, big->buf_size);
1417  StreamingBufferRegion *end = FindRightEdge(cfg, big, offset, len);
1418  DEBUG_VALIDATE_BUG_ON(end == NULL);
1419  if (end == NULL) {
1420  sc_errno = SC_EINVAL;
1421  return NULL;
1422  }
1423  insert_adjusted_re = MAX(insert_adjusted_re, (end->stream_offset + end->buf_size));
1424 
1425  DEBUG_VALIDATE_BUG_ON(insert_adjusted_re - insert_start_offset > UINT32_MAX);
1426  uint32_t new_buf_size = ToNextMultipleOf(
1427  (uint32_t)(insert_adjusted_re - insert_start_offset), cfg->buf_size);
1428  SCLogDebug("new_buf_size %u", new_buf_size);
1429 
1430  /* see if our new buf size + cfg->buf_size margin leads to an overlap with the next region.
1431  * If so, include that in the consolidation. */
1432  if (end->next != NULL && new_buf_size + insert_start_offset > end->next->stream_offset) {
1433  SCLogDebug("adjusted end from %p to %p", end, end->next);
1434  end = end->next;
1435  insert_adjusted_re = MAX(insert_adjusted_re, (end->stream_offset + end->buf_size));
1436  DEBUG_VALIDATE_BUG_ON(insert_adjusted_re - insert_start_offset > UINT32_MAX);
1437  new_buf_size = ToNextMultipleOf(
1438  (uint32_t)(insert_adjusted_re - insert_start_offset), cfg->buf_size);
1439  SCLogDebug("new_buf_size %u", new_buf_size);
1440  }
1441 
1442  SCLogDebug("end region %p/%" PRIu64 "/%u", end, end->stream_offset, end->buf_size);
1443  /* sets sc_errno */
1444  StreamingBufferRegion *ret = BufferInsertAtRegionConsolidate(
1445  sb, cfg, big, start, end, offset, len, start_prev, new_buf_size);
1446  return ret;
1447  } else {
1448  /* if there was no region we can use we add a new region and insert it */
1449  StreamingBufferRegion *append = &sb->region;
1450  for (StreamingBufferRegion *r = append; r != NULL; r = r->next) {
1451  if (r->stream_offset > offset) {
1452  break;
1453  } else {
1454  append = r;
1455  }
1456  }
1457 
1458  SCLogDebug("no matching region found, append to %p (%s)", append,
1459  append == &sb->region ? "main" : "aux");
1460  /* sets sc_errno */
1461  StreamingBufferRegion *add = InitBufferRegion(sb, cfg, len);
1462  if (add == NULL)
1463  return NULL;
1464  add->stream_offset = offset;
1465  add->next = append->next;
1466  append->next = add;
1467  SCLogDebug("new region %p offset %" PRIu64, add, add->stream_offset);
1468  return add;
1469  }
1470 }
1471 
1472 /** \internal
1473  * \brief return the region to put the new data in
1474  *
1475  * Will find an existing region, expand it if needed. If no existing region exists or is
1476  * a good fit, it will try to set up a new region. If the region then overlaps or gets
1477  * too close to the next, merge them.
1478  *
1479  * \note sets sc_errno
1480  */
1481 static StreamingBufferRegion *BufferInsertAtRegion(StreamingBuffer *sb,
1482  const StreamingBufferConfig *cfg, const uint32_t data_len, const uint64_t data_offset)
1483 {
1484  SCLogDebug("data_offset %" PRIu64 ", data_len %u, re %" PRIu64, data_offset, data_len,
1485  data_offset + data_len);
1486  ListRegions(sb);
1487 
1488  if (RegionsIntersect(cfg, &sb->region, data_offset, data_offset + data_len)) {
1489  SCLogDebug("data_offset %" PRIu64 ", data_len %u intersects with main region (next %p)",
1490  data_offset, data_len, sb->region.next);
1491  if (sb->region.next == NULL ||
1492  !RegionsIntersect(cfg, sb->region.next, data_offset, data_offset + data_len)) {
1493  SCLogDebug(
1494  "data_offset %" PRIu64
1495  ", data_len %u intersects with main region, no next or way before next region",
1496  data_offset, data_len);
1497  if (sb->region.buf == NULL) {
1498  int r;
1499  if ((r = InitBuffer(sb, cfg)) != SC_OK) { // TODO init with size
1500  sc_errno = r;
1501  return NULL;
1502  }
1503  }
1504  return &sb->region;
1505  }
1506  } else if (sb->region.next == NULL) {
1507  /* InitBufferRegion sets sc_errno */
1508  StreamingBufferRegion *aux_r = sb->region.next = InitBufferRegion(sb, cfg, data_len);
1509  if (aux_r == NULL)
1510  return NULL;
1511  aux_r->stream_offset = data_offset;
1512  DEBUG_VALIDATE_BUG_ON(data_len > aux_r->buf_size);
1513  SCLogDebug("created new region %p with offset %" PRIu64 ", size %u", aux_r,
1514  aux_r->stream_offset, aux_r->buf_size);
1515  return aux_r;
1516  }
1517  /* BufferInsertAtRegionDo sets sc_errno */
1518  StreamingBufferRegion *blob = BufferInsertAtRegionDo(sb, cfg, data_offset, data_len);
1519  SCLogDebug("blob %p (%s)", blob, blob == &sb->region ? "main" : "aux");
1520  return blob;
1521 }
1522 
1523 /**
1524  * \param offset offset relative to StreamingBuffer::stream_offset
1525  *
1526  * \return SC_OK in case of success
1527  * \return SC_E* errors otherwise
1528  */
1530  StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len, uint64_t offset)
1531 {
1532  DEBUG_VALIDATE_BUG_ON(seg == NULL);
1533  DEBUG_VALIDATE_BUG_ON(data_len > BIT_U32(27)); // 128MiB is excessive already
1534  DEBUG_VALIDATE_BUG_ON(offset < sb->region.stream_offset);
1535  if (offset < sb->region.stream_offset) {
1536  return SC_EINVAL;
1537  }
1538 
1539  StreamingBufferRegion *region = BufferInsertAtRegion(sb, cfg, data_len, offset);
1540  if (region == NULL) {
1541  return sc_errno;
1542  }
1543 
1544  const bool region_is_main = region == &sb->region;
1545 
1546  SCLogDebug("inserting %" PRIu64 "/%u using %s region %p", offset, data_len,
1547  region == &sb->region ? "main" : "aux", region);
1548 
1549  DEBUG_VALIDATE_BUG_ON(offset - region->stream_offset > UINT32_MAX);
1550  uint32_t rel_offset = (uint32_t)(offset - region->stream_offset);
1551  int r = DataFitsAtOffset(region, data_len, rel_offset);
1552  if (r < 0) {
1554  return SC_ELIMIT;
1555  } else if (r == 0) {
1556  if ((r = GrowToSize(sb, cfg, (rel_offset + data_len))) != SC_OK)
1557  return r;
1558  }
1559  DEBUG_VALIDATE_BUG_ON(DataFitsAtOffset(region, data_len, rel_offset) != 1);
1560 
1561  SCLogDebug("offset %" PRIu64 " data_len %u, rel_offset %u into region offset %" PRIu64
1562  ", buf_offset %u, buf_size %u",
1563  offset, data_len, rel_offset, region->stream_offset, region->buf_offset,
1564  region->buf_size);
1565  memcpy(region->buf + rel_offset, data, data_len);
1566  seg->stream_offset = offset;
1567  seg->segment_len = data_len;
1568 
1569  SCLogDebug("rel_offset %u region->stream_offset %" PRIu64 ", buf_offset %u", rel_offset,
1570  region->stream_offset, region->buf_offset);
1571 
1572  if (RB_EMPTY(&sb->sbb_tree)) {
1573  SCLogDebug("empty sbb list");
1574 
1575  if (region_is_main) {
1576  if (sb->region.stream_offset == offset) {
1577  SCLogDebug("empty sbb list: block exactly what was expected, fall through");
1578  /* empty list, data is exactly what is expected (append),
1579  * so do nothing.
1580  * Update buf_offset if needed, but in case of overlaps it might be beyond us. */
1581  sb->region.buf_offset = MAX(sb->region.buf_offset, rel_offset + data_len);
1582  } else if ((rel_offset + data_len) <= sb->region.buf_offset) {
1583  SCLogDebug("empty sbb list: block is within existing main data region");
1584  } else {
1585  if (sb->region.buf_offset && rel_offset == sb->region.buf_offset) {
1586  SCLogDebug("exactly at expected offset");
1587  // nothing to do
1588  sb->region.buf_offset = rel_offset + data_len;
1589 
1590  } else if (rel_offset < sb->region.buf_offset) {
1591  // nothing to do
1592 
1593  SCLogDebug("before expected offset: %u < sb->region.buf_offset %u", rel_offset,
1594  sb->region.buf_offset);
1595  if (rel_offset + data_len > sb->region.buf_offset) {
1596  SCLogDebug("before expected offset, ends after: %u < sb->region.buf_offset "
1597  "%u, %u > %u",
1598  rel_offset, sb->region.buf_offset, rel_offset + data_len,
1599  sb->region.buf_offset);
1600  sb->region.buf_offset = rel_offset + data_len;
1601  }
1602 
1603  } else if (sb->region.buf_offset) {
1604  SCLogDebug("beyond expected offset: SBBInit");
1605  /* existing data, but there is a gap between us */
1606  if ((r = SBBInit(sb, cfg, region, rel_offset, data_len)) != SC_OK)
1607  return r;
1608  } else {
1609  /* gap before data in empty list */
1610  SCLogDebug("empty sbb list: invoking SBBInitLeadingGap");
1611  if ((r = SBBInitLeadingGap(sb, cfg, offset, data_len)) != SC_OK)
1612  return r;
1613  }
1614  }
1615  } else {
1616  if (sb->region.buf_offset) {
1617  /* existing data, but there is a gap between us */
1618  SCLogDebug("empty sbb list, no data in main: use SBBInit");
1619  if ((r = SBBInit(sb, cfg, region, rel_offset, data_len)) != SC_OK)
1620  return r;
1621  } else {
1622  /* gap before data in empty list */
1623  SCLogDebug("empty sbb list: invoking SBBInitLeadingGap");
1624  if ((r = SBBInitLeadingGap(sb, cfg, offset, data_len)) != SC_OK)
1625  return r;
1626  }
1627  if (rel_offset == region->buf_offset) {
1628  SCLogDebug("pre region->buf_offset %u", region->buf_offset);
1629  region->buf_offset = rel_offset + data_len;
1630  SCLogDebug("post region->buf_offset %u", region->buf_offset);
1631  }
1632  }
1633  } else {
1634  SCLogDebug("updating sbb tree");
1635  /* already have blocks, so append new block based on new data */
1636  if ((r = SBBUpdate(sb, cfg, region, rel_offset, data_len)) != SC_OK)
1637  return r;
1638  }
1639  DEBUG_VALIDATE_BUG_ON(!region_is_main && sb->head == NULL);
1640 
1641  ListRegions(sb);
1642  if (RB_EMPTY(&sb->sbb_tree)) {
1644  }
1645 
1646  return SC_OK;
1647 }
1648 
1650  const StreamingBufferSegment *seg)
1651 {
1652  if (seg->stream_offset < sb->region.stream_offset) {
1653  if (seg->stream_offset + seg->segment_len <= sb->region.stream_offset) {
1654  return 1;
1655  }
1656  }
1657  return 0;
1658 }
1659 
1660 static inline const StreamingBufferRegion *GetRegionForOffset(
1661  const StreamingBuffer *sb, const uint64_t offset)
1662 {
1663  if (sb == NULL)
1664  return NULL;
1665  if (sb->region.next == NULL) {
1666  return &sb->region;
1667  }
1668  if (offset >= sb->region.stream_offset &&
1669  offset < (sb->region.stream_offset + sb->region.buf_size)) {
1670  return &sb->region;
1671  }
1672  for (const StreamingBufferRegion *r = sb->region.next; r != NULL; r = r->next) {
1673  if (offset >= r->stream_offset && offset < (r->stream_offset + r->buf_size)) {
1674  return r;
1675  }
1676  }
1677  return NULL;
1678 }
1679 
1680 /** \brief get the data for one SBB */
1682  const StreamingBufferBlock *sbb,
1683  const uint8_t **data, uint32_t *data_len)
1684 {
1685  const StreamingBufferRegion *region = GetRegionForOffset(sb, sbb->offset);
1686  SCLogDebug("first find our region (offset %" PRIu64 ") -> %p", sbb->offset, region);
1687  if (region) {
1688  SCLogDebug("region %p found %" PRIu64 "/%u/%u", region, region->stream_offset,
1689  region->buf_size, region->buf_offset);
1691  region->stream_offset == sbb->offset && region->buf_offset > sbb->len);
1692  // buf_offset should match first sbb len if it has the same offset
1693 
1694  if (sbb->offset >= region->stream_offset) {
1695  SCLogDebug("1");
1696  uint64_t offset = sbb->offset - region->stream_offset;
1697  *data = region->buf + offset;
1698  DEBUG_VALIDATE_BUG_ON(offset + sbb->len > region->buf_size);
1699  *data_len = sbb->len;
1700  return;
1701  } else {
1702  SCLogDebug("2");
1703  uint32_t offset = (uint32_t)(region->stream_offset - sbb->offset);
1704  if (offset < sbb->len) {
1705  *data = region->buf;
1706  *data_len = sbb->len - offset;
1707  return;
1708  }
1709  SCLogDebug("3");
1710  }
1711  }
1712  *data = NULL;
1713  *data_len = 0;
1714 }
1715 
1716 /** \brief get the data for one SBB */
1718  const StreamingBufferBlock *sbb,
1719  const uint8_t **data, uint32_t *data_len,
1720  uint64_t offset)
1721 {
1722  /* validate that we are looking for a offset within the sbb */
1723  DEBUG_VALIDATE_BUG_ON(!(offset >= sbb->offset && offset < (sbb->offset + sbb->len)));
1724  if (!(offset >= sbb->offset && offset < (sbb->offset + sbb->len))) {
1725  *data = NULL;
1726  *data_len = 0;
1727  return;
1728  }
1729 
1730  const StreamingBufferRegion *region = GetRegionForOffset(sb, offset);
1731  if (region) {
1732  DEBUG_VALIDATE_BUG_ON(sbb->len - (offset - sbb->offset) > UINT32_MAX);
1733  uint32_t sbblen = (uint32_t)(sbb->len - (offset - sbb->offset));
1734 
1735  if (offset >= region->stream_offset) {
1736  uint32_t data_offset = (uint32_t)(offset - region->stream_offset);
1737  *data = region->buf + data_offset;
1738  if (data_offset + sbblen > region->buf_size) {
1739  *data_len = region->buf_size - data_offset;
1740  } else {
1741  *data_len = sbblen;
1742  }
1743  DEBUG_VALIDATE_BUG_ON(*data_len > sbblen);
1744  return;
1745  } else {
1746  uint32_t data_offset = (uint32_t)(region->stream_offset - sbb->offset);
1747  if (data_offset < sbblen) {
1748  *data = region->buf;
1749  *data_len = sbblen - data_offset;
1750  DEBUG_VALIDATE_BUG_ON(*data_len > sbblen);
1751  return;
1752  }
1753  }
1754  }
1755 
1756  *data = NULL;
1757  *data_len = 0;
1758 }
1759 
1761  const StreamingBufferSegment *seg,
1762  const uint8_t **data, uint32_t *data_len)
1763 {
1764  const StreamingBufferRegion *region = GetRegionForOffset(sb, seg->stream_offset);
1765  if (region) {
1766  if (seg->stream_offset >= region->stream_offset) {
1767  uint32_t offset = (uint32_t)(seg->stream_offset - region->stream_offset);
1768  *data = region->buf + offset;
1769  if (offset + seg->segment_len > region->buf_size) {
1770  *data_len = region->buf_size - offset;
1771  } else {
1772  *data_len = seg->segment_len;
1773  }
1774  SCLogDebug("*data_len %u", *data_len);
1775  return;
1776  } else {
1777  uint32_t offset = (uint32_t)(region->stream_offset - seg->stream_offset);
1778  if (offset < seg->segment_len) {
1779  *data = region->buf;
1780  *data_len = seg->segment_len - offset;
1781  SCLogDebug("*data_len %u", *data_len);
1782  return;
1783  }
1784  }
1785  }
1786  *data = NULL;
1787  *data_len = 0;
1788 }
1789 
1790 /**
1791  * \retval 1 data is the same
1792  * \retval 0 data is different
1793  */
1795  const StreamingBufferSegment *seg,
1796  const uint8_t *rawdata, uint32_t rawdata_len)
1797 {
1798  const uint8_t *segdata = NULL;
1799  uint32_t segdata_len = 0;
1800  StreamingBufferSegmentGetData(sb, seg, &segdata, &segdata_len);
1801  if (segdata && segdata_len &&
1802  segdata_len == rawdata_len &&
1803  memcmp(segdata, rawdata, segdata_len) == 0)
1804  {
1805  return 1;
1806  }
1807  return 0;
1808 }
1809 
1811  const uint8_t **data, uint32_t *data_len,
1812  uint64_t *stream_offset)
1813 {
1814  if (sb != NULL && sb->region.buf != NULL) {
1815  *data = sb->region.buf;
1816  *data_len = sb->region.buf_offset;
1818  return 1;
1819  } else {
1820  *data = NULL;
1821  *data_len = 0;
1822  *stream_offset = 0;
1823  return 0;
1824  }
1825 }
1826 
1828  const uint8_t **data, uint32_t *data_len,
1829  uint64_t offset)
1830 {
1831  const StreamingBufferRegion *region = GetRegionForOffset(sb, offset);
1832  if (region != NULL && region->buf != NULL && offset >= region->stream_offset &&
1833  offset < (region->stream_offset + region->buf_offset)) {
1834  DEBUG_VALIDATE_BUG_ON(offset - region->stream_offset > UINT32_MAX);
1835  uint32_t skip = (uint32_t)(offset - region->stream_offset);
1836  *data = region->buf + skip;
1837  *data_len = region->buf_offset - skip;
1838  return 1;
1839  } else {
1840  *data = NULL;
1841  *data_len = 0;
1842  return 0;
1843  }
1844 }
1845 
1846 /**
1847  * \retval 1 data is the same
1848  * \retval 0 data is different
1849  */
1851  const uint8_t *rawdata, uint32_t rawdata_len)
1852 {
1853  const uint8_t *sbdata = NULL;
1854  uint32_t sbdata_len = 0;
1855  uint64_t offset = 0;
1856  StreamingBufferGetData(sb, &sbdata, &sbdata_len, &offset);
1857  if (offset == 0 &&
1858  sbdata && sbdata_len &&
1859  sbdata_len == rawdata_len &&
1860  memcmp(sbdata, rawdata, sbdata_len) == 0)
1861  {
1862  return 1;
1863  }
1864  SCLogDebug("sbdata_len %u, offset %" PRIu64, sbdata_len, offset);
1865  printf("got:\n");
1866  PrintRawDataFp(stdout, sbdata,sbdata_len);
1867  printf("wanted:\n");
1868  PrintRawDataFp(stdout, rawdata,rawdata_len);
1869  return 0;
1870 }
1871 
1872 #ifdef UNITTESTS
1873 static void Dump(StreamingBuffer *sb)
1874 {
1875  PrintRawDataFp(stdout, sb->region.buf, sb->region.buf_offset);
1876 }
1877 
1878 static void DumpSegment(StreamingBuffer *sb, StreamingBufferSegment *seg)
1879 {
1880  const uint8_t *data = NULL;
1881  uint32_t data_len = 0;
1882  StreamingBufferSegmentGetData(sb, seg, &data, &data_len);
1883  if (data && data_len) {
1884  PrintRawDataFp(stdout, data, data_len);
1885  }
1886 }
1887 
1888 static int StreamingBufferTest02(void)
1889 {
1890  StreamingBufferConfig cfg = { 24, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
1892  FAIL_IF(sb == NULL);
1893 
1894  StreamingBufferSegment seg1;
1895  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg1, (const uint8_t *)"ABCDEFGH", 8) != 0);
1896  StreamingBufferSegment seg2;
1897  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg2, (const uint8_t *)"01234567", 8) != 0);
1898  FAIL_IF(sb->region.stream_offset != 0);
1899  FAIL_IF(sb->region.buf_offset != 16);
1900  FAIL_IF(seg1.stream_offset != 0);
1901  FAIL_IF(seg2.stream_offset != 8);
1904  Dump(sb);
1905  DumpSegment(sb, &seg1);
1906  DumpSegment(sb, &seg2);
1907  FAIL_IF_NOT_NULL(sb->head);
1908  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1909 
1910  StreamingBufferSlideToOffset(sb, &cfg, 6);
1911  FAIL_IF_NOT_NULL(sb->head);
1912  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1913 
1914  StreamingBufferSegment seg3;
1915  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg3, (const uint8_t *)"QWERTY", 6) != 0);
1916  FAIL_IF(sb->region.stream_offset != 6);
1917  FAIL_IF(sb->region.buf_offset != 16);
1918  FAIL_IF(seg3.stream_offset != 16);
1922  Dump(sb);
1923  DumpSegment(sb, &seg1);
1924  DumpSegment(sb, &seg2);
1925  DumpSegment(sb, &seg3);
1926  FAIL_IF_NOT_NULL(sb->head);
1927  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1928 
1929  StreamingBufferSlideToOffset(sb, &cfg, 12);
1933  Dump(sb);
1934  DumpSegment(sb, &seg1);
1935  DumpSegment(sb, &seg2);
1936  DumpSegment(sb, &seg3);
1937  FAIL_IF_NOT_NULL(sb->head);
1938  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1939 
1940  StreamingBufferFree(sb, &cfg);
1941  PASS;
1942 }
1943 
1944 static int StreamingBufferTest03(void)
1945 {
1946  StreamingBufferConfig cfg = { 24, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
1948  FAIL_IF(sb == NULL);
1949 
1950  StreamingBufferSegment seg1;
1951  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg1, (const uint8_t *)"ABCDEFGH", 8) != 0);
1952  StreamingBufferSegment seg2;
1953  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"01234567", 8, 14) != 0);
1954  FAIL_IF(sb->region.stream_offset != 0);
1955  FAIL_IF(sb->region.buf_offset != 8);
1956  FAIL_IF(seg1.stream_offset != 0);
1957  FAIL_IF(seg2.stream_offset != 14);
1960  Dump(sb);
1961  DumpSegment(sb, &seg1);
1962  DumpSegment(sb, &seg2);
1963  FAIL_IF_NULL(sb->head);
1964  FAIL_IF_NOT(sb->sbb_size == 16);
1965  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1966 
1967  StreamingBufferSegment seg3;
1968  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"QWERTY", 6, 8) != 0);
1969  FAIL_IF(sb->region.stream_offset != 0);
1970  FAIL_IF(sb->region.buf_offset != 22);
1971  FAIL_IF(seg3.stream_offset != 8);
1975  Dump(sb);
1976  DumpSegment(sb, &seg1);
1977  DumpSegment(sb, &seg2);
1978  DumpSegment(sb, &seg3);
1979  FAIL_IF_NULL(sb->head);
1980  FAIL_IF_NOT(sb->sbb_size == 22);
1981  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1982 
1983  StreamingBufferSlideToOffset(sb, &cfg, 10);
1987  Dump(sb);
1988  DumpSegment(sb, &seg1);
1989  DumpSegment(sb, &seg2);
1990  DumpSegment(sb, &seg3);
1991  FAIL_IF_NULL(sb->head);
1992  FAIL_IF_NOT(sb->sbb_size == 12);
1993  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
1994 
1995  StreamingBufferFree(sb, &cfg);
1996  PASS;
1997 }
1998 
1999 static int StreamingBufferTest04(void)
2000 {
2001  StreamingBufferConfig cfg = { 16, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2003  FAIL_IF(sb == NULL);
2004 
2005  StreamingBufferSegment seg1;
2006  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg1, (const uint8_t *)"ABCDEFGH", 8) != 0);
2007  FAIL_IF(!RB_EMPTY(&sb->sbb_tree));
2008  StreamingBufferSegment seg2;
2009  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"01234567", 8, 14) != 0);
2010  FAIL_IF(sb->region.stream_offset != 0);
2011  FAIL_IF(sb->region.buf_offset != 8);
2012  FAIL_IF(seg1.stream_offset != 0);
2013  FAIL_IF(seg2.stream_offset != 14);
2016  FAIL_IF(RB_EMPTY(&sb->sbb_tree));
2017  StreamingBufferBlock *sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2018  FAIL_IF(sbb1 != sb->head);
2019  FAIL_IF_NULL(sbb1);
2020  FAIL_IF(sbb1->offset != 0);
2021  FAIL_IF(sbb1->len != 8);
2022  StreamingBufferBlock *sbb2 = SBB_RB_NEXT(sbb1);
2023  FAIL_IF_NULL(sbb2);
2024  FAIL_IF(sbb2 == sb->head);
2025  FAIL_IF(sbb2->offset != 14);
2026  FAIL_IF(sbb2->len != 8);
2027  Dump(sb);
2028  DumpSegment(sb, &seg1);
2029  DumpSegment(sb, &seg2);
2030  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2031 
2032  StreamingBufferSegment seg3;
2033  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"QWERTY", 6, 8) != 0);
2034  FAIL_IF(sb->region.stream_offset != 0);
2035  FAIL_IF(sb->region.buf_offset != 22);
2036  FAIL_IF(seg3.stream_offset != 8);
2040  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2041  FAIL_IF_NULL(sbb1);
2042  FAIL_IF(sbb1 != sb->head);
2043  FAIL_IF(sbb1->offset != 0);
2044  FAIL_IF(sbb1->len != 22);
2045  FAIL_IF(SBB_RB_NEXT(sbb1));
2046  Dump(sb);
2047  DumpSegment(sb, &seg1);
2048  DumpSegment(sb, &seg2);
2049  DumpSegment(sb, &seg3);
2050  FAIL_IF_NULL(sb->head);
2051  FAIL_IF_NOT(sb->sbb_size == 22);
2052  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2053 
2054  /* far ahead of curve: */
2055  StreamingBufferSegment seg4;
2056  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg4, (const uint8_t *)"XYZ", 3, 124) != 0);
2057  FAIL_IF(sb->region.stream_offset != 0);
2058  FAIL_IF(sb->region.buf_offset != 22);
2059  FAIL_IF(sb->region.buf_size != 128);
2060  FAIL_IF(seg4.stream_offset != 124);
2065  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2066  FAIL_IF_NULL(sbb1);
2067  FAIL_IF(sbb1 != sb->head);
2068  FAIL_IF(sbb1->offset != 0);
2069  FAIL_IF(sbb1->len != 22);
2070  FAIL_IF(!SBB_RB_NEXT(sbb1));
2071  Dump(sb);
2072  DumpSegment(sb, &seg1);
2073  DumpSegment(sb, &seg2);
2074  DumpSegment(sb, &seg3);
2075  DumpSegment(sb, &seg4);
2076  FAIL_IF_NULL(sb->head);
2077  FAIL_IF_NOT(sb->sbb_size == 25);
2078  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2079 
2080  FAIL_IF(!StreamingBufferSegmentCompareRawData(sb,&seg1,(const uint8_t *)"ABCDEFGH", 8));
2081  FAIL_IF(!StreamingBufferSegmentCompareRawData(sb,&seg2,(const uint8_t *)"01234567", 8));
2082  FAIL_IF(!StreamingBufferSegmentCompareRawData(sb,&seg3,(const uint8_t *)"QWERTY", 6));
2083  FAIL_IF(!StreamingBufferSegmentCompareRawData(sb,&seg4,(const uint8_t *)"XYZ", 3));
2084 
2085  StreamingBufferFree(sb, &cfg);
2086  PASS;
2087 }
2088 
2089 /** \test lots of gaps in block list */
2090 static int StreamingBufferTest06(void)
2091 {
2092  StreamingBufferConfig cfg = { 16, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2094  FAIL_IF(sb == NULL);
2095 
2096  StreamingBufferSegment seg1;
2097  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg1, (const uint8_t *)"A", 1) != 0);
2098  StreamingBufferSegment seg2;
2099  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"C", 1, 2) != 0);
2100  Dump(sb);
2101  FAIL_IF_NULL(sb->head);
2102  FAIL_IF_NOT(sb->sbb_size == 2);
2103  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2104 
2105  StreamingBufferSegment seg3;
2106  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"F", 1, 5) != 0);
2107  Dump(sb);
2108  FAIL_IF_NULL(sb->head);
2109  FAIL_IF_NOT(sb->sbb_size == 3);
2110  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2111 
2112  StreamingBufferSegment seg4;
2113  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg4, (const uint8_t *)"H", 1, 7) != 0);
2114  Dump(sb);
2115  FAIL_IF_NULL(sb->head);
2116  FAIL_IF_NOT(sb->sbb_size == 4);
2117  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2118 
2119  StreamingBufferSegment seg5;
2120  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg5, (const uint8_t *)"ABCDEFGHIJ", 10, 0) != 0);
2121  Dump(sb);
2122  StreamingBufferBlock *sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2123  FAIL_IF_NULL(sbb1);
2124  FAIL_IF(sbb1->offset != 0);
2125  FAIL_IF(sbb1->len != 10);
2126  FAIL_IF(SBB_RB_NEXT(sbb1));
2127  FAIL_IF_NULL(sb->head);
2128  FAIL_IF_NOT(sb->sbb_size == 10);
2129  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2130 
2131  StreamingBufferSegment seg6;
2132  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg6, (const uint8_t *)"abcdefghij", 10, 0) != 0);
2133  Dump(sb);
2134  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2135  FAIL_IF_NULL(sbb1);
2136  FAIL_IF(sbb1->offset != 0);
2137  FAIL_IF(sbb1->len != 10);
2138  FAIL_IF(SBB_RB_NEXT(sbb1));
2139  FAIL_IF_NULL(sb->head);
2140  FAIL_IF_NOT(sb->sbb_size == 10);
2141  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2142 
2143  StreamingBufferFree(sb, &cfg);
2144  PASS;
2145 }
2146 
2147 /** \test lots of gaps in block list */
2148 static int StreamingBufferTest07(void)
2149 {
2150  StreamingBufferConfig cfg = { 16, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2152  FAIL_IF(sb == NULL);
2153 
2154  StreamingBufferSegment seg1;
2155  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg1, (const uint8_t *)"B", 1, 1) != 0);
2156  StreamingBufferSegment seg2;
2157  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"D", 1, 3) != 0);
2158  Dump(sb);
2159  FAIL_IF_NULL(sb->head);
2160  FAIL_IF_NOT(sb->sbb_size == 2);
2161  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2162 
2163  StreamingBufferSegment seg3;
2164  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"F", 1, 5) != 0);
2165  Dump(sb);
2166  FAIL_IF_NULL(sb->head);
2167  FAIL_IF_NOT(sb->sbb_size == 3);
2168  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2169 
2170  StreamingBufferSegment seg4;
2171  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg4, (const uint8_t *)"H", 1, 7) != 0);
2172  Dump(sb);
2173  FAIL_IF_NULL(sb->head);
2174  FAIL_IF_NOT(sb->sbb_size == 4);
2175  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2176 
2177  StreamingBufferSegment seg5;
2178  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg5, (const uint8_t *)"ABCDEFGHIJ", 10, 0) != 0);
2179  Dump(sb);
2180  StreamingBufferBlock *sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2181  FAIL_IF_NULL(sbb1);
2182  FAIL_IF(sbb1->offset != 0);
2183  FAIL_IF(sbb1->len != 10);
2184  FAIL_IF(SBB_RB_NEXT(sbb1));
2185  FAIL_IF_NULL(sb->head);
2186  FAIL_IF_NOT(sb->sbb_size == 10);
2187  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2188 
2189  StreamingBufferSegment seg6;
2190  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg6, (const uint8_t *)"abcdefghij", 10, 0) != 0);
2191  Dump(sb);
2192  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2193  FAIL_IF_NULL(sbb1);
2194  FAIL_IF(sbb1->offset != 0);
2195  FAIL_IF(sbb1->len != 10);
2196  FAIL_IF(SBB_RB_NEXT(sbb1));
2197  FAIL_IF_NULL(sb->head);
2198  FAIL_IF_NOT(sb->sbb_size == 10);
2199  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2200 
2201  StreamingBufferFree(sb, &cfg);
2202  PASS;
2203 }
2204 
2205 /** \test lots of gaps in block list */
2206 static int StreamingBufferTest08(void)
2207 {
2208  StreamingBufferConfig cfg = { 16, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2210  FAIL_IF(sb == NULL);
2211 
2212  StreamingBufferSegment seg1;
2213  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg1, (const uint8_t *)"B", 1, 1) != 0);
2214  StreamingBufferSegment seg2;
2215  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"D", 1, 3) != 0);
2216  Dump(sb);
2217  FAIL_IF_NULL(sb->head);
2218  FAIL_IF_NOT(sb->sbb_size == 2);
2219  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2220 
2221  StreamingBufferSegment seg3;
2222  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"F", 1, 5) != 0);
2223  Dump(sb);
2224  FAIL_IF_NULL(sb->head);
2225  FAIL_IF_NOT(sb->sbb_size == 3);
2226  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2227 
2228  StreamingBufferSegment seg4;
2229  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg4, (const uint8_t *)"H", 1, 7) != 0);
2230  Dump(sb);
2231  FAIL_IF_NULL(sb->head);
2232  FAIL_IF_NOT(sb->sbb_size == 4);
2233  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2234 
2235  StreamingBufferSegment seg5;
2236  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg5, (const uint8_t *)"ABCDEFGHIJ", 10, 0) != 0);
2237  Dump(sb);
2238  StreamingBufferBlock *sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2239  FAIL_IF_NULL(sbb1);
2240  FAIL_IF(sbb1->offset != 0);
2241  FAIL_IF(sbb1->len != 10);
2242  FAIL_IF(SBB_RB_NEXT(sbb1));
2243  FAIL_IF_NULL(sb->head);
2244  FAIL_IF_NOT(sb->sbb_size == 10);
2245  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2246 
2247  StreamingBufferSegment seg6;
2248  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg6, (const uint8_t *)"abcdefghij", 10) != 0);
2249  Dump(sb);
2250  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2251  FAIL_IF_NULL(sbb1);
2252  FAIL_IF(sbb1->offset != 0);
2253  FAIL_IF(sbb1->len != 20);
2254  FAIL_IF(SBB_RB_NEXT(sbb1));
2255  FAIL_IF_NULL(sb->head);
2256  FAIL_IF_NOT(sb->sbb_size == 20);
2257  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2258 
2259  StreamingBufferFree(sb, &cfg);
2260  PASS;
2261 }
2262 
2263 /** \test lots of gaps in block list */
2264 static int StreamingBufferTest09(void)
2265 {
2266  StreamingBufferConfig cfg = { 16, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2268  FAIL_IF(sb == NULL);
2269 
2270  StreamingBufferSegment seg1;
2271  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg1, (const uint8_t *)"B", 1, 1) != 0);
2272  StreamingBufferSegment seg2;
2273  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"D", 1, 3) != 0);
2274  Dump(sb);
2275  FAIL_IF_NULL(sb->head);
2276  FAIL_IF_NOT(sb->sbb_size == 2);
2277  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2278 
2279  StreamingBufferSegment seg3;
2280  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"H", 1, 7) != 0);
2281  Dump(sb);
2282  FAIL_IF_NULL(sb->head);
2283  FAIL_IF_NOT(sb->sbb_size == 3);
2284  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2285 
2286  StreamingBufferSegment seg4;
2287  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg4, (const uint8_t *)"F", 1, 5) != 0);
2288  Dump(sb);
2289  FAIL_IF_NULL(sb->head);
2290  FAIL_IF_NOT(sb->sbb_size == 4);
2291  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2292 
2293  StreamingBufferSegment seg5;
2294  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg5, (const uint8_t *)"ABCDEFGHIJ", 10, 0) != 0);
2295  Dump(sb);
2296  StreamingBufferBlock *sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2297  FAIL_IF_NULL(sbb1);
2298  FAIL_IF(sbb1->offset != 0);
2299  FAIL_IF(sbb1->len != 10);
2300  FAIL_IF(SBB_RB_NEXT(sbb1));
2301  FAIL_IF_NULL(sb->head);
2302  FAIL_IF_NOT(sb->sbb_size == 10);
2303  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2304 
2305  StreamingBufferSegment seg6;
2306  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg6, (const uint8_t *)"abcdefghij", 10, 0) != 0);
2307  Dump(sb);
2308  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2309  FAIL_IF_NULL(sbb1);
2310  FAIL_IF(sbb1->offset != 0);
2311  FAIL_IF(sbb1->len != 10);
2312  FAIL_IF(SBB_RB_NEXT(sbb1));
2313  FAIL_IF_NULL(sb->head);
2314  FAIL_IF_NOT(sb->sbb_size == 10);
2315  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2316 
2317  StreamingBufferFree(sb, &cfg);
2318  PASS;
2319 }
2320 
2321 /** \test lots of gaps in block list */
2322 static int StreamingBufferTest10(void)
2323 {
2324  StreamingBufferConfig cfg = { 16, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2326  FAIL_IF(sb == NULL);
2327 
2328  StreamingBufferSegment seg1;
2329  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg1, (const uint8_t *)"A", 1, 0) != 0);
2330  Dump(sb);
2331  StreamingBufferSegment seg2;
2332  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg2, (const uint8_t *)"D", 1, 3) != 0);
2333  Dump(sb);
2334  StreamingBufferSegment seg3;
2335  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg3, (const uint8_t *)"H", 1, 7) != 0);
2336  Dump(sb);
2337  FAIL_IF_NULL(sb->head);
2338  FAIL_IF_NOT(sb->sbb_size == 3);
2339 
2340  StreamingBufferSegment seg4;
2341  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg4, (const uint8_t *)"B", 1, 1) != 0);
2342  Dump(sb);
2343  StreamingBufferSegment seg5;
2344  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg5, (const uint8_t *)"C", 1, 2) != 0);
2345  Dump(sb);
2346  StreamingBufferSegment seg6;
2347  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg6, (const uint8_t *)"G", 1, 6) != 0);
2348  Dump(sb);
2349  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2350  FAIL_IF_NULL(sb->head);
2351  FAIL_IF_NOT(sb->sbb_size == 6);
2352 
2353  StreamingBufferSegment seg7;
2354  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg7, (const uint8_t *)"ABCDEFGHIJ", 10, 0) != 0);
2355  Dump(sb);
2356  StreamingBufferBlock *sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2357  FAIL_IF_NULL(sbb1);
2358  FAIL_IF(sbb1->offset != 0);
2359  FAIL_IF(sbb1->len != 10);
2360  FAIL_IF(SBB_RB_NEXT(sbb1));
2361  FAIL_IF_NULL(sb->head);
2362  FAIL_IF_NOT(sb->sbb_size == 10);
2363 
2364  StreamingBufferSegment seg8;
2365  FAIL_IF(StreamingBufferInsertAt(sb, &cfg, &seg8, (const uint8_t *)"abcdefghij", 10, 0) != 0);
2366  Dump(sb);
2367  sbb1 = RB_MIN(SBB, &sb->sbb_tree);
2368  FAIL_IF_NOT(sb->head == RB_MIN(SBB, &sb->sbb_tree));
2369  FAIL_IF_NULL(sbb1);
2370  FAIL_IF(sbb1->offset != 0);
2371  FAIL_IF(sbb1->len != 10);
2372  FAIL_IF(SBB_RB_NEXT(sbb1));
2373  FAIL_IF_NULL(sb->head);
2374  FAIL_IF_NOT(sb->sbb_size == 10);
2375 
2376  StreamingBufferFree(sb, &cfg);
2377  PASS;
2378 }
2379 
2380 static int StreamingBufferTest11(void)
2381 {
2382  StreamingBufferConfig cfg = { 24, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, NULL, NULL, NULL };
2384  FAIL_IF(sb == NULL);
2385 
2386  StreamingBufferSegment seg1;
2387  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg1, (const uint8_t *)"ABCDEFGH", 8) != 0);
2388  StreamingBufferSegment seg2;
2389  unsigned int data_len = 0xffffffff;
2390  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg2, (const uint8_t *)"unused", data_len) != -1);
2392  sb, &cfg, &seg2, (const uint8_t *)"abcdefghij", data_len, 100000) != SC_ELIMIT);
2393  StreamingBufferFree(sb, &cfg);
2394  PASS;
2395 }
2396 
2397 static const char *dummy_conf_string = "%YAML 1.1\n"
2398  "---\n"
2399  "\n"
2400  "app-layer:\n"
2401  " protocols:\n"
2402  " http:\n"
2403  " enabled: yes\n"
2404  " memcap: 88\n"
2405  "\n";
2406 
2407 static int StreamingBufferTest12(void)
2408 {
2410  SCConfInit();
2412  SCConfYamlLoadString((const char *)dummy_conf_string, strlen(dummy_conf_string));
2413  HTPConfigure();
2414 
2416  HTPFree };
2418  FAIL_IF(sb == NULL);
2419 
2420  StreamingBufferSegment seg1;
2421  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg1, (const uint8_t *)"ABCDEFGHIJKLMNOP", 16) != 0);
2422 
2423  StreamingBufferSegment seg2;
2424  FAIL_IF(StreamingBufferAppend(sb, &cfg, &seg2,
2425  (const uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ",
2426  52) != -1);
2428 
2429  StreamingBufferFree(sb, &cfg);
2430  HTPFreeConfig();
2431  SCConfDeInit();
2434 
2435  PASS;
2436 }
2437 #endif
2438 
2440 {
2441 #ifdef UNITTESTS
2442  UtRegisterTest("StreamingBufferTest02", StreamingBufferTest02);
2443  UtRegisterTest("StreamingBufferTest03", StreamingBufferTest03);
2444  UtRegisterTest("StreamingBufferTest04", StreamingBufferTest04);
2445  UtRegisterTest("StreamingBufferTest06", StreamingBufferTest06);
2446  UtRegisterTest("StreamingBufferTest07", StreamingBufferTest07);
2447  UtRegisterTest("StreamingBufferTest08", StreamingBufferTest08);
2448  UtRegisterTest("StreamingBufferTest09", StreamingBufferTest09);
2449  UtRegisterTest("StreamingBufferTest10", StreamingBufferTest10);
2450  UtRegisterTest("StreamingBufferTest11 Bug 6903", StreamingBufferTest11);
2451  UtRegisterTest("StreamingBufferTest12 Bug 6782", StreamingBufferTest12);
2452 #endif
2453 }
SCConfYamlLoadString
int SCConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:535
FREE
#define FREE(cfg, ptr, s)
Definition: util-streaming-buffer.c:66
StreamingBufferSlideToOffset
void StreamingBufferSlideToOffset(StreamingBuffer *sb, const StreamingBufferConfig *cfg, uint64_t offset)
slide to absolute offset
Definition: util-streaming-buffer.c:1019
len
uint8_t len
Definition: app-layer-dnp3.h:2
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
StreamingBufferConfig_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:66
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
StreamingBuffer_::head
StreamingBufferBlock * head
Definition: util-streaming-buffer.h:111
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
StreamingBufferRegion_::next
struct StreamingBufferRegion_ * next
Definition: util-streaming-buffer.h:89
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
StreamingBufferFree
void StreamingBufferFree(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:291
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
RB_LEFT
#define RB_LEFT(elm, field)
Definition: tree.h:322
SC_EINVAL
@ SC_EINVAL
Definition: util-error.h:30
StreamingBuffer_::max_regions
uint16_t max_regions
Definition: util-streaming-buffer.h:114
StreamingBuffer_::regions
uint16_t regions
Definition: util-streaming-buffer.h:113
StreamingBufferGetData
int StreamingBufferGetData(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t *stream_offset)
Definition: util-streaming-buffer.c:1810
RB_MIN
#define RB_MIN(name, x)
Definition: tree.h:778
MIN
#define MIN(x, y)
Definition: suricata-common.h:408
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
Definition: util-streaming-buffer.c:1097
HTPRealloc
void * HTPRealloc(void *ptr, size_t orig_size, size_t size)
Definition: app-layer-htp-mem.c:174
segment_len
uint32_t segment_len
Definition: util-streaming-buffer.h:0
m
SCMutex m
Definition: flow-hash.h:6
StreamingBufferGetDataAtOffset
int StreamingBufferGetDataAtOffset(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t offset)
Definition: util-streaming-buffer.c:1827
MAX
#define MAX(x, y)
Definition: suricata-common.h:412
StreamingBufferSegmentCompareRawData
int StreamingBufferSegmentCompareRawData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:1794
stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:1
StreamingBufferConfig_::max_regions
uint16_t max_regions
Definition: util-streaming-buffer.h:67
SC_ELIMIT
@ SC_ELIMIT
Definition: util-error.h:31
util-unittest.h
HTPConfigure
void HTPConfigure(void)
Definition: app-layer-htp.c:2355
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
SC_ENOMEM
@ SC_ENOMEM
Definition: util-error.h:29
SCConfInit
void SCConfInit(void)
Initialize the configuration system.
Definition: conf.c:121
app-layer-htp.h
RB_EMPTY
#define RB_EMPTY(head)
Definition: tree.h:327
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
StreamingBufferRegion_::buf_size
uint32_t buf_size
Definition: util-streaming-buffer.h:86
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
RB_ROOT
#define RB_ROOT(head)
Definition: tree.h:326
StreamingBufferInit
StreamingBuffer * StreamingBufferInit(const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:245
HtpConfigCreateBackup
void HtpConfigCreateBackup(void)
Definition: app-layer-htp.c:2691
StreamingBufferSegmentIsBeforeWindow
int StreamingBufferSegmentIsBeforeWindow(const StreamingBuffer *sb, const StreamingBufferSegment *seg)
Definition: util-streaming-buffer.c:1649
BIT_U32
#define BIT_U32(n)
Definition: suricata-common.h:417
REALLOC
#define REALLOC(cfg, ptr, orig_s, s)
Definition: util-streaming-buffer.c:64
CALLOC
#define CALLOC(cfg, n, s)
Definition: util-streaming-buffer.c:63
BOOL2STR
#define BOOL2STR(b)
Definition: util-debug.h:542
util-print.h
RB_FOREACH_SAFE
#define RB_FOREACH_SAFE(x, name, head, y)
Definition: tree.h:791
StreamingBufferClear
void StreamingBufferClear(StreamingBuffer *sb, const StreamingBufferConfig *cfg)
Definition: util-streaming-buffer.c:268
HTPCalloc
void * HTPCalloc(size_t n, size_t size)
Definition: app-layer-htp-mem.c:153
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
PrintRawDataFp
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:112
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
RB_FOREACH
#define RB_FOREACH(x, name, head)
Definition: tree.h:781
StreamingBuffer_::sbb_size
uint32_t sbb_size
Definition: util-streaming-buffer.h:112
STREAMING_BUFFER_REGION_GAP_DEFAULT
#define STREAMING_BUFFER_REGION_GAP_DEFAULT
Definition: util-streaming-buffer.h:63
conf-yaml-loader.h
StreamingBufferRegion_::buf
uint8_t * buf
Definition: util-streaming-buffer.h:85
StreamingBufferCompareRawData
int StreamingBufferCompareRawData(const StreamingBuffer *sb, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:1850
SC_OK
@ SC_OK
Definition: util-error.h:27
RB_GENERATE
RB_GENERATE(SBB, StreamingBufferBlock, rb, SBBCompare)
StreamingBufferRegion_::buf_offset
uint32_t buf_offset
Definition: util-streaming-buffer.h:87
SCConfCreateContextBackup
void SCConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
Definition: conf.c:715
SBB_RB_FIND_INCLUSIVE
StreamingBufferBlock * SBB_RB_FIND_INCLUSIVE(struct SBB *head, StreamingBufferBlock *elm)
Definition: util-streaming-buffer.c:104
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
StreamingBuffer_::sbb_tree
struct SBB sbb_tree
Definition: util-streaming-buffer.h:110
StreamingBuffer_
Definition: util-streaming-buffer.h:108
StreamingBufferSBBGetDataAtOffset
void StreamingBufferSBBGetDataAtOffset(const StreamingBuffer *sb, const StreamingBufferBlock *sbb, const uint8_t **data, uint32_t *data_len, uint64_t offset)
get the data for one SBB
Definition: util-streaming-buffer.c:1717
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
StreamingBufferAppendNoTrack
int StreamingBufferAppendNoTrack(StreamingBuffer *sb, const StreamingBufferConfig *cfg, const uint8_t *data, uint32_t data_len)
add data w/o tracking a segment
Definition: util-streaming-buffer.c:1141
suricata-common.h
util-streaming-buffer.h
SCConfDeInit
void SCConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:734
util-validate.h
HtpConfigRestoreBackup
void HtpConfigRestoreBackup(void)
Definition: app-layer-htp.c:2696
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
HtpBodyChunk_::next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:124
RB_RIGHT
#define RB_RIGHT(elm, field)
Definition: tree.h:323
StreamingBufferInsertAt
int StreamingBufferInsertAt(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len, uint64_t offset)
Definition: util-streaming-buffer.c:1529
head
Flow * head
Definition: flow-hash.h:1
SCConfRestoreContextBackup
void SCConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:725
sc_errno
thread_local SCError sc_errno
Definition: util-error.c:31
WARN_UNUSED
#define WARN_UNUSED
Definition: bindgen.h:33
StreamingBufferRegion_
Definition: util-streaming-buffer.h:84
app-layer-htp-mem.h
RB_FOREACH_REVERSE_FROM
#define RB_FOREACH_REVERSE_FROM(x, name, y)
Definition: tree.h:801
SBBCompare
int SBBCompare(struct StreamingBufferBlock *a, struct StreamingBufferBlock *b)
Definition: util-streaming-buffer.c:73
HTPFree
void HTPFree(void *ptr, size_t size)
Definition: app-layer-htp-mem.c:198
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
RB_FOREACH_FROM
#define RB_FOREACH_FROM(x, name, y)
Definition: tree.h:786
HTPFreeConfig
void HTPFreeConfig(void)
Clears the HTTP server configuration memory used by HTP library.
Definition: app-layer-htp.c:1595
StreamingBuffer_::region
StreamingBufferRegion region
Definition: util-streaming-buffer.h:109
StreamingBufferSegmentGetData
void StreamingBufferSegmentGetData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t **data, uint32_t *data_len)
Definition: util-streaming-buffer.c:1760
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
StreamingBufferConfig_::region_gap
uint32_t region_gap
Definition: util-streaming-buffer.h:68
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
StreamingBufferBlock::len
uint32_t len
Definition: util-streaming-buffer.h:98
StreamingBufferBlock::offset
uint64_t offset
Definition: util-streaming-buffer.h:96
StreamingBufferSBBGetData
void StreamingBufferSBBGetData(const StreamingBuffer *sb, const StreamingBufferBlock *sbb, const uint8_t **data, uint32_t *data_len)
get the data for one SBB
Definition: util-streaming-buffer.c:1681
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
StreamingBufferBlock
block of continues data
Definition: util-streaming-buffer.h:95
StreamingBufferRegion_::stream_offset
uint64_t stream_offset
Definition: util-streaming-buffer.h:88
StreamingBufferRegisterTests
void StreamingBufferRegisterTests(void)
Definition: util-streaming-buffer.c:2439