diff options
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/strutil.cpp')
-rw-r--r-- | src/libs/dutil/WixToolset.DUtil/strutil.cpp | 145 |
1 files changed, 58 insertions, 87 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/strutil.cpp b/src/libs/dutil/WixToolset.DUtil/strutil.cpp index e4fcc9c8..a483cf54 100644 --- a/src/libs/dutil/WixToolset.DUtil/strutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/strutil.cpp | |||
@@ -390,13 +390,8 @@ static HRESULT AllocStringHelper( | |||
390 | 390 | ||
391 | if (*ppwz) | 391 | if (*ppwz) |
392 | { | 392 | { |
393 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 393 | hr = StrMaxLength(*ppwz, &cch); |
394 | if (-1 == cch) | 394 | StrExitOnFailure(hr, "failed to get size of destination string"); |
395 | { | ||
396 | hr = E_INVALIDARG; | ||
397 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
398 | } | ||
399 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
400 | } | 395 | } |
401 | 396 | ||
402 | if (0 == cchSource && wzSource) | 397 | if (0 == cchSource && wzSource) |
@@ -447,13 +442,8 @@ extern "C" HRESULT DAPI StrAnsiAllocString( | |||
447 | 442 | ||
448 | if (*ppsz) | 443 | if (*ppsz) |
449 | { | 444 | { |
450 | cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) | 445 | hr = StrMaxLengthAnsi(*ppsz, &cch); |
451 | if (-1 == cch) | 446 | StrExitOnFailure(hr, "failed to get size of destination string"); |
452 | { | ||
453 | hr = E_INVALIDARG; | ||
454 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
455 | } | ||
456 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | ||
457 | } | 447 | } |
458 | 448 | ||
459 | if (0 == cchSource) | 449 | if (0 == cchSource) |
@@ -527,13 +517,8 @@ extern "C" HRESULT DAPI StrAllocStringAnsi( | |||
527 | 517 | ||
528 | if (*ppwz) | 518 | if (*ppwz) |
529 | { | 519 | { |
530 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 520 | hr = StrMaxLength(*ppwz, &cch); |
531 | if (-1 == cch) | 521 | StrExitOnFailure(hr, "failed to get size of destination string"); |
532 | { | ||
533 | hr = E_INVALIDARG; | ||
534 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
535 | } | ||
536 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
537 | } | 522 | } |
538 | 523 | ||
539 | if (0 == cchSource) | 524 | if (0 == cchSource) |
@@ -605,13 +590,8 @@ HRESULT DAPI StrAnsiAllocStringAnsi( | |||
605 | 590 | ||
606 | if (*ppsz) | 591 | if (*ppsz) |
607 | { | 592 | { |
608 | cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) | 593 | hr = StrMaxLengthAnsi(*ppsz, &cch); |
609 | if (-1 == cch) | 594 | StrExitOnRootFailure(hr, "failed to get size of destination string"); |
610 | { | ||
611 | hr = E_INVALIDARG; | ||
612 | StrExitOnRootFailure(hr, "failed to get size of destination string"); | ||
613 | } | ||
614 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | ||
615 | } | 595 | } |
616 | 596 | ||
617 | if (0 == cchSource && szSource) | 597 | if (0 == cchSource && szSource) |
@@ -664,13 +644,8 @@ extern "C" HRESULT DAPI StrAllocPrefix( | |||
664 | 644 | ||
665 | if (*ppwz) | 645 | if (*ppwz) |
666 | { | 646 | { |
667 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 647 | hr = StrMaxLength(*ppwz, &cch); |
668 | if (-1 == cch) | 648 | StrExitOnFailure(hr, "failed to get size of destination string"); |
669 | { | ||
670 | hr = E_INVALIDARG; | ||
671 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
672 | } | ||
673 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
674 | 649 | ||
675 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 650 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
676 | StrExitOnFailure(hr, "Failed to calculate length of string"); | 651 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
@@ -770,13 +745,8 @@ static HRESULT AllocConcatHelper( | |||
770 | 745 | ||
771 | if (*ppwz) | 746 | if (*ppwz) |
772 | { | 747 | { |
773 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 748 | hr = StrMaxLength(*ppwz, &cch); |
774 | if (-1 == cch) | 749 | StrExitOnFailure(hr, "failed to get size of destination string"); |
775 | { | ||
776 | hr = E_INVALIDARG; | ||
777 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
778 | } | ||
779 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
780 | 750 | ||
781 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 751 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
782 | StrExitOnFailure(hr, "Failed to calculate length of string"); | 752 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
@@ -833,13 +803,8 @@ extern "C" HRESULT DAPI StrAnsiAllocConcat( | |||
833 | 803 | ||
834 | if (*ppz) | 804 | if (*ppz) |
835 | { | 805 | { |
836 | cch = MemSize(*ppz); // get the count in bytes so we can check if it failed (returns -1) | 806 | hr = StrMaxLengthAnsi(*ppz, &cch); |
837 | if (-1 == cch) | 807 | StrExitOnFailure(hr, "failed to get size of destination string"); |
838 | { | ||
839 | hr = E_INVALIDARG; | ||
840 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
841 | } | ||
842 | cch /= sizeof(CHAR); // convert the count in bytes to count in characters | ||
843 | 808 | ||
844 | #pragma prefast(push) | 809 | #pragma prefast(push) |
845 | #pragma prefast(disable:25068) | 810 | #pragma prefast(disable:25068) |
@@ -1085,12 +1050,8 @@ static HRESULT AllocFormattedArgsHelper( | |||
1085 | 1050 | ||
1086 | if (*ppwz) | 1051 | if (*ppwz) |
1087 | { | 1052 | { |
1088 | cbOriginal = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 1053 | hr = StrSize(*ppwz, &cbOriginal); |
1089 | if (-1 == cbOriginal) | 1054 | StrExitOnFailure(hr, "failed to get size of destination string"); |
1090 | { | ||
1091 | hr = E_INVALIDARG; | ||
1092 | StrExitOnRootFailure(hr, "failed to get size of destination string"); | ||
1093 | } | ||
1094 | 1055 | ||
1095 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters | 1056 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters |
1096 | 1057 | ||
@@ -1161,19 +1122,14 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
1161 | Assert(ppsz && szFormat && *szFormat); | 1122 | Assert(ppsz && szFormat && *szFormat); |
1162 | 1123 | ||
1163 | HRESULT hr = S_OK; | 1124 | HRESULT hr = S_OK; |
1164 | SIZE_T cch = *ppsz ? MemSize(*ppsz) / sizeof(CHAR) : 0; | 1125 | SIZE_T cch = 0; |
1165 | LPSTR pszOriginal = NULL; | 1126 | LPSTR pszOriginal = NULL; |
1166 | size_t cchOriginal = 0; | 1127 | size_t cchOriginal = 0; |
1167 | 1128 | ||
1168 | if (*ppsz) | 1129 | if (*ppsz) |
1169 | { | 1130 | { |
1170 | cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) | 1131 | hr = StrMaxLengthAnsi(*ppsz, &cch); |
1171 | if (-1 == cch) | 1132 | StrExitOnFailure(hr, "failed to get size of destination string"); |
1172 | { | ||
1173 | hr = E_INVALIDARG; | ||
1174 | StrExitOnRootFailure(hr, "failed to get size of destination string"); | ||
1175 | } | ||
1176 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | ||
1177 | 1133 | ||
1178 | hr = ::StringCchLengthA(*ppsz, STRSAFE_MAX_CCH, &cchOriginal); | 1134 | hr = ::StringCchLengthA(*ppsz, STRSAFE_MAX_CCH, &cchOriginal); |
1179 | StrExitOnRootFailure(hr, "failed to get length of original string"); | 1135 | StrExitOnRootFailure(hr, "failed to get length of original string"); |
@@ -1280,11 +1236,8 @@ extern "C" HRESULT DAPI StrMaxLength( | |||
1280 | 1236 | ||
1281 | if (p) | 1237 | if (p) |
1282 | { | 1238 | { |
1283 | *pcch = MemSize(p); // get size of entire buffer | 1239 | hr = StrSize(p, pcch); |
1284 | if (-1 == *pcch) | 1240 | StrExitOnFailure(hr, "Failed to get size of string buffer."); |
1285 | { | ||
1286 | ExitFunction1(hr = E_FAIL); | ||
1287 | } | ||
1288 | 1241 | ||
1289 | *pcch /= sizeof(WCHAR); // reduce to count of characters | 1242 | *pcch /= sizeof(WCHAR); // reduce to count of characters |
1290 | } | 1243 | } |
@@ -1300,27 +1253,51 @@ LExit: | |||
1300 | 1253 | ||
1301 | 1254 | ||
1302 | /******************************************************************** | 1255 | /******************************************************************** |
1303 | StrSize - returns count of bytes in dynamic string p | 1256 | StrMaxLengthAnsi - returns maximum number of characters that can be stored in dynamic string p |
1304 | 1257 | ||
1258 | NOTE: assumes non-Unicode string | ||
1305 | ********************************************************************/ | 1259 | ********************************************************************/ |
1306 | extern "C" HRESULT DAPI StrSize( | 1260 | extern "C" HRESULT DAPI StrMaxLengthAnsi( |
1307 | __in LPCVOID p, | 1261 | __in LPCVOID p, |
1308 | __out SIZE_T* pcb | 1262 | __out SIZE_T* pcch |
1309 | ) | 1263 | ) |
1310 | { | 1264 | { |
1311 | Assert(p && pcb); | 1265 | Assert(pcch); |
1312 | 1266 | ||
1313 | HRESULT hr = S_OK; | 1267 | HRESULT hr = S_OK; |
1314 | 1268 | ||
1315 | *pcb = MemSize(p); | 1269 | if (p) |
1316 | if (-1 == *pcb) | 1270 | { |
1271 | hr = StrSize(p, pcch); | ||
1272 | StrExitOnFailure(hr, "Failed to get size of string buffer."); | ||
1273 | |||
1274 | *pcch /= sizeof(CHAR); // reduce to count of characters | ||
1275 | } | ||
1276 | else | ||
1317 | { | 1277 | { |
1318 | hr = E_FAIL; | 1278 | *pcch = 0; |
1319 | } | 1279 | } |
1280 | Assert(S_OK == hr); | ||
1320 | 1281 | ||
1282 | LExit: | ||
1321 | return hr; | 1283 | return hr; |
1322 | } | 1284 | } |
1323 | 1285 | ||
1286 | |||
1287 | /******************************************************************** | ||
1288 | StrSize - returns count of bytes in dynamic string p | ||
1289 | |||
1290 | ********************************************************************/ | ||
1291 | extern "C" HRESULT DAPI StrSize( | ||
1292 | __in LPCVOID p, | ||
1293 | __out SIZE_T* pcb | ||
1294 | ) | ||
1295 | { | ||
1296 | Assert(p && pcb); | ||
1297 | |||
1298 | return MemSizeChecked(p, pcb); | ||
1299 | } | ||
1300 | |||
1324 | /******************************************************************** | 1301 | /******************************************************************** |
1325 | StrFree - releases dynamic string memory allocated by any StrAlloc*() functions | 1302 | StrFree - releases dynamic string memory allocated by any StrAlloc*() functions |
1326 | 1303 | ||
@@ -2786,22 +2763,16 @@ extern "C" DAPI_(HRESULT) StrSecureZeroString( | |||
2786 | ) | 2763 | ) |
2787 | { | 2764 | { |
2788 | HRESULT hr = S_OK; | 2765 | HRESULT hr = S_OK; |
2789 | SIZE_T cch; | 2766 | SIZE_T cb = 0; |
2790 | 2767 | ||
2791 | if (pwz) | 2768 | if (pwz) |
2792 | { | 2769 | { |
2793 | cch = MemSize(pwz); | 2770 | hr = StrSize(pwz, &cb); |
2794 | if (-1 == cch) | 2771 | StrExitOnFailure(hr, "Failed to get size of string"); |
2795 | { | 2772 | |
2796 | hr = E_INVALIDARG; | 2773 | SecureZeroMemory(pwz, cb); |
2797 | StrExitOnFailure(hr, "Failed to get size of string"); | ||
2798 | } | ||
2799 | else | ||
2800 | { | ||
2801 | SecureZeroMemory(pwz, cch); | ||
2802 | } | ||
2803 | } | 2774 | } |
2804 | 2775 | ||
2805 | LExit: | 2776 | LExit: |
2806 | return hr; | 2777 | return hr; |
2807 | } | 2778 | } |