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.

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