diff options
Diffstat (limited to '')
-rw-r--r-- | src/dutil/strutil.cpp | 215 |
1 files changed, 115 insertions, 100 deletions
diff --git a/src/dutil/strutil.cpp b/src/dutil/strutil.cpp index c1d701d3..daa090c9 100644 --- a/src/dutil/strutil.cpp +++ b/src/dutil/strutil.cpp | |||
@@ -2,6 +2,21 @@ | |||
2 | 2 | ||
3 | #include "precomp.h" | 3 | #include "precomp.h" |
4 | 4 | ||
5 | |||
6 | // Exit macros | ||
7 | #define StrExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_STRUTIL, x, s, __VA_ARGS__) | ||
8 | #define StrExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_STRUTIL, x, s, __VA_ARGS__) | ||
9 | #define StrExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_STRUTIL, x, s, __VA_ARGS__) | ||
10 | #define StrExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_STRUTIL, x, s, __VA_ARGS__) | ||
11 | #define StrExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_STRUTIL, x, s, __VA_ARGS__) | ||
12 | #define StrExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_STRUTIL, x, s, __VA_ARGS__) | ||
13 | #define StrExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_STRUTIL, p, x, e, s, __VA_ARGS__) | ||
14 | #define StrExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_STRUTIL, p, x, s, __VA_ARGS__) | ||
15 | #define StrExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_STRUTIL, p, x, e, s, __VA_ARGS__) | ||
16 | #define StrExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_STRUTIL, p, x, s, __VA_ARGS__) | ||
17 | #define StrExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_STRUTIL, e, x, s, __VA_ARGS__) | ||
18 | #define StrExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_STRUTIL, g, x, s, __VA_ARGS__) | ||
19 | |||
5 | #define ARRAY_GROWTH_SIZE 5 | 20 | #define ARRAY_GROWTH_SIZE 5 |
6 | 21 | ||
7 | // Forward declarations. | 22 | // Forward declarations. |
@@ -84,7 +99,7 @@ static HRESULT AllocHelper( | |||
84 | if (cch >= MAXDWORD / sizeof(WCHAR)) | 99 | if (cch >= MAXDWORD / sizeof(WCHAR)) |
85 | { | 100 | { |
86 | hr = E_OUTOFMEMORY; | 101 | hr = E_OUTOFMEMORY; |
87 | ExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); | 102 | StrExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); |
88 | } | 103 | } |
89 | 104 | ||
90 | if (*ppwz) | 105 | if (*ppwz) |
@@ -93,7 +108,7 @@ static HRESULT AllocHelper( | |||
93 | { | 108 | { |
94 | LPVOID pvNew = NULL; | 109 | LPVOID pvNew = NULL; |
95 | hr = MemReAllocSecure(*ppwz, sizeof(WCHAR)* cch, FALSE, &pvNew); | 110 | hr = MemReAllocSecure(*ppwz, sizeof(WCHAR)* cch, FALSE, &pvNew); |
96 | ExitOnFailure(hr, "Failed to reallocate string"); | 111 | StrExitOnFailure(hr, "Failed to reallocate string"); |
97 | pwz = static_cast<LPWSTR>(pvNew); | 112 | pwz = static_cast<LPWSTR>(pvNew); |
98 | } | 113 | } |
99 | else | 114 | else |
@@ -106,7 +121,7 @@ static HRESULT AllocHelper( | |||
106 | pwz = static_cast<LPWSTR>(MemAlloc(sizeof(WCHAR) * cch, TRUE)); | 121 | pwz = static_cast<LPWSTR>(MemAlloc(sizeof(WCHAR) * cch, TRUE)); |
107 | } | 122 | } |
108 | 123 | ||
109 | ExitOnNull(pwz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); | 124 | StrExitOnNull(pwz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); |
110 | 125 | ||
111 | *ppwz = pwz; | 126 | *ppwz = pwz; |
112 | LExit: | 127 | LExit: |
@@ -131,12 +146,12 @@ HRESULT DAPI StrTrimCapacity( | |||
131 | SIZE_T cchLen = 0; | 146 | SIZE_T cchLen = 0; |
132 | 147 | ||
133 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 148 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
134 | ExitOnFailure(hr, "Failed to calculate length of string"); | 149 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
135 | 150 | ||
136 | ++cchLen; // Add 1 for null-terminator | 151 | ++cchLen; // Add 1 for null-terminator |
137 | 152 | ||
138 | hr = StrAlloc(ppwz, cchLen); | 153 | hr = StrAlloc(ppwz, cchLen); |
139 | ExitOnFailure(hr, "Failed to reallocate string"); | 154 | StrExitOnFailure(hr, "Failed to reallocate string"); |
140 | 155 | ||
141 | LExit: | 156 | LExit: |
142 | return hr; | 157 | return hr; |
@@ -181,7 +196,7 @@ HRESULT DAPI StrTrimWhitespace( | |||
181 | } | 196 | } |
182 | 197 | ||
183 | hr = StrAllocString(&sczResult, wzSource, i); | 198 | hr = StrAllocString(&sczResult, wzSource, i); |
184 | ExitOnFailure(hr, "Failed to copy result string"); | 199 | StrExitOnFailure(hr, "Failed to copy result string"); |
185 | 200 | ||
186 | // Output result | 201 | // Output result |
187 | *ppwz = sczResult; | 202 | *ppwz = sczResult; |
@@ -212,7 +227,7 @@ extern "C" HRESULT DAPI StrAnsiAlloc( | |||
212 | if (cch >= MAXDWORD / sizeof(WCHAR)) | 227 | if (cch >= MAXDWORD / sizeof(WCHAR)) |
213 | { | 228 | { |
214 | hr = E_OUTOFMEMORY; | 229 | hr = E_OUTOFMEMORY; |
215 | ExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); | 230 | StrExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); |
216 | } | 231 | } |
217 | 232 | ||
218 | if (*ppsz) | 233 | if (*ppsz) |
@@ -224,7 +239,7 @@ extern "C" HRESULT DAPI StrAnsiAlloc( | |||
224 | psz = static_cast<LPSTR>(MemAlloc(sizeof(CHAR) * cch, TRUE)); | 239 | psz = static_cast<LPSTR>(MemAlloc(sizeof(CHAR) * cch, TRUE)); |
225 | } | 240 | } |
226 | 241 | ||
227 | ExitOnNull(psz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); | 242 | StrExitOnNull(psz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); |
228 | 243 | ||
229 | *ppsz = psz; | 244 | *ppsz = psz; |
230 | LExit: | 245 | LExit: |
@@ -252,12 +267,12 @@ HRESULT DAPI StrAnsiTrimCapacity( | |||
252 | #pragma prefast(disable:25068) | 267 | #pragma prefast(disable:25068) |
253 | hr = ::StringCchLengthA(*ppz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 268 | hr = ::StringCchLengthA(*ppz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
254 | #pragma prefast(pop) | 269 | #pragma prefast(pop) |
255 | ExitOnFailure(hr, "Failed to calculate length of string"); | 270 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
256 | 271 | ||
257 | ++cchLen; // Add 1 for null-terminator | 272 | ++cchLen; // Add 1 for null-terminator |
258 | 273 | ||
259 | hr = StrAnsiAlloc(ppz, cchLen); | 274 | hr = StrAnsiAlloc(ppz, cchLen); |
260 | ExitOnFailure(hr, "Failed to reallocate string"); | 275 | StrExitOnFailure(hr, "Failed to reallocate string"); |
261 | 276 | ||
262 | LExit: | 277 | LExit: |
263 | return hr; | 278 | return hr; |
@@ -302,7 +317,7 @@ HRESULT DAPI StrAnsiTrimWhitespace( | |||
302 | } | 317 | } |
303 | 318 | ||
304 | hr = StrAnsiAllocStringAnsi(&sczResult, szSource, i); | 319 | hr = StrAnsiAllocStringAnsi(&sczResult, szSource, i); |
305 | ExitOnFailure(hr, "Failed to copy result string"); | 320 | StrExitOnFailure(hr, "Failed to copy result string"); |
306 | 321 | ||
307 | // Output result | 322 | // Output result |
308 | *ppz = sczResult; | 323 | *ppz = sczResult; |
@@ -375,7 +390,7 @@ static HRESULT AllocStringHelper( | |||
375 | if (-1 == cch) | 390 | if (-1 == cch) |
376 | { | 391 | { |
377 | hr = E_INVALIDARG; | 392 | hr = E_INVALIDARG; |
378 | ExitOnFailure(hr, "failed to get size of destination string"); | 393 | StrExitOnFailure(hr, "failed to get size of destination string"); |
379 | } | 394 | } |
380 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | 395 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters |
381 | } | 396 | } |
@@ -387,13 +402,13 @@ static HRESULT AllocStringHelper( | |||
387 | 402 | ||
388 | SIZE_T cchNeeded; | 403 | SIZE_T cchNeeded; |
389 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator | 404 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator |
390 | ExitOnFailure(hr, "source string is too long"); | 405 | StrExitOnFailure(hr, "source string is too long"); |
391 | 406 | ||
392 | if (cch < cchNeeded) | 407 | if (cch < cchNeeded) |
393 | { | 408 | { |
394 | cch = cchNeeded; | 409 | cch = cchNeeded; |
395 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); | 410 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); |
396 | ExitOnFailure(hr, "failed to allocate string from string."); | 411 | StrExitOnFailure(hr, "failed to allocate string from string."); |
397 | } | 412 | } |
398 | 413 | ||
399 | // copy everything (the NULL terminator will be included) | 414 | // copy everything (the NULL terminator will be included) |
@@ -431,7 +446,7 @@ extern "C" HRESULT DAPI StrAnsiAllocString( | |||
431 | if (-1 == cch) | 446 | if (-1 == cch) |
432 | { | 447 | { |
433 | hr = E_INVALIDARG; | 448 | hr = E_INVALIDARG; |
434 | ExitOnFailure(hr, "failed to get size of destination string"); | 449 | StrExitOnFailure(hr, "failed to get size of destination string"); |
435 | } | 450 | } |
436 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | 451 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters |
437 | } | 452 | } |
@@ -441,7 +456,7 @@ extern "C" HRESULT DAPI StrAnsiAllocString( | |||
441 | cchDest = ::WideCharToMultiByte(uiCodepage, 0, wzSource, -1, NULL, 0, NULL, NULL); | 456 | cchDest = ::WideCharToMultiByte(uiCodepage, 0, wzSource, -1, NULL, 0, NULL, NULL); |
442 | if (0 == cchDest) | 457 | if (0 == cchDest) |
443 | { | 458 | { |
444 | ExitWithLastError(hr, "failed to get required size for conversion to ANSI: %ls", wzSource); | 459 | StrExitWithLastError(hr, "failed to get required size for conversion to ANSI: %ls", wzSource); |
445 | } | 460 | } |
446 | 461 | ||
447 | --cchDest; // subtract one because WideChageToMultiByte includes space for the NULL terminator that we track below | 462 | --cchDest; // subtract one because WideChageToMultiByte includes space for the NULL terminator that we track below |
@@ -457,7 +472,7 @@ extern "C" HRESULT DAPI StrAnsiAllocString( | |||
457 | if (cch >= MAXDWORD / sizeof(WCHAR)) | 472 | if (cch >= MAXDWORD / sizeof(WCHAR)) |
458 | { | 473 | { |
459 | hr = E_OUTOFMEMORY; | 474 | hr = E_OUTOFMEMORY; |
460 | ExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); | 475 | StrExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); |
461 | } | 476 | } |
462 | 477 | ||
463 | if (*ppsz) | 478 | if (*ppsz) |
@@ -468,14 +483,14 @@ extern "C" HRESULT DAPI StrAnsiAllocString( | |||
468 | { | 483 | { |
469 | psz = static_cast<LPSTR>(MemAlloc(sizeof(CHAR) * cch, TRUE)); | 484 | psz = static_cast<LPSTR>(MemAlloc(sizeof(CHAR) * cch, TRUE)); |
470 | } | 485 | } |
471 | ExitOnNull(psz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); | 486 | StrExitOnNull(psz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); |
472 | 487 | ||
473 | *ppsz = psz; | 488 | *ppsz = psz; |
474 | } | 489 | } |
475 | 490 | ||
476 | if (0 == ::WideCharToMultiByte(uiCodepage, 0, wzSource, 0 == cchSource ? -1 : (int)cchSource, *ppsz, (int)cch, NULL, NULL)) | 491 | if (0 == ::WideCharToMultiByte(uiCodepage, 0, wzSource, 0 == cchSource ? -1 : (int)cchSource, *ppsz, (int)cch, NULL, NULL)) |
477 | { | 492 | { |
478 | ExitWithLastError(hr, "failed to convert to ansi: %ls", wzSource); | 493 | StrExitWithLastError(hr, "failed to convert to ansi: %ls", wzSource); |
479 | } | 494 | } |
480 | (*ppsz)[cchDest] = L'\0'; | 495 | (*ppsz)[cchDest] = L'\0'; |
481 | 496 | ||
@@ -511,7 +526,7 @@ extern "C" HRESULT DAPI StrAllocStringAnsi( | |||
511 | if (-1 == cch) | 526 | if (-1 == cch) |
512 | { | 527 | { |
513 | hr = E_INVALIDARG; | 528 | hr = E_INVALIDARG; |
514 | ExitOnFailure(hr, "failed to get size of destination string"); | 529 | StrExitOnFailure(hr, "failed to get size of destination string"); |
515 | } | 530 | } |
516 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | 531 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters |
517 | } | 532 | } |
@@ -521,7 +536,7 @@ extern "C" HRESULT DAPI StrAllocStringAnsi( | |||
521 | cchDest = ::MultiByteToWideChar(uiCodepage, 0, szSource, -1, NULL, 0); | 536 | cchDest = ::MultiByteToWideChar(uiCodepage, 0, szSource, -1, NULL, 0); |
522 | if (0 == cchDest) | 537 | if (0 == cchDest) |
523 | { | 538 | { |
524 | ExitWithLastError(hr, "failed to get required size for conversion to unicode: %s", szSource); | 539 | StrExitWithLastError(hr, "failed to get required size for conversion to unicode: %s", szSource); |
525 | } | 540 | } |
526 | 541 | ||
527 | --cchDest; //subtract one because MultiByteToWideChar includes space for the NULL terminator that we track below | 542 | --cchDest; //subtract one because MultiByteToWideChar includes space for the NULL terminator that we track below |
@@ -537,7 +552,7 @@ extern "C" HRESULT DAPI StrAllocStringAnsi( | |||
537 | if (cch >= MAXDWORD / sizeof(WCHAR)) | 552 | if (cch >= MAXDWORD / sizeof(WCHAR)) |
538 | { | 553 | { |
539 | hr = E_OUTOFMEMORY; | 554 | hr = E_OUTOFMEMORY; |
540 | ExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); | 555 | StrExitOnFailure(hr, "Not enough memory to allocate string of size: %u", cch); |
541 | } | 556 | } |
542 | 557 | ||
543 | if (*ppwz) | 558 | if (*ppwz) |
@@ -549,14 +564,14 @@ extern "C" HRESULT DAPI StrAllocStringAnsi( | |||
549 | pwz = static_cast<LPWSTR>(MemAlloc(sizeof(WCHAR) * cch, TRUE)); | 564 | pwz = static_cast<LPWSTR>(MemAlloc(sizeof(WCHAR) * cch, TRUE)); |
550 | } | 565 | } |
551 | 566 | ||
552 | ExitOnNull(pwz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); | 567 | StrExitOnNull(pwz, hr, E_OUTOFMEMORY, "failed to allocate string, len: %u", cch); |
553 | 568 | ||
554 | *ppwz = pwz; | 569 | *ppwz = pwz; |
555 | } | 570 | } |
556 | 571 | ||
557 | if (0 == ::MultiByteToWideChar(uiCodepage, 0, szSource, 0 == cchSource ? -1 : (int)cchSource, *ppwz, (int)cch)) | 572 | if (0 == ::MultiByteToWideChar(uiCodepage, 0, szSource, 0 == cchSource ? -1 : (int)cchSource, *ppwz, (int)cch)) |
558 | { | 573 | { |
559 | ExitWithLastError(hr, "failed to convert to unicode: %s", szSource); | 574 | StrExitWithLastError(hr, "failed to convert to unicode: %s", szSource); |
560 | } | 575 | } |
561 | (*ppwz)[cchDest] = L'\0'; | 576 | (*ppwz)[cchDest] = L'\0'; |
562 | 577 | ||
@@ -589,7 +604,7 @@ HRESULT DAPI StrAnsiAllocStringAnsi( | |||
589 | if (-1 == cch) | 604 | if (-1 == cch) |
590 | { | 605 | { |
591 | hr = E_INVALIDARG; | 606 | hr = E_INVALIDARG; |
592 | ExitOnFailure(hr, "failed to get size of destination string"); | 607 | StrExitOnFailure(hr, "failed to get size of destination string"); |
593 | } | 608 | } |
594 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | 609 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters |
595 | } | 610 | } |
@@ -601,13 +616,13 @@ HRESULT DAPI StrAnsiAllocStringAnsi( | |||
601 | 616 | ||
602 | SIZE_T cchNeeded; | 617 | SIZE_T cchNeeded; |
603 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator | 618 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator |
604 | ExitOnFailure(hr, "source string is too long"); | 619 | StrExitOnFailure(hr, "source string is too long"); |
605 | 620 | ||
606 | if (cch < cchNeeded) | 621 | if (cch < cchNeeded) |
607 | { | 622 | { |
608 | cch = cchNeeded; | 623 | cch = cchNeeded; |
609 | hr = StrAnsiAlloc(ppsz, cch); | 624 | hr = StrAnsiAlloc(ppsz, cch); |
610 | ExitOnFailure(hr, "failed to allocate string from string."); | 625 | StrExitOnFailure(hr, "failed to allocate string from string."); |
611 | } | 626 | } |
612 | 627 | ||
613 | // copy everything (the NULL terminator will be included) | 628 | // copy everything (the NULL terminator will be included) |
@@ -647,12 +662,12 @@ extern "C" HRESULT DAPI StrAllocPrefix( | |||
647 | if (-1 == cch) | 662 | if (-1 == cch) |
648 | { | 663 | { |
649 | hr = E_INVALIDARG; | 664 | hr = E_INVALIDARG; |
650 | ExitOnFailure(hr, "failed to get size of destination string"); | 665 | StrExitOnFailure(hr, "failed to get size of destination string"); |
651 | } | 666 | } |
652 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | 667 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters |
653 | 668 | ||
654 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 669 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
655 | ExitOnFailure(hr, "Failed to calculate length of string"); | 670 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
656 | } | 671 | } |
657 | 672 | ||
658 | Assert(cchLen <= cch); | 673 | Assert(cchLen <= cch); |
@@ -660,14 +675,14 @@ extern "C" HRESULT DAPI StrAllocPrefix( | |||
660 | if (0 == cchPrefix) | 675 | if (0 == cchPrefix) |
661 | { | 676 | { |
662 | hr = ::StringCchLengthW(wzPrefix, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchPrefix)); | 677 | hr = ::StringCchLengthW(wzPrefix, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchPrefix)); |
663 | ExitOnFailure(hr, "Failed to calculate length of string"); | 678 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
664 | } | 679 | } |
665 | 680 | ||
666 | if (cch - cchLen < cchPrefix + 1) | 681 | if (cch - cchLen < cchPrefix + 1) |
667 | { | 682 | { |
668 | cch = cchPrefix + cchLen + 1; | 683 | cch = cchPrefix + cchLen + 1; |
669 | hr = StrAlloc(ppwz, cch); | 684 | hr = StrAlloc(ppwz, cch); |
670 | ExitOnFailure(hr, "failed to allocate string from string: %ls", wzPrefix); | 685 | StrExitOnFailure(hr, "failed to allocate string from string: %ls", wzPrefix); |
671 | } | 686 | } |
672 | 687 | ||
673 | if (*ppwz) | 688 | if (*ppwz) |
@@ -681,7 +696,7 @@ extern "C" HRESULT DAPI StrAllocPrefix( | |||
681 | else | 696 | else |
682 | { | 697 | { |
683 | hr = E_UNEXPECTED; | 698 | hr = E_UNEXPECTED; |
684 | ExitOnFailure(hr, "for some reason our buffer is still null"); | 699 | StrExitOnFailure(hr, "for some reason our buffer is still null"); |
685 | } | 700 | } |
686 | 701 | ||
687 | LExit: | 702 | LExit: |
@@ -753,12 +768,12 @@ static HRESULT AllocConcatHelper( | |||
753 | if (-1 == cch) | 768 | if (-1 == cch) |
754 | { | 769 | { |
755 | hr = E_INVALIDARG; | 770 | hr = E_INVALIDARG; |
756 | ExitOnFailure(hr, "failed to get size of destination string"); | 771 | StrExitOnFailure(hr, "failed to get size of destination string"); |
757 | } | 772 | } |
758 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | 773 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters |
759 | 774 | ||
760 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 775 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
761 | ExitOnFailure(hr, "Failed to calculate length of string"); | 776 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
762 | } | 777 | } |
763 | 778 | ||
764 | Assert(cchLen <= cch); | 779 | Assert(cchLen <= cch); |
@@ -766,14 +781,14 @@ static HRESULT AllocConcatHelper( | |||
766 | if (0 == cchSource) | 781 | if (0 == cchSource) |
767 | { | 782 | { |
768 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchSource)); | 783 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchSource)); |
769 | ExitOnFailure(hr, "Failed to calculate length of string"); | 784 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
770 | } | 785 | } |
771 | 786 | ||
772 | if (cch - cchLen < cchSource + 1) | 787 | if (cch - cchLen < cchSource + 1) |
773 | { | 788 | { |
774 | cch = (cchSource + cchLen + 1) * 2; | 789 | cch = (cchSource + cchLen + 1) * 2; |
775 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); | 790 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); |
776 | ExitOnFailure(hr, "failed to allocate string from string: %ls", wzSource); | 791 | StrExitOnFailure(hr, "failed to allocate string from string: %ls", wzSource); |
777 | } | 792 | } |
778 | 793 | ||
779 | if (*ppwz) | 794 | if (*ppwz) |
@@ -783,7 +798,7 @@ static HRESULT AllocConcatHelper( | |||
783 | else | 798 | else |
784 | { | 799 | { |
785 | hr = E_UNEXPECTED; | 800 | hr = E_UNEXPECTED; |
786 | ExitOnFailure(hr, "for some reason our buffer is still null"); | 801 | StrExitOnFailure(hr, "for some reason our buffer is still null"); |
787 | } | 802 | } |
788 | 803 | ||
789 | LExit: | 804 | LExit: |
@@ -816,7 +831,7 @@ extern "C" HRESULT DAPI StrAnsiAllocConcat( | |||
816 | if (-1 == cch) | 831 | if (-1 == cch) |
817 | { | 832 | { |
818 | hr = E_INVALIDARG; | 833 | hr = E_INVALIDARG; |
819 | ExitOnFailure(hr, "failed to get size of destination string"); | 834 | StrExitOnFailure(hr, "failed to get size of destination string"); |
820 | } | 835 | } |
821 | cch /= sizeof(CHAR); // convert the count in bytes to count in characters | 836 | cch /= sizeof(CHAR); // convert the count in bytes to count in characters |
822 | 837 | ||
@@ -824,7 +839,7 @@ extern "C" HRESULT DAPI StrAnsiAllocConcat( | |||
824 | #pragma prefast(disable:25068) | 839 | #pragma prefast(disable:25068) |
825 | hr = ::StringCchLengthA(*ppz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 840 | hr = ::StringCchLengthA(*ppz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
826 | #pragma prefast(pop) | 841 | #pragma prefast(pop) |
827 | ExitOnFailure(hr, "Failed to calculate length of string"); | 842 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
828 | } | 843 | } |
829 | 844 | ||
830 | Assert(cchLen <= cch); | 845 | Assert(cchLen <= cch); |
@@ -835,14 +850,14 @@ extern "C" HRESULT DAPI StrAnsiAllocConcat( | |||
835 | #pragma prefast(disable:25068) | 850 | #pragma prefast(disable:25068) |
836 | hr = ::StringCchLengthA(pzSource, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchSource)); | 851 | hr = ::StringCchLengthA(pzSource, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchSource)); |
837 | #pragma prefast(pop) | 852 | #pragma prefast(pop) |
838 | ExitOnFailure(hr, "Failed to calculate length of string"); | 853 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
839 | } | 854 | } |
840 | 855 | ||
841 | if (cch - cchLen < cchSource + 1) | 856 | if (cch - cchLen < cchSource + 1) |
842 | { | 857 | { |
843 | cch = (cchSource + cchLen + 1) * 2; | 858 | cch = (cchSource + cchLen + 1) * 2; |
844 | hr = StrAnsiAlloc(ppz, cch); | 859 | hr = StrAnsiAlloc(ppz, cch); |
845 | ExitOnFailure(hr, "failed to allocate string from string: %hs", pzSource); | 860 | StrExitOnFailure(hr, "failed to allocate string from string: %hs", pzSource); |
846 | } | 861 | } |
847 | 862 | ||
848 | if (*ppz) | 863 | if (*ppz) |
@@ -855,7 +870,7 @@ extern "C" HRESULT DAPI StrAnsiAllocConcat( | |||
855 | else | 870 | else |
856 | { | 871 | { |
857 | hr = E_UNEXPECTED; | 872 | hr = E_UNEXPECTED; |
858 | ExitOnFailure(hr, "for some reason our buffer is still null"); | 873 | StrExitOnFailure(hr, "for some reason our buffer is still null"); |
859 | } | 874 | } |
860 | 875 | ||
861 | LExit: | 876 | LExit: |
@@ -908,7 +923,7 @@ extern "C" HRESULT __cdecl StrAllocConcatFormatted( | |||
908 | va_start(args, wzFormat); | 923 | va_start(args, wzFormat); |
909 | hr = StrAllocFormattedArgs(&sczFormatted, wzFormat, args); | 924 | hr = StrAllocFormattedArgs(&sczFormatted, wzFormat, args); |
910 | va_end(args); | 925 | va_end(args); |
911 | ExitOnFailure(hr, "Failed to allocate formatted string"); | 926 | StrExitOnFailure(hr, "Failed to allocate formatted string"); |
912 | 927 | ||
913 | hr = StrAllocConcat(ppwz, sczFormatted, 0); | 928 | hr = StrAllocConcat(ppwz, sczFormatted, 0); |
914 | 929 | ||
@@ -942,7 +957,7 @@ extern "C" HRESULT __cdecl StrAllocConcatFormattedSecure( | |||
942 | va_start(args, wzFormat); | 957 | va_start(args, wzFormat); |
943 | hr = StrAllocFormattedArgsSecure(&sczFormatted, wzFormat, args); | 958 | hr = StrAllocFormattedArgsSecure(&sczFormatted, wzFormat, args); |
944 | va_end(args); | 959 | va_end(args); |
945 | ExitOnFailure(hr, "Failed to allocate formatted string"); | 960 | StrExitOnFailure(hr, "Failed to allocate formatted string"); |
946 | 961 | ||
947 | hr = StrAllocConcatSecure(ppwz, sczFormatted, 0); | 962 | hr = StrAllocConcatSecure(ppwz, sczFormatted, 0); |
948 | 963 | ||
@@ -1068,7 +1083,7 @@ static HRESULT AllocFormattedArgsHelper( | |||
1068 | if (-1 == cbOriginal) | 1083 | if (-1 == cbOriginal) |
1069 | { | 1084 | { |
1070 | hr = E_INVALIDARG; | 1085 | hr = E_INVALIDARG; |
1071 | ExitOnFailure(hr, "failed to get size of destination string"); | 1086 | StrExitOnFailure(hr, "failed to get size of destination string"); |
1072 | } | 1087 | } |
1073 | 1088 | ||
1074 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters | 1089 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters |
@@ -1080,7 +1095,7 @@ static HRESULT AllocFormattedArgsHelper( | |||
1080 | cch = 256; | 1095 | cch = 256; |
1081 | 1096 | ||
1082 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); | 1097 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); |
1083 | ExitOnFailure(hr, "failed to allocate string to format: %ls", wzFormat); | 1098 | StrExitOnFailure(hr, "failed to allocate string to format: %ls", wzFormat); |
1084 | } | 1099 | } |
1085 | 1100 | ||
1086 | // format the message (grow until it fits or there is a failure) | 1101 | // format the message (grow until it fits or there is a failure) |
@@ -1104,12 +1119,12 @@ static HRESULT AllocFormattedArgsHelper( | |||
1104 | cch *= 2; | 1119 | cch *= 2; |
1105 | 1120 | ||
1106 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); | 1121 | hr = AllocHelper(ppwz, cch, fZeroOnRealloc); |
1107 | ExitOnFailure(hr, "failed to allocate string to format: %ls", wzFormat); | 1122 | StrExitOnFailure(hr, "failed to allocate string to format: %ls", wzFormat); |
1108 | 1123 | ||
1109 | hr = S_FALSE; | 1124 | hr = S_FALSE; |
1110 | } | 1125 | } |
1111 | } while (S_FALSE == hr); | 1126 | } while (S_FALSE == hr); |
1112 | ExitOnFailure(hr, "failed to format string"); | 1127 | StrExitOnFailure(hr, "failed to format string"); |
1113 | 1128 | ||
1114 | LExit: | 1129 | LExit: |
1115 | if (pwzOriginal && fZeroOnRealloc) | 1130 | if (pwzOriginal && fZeroOnRealloc) |
@@ -1148,7 +1163,7 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
1148 | if (-1 == cch) | 1163 | if (-1 == cch) |
1149 | { | 1164 | { |
1150 | hr = E_INVALIDARG; | 1165 | hr = E_INVALIDARG; |
1151 | ExitOnFailure(hr, "failed to get size of destination string"); | 1166 | StrExitOnFailure(hr, "failed to get size of destination string"); |
1152 | } | 1167 | } |
1153 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | 1168 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters |
1154 | 1169 | ||
@@ -1159,7 +1174,7 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
1159 | { | 1174 | { |
1160 | cch = 256; | 1175 | cch = 256; |
1161 | hr = StrAnsiAlloc(ppsz, cch); | 1176 | hr = StrAnsiAlloc(ppsz, cch); |
1162 | ExitOnFailure(hr, "failed to allocate string to format: %s", szFormat); | 1177 | StrExitOnFailure(hr, "failed to allocate string to format: %s", szFormat); |
1163 | } | 1178 | } |
1164 | 1179 | ||
1165 | // format the message (grow until it fits or there is a failure) | 1180 | // format the message (grow until it fits or there is a failure) |
@@ -1183,11 +1198,11 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
1183 | } | 1198 | } |
1184 | cch *= 2; | 1199 | cch *= 2; |
1185 | hr = StrAnsiAlloc(ppsz, cch); | 1200 | hr = StrAnsiAlloc(ppsz, cch); |
1186 | ExitOnFailure(hr, "failed to allocate string to format: %hs", szFormat); | 1201 | StrExitOnFailure(hr, "failed to allocate string to format: %hs", szFormat); |
1187 | hr = S_FALSE; | 1202 | hr = S_FALSE; |
1188 | } | 1203 | } |
1189 | } while (S_FALSE == hr); | 1204 | } while (S_FALSE == hr); |
1190 | ExitOnFailure(hr, "failed to format string"); | 1205 | StrExitOnFailure(hr, "failed to format string"); |
1191 | 1206 | ||
1192 | LExit: | 1207 | LExit: |
1193 | ReleaseStr(pszOriginal); | 1208 | ReleaseStr(pszOriginal); |
@@ -1224,11 +1239,11 @@ extern "C" HRESULT DAPI StrAllocFromError( | |||
1224 | 1239 | ||
1225 | if (0 == cchMessage) | 1240 | if (0 == cchMessage) |
1226 | { | 1241 | { |
1227 | ExitWithLastError(hr, "Failed to format message for error: 0x%x", hrError); | 1242 | StrExitWithLastError(hr, "Failed to format message for error: 0x%x", hrError); |
1228 | } | 1243 | } |
1229 | 1244 | ||
1230 | hr = StrAllocString(ppwzMessage, reinterpret_cast<LPCWSTR>(pvMessage), cchMessage); | 1245 | hr = StrAllocString(ppwzMessage, reinterpret_cast<LPCWSTR>(pvMessage), cchMessage); |
1231 | ExitOnFailure(hr, "Failed to allocate string for message."); | 1246 | StrExitOnFailure(hr, "Failed to allocate string for message."); |
1232 | 1247 | ||
1233 | LExit: | 1248 | LExit: |
1234 | if (pvMessage) | 1249 | if (pvMessage) |
@@ -1308,7 +1323,7 @@ extern "C" HRESULT DAPI StrFree( | |||
1308 | Assert(p); | 1323 | Assert(p); |
1309 | 1324 | ||
1310 | HRESULT hr = MemFree(p); | 1325 | HRESULT hr = MemFree(p); |
1311 | ExitOnFailure(hr, "failed to free string"); | 1326 | StrExitOnFailure(hr, "failed to free string"); |
1312 | 1327 | ||
1313 | LExit: | 1328 | LExit: |
1314 | return hr; | 1329 | return hr; |
@@ -1332,7 +1347,7 @@ extern "C" HRESULT DAPI StrReplaceStringAll( | |||
1332 | do | 1347 | do |
1333 | { | 1348 | { |
1334 | hr = StrReplaceString(ppwzOriginal, &dwStartIndex, wzOldSubString, wzNewSubString); | 1349 | hr = StrReplaceString(ppwzOriginal, &dwStartIndex, wzOldSubString, wzNewSubString); |
1335 | ExitOnFailure(hr, "Failed to replace substring"); | 1350 | StrExitOnFailure(hr, "Failed to replace substring"); |
1336 | } | 1351 | } |
1337 | while (S_OK == hr); | 1352 | while (S_OK == hr); |
1338 | 1353 | ||
@@ -1373,21 +1388,21 @@ extern "C" HRESULT DAPI StrReplaceString( | |||
1373 | } | 1388 | } |
1374 | 1389 | ||
1375 | hr = ::PtrdiffTToDWord(wzSubLocation - *ppwzOriginal, pdwStartIndex); | 1390 | hr = ::PtrdiffTToDWord(wzSubLocation - *ppwzOriginal, pdwStartIndex); |
1376 | ExitOnFailure(hr, "Failed to diff pointers."); | 1391 | StrExitOnFailure(hr, "Failed to diff pointers."); |
1377 | 1392 | ||
1378 | hr = StrAllocString(&pwzBuffer, *ppwzOriginal, wzSubLocation - *ppwzOriginal); | 1393 | hr = StrAllocString(&pwzBuffer, *ppwzOriginal, wzSubLocation - *ppwzOriginal); |
1379 | ExitOnFailure(hr, "Failed to duplicate string."); | 1394 | StrExitOnFailure(hr, "Failed to duplicate string."); |
1380 | 1395 | ||
1381 | pwzBuffer[wzSubLocation - *ppwzOriginal] = '\0'; | 1396 | pwzBuffer[wzSubLocation - *ppwzOriginal] = '\0'; |
1382 | 1397 | ||
1383 | hr = StrAllocConcat(&pwzBuffer, wzNewSubString, 0); | 1398 | hr = StrAllocConcat(&pwzBuffer, wzNewSubString, 0); |
1384 | ExitOnFailure(hr, "Failed to append new string."); | 1399 | StrExitOnFailure(hr, "Failed to append new string."); |
1385 | 1400 | ||
1386 | hr = StrAllocConcat(&pwzBuffer, wzSubLocation + wcslen(wzOldSubString), 0); | 1401 | hr = StrAllocConcat(&pwzBuffer, wzSubLocation + wcslen(wzOldSubString), 0); |
1387 | ExitOnFailure(hr, "Failed to append post string."); | 1402 | StrExitOnFailure(hr, "Failed to append post string."); |
1388 | 1403 | ||
1389 | hr = StrFree(*ppwzOriginal); | 1404 | hr = StrFree(*ppwzOriginal); |
1390 | ExitOnFailure(hr, "Failed to free original string."); | 1405 | StrExitOnFailure(hr, "Failed to free original string."); |
1391 | 1406 | ||
1392 | *ppwzOriginal = pwzBuffer; | 1407 | *ppwzOriginal = pwzBuffer; |
1393 | *pdwStartIndex = *pdwStartIndex + static_cast<DWORD>(wcslen(wzNewSubString)); | 1408 | *pdwStartIndex = *pdwStartIndex + static_cast<DWORD>(wcslen(wzNewSubString)); |
@@ -1477,10 +1492,10 @@ HRESULT DAPI StrAllocHexEncode( | |||
1477 | SIZE_T cchSource = sizeof(WCHAR) * (cbSource + 1); | 1492 | SIZE_T cchSource = sizeof(WCHAR) * (cbSource + 1); |
1478 | 1493 | ||
1479 | hr = StrAlloc(ppwzDest, cchSource); | 1494 | hr = StrAlloc(ppwzDest, cchSource); |
1480 | ExitOnFailure(hr, "Failed to allocate hex string."); | 1495 | StrExitOnFailure(hr, "Failed to allocate hex string."); |
1481 | 1496 | ||
1482 | hr = StrHexEncode(pbSource, cbSource, *ppwzDest, cchSource); | 1497 | hr = StrHexEncode(pbSource, cbSource, *ppwzDest, cchSource); |
1483 | ExitOnFailure(hr, "Failed to encode hex string."); | 1498 | StrExitOnFailure(hr, "Failed to encode hex string."); |
1484 | 1499 | ||
1485 | LExit: | 1500 | LExit: |
1486 | return hr; | 1501 | return hr; |
@@ -1509,7 +1524,7 @@ extern "C" HRESULT DAPI StrHexDecode( | |||
1509 | if (cbDest < cchSource / 2) | 1524 | if (cbDest < cchSource / 2) |
1510 | { | 1525 | { |
1511 | hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); | 1526 | hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); |
1512 | ExitOnRootFailure(hr, "Insufficient buffer to decode string '%ls' len: %u into %u bytes.", wzSource, cchSource, cbDest); | 1527 | StrExitOnRootFailure(hr, "Insufficient buffer to decode string '%ls' len: %u into %u bytes.", wzSource, cchSource, cbDest); |
1513 | } | 1528 | } |
1514 | 1529 | ||
1515 | for (i = 0; i < cchSource / 2; ++i) | 1530 | for (i = 0; i < cchSource / 2; ++i) |
@@ -1547,20 +1562,20 @@ extern "C" HRESULT DAPI StrAllocHexDecode( | |||
1547 | DWORD cb = 0; | 1562 | DWORD cb = 0; |
1548 | 1563 | ||
1549 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, &cch); | 1564 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, &cch); |
1550 | ExitOnFailure(hr, "Failed to calculate length of source string."); | 1565 | StrExitOnFailure(hr, "Failed to calculate length of source string."); |
1551 | 1566 | ||
1552 | if (cch % 2) | 1567 | if (cch % 2) |
1553 | { | 1568 | { |
1554 | hr = E_INVALIDARG; | 1569 | hr = E_INVALIDARG; |
1555 | ExitOnFailure(hr, "Invalid source parameter, string must be even length or it cannot be decoded."); | 1570 | StrExitOnFailure(hr, "Invalid source parameter, string must be even length or it cannot be decoded."); |
1556 | } | 1571 | } |
1557 | 1572 | ||
1558 | cb = static_cast<DWORD>(cch / 2); | 1573 | cb = static_cast<DWORD>(cch / 2); |
1559 | pb = static_cast<BYTE*>(MemAlloc(cb, TRUE)); | 1574 | pb = static_cast<BYTE*>(MemAlloc(cb, TRUE)); |
1560 | ExitOnNull(pb, hr, E_OUTOFMEMORY, "Failed to allocate memory for hex decode."); | 1575 | StrExitOnNull(pb, hr, E_OUTOFMEMORY, "Failed to allocate memory for hex decode."); |
1561 | 1576 | ||
1562 | hr = StrHexDecode(wzSource, pb, cb); | 1577 | hr = StrHexDecode(wzSource, pb, cb); |
1563 | ExitOnFailure(hr, "Failed to decode source string."); | 1578 | StrExitOnFailure(hr, "Failed to decode source string."); |
1564 | 1579 | ||
1565 | *ppbDest = pb; | 1580 | *ppbDest = pb; |
1566 | pb = NULL; | 1581 | pb = NULL; |
@@ -1637,7 +1652,7 @@ extern "C" HRESULT DAPI StrAllocBase85Encode( | |||
1637 | ++cchDest; // add room for null terminator | 1652 | ++cchDest; // add room for null terminator |
1638 | 1653 | ||
1639 | hr = StrAlloc(pwzDest, cchDest); | 1654 | hr = StrAlloc(pwzDest, cchDest); |
1640 | ExitOnFailure(hr, "failed to allocate destination string"); | 1655 | StrExitOnFailure(hr, "failed to allocate destination string"); |
1641 | 1656 | ||
1642 | wzDest = *pwzDest; | 1657 | wzDest = *pwzDest; |
1643 | 1658 | ||
@@ -1740,7 +1755,7 @@ extern "C" HRESULT DAPI StrAllocBase85Decode( | |||
1740 | } | 1755 | } |
1741 | 1756 | ||
1742 | *ppbDest = static_cast<BYTE*>(MemAlloc(cbDest, FALSE)); | 1757 | *ppbDest = static_cast<BYTE*>(MemAlloc(cbDest, FALSE)); |
1743 | ExitOnNull(*ppbDest, hr, E_OUTOFMEMORY, "failed allocate memory to decode the string"); | 1758 | StrExitOnNull(*ppbDest, hr, E_OUTOFMEMORY, "failed allocate memory to decode the string"); |
1744 | 1759 | ||
1745 | pbDest = *ppbDest; | 1760 | pbDest = *ppbDest; |
1746 | *pcbDest = cbDest; | 1761 | *pcbDest = cbDest; |
@@ -1860,7 +1875,7 @@ extern "C" HRESULT DAPI MultiSzLen( | |||
1860 | DWORD_PTR dwMaxSize = 0; | 1875 | DWORD_PTR dwMaxSize = 0; |
1861 | 1876 | ||
1862 | hr = StrMaxLength(pwzMultiSz, &dwMaxSize); | 1877 | hr = StrMaxLength(pwzMultiSz, &dwMaxSize); |
1863 | ExitOnFailure(hr, "failed to get the max size of a string while calculating MULTISZ length"); | 1878 | StrExitOnFailure(hr, "failed to get the max size of a string while calculating MULTISZ length"); |
1864 | 1879 | ||
1865 | *pcch = 0; | 1880 | *pcch = 0; |
1866 | while (*pcch < dwMaxSize) | 1881 | while (*pcch < dwMaxSize) |
@@ -1914,7 +1929,7 @@ extern "C" HRESULT DAPI MultiSzPrepend( | |||
1914 | else | 1929 | else |
1915 | { | 1930 | { |
1916 | hr = MultiSzLen(*ppwzMultiSz, &cchMultiSz); | 1931 | hr = MultiSzLen(*ppwzMultiSz, &cchMultiSz); |
1917 | ExitOnFailure(hr, "failed to get length of multisz"); | 1932 | StrExitOnFailure(hr, "failed to get length of multisz"); |
1918 | } | 1933 | } |
1919 | 1934 | ||
1920 | cchInsert = lstrlenW(pwzInsert); | 1935 | cchInsert = lstrlenW(pwzInsert); |
@@ -1923,11 +1938,11 @@ extern "C" HRESULT DAPI MultiSzPrepend( | |||
1923 | 1938 | ||
1924 | // Allocate the result buffer | 1939 | // Allocate the result buffer |
1925 | hr = StrAlloc(&pwzResult, cchResult + 1); | 1940 | hr = StrAlloc(&pwzResult, cchResult + 1); |
1926 | ExitOnFailure(hr, "failed to allocate result string"); | 1941 | StrExitOnFailure(hr, "failed to allocate result string"); |
1927 | 1942 | ||
1928 | // Prepend | 1943 | // Prepend |
1929 | hr = ::StringCchCopyW(pwzResult, cchResult, pwzInsert); | 1944 | hr = ::StringCchCopyW(pwzResult, cchResult, pwzInsert); |
1930 | ExitOnFailure(hr, "failed to copy prepend string: %ls", pwzInsert); | 1945 | StrExitOnFailure(hr, "failed to copy prepend string: %ls", pwzInsert); |
1931 | 1946 | ||
1932 | // If there was no MULTISZ, double null terminate our result, otherwise, copy the MULTISZ in | 1947 | // If there was no MULTISZ, double null terminate our result, otherwise, copy the MULTISZ in |
1933 | if (0 == cchMultiSz) | 1948 | if (0 == cchMultiSz) |
@@ -1983,7 +1998,7 @@ extern "C" HRESULT DAPI MultiSzFindSubstring( | |||
1983 | SIZE_T cchProgress = 0; | 1998 | SIZE_T cchProgress = 0; |
1984 | 1999 | ||
1985 | hr = MultiSzLen(pwzMultiSz, &cchMultiSz); | 2000 | hr = MultiSzLen(pwzMultiSz, &cchMultiSz); |
1986 | ExitOnFailure(hr, "failed to get the length of a MULTISZ string"); | 2001 | StrExitOnFailure(hr, "failed to get the length of a MULTISZ string"); |
1987 | 2002 | ||
1988 | // Find the string containing the sub string | 2003 | // Find the string containing the sub string |
1989 | hr = S_OK; | 2004 | hr = S_OK; |
@@ -2049,7 +2064,7 @@ extern "C" HRESULT DAPI MultiSzFindString( | |||
2049 | SIZE_T cchProgress = 0; | 2064 | SIZE_T cchProgress = 0; |
2050 | 2065 | ||
2051 | hr = MultiSzLen(pwzMultiSz, &cchMutliSz); | 2066 | hr = MultiSzLen(pwzMultiSz, &cchMutliSz); |
2052 | ExitOnFailure(hr, "failed to get the length of a MULTISZ string"); | 2067 | StrExitOnFailure(hr, "failed to get the length of a MULTISZ string"); |
2053 | 2068 | ||
2054 | // Find the string | 2069 | // Find the string |
2055 | hr = S_OK; | 2070 | hr = S_OK; |
@@ -2116,7 +2131,7 @@ extern "C" HRESULT DAPI MultiSzRemoveString( | |||
2116 | SIZE_T cchProgress = 0; | 2131 | SIZE_T cchProgress = 0; |
2117 | 2132 | ||
2118 | hr = MultiSzLen(*ppwzMultiSz, &cchMultiSz); | 2133 | hr = MultiSzLen(*ppwzMultiSz, &cchMultiSz); |
2119 | ExitOnFailure(hr, "failed to get the length of a MULTISZ string"); | 2134 | StrExitOnFailure(hr, "failed to get the length of a MULTISZ string"); |
2120 | 2135 | ||
2121 | // Find the index we want to remove | 2136 | // Find the index we want to remove |
2122 | hr = S_OK; | 2137 | hr = S_OK; |
@@ -2159,7 +2174,7 @@ extern "C" HRESULT DAPI MultiSzRemoveString( | |||
2159 | if (cchProgress > cchMultiSz) | 2174 | if (cchProgress > cchMultiSz) |
2160 | { | 2175 | { |
2161 | hr = E_UNEXPECTED; | 2176 | hr = E_UNEXPECTED; |
2162 | ExitOnFailure(hr, "failed to move past the string to be removed from MULTISZ"); | 2177 | StrExitOnFailure(hr, "failed to move past the string to be removed from MULTISZ"); |
2163 | } | 2178 | } |
2164 | 2179 | ||
2165 | // Move on to the next character | 2180 | // Move on to the next character |
@@ -2181,7 +2196,7 @@ extern "C" HRESULT DAPI MultiSzInsertString( | |||
2181 | __deref_inout __nullnullterminated LPWSTR* ppwzMultiSz, | 2196 | __deref_inout __nullnullterminated LPWSTR* ppwzMultiSz, |
2182 | __inout_opt SIZE_T* pcchMultiSz, | 2197 | __inout_opt SIZE_T* pcchMultiSz, |
2183 | __in DWORD_PTR dwIndex, | 2198 | __in DWORD_PTR dwIndex, |
2184 | __in __nullnullterminated LPCWSTR pwzInsert | 2199 | __in_z LPCWSTR pwzInsert |
2185 | ) | 2200 | ) |
2186 | { | 2201 | { |
2187 | Assert(ppwzMultiSz && pwzInsert && *pwzInsert); | 2202 | Assert(ppwzMultiSz && pwzInsert && *pwzInsert); |
@@ -2202,7 +2217,7 @@ extern "C" HRESULT DAPI MultiSzInsertString( | |||
2202 | else | 2217 | else |
2203 | { | 2218 | { |
2204 | hr = MultiSzLen(*ppwzMultiSz, &cchMultiSz); | 2219 | hr = MultiSzLen(*ppwzMultiSz, &cchMultiSz); |
2205 | ExitOnFailure(hr, "failed to get the length of a MULTISZ string"); | 2220 | StrExitOnFailure(hr, "failed to get the length of a MULTISZ string"); |
2206 | } | 2221 | } |
2207 | 2222 | ||
2208 | // Find the index we want to insert at | 2223 | // Find the index we want to insert at |
@@ -2220,7 +2235,7 @@ extern "C" HRESULT DAPI MultiSzInsertString( | |||
2220 | if ((dwCurrentIndex + 1 != dwIndex && L'\0' == *(wz + 1)) || cchProgress >= cchMultiSz) | 2235 | if ((dwCurrentIndex + 1 != dwIndex && L'\0' == *(wz + 1)) || cchProgress >= cchMultiSz) |
2221 | { | 2236 | { |
2222 | hr = HRESULT_FROM_WIN32(ERROR_OBJECT_NOT_FOUND); | 2237 | hr = HRESULT_FROM_WIN32(ERROR_OBJECT_NOT_FOUND); |
2223 | ExitOnRootFailure(hr, "requested to insert into an invalid index: %u in a MULTISZ", dwIndex); | 2238 | StrExitOnRootFailure(hr, "requested to insert into an invalid index: %u in a MULTISZ", dwIndex); |
2224 | } | 2239 | } |
2225 | 2240 | ||
2226 | // Move on to the next string | 2241 | // Move on to the next string |
@@ -2235,7 +2250,7 @@ extern "C" HRESULT DAPI MultiSzInsertString( | |||
2235 | cchResult = cchMultiSz + cchString + 1; | 2250 | cchResult = cchMultiSz + cchString + 1; |
2236 | 2251 | ||
2237 | hr = StrAlloc(&pwzResult, cchResult); | 2252 | hr = StrAlloc(&pwzResult, cchResult); |
2238 | ExitOnFailure(hr, "failed to allocate result string for MULTISZ insert"); | 2253 | StrExitOnFailure(hr, "failed to allocate result string for MULTISZ insert"); |
2239 | 2254 | ||
2240 | // Copy the part before the insert | 2255 | // Copy the part before the insert |
2241 | ::CopyMemory(pwzResult, *ppwzMultiSz, cchProgress * sizeof(WCHAR)); | 2256 | ::CopyMemory(pwzResult, *ppwzMultiSz, cchProgress * sizeof(WCHAR)); |
@@ -2273,7 +2288,7 @@ MultiSzReplaceString - replaces string at the specified index with a new one | |||
2273 | extern "C" HRESULT DAPI MultiSzReplaceString( | 2288 | extern "C" HRESULT DAPI MultiSzReplaceString( |
2274 | __deref_inout __nullnullterminated LPWSTR* ppwzMultiSz, | 2289 | __deref_inout __nullnullterminated LPWSTR* ppwzMultiSz, |
2275 | __in DWORD_PTR dwIndex, | 2290 | __in DWORD_PTR dwIndex, |
2276 | __in __nullnullterminated LPCWSTR pwzString | 2291 | __in_z LPCWSTR pwzString |
2277 | ) | 2292 | ) |
2278 | { | 2293 | { |
2279 | Assert(ppwzMultiSz && pwzString && *pwzString); | 2294 | Assert(ppwzMultiSz && pwzString && *pwzString); |
@@ -2281,10 +2296,10 @@ extern "C" HRESULT DAPI MultiSzReplaceString( | |||
2281 | HRESULT hr = S_OK; | 2296 | HRESULT hr = S_OK; |
2282 | 2297 | ||
2283 | hr = MultiSzRemoveString(ppwzMultiSz, dwIndex); | 2298 | hr = MultiSzRemoveString(ppwzMultiSz, dwIndex); |
2284 | ExitOnFailure(hr, "failed to remove string from MULTISZ at the specified index: %u", dwIndex); | 2299 | StrExitOnFailure(hr, "failed to remove string from MULTISZ at the specified index: %u", dwIndex); |
2285 | 2300 | ||
2286 | hr = MultiSzInsertString(ppwzMultiSz, NULL, dwIndex, pwzString); | 2301 | hr = MultiSzInsertString(ppwzMultiSz, NULL, dwIndex, pwzString); |
2287 | ExitOnFailure(hr, "failed to insert string into MULTISZ at the specified index: %u", dwIndex); | 2302 | StrExitOnFailure(hr, "failed to insert string into MULTISZ at the specified index: %u", dwIndex); |
2288 | 2303 | ||
2289 | LExit: | 2304 | LExit: |
2290 | return hr; | 2305 | return hr; |
@@ -2344,7 +2359,7 @@ extern "C" HRESULT DAPI StrStringToInt16( | |||
2344 | LONGLONG ll = 0; | 2359 | LONGLONG ll = 0; |
2345 | 2360 | ||
2346 | hr = StrStringToInt64(wzIn, cchIn, &ll); | 2361 | hr = StrStringToInt64(wzIn, cchIn, &ll); |
2347 | ExitOnFailure(hr, "Failed to parse int64."); | 2362 | StrExitOnFailure(hr, "Failed to parse int64."); |
2348 | 2363 | ||
2349 | if (SHORT_MAX < ll || SHORT_MIN > ll) | 2364 | if (SHORT_MAX < ll || SHORT_MIN > ll) |
2350 | { | 2365 | { |
@@ -2370,7 +2385,7 @@ extern "C" HRESULT DAPI StrStringToUInt16( | |||
2370 | ULONGLONG ull = 0; | 2385 | ULONGLONG ull = 0; |
2371 | 2386 | ||
2372 | hr = StrStringToUInt64(wzIn, cchIn, &ull); | 2387 | hr = StrStringToUInt64(wzIn, cchIn, &ull); |
2373 | ExitOnFailure(hr, "Failed to parse uint64."); | 2388 | StrExitOnFailure(hr, "Failed to parse uint64."); |
2374 | 2389 | ||
2375 | if (USHORT_MAX < ull) | 2390 | if (USHORT_MAX < ull) |
2376 | { | 2391 | { |
@@ -2396,7 +2411,7 @@ extern "C" HRESULT DAPI StrStringToInt32( | |||
2396 | LONGLONG ll = 0; | 2411 | LONGLONG ll = 0; |
2397 | 2412 | ||
2398 | hr = StrStringToInt64(wzIn, cchIn, &ll); | 2413 | hr = StrStringToInt64(wzIn, cchIn, &ll); |
2399 | ExitOnFailure(hr, "Failed to parse int64."); | 2414 | StrExitOnFailure(hr, "Failed to parse int64."); |
2400 | 2415 | ||
2401 | if (INT_MAX < ll || INT_MIN > ll) | 2416 | if (INT_MAX < ll || INT_MIN > ll) |
2402 | { | 2417 | { |
@@ -2422,7 +2437,7 @@ extern "C" HRESULT DAPI StrStringToUInt32( | |||
2422 | ULONGLONG ull = 0; | 2437 | ULONGLONG ull = 0; |
2423 | 2438 | ||
2424 | hr = StrStringToUInt64(wzIn, cchIn, &ull); | 2439 | hr = StrStringToUInt64(wzIn, cchIn, &ull); |
2425 | ExitOnFailure(hr, "Failed to parse uint64."); | 2440 | StrExitOnFailure(hr, "Failed to parse uint64."); |
2426 | 2441 | ||
2427 | if (UINT_MAX < ull) | 2442 | if (UINT_MAX < ull) |
2428 | { | 2443 | { |
@@ -2607,13 +2622,13 @@ extern "C" HRESULT DAPI StrArrayAllocString( | |||
2607 | UINT cNewStrArray; | 2622 | UINT cNewStrArray; |
2608 | 2623 | ||
2609 | hr = ::UIntAdd(*pcStrArray, 1, &cNewStrArray); | 2624 | hr = ::UIntAdd(*pcStrArray, 1, &cNewStrArray); |
2610 | ExitOnFailure(hr, "Failed to increment the string array element count."); | 2625 | StrExitOnFailure(hr, "Failed to increment the string array element count."); |
2611 | 2626 | ||
2612 | hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(prgsczStrArray), cNewStrArray, sizeof(LPWSTR), ARRAY_GROWTH_SIZE); | 2627 | hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(prgsczStrArray), cNewStrArray, sizeof(LPWSTR), ARRAY_GROWTH_SIZE); |
2613 | ExitOnFailure(hr, "Failed to allocate memory for the string array."); | 2628 | StrExitOnFailure(hr, "Failed to allocate memory for the string array."); |
2614 | 2629 | ||
2615 | hr = StrAllocString(&(*prgsczStrArray)[*pcStrArray], wzSource, cchSource); | 2630 | hr = StrAllocString(&(*prgsczStrArray)[*pcStrArray], wzSource, cchSource); |
2616 | ExitOnFailure(hr, "Failed to allocate and assign the string."); | 2631 | StrExitOnFailure(hr, "Failed to allocate and assign the string."); |
2617 | 2632 | ||
2618 | *pcStrArray = cNewStrArray; | 2633 | *pcStrArray = cNewStrArray; |
2619 | 2634 | ||
@@ -2639,12 +2654,12 @@ extern "C" HRESULT DAPI StrArrayFree( | |||
2639 | if (NULL != rgsczStrArray[i]) | 2654 | if (NULL != rgsczStrArray[i]) |
2640 | { | 2655 | { |
2641 | hr = StrFree(rgsczStrArray[i]); | 2656 | hr = StrFree(rgsczStrArray[i]); |
2642 | ExitOnFailure(hr, "Failed to free the string at index %u.", i); | 2657 | StrExitOnFailure(hr, "Failed to free the string at index %u.", i); |
2643 | } | 2658 | } |
2644 | } | 2659 | } |
2645 | 2660 | ||
2646 | hr = MemFree(rgsczStrArray); | 2661 | hr = MemFree(rgsczStrArray); |
2647 | ExitOnFailure(hr, "Failed to free memory for the string array."); | 2662 | StrExitOnFailure(hr, "Failed to free memory for the string array."); |
2648 | 2663 | ||
2649 | LExit: | 2664 | LExit: |
2650 | return hr; | 2665 | return hr; |
@@ -2667,12 +2682,12 @@ extern "C" HRESULT DAPI StrSplitAllocArray( | |||
2667 | 2682 | ||
2668 | // Copy wzSource so it is not modified. | 2683 | // Copy wzSource so it is not modified. |
2669 | hr = StrAllocString(&sczCopy, wzSource, 0); | 2684 | hr = StrAllocString(&sczCopy, wzSource, 0); |
2670 | ExitOnFailure(hr, "Failed to copy the source string."); | 2685 | StrExitOnFailure(hr, "Failed to copy the source string."); |
2671 | 2686 | ||
2672 | for (LPCWSTR wzToken = ::wcstok_s(sczCopy, wzDelim, &wzContext); wzToken; wzToken = ::wcstok_s(NULL, wzDelim, &wzContext)) | 2687 | for (LPCWSTR wzToken = ::wcstok_s(sczCopy, wzDelim, &wzContext); wzToken; wzToken = ::wcstok_s(NULL, wzDelim, &wzContext)) |
2673 | { | 2688 | { |
2674 | hr = StrArrayAllocString(prgsczStrArray, pcStrArray, wzToken, 0); | 2689 | hr = StrArrayAllocString(prgsczStrArray, pcStrArray, wzToken, 0); |
2675 | ExitOnFailure(hr, "Failed to add the string to the string array."); | 2690 | StrExitOnFailure(hr, "Failed to add the string to the string array."); |
2676 | } | 2691 | } |
2677 | 2692 | ||
2678 | LExit: | 2693 | LExit: |
@@ -2696,20 +2711,20 @@ static HRESULT StrAllocStringMapInvariant( | |||
2696 | HRESULT hr = S_OK; | 2711 | HRESULT hr = S_OK; |
2697 | 2712 | ||
2698 | hr = StrAllocString(pscz, wzSource, cchSource); | 2713 | hr = StrAllocString(pscz, wzSource, cchSource); |
2699 | ExitOnFailure(hr, "Failed to allocate a copy of the source string."); | 2714 | StrExitOnFailure(hr, "Failed to allocate a copy of the source string."); |
2700 | 2715 | ||
2701 | if (0 == cchSource) | 2716 | if (0 == cchSource) |
2702 | { | 2717 | { |
2703 | // Need the actual string size for LCMapString. This includes the null-terminator | 2718 | // Need the actual string size for LCMapString. This includes the null-terminator |
2704 | // but LCMapString doesn't care either way. | 2719 | // but LCMapString doesn't care either way. |
2705 | hr = ::StringCchLengthW(*pscz, INT_MAX, reinterpret_cast<size_t*>(&cchSource)); | 2720 | hr = ::StringCchLengthW(*pscz, INT_MAX, reinterpret_cast<size_t*>(&cchSource)); |
2706 | ExitOnFailure(hr, "Failed to get the length of the string."); | 2721 | StrExitOnFailure(hr, "Failed to get the length of the string."); |
2707 | } | 2722 | } |
2708 | 2723 | ||
2709 | // Convert the copy of the string to upper or lower case in-place. | 2724 | // Convert the copy of the string to upper or lower case in-place. |
2710 | if (0 == ::LCMapStringW(LOCALE_INVARIANT, dwMapFlags, *pscz, cchSource, *pscz, cchSource)) | 2725 | if (0 == ::LCMapStringW(LOCALE_INVARIANT, dwMapFlags, *pscz, cchSource, *pscz, cchSource)) |
2711 | { | 2726 | { |
2712 | ExitWithLastError(hr, "Failed to convert the string case."); | 2727 | StrExitWithLastError(hr, "Failed to convert the string case."); |
2713 | } | 2728 | } |
2714 | 2729 | ||
2715 | LExit: | 2730 | LExit: |
@@ -2734,7 +2749,7 @@ extern "C" DAPI_(HRESULT) StrSecureZeroString( | |||
2734 | if (-1 == cch) | 2749 | if (-1 == cch) |
2735 | { | 2750 | { |
2736 | hr = E_INVALIDARG; | 2751 | hr = E_INVALIDARG; |
2737 | ExitOnFailure(hr, "Failed to get size of string"); | 2752 | StrExitOnFailure(hr, "Failed to get size of string"); |
2738 | } | 2753 | } |
2739 | else | 2754 | else |
2740 | { | 2755 | { |