1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
|
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "syshead.h"
#include "common.h"
#include "buffer.h"
#include "error.h"
#include "mtu.h"
#include "thread.h"
#include "memdbg.h"
size_t
array_mult_safe (const size_t m1, const size_t m2, const size_t extra)
{
const size_t limit = 0xFFFFFFFF;
unsigned long long res = (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit) || unlikely(res > (unsigned long long)limit))
msg (M_FATAL, "attemped allocation of excessively large array");
return (size_t) res;
}
void
buf_size_error (const size_t size)
{
msg (M_FATAL, "fatal buffer size error, size=%lu", (unsigned long)size);
}
struct buffer
#ifdef DMALLOC
alloc_buf_debug (size_t size, const char *file, int line)
#else
alloc_buf (size_t size)
#endif
{
#ifdef DMALLOC
return alloc_buf_gc_debug (size, NULL, file, line);
#else
return alloc_buf_gc (size, NULL);
#endif
}
struct buffer
#ifdef DMALLOC
alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line)
#else
alloc_buf_gc (size_t size, struct gc_arena *gc)
#endif
{
struct buffer buf;
if (!buf_size_valid (size))
buf_size_error (size);
buf.capacity = (int)size;
buf.offset = 0;
buf.len = 0;
#ifdef DMALLOC
buf.data = (uint8_t *) gc_malloc_debug (size, false, gc, file, line);
#else
buf.data = (uint8_t *) gc_malloc (size, false, gc);
#endif
if (size)
*buf.data = 0;
return buf;
}
struct buffer
#ifdef DMALLOC
clone_buf_debug (const struct buffer* buf, const char *file, int line)
#else
clone_buf (const struct buffer* buf)
#endif
{
struct buffer ret;
ret.capacity = buf->capacity;
ret.offset = buf->offset;
ret.len = buf->len;
#ifdef DMALLOC
ret.data = (uint8_t *) openvpn_dmalloc (file, line, buf->capacity);
#else
ret.data = (uint8_t *) malloc (buf->capacity);
#endif
check_malloc_return (ret.data);
memcpy (BPTR (&ret), BPTR (buf), BLEN (buf));
return ret;
}
#ifdef BUF_INIT_TRACKING
bool
buf_init_debug (struct buffer *buf, int offset, const char *file, int line)
{
buf->debug_file = file;
buf->debug_line = line;
return buf_init_dowork (buf, offset);
}
static inline int
buf_debug_line (const struct buffer *buf)
{
return buf->debug_line;
}
static const char *
buf_debug_file (const struct buffer *buf)
{
return buf->debug_file;
}
#else
#define buf_debug_line(buf) 0
#define buf_debug_file(buf) "[UNDEF]"
#endif
void
buf_clear (struct buffer *buf)
{
if (buf->capacity > 0)
memset (buf->data, 0, buf->capacity);
buf->len = 0;
buf->offset = 0;
}
bool
buf_assign (struct buffer *dest, const struct buffer *src)
{
if (!buf_init (dest, src->offset))
return false;
return buf_write (dest, BPTR (src), BLEN (src));
}
struct buffer
clear_buf ()
{
struct buffer buf;
CLEAR (buf);
return buf;
}
void
free_buf (struct buffer *buf)
{
if (buf->data)
free (buf->data);
CLEAR (*buf);
}
/*
* Return a buffer for write that is a subset of another buffer
*/
struct buffer
buf_sub (struct buffer *buf, int size, bool prepend)
{
struct buffer ret;
uint8_t *data;
CLEAR (ret);
data = prepend ? buf_prepend (buf, size) : buf_write_alloc (buf, size);
if (data)
{
ret.capacity = size;
ret.data = data;
}
return ret;
}
/*
* printf append to a buffer with overflow check
*/
bool
buf_printf (struct buffer *buf, const char *format, ...)
{
int ret = false;
if (buf_defined (buf))
{
va_list arglist;
uint8_t *ptr = BEND (buf);
int cap = buf_forward_capacity (buf);
if (cap > 0)
{
int stat;
va_start (arglist, format);
stat = vsnprintf ((char *)ptr, cap, format, arglist);
va_end (arglist);
*(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
buf->len += (int) strlen ((char *)ptr);
if (stat >= 0 && stat < cap)
ret = true;
}
}
return ret;
}
bool
buf_puts(struct buffer *buf, const char *str)
{
int ret = false;
uint8_t *ptr = BEND (buf);
int cap = buf_forward_capacity (buf);
if (cap > 0)
{
strncpynt ((char *)ptr,str, cap);
*(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
buf->len += (int) strlen ((char *)ptr);
ret = true;
}
return ret;
}
/*
* This is necessary due to certain buggy implementations of snprintf,
* that don't guarantee null termination for size > 0.
*/
int openvpn_snprintf(char *str, size_t size, const char *format, ...)
{
va_list arglist;
int ret = 0;
if (size > 0)
{
va_start (arglist, format);
ret = vsnprintf (str, size, format, arglist);
va_end (arglist);
str[size - 1] = 0;
}
return ret;
}
/*
* write a string to the end of a buffer that was
* truncated by buf_printf
*/
void
buf_catrunc (struct buffer *buf, const char *str)
{
if (buf_forward_capacity (buf) <= 1)
{
int len = (int) strlen (str) + 1;
if (len < buf_forward_capacity_total (buf))
{
strncpynt ((char *)(buf->data + buf->capacity - len), str, len);
}
}
}
/*
* convert a multi-line output to one line
*/
void
convert_to_one_line (struct buffer *buf)
{
uint8_t *cp = BPTR(buf);
int len = BLEN(buf);
while (len--)
{
if (*cp == '\n')
*cp = '|';
++cp;
}
}
/* NOTE: requires that string be null terminated */
void
buf_write_string_file (const struct buffer *buf, const char *filename, int fd)
{
const int len = strlen ((char *) BPTR (buf));
const int size = write (fd, BPTR (buf), len);
if (size != len)
msg (M_ERR, "Write error on file '%s'", filename);
}
/*
* Garbage collection
*/
void *
#ifdef DMALLOC
gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line)
#else
gc_malloc (size_t size, bool clear, struct gc_arena *a)
#endif
{
void *ret;
if (a)
{
struct gc_entry *e;
#ifdef DMALLOC
e = (struct gc_entry *) openvpn_dmalloc (file, line, size + sizeof (struct gc_entry));
#else
e = (struct gc_entry *) malloc (size + sizeof (struct gc_entry));
#endif
check_malloc_return (e);
ret = (char *) e + sizeof (struct gc_entry);
/*mutex_lock_static (L_GC_MALLOC);*/
e->next = a->list;
a->list = e;
/*mutex_unlock_static (L_GC_MALLOC);*/
}
else
{
#ifdef DMALLOC
ret = openvpn_dmalloc (file, line, size);
#else
ret = malloc (size);
#endif
check_malloc_return (ret);
}
#ifndef ZERO_BUFFER_ON_ALLOC
if (clear)
#endif
memset (ret, 0, size);
return ret;
}
void
x_gc_free (struct gc_arena *a)
{
struct gc_entry *e;
/*mutex_lock_static (L_GC_MALLOC);*/
e = a->list;
a->list = NULL;
/*mutex_unlock_static (L_GC_MALLOC);*/
while (e != NULL)
{
struct gc_entry *next = e->next;
free (e);
e = next;
}
}
/*
* Transfer src arena to dest, resetting src to an empty arena.
*/
void
gc_transfer (struct gc_arena *dest, struct gc_arena *src)
{
if (dest && src)
{
struct gc_entry *e = src->list;
if (e)
{
while (e->next != NULL)
e = e->next;
e->next = dest->list;
dest->list = src->list;
src->list = NULL;
}
}
}
/*
* Hex dump -- Output a binary buffer to a hex string and return it.
*/
char *
format_hex_ex (const uint8_t *data, int size, int maxoutput,
int space_break, const char* separator,
struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (maxoutput ? maxoutput :
((size * 2) + (size / space_break) * (int) strlen (separator) + 2),
gc);
int i;
for (i = 0; i < size; ++i)
{
if (separator && i && !(i % space_break))
buf_printf (&out, "%s", separator);
buf_printf (&out, "%02x", data[i]);
}
buf_catrunc (&out, "[more...]");
return (char *)out.data;
}
/*
* remove specific trailing character
*/
void
buf_rmtail (struct buffer *buf, uint8_t remove)
{
uint8_t *cp = BLAST(buf);
if (cp && *cp == remove)
{
*cp = '\0';
--buf->len;
}
}
/*
* force a null termination even it requires
* truncation of the last char.
*/
void
buf_null_terminate (struct buffer *buf)
{
char *last = (char *) BLAST (buf);
if (last && *last == '\0') /* already terminated? */
return;
if (!buf_safe (buf, 1)) /* make space for trailing null */
buf_inc_len (buf, -1);
buf_write_u8 (buf, 0);
}
/*
* Remove trailing \r and \n chars and ensure
* null termination.
*/
void
buf_chomp (struct buffer *buf)
{
while (true)
{
char *last = (char *) BLAST (buf);
if (!last)
break;
if (char_class (*last, CC_CRLF|CC_NULL))
{
if (!buf_inc_len (buf, -1))
break;
}
else
break;
}
buf_null_terminate (buf);
}
const char *
skip_leading_whitespace (const char *str)
{
while (*str)
{
const char c = *str;
if (!(c == ' ' || c == '\t'))
break;
++str;
}
return str;
}
/*
* like buf_null_terminate, but operate on strings
*/
void
string_null_terminate (char *str, int len, int capacity)
{
ASSERT (len >= 0 && len <= capacity && capacity > 0);
if (len < capacity)
*(str + len) = '\0';
else if (len == capacity)
*(str + len - 1) = '\0';
}
/*
* Remove trailing \r and \n chars.
*/
void
chomp (char *str)
{
rm_trailing_chars (str, "\r\n");
}
/*
* Remove trailing chars
*/
void
rm_trailing_chars (char *str, const char *what_to_delete)
{
bool modified;
do {
const int len = strlen (str);
modified = false;
if (len > 0)
{
char *cp = str + (len - 1);
if (strchr (what_to_delete, *cp) != NULL)
{
*cp = '\0';
modified = true;
}
}
} while (modified);
}
/*
* Allocate a string
*/
char *
#ifdef DMALLOC
string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line)
#else
string_alloc (const char *str, struct gc_arena *gc)
#endif
{
if (str)
{
const int n = strlen (str) + 1;
char *ret;
#ifdef DMALLOC
ret = (char *) gc_malloc_debug (n, false, gc, file, line);
#else
ret = (char *) gc_malloc (n, false, gc);
#endif
memcpy (ret, str, n);
return ret;
}
else
return NULL;
}
/*
* Erase all characters in a string
*/
void
string_clear (char *str)
{
if (str)
{
const int len = strlen (str);
if (len > 0)
memset (str, 0, len);
}
}
/*
* Return the length of a string array
*/
int
string_array_len (const char **array)
{
int i = 0;
if (array)
{
while (array[i])
++i;
}
return i;
}
char *
print_argv (const char **p, struct gc_arena *gc, const unsigned int flags)
{
struct buffer out = alloc_buf_gc (256, gc);
int i = 0;
for (;;)
{
const char *cp = *p++;
if (!cp)
break;
if (i)
buf_printf (&out, " ");
if (flags & PA_BRACKET)
buf_printf (&out, "[%s]", cp);
else
buf_printf (&out, "%s", cp);
++i;
}
return BSTR (&out);
}
/*
* Allocate a string inside a buffer
*/
struct buffer
#ifdef DMALLOC
string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line)
#else
string_alloc_buf (const char *str, struct gc_arena *gc)
#endif
{
struct buffer buf;
ASSERT (str);
#ifdef DMALLOC
buf_set_read (&buf, (uint8_t*) string_alloc_debug (str, gc, file, line), strlen (str) + 1);
#else
buf_set_read (&buf, (uint8_t*) string_alloc (str, gc), strlen (str) + 1);
#endif
if (buf.len > 0) /* Don't count trailing '\0' as part of length */
--buf.len;
return buf;
}
/*
* String comparison
*/
bool
buf_string_match_head_str (const struct buffer *src, const char *match)
{
const int size = strlen (match);
if (size < 0 || size > src->len)
return false;
return memcmp (BPTR (src), match, size) == 0;
}
bool
buf_string_compare_advance (struct buffer *src, const char *match)
{
if (buf_string_match_head_str (src, match))
{
buf_advance (src, strlen (match));
return true;
}
else
return false;
}
int
buf_substring_len (const struct buffer *buf, int delim)
{
int i = 0;
struct buffer tmp = *buf;
int c;
while ((c = buf_read_u8 (&tmp)) >= 0)
{
++i;
if (c == delim)
return i;
}
return -1;
}
/*
* String parsing
*/
bool
buf_parse (struct buffer *buf, const int delim, char *line, const int size)
{
bool eol = false;
int n = 0;
int c;
ASSERT (size > 0);
do
{
c = buf_read_u8 (buf);
if (c < 0)
eol = true;
if (c <= 0 || c == delim)
c = 0;
if (n >= size)
break;
line[n++] = c;
}
while (c);
line[size-1] = '\0';
return !(eol && !strlen (line));
}
/*
* Print a string which might be NULL
*/
const char *
np (const char *str)
{
if (str)
return str;
else
return "[NULL]";
}
/*
* Classify and mutate strings based on character types.
*/
bool
char_class (const unsigned char c, const unsigned int flags)
{
if (!flags)
return false;
if (flags & CC_ANY)
return true;
if ((flags & CC_NULL) && c == '\0')
return true;
if ((flags & CC_ALNUM) && isalnum (c))
return true;
if ((flags & CC_ALPHA) && isalpha (c))
return true;
if ((flags & CC_ASCII) && isascii (c))
return true;
if ((flags & CC_CNTRL) && iscntrl (c))
return true;
if ((flags & CC_DIGIT) && isdigit (c))
return true;
if ((flags & CC_PRINT) && isprint (c))
return true;
if ((flags & CC_PUNCT) && ispunct (c))
return true;
if ((flags & CC_SPACE) && isspace (c))
return true;
if ((flags & CC_XDIGIT) && isxdigit (c))
return true;
if ((flags & CC_BLANK) && (c == ' ' || c == '\t'))
return true;
if ((flags & CC_NEWLINE) && c == '\n')
return true;
if ((flags & CC_CR) && c == '\r')
return true;
if ((flags & CC_BACKSLASH) && c == '\\')
return true;
if ((flags & CC_UNDERBAR) && c == '_')
return true;
if ((flags & CC_DASH) && c == '-')
return true;
if ((flags & CC_DOT) && c == '.')
return true;
if ((flags & CC_COMMA) && c == ',')
return true;
if ((flags & CC_COLON) && c == ':')
return true;
if ((flags & CC_SLASH) && c == '/')
return true;
if ((flags & CC_SINGLE_QUOTE) && c == '\'')
return true;
if ((flags & CC_DOUBLE_QUOTE) && c == '\"')
return true;
if ((flags & CC_REVERSE_QUOTE) && c == '`')
return true;
if ((flags & CC_AT) && c == '@')
return true;
if ((flags & CC_EQUAL) && c == '=')
return true;
return false;
}
static inline bool
char_inc_exc (const char c, const unsigned int inclusive, const unsigned int exclusive)
{
return char_class (c, inclusive) && !char_class (c, exclusive);
}
bool
string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive)
{
char c;
ASSERT (str);
while ((c = *str++))
{
if (!char_inc_exc (c, inclusive, exclusive))
return false;
}
return true;
}
/*
* Modify string in place.
* Guaranteed to not increase string length.
*/
bool
string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
{
const char *in = str;
bool ret = true;
ASSERT (str);
while (true)
{
char c = *in++;
if (c)
{
if (!char_inc_exc (c, inclusive, exclusive))
{
c = replace;
ret = false;
}
if (c)
*str++ = c;
}
else
{
*str = '\0';
break;
}
}
return ret;
}
const char *
string_mod_const (const char *str,
const unsigned int inclusive,
const unsigned int exclusive,
const char replace,
struct gc_arena *gc)
{
if (str)
{
char *buf = string_alloc (str, gc);
string_mod (buf, inclusive, exclusive, replace);
return buf;
}
else
return NULL;
}
void
string_replace_leading (char *str, const char match, const char replace)
{
ASSERT (match != '\0');
while (*str)
{
if (*str == match)
*str = replace;
else
break;
++str;
}
}
#ifdef CHARACTER_CLASS_DEBUG
#define CC_INCLUDE (CC_PRINT)
#define CC_EXCLUDE (0)
#define CC_REPLACE ('.')
void
character_class_debug (void)
{
char buf[256];
while (fgets (buf, sizeof (buf), stdin) != NULL)
{
string_mod (buf, CC_INCLUDE, CC_EXCLUDE, CC_REPLACE);
printf ("%s", buf);
}
}
#endif
#ifdef VERIFY_ALIGNMENT
void
valign4 (const struct buffer *buf, const char *file, const int line)
{
if (buf && buf->len)
{
int msglevel = D_ALIGN_DEBUG;
const unsigned int u = (unsigned int) BPTR (buf);
if (u & (PAYLOAD_ALIGN-1))
msglevel = D_ALIGN_ERRORS;
msg (msglevel, "%sAlignment at %s/%d ptr=" ptr_format " OLC=%d/%d/%d I=%s/%d",
(msglevel == D_ALIGN_ERRORS) ? "ERROR: " : "",
file,
line,
(ptr_type)buf->data,
buf->offset,
buf->len,
buf->capacity,
buf_debug_file (buf),
buf_debug_line (buf));
}
}
#endif
/*
* struct buffer_list
*/
#ifdef ENABLE_BUFFER_LIST
struct buffer_list *
buffer_list_new (const int max_size)
{
struct buffer_list *ret;
ALLOC_OBJ_CLEAR (ret, struct buffer_list);
ret->max_size = max_size;
ret->size = 0;
return ret;
}
void
buffer_list_free (struct buffer_list *ol)
{
if (ol)
{
buffer_list_reset (ol);
free (ol);
}
}
bool
buffer_list_defined (const struct buffer_list *ol)
{
return ol->head != NULL;
}
void
buffer_list_reset (struct buffer_list *ol)
{
struct buffer_entry *e = ol->head;
while (e)
{
struct buffer_entry *next = e->next;
free_buf (&e->buf);
free (e);
e = next;
}
ol->head = ol->tail = NULL;
ol->size = 0;
}
void
buffer_list_push (struct buffer_list *ol, const unsigned char *str)
{
if (str)
{
const size_t len = strlen ((const char *)str);
struct buffer_entry *e = buffer_list_push_data (ol, str, len+1);
if (e)
e->buf.len = len; /* Don't count trailing '\0' as part of length */
}
}
struct buffer_entry *
buffer_list_push_data (struct buffer_list *ol, const uint8_t *data, size_t size)
{
struct buffer_entry *e = NULL;
if (data && (!ol->max_size || ol->size < ol->max_size))
{
ALLOC_OBJ_CLEAR (e, struct buffer_entry);
++ol->size;
if (ol->tail)
{
ASSERT (ol->head);
ol->tail->next = e;
}
else
{
ASSERT (!ol->head);
ol->head = e;
}
e->buf = alloc_buf (size);
memcpy (e->buf.data, data, size);
e->buf.len = (int)size;
ol->tail = e;
}
return e;
}
struct buffer *
buffer_list_peek (struct buffer_list *ol)
{
if (ol && ol->head)
return &ol->head->buf;
else
return NULL;
}
void
buffer_list_aggregate (struct buffer_list *bl, const size_t max)
{
if (bl->head)
{
struct buffer_entry *more = bl->head;
size_t size = 0;
int count = 0;
for (count = 0; more && size <= max; ++count)
{
size += BLEN(&more->buf);
more = more->next;
}
if (count >= 2)
{
int i;
struct buffer_entry *e = bl->head, *f;
ALLOC_OBJ_CLEAR (f, struct buffer_entry);
f->buf.data = malloc (size);
check_malloc_return (f->buf.data);
f->buf.capacity = size;
for (i = 0; e && i < count; ++i)
{
struct buffer_entry *next = e->next;
buf_copy (&f->buf, &e->buf);
free_buf (&e->buf);
free (e);
e = next;
}
bl->head = f;
f->next = more;
if (!more)
bl->tail = f;
}
}
}
void
buffer_list_pop (struct buffer_list *ol)
{
if (ol && ol->head)
{
struct buffer_entry *e = ol->head->next;
free_buf (&ol->head->buf);
free (ol->head);
ol->head = e;
--ol->size;
if (!e)
ol->tail = NULL;
}
}
void
buffer_list_advance (struct buffer_list *ol, int n)
{
if (ol->head)
{
struct buffer *buf = &ol->head->buf;
ASSERT (buf_advance (buf, n));
if (!BLEN (buf))
buffer_list_pop (ol);
}
}
struct buffer_list *
buffer_list_file (const char *fn, int max_line_len)
{
FILE *fp = fopen (fn, "r");
struct buffer_list *bl = NULL;
if (fp)
{
char *line = (char *) malloc (max_line_len);
if (line)
{
bl = buffer_list_new (0);
while (fgets (line, max_line_len, fp) != NULL)
buffer_list_push (bl, (unsigned char *)line);
free (line);
}
fclose (fp);
}
return bl;
}
#endif
|