bprint.c 11 KB

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