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.

403 lines
11KB

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