avstring.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. const char *end = a + n;
  193. uint8_t c1, c2;
  194. do {
  195. c1 = av_tolower(*a++);
  196. c2 = av_tolower(*b++);
  197. } while (a < end && c1 && c1 == c2);
  198. return c1 - c2;
  199. }
  200. const char *av_basename(const char *path)
  201. {
  202. char *p = strrchr(path, '/');
  203. #if HAVE_DOS_PATHS
  204. char *q = strrchr(path, '\\');
  205. char *d = strchr(path, ':');
  206. p = FFMAX3(p, q, d);
  207. #endif
  208. if (!p)
  209. return path;
  210. return p + 1;
  211. }
  212. const char *av_dirname(char *path)
  213. {
  214. char *p = strrchr(path, '/');
  215. #if HAVE_DOS_PATHS
  216. char *q = strrchr(path, '\\');
  217. char *d = strchr(path, ':');
  218. d = d ? d + 1 : d;
  219. p = FFMAX3(p, q, d);
  220. #endif
  221. if (!p)
  222. return ".";
  223. *p = '\0';
  224. return path;
  225. }
  226. int av_escape(char **dst, const char *src, const char *special_chars,
  227. enum AVEscapeMode mode, int flags)
  228. {
  229. AVBPrint dstbuf;
  230. av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  231. av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
  232. if (!av_bprint_is_complete(&dstbuf)) {
  233. av_bprint_finalize(&dstbuf, NULL);
  234. return AVERROR(ENOMEM);
  235. } else {
  236. av_bprint_finalize(&dstbuf, dst);
  237. return dstbuf.len;
  238. }
  239. }
  240. int av_isdigit(int c)
  241. {
  242. return c >= '0' && c <= '9';
  243. }
  244. int av_isgraph(int c)
  245. {
  246. return c > 32 && c < 127;
  247. }
  248. int av_isspace(int c)
  249. {
  250. return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
  251. c == '\v';
  252. }
  253. int av_isxdigit(int c)
  254. {
  255. c = av_tolower(c);
  256. return av_isdigit(c) || (c >= 'a' && c <= 'f');
  257. }
  258. int av_match_name(const char *name, const char *names)
  259. {
  260. const char *p;
  261. int len, namelen;
  262. if (!name || !names)
  263. return 0;
  264. namelen = strlen(name);
  265. while ((p = strchr(names, ','))) {
  266. len = FFMAX(p - names, namelen);
  267. if (!av_strncasecmp(name, names, len))
  268. return 1;
  269. names = p + 1;
  270. }
  271. return !av_strcasecmp(name, names);
  272. }
  273. int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
  274. unsigned int flags)
  275. {
  276. const uint8_t *p = *bufp;
  277. uint32_t top;
  278. uint64_t code;
  279. int ret = 0, tail_len;
  280. uint32_t overlong_encoding_mins[6] = {
  281. 0x00000000, 0x00000080, 0x00000800, 0x00010000, 0x00200000, 0x04000000,
  282. };
  283. if (p >= buf_end)
  284. return 0;
  285. code = *p++;
  286. /* first sequence byte starts with 10, or is 1111-1110 or 1111-1111,
  287. which is not admitted */
  288. if ((code & 0xc0) == 0x80 || code >= 0xFE) {
  289. ret = AVERROR(EILSEQ);
  290. goto end;
  291. }
  292. top = (code & 128) >> 1;
  293. tail_len = 0;
  294. while (code & top) {
  295. int tmp;
  296. tail_len++;
  297. if (p >= buf_end) {
  298. (*bufp) ++;
  299. return AVERROR(EILSEQ); /* incomplete sequence */
  300. }
  301. /* we assume the byte to be in the form 10xx-xxxx */
  302. tmp = *p++ - 128; /* strip leading 1 */
  303. if (tmp>>6) {
  304. (*bufp) ++;
  305. return AVERROR(EILSEQ);
  306. }
  307. code = (code<<6) + tmp;
  308. top <<= 5;
  309. }
  310. code &= (top << 1) - 1;
  311. /* check for overlong encodings */
  312. av_assert0(tail_len <= 5);
  313. if (code < overlong_encoding_mins[tail_len]) {
  314. ret = AVERROR(EILSEQ);
  315. goto end;
  316. }
  317. if (code >= 1<<31) {
  318. ret = AVERROR(EILSEQ); /* out-of-range value */
  319. goto end;
  320. }
  321. *codep = code;
  322. if (code > 0x10FFFF &&
  323. !(flags & AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES))
  324. ret = AVERROR(EILSEQ);
  325. if (code < 0x20 && code != 0x9 && code != 0xA && code != 0xD &&
  326. flags & AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES)
  327. ret = AVERROR(EILSEQ);
  328. if (code >= 0xD800 && code <= 0xDFFF &&
  329. !(flags & AV_UTF8_FLAG_ACCEPT_SURROGATES))
  330. ret = AVERROR(EILSEQ);
  331. if ((code == 0xFFFE || code == 0xFFFF) &&
  332. !(flags & AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS))
  333. ret = AVERROR(EILSEQ);
  334. end:
  335. *bufp = p;
  336. return ret;
  337. }
  338. #ifdef TEST
  339. int main(void)
  340. {
  341. int i;
  342. static const char * const strings[] = {
  343. "''",
  344. "",
  345. ":",
  346. "\\",
  347. "'",
  348. " '' :",
  349. " '' '' :",
  350. "foo '' :",
  351. "'foo'",
  352. "foo ",
  353. " ' foo ' ",
  354. "foo\\",
  355. "foo': blah:blah",
  356. "foo\\: blah:blah",
  357. "foo\'",
  358. "'foo : ' :blahblah",
  359. "\\ :blah",
  360. " foo",
  361. " foo ",
  362. " foo \\ ",
  363. "foo ':blah",
  364. " foo bar : blahblah",
  365. "\\f\\o\\o",
  366. "'foo : \\ \\ ' : blahblah",
  367. "'\\fo\\o:': blahblah",
  368. "\\'fo\\o\\:': foo ' :blahblah"
  369. };
  370. printf("Testing av_get_token()\n");
  371. for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
  372. const char *p = strings[i];
  373. char *q;
  374. printf("|%s|", p);
  375. q = av_get_token(&p, ":");
  376. printf(" -> |%s|", q);
  377. printf(" + |%s|\n", p);
  378. av_free(q);
  379. }
  380. return 0;
  381. }
  382. #endif /* TEST */