bprint.c 10 KB

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