bprint.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 <time.h>
  24. #include "avassert.h"
  25. #include "bprint.h"
  26. #include "common.h"
  27. #include "error.h"
  28. #include "mem.h"
  29. #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
  30. #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
  31. static int av_bprint_alloc(AVBPrint *buf, unsigned room)
  32. {
  33. char *old_str, *new_str;
  34. unsigned min_size, new_size;
  35. if (buf->size == buf->size_max)
  36. return AVERROR(EIO);
  37. if (!av_bprint_is_complete(buf))
  38. return AVERROR_INVALIDDATA; /* it is already truncated anyway */
  39. min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
  40. new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
  41. if (new_size < min_size)
  42. new_size = FFMIN(buf->size_max, min_size);
  43. old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
  44. new_str = av_realloc(old_str, new_size);
  45. if (!new_str)
  46. return AVERROR(ENOMEM);
  47. if (!old_str)
  48. memcpy(new_str, buf->str, buf->len + 1);
  49. buf->str = new_str;
  50. buf->size = new_size;
  51. return 0;
  52. }
  53. static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
  54. {
  55. /* arbitrary margin to avoid small overflows */
  56. extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
  57. buf->len += extra_len;
  58. if (buf->size)
  59. buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
  60. }
  61. void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
  62. {
  63. unsigned size_auto = (char *)buf + sizeof(*buf) -
  64. buf->reserved_internal_buffer;
  65. if (size_max == 1)
  66. size_max = size_auto;
  67. buf->str = buf->reserved_internal_buffer;
  68. buf->len = 0;
  69. buf->size = FFMIN(size_auto, size_max);
  70. buf->size_max = size_max;
  71. *buf->str = 0;
  72. if (size_init > buf->size)
  73. av_bprint_alloc(buf, size_init - 1);
  74. }
  75. void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
  76. {
  77. buf->str = buffer;
  78. buf->len = 0;
  79. buf->size = size;
  80. buf->size_max = size;
  81. *buf->str = 0;
  82. }
  83. void av_bprintf(AVBPrint *buf, const char *fmt, ...)
  84. {
  85. unsigned room;
  86. char *dst;
  87. va_list vl;
  88. int extra_len;
  89. while (1) {
  90. room = av_bprint_room(buf);
  91. dst = room ? buf->str + buf->len : NULL;
  92. va_start(vl, fmt);
  93. extra_len = vsnprintf(dst, room, fmt, vl);
  94. va_end(vl);
  95. if (extra_len <= 0)
  96. return;
  97. if (extra_len < room)
  98. break;
  99. if (av_bprint_alloc(buf, extra_len))
  100. break;
  101. }
  102. av_bprint_grow(buf, extra_len);
  103. }
  104. void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
  105. {
  106. unsigned room, real_n;
  107. while (1) {
  108. room = av_bprint_room(buf);
  109. if (n < room)
  110. break;
  111. if (av_bprint_alloc(buf, n))
  112. break;
  113. }
  114. if (room) {
  115. real_n = FFMIN(n, room - 1);
  116. memset(buf->str + buf->len, c, real_n);
  117. }
  118. av_bprint_grow(buf, n);
  119. }
  120. void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
  121. {
  122. unsigned room;
  123. size_t l;
  124. if (!*fmt)
  125. return;
  126. while (1) {
  127. room = av_bprint_room(buf);
  128. if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
  129. break;
  130. /* strftime does not tell us how much room it would need: let us
  131. retry with twice as much until the buffer is large enough */
  132. room = !room ? strlen(fmt) + 1 :
  133. room <= INT_MAX / 2 ? room * 2 : INT_MAX;
  134. if (av_bprint_alloc(buf, room)) {
  135. /* impossible to grow, try to manage something useful anyway */
  136. room = av_bprint_room(buf);
  137. if (room < 1024) {
  138. /* if strftime fails because the buffer has (almost) reached
  139. its maximum size, let us try in a local buffer; 1k should
  140. be enough to format any real date+time string */
  141. char buf2[1024];
  142. if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
  143. av_bprintf(buf, "%s", buf2);
  144. return;
  145. }
  146. }
  147. if (room) {
  148. /* if anything else failed and the buffer is not already
  149. truncated, let us add a stock string and force truncation */
  150. static const char txt[] = "[truncated strftime output]";
  151. memset(buf->str + buf->len, '!', room);
  152. memcpy(buf->str + buf->len, txt, FFMIN(sizeof(txt) - 1, room));
  153. av_bprint_grow(buf, room); /* force truncation */
  154. }
  155. return;
  156. }
  157. }
  158. av_bprint_grow(buf, l);
  159. }
  160. void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
  161. unsigned char **mem, unsigned *actual_size)
  162. {
  163. if (size > av_bprint_room(buf))
  164. av_bprint_alloc(buf, size);
  165. *actual_size = av_bprint_room(buf);
  166. *mem = *actual_size ? buf->str + buf->len : NULL;
  167. }
  168. void av_bprint_clear(AVBPrint *buf)
  169. {
  170. if (buf->len) {
  171. *buf->str = 0;
  172. buf->len = 0;
  173. }
  174. }
  175. int av_bprint_finalize(AVBPrint *buf, char **ret_str)
  176. {
  177. unsigned real_size = FFMIN(buf->len + 1, buf->size);
  178. char *str;
  179. int ret = 0;
  180. if (ret_str) {
  181. if (av_bprint_is_allocated(buf)) {
  182. str = av_realloc(buf->str, real_size);
  183. if (!str)
  184. str = buf->str;
  185. buf->str = NULL;
  186. } else {
  187. str = av_malloc(real_size);
  188. if (str)
  189. memcpy(str, buf->str, real_size);
  190. else
  191. ret = AVERROR(ENOMEM);
  192. }
  193. *ret_str = str;
  194. } else {
  195. if (av_bprint_is_allocated(buf))
  196. av_freep(&buf->str);
  197. }
  198. buf->size = real_size;
  199. return ret;
  200. }
  201. #ifdef TEST
  202. #undef printf
  203. static void bprint_pascal(AVBPrint *b, unsigned size)
  204. {
  205. unsigned i, j;
  206. unsigned p[42];
  207. av_assert0(size < FF_ARRAY_ELEMS(p));
  208. p[0] = 1;
  209. av_bprintf(b, "%8d\n", 1);
  210. for (i = 1; i <= size; i++) {
  211. p[i] = 1;
  212. for (j = i - 1; j > 0; j--)
  213. p[j] = p[j] + p[j - 1];
  214. for (j = 0; j <= i; j++)
  215. av_bprintf(b, "%8d", p[j]);
  216. av_bprintf(b, "\n");
  217. }
  218. }
  219. int main(void)
  220. {
  221. AVBPrint b;
  222. char buf[256];
  223. struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
  224. av_bprint_init(&b, 0, -1);
  225. bprint_pascal(&b, 5);
  226. printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  227. printf("%s\n", b.str);
  228. av_bprint_finalize(&b, NULL);
  229. av_bprint_init(&b, 0, -1);
  230. bprint_pascal(&b, 25);
  231. printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  232. av_bprint_finalize(&b, NULL);
  233. av_bprint_init(&b, 0, 2048);
  234. bprint_pascal(&b, 25);
  235. printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  236. av_bprint_finalize(&b, NULL);
  237. av_bprint_init(&b, 0, 1);
  238. bprint_pascal(&b, 5);
  239. printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  240. av_bprint_init(&b, 0, 1);
  241. bprint_pascal(&b, 25);
  242. printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
  243. /* Note that the size of the automatic buffer is arch-dependant. */
  244. av_bprint_init(&b, 0, 0);
  245. bprint_pascal(&b, 25);
  246. printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
  247. av_bprint_init_for_buffer(&b, buf, sizeof(buf));
  248. bprint_pascal(&b, 25);
  249. printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
  250. av_bprint_init(&b, 0, -1);
  251. av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
  252. printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
  253. av_bprint_finalize(&b, NULL);
  254. av_bprint_init(&b, 0, 8);
  255. av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
  256. printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
  257. return 0;
  258. }
  259. #endif