bprint.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2012 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "avassert.h"
  24. #include "bprint.h"
  25. #include "common.h"
  26. #include "error.h"
  27. #include "mem.h"
  28. #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
  29. #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
  30. static int av_bprint_alloc(AVBPrint *buf, unsigned room)
  31. {
  32. char *old_str, *new_str;
  33. unsigned min_size, new_size;
  34. if (buf->size == buf->size_max)
  35. return AVERROR(EIO);
  36. if (!av_bprint_is_complete(buf))
  37. return AVERROR_INVALIDDATA; /* it is already truncated anyway */
  38. min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
  39. new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
  40. if (new_size < min_size)
  41. new_size = FFMIN(buf->size_max, min_size);
  42. old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
  43. new_str = av_realloc(old_str, new_size);
  44. if (!new_str)
  45. return AVERROR(ENOMEM);
  46. if (!old_str)
  47. memcpy(new_str, buf->str, buf->len + 1);
  48. buf->str = new_str;
  49. buf->size = new_size;
  50. return 0;
  51. }
  52. static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
  53. {
  54. /* arbitrary margin to avoid small overflows */
  55. extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
  56. buf->len += extra_len;
  57. if (buf->size)
  58. buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
  59. }
  60. void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
  61. {
  62. unsigned size_auto = (char *)buf + sizeof(*buf) -
  63. buf->reserved_internal_buffer;
  64. if (size_max == 1)
  65. size_max = size_auto;
  66. buf->str = buf->reserved_internal_buffer;
  67. buf->len = 0;
  68. buf->size = FFMIN(size_auto, size_max);
  69. buf->size_max = size_max;
  70. *buf->str = 0;
  71. if (size_init > buf->size)
  72. av_bprint_alloc(buf, size_init - 1);
  73. }
  74. void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
  75. {
  76. buf->str = buffer;
  77. buf->len = 0;
  78. buf->size = size;
  79. buf->size_max = size;
  80. *buf->str = 0;
  81. }
  82. void av_bprintf(AVBPrint *buf, const char *fmt, ...)
  83. {
  84. unsigned room;
  85. char *dst;
  86. va_list vl;
  87. int extra_len;
  88. while (1) {
  89. room = av_bprint_room(buf);
  90. dst = room ? buf->str + buf->len : NULL;
  91. va_start(vl, fmt);
  92. extra_len = vsnprintf(dst, room, fmt, vl);
  93. va_end(vl);
  94. if (extra_len <= 0)
  95. return;
  96. if (extra_len < room)
  97. break;
  98. if (av_bprint_alloc(buf, extra_len))
  99. break;
  100. }
  101. av_bprint_grow(buf, extra_len);
  102. }
  103. void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
  104. {
  105. unsigned room, real_n;
  106. while (1) {
  107. room = av_bprint_room(buf);
  108. if (n < room)
  109. break;
  110. if (av_bprint_alloc(buf, n))
  111. break;
  112. }
  113. if (room) {
  114. real_n = FFMIN(n, room - 1);
  115. memset(buf->str + buf->len, c, real_n);
  116. }
  117. av_bprint_grow(buf, n);
  118. }
  119. void av_bprint_clear(AVBPrint *buf)
  120. {
  121. if (buf->len) {
  122. *buf->str = 0;
  123. buf->len = 0;
  124. }
  125. }
  126. int av_bprint_finalize(AVBPrint *buf, char **ret_str)
  127. {
  128. unsigned real_size = FFMIN(buf->len + 1, buf->size);
  129. char *str;
  130. int ret = 0;
  131. if (ret_str) {
  132. if (av_bprint_is_allocated(buf)) {
  133. str = av_realloc(buf->str, real_size);
  134. if (!str)
  135. str = buf->str;
  136. buf->str = NULL;
  137. } else {
  138. str = av_malloc(real_size);
  139. if (str)
  140. memcpy(str, buf->str, real_size);
  141. else
  142. ret = AVERROR(ENOMEM);
  143. }
  144. *ret_str = str;
  145. } else {
  146. if (av_bprint_is_allocated(buf))
  147. av_freep(&buf->str);
  148. }
  149. buf->size = real_size;
  150. return ret;
  151. }
  152. #ifdef TEST
  153. #undef printf
  154. static void bprint_pascal(AVBPrint *b, unsigned size)
  155. {
  156. unsigned i, j;
  157. unsigned p[42];
  158. av_assert0(size < FF_ARRAY_ELEMS(p));
  159. p[0] = 1;
  160. av_bprintf(b, "%8d\n", 1);
  161. for (i = 1; i <= size; i++) {
  162. p[i] = 1;
  163. for (j = i - 1; j > 0; j--)
  164. p[j] = p[j] + p[j - 1];
  165. for (j = 0; j <= i; j++)
  166. av_bprintf(b, "%8d", p[j]);
  167. av_bprintf(b, "\n");
  168. }
  169. }
  170. int main(void)
  171. {
  172. AVBPrint b;
  173. char buf[256];
  174. av_bprint_init(&b, 0, -1);
  175. bprint_pascal(&b, 5);
  176. printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  177. printf("%s\n", b.str);
  178. av_bprint_finalize(&b, NULL);
  179. av_bprint_init(&b, 0, -1);
  180. bprint_pascal(&b, 25);
  181. printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  182. av_bprint_finalize(&b, NULL);
  183. av_bprint_init(&b, 0, 2048);
  184. bprint_pascal(&b, 25);
  185. printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  186. av_bprint_finalize(&b, NULL);
  187. av_bprint_init(&b, 0, 1);
  188. bprint_pascal(&b, 5);
  189. printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  190. av_bprint_init(&b, 0, 1);
  191. bprint_pascal(&b, 25);
  192. printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
  193. /* Note that the size of the automatic buffer is arch-dependant. */
  194. av_bprint_init(&b, 0, 0);
  195. bprint_pascal(&b, 25);
  196. printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  197. av_bprint_init_for_buffer(&b, buf, sizeof(buf));
  198. bprint_pascal(&b, 25);
  199. printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
  200. return 0;
  201. }
  202. #endif