xmlstring.c 26 KB

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