annotate modules/javafx.web/src/main/native/Source/ThirdParty/libxml/src/xmlstring.c @ 10839:d4868b8fd76f

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