OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2007-2009 NVIDIA Corporation. |
| 3 * All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: |
| 7 * |
| 8 * Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. |
| 10 * |
| 11 * Redistributions in binary form must reproduce the above copyright notice, |
| 12 * this list of conditions and the following disclaimer in the documentation |
| 13 * and/or other materials provided with the distribution. |
| 14 * |
| 15 * Neither the name of the NVIDIA Corporation nor the names of its contributors |
| 16 * may be used to endorse or promote products derived from this software |
| 17 * without specific prior written permission. |
| 18 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 * POSSIBILITY OF SUCH DAMAGE. |
| 30 * |
| 31 */ |
| 32 |
| 33 #include "nvutil.h" |
| 34 #include "nvassert.h" |
| 35 |
| 36 //=========================================================================== |
| 37 // NvUIsdigit() - like the standard isdigit function |
| 38 //=========================================================================== |
| 39 static int NvUIsdigit(int c) |
| 40 { |
| 41 return (c>='0' && c<='9'); |
| 42 } |
| 43 |
| 44 //=========================================================================== |
| 45 // NvUIsxdigit() - like the standard isxdigit function |
| 46 //=========================================================================== |
| 47 static int NvUIsxdigit(int c) |
| 48 { |
| 49 return (c>='0' && c<='9') || (c>='A' && c<='F') || (c>='a' && c<='f'); |
| 50 } |
| 51 |
| 52 //=========================================================================== |
| 53 // NvUCharToXDigit() - convert a hex character to its value |
| 54 //=========================================================================== |
| 55 static int NvUCharToXDigit(int c) |
| 56 { |
| 57 return (c>='0' && c<='9') ? c - '0' : |
| 58 (c>='a' && c<='f') ? c - 'a' + 10 : |
| 59 (c>='A' && c<='F') ? c - 'A' + 10 : -1; |
| 60 } |
| 61 |
| 62 //=========================================================================== |
| 63 // NvUStrtoull() - like the standard strtoull function |
| 64 //=========================================================================== |
| 65 unsigned long long int NvUStrtoull(const char *s, char **endptr, int base) |
| 66 { |
| 67 int neg = 0; |
| 68 unsigned long long int val = 0; |
| 69 |
| 70 NV_ASSERT(s); |
| 71 NV_ASSERT(base==0 || base==10 || base==16); |
| 72 |
| 73 if (*s == '-') { |
| 74 s++; |
| 75 neg = 1; |
| 76 } |
| 77 if (s[0]=='0' && (s[1]=='x' || s[1]=='X')) { |
| 78 if (base == 10) { |
| 79 if (endptr) { |
| 80 *endptr = (char*)s+1; |
| 81 return val; |
| 82 } |
| 83 } |
| 84 s += 2; |
| 85 base = 16; |
| 86 } |
| 87 |
| 88 if (base == 16) { |
| 89 while (NvUIsxdigit(*s)) { |
| 90 val <<= 4; |
| 91 val += NvUCharToXDigit(*s); |
| 92 s++; |
| 93 } |
| 94 } else { |
| 95 while (NvUIsdigit(*s)) { |
| 96 val *= 10; |
| 97 val += NvUCharToXDigit(*s); |
| 98 s++; |
| 99 } |
| 100 } |
| 101 |
| 102 if (endptr) { |
| 103 *endptr = (char*)s; |
| 104 } |
| 105 return neg ? ((~val)+1) : val; |
| 106 } |
| 107 |
| 108 //=========================================================================== |
| 109 // NvUStrtoul() - like the standard strtoul function |
| 110 //=========================================================================== |
| 111 unsigned long int NvUStrtoul(const char *s, char **endptr, int base) |
| 112 { |
| 113 return (unsigned long)NvUStrtoull( s, endptr, base ); |
| 114 } |
| 115 |
| 116 //=========================================================================== |
| 117 // NvUStrtol() - like the standard strtol function |
| 118 //=========================================================================== |
| 119 long int NvUStrtol(const char *s, char **endptr, int base) |
| 120 { |
| 121 return (long int)NvUStrtoul(s,endptr,base); |
| 122 } |
| 123 |
| 124 //=========================================================================== |
| 125 // NvUStrncat() - like the standard strcat function |
| 126 //=========================================================================== |
| 127 void NvUStrncat(char *dest, const char *src, size_t n) |
| 128 { |
| 129 while(*dest) dest++; |
| 130 while(*src && n--) { |
| 131 *(dest++) = *(src++); |
| 132 } |
| 133 *dest = 0; |
| 134 } |
| 135 |
| 136 //=========================================================================== |
| 137 // NvUStrstr() - like the standard strstr function |
| 138 //=========================================================================== |
| 139 char * |
| 140 NvUStrstr( const char *str1, const char *str2 ) |
| 141 { |
| 142 char s2; |
| 143 NvU32 len; |
| 144 |
| 145 NV_ASSERT( str1 ); |
| 146 NV_ASSERT( str2 ); |
| 147 |
| 148 s2 = *str2++; |
| 149 |
| 150 // empty string case |
| 151 if (!s2) { |
| 152 return (char *)str1; |
| 153 } |
| 154 |
| 155 len = NvOsStrlen(str2); |
| 156 do { |
| 157 char s1; |
| 158 |
| 159 do { |
| 160 s1 = *str1++; |
| 161 if (!s1) { |
| 162 return (char *)0; |
| 163 } |
| 164 } while (s1 != s2); |
| 165 } while (NvOsStrncmp(str1, str2, len) != 0); |
| 166 |
| 167 return (char *)(str1 - 1); |
| 168 } |
| 169 |
| 170 //=========================================================================== |
| 171 // NvUStrlConvertCodePage() - see definition in nvutil.h |
| 172 // Lots of static helper functions to get/put characters in various |
| 173 // code pages. For reference on the encodings, see: |
| 174 // http://en.wikipedia.org/wiki/Windows-1252 |
| 175 // http://en.wikipedia.org/wiki/UTF-8 |
| 176 // http://en.wikipedia.org/wiki/UTF-16 |
| 177 //=========================================================================== |
| 178 typedef const void* (*StrGetFn)(const void*, NvU32*, size_t*); |
| 179 typedef size_t (*StrPutFn)(void*, NvU32); |
| 180 |
| 181 static const void* |
| 182 NvUStr_GetUtf8Coding(const void *pSrc, |
| 183 NvU32 *Coding, |
| 184 size_t *SrcSize) |
| 185 { |
| 186 const char *pCh = (const char *)pSrc; |
| 187 NvU32 tmp = 0; |
| 188 NvU8 ch; |
| 189 |
| 190 if (!*SrcSize) |
| 191 { |
| 192 *Coding = 0; |
| 193 return pSrc; |
| 194 } |
| 195 else |
| 196 { |
| 197 ch = (NvU8)*pCh++; |
| 198 *SrcSize = *SrcSize-1; |
| 199 } |
| 200 |
| 201 if (*SrcSize && (ch & 0x80)) |
| 202 { |
| 203 tmp = ((ch>>4) & 0x3); |
| 204 if (tmp) |
| 205 tmp--; |
| 206 tmp = (ch & (0x1f>>tmp)); |
| 207 do |
| 208 { |
| 209 ch = (NvU8)*pCh++; |
| 210 tmp<<=6; |
| 211 tmp |= (ch & 0x3f); |
| 212 *SrcSize = *SrcSize - 1; |
| 213 } while (*SrcSize && ((NvU8)*pCh & 0xc0)==0x80); |
| 214 |
| 215 } |
| 216 else |
| 217 { |
| 218 tmp = (NvU32)(ch&0x7f); |
| 219 } |
| 220 |
| 221 *Coding = tmp; |
| 222 return (const void *)pCh; |
| 223 } |
| 224 |
| 225 static const void* |
| 226 NvUStr_GetUtf16Coding(const void *pSrc, |
| 227 NvU32 *Coding, |
| 228 size_t *SrcSize) |
| 229 { |
| 230 const wchar_t *pCh = (const wchar_t *)pSrc; |
| 231 NvU32 tmp = 0; |
| 232 |
| 233 if (*SrcSize<2) |
| 234 { |
| 235 *Coding = 0; |
| 236 *SrcSize = 0; |
| 237 return pSrc; |
| 238 } |
| 239 |
| 240 tmp = (NvU32) *pCh++; |
| 241 *SrcSize = *SrcSize - 2; |
| 242 |
| 243 if ((*SrcSize>1) && ((tmp & 0xd800UL) == 0xd800UL)) |
| 244 { |
| 245 tmp = 0x10000UL + (((tmp & 0x3ff)<<10) | (((NvU32)*pCh++) & 0x3ffUL)); |
| 246 *SrcSize = *SrcSize - 2; |
| 247 } |
| 248 |
| 249 *Coding = tmp; |
| 250 return (const void *)pCh; |
| 251 } |
| 252 |
| 253 static const NvU16 Windows1252EscapeRemapTable[32] = { |
| 254 0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, |
| 255 0x2C26, 0x2030, 0x0160, 0x2039, 0x0152, 0, 0x017D, 0, |
| 256 0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, |
| 257 0x02Dc, 0x2122, 0x0161, 0x203A, 0x0153, 0, 0x017E, 0x0178 }; |
| 258 |
| 259 static const void* |
| 260 NvUStr_GetWindows1252Coding(const void *pSrc, |
| 261 NvU32 *Coding, |
| 262 size_t *SrcSize) |
| 263 { |
| 264 // the following table is used to remap windows-1252 codings 0x80-0x9f to |
| 265 // the closest unicode codings. reference: |
| 266 // http://en.wikipedia.org/wiki/Windows-1252 |
| 267 |
| 268 const char *pCh = (const char *)pSrc; |
| 269 NvU32 tmp; |
| 270 |
| 271 if (!*SrcSize) |
| 272 { |
| 273 *Coding = 0; |
| 274 return pSrc; |
| 275 } |
| 276 tmp = (NvU32)*pCh++; |
| 277 *SrcSize = *SrcSize - 1; |
| 278 |
| 279 if (tmp>=0x80 && tmp<0xA0) tmp = |
| 280 (NvU32) Windows1252EscapeRemapTable[tmp-0x80]; |
| 281 |
| 282 return (const void *) pCh; |
| 283 } |
| 284 |
| 285 static size_t |
| 286 NvUStr_PutUtf8Coding(void *pDest, |
| 287 NvU32 Coding) |
| 288 { |
| 289 unsigned int bytes; |
| 290 unsigned int i; |
| 291 unsigned int mask; |
| 292 unsigned int shift; |
| 293 NvU8 *pCh = (NvU8 *)pDest; |
| 294 |
| 295 if (Coding < 0x80) |
| 296 bytes = 1; |
| 297 else if (Coding < 0x800UL) |
| 298 bytes = 2; |
| 299 else if (Coding < 0x10000UL) |
| 300 bytes = 3; |
| 301 else |
| 302 bytes = 4; |
| 303 |
| 304 if (pCh) |
| 305 { |
| 306 mask = 0x7f; |
| 307 if (bytes>1) |
| 308 { |
| 309 mask >>= bytes; |
| 310 } |
| 311 shift = (bytes-1)*6; |
| 312 i = bytes; |
| 313 while (i--) |
| 314 { |
| 315 *pCh++ = (((~((mask<<1)|1))&0xff) | |
| 316 ((Coding>>shift) & mask)); |
| 317 shift -= 6; |
| 318 mask = 0x3f; |
| 319 } |
| 320 } |
| 321 |
| 322 return (size_t)bytes; |
| 323 } |
| 324 |
| 325 static size_t |
| 326 NvUStr_PutUtf16Coding(void *pDest, |
| 327 NvU32 Coding) |
| 328 |
| 329 { |
| 330 size_t bytes = (Coding > 0x10000UL) ? 4 : 2; |
| 331 NvU16 *pCh = (NvU16 *)pDest; |
| 332 |
| 333 if (pCh) |
| 334 { |
| 335 if (bytes==4) |
| 336 { |
| 337 Coding -= 0x10000UL; |
| 338 *pCh++ = (NvU16) (0xd800UL | ((Coding>>10)&0x3ffUL)); |
| 339 *pCh++ = (NvU16) (0xdb00UL | (Coding & 0x3ffUL)); |
| 340 } |
| 341 else |
| 342 *pCh++ = (NvU16) (Coding & 0xffffUL); |
| 343 } |
| 344 |
| 345 return bytes; |
| 346 } |
| 347 |
| 348 static size_t |
| 349 NvUStr_PutWindows1252Coding(void *pDest, |
| 350 NvU32 Coding) |
| 351 { |
| 352 NvU8 *pCh = (NvU8 *)pDest; |
| 353 unsigned int i; |
| 354 |
| 355 if (pCh) |
| 356 { |
| 357 if ((Coding<0x80UL) || ((Coding<0x100UL)&&(Coding>0x9FUL))) |
| 358 *pCh++ = (NvU8)(Coding & 0xff); |
| 359 else |
| 360 { |
| 361 for (i=0; i<32 && (NvU32)Windows1252EscapeRemapTable[i]!=Coding; i++
) { } |
| 362 *pCh++ = ((i==32) ? 0x90 : ((0x80+i) & 0xff)); |
| 363 } |
| 364 } |
| 365 return 1; |
| 366 } |
| 367 |
| 368 size_t |
| 369 NvUStrlConvertCodePage(void *pDest, |
| 370 size_t DestSize, |
| 371 NvOsCodePage DestCodePage, |
| 372 const void *pSrc, |
| 373 size_t SrcSize, |
| 374 NvOsCodePage SrcCodePage) |
| 375 { |
| 376 StrGetFn GetChar = NULL; |
| 377 StrPutFn PutChar = NULL; |
| 378 char *pStr = (char *)pDest; |
| 379 size_t OutputSize = 0; |
| 380 size_t CodeSize = 0; |
| 381 size_t Remain = SrcSize; |
| 382 NvU32 Coding; |
| 383 |
| 384 if (!pSrc) |
| 385 return 0; |
| 386 // to simplify down-stream code paths, if the source is NULL-terminated |
| 387 // (SrcSize==0), or the destination is NULL, set the corresponding sizes |
| 388 // to ~0 (effectively infinite, since memory will be filled before the |
| 389 // size limit is reached) |
| 390 if (!pDest) |
| 391 DestSize = (size_t)~0; |
| 392 if (!Remain) |
| 393 Remain = (size_t)~0; |
| 394 |
| 395 if (DestCodePage == NvOsCodePage_Unknown) |
| 396 DestCodePage = NvOsStrGetSystemCodePage(); |
| 397 if (SrcCodePage == NvOsCodePage_Unknown) |
| 398 SrcCodePage = NvOsStrGetSystemCodePage(); |
| 399 |
| 400 switch (DestCodePage) |
| 401 { |
| 402 case NvOsCodePage_Utf8: |
| 403 PutChar = NvUStr_PutUtf8Coding; break; |
| 404 case NvOsCodePage_Utf16: |
| 405 PutChar = NvUStr_PutUtf16Coding; break; |
| 406 case NvOsCodePage_Windows1252: |
| 407 PutChar = NvUStr_PutWindows1252Coding; break; |
| 408 default: |
| 409 NV_ASSERT(!"Unsupported destination code page"); |
| 410 return 0; |
| 411 } |
| 412 |
| 413 // the NULL terminator in Unicode is 0; compute the size of the terminator |
| 414 // in the destination coding by calling the PutChar routine once with |
| 415 // coding zero. |
| 416 OutputSize = PutChar(NULL, 0); |
| 417 if (OutputSize > DestSize) |
| 418 return 0; |
| 419 |
| 420 switch (SrcCodePage) |
| 421 { |
| 422 case NvOsCodePage_Utf8: GetChar = |
| 423 NvUStr_GetUtf8Coding; break; |
| 424 case NvOsCodePage_Utf16: GetChar = |
| 425 NvUStr_GetUtf16Coding; break; |
| 426 case NvOsCodePage_Windows1252: GetChar = |
| 427 NvUStr_GetWindows1252Coding; break; |
| 428 default: |
| 429 NV_ASSERT(!"Unsupported source code page"); |
| 430 return 0; |
| 431 } |
| 432 |
| 433 // optimized path for conversions of the lower 128 ASCII characters |
| 434 if (( (DestCodePage == NvOsCodePage_Utf8) || |
| 435 (DestCodePage == NvOsCodePage_Windows1252)) && |
| 436 (SrcCodePage == NvOsCodePage_Utf16)) |
| 437 { |
| 438 const NvU16 *pCh = (const NvU16 *)pSrc; |
| 439 while (*pCh && (*pCh<0x80) && (OutputSize < DestSize) && Remain) |
| 440 { |
| 441 if (pStr) |
| 442 *pStr++ = (char)*pCh; |
| 443 OutputSize++; |
| 444 Remain -= 2; |
| 445 pCh++; |
| 446 } |
| 447 pSrc = (const void *)pCh; |
| 448 } |
| 449 else if ((DestCodePage == NvOsCodePage_Utf16) && |
| 450 ( (SrcCodePage == NvOsCodePage_Utf8) || |
| 451 (SrcCodePage == NvOsCodePage_Windows1252))) |
| 452 { |
| 453 const NvU8 *pCh = (const NvU8 *)pSrc; |
| 454 wchar_t *pStrW = (wchar_t *)pStr; |
| 455 while (*pCh && (*pCh<0x80) && (OutputSize < DestSize) && Remain) |
| 456 { |
| 457 if (pStrW) |
| 458 *pStrW++ = (wchar_t)*pCh; |
| 459 OutputSize+=2; |
| 460 Remain--; |
| 461 pCh++; |
| 462 } |
| 463 pStr = (char *)pStrW; |
| 464 pSrc = (const void *)pCh; |
| 465 } |
| 466 |
| 467 pSrc = GetChar(pSrc, &Coding, &Remain); |
| 468 // All the GetChar* functions return a NULL coding when insufficient |
| 469 // source bytes remain, so we don't need to check it in the loop |
| 470 while (Coding) |
| 471 { |
| 472 CodeSize = PutChar(NULL, Coding); |
| 473 if (pStr) |
| 474 { |
| 475 if ((OutputSize + CodeSize)<=DestSize) |
| 476 { |
| 477 pStr += PutChar(pStr, Coding); |
| 478 OutputSize += CodeSize; |
| 479 } |
| 480 else |
| 481 break; |
| 482 } |
| 483 else |
| 484 OutputSize += CodeSize; |
| 485 |
| 486 pSrc = GetChar(pSrc, &Coding, &Remain); |
| 487 } |
| 488 if (pStr) |
| 489 { |
| 490 pStr += PutChar(pStr, 0); |
| 491 } |
| 492 |
| 493 return OutputSize; |
| 494 } |
| 495 |
| 496 NvU32 |
| 497 NvULowestBitSet( NvU32 bits, NvU32 nBits ) |
| 498 { |
| 499 NvU32 ret = 0; |
| 500 |
| 501 if( nBits > 16 ) |
| 502 { |
| 503 if( !(bits & 0xffff) ) |
| 504 { |
| 505 ret += 16; |
| 506 bits >>= 16; |
| 507 } |
| 508 } |
| 509 |
| 510 if( nBits > 8 ) |
| 511 { |
| 512 if( !(bits & 0xff) ) |
| 513 { |
| 514 ret += 8; |
| 515 bits >>= 8; |
| 516 } |
| 517 } |
| 518 |
| 519 if( !(bits & 0xf) ) |
| 520 { |
| 521 ret += 4; |
| 522 bits >>= 4; |
| 523 } |
| 524 |
| 525 if( !(bits & 0x3) ) |
| 526 { |
| 527 ret += 2; |
| 528 bits >>= 2; |
| 529 } |
| 530 |
| 531 return ret + ((bits & 1) ? 0 : 1 ); |
| 532 } |
OLD | NEW |