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.

229 lines
6.1KB

  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_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
  74. {
  75. buf->str = buffer;
  76. buf->len = 0;
  77. buf->size = size;
  78. buf->size_max = size;
  79. *buf->str = 0;
  80. }
  81. void av_bprintf(AVBPrint *buf, const char *fmt, ...)
  82. {
  83. unsigned room;
  84. char *dst;
  85. va_list vl;
  86. int extra_len;
  87. while (1) {
  88. room = av_bprint_room(buf);
  89. dst = room ? buf->str + buf->len : NULL;
  90. va_start(vl, fmt);
  91. extra_len = vsnprintf(dst, room, fmt, vl);
  92. va_end(vl);
  93. if (extra_len <= 0)
  94. return;
  95. if (extra_len < room)
  96. break;
  97. if (av_bprint_alloc(buf, extra_len))
  98. break;
  99. }
  100. av_bprint_grow(buf, extra_len);
  101. }
  102. void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
  103. {
  104. unsigned room, real_n;
  105. while (1) {
  106. room = av_bprint_room(buf);
  107. if (n < room)
  108. break;
  109. if (av_bprint_alloc(buf, n))
  110. break;
  111. }
  112. if (room) {
  113. real_n = FFMIN(n, room - 1);
  114. memset(buf->str + buf->len, c, real_n);
  115. }
  116. av_bprint_grow(buf, n);
  117. }
  118. void av_bprint_clear(AVBPrint *buf)
  119. {
  120. if (buf->len) {
  121. *buf->str = 0;
  122. buf->len = 0;
  123. }
  124. }
  125. int av_bprint_finalize(AVBPrint *buf, char **ret_str)
  126. {
  127. unsigned real_size = FFMIN(buf->len + 1, buf->size);
  128. char *str;
  129. int ret = 0;
  130. if (ret_str) {
  131. if (av_bprint_is_allocated(buf)) {
  132. str = av_realloc(buf->str, real_size);
  133. if (!str)
  134. str = buf->str;
  135. buf->str = NULL;
  136. } else {
  137. str = av_malloc(real_size);
  138. if (str)
  139. memcpy(str, buf->str, real_size);
  140. else
  141. ret = AVERROR(ENOMEM);
  142. }
  143. *ret_str = str;
  144. } else {
  145. if (av_bprint_is_allocated(buf))
  146. av_freep(&buf->str);
  147. }
  148. buf->size = real_size;
  149. return ret;
  150. }
  151. #ifdef TEST
  152. #undef printf
  153. static void bprint_pascal(AVBPrint *b, unsigned size)
  154. {
  155. unsigned p[size + 1], i, j;
  156. p[0] = 1;
  157. av_bprintf(b, "%8d\n", 1);
  158. for (i = 1; i <= size; i++) {
  159. p[i] = 1;
  160. for (j = i - 1; j > 0; j--)
  161. p[j] = p[j] + p[j - 1];
  162. for (j = 0; j <= i; j++)
  163. av_bprintf(b, "%8d", p[j]);
  164. av_bprintf(b, "\n");
  165. }
  166. }
  167. int main(void)
  168. {
  169. AVBPrint b;
  170. char buf[256];
  171. av_bprint_init(&b, 0, -1);
  172. bprint_pascal(&b, 5);
  173. printf("Short text in unlimited buffer: %zu/%u\n", strlen(b.str), b.len);
  174. printf("%s\n", b.str);
  175. av_bprint_finalize(&b, NULL);
  176. av_bprint_init(&b, 0, -1);
  177. bprint_pascal(&b, 25);
  178. printf("Long text in unlimited buffer: %zu/%u\n", strlen(b.str), b.len);
  179. av_bprint_finalize(&b, NULL);
  180. av_bprint_init(&b, 0, 2048);
  181. bprint_pascal(&b, 25);
  182. printf("Long text in limited buffer: %zu/%u\n", strlen(b.str), b.len);
  183. av_bprint_finalize(&b, NULL);
  184. av_bprint_init(&b, 0, 1);
  185. bprint_pascal(&b, 5);
  186. printf("Short text in automatic buffer: %zu/%u\n", strlen(b.str), b.len);
  187. av_bprint_init(&b, 0, 1);
  188. bprint_pascal(&b, 25);
  189. printf("Long text in automatic buffer: %zu/%u\n", strlen(b.str)/8*8, b.len);
  190. /* Note that the size of the automatic buffer is arch-dependant. */
  191. av_bprint_init(&b, 0, 0);
  192. bprint_pascal(&b, 25);
  193. printf("Long text count only buffer: %zu/%u\n", strlen(b.str), b.len);
  194. av_bprint_init_for_buffer(&b, buf, sizeof(buf));
  195. bprint_pascal(&b, 25);
  196. printf("Long text count only buffer: %zu/%u\n", strlen(buf), b.len);
  197. return 0;
  198. }
  199. #endif