avstring.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "avstring.h"
  29. #include "bprint.h"
  30. int av_strstart(const char *str, const char *pfx, const char **ptr)
  31. {
  32. while (*pfx && *pfx == *str) {
  33. pfx++;
  34. str++;
  35. }
  36. if (!*pfx && ptr)
  37. *ptr = str;
  38. return !*pfx;
  39. }
  40. int av_stristart(const char *str, const char *pfx, const char **ptr)
  41. {
  42. while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
  43. pfx++;
  44. str++;
  45. }
  46. if (!*pfx && ptr)
  47. *ptr = str;
  48. return !*pfx;
  49. }
  50. char *av_stristr(const char *s1, const char *s2)
  51. {
  52. if (!*s2)
  53. return (char*)(intptr_t)s1;
  54. do
  55. if (av_stristart(s1, s2, NULL))
  56. return (char*)(intptr_t)s1;
  57. while (*s1++);
  58. return NULL;
  59. }
  60. char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
  61. {
  62. size_t needle_len = strlen(needle);
  63. if (!needle_len)
  64. return (char*)haystack;
  65. while (hay_length >= needle_len) {
  66. hay_length--;
  67. if (!memcmp(haystack, needle, needle_len))
  68. return (char*)haystack;
  69. haystack++;
  70. }
  71. return NULL;
  72. }
  73. size_t av_strlcpy(char *dst, const char *src, size_t size)
  74. {
  75. size_t len = 0;
  76. while (++len < size && *src)
  77. *dst++ = *src++;
  78. if (len <= size)
  79. *dst = 0;
  80. return len + strlen(src) - 1;
  81. }
  82. size_t av_strlcat(char *dst, const char *src, size_t size)
  83. {
  84. size_t len = strlen(dst);
  85. if (size <= len + 1)
  86. return len + strlen(src);
  87. return len + av_strlcpy(dst + len, src, size - len);
  88. }
  89. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
  90. {
  91. int len = strlen(dst);
  92. va_list vl;
  93. va_start(vl, fmt);
  94. len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
  95. va_end(vl);
  96. return len;
  97. }
  98. char *av_asprintf(const char *fmt, ...)
  99. {
  100. char *p = NULL;
  101. va_list va;
  102. int len;
  103. va_start(va, fmt);
  104. len = vsnprintf(NULL, 0, fmt, va);
  105. va_end(va);
  106. if (len < 0)
  107. goto end;
  108. p = av_malloc(len + 1);
  109. if (!p)
  110. goto end;
  111. va_start(va, fmt);
  112. len = vsnprintf(p, len + 1, fmt, va);
  113. va_end(va);
  114. if (len < 0)
  115. av_freep(&p);
  116. end:
  117. return p;
  118. }
  119. char *av_d2str(double d)
  120. {
  121. char *str = av_malloc(16);
  122. if (str)
  123. snprintf(str, 16, "%f", d);
  124. return str;
  125. }
  126. #define WHITESPACES " \n\t"
  127. char *av_get_token(const char **buf, const char *term)
  128. {
  129. char *out = av_malloc(strlen(*buf) + 1);
  130. char *ret = out, *end = out;
  131. const char *p = *buf;
  132. if (!out)
  133. return NULL;
  134. p += strspn(p, WHITESPACES);
  135. while (*p && !strspn(p, term)) {
  136. char c = *p++;
  137. if (c == '\\' && *p) {
  138. *out++ = *p++;
  139. end = out;
  140. } else if (c == '\'') {
  141. while (*p && *p != '\'')
  142. *out++ = *p++;
  143. if (*p) {
  144. p++;
  145. end = out;
  146. }
  147. } else {
  148. *out++ = c;
  149. }
  150. }
  151. do
  152. *out-- = 0;
  153. while (out >= end && strspn(out, WHITESPACES));
  154. *buf = p;
  155. return ret;
  156. }
  157. char *av_strtok(char *s, const char *delim, char **saveptr)
  158. {
  159. char *tok;
  160. if (!s && !(s = *saveptr))
  161. return NULL;
  162. /* skip leading delimiters */
  163. s += strspn(s, delim);
  164. /* s now points to the first non delimiter char, or to the end of the string */
  165. if (!*s) {
  166. *saveptr = NULL;
  167. return NULL;
  168. }
  169. tok = s++;
  170. /* skip non delimiters */
  171. s += strcspn(s, delim);
  172. if (*s) {
  173. *s = 0;
  174. *saveptr = s+1;
  175. } else {
  176. *saveptr = NULL;
  177. }
  178. return tok;
  179. }
  180. int av_strcasecmp(const char *a, const char *b)
  181. {
  182. uint8_t c1, c2;
  183. do {
  184. c1 = av_tolower(*a++);
  185. c2 = av_tolower(*b++);
  186. } while (c1 && c1 == c2);
  187. return c1 - c2;
  188. }
  189. int av_strncasecmp(const char *a, const char *b, size_t n)
  190. {
  191. const char *end = a + n;
  192. uint8_t c1, c2;
  193. do {
  194. c1 = av_tolower(*a++);
  195. c2 = av_tolower(*b++);
  196. } while (a < end && c1 && c1 == c2);
  197. return c1 - c2;
  198. }
  199. const char *av_basename(const char *path)
  200. {
  201. char *p = strrchr(path, '/');
  202. #if HAVE_DOS_PATHS
  203. char *q = strrchr(path, '\\');
  204. char *d = strchr(path, ':');
  205. p = FFMAX3(p, q, d);
  206. #endif
  207. if (!p)
  208. return path;
  209. return p + 1;
  210. }
  211. const char *av_dirname(char *path)
  212. {
  213. char *p = strrchr(path, '/');
  214. #if HAVE_DOS_PATHS
  215. char *q = strrchr(path, '\\');
  216. char *d = strchr(path, ':');
  217. d = d ? d + 1 : d;
  218. p = FFMAX3(p, q, d);
  219. #endif
  220. if (!p)
  221. return ".";
  222. *p = '\0';
  223. return path;
  224. }
  225. int av_escape(char **dst, const char *src, const char *special_chars,
  226. enum AVEscapeMode mode, int flags)
  227. {
  228. AVBPrint dstbuf;
  229. av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  230. av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
  231. if (!av_bprint_is_complete(&dstbuf)) {
  232. av_bprint_finalize(&dstbuf, NULL);
  233. return AVERROR(ENOMEM);
  234. } else {
  235. av_bprint_finalize(&dstbuf, dst);
  236. return dstbuf.len;
  237. }
  238. }
  239. int av_isdigit(int c)
  240. {
  241. return c >= '0' && c <= '9';
  242. }
  243. int av_isgraph(int c)
  244. {
  245. return c > 32 && c < 127;
  246. }
  247. int av_isspace(int c)
  248. {
  249. return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
  250. c == '\v';
  251. }
  252. int av_isxdigit(int c)
  253. {
  254. c = av_tolower(c);
  255. return av_isdigit(c) || (c >= 'a' && c <= 'f');
  256. }
  257. int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
  258. unsigned int flags)
  259. {
  260. const uint8_t *p = *bufp;
  261. uint32_t top;
  262. uint64_t code;
  263. int ret = 0;
  264. if (p >= buf_end)
  265. return 0;
  266. code = *p++;
  267. /* first sequence byte starts with 10, or is 1111-1110 or 1111-1111,
  268. which is not admitted */
  269. if ((code & 0xc0) == 0x80 || code >= 0xFE) {
  270. ret = AVERROR(EILSEQ);
  271. goto end;
  272. }
  273. top = (code & 128) >> 1;
  274. while (code & top) {
  275. int tmp;
  276. if (p >= buf_end) {
  277. (*bufp) ++;
  278. return AVERROR(EILSEQ); /* incomplete sequence */
  279. }
  280. /* we assume the byte to be in the form 10xx-xxxx */
  281. tmp = *p++ - 128; /* strip leading 1 */
  282. if (tmp>>6) {
  283. (*bufp) ++;
  284. return AVERROR(EILSEQ);
  285. }
  286. code = (code<<6) + tmp;
  287. top <<= 5;
  288. }
  289. code &= (top << 1) - 1;
  290. if (code >= 1<<31) {
  291. ret = AVERROR(EILSEQ); /* out-of-range value */
  292. goto end;
  293. }
  294. *codep = code;
  295. if (code > 0x10FFFF &&
  296. !(flags & AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES))
  297. ret = AVERROR(EILSEQ);
  298. if (code < 0x20 && code != 0x9 && code != 0xA && code != 0xD &&
  299. flags & AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES)
  300. ret = AVERROR(EILSEQ);
  301. if (code >= 0xD800 && code <= 0xDFFF &&
  302. !(flags & AV_UTF8_FLAG_ACCEPT_SURROGATES))
  303. ret = AVERROR(EILSEQ);
  304. if ((code == 0xFFFE || code == 0xFFFF) &&
  305. !(flags & AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS))
  306. ret = AVERROR(EILSEQ);
  307. end:
  308. *bufp = p;
  309. return ret;
  310. }
  311. #ifdef TEST
  312. int main(void)
  313. {
  314. int i;
  315. static const char * const strings[] = {
  316. "''",
  317. "",
  318. ":",
  319. "\\",
  320. "'",
  321. " '' :",
  322. " '' '' :",
  323. "foo '' :",
  324. "'foo'",
  325. "foo ",
  326. " ' foo ' ",
  327. "foo\\",
  328. "foo': blah:blah",
  329. "foo\\: blah:blah",
  330. "foo\'",
  331. "'foo : ' :blahblah",
  332. "\\ :blah",
  333. " foo",
  334. " foo ",
  335. " foo \\ ",
  336. "foo ':blah",
  337. " foo bar : blahblah",
  338. "\\f\\o\\o",
  339. "'foo : \\ \\ ' : blahblah",
  340. "'\\fo\\o:': blahblah",
  341. "\\'fo\\o\\:': foo ' :blahblah"
  342. };
  343. printf("Testing av_get_token()\n");
  344. for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
  345. const char *p = strings[i];
  346. char *q;
  347. printf("|%s|", p);
  348. q = av_get_token(&p, ":");
  349. printf(" -> |%s|", q);
  350. printf(" + |%s|\n", p);
  351. av_free(q);
  352. }
  353. return 0;
  354. }
  355. #endif /* TEST */