tor  0.4.2.0-alpha-dev
buffers.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
25 #define BUFFERS_PRIVATE
26 #include "orconfig.h"
27 #include <stddef.h>
28 #include "lib/buf/buffers.h"
29 #include "lib/cc/torint.h"
30 #include "lib/log/log.h"
31 #include "lib/log/util_bug.h"
32 #include "lib/ctime/di_ops.h"
33 #include "lib/malloc/malloc.h"
34 #include "lib/string/printf.h"
35 #include "lib/time/compat_time.h"
36 
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 
41 #include <stdlib.h>
42 #include <string.h>
43 
44 //#define PARANOIA
45 
46 #ifdef PARANOIA
47 
49 #define check() STMT_BEGIN buf_assert_ok(buf); STMT_END
50 #else
51 #define check() STMT_NIL
52 #endif /* defined(PARANOIA) */
53 
54 /* Implementation notes:
55  *
56  * After flirting with memmove, and dallying with ring-buffers, we're finally
57  * getting up to speed with the 1970s and implementing buffers as a linked
58  * list of small chunks. Each buffer has such a list; data is removed from
59  * the head of the list, and added at the tail. The list is singly linked,
60  * and the buffer keeps a pointer to the head and the tail.
61  *
62  * Every chunk, except the tail, contains at least one byte of data. Data in
63  * each chunk is contiguous.
64  *
65  * When you need to treat the first N characters on a buffer as a contiguous
66  * string, use the buf_pullup function to make them so. Don't do this more
67  * than necessary.
68  *
69  * The major free Unix kernels have handled buffers like this since, like,
70  * forever.
71  */
72 
73 /* Chunk manipulation functions */
74 
75 #define CHUNK_HEADER_LEN offsetof(chunk_t, mem[0])
76 
77 /* We leave this many NUL bytes at the end of the buffer. */
78 #ifdef DISABLE_MEMORY_SENTINELS
79 #define SENTINEL_LEN 0
80 #else
81 #define SENTINEL_LEN 4
82 #endif
83 
84 /* Header size plus NUL bytes at the end */
85 #define CHUNK_OVERHEAD (CHUNK_HEADER_LEN + SENTINEL_LEN)
86 
89 #define CHUNK_ALLOC_SIZE(memlen) (CHUNK_OVERHEAD + (memlen))
90 
92 #define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_OVERHEAD)
93 
94 #define DEBUG_SENTINEL
95 
96 #if defined(DEBUG_SENTINEL) && !defined(DISABLE_MEMORY_SENTINELS)
97 #define DBG_S(s) s
98 #else
99 #define DBG_S(s) (void)0
100 #endif
101 
102 #ifdef DISABLE_MEMORY_SENTINELS
103 #define CHUNK_SET_SENTINEL(chunk, alloclen) STMT_NIL
104 #else
105 #define CHUNK_SET_SENTINEL(chunk, alloclen) do { \
106  uint8_t *a = (uint8_t*) &(chunk)->mem[(chunk)->memlen]; \
107  DBG_S(uint8_t *b = &((uint8_t*)(chunk))[(alloclen)-SENTINEL_LEN]); \
108  DBG_S(tor_assert(a == b)); \
109  memset(a,0,SENTINEL_LEN); \
110  } while (0)
111 #endif /* defined(DISABLE_MEMORY_SENTINELS) */
112 
115 static inline void
116 chunk_repack(chunk_t *chunk)
117 {
118  if (chunk->datalen && chunk->data != &chunk->mem[0]) {
119  memmove(chunk->mem, chunk->data, chunk->datalen);
120  }
121  chunk->data = &chunk->mem[0];
122 }
123 
126 static void
127 buf_chunk_free_unchecked(chunk_t *chunk)
128 {
129  if (!chunk)
130  return;
131 #ifdef DEBUG_CHUNK_ALLOC
132  tor_assert(CHUNK_ALLOC_SIZE(chunk->memlen) == chunk->DBG_alloc);
133 #endif
135  CHUNK_ALLOC_SIZE(chunk->memlen));
137  tor_free(chunk);
138 }
139 static inline chunk_t *
140 chunk_new_with_alloc_size(size_t alloc)
141 {
142  chunk_t *ch;
143  ch = tor_malloc(alloc);
144  ch->next = NULL;
145  ch->datalen = 0;
146 #ifdef DEBUG_CHUNK_ALLOC
147  ch->DBG_alloc = alloc;
148 #endif
149  ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
151  ch->data = &ch->mem[0];
152  CHUNK_SET_SENTINEL(ch, alloc);
153  return ch;
154 }
155 
158 static inline chunk_t *
159 chunk_grow(chunk_t *chunk, size_t sz)
160 {
161  off_t offset;
162  const size_t memlen_orig = chunk->memlen;
163  const size_t orig_alloc = CHUNK_ALLOC_SIZE(memlen_orig);
164  const size_t new_alloc = CHUNK_ALLOC_SIZE(sz);
165  tor_assert(sz > chunk->memlen);
166  offset = chunk->data - chunk->mem;
167  chunk = tor_realloc(chunk, new_alloc);
168  chunk->memlen = sz;
169  chunk->data = chunk->mem + offset;
170 #ifdef DEBUG_CHUNK_ALLOC
171  tor_assert(chunk->DBG_alloc == orig_alloc);
172  chunk->DBG_alloc = new_alloc;
173 #endif
174  total_bytes_allocated_in_chunks += new_alloc - orig_alloc;
175  CHUNK_SET_SENTINEL(chunk, new_alloc);
176  return chunk;
177 }
178 
180 #define MIN_CHUNK_ALLOC 256
181 
182 #define MAX_CHUNK_ALLOC 65536
183 
186 size_t
188 {
189  tor_assert(target <= SIZE_T_CEILING - CHUNK_OVERHEAD);
190  if (CHUNK_ALLOC_SIZE(target) >= MAX_CHUNK_ALLOC)
191  return CHUNK_ALLOC_SIZE(target);
192  size_t sz = MIN_CHUNK_ALLOC;
193  while (CHUNK_SIZE_WITH_ALLOC(sz) < target) {
194  sz <<= 1;
195  }
196  return sz;
197 }
198 
208 void
209 buf_pullup(buf_t *buf, size_t bytes, const char **head_out, size_t *len_out)
210 {
211  chunk_t *dest, *src;
212  size_t capacity;
213  if (!buf->head) {
214  *head_out = NULL;
215  *len_out = 0;
216  return;
217  }
218 
219  check();
220  if (buf->datalen < bytes)
221  bytes = buf->datalen;
222 
223  capacity = bytes;
224  if (buf->head->datalen >= bytes) {
225  *head_out = buf->head->data;
226  *len_out = buf->head->datalen;
227  return;
228  }
229 
230  if (buf->head->memlen >= capacity) {
231  /* We don't need to grow the first chunk, but we might need to repack it.*/
232  size_t needed = capacity - buf->head->datalen;
233  if (CHUNK_REMAINING_CAPACITY(buf->head) < needed)
234  chunk_repack(buf->head);
235  tor_assert(CHUNK_REMAINING_CAPACITY(buf->head) >= needed);
236  } else {
237  chunk_t *newhead;
238  size_t newsize;
239  /* We need to grow the chunk. */
240  chunk_repack(buf->head);
241  newsize = CHUNK_SIZE_WITH_ALLOC(buf_preferred_chunk_size(capacity));
242  newhead = chunk_grow(buf->head, newsize);
243  tor_assert(newhead->memlen >= capacity);
244  if (newhead != buf->head) {
245  if (buf->tail == buf->head)
246  buf->tail = newhead;
247  buf->head = newhead;
248  }
249  }
250 
251  dest = buf->head;
252  while (dest->datalen < bytes) {
253  size_t n = bytes - dest->datalen;
254  src = dest->next;
255  tor_assert(src);
256  if (n >= src->datalen) {
257  memcpy(CHUNK_WRITE_PTR(dest), src->data, src->datalen);
258  dest->datalen += src->datalen;
259  dest->next = src->next;
260  if (buf->tail == src)
261  buf->tail = dest;
262  buf_chunk_free_unchecked(src);
263  } else {
264  memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
265  dest->datalen += n;
266  src->data += n;
267  src->datalen -= n;
268  tor_assert(dest->datalen == bytes);
269  }
270  }
271 
272  check();
273  *head_out = buf->head->data;
274  *len_out = buf->head->datalen;
275 }
276 
277 #ifdef TOR_UNIT_TESTS
278 /* Write sz bytes from cp into a newly allocated buffer buf.
279  * Returns NULL when passed a NULL cp or zero sz.
280  * Asserts on failure: only for use in unit tests.
281  * buf must be freed using buf_free(). */
282 buf_t *
283 buf_new_with_data(const char *cp, size_t sz)
284 {
285  /* Validate arguments */
286  if (!cp || sz <= 0 || sz >= INT_MAX) {
287  return NULL;
288  }
289 
291 
292  /* Allocate a buffer */
293  buf_t *buf = buf_new_with_capacity(sz);
294  tor_assert(buf);
295  buf_assert_ok(buf);
296  tor_assert(!buf->head);
297 
298  /* Allocate a chunk that is sz bytes long */
299  buf->head = chunk_new_with_alloc_size(CHUNK_ALLOC_SIZE(sz));
300  buf->tail = buf->head;
301  tor_assert(buf->head);
302  buf_assert_ok(buf);
303  tor_assert(buf_allocation(buf) >= sz);
304 
305  /* Copy the data and size the buffers */
306  tor_assert(sz <= buf_slack(buf));
307  tor_assert(sz <= CHUNK_REMAINING_CAPACITY(buf->head));
308  memcpy(&buf->head->mem[0], cp, sz);
309  buf->datalen = sz;
310  buf->head->datalen = sz;
311  buf->head->data = &buf->head->mem[0];
312  buf_assert_ok(buf);
313 
314  /* Make sure everything is large enough */
315  tor_assert(buf_allocation(buf) >= sz);
316  tor_assert(buf_allocation(buf) >= buf_datalen(buf) + buf_slack(buf));
317  /* Does the buffer implementation allocate more than the requested size?
318  * (for example, by rounding up). If so, these checks will fail. */
319  tor_assert(buf_datalen(buf) == sz);
320  tor_assert(buf_slack(buf) == 0);
321 
322  return buf;
323 }
324 #endif /* defined(TOR_UNIT_TESTS) */
325 
327 void
328 buf_drain(buf_t *buf, size_t n)
329 {
330  tor_assert(buf->datalen >= n);
331  while (n) {
332  tor_assert(buf->head);
333  if (buf->head->datalen > n) {
334  buf->head->datalen -= n;
335  buf->head->data += n;
336  buf->datalen -= n;
337  return;
338  } else {
339  chunk_t *victim = buf->head;
340  n -= victim->datalen;
341  buf->datalen -= victim->datalen;
342  buf->head = victim->next;
343  if (buf->tail == victim)
344  buf->tail = NULL;
345  buf_chunk_free_unchecked(victim);
346  }
347  }
348  check();
349 }
350 
353 buf_t *
355 {
356  buf_t *b = buf_new();
357  b->default_chunk_size = buf_preferred_chunk_size(size);
358  return b;
359 }
360 
362 buf_t *
363 buf_new(void)
364 {
365  buf_t *buf = tor_malloc_zero(sizeof(buf_t));
366  buf->magic = BUFFER_MAGIC;
367  buf->default_chunk_size = 4096;
368  return buf;
369 }
370 
371 size_t
372 buf_get_default_chunk_size(const buf_t *buf)
373 {
374  return buf->default_chunk_size;
375 }
376 
378 void
379 buf_clear(buf_t *buf)
380 {
381  chunk_t *chunk, *next;
382  buf->datalen = 0;
383  for (chunk = buf->head; chunk; chunk = next) {
384  next = chunk->next;
385  buf_chunk_free_unchecked(chunk);
386  }
387  buf->head = buf->tail = NULL;
388 }
389 
391 MOCK_IMPL(size_t,
392 buf_datalen, (const buf_t *buf))
393 {
394  return buf->datalen;
395 }
396 
398 size_t
399 buf_allocation(const buf_t *buf)
400 {
401  size_t total = 0;
402  const chunk_t *chunk;
403  for (chunk = buf->head; chunk; chunk = chunk->next) {
404  total += CHUNK_ALLOC_SIZE(chunk->memlen);
405  }
406  return total;
407 }
408 
411 size_t
412 buf_slack(const buf_t *buf)
413 {
414  if (!buf->tail)
415  return 0;
416  else
417  return CHUNK_REMAINING_CAPACITY(buf->tail);
418 }
419 
421 void
422 buf_free_(buf_t *buf)
423 {
424  if (!buf)
425  return;
426 
427  buf_clear(buf);
428  buf->magic = 0xdeadbeef;
429  tor_free(buf);
430 }
431 
433 static chunk_t *
434 chunk_copy(const chunk_t *in_chunk)
435 {
436  chunk_t *newch = tor_memdup(in_chunk, CHUNK_ALLOC_SIZE(in_chunk->memlen));
438 #ifdef DEBUG_CHUNK_ALLOC
439  newch->DBG_alloc = CHUNK_ALLOC_SIZE(in_chunk->memlen);
440 #endif
441  newch->next = NULL;
442  if (in_chunk->data) {
443  off_t offset = in_chunk->data - in_chunk->mem;
444  newch->data = newch->mem + offset;
445  }
446  return newch;
447 }
448 
450 buf_t *
451 buf_copy(const buf_t *buf)
452 {
453  chunk_t *ch;
454  buf_t *out = buf_new();
455  out->default_chunk_size = buf->default_chunk_size;
456  for (ch = buf->head; ch; ch = ch->next) {
457  chunk_t *newch = chunk_copy(ch);
458  if (out->tail) {
459  out->tail->next = newch;
460  out->tail = newch;
461  } else {
462  out->head = out->tail = newch;
463  }
464  }
465  out->datalen = buf->datalen;
466  return out;
467 }
468 
472 chunk_t *
473 buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
474 {
475  chunk_t *chunk;
476 
477  if (CHUNK_ALLOC_SIZE(capacity) < buf->default_chunk_size) {
478  chunk = chunk_new_with_alloc_size(buf->default_chunk_size);
479  } else if (capped && CHUNK_ALLOC_SIZE(capacity) > MAX_CHUNK_ALLOC) {
480  chunk = chunk_new_with_alloc_size(MAX_CHUNK_ALLOC);
481  } else {
482  chunk = chunk_new_with_alloc_size(buf_preferred_chunk_size(capacity));
483  }
484 
485  chunk->inserted_time = monotime_coarse_get_stamp();
486 
487  if (buf->tail) {
488  tor_assert(buf->head);
489  buf->tail->next = chunk;
490  buf->tail = chunk;
491  } else {
492  tor_assert(!buf->head);
493  buf->head = buf->tail = chunk;
494  }
495  check();
496  return chunk;
497 }
498 
503 uint32_t
504 buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now)
505 {
506  if (buf->head) {
507  return now - buf->head->inserted_time;
508  } else {
509  return 0;
510  }
511 }
512 
513 size_t
514 buf_get_total_allocation(void)
515 {
517 }
518 
524 int
525 buf_add(buf_t *buf, const char *string, size_t string_len)
526 {
527  if (!string_len)
528  return (int)buf->datalen;
529  check();
530 
531  if (BUG(buf->datalen >= INT_MAX))
532  return -1;
533  if (BUG(buf->datalen >= INT_MAX - string_len))
534  return -1;
535 
536  while (string_len) {
537  size_t copy;
538  if (!buf->tail || !CHUNK_REMAINING_CAPACITY(buf->tail))
539  buf_add_chunk_with_capacity(buf, string_len, 1);
540 
541  copy = CHUNK_REMAINING_CAPACITY(buf->tail);
542  if (copy > string_len)
543  copy = string_len;
544  memcpy(CHUNK_WRITE_PTR(buf->tail), string, copy);
545  string_len -= copy;
546  string += copy;
547  buf->datalen += copy;
548  buf->tail->datalen += copy;
549  }
550 
551  check();
552  tor_assert(buf->datalen < INT_MAX);
553  return (int)buf->datalen;
554 }
555 
558 void
559 buf_add_string(buf_t *buf, const char *string)
560 {
561  buf_add(buf, string, strlen(string));
562 }
563 
565 void
566 buf_add_printf(buf_t *buf, const char *format, ...)
567 {
568  va_list ap;
569  va_start(ap,format);
570  buf_add_vprintf(buf, format, ap);
571  va_end(ap);
572 }
573 
575 void
576 buf_add_vprintf(buf_t *buf, const char *format, va_list args)
577 {
578  /* XXXX Faster implementations are easy enough, but let's optimize later */
579  char *tmp;
580  tor_vasprintf(&tmp, format, args);
581  buf_add(buf, tmp, strlen(tmp));
582  tor_free(tmp);
583 }
584 
588 char *
589 buf_extract(buf_t *buf, size_t *sz_out)
590 {
591  tor_assert(buf);
592 
593  size_t sz = buf_datalen(buf);
594  char *result;
595  result = tor_malloc(sz+1);
596  buf_peek(buf, result, sz);
597  result[sz] = 0;
598  if (sz_out)
599  *sz_out = sz;
600  return result;
601 }
602 
606 void
607 buf_peek(const buf_t *buf, char *string, size_t string_len)
608 {
609  chunk_t *chunk;
610 
611  tor_assert(string);
612  /* make sure we don't ask for too much */
613  tor_assert(string_len <= buf->datalen);
614  /* buf_assert_ok(buf); */
615 
616  chunk = buf->head;
617  while (string_len) {
618  size_t copy = string_len;
619  tor_assert(chunk);
620  if (chunk->datalen < copy)
621  copy = chunk->datalen;
622  memcpy(string, chunk->data, copy);
623  string_len -= copy;
624  string += copy;
625  chunk = chunk->next;
626  }
627 }
628 
633 int
634 buf_get_bytes(buf_t *buf, char *string, size_t string_len)
635 {
636  /* There must be string_len bytes in buf; write them onto string,
637  * then memmove buf back (that is, remove them from buf).
638  *
639  * Return the number of bytes still on the buffer. */
640 
641  check();
642  buf_peek(buf, string, string_len);
643  buf_drain(buf, string_len);
644  check();
645  tor_assert(buf->datalen < INT_MAX);
646  return (int)buf->datalen;
647 }
648 
653 int
654 buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
655 {
656  /* We can do way better here, but this doesn't turn up in any profiles. */
657  char b[4096];
658  size_t cp, len;
659 
660  if (BUG(buf_out->datalen >= INT_MAX || *buf_flushlen >= INT_MAX))
661  return -1;
662  if (BUG(buf_out->datalen >= INT_MAX - *buf_flushlen))
663  return -1;
664 
665  len = *buf_flushlen;
666  if (len > buf_in->datalen)
667  len = buf_in->datalen;
668 
669  cp = len; /* Remember the number of bytes we intend to copy. */
670  tor_assert(cp < INT_MAX);
671  while (len) {
672  /* This isn't the most efficient implementation one could imagine, since
673  * it does two copies instead of 1, but I kinda doubt that this will be
674  * critical path. */
675  size_t n = len > sizeof(b) ? sizeof(b) : len;
676  buf_get_bytes(buf_in, b, n);
677  buf_add(buf_out, b, n);
678  len -= n;
679  }
680  *buf_flushlen -= cp;
681  return (int)cp;
682 }
683 
686 void
687 buf_move_all(buf_t *buf_out, buf_t *buf_in)
688 {
689  tor_assert(buf_out);
690  if (!buf_in)
691  return;
692  if (BUG(buf_out->datalen >= INT_MAX || buf_in->datalen >= INT_MAX))
693  return;
694  if (BUG(buf_out->datalen >= INT_MAX - buf_in->datalen))
695  return;
696 
697  if (buf_out->head == NULL) {
698  buf_out->head = buf_in->head;
699  buf_out->tail = buf_in->tail;
700  } else {
701  buf_out->tail->next = buf_in->head;
702  buf_out->tail = buf_in->tail;
703  }
704 
705  buf_out->datalen += buf_in->datalen;
706  buf_in->head = buf_in->tail = NULL;
707  buf_in->datalen = 0;
708 }
709 
711 typedef struct buf_pos_t {
712  const chunk_t *chunk;
713  int pos;
714  size_t chunk_pos;
715 } buf_pos_t;
716 
718 static void
719 buf_pos_init(const buf_t *buf, buf_pos_t *out)
720 {
721  out->chunk = buf->head;
722  out->pos = 0;
723  out->chunk_pos = 0;
724 }
725 
729 static off_t
731 {
732  const chunk_t *chunk;
733  int pos;
734  tor_assert(out);
735  if (out->chunk) {
736  if (out->chunk->datalen) {
737  tor_assert(out->pos < (off_t)out->chunk->datalen);
738  } else {
739  tor_assert(out->pos == 0);
740  }
741  }
742  pos = out->pos;
743  for (chunk = out->chunk; chunk; chunk = chunk->next) {
744  char *cp = memchr(chunk->data+pos, ch, chunk->datalen - pos);
745  if (cp) {
746  out->chunk = chunk;
747  tor_assert(cp - chunk->data < INT_MAX);
748  out->pos = (int)(cp - chunk->data);
749  return out->chunk_pos + out->pos;
750  } else {
751  out->chunk_pos += chunk->datalen;
752  pos = 0;
753  }
754  }
755  return -1;
756 }
757 
760 static inline int
762 {
763  tor_assert(pos->pos < INT_MAX - 1);
764  ++pos->pos;
765  if (pos->pos == (off_t)pos->chunk->datalen) {
766  if (!pos->chunk->next)
767  return -1;
768  pos->chunk_pos += pos->chunk->datalen;
769  pos->chunk = pos->chunk->next;
770  pos->pos = 0;
771  }
772  return 0;
773 }
774 
777 static int
778 buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
779 {
780  buf_pos_t p;
781  if (!n)
782  return 1;
783 
784  memcpy(&p, pos, sizeof(p));
785 
786  while (1) {
787  char ch = p.chunk->data[p.pos];
788  if (ch != *s)
789  return 0;
790  ++s;
791  /* If we're out of characters that don't match, we match. Check this
792  * _before_ we test incrementing pos, in case we're at the end of the
793  * string. */
794  if (--n == 0)
795  return 1;
796  if (buf_pos_inc(&p)<0)
797  return 0;
798  }
799 }
800 
803 int
804 buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
805 {
806  buf_pos_t pos;
807  buf_pos_init(buf, &pos);
808  while (buf_find_pos_of_char(*s, &pos) >= 0) {
809  if (buf_matches_at_pos(&pos, s, n)) {
810  tor_assert(pos.chunk_pos + pos.pos < INT_MAX);
811  return (int)(pos.chunk_pos + pos.pos);
812  } else {
813  if (buf_pos_inc(&pos)<0)
814  return -1;
815  }
816  }
817  return -1;
818 }
819 
822 int
823 buf_peek_startswith(const buf_t *buf, const char *cmd)
824 {
825  char tmp[PEEK_BUF_STARTSWITH_MAX];
826  size_t clen = strlen(cmd);
827  if (clen == 0)
828  return 1;
829  if (BUG(clen > sizeof(tmp)))
830  return 0;
831  if (buf->datalen < clen)
832  return 0;
833  buf_peek(buf, tmp, clen);
834  return fast_memeq(tmp, cmd, clen);
835 }
836 
839 static off_t
840 buf_find_offset_of_char(buf_t *buf, char ch)
841 {
842  chunk_t *chunk;
843  off_t offset = 0;
844  tor_assert(buf->datalen < INT_MAX);
845  for (chunk = buf->head; chunk; chunk = chunk->next) {
846  char *cp = memchr(chunk->data, ch, chunk->datalen);
847  if (cp)
848  return offset + (cp - chunk->data);
849  else
850  offset += chunk->datalen;
851  }
852  return -1;
853 }
854 
862 int
863 buf_get_line(buf_t *buf, char *data_out, size_t *data_len)
864 {
865  size_t sz;
866  off_t offset;
867 
868  if (!buf->head)
869  return 0;
870 
871  offset = buf_find_offset_of_char(buf, '\n');
872  if (offset < 0)
873  return 0;
874  sz = (size_t) offset;
875  if (sz+2 > *data_len) {
876  *data_len = sz + 2;
877  return -1;
878  }
879  buf_get_bytes(buf, data_out, sz+1);
880  data_out[sz+1] = '\0';
881  *data_len = sz+1;
882  return 1;
883 }
884 
886 int
887 buf_set_to_copy(buf_t **output,
888  const buf_t *input)
889 {
890  if (*output)
891  buf_free(*output);
892  *output = buf_copy(input);
893  return 0;
894 }
895 
898 void
899 buf_assert_ok(buf_t *buf)
900 {
901  tor_assert(buf);
902  tor_assert(buf->magic == BUFFER_MAGIC);
903 
904  if (! buf->head) {
905  tor_assert(!buf->tail);
906  tor_assert(buf->datalen == 0);
907  } else {
908  chunk_t *ch;
909  size_t total = 0;
910  tor_assert(buf->tail);
911  for (ch = buf->head; ch; ch = ch->next) {
912  total += ch->datalen;
913  tor_assert(ch->datalen <= ch->memlen);
914  tor_assert(ch->datalen < INT_MAX);
915  tor_assert(ch->data >= &ch->mem[0]);
916  tor_assert(ch->data <= &ch->mem[0]+ch->memlen);
917  if (ch->data == &ch->mem[0]+ch->memlen) {
918  /* LCOV_EXCL_START */
919  static int warned = 0;
920  if (! warned) {
921  log_warn(LD_BUG, "Invariant violation in buf.c related to #15083");
922  warned = 1;
923  }
924  /* LCOV_EXCL_STOP */
925  }
926  tor_assert(ch->data+ch->datalen <= &ch->mem[0] + ch->memlen);
927  if (!ch->next)
928  tor_assert(ch == buf->tail);
929  }
930  tor_assert(buf->datalen == total);
931  }
932 }
MOCK_IMPL(size_t, buf_datalen,(const buf_t *buf))
Definition: buffers.c:391
static size_t total_bytes_allocated_in_chunks
Definition: buffers.c:125
void buf_pullup(buf_t *buf, size_t bytes, const char **head_out, size_t *len_out)
Definition: buffers.c:209
Header for printf.c.
Headers for di_ops.c.
void buf_peek(const buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:607
int buf_peek_startswith(const buf_t *buf, const char *cmd)
Definition: buffers.c:823
Functions and types for monotonic times.
chunk_t * buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
Definition: buffers.c:473
static void buf_pos_init(const buf_t *buf, buf_pos_t *out)
Definition: buffers.c:719
int tor_vasprintf(char **strp, const char *fmt, va_list args)
Definition: printf.c:96
#define SSIZE_T_CEILING
Definition: torint.h:124
#define tor_free(p)
Definition: malloc.h:52
Integer definitions used throughout Tor.
Headers for util_malloc.c.
struct buf_pos_t buf_pos_t
static int buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
Definition: buffers.c:778
buf_t * buf_new(void)
Definition: buffers.c:363
int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
Definition: buffers.c:654
int buf_get_line(buf_t *buf, char *data_out, size_t *data_len)
Definition: buffers.c:863
tor_assert(buffer)
int buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
Definition: buffers.c:804
#define CHUNK_ALLOC_SIZE(memlen)
Definition: buffers.c:89
buf_t * buf_copy(const buf_t *buf)
Definition: buffers.c:451
void buf_add_vprintf(buf_t *buf, const char *format, va_list args)
Definition: buffers.c:576
void buf_move_all(buf_t *buf_out, buf_t *buf_in)
Definition: buffers.c:687
#define SIZE_T_CEILING
Definition: torint.h:126
int buf_get_bytes(buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:634
static void chunk_repack(chunk_t *chunk)
Definition: buffers.c:116
void buf_add_string(buf_t *buf, const char *string)
Definition: buffers.c:559
int buf_add(buf_t *buf, const char *string, size_t string_len)
Definition: buffers.c:525
size_t chunk_pos
Definition: buffers.c:714
static int buf_pos_inc(buf_pos_t *pos)
Definition: buffers.c:761
#define MAX_CHUNK_ALLOC
Definition: buffers.c:182
void buf_drain(buf_t *buf, size_t n)
Definition: buffers.c:328
size_t buf_allocation(const buf_t *buf)
Definition: buffers.c:399
static off_t buf_find_pos_of_char(char ch, buf_pos_t *out)
Definition: buffers.c:730
void buf_add_printf(buf_t *buf, const char *format,...)
Definition: buffers.c:566
uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now)
Definition: buffers.c:504
void buf_free_(buf_t *buf)
Definition: buffers.c:422
void buf_assert_ok(buf_t *buf)
Definition: buffers.c:899
#define CHUNK_SIZE_WITH_ALLOC(memlen)
Definition: buffers.c:92
const chunk_t * chunk
Definition: buffers.c:712
int pos
Definition: buffers.c:713
Header file for buffers.c.
size_t buf_slack(const buf_t *buf)
Definition: buffers.c:412
static chunk_t * chunk_copy(const chunk_t *in_chunk)
Definition: buffers.c:434
buf_t * buf_new_with_capacity(size_t size)
Definition: buffers.c:354
Headers for log.c.
static off_t buf_find_offset_of_char(buf_t *buf, char ch)
Definition: buffers.c:840
static chunk_t * chunk_grow(chunk_t *chunk, size_t sz)
Definition: buffers.c:159
Macros to manage assertions, fatal and non-fatal.
void buf_clear(buf_t *buf)
Definition: buffers.c:379
int buf_set_to_copy(buf_t **output, const buf_t *input)
Definition: buffers.c:887
#define LD_BUG
Definition: log.h:83
uint32_t monotime_coarse_get_stamp(void)
Definition: compat_time.c:844
char * buf_extract(buf_t *buf, size_t *sz_out)
Definition: buffers.c:589
#define MIN_CHUNK_ALLOC
Definition: buffers.c:180
size_t buf_preferred_chunk_size(size_t target)
Definition: buffers.c:187