bprint.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "bprint.h"
  24. #include "common.h"
  25. #include "error.h"
  26. #include "mem.h"
  27. #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
  28. #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
  29. static int av_bprint_alloc(AVBPrint *buf, unsigned room)
  30. {
  31. char *old_str, *new_str;
  32. unsigned min_size, new_size;
  33. if (buf->size == buf->size_max)
  34. return AVERROR(EIO);
  35. if (!av_bprint_is_complete(buf))
  36. return AVERROR_INVALIDDATA; /* it is already truncated anyway */
  37. min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
  38. new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
  39. if (new_size < min_size)
  40. new_size = FFMIN(buf->size_max, min_size);
  41. old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
  42. new_str = av_realloc(old_str, new_size);
  43. if (!new_str)
  44. return AVERROR(ENOMEM);
  45. if (!old_str)
  46. memcpy(new_str, buf->str, buf->len + 1);
  47. buf->str = new_str;
  48. buf->size = new_size;
  49. return 0;
  50. }
  51. static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
  52. {
  53. /* arbitrary margin to avoid small overflows */
  54. extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
  55. buf->len += extra_len;
  56. if (buf->size)
  57. buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
  58. }
  59. void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
  60. {
  61. unsigned size_auto = (char *)buf + sizeof(*buf) -
  62. buf->reserved_internal_buffer;
  63. if (size_max == 1)
  64. size_max = size_auto;
  65. buf->str = buf->reserved_internal_buffer;
  66. buf->len = 0;
  67. buf->size = FFMIN(size_auto, size_max);
  68. buf->size_max = size_max;
  69. *buf->str = 0;
  70. if (size_init > buf->size)
  71. av_bprint_alloc(buf, size_init - 1);
  72. }
  73. void av_bprintf(AVBPrint *buf, const char *fmt, ...)
  74. {
  75. unsigned room;
  76. char *dst;
  77. va_list vl;
  78. int extra_len;
  79. while (1) {
  80. room = av_bprint_room(buf);
  81. dst = room ? buf->str + buf->len : NULL;
  82. va_start(vl, fmt);
  83. extra_len = vsnprintf(dst, room, fmt, vl);
  84. va_end(vl);
  85. if (extra_len <= 0)
  86. return;
  87. if (extra_len < room)
  88. break;
  89. if (av_bprint_alloc(buf, extra_len))
  90. break;
  91. }
  92. av_bprint_grow(buf, extra_len);
  93. }
  94. void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
  95. {
  96. unsigned room, real_n;
  97. while (1) {
  98. room = av_bprint_room(buf);
  99. if (n < room)
  100. break;
  101. if (av_bprint_alloc(buf, n))
  102. break;
  103. }
  104. if (room) {
  105. real_n = FFMIN(n, room - 1);
  106. memset(buf->str + buf->len, c, real_n);
  107. }
  108. av_bprint_grow(buf, n);
  109. }
  110. void av_bprint_clear(AVBPrint *buf)
  111. {
  112. if (buf->len) {
  113. *buf->str = 0;
  114. buf->len = 0;
  115. }
  116. }
  117. int av_bprint_finalize(AVBPrint *buf, char **ret_str)
  118. {
  119. unsigned real_size = FFMIN(buf->len + 1, buf->size);
  120. char *str;
  121. int ret = 0;
  122. if (ret_str) {
  123. if (av_bprint_is_allocated(buf)) {
  124. str = av_realloc(buf->str, real_size);
  125. if (!str)
  126. str = buf->str;
  127. buf->str = NULL;
  128. } else {
  129. str = av_malloc(real_size);
  130. if (str)
  131. memcpy(str, buf->str, real_size);
  132. else
  133. ret = AVERROR(ENOMEM);
  134. }
  135. *ret_str = str;
  136. } else {
  137. if (av_bprint_is_allocated(buf))
  138. av_freep(&buf->str);
  139. }
  140. buf->size = real_size;
  141. return ret;
  142. }
  143. #ifdef TEST
  144. #undef printf
  145. static void bprint_pascal(AVBPrint *b, unsigned size)
  146. {
  147. unsigned p[size + 1], i, j;
  148. p[0] = 1;
  149. av_bprintf(b, "%8d\n", 1);
  150. for (i = 1; i <= size; i++) {
  151. p[i] = 1;
  152. for (j = i - 1; j > 0; j--)
  153. p[j] = p[j] + p[j - 1];
  154. for (j = 0; j <= i; j++)
  155. av_bprintf(b, "%8d", p[j]);
  156. av_bprintf(b, "\n");
  157. }
  158. }
  159. int main(void)
  160. {
  161. AVBPrint b;
  162. av_bprint_init(&b, 0, -1);
  163. bprint_pascal(&b, 5);
  164. printf("Short text in unlimited buffer: %zu/%u\n", strlen(b.str), b.len);
  165. printf("%s\n", b.str);
  166. av_bprint_finalize(&b, NULL);
  167. av_bprint_init(&b, 0, -1);
  168. bprint_pascal(&b, 25);
  169. printf("Long text in unlimited buffer: %zu/%u\n", strlen(b.str), b.len);
  170. av_bprint_finalize(&b, NULL);
  171. av_bprint_init(&b, 0, 2048);
  172. bprint_pascal(&b, 25);
  173. printf("Long text in limited buffer: %zu/%u\n", strlen(b.str), b.len);
  174. av_bprint_finalize(&b, NULL);
  175. av_bprint_init(&b, 0, 1);
  176. bprint_pascal(&b, 5);
  177. printf("Short text in automatic buffer: %zu/%u\n", strlen(b.str), b.len);
  178. av_bprint_init(&b, 0, 1);
  179. bprint_pascal(&b, 25);
  180. printf("Long text in automatic buffer: %zu/%u\n", strlen(b.str), b.len);
  181. /* Note that the size of the automatic buffer is arch-dependant. */
  182. av_bprint_init(&b, 0, 0);
  183. bprint_pascal(&b, 25);
  184. printf("Long text count only buffer: %zu/%u\n", strlen(b.str), b.len);
  185. return 0;
  186. }
  187. #endif