xmlstring.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * string.c : an XML string utilities module
  3. *
  4. * This module provides various utility functions for manipulating
  5. * the xmlChar* type. All functions named xmlStr* have been moved here
  6. * from the parser.c file (their original home).
  7. *
  8. * See Copyright for the status of this software.
  9. *
  10. * UTF8 string routines from:
  11. * William Brack <wbrack@mmm.com.hk>
  12. *
  13. * daniel@veillard.com
  14. */
  15. #define IN_LIBXML
  16. #include "libxml.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <limits.h>
  20. #include <libxml/xmlmemory.h>
  21. #include <libxml/parserInternals.h>
  22. #include <libxml/xmlstring.h>
  23. /************************************************************************
  24. * *
  25. * Commodity functions to handle xmlChars *
  26. * *
  27. ************************************************************************/
  28. /**
  29. * xmlStrndup:
  30. * @cur: the input xmlChar *
  31. * @len: the len of @cur
  32. *
  33. * a strndup for array of xmlChar's
  34. *
  35. * Returns a new xmlChar * or NULL
  36. */
  37. xmlChar *
  38. xmlStrndup(const xmlChar *cur, int len) {
  39. xmlChar *ret;
  40. if ((cur == NULL) || (len < 0)) return(NULL);
  41. ret = (xmlChar *) xmlMallocAtomic(((size_t) len + 1) * sizeof(xmlChar));
  42. if (ret == NULL) {
  43. xmlErrMemory(NULL, NULL);
  44. return(NULL);
  45. }
  46. memcpy(ret, cur, len * sizeof(xmlChar));
  47. ret[len] = 0;
  48. return(ret);
  49. }
  50. /**
  51. * xmlStrdup:
  52. * @cur: the input xmlChar *
  53. *
  54. * a strdup for array of xmlChar's. Since they are supposed to be
  55. * encoded in UTF-8 or an encoding with 8bit based chars, we assume
  56. * a termination mark of '0'.
  57. *
  58. * Returns a new xmlChar * or NULL
  59. */
  60. xmlChar *
  61. xmlStrdup(const xmlChar *cur) {
  62. const xmlChar *p = cur;
  63. if (cur == NULL) return(NULL);
  64. while (*p != 0) p++; /* non input consuming */
  65. return(xmlStrndup(cur, p - cur));
  66. }
  67. /**
  68. * xmlCharStrndup:
  69. * @cur: the input char *
  70. * @len: the len of @cur
  71. *
  72. * a strndup for char's to xmlChar's
  73. *
  74. * Returns a new xmlChar * or NULL
  75. */
  76. xmlChar *
  77. xmlCharStrndup(const char *cur, int len) {
  78. int i;
  79. xmlChar *ret;
  80. if ((cur == NULL) || (len < 0)) return(NULL);
  81. ret = (xmlChar *) xmlMallocAtomic(((size_t) len + 1) * sizeof(xmlChar));
  82. if (ret == NULL) {
  83. xmlErrMemory(NULL, NULL);
  84. return(NULL);
  85. }
  86. for (i = 0;i < len;i++) {
  87. ret[i] = (xmlChar) cur[i];
  88. if (ret[i] == 0) return(ret);
  89. }
  90. ret[len] = 0;
  91. return(ret);
  92. }
  93. /**
  94. * xmlCharStrdup:
  95. * @cur: the input char *
  96. *
  97. * a strdup for char's to xmlChar's
  98. *
  99. * Returns a new xmlChar * or NULL
  100. */
  101. xmlChar *
  102. xmlCharStrdup(const char *cur) {
  103. const char *p = cur;
  104. if (cur == NULL) return(NULL);
  105. while (*p != '\0') p++; /* non input consuming */
  106. return(xmlCharStrndup(cur, p - cur));
  107. }
  108. /**
  109. * xmlStrcmp:
  110. * @str1: the first xmlChar *
  111. * @str2: the second xmlChar *
  112. *
  113. * a strcmp for xmlChar's
  114. *
  115. * Returns the integer result of the comparison
  116. */
  117. int
  118. xmlStrcmp(const xmlChar *str1, const xmlChar *str2) {
  119. if (str1 == str2) return(0);
  120. if (str1 == NULL) return(-1);
  121. if (str2 == NULL) return(1);
  122. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  123. return(strcmp((const char *)str1, (const char *)str2));
  124. #else
  125. do {
  126. int tmp = *str1++ - *str2;
  127. if (tmp != 0) return(tmp);
  128. } while (*str2++ != 0);
  129. return 0;
  130. #endif
  131. }
  132. /**
  133. * xmlStrEqual:
  134. * @str1: the first xmlChar *
  135. * @str2: the second xmlChar *
  136. *
  137. * Check if both strings are equal of have same content.
  138. * Should be a bit more readable and faster than xmlStrcmp()
  139. *
  140. * Returns 1 if they are equal, 0 if they are different
  141. */
  142. int
  143. xmlStrEqual(const xmlChar *str1, const xmlChar *str2) {
  144. if (str1 == str2) return(1);
  145. if (str1 == NULL) return(0);
  146. if (str2 == NULL) return(0);
  147. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  148. return(strcmp((const char *)str1, (const char *)str2) == 0);
  149. #else
  150. do {
  151. if (*str1++ != *str2) return(0);
  152. } while (*str2++);
  153. return(1);
  154. #endif
  155. }
  156. /**
  157. * xmlStrQEqual:
  158. * @pref: the prefix of the QName
  159. * @name: the localname of the QName
  160. * @str: the second xmlChar *
  161. *
  162. * Check if a QName is Equal to a given string
  163. *
  164. * Returns 1 if they are equal, 0 if they are different
  165. */
  166. int
  167. xmlStrQEqual(const xmlChar *pref, const xmlChar *name, const xmlChar *str) {
  168. if (pref == NULL) return(xmlStrEqual(name, str));
  169. if (name == NULL) return(0);
  170. if (str == NULL) return(0);
  171. do {
  172. if (*pref++ != *str) return(0);
  173. } while ((*str++) && (*pref));
  174. if (*str++ != ':') return(0);
  175. do {
  176. if (*name++ != *str) return(0);
  177. } while (*str++);
  178. return(1);
  179. }
  180. /**
  181. * xmlStrncmp:
  182. * @str1: the first xmlChar *
  183. * @str2: the second xmlChar *
  184. * @len: the max comparison length
  185. *
  186. * a strncmp for xmlChar's
  187. *
  188. * Returns the integer result of the comparison
  189. */
  190. int
  191. xmlStrncmp(const xmlChar *str1, const xmlChar *str2, int len) {
  192. if (len <= 0) return(0);
  193. if (str1 == str2) return(0);
  194. if (str1 == NULL) return(-1);
  195. if (str2 == NULL) return(1);
  196. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  197. return(strncmp((const char *)str1, (const char *)str2, len));
  198. #else
  199. do {
  200. int tmp = *str1++ - *str2;
  201. if (tmp != 0 || --len == 0) return(tmp);
  202. } while (*str2++ != 0);
  203. return 0;
  204. #endif
  205. }
  206. static const xmlChar casemap[256] = {
  207. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  208. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
  209. 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
  210. 0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
  211. 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
  212. 0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
  213. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
  214. 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
  215. 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
  216. 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
  217. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
  218. 0x78,0x79,0x7A,0x7B,0x5C,0x5D,0x5E,0x5F,
  219. 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
  220. 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
  221. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
  222. 0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
  223. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
  224. 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
  225. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
  226. 0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
  227. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
  228. 0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
  229. 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
  230. 0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
  231. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,
  232. 0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
  233. 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,
  234. 0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
  235. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,
  236. 0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
  237. 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
  238. 0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
  239. };
  240. /**
  241. * xmlStrcasecmp:
  242. * @str1: the first xmlChar *
  243. * @str2: the second xmlChar *
  244. *
  245. * a strcasecmp for xmlChar's
  246. *
  247. * Returns the integer result of the comparison
  248. */
  249. int
  250. xmlStrcasecmp(const xmlChar *str1, const xmlChar *str2) {
  251. register int tmp;
  252. if (str1 == str2) return(0);
  253. if (str1 == NULL) return(-1);
  254. if (str2 == NULL) return(1);
  255. do {
  256. tmp = casemap[*str1++] - casemap[*str2];
  257. if (tmp != 0) return(tmp);
  258. } while (*str2++ != 0);
  259. return 0;
  260. }
  261. /**
  262. * xmlStrncasecmp:
  263. * @str1: the first xmlChar *
  264. * @str2: the second xmlChar *
  265. * @len: the max comparison length
  266. *
  267. * a strncasecmp for xmlChar's
  268. *
  269. * Returns the integer result of the comparison
  270. */
  271. int
  272. xmlStrncasecmp(const xmlChar *str1, const xmlChar *str2, int len) {
  273. register int tmp;
  274. if (len <= 0) return(0);
  275. if (str1 == str2) return(0);
  276. if (str1 == NULL) return(-1);
  277. if (str2 == NULL) return(1);
  278. do {
  279. tmp = casemap[*str1++] - casemap[*str2];
  280. if (tmp != 0 || --len == 0) return(tmp);
  281. } while (*str2++ != 0);
  282. return 0;
  283. }
  284. /**
  285. * xmlStrchr:
  286. * @str: the xmlChar * array
  287. * @val: the xmlChar to search
  288. *
  289. * a strchr for xmlChar's
  290. *
  291. * Returns the xmlChar * for the first occurrence or NULL.
  292. */
  293. const xmlChar *
  294. xmlStrchr(const xmlChar *str, xmlChar val) {
  295. if (str == NULL) return(NULL);
  296. while (*str != 0) { /* non input consuming */
  297. if (*str == val) return((xmlChar *) str);
  298. str++;
  299. }
  300. return(NULL);
  301. }
  302. /**
  303. * xmlStrstr:
  304. * @str: the xmlChar * array (haystack)
  305. * @val: the xmlChar to search (needle)
  306. *
  307. * a strstr for xmlChar's
  308. *
  309. * Returns the xmlChar * for the first occurrence or NULL.
  310. */
  311. const xmlChar *
  312. xmlStrstr(const xmlChar *str, const xmlChar *val) {
  313. int n;
  314. if (str == NULL) return(NULL);
  315. if (val == NULL) return(NULL);
  316. n = xmlStrlen(val);
  317. if (n == 0) return(str);
  318. while (*str != 0) { /* non input consuming */
  319. if (*str == *val) {
  320. if (!xmlStrncmp(str, val, n)) return((const xmlChar *) str);
  321. }
  322. str++;
  323. }
  324. return(NULL);
  325. }
  326. /**
  327. * xmlStrcasestr:
  328. * @str: the xmlChar * array (haystack)
  329. * @val: the xmlChar to search (needle)
  330. *
  331. * a case-ignoring strstr for xmlChar's
  332. *
  333. * Returns the xmlChar * for the first occurrence or NULL.
  334. */
  335. const xmlChar *
  336. xmlStrcasestr(const xmlChar *str, const xmlChar *val) {
  337. int n;
  338. if (str == NULL) return(NULL);
  339. if (val == NULL) return(NULL);
  340. n = xmlStrlen(val);
  341. if (n == 0) return(str);
  342. while (*str != 0) { /* non input consuming */
  343. if (casemap[*str] == casemap[*val])
  344. if (!xmlStrncasecmp(str, val, n)) return(str);
  345. str++;
  346. }
  347. return(NULL);
  348. }
  349. /**
  350. * xmlStrsub:
  351. * @str: the xmlChar * array (haystack)
  352. * @start: the index of the first char (zero based)
  353. * @len: the length of the substring
  354. *
  355. * Extract a substring of a given string
  356. *
  357. * Returns the xmlChar * for the first occurrence or NULL.
  358. */
  359. xmlChar *
  360. xmlStrsub(const xmlChar *str, int start, int len) {
  361. int i;
  362. if (str == NULL) return(NULL);
  363. if (start < 0) return(NULL);
  364. if (len < 0) return(NULL);
  365. for (i = 0;i < start;i++) {
  366. if (*str == 0) return(NULL);
  367. str++;
  368. }
  369. if (*str == 0) return(NULL);
  370. return(xmlStrndup(str, len));
  371. }
  372. /**
  373. * xmlStrlen:
  374. * @str: the xmlChar * array
  375. *
  376. * length of a xmlChar's string
  377. *
  378. * Returns the number of xmlChar contained in the ARRAY.
  379. */
  380. int
  381. xmlStrlen(const xmlChar *str) {
  382. size_t len = 0;
  383. if (str == NULL) return(0);
  384. while (*str != 0) { /* non input consuming */
  385. str++;
  386. len++;
  387. }
  388. return(len > INT_MAX ? 0 : len);
  389. }
  390. /**
  391. * xmlStrncat:
  392. * @cur: the original xmlChar * array
  393. * @add: the xmlChar * array added
  394. * @len: the length of @add
  395. *
  396. * a strncat for array of xmlChar's, it will extend @cur with the len
  397. * first bytes of @add. Note that if @len < 0 then this is an API error
  398. * and NULL will be returned.
  399. *
  400. * Returns a new xmlChar *, the original @cur is reallocated and should
  401. * not be freed.
  402. */
  403. xmlChar *
  404. xmlStrncat(xmlChar *cur, const xmlChar *add, int len) {
  405. int size;
  406. xmlChar *ret;
  407. if ((add == NULL) || (len == 0))
  408. return(cur);
  409. if (len < 0)
  410. return(NULL);
  411. if (cur == NULL)
  412. return(xmlStrndup(add, len));
  413. size = xmlStrlen(cur);
  414. if ((size < 0) || (size > INT_MAX - len))
  415. return(NULL);
  416. ret = (xmlChar *) xmlRealloc(cur, ((size_t) size + len + 1) * sizeof(xmlChar));
  417. if (ret == NULL) {
  418. xmlErrMemory(NULL, NULL);
  419. return(cur);
  420. }
  421. memcpy(&ret[size], add, len * sizeof(xmlChar));
  422. ret[size + len] = 0;
  423. return(ret);
  424. }
  425. /**
  426. * xmlStrncatNew:
  427. * @str1: first xmlChar string
  428. * @str2: second xmlChar string
  429. * @len: the len of @str2 or < 0
  430. *
  431. * same as xmlStrncat, but creates a new string. The original
  432. * two strings are not freed. If @len is < 0 then the length
  433. * will be calculated automatically.
  434. *
  435. * Returns a new xmlChar * or NULL
  436. */
  437. xmlChar *
  438. xmlStrncatNew(const xmlChar *str1, const xmlChar *str2, int len) {
  439. int size;
  440. xmlChar *ret;
  441. if (len < 0) {
  442. len = xmlStrlen(str2);
  443. if (len < 0)
  444. return(NULL);
  445. }
  446. if ((str2 == NULL) || (len == 0))
  447. return(xmlStrdup(str1));
  448. if (str1 == NULL)
  449. return(xmlStrndup(str2, len));
  450. size = xmlStrlen(str1);
  451. if ((size < 0) || (size > INT_MAX - len))
  452. return(NULL);
  453. ret = (xmlChar *) xmlMalloc(((size_t) size + len + 1) * sizeof(xmlChar));
  454. if (ret == NULL) {
  455. xmlErrMemory(NULL, NULL);
  456. return(xmlStrndup(str1, size));
  457. }
  458. memcpy(ret, str1, size * sizeof(xmlChar));
  459. memcpy(&ret[size], str2, len * sizeof(xmlChar));
  460. ret[size + len] = 0;
  461. return(ret);
  462. }
  463. /**
  464. * xmlStrcat:
  465. * @cur: the original xmlChar * array
  466. * @add: the xmlChar * array added
  467. *
  468. * a strcat for array of xmlChar's. Since they are supposed to be
  469. * encoded in UTF-8 or an encoding with 8bit based chars, we assume
  470. * a termination mark of '0'.
  471. *
  472. * Returns a new xmlChar * containing the concatenated string. The original
  473. * @cur is reallocated and should not be freed.
  474. */
  475. xmlChar *
  476. xmlStrcat(xmlChar *cur, const xmlChar *add) {
  477. const xmlChar *p = add;
  478. if (add == NULL) return(cur);
  479. if (cur == NULL)
  480. return(xmlStrdup(add));
  481. while (*p != 0) p++; /* non input consuming */
  482. return(xmlStrncat(cur, add, p - add));
  483. }
  484. /**
  485. * xmlStrPrintf:
  486. * @buf: the result buffer.
  487. * @len: the result buffer length.
  488. * @msg: the message with printf formatting.
  489. * @...: extra parameters for the message.
  490. *
  491. * Formats @msg and places result into @buf.
  492. *
  493. * Returns the number of characters written to @buf or -1 if an error occurs.
  494. */
  495. int XMLCDECL
  496. xmlStrPrintf(xmlChar *buf, int len, const char *msg, ...) {
  497. va_list args;
  498. int ret;
  499. if((buf == NULL) || (msg == NULL)) {
  500. return(-1);
  501. }
  502. va_start(args, msg);
  503. ret = vsnprintf((char *) buf, len, (const char *) msg, args);
  504. va_end(args);
  505. buf[len - 1] = 0; /* be safe ! */
  506. return(ret);
  507. }
  508. /**
  509. * xmlStrVPrintf:
  510. * @buf: the result buffer.
  511. * @len: the result buffer length.
  512. * @msg: the message with printf formatting.
  513. * @ap: extra parameters for the message.
  514. *
  515. * Formats @msg and places result into @buf.
  516. *
  517. * Returns the number of characters written to @buf or -1 if an error occurs.
  518. */
  519. int
  520. xmlStrVPrintf(xmlChar *buf, int len, const char *msg, va_list ap) {
  521. int ret;
  522. if((buf == NULL) || (msg == NULL)) {
  523. return(-1);
  524. }
  525. ret = vsnprintf((char *) buf, len, (const char *) msg, ap);
  526. buf[len - 1] = 0; /* be safe ! */
  527. return(ret);
  528. }
  529. /************************************************************************
  530. * *
  531. * Generic UTF8 handling routines *
  532. * *
  533. * From rfc2044: encoding of the Unicode values on UTF-8: *
  534. * *
  535. * UCS-4 range (hex.) UTF-8 octet sequence (binary) *
  536. * 0000 0000-0000 007F 0xxxxxxx *
  537. * 0000 0080-0000 07FF 110xxxxx 10xxxxxx *
  538. * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx *
  539. * *
  540. * I hope we won't use values > 0xFFFF anytime soon ! *
  541. * *
  542. ************************************************************************/
  543. /**
  544. * xmlUTF8Size:
  545. * @utf: pointer to the UTF8 character
  546. *
  547. * calculates the internal size of a UTF8 character
  548. *
  549. * returns the numbers of bytes in the character, -1 on format error
  550. */
  551. int
  552. xmlUTF8Size(const xmlChar *utf) {
  553. xmlChar mask;
  554. int len;
  555. if (utf == NULL)
  556. return -1;
  557. if (*utf < 0x80)
  558. return 1;
  559. /* check valid UTF8 character */
  560. if (!(*utf & 0x40))
  561. return -1;
  562. /* determine number of bytes in char */
  563. len = 2;
  564. for (mask=0x20; mask != 0; mask>>=1) {
  565. if (!(*utf & mask))
  566. return len;
  567. len++;
  568. }
  569. return -1;
  570. }
  571. /**
  572. * xmlUTF8Charcmp:
  573. * @utf1: pointer to first UTF8 char
  574. * @utf2: pointer to second UTF8 char
  575. *
  576. * compares the two UCS4 values
  577. *
  578. * returns result of the compare as with xmlStrncmp
  579. */
  580. int
  581. xmlUTF8Charcmp(const xmlChar *utf1, const xmlChar *utf2) {
  582. if (utf1 == NULL ) {
  583. if (utf2 == NULL)
  584. return 0;
  585. return -1;
  586. }
  587. return xmlStrncmp(utf1, utf2, xmlUTF8Size(utf1));
  588. }
  589. /**
  590. * xmlUTF8Strlen:
  591. * @utf: a sequence of UTF-8 encoded bytes
  592. *
  593. * compute the length of an UTF8 string, it doesn't do a full UTF8
  594. * checking of the content of the string.
  595. *
  596. * Returns the number of characters in the string or -1 in case of error
  597. */
  598. int
  599. xmlUTF8Strlen(const xmlChar *utf) {
  600. size_t ret = 0;
  601. if (utf == NULL)
  602. return(-1);
  603. while (*utf != 0) {
  604. if (utf[0] & 0x80) {
  605. if ((utf[1] & 0xc0) != 0x80)
  606. return(-1);
  607. if ((utf[0] & 0xe0) == 0xe0) {
  608. if ((utf[2] & 0xc0) != 0x80)
  609. return(-1);
  610. if ((utf[0] & 0xf0) == 0xf0) {
  611. if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
  612. return(-1);
  613. utf += 4;
  614. } else {
  615. utf += 3;
  616. }
  617. } else {
  618. utf += 2;
  619. }
  620. } else {
  621. utf++;
  622. }
  623. ret++;
  624. }
  625. return(ret > INT_MAX ? 0 : ret);
  626. }
  627. /**
  628. * xmlGetUTF8Char:
  629. * @utf: a sequence of UTF-8 encoded bytes
  630. * @len: a pointer to the minimum number of bytes present in
  631. * the sequence. This is used to assure the next character
  632. * is completely contained within the sequence.
  633. *
  634. * Read the first UTF8 character from @utf
  635. *
  636. * Returns the char value or -1 in case of error, and sets *len to
  637. * the actual number of bytes consumed (0 in case of error)
  638. */
  639. int
  640. xmlGetUTF8Char(const unsigned char *utf, int *len) {
  641. unsigned int c;
  642. if (utf == NULL)
  643. goto error;
  644. if (len == NULL)
  645. goto error;
  646. if (*len < 1)
  647. goto error;
  648. c = utf[0];
  649. if (c & 0x80) {
  650. if (*len < 2)
  651. goto error;
  652. if ((utf[1] & 0xc0) != 0x80)
  653. goto error;
  654. if ((c & 0xe0) == 0xe0) {
  655. if (*len < 3)
  656. goto error;
  657. if ((utf[2] & 0xc0) != 0x80)
  658. goto error;
  659. if ((c & 0xf0) == 0xf0) {
  660. if (*len < 4)
  661. goto error;
  662. if ((c & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
  663. goto error;
  664. *len = 4;
  665. /* 4-byte code */
  666. c = (utf[0] & 0x7) << 18;
  667. c |= (utf[1] & 0x3f) << 12;
  668. c |= (utf[2] & 0x3f) << 6;
  669. c |= utf[3] & 0x3f;
  670. } else {
  671. /* 3-byte code */
  672. *len = 3;
  673. c = (utf[0] & 0xf) << 12;
  674. c |= (utf[1] & 0x3f) << 6;
  675. c |= utf[2] & 0x3f;
  676. }
  677. } else {
  678. /* 2-byte code */
  679. *len = 2;
  680. c = (utf[0] & 0x1f) << 6;
  681. c |= utf[1] & 0x3f;
  682. }
  683. } else {
  684. /* 1-byte code */
  685. *len = 1;
  686. }
  687. return(c);
  688. error:
  689. if (len != NULL)
  690. *len = 0;
  691. return(-1);
  692. }
  693. /**
  694. * xmlCheckUTF8:
  695. * @utf: Pointer to putative UTF-8 encoded string.
  696. *
  697. * Checks @utf for being valid UTF-8. @utf is assumed to be
  698. * null-terminated. This function is not super-strict, as it will
  699. * allow longer UTF-8 sequences than necessary. Note that Java is
  700. * capable of producing these sequences if provoked. Also note, this
  701. * routine checks for the 4-byte maximum size, but does not check for
  702. * 0x10ffff maximum value.
  703. *
  704. * Return value: true if @utf is valid.
  705. **/
  706. int
  707. xmlCheckUTF8(const unsigned char *utf)
  708. {
  709. int ix;
  710. unsigned char c;
  711. if (utf == NULL)
  712. return(0);
  713. /*
  714. * utf is a string of 1, 2, 3 or 4 bytes. The valid strings
  715. * are as follows (in "bit format"):
  716. * 0xxxxxxx valid 1-byte
  717. * 110xxxxx 10xxxxxx valid 2-byte
  718. * 1110xxxx 10xxxxxx 10xxxxxx valid 3-byte
  719. * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx valid 4-byte
  720. */
  721. while ((c = utf[0])) { /* string is 0-terminated */
  722. ix = 0;
  723. if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */
  724. ix = 1;
  725. } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */
  726. if ((utf[1] & 0xc0 ) != 0x80)
  727. return 0;
  728. ix = 2;
  729. } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */
  730. if (((utf[1] & 0xc0) != 0x80) ||
  731. ((utf[2] & 0xc0) != 0x80))
  732. return 0;
  733. ix = 3;
  734. } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */
  735. if (((utf[1] & 0xc0) != 0x80) ||
  736. ((utf[2] & 0xc0) != 0x80) ||
  737. ((utf[3] & 0xc0) != 0x80))
  738. return 0;
  739. ix = 4;
  740. } else /* unknown encoding */
  741. return 0;
  742. utf += ix;
  743. }
  744. return(1);
  745. }
  746. /**
  747. * xmlUTF8Strsize:
  748. * @utf: a sequence of UTF-8 encoded bytes
  749. * @len: the number of characters in the array
  750. *
  751. * storage size of an UTF8 string
  752. * the behaviour is not guaranteed if the input string is not UTF-8
  753. *
  754. * Returns the storage size of
  755. * the first 'len' characters of ARRAY
  756. */
  757. int
  758. xmlUTF8Strsize(const xmlChar *utf, int len) {
  759. const xmlChar *ptr=utf;
  760. int ch;
  761. size_t ret;
  762. if (utf == NULL)
  763. return(0);
  764. if (len <= 0)
  765. return(0);
  766. while ( len-- > 0) {
  767. if ( !*ptr )
  768. break;
  769. if ( (ch = *ptr++) & 0x80)
  770. while ((ch<<=1) & 0x80 ) {
  771. if (*ptr == 0) break;
  772. ptr++;
  773. }
  774. }
  775. ret = ptr - utf;
  776. return (ret > INT_MAX ? 0 : ret);
  777. }
  778. /**
  779. * xmlUTF8Strndup:
  780. * @utf: the input UTF8 *
  781. * @len: the len of @utf (in chars)
  782. *
  783. * a strndup for array of UTF8's
  784. *
  785. * Returns a new UTF8 * or NULL
  786. */
  787. xmlChar *
  788. xmlUTF8Strndup(const xmlChar *utf, int len) {
  789. xmlChar *ret;
  790. int i;
  791. if ((utf == NULL) || (len < 0)) return(NULL);
  792. i = xmlUTF8Strsize(utf, len);
  793. ret = (xmlChar *) xmlMallocAtomic(((size_t) i + 1) * sizeof(xmlChar));
  794. if (ret == NULL) {
  795. return(NULL);
  796. }
  797. memcpy(ret, utf, i * sizeof(xmlChar));
  798. ret[i] = 0;
  799. return(ret);
  800. }
  801. /**
  802. * xmlUTF8Strpos:
  803. * @utf: the input UTF8 *
  804. * @pos: the position of the desired UTF8 char (in chars)
  805. *
  806. * a function to provide the equivalent of fetching a
  807. * character from a string array
  808. *
  809. * Returns a pointer to the UTF8 character or NULL
  810. */
  811. const xmlChar *
  812. xmlUTF8Strpos(const xmlChar *utf, int pos) {
  813. int ch;
  814. if (utf == NULL) return(NULL);
  815. if (pos < 0)
  816. return(NULL);
  817. while (pos--) {
  818. if ((ch=*utf++) == 0) return(NULL);
  819. if ( ch & 0x80 ) {
  820. /* if not simple ascii, verify proper format */
  821. if ( (ch & 0xc0) != 0xc0 )
  822. return(NULL);
  823. /* then skip over remaining bytes for this char */
  824. while ( (ch <<= 1) & 0x80 )
  825. if ( (*utf++ & 0xc0) != 0x80 )
  826. return(NULL);
  827. }
  828. }
  829. return((xmlChar *)utf);
  830. }
  831. /**
  832. * xmlUTF8Strloc:
  833. * @utf: the input UTF8 *
  834. * @utfchar: the UTF8 character to be found
  835. *
  836. * a function to provide the relative location of a UTF8 char
  837. *
  838. * Returns the relative character position of the desired char
  839. * or -1 if not found
  840. */
  841. int
  842. xmlUTF8Strloc(const xmlChar *utf, const xmlChar *utfchar) {
  843. size_t i;
  844. int size;
  845. int ch;
  846. if (utf==NULL || utfchar==NULL) return -1;
  847. size = xmlUTF8Strsize(utfchar, 1);
  848. for(i=0; (ch=*utf) != 0; i++) {
  849. if (xmlStrncmp(utf, utfchar, size)==0)
  850. return(i > INT_MAX ? 0 : i);
  851. utf++;
  852. if ( ch & 0x80 ) {
  853. /* if not simple ascii, verify proper format */
  854. if ( (ch & 0xc0) != 0xc0 )
  855. return(-1);
  856. /* then skip over remaining bytes for this char */
  857. while ( (ch <<= 1) & 0x80 )
  858. if ( (*utf++ & 0xc0) != 0x80 )
  859. return(-1);
  860. }
  861. }
  862. return(-1);
  863. }
  864. /**
  865. * xmlUTF8Strsub:
  866. * @utf: a sequence of UTF-8 encoded bytes
  867. * @start: relative pos of first char
  868. * @len: total number to copy
  869. *
  870. * Create a substring from a given UTF-8 string
  871. * Note: positions are given in units of UTF-8 chars
  872. *
  873. * Returns a pointer to a newly created string
  874. * or NULL if any problem
  875. */
  876. xmlChar *
  877. xmlUTF8Strsub(const xmlChar *utf, int start, int len) {
  878. int i;
  879. int ch;
  880. if (utf == NULL) return(NULL);
  881. if (start < 0) return(NULL);
  882. if (len < 0) return(NULL);
  883. /*
  884. * Skip over any leading chars
  885. */
  886. for (i = 0;i < start;i++) {
  887. if ((ch=*utf++) == 0) return(NULL);
  888. if ( ch & 0x80 ) {
  889. /* if not simple ascii, verify proper format */
  890. if ( (ch & 0xc0) != 0xc0 )
  891. return(NULL);
  892. /* then skip over remaining bytes for this char */
  893. while ( (ch <<= 1) & 0x80 )
  894. if ( (*utf++ & 0xc0) != 0x80 )
  895. return(NULL);
  896. }
  897. }
  898. return(xmlUTF8Strndup(utf, len));
  899. }
  900. /**
  901. * xmlEscapeFormatString:
  902. * @msg: a pointer to the string in which to escape '%' characters.
  903. * Must be a heap-allocated buffer created by libxml2 that may be
  904. * returned, or that may be freed and replaced.
  905. *
  906. * Replaces the string pointed to by 'msg' with an escaped string.
  907. * Returns the same string with all '%' characters escaped.
  908. */
  909. xmlChar *
  910. xmlEscapeFormatString(xmlChar **msg)
  911. {
  912. xmlChar *msgPtr = NULL;
  913. xmlChar *result = NULL;
  914. xmlChar *resultPtr = NULL;
  915. size_t count = 0;
  916. size_t msgLen = 0;
  917. size_t resultLen = 0;
  918. if (!msg || !*msg)
  919. return(NULL);
  920. for (msgPtr = *msg; *msgPtr != '\0'; ++msgPtr) {
  921. ++msgLen;
  922. if (*msgPtr == '%')
  923. ++count;
  924. }
  925. if (count == 0)
  926. return(*msg);
  927. if ((count > INT_MAX) || (msgLen > INT_MAX - count))
  928. return(NULL);
  929. resultLen = msgLen + count + 1;
  930. result = (xmlChar *) xmlMallocAtomic(resultLen * sizeof(xmlChar));
  931. if (result == NULL) {
  932. /* Clear *msg to prevent format string vulnerabilities in
  933. out-of-memory situations. */
  934. xmlFree(*msg);
  935. *msg = NULL;
  936. xmlErrMemory(NULL, NULL);
  937. return(NULL);
  938. }
  939. for (msgPtr = *msg, resultPtr = result; *msgPtr != '\0'; ++msgPtr, ++resultPtr) {
  940. *resultPtr = *msgPtr;
  941. if (*msgPtr == '%')
  942. *(++resultPtr) = '%';
  943. }
  944. result[resultLen - 1] = '\0';
  945. xmlFree(*msg);
  946. *msg = result;
  947. return *msg;
  948. }
  949. #define bottom_xmlstring
  950. #include "elfgcchack.h"