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

252 lines
6.5KB

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