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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "bits.h"
  26. #include "debug.h"
  27. #include "printbuf.h"
  28. static int printbuf_extend(struct printbuf *p, int min_size);
  29. struct printbuf* printbuf_new(void)
  30. {
  31. struct printbuf *p;
  32. p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
  33. if(!p) return NULL;
  34. p->size = 32;
  35. p->bpos = 0;
  36. if(!(p->buf = (char*)malloc(p->size))) {
  37. free(p);
  38. return NULL;
  39. }
  40. return p;
  41. }
  42. /**
  43. * Extend the buffer p so it has a size of at least min_size.
  44. *
  45. * If the current size is large enough, nothing is changed.
  46. *
  47. * Note: this does not check the available space! The caller
  48. * is responsible for performing those calculations.
  49. */
  50. static int printbuf_extend(struct printbuf *p, int min_size)
  51. {
  52. char *t;
  53. int new_size;
  54. if (p->size >= min_size)
  55. return 0;
  56. /* Prevent signed integer overflows with large buffers. */
  57. if (min_size > INT_MAX - 8)
  58. return -1;
  59. if (p->size > INT_MAX / 2)
  60. new_size = min_size + 8;
  61. else {
  62. new_size = p->size * 2;
  63. if (new_size < min_size + 8)
  64. new_size = min_size + 8;
  65. }
  66. #ifdef PRINTBUF_DEBUG
  67. MC_DEBUG("printbuf_memappend: realloc "
  68. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  69. p->bpos, min_size, p->size, new_size);
  70. #endif /* PRINTBUF_DEBUG */
  71. if(!(t = (char*)realloc(p->buf, new_size)))
  72. return -1;
  73. p->size = new_size;
  74. p->buf = t;
  75. return 0;
  76. }
  77. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  78. {
  79. /* Prevent signed integer overflows with large buffers. */
  80. if (size > INT_MAX - p->bpos - 1)
  81. return -1;
  82. if (p->size <= p->bpos + size + 1) {
  83. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  84. return -1;
  85. }
  86. memcpy(p->buf + p->bpos, buf, size);
  87. p->bpos += size;
  88. p->buf[p->bpos]= '\0';
  89. return size;
  90. }
  91. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  92. {
  93. int size_needed;
  94. if (offset == -1)
  95. offset = pb->bpos;
  96. /* Prevent signed integer overflows with large buffers. */
  97. if (len > INT_MAX - offset)
  98. return -1;
  99. size_needed = offset + len;
  100. if (pb->size < size_needed)
  101. {
  102. if (printbuf_extend(pb, size_needed) < 0)
  103. return -1;
  104. }
  105. memset(pb->buf + offset, charvalue, len);
  106. if (pb->bpos < size_needed)
  107. pb->bpos = size_needed;
  108. return 0;
  109. }
  110. #if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER)
  111. # define vsnprintf _vsnprintf
  112. #elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */
  113. # error Need vsnprintf!
  114. #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
  115. #if !defined(HAVE_VASPRINTF)
  116. /* CAW: compliant version of vasprintf */
  117. static int vasprintf(char **buf, const char *fmt, va_list ap)
  118. {
  119. #ifndef WIN32
  120. static char _T_emptybuffer = '\0';
  121. #endif /* !defined(WIN32) */
  122. int chars;
  123. char *b;
  124. if(!buf) { return -1; }
  125. #ifdef WIN32
  126. chars = _vscprintf(fmt, ap)+1;
  127. #else /* !defined(WIN32) */
  128. /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
  129. our buffer like on some 64bit sun systems.... but hey, its time to move on */
  130. chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
  131. if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
  132. #endif /* defined(WIN32) */
  133. b = (char*)malloc(sizeof(char)*chars);
  134. if(!b) { return -1; }
  135. if((chars = vsprintf(b, fmt, ap)) < 0)
  136. {
  137. free(b);
  138. } else {
  139. *buf = b;
  140. }
  141. return chars;
  142. }
  143. #endif /* !HAVE_VASPRINTF */
  144. int sprintbuf(struct printbuf *p, const char *msg, ...)
  145. {
  146. va_list ap;
  147. char *t;
  148. int size;
  149. char buf[128];
  150. /* user stack buffer first */
  151. va_start(ap, msg);
  152. size = vsnprintf(buf, 128, msg, ap);
  153. va_end(ap);
  154. /* if string is greater than stack buffer, then use dynamic string
  155. with vasprintf. Note: some implementation of vsnprintf return -1
  156. if output is truncated whereas some return the number of bytes that
  157. would have been written - this code handles both cases. */
  158. if(size == -1 || size > 127) {
  159. va_start(ap, msg);
  160. if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
  161. va_end(ap);
  162. printbuf_memappend(p, t, size);
  163. free(t);
  164. return size;
  165. } else {
  166. printbuf_memappend(p, buf, size);
  167. return size;
  168. }
  169. }
  170. void printbuf_reset(struct printbuf *p)
  171. {
  172. p->buf[0] = '\0';
  173. p->bpos = 0;
  174. }
  175. void printbuf_free(struct printbuf *p)
  176. {
  177. if(p) {
  178. free(p->buf);
  179. free(p);
  180. }
  181. }