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 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. #include "config.h"
  16. #include <limits.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef HAVE_STDARG_H
  21. #include <stdarg.h>
  22. #else /* !HAVE_STDARG_H */
  23. #error Not enough var arg support!
  24. #endif /* HAVE_STDARG_H */
  25. #include "debug.h"
  26. #include "printbuf.h"
  27. #include "snprintf_compat.h"
  28. #include "vasprintf_compat.h"
  29. static int printbuf_extend(struct printbuf *p, int min_size);
  30. struct printbuf *printbuf_new(void)
  31. {
  32. struct printbuf *p;
  33. p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
  34. if (!p)
  35. return NULL;
  36. p->size = 32;
  37. p->bpos = 0;
  38. if (!(p->buf = (char *)malloc(p->size)))
  39. {
  40. free(p);
  41. return NULL;
  42. }
  43. p->buf[0] = '\0';
  44. return p;
  45. }
  46. /**
  47. * Extend the buffer p so it has a size of at least min_size.
  48. *
  49. * If the current size is large enough, nothing is changed.
  50. *
  51. * Note: this does not check the available space! The caller
  52. * is responsible for performing those calculations.
  53. */
  54. static int printbuf_extend(struct printbuf *p, int min_size)
  55. {
  56. char *t;
  57. int new_size;
  58. if (p->size >= min_size)
  59. return 0;
  60. /* Prevent signed integer overflows with large buffers. */
  61. if (min_size > INT_MAX - 8)
  62. return -1;
  63. if (p->size > INT_MAX / 2)
  64. new_size = min_size + 8;
  65. else {
  66. new_size = p->size * 2;
  67. if (new_size < min_size + 8)
  68. new_size = min_size + 8;
  69. }
  70. #ifdef PRINTBUF_DEBUG
  71. MC_DEBUG("printbuf_memappend: realloc "
  72. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  73. p->bpos, min_size, p->size, new_size);
  74. #endif /* PRINTBUF_DEBUG */
  75. if (!(t = (char *)realloc(p->buf, new_size)))
  76. return -1;
  77. p->size = new_size;
  78. p->buf = t;
  79. return 0;
  80. }
  81. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  82. {
  83. /* Prevent signed integer overflows with large buffers. */
  84. if (size > INT_MAX - p->bpos - 1)
  85. return -1;
  86. if (p->size <= p->bpos + size + 1)
  87. {
  88. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  89. return -1;
  90. }
  91. memcpy(p->buf + p->bpos, buf, size);
  92. p->bpos += size;
  93. p->buf[p->bpos] = '\0';
  94. return size;
  95. }
  96. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  97. {
  98. int size_needed;
  99. if (offset == -1)
  100. offset = pb->bpos;
  101. /* Prevent signed integer overflows with large buffers. */
  102. if (len > INT_MAX - offset)
  103. return -1;
  104. size_needed = offset + len;
  105. if (pb->size < size_needed)
  106. {
  107. if (printbuf_extend(pb, size_needed) < 0)
  108. return -1;
  109. }
  110. memset(pb->buf + offset, charvalue, len);
  111. if (pb->bpos < size_needed)
  112. pb->bpos = size_needed;
  113. return 0;
  114. }
  115. int sprintbuf(struct printbuf *p, const char *msg, ...)
  116. {
  117. va_list ap;
  118. char *t;
  119. int size;
  120. char buf[128];
  121. /* user stack buffer first */
  122. va_start(ap, msg);
  123. size = vsnprintf(buf, 128, msg, ap);
  124. va_end(ap);
  125. /* if string is greater than stack buffer, then use dynamic string
  126. * with vasprintf. Note: some implementation of vsnprintf return -1
  127. * if output is truncated whereas some return the number of bytes that
  128. * would have been written - this code handles both cases.
  129. */
  130. if (size == -1 || size > 127)
  131. {
  132. va_start(ap, msg);
  133. if ((size = vasprintf(&t, msg, ap)) < 0)
  134. {
  135. va_end(ap);
  136. return -1;
  137. }
  138. va_end(ap);
  139. printbuf_memappend(p, t, size);
  140. free(t);
  141. return size;
  142. }
  143. else
  144. {
  145. printbuf_memappend(p, buf, size);
  146. return size;
  147. }
  148. }
  149. void printbuf_reset(struct printbuf *p)
  150. {
  151. p->buf[0] = '\0';
  152. p->bpos = 0;
  153. }
  154. void printbuf_free(struct printbuf *p)
  155. {
  156. if (p)
  157. {
  158. free(p->buf);
  159. free(p);
  160. }
  161. }