xmlstring.c 24 KB

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