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.

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