You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

printbuf.c 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. *
  11. * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
  12. * The copyrights to the contents of this file are licensed under the MIT License
  13. * (https://www.opensource.org/licenses/mit-license.php)
  14. */
  15. #include "config.h"
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef HAVE_STDARG_H
  22. #include <stdarg.h>
  23. #else /* !HAVE_STDARG_H */
  24. #error Not enough var arg support!
  25. #endif /* HAVE_STDARG_H */
  26. #include "debug.h"
  27. #include "printbuf.h"
  28. #include "snprintf_compat.h"
  29. #include "vasprintf_compat.h"
  30. static int printbuf_extend(struct printbuf *p, int min_size);
  31. struct printbuf *printbuf_new(void)
  32. {
  33. struct printbuf *p;
  34. p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
  35. if (!p)
  36. return NULL;
  37. p->size = 32;
  38. p->bpos = 0;
  39. if (!(p->buf = (char *)malloc(p->size)))
  40. {
  41. free(p);
  42. return NULL;
  43. }
  44. p->buf[0] = '\0';
  45. return p;
  46. }
  47. /**
  48. * Extend the buffer p so it has a size of at least min_size.
  49. *
  50. * If the current size is large enough, nothing is changed.
  51. *
  52. * If extension failed, errno is set to indicate the error.
  53. *
  54. * Note: this does not check the available space! The caller
  55. * is responsible for performing those calculations.
  56. */
  57. static int printbuf_extend(struct printbuf *p, int min_size)
  58. {
  59. char *t;
  60. int new_size;
  61. if (p->size >= min_size)
  62. return 0;
  63. /* Prevent signed integer overflows with large buffers. */
  64. if (min_size > INT_MAX - 8)
  65. {
  66. errno = EFBIG;
  67. return -1;
  68. }
  69. if (p->size > INT_MAX / 2)
  70. new_size = min_size + 8;
  71. else {
  72. new_size = p->size * 2;
  73. if (new_size < min_size + 8)
  74. new_size = min_size + 8;
  75. }
  76. #ifdef PRINTBUF_DEBUG
  77. MC_DEBUG("printbuf_extend: realloc "
  78. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  79. p->bpos, min_size, p->size, new_size);
  80. #endif /* PRINTBUF_DEBUG */
  81. if (!(t = (char *)realloc(p->buf, new_size)))
  82. return -1;
  83. p->size = new_size;
  84. p->buf = t;
  85. return 0;
  86. }
  87. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  88. {
  89. /* Prevent signed integer overflows with large buffers. */
  90. if (size < 0 || size > INT_MAX - p->bpos - 1)
  91. {
  92. errno = EFBIG;
  93. return -1;
  94. }
  95. if (p->size <= p->bpos + size + 1)
  96. {
  97. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  98. return -1;
  99. }
  100. memcpy(p->buf + p->bpos, buf, size);
  101. p->bpos += size;
  102. p->buf[p->bpos] = '\0';
  103. return size;
  104. }
  105. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  106. {
  107. int size_needed;
  108. if (offset == -1)
  109. offset = pb->bpos;
  110. /* Prevent signed integer overflows with large buffers. */
  111. if (len < 0 || offset < -1 || len > INT_MAX - offset)
  112. {
  113. errno = EFBIG;
  114. return -1;
  115. }
  116. size_needed = offset + len;
  117. if (pb->size < size_needed)
  118. {
  119. if (printbuf_extend(pb, size_needed) < 0)
  120. return -1;
  121. }
  122. if (pb->bpos < offset)
  123. memset(pb->buf + pb->bpos, '\0', offset - pb->bpos);
  124. memset(pb->buf + offset, charvalue, len);
  125. if (pb->bpos < size_needed)
  126. pb->bpos = size_needed;
  127. return 0;
  128. }
  129. int sprintbuf(struct printbuf *p, const char *msg, ...)
  130. {
  131. va_list ap;
  132. char *t;
  133. int size;
  134. char buf[128];
  135. /* use stack buffer first */
  136. va_start(ap, msg);
  137. size = vsnprintf(buf, 128, msg, ap);
  138. va_end(ap);
  139. /* if string is greater than stack buffer, then use dynamic string
  140. * with vasprintf. Note: some implementations of vsnprintf return -1
  141. * if output is truncated whereas some return the number of bytes that
  142. * would have been written - this code handles both cases.
  143. */
  144. if (size < 0 || size > 127)
  145. {
  146. va_start(ap, msg);
  147. if ((size = vasprintf(&t, msg, ap)) < 0)
  148. {
  149. va_end(ap);
  150. return -1;
  151. }
  152. va_end(ap);
  153. size = printbuf_memappend(p, t, size);
  154. free(t);
  155. }
  156. else
  157. {
  158. size = printbuf_memappend(p, buf, size);
  159. }
  160. return size;
  161. }
  162. void printbuf_reset(struct printbuf *p)
  163. {
  164. p->buf[0] = '\0';
  165. p->bpos = 0;
  166. }
  167. void printbuf_free(struct printbuf *p)
  168. {
  169. if (p)
  170. {
  171. free(p->buf);
  172. free(p);
  173. }
  174. }