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.

520 lines
12KB

  1. /*
  2. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  3. * Copyright (c) 2007 Mans Rullgard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdarg.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "config.h"
  26. #include "common.h"
  27. #include "mem.h"
  28. #include "avassert.h"
  29. #include "avstring.h"
  30. #include "bprint.h"
  31. int av_strstart(const char *str, const char *pfx, const char **ptr)
  32. {
  33. while (*pfx && *pfx == *str) {
  34. pfx++;
  35. str++;
  36. }
  37. if (!*pfx && ptr)
  38. *ptr = str;
  39. return !*pfx;
  40. }
  41. int av_stristart(const char *str, const char *pfx, const char **ptr)
  42. {
  43. while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
  44. pfx++;
  45. str++;
  46. }
  47. if (!*pfx && ptr)
  48. *ptr = str;
  49. return !*pfx;
  50. }
  51. char *av_stristr(const char *s1, const char *s2)
  52. {
  53. if (!*s2)
  54. return (char*)(intptr_t)s1;
  55. do
  56. if (av_stristart(s1, s2, NULL))
  57. return (char*)(intptr_t)s1;
  58. while (*s1++);
  59. return NULL;
  60. }
  61. char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
  62. {
  63. size_t needle_len = strlen(needle);
  64. if (!needle_len)
  65. return (char*)haystack;
  66. while (hay_length >= needle_len) {
  67. hay_length--;
  68. if (!memcmp(haystack, needle, needle_len))
  69. return (char*)haystack;
  70. haystack++;
  71. }
  72. return NULL;
  73. }
  74. size_t av_strlcpy(char *dst, const char *src, size_t size)
  75. {
  76. size_t len = 0;
  77. while (++len < size && *src)
  78. *dst++ = *src++;
  79. if (len <= size)
  80. *dst = 0;
  81. return len + strlen(src) - 1;
  82. }
  83. size_t av_strlcat(char *dst, const char *src, size_t size)
  84. {
  85. size_t len = strlen(dst);
  86. if (size <= len + 1)
  87. return len + strlen(src);
  88. return len + av_strlcpy(dst + len, src, size - len);
  89. }
  90. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  91. {
  92. size_t len = strlen(dst);
  93. va_list vl;
  94. va_start(vl, fmt);
  95. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  96. va_end(vl);
  97. return len;
  98. }
  99. char *av_asprintf(const char *fmt, ...)
  100. {
  101. char *p = NULL;
  102. va_list va;
  103. int len;
  104. va_start(va, fmt);
  105. len = vsnprintf(NULL, 0, fmt, va);
  106. va_end(va);
  107. if (len < 0)
  108. goto end;
  109. p = av_malloc(len + 1);
  110. if (!p)
  111. goto end;
  112. va_start(va, fmt);
  113. len = vsnprintf(p, len + 1, fmt, va);
  114. va_end(va);
  115. if (len < 0)
  116. av_freep(&p);
  117. end:
  118. return p;
  119. }
  120. char *av_d2str(double d)
  121. {
  122. char *str = av_malloc(16);
  123. if (str)
  124. snprintf(str, 16, "%f", d);
  125. return str;
  126. }
  127. #define WHITESPACES " \n\t"
  128. char *av_get_token(const char **buf, const char *term)
  129. {
  130. char *out = av_malloc(strlen(*buf) + 1);
  131. char *ret = out, *end = out;
  132. const char *p = *buf;
  133. if (!out)
  134. return NULL;
  135. p += strspn(p, WHITESPACES);
  136. while (*p && !strspn(p, term)) {
  137. char c = *p++;
  138. if (c == '\\' && *p) {
  139. *out++ = *p++;
  140. end = out;
  141. } else if (c == '\'') {
  142. while (*p && *p != '\'')
  143. *out++ = *p++;
  144. if (*p) {
  145. p++;
  146. end = out;
  147. }
  148. } else {
  149. *out++ = c;
  150. }
  151. }
  152. do
  153. *out-- = 0;
  154. while (out >= end && strspn(out, WHITESPACES));
  155. *buf = p;
  156. return ret;
  157. }
  158. char *av_strtok(char *s, const char *delim, char **saveptr)
  159. {
  160. char *tok;
  161. if (!s && !(s = *saveptr))
  162. return NULL;
  163. /* skip leading delimiters */
  164. s += strspn(s, delim);
  165. /* s now points to the first non delimiter char, or to the end of the string */
  166. if (!*s) {
  167. *saveptr = NULL;
  168. return NULL;
  169. }
  170. tok = s++;
  171. /* skip non delimiters */
  172. s += strcspn(s, delim);
  173. if (*s) {
  174. *s = 0;
  175. *saveptr = s+1;
  176. } else {
  177. *saveptr = NULL;
  178. }
  179. return tok;
  180. }
  181. int av_strcasecmp(const char *a, const char *b)
  182. {
  183. uint8_t c1, c2;
  184. do {
  185. c1 = av_tolower(*a++);
  186. c2 = av_tolower(*b++);
  187. } while (c1 && c1 == c2);
  188. return c1 - c2;
  189. }
  190. int av_strncasecmp(const char *a, const char *b, size_t n)
  191. {
  192. uint8_t c1, c2;
  193. if (n <= 0)
  194. return 0;
  195. do {
  196. c1 = av_tolower(*a++);
  197. c2 = av_tolower(*b++);
  198. } while (--n && c1 && c1 == c2);
  199. return c1 - c2;
  200. }
  201. const char *av_basename(const char *path)
  202. {
  203. char *p = strrchr(path, '/');
  204. #if HAVE_DOS_PATHS
  205. char *q = strrchr(path, '\\');
  206. char *d = strchr(path, ':');
  207. p = FFMAX3(p, q, d);
  208. #endif
  209. if (!p)
  210. return path;
  211. return p + 1;
  212. }
  213. const char *av_dirname(char *path)
  214. {
  215. char *p = strrchr(path, '/');
  216. #if HAVE_DOS_PATHS
  217. char *q = strrchr(path, '\\');
  218. char *d = strchr(path, ':');
  219. d = d ? d + 1 : d;
  220. p = FFMAX3(p, q, d);
  221. #endif
  222. if (!p)
  223. return ".";
  224. *p = '\0';
  225. return path;
  226. }
  227. char *av_append_path_component(const char *path, const char *component)
  228. {
  229. size_t p_len, c_len;
  230. char *fullpath;
  231. if (!path)
  232. return av_strdup(component);
  233. if (!component)
  234. return av_strdup(path);
  235. p_len = strlen(path);
  236. c_len = strlen(component);
  237. if (p_len > SIZE_MAX - c_len || p_len + c_len > SIZE_MAX - 2)
  238. return NULL;
  239. fullpath = av_malloc(p_len + c_len + 2);
  240. if (fullpath) {
  241. if (p_len) {
  242. av_strlcpy(fullpath, path, p_len + 1);
  243. if (c_len) {
  244. if (fullpath[p_len - 1] != '/' && component[0] != '/')
  245. fullpath[p_len++] = '/';
  246. else if (fullpath[p_len - 1] == '/' && component[0] == '/')
  247. p_len--;
  248. }
  249. }
  250. av_strlcpy(&fullpath[p_len], component, c_len + 1);
  251. fullpath[p_len + c_len] = 0;
  252. }
  253. return fullpath;
  254. }
  255. int av_escape(char **dst, const char *src, const char *special_chars,
  256. enum AVEscapeMode mode, int flags)
  257. {
  258. AVBPrint dstbuf;
  259. av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  260. av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
  261. if (!av_bprint_is_complete(&dstbuf)) {
  262. av_bprint_finalize(&dstbuf, NULL);
  263. return AVERROR(ENOMEM);
  264. } else {
  265. av_bprint_finalize(&dstbuf, dst);
  266. return dstbuf.len;
  267. }
  268. }
  269. int av_isdigit(int c)
  270. {
  271. return c >= '0' && c <= '9';
  272. }
  273. int av_isgraph(int c)
  274. {
  275. return c > 32 && c < 127;
  276. }
  277. int av_isspace(int c)
  278. {
  279. return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
  280. c == '\v';
  281. }
  282. int av_isxdigit(int c)
  283. {
  284. c = av_tolower(c);
  285. return av_isdigit(c) || (c >= 'a' && c <= 'f');
  286. }
  287. int av_match_name(const char *name, const char *names)
  288. {
  289. const char *p;
  290. int len, namelen;
  291. if (!name || !names)
  292. return 0;
  293. namelen = strlen(name);
  294. while ((p = strchr(names, ','))) {
  295. len = FFMAX(p - names, namelen);
  296. if (!av_strncasecmp(name, names, len))
  297. return 1;
  298. names = p + 1;
  299. }
  300. return !av_strcasecmp(name, names);
  301. }
  302. int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
  303. unsigned int flags)
  304. {
  305. const uint8_t *p = *bufp;
  306. uint32_t top;
  307. uint64_t code;
  308. int ret = 0, tail_len;
  309. uint32_t overlong_encoding_mins[6] = {
  310. 0x00000000, 0x00000080, 0x00000800, 0x00010000, 0x00200000, 0x04000000,
  311. };
  312. if (p >= buf_end)
  313. return 0;
  314. code = *p++;
  315. /* first sequence byte starts with 10, or is 1111-1110 or 1111-1111,
  316. which is not admitted */
  317. if ((code & 0xc0) == 0x80 || code >= 0xFE) {
  318. ret = AVERROR(EILSEQ);
  319. goto end;
  320. }
  321. top = (code & 128) >> 1;
  322. tail_len = 0;
  323. while (code & top) {
  324. int tmp;
  325. tail_len++;
  326. if (p >= buf_end) {
  327. (*bufp) ++;
  328. return AVERROR(EILSEQ); /* incomplete sequence */
  329. }
  330. /* we assume the byte to be in the form 10xx-xxxx */
  331. tmp = *p++ - 128; /* strip leading 1 */
  332. if (tmp>>6) {
  333. (*bufp) ++;
  334. return AVERROR(EILSEQ);
  335. }
  336. code = (code<<6) + tmp;
  337. top <<= 5;
  338. }
  339. code &= (top << 1) - 1;
  340. /* check for overlong encodings */
  341. av_assert0(tail_len <= 5);
  342. if (code < overlong_encoding_mins[tail_len]) {
  343. ret = AVERROR(EILSEQ);
  344. goto end;
  345. }
  346. if (code >= 1U<<31) {
  347. ret = AVERROR(EILSEQ); /* out-of-range value */
  348. goto end;
  349. }
  350. *codep = code;
  351. if (code > 0x10FFFF &&
  352. !(flags & AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES))
  353. ret = AVERROR(EILSEQ);
  354. if (code < 0x20 && code != 0x9 && code != 0xA && code != 0xD &&
  355. flags & AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES)
  356. ret = AVERROR(EILSEQ);
  357. if (code >= 0xD800 && code <= 0xDFFF &&
  358. !(flags & AV_UTF8_FLAG_ACCEPT_SURROGATES))
  359. ret = AVERROR(EILSEQ);
  360. if ((code == 0xFFFE || code == 0xFFFF) &&
  361. !(flags & AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS))
  362. ret = AVERROR(EILSEQ);
  363. end:
  364. *bufp = p;
  365. return ret;
  366. }
  367. int av_match_list(const char *name, const char *list, char separator)
  368. {
  369. const char *p, *q;
  370. for (p = name; p && *p; ) {
  371. for (q = list; q && *q; ) {
  372. int k;
  373. for (k = 0; p[k] == q[k] || (p[k]*q[k] == 0 && p[k]+q[k] == separator); k++)
  374. if (k && (!p[k] || p[k] == separator))
  375. return 1;
  376. q = strchr(q, separator);
  377. q += !!q;
  378. }
  379. p = strchr(p, separator);
  380. p += !!p;
  381. }
  382. return 0;
  383. }
  384. #ifdef TEST
  385. int main(void)
  386. {
  387. int i;
  388. char *fullpath;
  389. static const char * const strings[] = {
  390. "''",
  391. "",
  392. ":",
  393. "\\",
  394. "'",
  395. " '' :",
  396. " '' '' :",
  397. "foo '' :",
  398. "'foo'",
  399. "foo ",
  400. " ' foo ' ",
  401. "foo\\",
  402. "foo': blah:blah",
  403. "foo\\: blah:blah",
  404. "foo\'",
  405. "'foo : ' :blahblah",
  406. "\\ :blah",
  407. " foo",
  408. " foo ",
  409. " foo \\ ",
  410. "foo ':blah",
  411. " foo bar : blahblah",
  412. "\\f\\o\\o",
  413. "'foo : \\ \\ ' : blahblah",
  414. "'\\fo\\o:': blahblah",
  415. "\\'fo\\o\\:': foo ' :blahblah"
  416. };
  417. printf("Testing av_get_token()\n");
  418. for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
  419. const char *p = strings[i];
  420. char *q;
  421. printf("|%s|", p);
  422. q = av_get_token(&p, ":");
  423. printf(" -> |%s|", q);
  424. printf(" + |%s|\n", p);
  425. av_free(q);
  426. }
  427. printf("Testing av_append_path_component()\n");
  428. #define TEST_APPEND_PATH_COMPONENT(path, component, expected) \
  429. fullpath = av_append_path_component((path), (component)); \
  430. printf("%s = %s\n", fullpath ? fullpath : "(null)", expected); \
  431. av_free(fullpath);
  432. TEST_APPEND_PATH_COMPONENT(NULL, NULL, "(null)")
  433. TEST_APPEND_PATH_COMPONENT("path", NULL, "path");
  434. TEST_APPEND_PATH_COMPONENT(NULL, "comp", "comp");
  435. TEST_APPEND_PATH_COMPONENT("path", "comp", "path/comp");
  436. TEST_APPEND_PATH_COMPONENT("path/", "comp", "path/comp");
  437. TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
  438. TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
  439. TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
  440. return 0;
  441. }
  442. #endif /* TEST */