Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* |
michael@0 | 6 | * utf8.c |
michael@0 | 7 | * |
michael@0 | 8 | * This file contains some additional utility routines required for |
michael@0 | 9 | * handling UTF8 strings. |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef BASE_H |
michael@0 | 13 | #include "base.h" |
michael@0 | 14 | #endif /* BASE_H */ |
michael@0 | 15 | |
michael@0 | 16 | #include "plstr.h" |
michael@0 | 17 | |
michael@0 | 18 | /* |
michael@0 | 19 | * NOTES: |
michael@0 | 20 | * |
michael@0 | 21 | * There's an "is hex string" function in pki1/atav.c. If we need |
michael@0 | 22 | * it in more places, pull that one out. |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | * nssUTF8_CaseIgnoreMatch |
michael@0 | 27 | * |
michael@0 | 28 | * Returns true if the two UTF8-encoded strings pointed to by the |
michael@0 | 29 | * two specified NSSUTF8 pointers differ only in typcase. |
michael@0 | 30 | * |
michael@0 | 31 | * The error may be one of the following values: |
michael@0 | 32 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 33 | * |
michael@0 | 34 | * Return value: |
michael@0 | 35 | * PR_TRUE if the strings match, ignoring case |
michael@0 | 36 | * PR_FALSE if they don't |
michael@0 | 37 | * PR_FALSE upon error |
michael@0 | 38 | */ |
michael@0 | 39 | |
michael@0 | 40 | NSS_IMPLEMENT PRBool |
michael@0 | 41 | nssUTF8_CaseIgnoreMatch |
michael@0 | 42 | ( |
michael@0 | 43 | const NSSUTF8 *a, |
michael@0 | 44 | const NSSUTF8 *b, |
michael@0 | 45 | PRStatus *statusOpt |
michael@0 | 46 | ) |
michael@0 | 47 | { |
michael@0 | 48 | #ifdef NSSDEBUG |
michael@0 | 49 | if( ((const NSSUTF8 *)NULL == a) || |
michael@0 | 50 | ((const NSSUTF8 *)NULL == b) ) { |
michael@0 | 51 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 52 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 53 | *statusOpt = PR_FAILURE; |
michael@0 | 54 | } |
michael@0 | 55 | return PR_FALSE; |
michael@0 | 56 | } |
michael@0 | 57 | #endif /* NSSDEBUG */ |
michael@0 | 58 | |
michael@0 | 59 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 60 | *statusOpt = PR_SUCCESS; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | /* |
michael@0 | 64 | * XXX fgmr |
michael@0 | 65 | * |
michael@0 | 66 | * This is, like, so wrong! |
michael@0 | 67 | */ |
michael@0 | 68 | if( 0 == PL_strcasecmp((const char *)a, (const char *)b) ) { |
michael@0 | 69 | return PR_TRUE; |
michael@0 | 70 | } else { |
michael@0 | 71 | return PR_FALSE; |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /* |
michael@0 | 76 | * nssUTF8_PrintableMatch |
michael@0 | 77 | * |
michael@0 | 78 | * Returns true if the two Printable strings pointed to by the |
michael@0 | 79 | * two specified NSSUTF8 pointers match when compared with the |
michael@0 | 80 | * rules for Printable String (leading and trailing spaces are |
michael@0 | 81 | * disregarded, extents of whitespace match irregardless of length, |
michael@0 | 82 | * and case is not significant), then PR_TRUE will be returned. |
michael@0 | 83 | * Otherwise, PR_FALSE will be returned. Upon failure, PR_FALSE |
michael@0 | 84 | * will be returned. If the optional statusOpt argument is not |
michael@0 | 85 | * NULL, then PR_SUCCESS or PR_FAILURE will be stored in that |
michael@0 | 86 | * location. |
michael@0 | 87 | * |
michael@0 | 88 | * The error may be one of the following values: |
michael@0 | 89 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 90 | * |
michael@0 | 91 | * Return value: |
michael@0 | 92 | * PR_TRUE if the strings match, ignoring case |
michael@0 | 93 | * PR_FALSE if they don't |
michael@0 | 94 | * PR_FALSE upon error |
michael@0 | 95 | */ |
michael@0 | 96 | |
michael@0 | 97 | NSS_IMPLEMENT PRBool |
michael@0 | 98 | nssUTF8_PrintableMatch |
michael@0 | 99 | ( |
michael@0 | 100 | const NSSUTF8 *a, |
michael@0 | 101 | const NSSUTF8 *b, |
michael@0 | 102 | PRStatus *statusOpt |
michael@0 | 103 | ) |
michael@0 | 104 | { |
michael@0 | 105 | PRUint8 *c; |
michael@0 | 106 | PRUint8 *d; |
michael@0 | 107 | |
michael@0 | 108 | #ifdef NSSDEBUG |
michael@0 | 109 | if( ((const NSSUTF8 *)NULL == a) || |
michael@0 | 110 | ((const NSSUTF8 *)NULL == b) ) { |
michael@0 | 111 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 112 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 113 | *statusOpt = PR_FAILURE; |
michael@0 | 114 | } |
michael@0 | 115 | return PR_FALSE; |
michael@0 | 116 | } |
michael@0 | 117 | #endif /* NSSDEBUG */ |
michael@0 | 118 | |
michael@0 | 119 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 120 | *statusOpt = PR_SUCCESS; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | c = (PRUint8 *)a; |
michael@0 | 124 | d = (PRUint8 *)b; |
michael@0 | 125 | |
michael@0 | 126 | while( ' ' == *c ) { |
michael@0 | 127 | c++; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | while( ' ' == *d ) { |
michael@0 | 131 | d++; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | while( ('\0' != *c) && ('\0' != *d) ) { |
michael@0 | 135 | PRUint8 e, f; |
michael@0 | 136 | |
michael@0 | 137 | e = *c; |
michael@0 | 138 | f = *d; |
michael@0 | 139 | |
michael@0 | 140 | if( ('a' <= e) && (e <= 'z') ) { |
michael@0 | 141 | e -= ('a' - 'A'); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | if( ('a' <= f) && (f <= 'z') ) { |
michael@0 | 145 | f -= ('a' - 'A'); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | if( e != f ) { |
michael@0 | 149 | return PR_FALSE; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | c++; |
michael@0 | 153 | d++; |
michael@0 | 154 | |
michael@0 | 155 | if( ' ' == *c ) { |
michael@0 | 156 | while( ' ' == *c ) { |
michael@0 | 157 | c++; |
michael@0 | 158 | } |
michael@0 | 159 | c--; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | if( ' ' == *d ) { |
michael@0 | 163 | while( ' ' == *d ) { |
michael@0 | 164 | d++; |
michael@0 | 165 | } |
michael@0 | 166 | d--; |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | while( ' ' == *c ) { |
michael@0 | 171 | c++; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | while( ' ' == *d ) { |
michael@0 | 175 | d++; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | if( *c == *d ) { |
michael@0 | 179 | /* And both '\0', btw */ |
michael@0 | 180 | return PR_TRUE; |
michael@0 | 181 | } else { |
michael@0 | 182 | return PR_FALSE; |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | /* |
michael@0 | 187 | * nssUTF8_Duplicate |
michael@0 | 188 | * |
michael@0 | 189 | * This routine duplicates the UTF8-encoded string pointed to by the |
michael@0 | 190 | * specified NSSUTF8 pointer. If the optional arenaOpt argument is |
michael@0 | 191 | * not null, the memory required will be obtained from that arena; |
michael@0 | 192 | * otherwise, the memory required will be obtained from the heap. |
michael@0 | 193 | * A pointer to the new string will be returned. In case of error, |
michael@0 | 194 | * an error will be placed on the error stack and NULL will be |
michael@0 | 195 | * returned. |
michael@0 | 196 | * |
michael@0 | 197 | * The error may be one of the following values: |
michael@0 | 198 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 199 | * NSS_ERROR_INVALID_ARENA |
michael@0 | 200 | * NSS_ERROR_NO_MEMORY |
michael@0 | 201 | */ |
michael@0 | 202 | |
michael@0 | 203 | NSS_IMPLEMENT NSSUTF8 * |
michael@0 | 204 | nssUTF8_Duplicate |
michael@0 | 205 | ( |
michael@0 | 206 | const NSSUTF8 *s, |
michael@0 | 207 | NSSArena *arenaOpt |
michael@0 | 208 | ) |
michael@0 | 209 | { |
michael@0 | 210 | NSSUTF8 *rv; |
michael@0 | 211 | PRUint32 len; |
michael@0 | 212 | |
michael@0 | 213 | #ifdef NSSDEBUG |
michael@0 | 214 | if( (const NSSUTF8 *)NULL == s ) { |
michael@0 | 215 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 216 | return (NSSUTF8 *)NULL; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | if( (NSSArena *)NULL != arenaOpt ) { |
michael@0 | 220 | if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { |
michael@0 | 221 | return (NSSUTF8 *)NULL; |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | #endif /* NSSDEBUG */ |
michael@0 | 225 | |
michael@0 | 226 | len = PL_strlen((const char *)s); |
michael@0 | 227 | #ifdef PEDANTIC |
michael@0 | 228 | if( '\0' != ((const char *)s)[ len ] ) { |
michael@0 | 229 | /* must have wrapped, e.g., too big for PRUint32 */ |
michael@0 | 230 | nss_SetError(NSS_ERROR_NO_MEMORY); |
michael@0 | 231 | return (NSSUTF8 *)NULL; |
michael@0 | 232 | } |
michael@0 | 233 | #endif /* PEDANTIC */ |
michael@0 | 234 | len++; /* zero termination */ |
michael@0 | 235 | |
michael@0 | 236 | rv = nss_ZAlloc(arenaOpt, len); |
michael@0 | 237 | if( (void *)NULL == rv ) { |
michael@0 | 238 | return (NSSUTF8 *)NULL; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | (void)nsslibc_memcpy(rv, s, len); |
michael@0 | 242 | return rv; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | /* |
michael@0 | 246 | * nssUTF8_Size |
michael@0 | 247 | * |
michael@0 | 248 | * This routine returns the length in bytes (including the terminating |
michael@0 | 249 | * null) of the UTF8-encoded string pointed to by the specified |
michael@0 | 250 | * NSSUTF8 pointer. Zero is returned on error. |
michael@0 | 251 | * |
michael@0 | 252 | * The error may be one of the following values: |
michael@0 | 253 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 254 | * NSS_ERROR_VALUE_TOO_LARGE |
michael@0 | 255 | * |
michael@0 | 256 | * Return value: |
michael@0 | 257 | * 0 on error |
michael@0 | 258 | * nonzero length of the string. |
michael@0 | 259 | */ |
michael@0 | 260 | |
michael@0 | 261 | NSS_IMPLEMENT PRUint32 |
michael@0 | 262 | nssUTF8_Size |
michael@0 | 263 | ( |
michael@0 | 264 | const NSSUTF8 *s, |
michael@0 | 265 | PRStatus *statusOpt |
michael@0 | 266 | ) |
michael@0 | 267 | { |
michael@0 | 268 | PRUint32 sv; |
michael@0 | 269 | |
michael@0 | 270 | #ifdef NSSDEBUG |
michael@0 | 271 | if( (const NSSUTF8 *)NULL == s ) { |
michael@0 | 272 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 273 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 274 | *statusOpt = PR_FAILURE; |
michael@0 | 275 | } |
michael@0 | 276 | return 0; |
michael@0 | 277 | } |
michael@0 | 278 | #endif /* NSSDEBUG */ |
michael@0 | 279 | |
michael@0 | 280 | sv = PL_strlen((const char *)s) + 1; |
michael@0 | 281 | #ifdef PEDANTIC |
michael@0 | 282 | if( '\0' != ((const char *)s)[ sv-1 ] ) { |
michael@0 | 283 | /* wrapped */ |
michael@0 | 284 | nss_SetError(NSS_ERROR_VALUE_TOO_LARGE); |
michael@0 | 285 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 286 | *statusOpt = PR_FAILURE; |
michael@0 | 287 | } |
michael@0 | 288 | return 0; |
michael@0 | 289 | } |
michael@0 | 290 | #endif /* PEDANTIC */ |
michael@0 | 291 | |
michael@0 | 292 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 293 | *statusOpt = PR_SUCCESS; |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | return sv; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | /* |
michael@0 | 300 | * nssUTF8_Length |
michael@0 | 301 | * |
michael@0 | 302 | * This routine returns the length in characters (not including the |
michael@0 | 303 | * terminating null) of the UTF8-encoded string pointed to by the |
michael@0 | 304 | * specified NSSUTF8 pointer. |
michael@0 | 305 | * |
michael@0 | 306 | * The error may be one of the following values: |
michael@0 | 307 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 308 | * NSS_ERROR_VALUE_TOO_LARGE |
michael@0 | 309 | * NSS_ERROR_INVALID_STRING |
michael@0 | 310 | * |
michael@0 | 311 | * Return value: |
michael@0 | 312 | * length of the string (which may be zero) |
michael@0 | 313 | * 0 on error |
michael@0 | 314 | */ |
michael@0 | 315 | |
michael@0 | 316 | NSS_IMPLEMENT PRUint32 |
michael@0 | 317 | nssUTF8_Length |
michael@0 | 318 | ( |
michael@0 | 319 | const NSSUTF8 *s, |
michael@0 | 320 | PRStatus *statusOpt |
michael@0 | 321 | ) |
michael@0 | 322 | { |
michael@0 | 323 | PRUint32 l = 0; |
michael@0 | 324 | const PRUint8 *c = (const PRUint8 *)s; |
michael@0 | 325 | |
michael@0 | 326 | #ifdef NSSDEBUG |
michael@0 | 327 | if( (const NSSUTF8 *)NULL == s ) { |
michael@0 | 328 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 329 | goto loser; |
michael@0 | 330 | } |
michael@0 | 331 | #endif /* NSSDEBUG */ |
michael@0 | 332 | |
michael@0 | 333 | /* |
michael@0 | 334 | * From RFC 2044: |
michael@0 | 335 | * |
michael@0 | 336 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
michael@0 | 337 | * 0000 0000-0000 007F 0xxxxxxx |
michael@0 | 338 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
michael@0 | 339 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
michael@0 | 340 | * 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
michael@0 | 341 | * 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
michael@0 | 342 | * 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx |
michael@0 | 343 | */ |
michael@0 | 344 | |
michael@0 | 345 | while( 0 != *c ) { |
michael@0 | 346 | PRUint32 incr; |
michael@0 | 347 | if( (*c & 0x80) == 0 ) { |
michael@0 | 348 | incr = 1; |
michael@0 | 349 | } else if( (*c & 0xE0) == 0xC0 ) { |
michael@0 | 350 | incr = 2; |
michael@0 | 351 | } else if( (*c & 0xF0) == 0xE0 ) { |
michael@0 | 352 | incr = 3; |
michael@0 | 353 | } else if( (*c & 0xF8) == 0xF0 ) { |
michael@0 | 354 | incr = 4; |
michael@0 | 355 | } else if( (*c & 0xFC) == 0xF8 ) { |
michael@0 | 356 | incr = 5; |
michael@0 | 357 | } else if( (*c & 0xFE) == 0xFC ) { |
michael@0 | 358 | incr = 6; |
michael@0 | 359 | } else { |
michael@0 | 360 | nss_SetError(NSS_ERROR_INVALID_STRING); |
michael@0 | 361 | goto loser; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | l += incr; |
michael@0 | 365 | |
michael@0 | 366 | #ifdef PEDANTIC |
michael@0 | 367 | if( l < incr ) { |
michael@0 | 368 | /* Wrapped-- too big */ |
michael@0 | 369 | nss_SetError(NSS_ERROR_VALUE_TOO_LARGE); |
michael@0 | 370 | goto loser; |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | { |
michael@0 | 374 | PRUint8 *d; |
michael@0 | 375 | for( d = &c[1]; d < &c[incr]; d++ ) { |
michael@0 | 376 | if( (*d & 0xC0) != 0xF0 ) { |
michael@0 | 377 | nss_SetError(NSS_ERROR_INVALID_STRING); |
michael@0 | 378 | goto loser; |
michael@0 | 379 | } |
michael@0 | 380 | } |
michael@0 | 381 | } |
michael@0 | 382 | #endif /* PEDANTIC */ |
michael@0 | 383 | |
michael@0 | 384 | c += incr; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 388 | *statusOpt = PR_SUCCESS; |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | return l; |
michael@0 | 392 | |
michael@0 | 393 | loser: |
michael@0 | 394 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 395 | *statusOpt = PR_FAILURE; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | return 0; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | |
michael@0 | 402 | /* |
michael@0 | 403 | * nssUTF8_Create |
michael@0 | 404 | * |
michael@0 | 405 | * This routine creates a UTF8 string from a string in some other |
michael@0 | 406 | * format. Some types of string may include embedded null characters, |
michael@0 | 407 | * so for them the length parameter must be used. For string types |
michael@0 | 408 | * that are null-terminated, the length parameter is optional; if it |
michael@0 | 409 | * is zero, it will be ignored. If the optional arena argument is |
michael@0 | 410 | * non-null, the memory used for the new string will be obtained from |
michael@0 | 411 | * that arena, otherwise it will be obtained from the heap. This |
michael@0 | 412 | * routine may return NULL upon error, in which case it will have |
michael@0 | 413 | * placed an error on the error stack. |
michael@0 | 414 | * |
michael@0 | 415 | * The error may be one of the following: |
michael@0 | 416 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 417 | * NSS_ERROR_NO_MEMORY |
michael@0 | 418 | * NSS_ERROR_UNSUPPORTED_TYPE |
michael@0 | 419 | * |
michael@0 | 420 | * Return value: |
michael@0 | 421 | * NULL upon error |
michael@0 | 422 | * A non-null pointer to a new UTF8 string otherwise |
michael@0 | 423 | */ |
michael@0 | 424 | |
michael@0 | 425 | extern const NSSError NSS_ERROR_INTERNAL_ERROR; /* XXX fgmr */ |
michael@0 | 426 | |
michael@0 | 427 | NSS_IMPLEMENT NSSUTF8 * |
michael@0 | 428 | nssUTF8_Create |
michael@0 | 429 | ( |
michael@0 | 430 | NSSArena *arenaOpt, |
michael@0 | 431 | nssStringType type, |
michael@0 | 432 | const void *inputString, |
michael@0 | 433 | PRUint32 size /* in bytes, not characters */ |
michael@0 | 434 | ) |
michael@0 | 435 | { |
michael@0 | 436 | NSSUTF8 *rv = NULL; |
michael@0 | 437 | |
michael@0 | 438 | #ifdef NSSDEBUG |
michael@0 | 439 | if( (NSSArena *)NULL != arenaOpt ) { |
michael@0 | 440 | if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { |
michael@0 | 441 | return (NSSUTF8 *)NULL; |
michael@0 | 442 | } |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | if( (const void *)NULL == inputString ) { |
michael@0 | 446 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 447 | return (NSSUTF8 *)NULL; |
michael@0 | 448 | } |
michael@0 | 449 | #endif /* NSSDEBUG */ |
michael@0 | 450 | |
michael@0 | 451 | switch( type ) { |
michael@0 | 452 | case nssStringType_DirectoryString: |
michael@0 | 453 | /* This is a composite type requiring BER */ |
michael@0 | 454 | nss_SetError(NSS_ERROR_UNSUPPORTED_TYPE); |
michael@0 | 455 | break; |
michael@0 | 456 | case nssStringType_TeletexString: |
michael@0 | 457 | /* |
michael@0 | 458 | * draft-ietf-pkix-ipki-part1-11 says in part: |
michael@0 | 459 | * |
michael@0 | 460 | * In addition, many legacy implementations support names encoded |
michael@0 | 461 | * in the ISO 8859-1 character set (Latin1String) but tag them as |
michael@0 | 462 | * TeletexString. The Latin1String includes characters used in |
michael@0 | 463 | * Western European countries which are not part of the |
michael@0 | 464 | * TeletexString charcter set. Implementations that process |
michael@0 | 465 | * TeletexString SHOULD be prepared to handle the entire ISO |
michael@0 | 466 | * 8859-1 character set.[ISO 8859-1]. |
michael@0 | 467 | */ |
michael@0 | 468 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 469 | break; |
michael@0 | 470 | case nssStringType_PrintableString: |
michael@0 | 471 | /* |
michael@0 | 472 | * PrintableString consists of A-Za-z0-9 ,()+,-./:=? |
michael@0 | 473 | * This is a subset of ASCII, which is a subset of UTF8. |
michael@0 | 474 | * So we can just duplicate the string over. |
michael@0 | 475 | */ |
michael@0 | 476 | |
michael@0 | 477 | if( 0 == size ) { |
michael@0 | 478 | rv = nssUTF8_Duplicate((const NSSUTF8 *)inputString, arenaOpt); |
michael@0 | 479 | } else { |
michael@0 | 480 | rv = nss_ZAlloc(arenaOpt, size+1); |
michael@0 | 481 | if( (NSSUTF8 *)NULL == rv ) { |
michael@0 | 482 | return (NSSUTF8 *)NULL; |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | (void)nsslibc_memcpy(rv, inputString, size); |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | break; |
michael@0 | 489 | case nssStringType_UniversalString: |
michael@0 | 490 | /* 4-byte unicode */ |
michael@0 | 491 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 492 | break; |
michael@0 | 493 | case nssStringType_BMPString: |
michael@0 | 494 | /* Base Multilingual Plane of Unicode */ |
michael@0 | 495 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 496 | break; |
michael@0 | 497 | case nssStringType_UTF8String: |
michael@0 | 498 | if( 0 == size ) { |
michael@0 | 499 | rv = nssUTF8_Duplicate((const NSSUTF8 *)inputString, arenaOpt); |
michael@0 | 500 | } else { |
michael@0 | 501 | rv = nss_ZAlloc(arenaOpt, size+1); |
michael@0 | 502 | if( (NSSUTF8 *)NULL == rv ) { |
michael@0 | 503 | return (NSSUTF8 *)NULL; |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | (void)nsslibc_memcpy(rv, inputString, size); |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | break; |
michael@0 | 510 | case nssStringType_PHGString: |
michael@0 | 511 | /* |
michael@0 | 512 | * PHGString is an IA5String (with case-insensitive comparisons). |
michael@0 | 513 | * IA5 is ~almost~ ascii; ascii has dollar-sign where IA5 has |
michael@0 | 514 | * currency symbol. |
michael@0 | 515 | */ |
michael@0 | 516 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 517 | break; |
michael@0 | 518 | case nssStringType_GeneralString: |
michael@0 | 519 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 520 | break; |
michael@0 | 521 | default: |
michael@0 | 522 | nss_SetError(NSS_ERROR_UNSUPPORTED_TYPE); |
michael@0 | 523 | break; |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | return rv; |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | NSS_IMPLEMENT NSSItem * |
michael@0 | 530 | nssUTF8_GetEncoding |
michael@0 | 531 | ( |
michael@0 | 532 | NSSArena *arenaOpt, |
michael@0 | 533 | NSSItem *rvOpt, |
michael@0 | 534 | nssStringType type, |
michael@0 | 535 | NSSUTF8 *string |
michael@0 | 536 | ) |
michael@0 | 537 | { |
michael@0 | 538 | NSSItem *rv = (NSSItem *)NULL; |
michael@0 | 539 | PRStatus status = PR_SUCCESS; |
michael@0 | 540 | |
michael@0 | 541 | #ifdef NSSDEBUG |
michael@0 | 542 | if( (NSSArena *)NULL != arenaOpt ) { |
michael@0 | 543 | if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { |
michael@0 | 544 | return (NSSItem *)NULL; |
michael@0 | 545 | } |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | if( (NSSUTF8 *)NULL == string ) { |
michael@0 | 549 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 550 | return (NSSItem *)NULL; |
michael@0 | 551 | } |
michael@0 | 552 | #endif /* NSSDEBUG */ |
michael@0 | 553 | |
michael@0 | 554 | switch( type ) { |
michael@0 | 555 | case nssStringType_DirectoryString: |
michael@0 | 556 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 557 | break; |
michael@0 | 558 | case nssStringType_TeletexString: |
michael@0 | 559 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 560 | break; |
michael@0 | 561 | case nssStringType_PrintableString: |
michael@0 | 562 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 563 | break; |
michael@0 | 564 | case nssStringType_UniversalString: |
michael@0 | 565 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 566 | break; |
michael@0 | 567 | case nssStringType_BMPString: |
michael@0 | 568 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 569 | break; |
michael@0 | 570 | case nssStringType_UTF8String: |
michael@0 | 571 | { |
michael@0 | 572 | NSSUTF8 *dup = nssUTF8_Duplicate(string, arenaOpt); |
michael@0 | 573 | if( (NSSUTF8 *)NULL == dup ) { |
michael@0 | 574 | return (NSSItem *)NULL; |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | if( (NSSItem *)NULL == rvOpt ) { |
michael@0 | 578 | rv = nss_ZNEW(arenaOpt, NSSItem); |
michael@0 | 579 | if( (NSSItem *)NULL == rv ) { |
michael@0 | 580 | (void)nss_ZFreeIf(dup); |
michael@0 | 581 | return (NSSItem *)NULL; |
michael@0 | 582 | } |
michael@0 | 583 | } else { |
michael@0 | 584 | rv = rvOpt; |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | rv->data = dup; |
michael@0 | 588 | dup = (NSSUTF8 *)NULL; |
michael@0 | 589 | rv->size = nssUTF8_Size(rv->data, &status); |
michael@0 | 590 | if( (0 == rv->size) && (PR_SUCCESS != status) ) { |
michael@0 | 591 | if( (NSSItem *)NULL == rvOpt ) { |
michael@0 | 592 | (void)nss_ZFreeIf(rv); |
michael@0 | 593 | } |
michael@0 | 594 | return (NSSItem *)NULL; |
michael@0 | 595 | } |
michael@0 | 596 | } |
michael@0 | 597 | break; |
michael@0 | 598 | case nssStringType_PHGString: |
michael@0 | 599 | nss_SetError(NSS_ERROR_INTERNAL_ERROR); /* unimplemented */ |
michael@0 | 600 | break; |
michael@0 | 601 | default: |
michael@0 | 602 | nss_SetError(NSS_ERROR_UNSUPPORTED_TYPE); |
michael@0 | 603 | break; |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | return rv; |
michael@0 | 607 | } |
michael@0 | 608 | |
michael@0 | 609 | /* |
michael@0 | 610 | * nssUTF8_CopyIntoFixedBuffer |
michael@0 | 611 | * |
michael@0 | 612 | * This will copy a UTF8 string into a fixed-length buffer, making |
michael@0 | 613 | * sure that the all characters are valid. Any remaining space will |
michael@0 | 614 | * be padded with the specified ASCII character, typically either |
michael@0 | 615 | * null or space. |
michael@0 | 616 | * |
michael@0 | 617 | * Blah, blah, blah. |
michael@0 | 618 | */ |
michael@0 | 619 | |
michael@0 | 620 | NSS_IMPLEMENT PRStatus |
michael@0 | 621 | nssUTF8_CopyIntoFixedBuffer |
michael@0 | 622 | ( |
michael@0 | 623 | NSSUTF8 *string, |
michael@0 | 624 | char *buffer, |
michael@0 | 625 | PRUint32 bufferSize, |
michael@0 | 626 | char pad |
michael@0 | 627 | ) |
michael@0 | 628 | { |
michael@0 | 629 | PRUint32 stringSize = 0; |
michael@0 | 630 | |
michael@0 | 631 | #ifdef NSSDEBUG |
michael@0 | 632 | if( (char *)NULL == buffer ) { |
michael@0 | 633 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 634 | return PR_FALSE; |
michael@0 | 635 | } |
michael@0 | 636 | |
michael@0 | 637 | if( 0 == bufferSize ) { |
michael@0 | 638 | nss_SetError(NSS_ERROR_INVALID_ARGUMENT); |
michael@0 | 639 | return PR_FALSE; |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | if( (pad & 0x80) != 0x00 ) { |
michael@0 | 643 | nss_SetError(NSS_ERROR_INVALID_ARGUMENT); |
michael@0 | 644 | return PR_FALSE; |
michael@0 | 645 | } |
michael@0 | 646 | #endif /* NSSDEBUG */ |
michael@0 | 647 | |
michael@0 | 648 | if( (NSSUTF8 *)NULL == string ) { |
michael@0 | 649 | string = (NSSUTF8 *) ""; |
michael@0 | 650 | } |
michael@0 | 651 | |
michael@0 | 652 | stringSize = nssUTF8_Size(string, (PRStatus *)NULL); |
michael@0 | 653 | stringSize--; /* don't count the trailing null */ |
michael@0 | 654 | if( stringSize > bufferSize ) { |
michael@0 | 655 | PRUint32 bs = bufferSize; |
michael@0 | 656 | (void)nsslibc_memcpy(buffer, string, bufferSize); |
michael@0 | 657 | |
michael@0 | 658 | if( ( ((buffer[ bs-1 ] & 0x80) == 0x00)) || |
michael@0 | 659 | ((bs > 1) && ((buffer[ bs-2 ] & 0xE0) == 0xC0)) || |
michael@0 | 660 | ((bs > 2) && ((buffer[ bs-3 ] & 0xF0) == 0xE0)) || |
michael@0 | 661 | ((bs > 3) && ((buffer[ bs-4 ] & 0xF8) == 0xF0)) || |
michael@0 | 662 | ((bs > 4) && ((buffer[ bs-5 ] & 0xFC) == 0xF8)) || |
michael@0 | 663 | ((bs > 5) && ((buffer[ bs-6 ] & 0xFE) == 0xFC)) ) { |
michael@0 | 664 | /* It fit exactly */ |
michael@0 | 665 | return PR_SUCCESS; |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | /* Too long. We have to trim the last character */ |
michael@0 | 669 | for( /*bs*/; bs != 0; bs-- ) { |
michael@0 | 670 | if( (buffer[bs-1] & 0xC0) != 0x80 ) { |
michael@0 | 671 | buffer[bs-1] = pad; |
michael@0 | 672 | break; |
michael@0 | 673 | } else { |
michael@0 | 674 | buffer[bs-1] = pad; |
michael@0 | 675 | } |
michael@0 | 676 | } |
michael@0 | 677 | } else { |
michael@0 | 678 | (void)nsslibc_memset(buffer, pad, bufferSize); |
michael@0 | 679 | (void)nsslibc_memcpy(buffer, string, stringSize); |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | return PR_SUCCESS; |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | /* |
michael@0 | 686 | * nssUTF8_Equal |
michael@0 | 687 | * |
michael@0 | 688 | */ |
michael@0 | 689 | |
michael@0 | 690 | NSS_IMPLEMENT PRBool |
michael@0 | 691 | nssUTF8_Equal |
michael@0 | 692 | ( |
michael@0 | 693 | const NSSUTF8 *a, |
michael@0 | 694 | const NSSUTF8 *b, |
michael@0 | 695 | PRStatus *statusOpt |
michael@0 | 696 | ) |
michael@0 | 697 | { |
michael@0 | 698 | PRUint32 la, lb; |
michael@0 | 699 | |
michael@0 | 700 | #ifdef NSSDEBUG |
michael@0 | 701 | if( ((const NSSUTF8 *)NULL == a) || |
michael@0 | 702 | ((const NSSUTF8 *)NULL == b) ) { |
michael@0 | 703 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 704 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 705 | *statusOpt = PR_FAILURE; |
michael@0 | 706 | } |
michael@0 | 707 | return PR_FALSE; |
michael@0 | 708 | } |
michael@0 | 709 | #endif /* NSSDEBUG */ |
michael@0 | 710 | |
michael@0 | 711 | la = nssUTF8_Size(a, statusOpt); |
michael@0 | 712 | if( 0 == la ) { |
michael@0 | 713 | return PR_FALSE; |
michael@0 | 714 | } |
michael@0 | 715 | |
michael@0 | 716 | lb = nssUTF8_Size(b, statusOpt); |
michael@0 | 717 | if( 0 == lb ) { |
michael@0 | 718 | return PR_FALSE; |
michael@0 | 719 | } |
michael@0 | 720 | |
michael@0 | 721 | if( la != lb ) { |
michael@0 | 722 | return PR_FALSE; |
michael@0 | 723 | } |
michael@0 | 724 | |
michael@0 | 725 | return nsslibc_memequal(a, b, la, statusOpt); |
michael@0 | 726 | } |