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.

471 lines
11KB

  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. #if FF_API_D2STR
  121. char *av_d2str(double d)
  122. {
  123. char *str = av_malloc(16);
  124. if (str)
  125. snprintf(str, 16, "%f", d);
  126. return str;
  127. }
  128. #endif
  129. #define WHITESPACES " \n\t\r"
  130. char *av_get_token(const char **buf, const char *term)
  131. {
  132. char *out = av_malloc(strlen(*buf) + 1);
  133. char *ret = out, *end = out;
  134. const char *p = *buf;
  135. if (!out)
  136. return NULL;
  137. p += strspn(p, WHITESPACES);
  138. while (*p && !strspn(p, term)) {
  139. char c = *p++;
  140. if (c == '\\' && *p) {
  141. *out++ = *p++;
  142. end = out;
  143. } else if (c == '\'') {
  144. while (*p && *p != '\'')
  145. *out++ = *p++;
  146. if (*p) {
  147. p++;
  148. end = out;
  149. }
  150. } else {
  151. *out++ = c;
  152. }
  153. }
  154. do
  155. *out-- = 0;
  156. while (out >= end && strspn(out, WHITESPACES));
  157. *buf = p;
  158. return ret;
  159. }
  160. char *av_strtok(char *s, const char *delim, char **saveptr)
  161. {
  162. char *tok;
  163. if (!s && !(s = *saveptr))
  164. return NULL;
  165. /* skip leading delimiters */
  166. s += strspn(s, delim);
  167. /* s now points to the first non delimiter char, or to the end of the string */
  168. if (!*s) {
  169. *saveptr = NULL;
  170. return NULL;
  171. }
  172. tok = s++;
  173. /* skip non delimiters */
  174. s += strcspn(s, delim);
  175. if (*s) {
  176. *s = 0;
  177. *saveptr = s+1;
  178. } else {
  179. *saveptr = NULL;
  180. }
  181. return tok;
  182. }
  183. int av_strcasecmp(const char *a, const char *b)
  184. {
  185. uint8_t c1, c2;
  186. do {
  187. c1 = av_tolower(*a++);
  188. c2 = av_tolower(*b++);
  189. } while (c1 && c1 == c2);
  190. return c1 - c2;
  191. }
  192. int av_strncasecmp(const char *a, const char *b, size_t n)
  193. {
  194. uint8_t c1, c2;
  195. if (n <= 0)
  196. return 0;
  197. do {
  198. c1 = av_tolower(*a++);
  199. c2 = av_tolower(*b++);
  200. } while (--n && c1 && c1 == c2);
  201. return c1 - c2;
  202. }
  203. char *av_strireplace(const char *str, const char *from, const char *to)
  204. {
  205. char *ret = NULL;
  206. const char *pstr2, *pstr = str;
  207. size_t tolen = strlen(to), fromlen = strlen(from);
  208. AVBPrint pbuf;
  209. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  210. while ((pstr2 = av_stristr(pstr, from))) {
  211. av_bprint_append_data(&pbuf, pstr, pstr2 - pstr);
  212. pstr = pstr2 + fromlen;
  213. av_bprint_append_data(&pbuf, to, tolen);
  214. }
  215. av_bprint_append_data(&pbuf, pstr, strlen(pstr));
  216. if (!av_bprint_is_complete(&pbuf)) {
  217. av_bprint_finalize(&pbuf, NULL);
  218. } else {
  219. av_bprint_finalize(&pbuf, &ret);
  220. }
  221. return ret;
  222. }
  223. const char *av_basename(const char *path)
  224. {
  225. char *p;
  226. #if HAVE_DOS_PATHS
  227. char *q, *d;
  228. #endif
  229. if (!path || *path == '\0')
  230. return ".";
  231. p = strrchr(path, '/');
  232. #if HAVE_DOS_PATHS
  233. q = strrchr(path, '\\');
  234. d = strchr(path, ':');
  235. p = FFMAX3(p, q, d);
  236. #endif
  237. if (!p)
  238. return path;
  239. return p + 1;
  240. }
  241. const char *av_dirname(char *path)
  242. {
  243. char *p = path ? strrchr(path, '/') : NULL;
  244. #if HAVE_DOS_PATHS
  245. char *q = path ? strrchr(path, '\\') : NULL;
  246. char *d = path ? strchr(path, ':') : NULL;
  247. d = d ? d + 1 : d;
  248. p = FFMAX3(p, q, d);
  249. #endif
  250. if (!p)
  251. return ".";
  252. *p = '\0';
  253. return path;
  254. }
  255. char *av_append_path_component(const char *path, const char *component)
  256. {
  257. size_t p_len, c_len;
  258. char *fullpath;
  259. if (!path)
  260. return av_strdup(component);
  261. if (!component)
  262. return av_strdup(path);
  263. p_len = strlen(path);
  264. c_len = strlen(component);
  265. if (p_len > SIZE_MAX - c_len || p_len + c_len > SIZE_MAX - 2)
  266. return NULL;
  267. fullpath = av_malloc(p_len + c_len + 2);
  268. if (fullpath) {
  269. if (p_len) {
  270. av_strlcpy(fullpath, path, p_len + 1);
  271. if (c_len) {
  272. if (fullpath[p_len - 1] != '/' && component[0] != '/')
  273. fullpath[p_len++] = '/';
  274. else if (fullpath[p_len - 1] == '/' && component[0] == '/')
  275. p_len--;
  276. }
  277. }
  278. av_strlcpy(&fullpath[p_len], component, c_len + 1);
  279. fullpath[p_len + c_len] = 0;
  280. }
  281. return fullpath;
  282. }
  283. int av_escape(char **dst, const char *src, const char *special_chars,
  284. enum AVEscapeMode mode, int flags)
  285. {
  286. AVBPrint dstbuf;
  287. int ret;
  288. av_bprint_init(&dstbuf, 1, INT_MAX); /* (int)dstbuf.len must be >= 0 */
  289. av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
  290. if (!av_bprint_is_complete(&dstbuf)) {
  291. av_bprint_finalize(&dstbuf, NULL);
  292. return AVERROR(ENOMEM);
  293. }
  294. if ((ret = av_bprint_finalize(&dstbuf, dst)) < 0)
  295. return ret;
  296. return dstbuf.len;
  297. }
  298. int av_match_name(const char *name, const char *names)
  299. {
  300. const char *p;
  301. int len, namelen;
  302. if (!name || !names)
  303. return 0;
  304. namelen = strlen(name);
  305. while (*names) {
  306. int negate = '-' == *names;
  307. p = strchr(names, ',');
  308. if (!p)
  309. p = names + strlen(names);
  310. names += negate;
  311. len = FFMAX(p - names, namelen);
  312. if (!av_strncasecmp(name, names, len) || !strncmp("ALL", names, FFMAX(3, p - names)))
  313. return !negate;
  314. names = p + (*p == ',');
  315. }
  316. return 0;
  317. }
  318. int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
  319. unsigned int flags)
  320. {
  321. const uint8_t *p = *bufp;
  322. uint32_t top;
  323. uint64_t code;
  324. int ret = 0, tail_len;
  325. uint32_t overlong_encoding_mins[6] = {
  326. 0x00000000, 0x00000080, 0x00000800, 0x00010000, 0x00200000, 0x04000000,
  327. };
  328. if (p >= buf_end)
  329. return 0;
  330. code = *p++;
  331. /* first sequence byte starts with 10, or is 1111-1110 or 1111-1111,
  332. which is not admitted */
  333. if ((code & 0xc0) == 0x80 || code >= 0xFE) {
  334. ret = AVERROR(EILSEQ);
  335. goto end;
  336. }
  337. top = (code & 128) >> 1;
  338. tail_len = 0;
  339. while (code & top) {
  340. int tmp;
  341. tail_len++;
  342. if (p >= buf_end) {
  343. (*bufp) ++;
  344. return AVERROR(EILSEQ); /* incomplete sequence */
  345. }
  346. /* we assume the byte to be in the form 10xx-xxxx */
  347. tmp = *p++ - 128; /* strip leading 1 */
  348. if (tmp>>6) {
  349. (*bufp) ++;
  350. return AVERROR(EILSEQ);
  351. }
  352. code = (code<<6) + tmp;
  353. top <<= 5;
  354. }
  355. code &= (top << 1) - 1;
  356. /* check for overlong encodings */
  357. av_assert0(tail_len <= 5);
  358. if (code < overlong_encoding_mins[tail_len]) {
  359. ret = AVERROR(EILSEQ);
  360. goto end;
  361. }
  362. if (code >= 1U<<31) {
  363. ret = AVERROR(EILSEQ); /* out-of-range value */
  364. goto end;
  365. }
  366. *codep = code;
  367. if (code > 0x10FFFF &&
  368. !(flags & AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES))
  369. ret = AVERROR(EILSEQ);
  370. if (code < 0x20 && code != 0x9 && code != 0xA && code != 0xD &&
  371. flags & AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES)
  372. ret = AVERROR(EILSEQ);
  373. if (code >= 0xD800 && code <= 0xDFFF &&
  374. !(flags & AV_UTF8_FLAG_ACCEPT_SURROGATES))
  375. ret = AVERROR(EILSEQ);
  376. if ((code == 0xFFFE || code == 0xFFFF) &&
  377. !(flags & AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS))
  378. ret = AVERROR(EILSEQ);
  379. end:
  380. *bufp = p;
  381. return ret;
  382. }
  383. int av_match_list(const char *name, const char *list, char separator)
  384. {
  385. const char *p, *q;
  386. for (p = name; p && *p; ) {
  387. for (q = list; q && *q; ) {
  388. int k;
  389. for (k = 0; p[k] == q[k] || (p[k]*q[k] == 0 && p[k]+q[k] == separator); k++)
  390. if (k && (!p[k] || p[k] == separator))
  391. return 1;
  392. q = strchr(q, separator);
  393. q += !!q;
  394. }
  395. p = strchr(p, separator);
  396. p += !!p;
  397. }
  398. return 0;
  399. }