aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/fileutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/dutil/fileutil.cpp')
-rw-r--r--src/dutil/fileutil.cpp265
1 files changed, 140 insertions, 125 deletions
diff --git a/src/dutil/fileutil.cpp b/src/dutil/fileutil.cpp
index 6191ec06..c76017de 100644
--- a/src/dutil/fileutil.cpp
+++ b/src/dutil/fileutil.cpp
@@ -2,6 +2,21 @@
2 2
3#include "precomp.h" 3#include "precomp.h"
4 4
5
6// Exit macros
7#define FileExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_FILEUTIL, x, s, __VA_ARGS__)
8#define FileExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_FILEUTIL, x, s, __VA_ARGS__)
9#define FileExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_FILEUTIL, x, s, __VA_ARGS__)
10#define FileExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_FILEUTIL, x, s, __VA_ARGS__)
11#define FileExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_FILEUTIL, x, s, __VA_ARGS__)
12#define FileExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_FILEUTIL, x, s, __VA_ARGS__)
13#define FileExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_FILEUTIL, p, x, e, s, __VA_ARGS__)
14#define FileExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_FILEUTIL, p, x, s, __VA_ARGS__)
15#define FileExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_FILEUTIL, p, x, e, s, __VA_ARGS__)
16#define FileExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_FILEUTIL, p, x, s, __VA_ARGS__)
17#define FileExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_FILEUTIL, e, x, s, __VA_ARGS__)
18#define FileExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_FILEUTIL, g, x, s, __VA_ARGS__)
19
5// constants 20// constants
6 21
7const BYTE UTF8BOM[] = {0xEF, 0xBB, 0xBF}; 22const BYTE UTF8BOM[] = {0xEF, 0xBB, 0xBF};
@@ -15,7 +30,7 @@ const LPCWSTR REGISTRY_PENDING_FILE_RENAME_VALUE = L"PendingFileRenameOperations
15 30
16********************************************************************/ 31********************************************************************/
17extern "C" LPWSTR DAPI FileFromPath( 32extern "C" LPWSTR DAPI FileFromPath(
18 __in LPCWSTR wzPath 33 __in_z LPCWSTR wzPath
19 ) 34 )
20{ 35{
21 if (!wzPath) 36 if (!wzPath)
@@ -42,7 +57,7 @@ extern "C" LPWSTR DAPI FileFromPath(
42 57
43********************************************************************/ 58********************************************************************/
44extern "C" HRESULT DAPI FileResolvePath( 59extern "C" HRESULT DAPI FileResolvePath(
45 __in LPCWSTR wzRelativePath, 60 __in_z LPCWSTR wzRelativePath,
46 __out LPWSTR *ppwzFullPath 61 __out LPWSTR *ppwzFullPath
47 ) 62 )
48{ 63{
@@ -63,28 +78,28 @@ extern "C" HRESULT DAPI FileResolvePath(
63 // 78 //
64 cchExpandedPath = MAX_PATH; 79 cchExpandedPath = MAX_PATH;
65 hr = StrAlloc(&pwzExpandedPath, cchExpandedPath); 80 hr = StrAlloc(&pwzExpandedPath, cchExpandedPath);
66 ExitOnFailure(hr, "Failed to allocate space for expanded path."); 81 FileExitOnFailure(hr, "Failed to allocate space for expanded path.");
67 82
68 cch = ::ExpandEnvironmentStringsW(wzRelativePath, pwzExpandedPath, cchExpandedPath); 83 cch = ::ExpandEnvironmentStringsW(wzRelativePath, pwzExpandedPath, cchExpandedPath);
69 if (0 == cch) 84 if (0 == cch)
70 { 85 {
71 ExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath); 86 FileExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath);
72 } 87 }
73 else if (cchExpandedPath < cch) 88 else if (cchExpandedPath < cch)
74 { 89 {
75 cchExpandedPath = cch; 90 cchExpandedPath = cch;
76 hr = StrAlloc(&pwzExpandedPath, cchExpandedPath); 91 hr = StrAlloc(&pwzExpandedPath, cchExpandedPath);
77 ExitOnFailure(hr, "Failed to re-allocate more space for expanded path."); 92 FileExitOnFailure(hr, "Failed to re-allocate more space for expanded path.");
78 93
79 cch = ::ExpandEnvironmentStringsW(wzRelativePath, pwzExpandedPath, cchExpandedPath); 94 cch = ::ExpandEnvironmentStringsW(wzRelativePath, pwzExpandedPath, cchExpandedPath);
80 if (0 == cch) 95 if (0 == cch)
81 { 96 {
82 ExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath); 97 FileExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath);
83 } 98 }
84 else if (cchExpandedPath < cch) 99 else if (cchExpandedPath < cch)
85 { 100 {
86 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); 101 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
87 ExitOnRootFailure(hr, "Failed to allocate buffer for expanded path."); 102 FileExitOnRootFailure(hr, "Failed to allocate buffer for expanded path.");
88 } 103 }
89 } 104 }
90 105
@@ -93,28 +108,28 @@ extern "C" HRESULT DAPI FileResolvePath(
93 // 108 //
94 cchFullPath = MAX_PATH; 109 cchFullPath = MAX_PATH;
95 hr = StrAlloc(&pwzFullPath, cchFullPath); 110 hr = StrAlloc(&pwzFullPath, cchFullPath);
96 ExitOnFailure(hr, "Failed to allocate space for full path."); 111 FileExitOnFailure(hr, "Failed to allocate space for full path.");
97 112
98 cch = ::GetFullPathNameW(pwzExpandedPath, cchFullPath, pwzFullPath, &wzFileName); 113 cch = ::GetFullPathNameW(pwzExpandedPath, cchFullPath, pwzFullPath, &wzFileName);
99 if (0 == cch) 114 if (0 == cch)
100 { 115 {
101 ExitWithLastError(hr, "Failed to get full path for string: %ls", pwzExpandedPath); 116 FileExitWithLastError(hr, "Failed to get full path for string: %ls", pwzExpandedPath);
102 } 117 }
103 else if (cchFullPath < cch) 118 else if (cchFullPath < cch)
104 { 119 {
105 cchFullPath = cch; 120 cchFullPath = cch;
106 hr = StrAlloc(&pwzFullPath, cchFullPath); 121 hr = StrAlloc(&pwzFullPath, cchFullPath);
107 ExitOnFailure(hr, "Failed to re-allocate more space for full path."); 122 FileExitOnFailure(hr, "Failed to re-allocate more space for full path.");
108 123
109 cch = ::GetFullPathNameW(pwzExpandedPath, cchFullPath, pwzFullPath, &wzFileName); 124 cch = ::GetFullPathNameW(pwzExpandedPath, cchFullPath, pwzFullPath, &wzFileName);
110 if (0 == cch) 125 if (0 == cch)
111 { 126 {
112 ExitWithLastError(hr, "Failed to get full path for string: %ls", pwzExpandedPath); 127 FileExitWithLastError(hr, "Failed to get full path for string: %ls", pwzExpandedPath);
113 } 128 }
114 else if (cchFullPath < cch) 129 else if (cchFullPath < cch)
115 { 130 {
116 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); 131 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
117 ExitOnRootFailure(hr, "Failed to allocate buffer for full path."); 132 FileExitOnRootFailure(hr, "Failed to allocate buffer for full path.");
118 } 133 }
119 } 134 }
120 135
@@ -133,7 +148,7 @@ LExit:
133FileStripExtension - Strip extension from filename 148FileStripExtension - Strip extension from filename
134********************************************************************/ 149********************************************************************/
135extern "C" HRESULT DAPI FileStripExtension( 150extern "C" HRESULT DAPI FileStripExtension(
136__in LPCWSTR wzFileName, 151__in_z LPCWSTR wzFileName,
137__out LPWSTR *ppwzFileNameNoExtension 152__out LPWSTR *ppwzFileNameNoExtension
138) 153)
139{ 154{
@@ -158,14 +173,14 @@ __out LPWSTR *ppwzFileNameNoExtension
158 } 173 }
159 174
160 hr = StrAlloc(&pwzFileNameNoExtension, cchFileNameNoExtension); 175 hr = StrAlloc(&pwzFileNameNoExtension, cchFileNameNoExtension);
161 ExitOnFailure(hr, "failed to allocate space for File Name without extension"); 176 FileExitOnFailure(hr, "failed to allocate space for File Name without extension");
162 177
163 // _wsplitpath_s can handle drive/path/filename/extension 178 // _wsplitpath_s can handle drive/path/filename/extension
164 errno_t err = _wsplitpath_s(wzFileName, NULL, NULL, NULL, NULL, pwzFileNameNoExtension, cchFileNameNoExtension, NULL, NULL); 179 errno_t err = _wsplitpath_s(wzFileName, NULL, NULL, NULL, NULL, pwzFileNameNoExtension, cchFileNameNoExtension, NULL, NULL);
165 if (0 != err) 180 if (0 != err)
166 { 181 {
167 hr = E_INVALIDARG; 182 hr = E_INVALIDARG;
168 ExitOnFailure(hr, "failed to parse filename: %ls", wzFileName); 183 FileExitOnFailure(hr, "failed to parse filename: %ls", wzFileName);
169 } 184 }
170 185
171 *ppwzFileNameNoExtension = pwzFileNameNoExtension; 186 *ppwzFileNameNoExtension = pwzFileNameNoExtension;
@@ -182,8 +197,8 @@ LExit:
182FileChangeExtension - Changes the extension of a filename 197FileChangeExtension - Changes the extension of a filename
183********************************************************************/ 198********************************************************************/
184extern "C" HRESULT DAPI FileChangeExtension( 199extern "C" HRESULT DAPI FileChangeExtension(
185 __in LPCWSTR wzFileName, 200 __in_z LPCWSTR wzFileName,
186 __in LPCWSTR wzNewExtension, 201 __in_z LPCWSTR wzNewExtension,
187 __out LPWSTR *ppwzFileNameNewExtension 202 __out LPWSTR *ppwzFileNameNewExtension
188 ) 203 )
189{ 204{
@@ -193,10 +208,10 @@ extern "C" HRESULT DAPI FileChangeExtension(
193 LPWSTR sczFileName = NULL; 208 LPWSTR sczFileName = NULL;
194 209
195 hr = FileStripExtension(wzFileName, &sczFileName); 210 hr = FileStripExtension(wzFileName, &sczFileName);
196 ExitOnFailure(hr, "Failed to strip extension from file name: %ls", wzFileName); 211 FileExitOnFailure(hr, "Failed to strip extension from file name: %ls", wzFileName);
197 212
198 hr = StrAllocConcat(&sczFileName, wzNewExtension, 0); 213 hr = StrAllocConcat(&sczFileName, wzNewExtension, 0);
199 ExitOnFailure(hr, "Failed to add new extension."); 214 FileExitOnFailure(hr, "Failed to add new extension.");
200 215
201 *ppwzFileNameNewExtension = sczFileName; 216 *ppwzFileNameNewExtension = sczFileName;
202 sczFileName = NULL; 217 sczFileName = NULL;
@@ -238,11 +253,11 @@ extern "C" HRESULT DAPI FileAddSuffixToBaseName(
238 { 253 {
239 // no extension, so add the suffix at the end of the whole name 254 // no extension, so add the suffix at the end of the whole name
240 hr = StrAllocString(&sczNewFileName, wzFileName, 0); 255 hr = StrAllocString(&sczNewFileName, wzFileName, 0);
241 ExitOnFailure(hr, "Failed to allocate new file name."); 256 FileExitOnFailure(hr, "Failed to allocate new file name.");
242 257
243 hr = StrAllocConcat(&sczNewFileName, wzSuffix, 0); 258 hr = StrAllocConcat(&sczNewFileName, wzSuffix, 0);
244 } 259 }
245 ExitOnFailure(hr, "Failed to allocate new file name with suffix."); 260 FileExitOnFailure(hr, "Failed to allocate new file name with suffix.");
246 261
247 *psczNewFileName = sczNewFileName; 262 *psczNewFileName = sczNewFileName;
248 sczNewFileName = NULL; 263 sczNewFileName = NULL;
@@ -259,7 +274,7 @@ LExit:
259 274
260********************************************************************/ 275********************************************************************/
261extern "C" HRESULT DAPI FileVersion( 276extern "C" HRESULT DAPI FileVersion(
262 __in LPCWSTR wzFilename, 277 __in_z LPCWSTR wzFilename,
263 __out DWORD *pdwVerMajor, 278 __out DWORD *pdwVerMajor,
264 __out DWORD* pdwVerMinor 279 __out DWORD* pdwVerMinor
265 ) 280 )
@@ -274,20 +289,20 @@ extern "C" HRESULT DAPI FileVersion(
274 289
275 if (0 == (cbVerBuffer = ::GetFileVersionInfoSizeW(wzFilename, &dwHandle))) 290 if (0 == (cbVerBuffer = ::GetFileVersionInfoSizeW(wzFilename, &dwHandle)))
276 { 291 {
277 ExitOnLastErrorDebugTrace(hr, "failed to get version info for file: %ls", wzFilename); 292 FileExitOnLastErrorDebugTrace(hr, "failed to get version info for file: %ls", wzFilename);
278 } 293 }
279 294
280 pVerBuffer = ::GlobalAlloc(GMEM_FIXED, cbVerBuffer); 295 pVerBuffer = ::GlobalAlloc(GMEM_FIXED, cbVerBuffer);
281 ExitOnNullDebugTrace(pVerBuffer, hr, E_OUTOFMEMORY, "failed to allocate version info for file: %ls", wzFilename); 296 FileExitOnNullDebugTrace(pVerBuffer, hr, E_OUTOFMEMORY, "failed to allocate version info for file: %ls", wzFilename);
282 297
283 if (!::GetFileVersionInfoW(wzFilename, dwHandle, cbVerBuffer, pVerBuffer)) 298 if (!::GetFileVersionInfoW(wzFilename, dwHandle, cbVerBuffer, pVerBuffer))
284 { 299 {
285 ExitOnLastErrorDebugTrace(hr, "failed to get version info for file: %ls", wzFilename); 300 FileExitOnLastErrorDebugTrace(hr, "failed to get version info for file: %ls", wzFilename);
286 } 301 }
287 302
288 if (!::VerQueryValueW(pVerBuffer, L"\\", (void**)&pvsFileInfo, &cbFileInfo)) 303 if (!::VerQueryValueW(pVerBuffer, L"\\", (void**)&pvsFileInfo, &cbFileInfo))
289 { 304 {
290 ExitOnLastErrorDebugTrace(hr, "failed to get version value for file: %ls", wzFilename); 305 FileExitOnLastErrorDebugTrace(hr, "failed to get version value for file: %ls", wzFilename);
291 } 306 }
292 307
293 *pdwVerMajor = pvsFileInfo->dwFileVersionMS; 308 *pdwVerMajor = pvsFileInfo->dwFileVersionMS;
@@ -307,7 +322,7 @@ LExit:
307 322
308*******************************************************************/ 323*******************************************************************/
309extern "C" HRESULT DAPI FileVersionFromString( 324extern "C" HRESULT DAPI FileVersionFromString(
310 __in LPCWSTR wzVersion, 325 __in_z LPCWSTR wzVersion,
311 __out DWORD* pdwVerMajor, 326 __out DWORD* pdwVerMajor,
312 __out DWORD* pdwVerMinor 327 __out DWORD* pdwVerMinor
313 ) 328 )
@@ -394,7 +409,7 @@ LExit:
394 409
395*******************************************************************/ 410*******************************************************************/
396extern "C" HRESULT DAPI FileVersionFromStringEx( 411extern "C" HRESULT DAPI FileVersionFromStringEx(
397 __in LPCWSTR wzVersion, 412 __in_z LPCWSTR wzVersion,
398 __in DWORD cchVersion, 413 __in DWORD cchVersion,
399 __out DWORD64* pqwVersion 414 __out DWORD64* pqwVersion
400 ) 415 )
@@ -453,11 +468,11 @@ extern "C" HRESULT DAPI FileVersionFromStringEx(
453 468
454 DWORD cchPart; 469 DWORD cchPart;
455 hr = ::PtrdiffTToDWord(wzPartEnd - wzPartBegin, &cchPart); 470 hr = ::PtrdiffTToDWord(wzPartEnd - wzPartBegin, &cchPart);
456 ExitOnFailure(hr, "Version number part was too long."); 471 FileExitOnFailure(hr, "Version number part was too long.");
457 472
458 // parse version part 473 // parse version part
459 hr = StrStringToUInt16(wzPartBegin, cchPart, &us); 474 hr = StrStringToUInt16(wzPartBegin, cchPart, &us);
460 ExitOnFailure(hr, "Failed to parse version number part."); 475 FileExitOnFailure(hr, "Failed to parse version number part.");
461 476
462 // add part to qword version 477 // add part to qword version
463 qwVersion |= (DWORD64)us << ((3 - iPart) * 16); 478 qwVersion |= (DWORD64)us << ((3 - iPart) * 16);
@@ -501,7 +516,7 @@ extern "C" HRESULT DAPI FileVersionToStringEx(
501 516
502 // Format and return the version string. 517 // Format and return the version string.
503 hr = StrAllocFormatted(psczVersion, L"%u.%u.%u.%u", wMajor, wMinor, wBuild, wRevision); 518 hr = StrAllocFormatted(psczVersion, L"%u.%u.%u.%u", wMajor, wMinor, wBuild, wRevision);
504 ExitOnFailure(hr, "Failed to allocate and format the version number."); 519 FileExitOnFailure(hr, "Failed to allocate and format the version number.");
505 520
506LExit: 521LExit:
507 return hr; 522 return hr;
@@ -527,7 +542,7 @@ extern "C" HRESULT DAPI FileSetPointer(
527 liMove.QuadPart = dw64Move; 542 liMove.QuadPart = dw64Move;
528 if (!::SetFilePointerEx(hFile, liMove, &liNewPosition, dwMoveMethod)) 543 if (!::SetFilePointerEx(hFile, liMove, &liNewPosition, dwMoveMethod))
529 { 544 {
530 ExitWithLastError(hr, "Failed to set file pointer."); 545 FileExitWithLastError(hr, "Failed to set file pointer.");
531 } 546 }
532 547
533 if (pdw64NewPosition) 548 if (pdw64NewPosition)
@@ -545,23 +560,23 @@ LExit:
545 560
546********************************************************************/ 561********************************************************************/
547extern "C" HRESULT DAPI FileSize( 562extern "C" HRESULT DAPI FileSize(
548 __in LPCWSTR pwzFileName, 563 __in_z LPCWSTR pwzFileName,
549 __out LONGLONG* pllSize 564 __out LONGLONG* pllSize
550 ) 565 )
551{ 566{
552 HRESULT hr = S_OK; 567 HRESULT hr = S_OK;
553 HANDLE hFile = INVALID_HANDLE_VALUE; 568 HANDLE hFile = INVALID_HANDLE_VALUE;
554 569
555 ExitOnNull(pwzFileName, hr, E_INVALIDARG, "Attempted to check filename, but no filename was provided"); 570 FileExitOnNull(pwzFileName, hr, E_INVALIDARG, "Attempted to check filename, but no filename was provided");
556 571
557 hFile = ::CreateFileW(pwzFileName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 572 hFile = ::CreateFileW(pwzFileName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
558 if (INVALID_HANDLE_VALUE == hFile) 573 if (INVALID_HANDLE_VALUE == hFile)
559 { 574 {
560 ExitWithLastError(hr, "Failed to open file %ls while checking file size", pwzFileName); 575 FileExitWithLastError(hr, "Failed to open file %ls while checking file size", pwzFileName);
561 } 576 }
562 577
563 hr = FileSizeByHandle(hFile, pllSize); 578 hr = FileSizeByHandle(hFile, pllSize);
564 ExitOnFailure(hr, "Failed to check size of file %ls by handle", pwzFileName); 579 FileExitOnFailure(hr, "Failed to check size of file %ls by handle", pwzFileName);
565 580
566LExit: 581LExit:
567 ReleaseFileHandle(hFile); 582 ReleaseFileHandle(hFile);
@@ -587,7 +602,7 @@ extern "C" HRESULT DAPI FileSizeByHandle(
587 602
588 if (!::GetFileSizeEx(hFile, &li)) 603 if (!::GetFileSizeEx(hFile, &li))
589 { 604 {
590 ExitWithLastError(hr, "Failed to get size of file."); 605 FileExitWithLastError(hr, "Failed to get size of file.");
591 } 606 }
592 607
593 *pllSize = li.QuadPart; 608 *pllSize = li.QuadPart;
@@ -602,7 +617,7 @@ LExit:
602 617
603********************************************************************/ 618********************************************************************/
604extern "C" BOOL DAPI FileExistsEx( 619extern "C" BOOL DAPI FileExistsEx(
605 __in LPCWSTR wzPath, 620 __in_z LPCWSTR wzPath,
606 __out_opt DWORD *pdwAttributes 621 __out_opt DWORD *pdwAttributes
607 ) 622 )
608{ 623{
@@ -655,14 +670,14 @@ extern "C" BOOL DAPI FileExistsAfterRestart(
655 { 670 {
656 ExitFunction1(hr = S_OK); 671 ExitFunction1(hr = S_OK);
657 } 672 }
658 ExitOnFailure(hr, "Failed to open pending file rename registry key."); 673 FileExitOnFailure(hr, "Failed to open pending file rename registry key.");
659 674
660 hr = RegReadStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, &rgsczRenames, &cRenames); 675 hr = RegReadStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, &rgsczRenames, &cRenames);
661 if (E_FILENOTFOUND == hr) 676 if (E_FILENOTFOUND == hr)
662 { 677 {
663 ExitFunction1(hr = S_OK); 678 ExitFunction1(hr = S_OK);
664 } 679 }
665 ExitOnFailure(hr, "Failed to read pending file renames."); 680 FileExitOnFailure(hr, "Failed to read pending file renames.");
666 681
667 // The pending file renames array is pairs of source and target paths. We only care 682 // The pending file renames array is pairs of source and target paths. We only care
668 // about checking the source paths so skip the target paths (i += 2). 683 // about checking the source paths so skip the target paths (i += 2).
@@ -678,7 +693,7 @@ extern "C" BOOL DAPI FileExistsAfterRestart(
678 } 693 }
679 694
680 hr = PathCompare(wzPath, wzRename, &nCompare); 695 hr = PathCompare(wzPath, wzRename, &nCompare);
681 ExitOnFailure(hr, "Failed to compare path from pending file rename to check path."); 696 FileExitOnFailure(hr, "Failed to compare path from pending file rename to check path.");
682 697
683 if (CSTR_EQUAL == nCompare) 698 if (CSTR_EQUAL == nCompare)
684 { 699 {
@@ -719,14 +734,14 @@ extern "C" HRESULT DAPI FileRemoveFromPendingRename(
719 { 734 {
720 ExitFunction1(hr = S_OK); 735 ExitFunction1(hr = S_OK);
721 } 736 }
722 ExitOnFailure(hr, "Failed to open pending file rename registry key."); 737 FileExitOnFailure(hr, "Failed to open pending file rename registry key.");
723 738
724 hr = RegReadStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, &rgsczRenames, &cRenames); 739 hr = RegReadStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, &rgsczRenames, &cRenames);
725 if (E_FILENOTFOUND == hr) 740 if (E_FILENOTFOUND == hr)
726 { 741 {
727 ExitFunction1(hr = S_OK); 742 ExitFunction1(hr = S_OK);
728 } 743 }
729 ExitOnFailure(hr, "Failed to read pending file renames."); 744 FileExitOnFailure(hr, "Failed to read pending file renames.");
730 745
731 // The pending file renames array is pairs of source and target paths. We only care 746 // The pending file renames array is pairs of source and target paths. We only care
732 // about checking the source paths so skip the target paths (i += 2). 747 // about checking the source paths so skip the target paths (i += 2).
@@ -742,7 +757,7 @@ extern "C" HRESULT DAPI FileRemoveFromPendingRename(
742 } 757 }
743 758
744 hr = PathCompare(wzPath, wzRename, &nCompare); 759 hr = PathCompare(wzPath, wzRename, &nCompare);
745 ExitOnFailure(hr, "Failed to compare path from pending file rename to check path."); 760 FileExitOnFailure(hr, "Failed to compare path from pending file rename to check path.");
746 761
747 // If we find our path in the list, null out the source and target slot and 762 // If we find our path in the list, null out the source and target slot and
748 // we'll compact the array next. 763 // we'll compact the array next.
@@ -772,7 +787,7 @@ extern "C" HRESULT DAPI FileRemoveFromPendingRename(
772 787
773 // Write the new array back to the pending file rename key. 788 // Write the new array back to the pending file rename key.
774 hr = RegWriteStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, rgsczRenames, cRenames); 789 hr = RegWriteStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, rgsczRenames, cRenames);
775 ExitOnFailure(hr, "Failed to update pending file renames."); 790 FileExitOnFailure(hr, "Failed to update pending file renames.");
776 } 791 }
777 792
778LExit: 793LExit:
@@ -790,7 +805,7 @@ LExit:
790extern "C" HRESULT DAPI FileRead( 805extern "C" HRESULT DAPI FileRead(
791 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest, 806 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest,
792 __out SIZE_T* pcbDest, 807 __out SIZE_T* pcbDest,
793 __in LPCWSTR wzSrcPath 808 __in_z LPCWSTR wzSrcPath
794 ) 809 )
795{ 810{
796 HRESULT hr = FileReadPartial(ppbDest, pcbDest, wzSrcPath, FALSE, 0, 0xFFFFFFFF, FALSE); 811 HRESULT hr = FileReadPartial(ppbDest, pcbDest, wzSrcPath, FALSE, 0, 0xFFFFFFFF, FALSE);
@@ -819,7 +834,7 @@ extern "C" HRESULT DAPI FileReadEx(
819extern "C" HRESULT DAPI FileReadUntil( 834extern "C" HRESULT DAPI FileReadUntil(
820 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest, 835 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest,
821 __out_range(<=, cbMaxRead) SIZE_T* pcbDest, 836 __out_range(<=, cbMaxRead) SIZE_T* pcbDest,
822 __in LPCWSTR wzSrcPath, 837 __in_z LPCWSTR wzSrcPath,
823 __in DWORD cbMaxRead 838 __in DWORD cbMaxRead
824 ) 839 )
825{ 840{
@@ -835,7 +850,7 @@ extern "C" HRESULT DAPI FileReadUntil(
835extern "C" HRESULT DAPI FileReadPartial( 850extern "C" HRESULT DAPI FileReadPartial(
836 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest, 851 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest,
837 __out_range(<=, cbMaxRead) SIZE_T* pcbDest, 852 __out_range(<=, cbMaxRead) SIZE_T* pcbDest,
838 __in LPCWSTR wzSrcPath, 853 __in_z LPCWSTR wzSrcPath,
839 __in BOOL fSeek, 854 __in BOOL fSeek,
840 __in DWORD cbStartPosition, 855 __in DWORD cbStartPosition,
841 __in DWORD cbMaxRead, 856 __in DWORD cbMaxRead,
@@ -850,7 +865,7 @@ extern "C" HRESULT DAPI FileReadPartial(
850 (with specified share mode) 865 (with specified share mode)
851********************************************************************/ 866********************************************************************/
852extern "C" HRESULT DAPI FileReadPartialEx( 867extern "C" HRESULT DAPI FileReadPartialEx(
853 __deref_out_bcount_full(*pcbDest) LPBYTE* ppbDest, 868 __deref_inout_bcount_full(*pcbDest) LPBYTE* ppbDest,
854 __out_range(<=, cbMaxRead) SIZE_T* pcbDest, 869 __out_range(<=, cbMaxRead) SIZE_T* pcbDest,
855 __in_z LPCWSTR wzSrcPath, 870 __in_z LPCWSTR wzSrcPath,
856 __in BOOL fSeek, 871 __in BOOL fSeek,
@@ -868,10 +883,10 @@ extern "C" HRESULT DAPI FileReadPartialEx(
868 DWORD cbData = 0; 883 DWORD cbData = 0;
869 BYTE* pbData = NULL; 884 BYTE* pbData = NULL;
870 885
871 ExitOnNull(pcbDest, hr, E_INVALIDARG, "Invalid argument pcbDest"); 886 FileExitOnNull(pcbDest, hr, E_INVALIDARG, "Invalid argument pcbDest");
872 ExitOnNull(ppbDest, hr, E_INVALIDARG, "Invalid argument ppbDest"); 887 FileExitOnNull(ppbDest, hr, E_INVALIDARG, "Invalid argument ppbDest");
873 ExitOnNull(wzSrcPath, hr, E_INVALIDARG, "Invalid argument wzSrcPath"); 888 FileExitOnNull(wzSrcPath, hr, E_INVALIDARG, "Invalid argument wzSrcPath");
874 ExitOnNull(*wzSrcPath, hr, E_INVALIDARG, "*wzSrcPath is null"); 889 FileExitOnNull(*wzSrcPath, hr, E_INVALIDARG, "*wzSrcPath is null");
875 890
876 hFile = ::CreateFileW(wzSrcPath, GENERIC_READ, dwShareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); 891 hFile = ::CreateFileW(wzSrcPath, GENERIC_READ, dwShareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
877 if (INVALID_HANDLE_VALUE == hFile) 892 if (INVALID_HANDLE_VALUE == hFile)
@@ -881,12 +896,12 @@ extern "C" HRESULT DAPI FileReadPartialEx(
881 { 896 {
882 ExitFunction1(hr = E_FILENOTFOUND); 897 ExitFunction1(hr = E_FILENOTFOUND);
883 } 898 }
884 ExitOnWin32Error(er, hr, "Failed to open file: %ls", wzSrcPath); 899 FileExitOnWin32Error(er, hr, "Failed to open file: %ls", wzSrcPath);
885 } 900 }
886 901
887 if (!::GetFileSizeEx(hFile, &liFileSize)) 902 if (!::GetFileSizeEx(hFile, &liFileSize))
888 { 903 {
889 ExitWithLastError(hr, "Failed to get size of file: %ls", wzSrcPath); 904 FileExitWithLastError(hr, "Failed to get size of file: %ls", wzSrcPath);
890 } 905 }
891 906
892 if (fSeek) 907 if (fSeek)
@@ -894,13 +909,13 @@ extern "C" HRESULT DAPI FileReadPartialEx(
894 if (cbStartPosition > liFileSize.QuadPart) 909 if (cbStartPosition > liFileSize.QuadPart)
895 { 910 {
896 hr = E_INVALIDARG; 911 hr = E_INVALIDARG;
897 ExitOnFailure(hr, "Start position %d bigger than file '%ls' size %d", cbStartPosition, wzSrcPath, liFileSize.QuadPart); 912 FileExitOnFailure(hr, "Start position %d bigger than file '%ls' size %llu", cbStartPosition, wzSrcPath, liFileSize.QuadPart);
898 } 913 }
899 914
900 DWORD dwErr = ::SetFilePointer(hFile, cbStartPosition, NULL, FILE_CURRENT); 915 DWORD dwErr = ::SetFilePointer(hFile, cbStartPosition, NULL, FILE_CURRENT);
901 if (INVALID_SET_FILE_POINTER == dwErr) 916 if (INVALID_SET_FILE_POINTER == dwErr)
902 { 917 {
903 ExitOnLastError(hr, "Failed to seek position %d", cbStartPosition); 918 FileExitOnLastError(hr, "Failed to seek position %d", cbStartPosition);
904 } 919 }
905 } 920 }
906 else 921 else
@@ -918,7 +933,7 @@ extern "C" HRESULT DAPI FileReadPartialEx(
918 if (cbMaxRead < liFileSize.QuadPart - cbStartPosition) 933 if (cbMaxRead < liFileSize.QuadPart - cbStartPosition)
919 { 934 {
920 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); 935 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
921 ExitOnRootFailure(hr, "Failed to load file: %ls, too large.", wzSrcPath); 936 FileExitOnRootFailure(hr, "Failed to load file: %ls, too large.", wzSrcPath);
922 } 937 }
923 } 938 }
924 939
@@ -932,7 +947,7 @@ extern "C" HRESULT DAPI FileReadPartialEx(
932 } 947 }
933 948
934 LPVOID pv = MemReAlloc(*ppbDest, cbData, TRUE); 949 LPVOID pv = MemReAlloc(*ppbDest, cbData, TRUE);
935 ExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to re-allocate memory to read in file: %ls", wzSrcPath); 950 FileExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to re-allocate memory to read in file: %ls", wzSrcPath);
936 951
937 pbData = static_cast<BYTE*>(pv); 952 pbData = static_cast<BYTE*>(pv);
938 } 953 }
@@ -945,7 +960,7 @@ extern "C" HRESULT DAPI FileReadPartialEx(
945 } 960 }
946 961
947 pbData = static_cast<BYTE*>(MemAlloc(cbData, TRUE)); 962 pbData = static_cast<BYTE*>(MemAlloc(cbData, TRUE));
948 ExitOnNull(pbData, hr, E_OUTOFMEMORY, "Failed to allocate memory to read in file: %ls", wzSrcPath); 963 FileExitOnNull(pbData, hr, E_OUTOFMEMORY, "Failed to allocate memory to read in file: %ls", wzSrcPath);
949 } 964 }
950 965
951 DWORD cbTotalRead = 0; 966 DWORD cbTotalRead = 0;
@@ -954,11 +969,11 @@ extern "C" HRESULT DAPI FileReadPartialEx(
954 { 969 {
955 DWORD cbRemaining = 0; 970 DWORD cbRemaining = 0;
956 hr = ::ULongSub(cbData, cbTotalRead, &cbRemaining); 971 hr = ::ULongSub(cbData, cbTotalRead, &cbRemaining);
957 ExitOnFailure(hr, "Underflow calculating remaining buffer size."); 972 FileExitOnFailure(hr, "Underflow calculating remaining buffer size.");
958 973
959 if (!::ReadFile(hFile, pbData + cbTotalRead, cbRemaining, &cbRead, NULL)) 974 if (!::ReadFile(hFile, pbData + cbTotalRead, cbRemaining, &cbRead, NULL))
960 { 975 {
961 ExitWithLastError(hr, "Failed to read from file: %ls", wzSrcPath); 976 FileExitWithLastError(hr, "Failed to read from file: %ls", wzSrcPath);
962 } 977 }
963 978
964 cbTotalRead += cbRead; 979 cbTotalRead += cbRead;
@@ -967,7 +982,7 @@ extern "C" HRESULT DAPI FileReadPartialEx(
967 if (cbTotalRead != cbData) 982 if (cbTotalRead != cbData)
968 { 983 {
969 hr = E_UNEXPECTED; 984 hr = E_UNEXPECTED;
970 ExitOnFailure(hr, "Failed to completely read file: %ls", wzSrcPath); 985 FileExitOnFailure(hr, "Failed to completely read file: %ls", wzSrcPath);
971 } 986 }
972 987
973 *ppbDest = pbData; 988 *ppbDest = pbData;
@@ -999,10 +1014,10 @@ extern "C" HRESULT DAPI FileWrite(
999 1014
1000 // Open the file 1015 // Open the file
1001 hFile = ::CreateFileW(pwzFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, dwFlagsAndAttributes, NULL); 1016 hFile = ::CreateFileW(pwzFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, dwFlagsAndAttributes, NULL);
1002 ExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file: %ls", pwzFileName); 1017 FileExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file: %ls", pwzFileName);
1003 1018
1004 hr = FileWriteHandle(hFile, pbData, cbData); 1019 hr = FileWriteHandle(hFile, pbData, cbData);
1005 ExitOnFailure(hr, "Failed to write to file: %ls", pwzFileName); 1020 FileExitOnFailure(hr, "Failed to write to file: %ls", pwzFileName);
1006 1021
1007 if (pHandle) 1022 if (pHandle)
1008 { 1023 {
@@ -1036,7 +1051,7 @@ extern "C" HRESULT DAPI FileWriteHandle(
1036 { 1051 {
1037 if (!::WriteFile(hFile, pbData + cbTotal, (DWORD)(cbData - cbTotal), &cbDataWritten, NULL)) 1052 if (!::WriteFile(hFile, pbData + cbTotal, (DWORD)(cbData - cbTotal), &cbDataWritten, NULL))
1038 { 1053 {
1039 ExitOnLastError(hr, "Failed to write data to file handle."); 1054 FileExitOnLastError(hr, "Failed to write data to file handle.");
1040 } 1055 }
1041 1056
1042 cbTotal += cbDataWritten; 1057 cbTotal += cbDataWritten;
@@ -1068,13 +1083,13 @@ extern "C" HRESULT DAPI FileCopyUsingHandles(
1068 cbRead = static_cast<DWORD>((0 == cbCopy) ? countof(rgbData) : min(countof(rgbData), cbCopy - cbTotalCopied)); 1083 cbRead = static_cast<DWORD>((0 == cbCopy) ? countof(rgbData) : min(countof(rgbData), cbCopy - cbTotalCopied));
1069 if (!::ReadFile(hSource, rgbData, cbRead, &cbRead, NULL)) 1084 if (!::ReadFile(hSource, rgbData, cbRead, &cbRead, NULL))
1070 { 1085 {
1071 ExitWithLastError(hr, "Failed to read from source."); 1086 FileExitWithLastError(hr, "Failed to read from source.");
1072 } 1087 }
1073 1088
1074 if (cbRead) 1089 if (cbRead)
1075 { 1090 {
1076 hr = FileWriteHandle(hTarget, rgbData, cbRead); 1091 hr = FileWriteHandle(hTarget, rgbData, cbRead);
1077 ExitOnFailure(hr, "Failed to write to target."); 1092 FileExitOnFailure(hr, "Failed to write to target.");
1078 } 1093 }
1079 1094
1080 cbTotalCopied += cbRead; 1095 cbTotalCopied += cbRead;
@@ -1095,8 +1110,8 @@ LExit:
1095 1110
1096*******************************************************************/ 1111*******************************************************************/
1097extern "C" HRESULT DAPI FileEnsureCopy( 1112extern "C" HRESULT DAPI FileEnsureCopy(
1098 __in LPCWSTR wzSource, 1113 __in_z LPCWSTR wzSource,
1099 __in LPCWSTR wzTarget, 1114 __in_z LPCWSTR wzTarget,
1100 __in BOOL fOverwrite 1115 __in BOOL fOverwrite
1101 ) 1116 )
1102{ 1117{
@@ -1132,12 +1147,12 @@ extern "C" HRESULT DAPI FileEnsureCopy(
1132 *pwzLastSlash = L'\0'; // null terminate 1147 *pwzLastSlash = L'\0'; // null terminate
1133 hr = DirEnsureExists(wzTarget, NULL); 1148 hr = DirEnsureExists(wzTarget, NULL);
1134 *pwzLastSlash = L'\\'; // now put the slash back 1149 *pwzLastSlash = L'\\'; // now put the slash back
1135 ExitOnFailureDebugTrace(hr, "failed to create directory while copying file: '%ls' to: '%ls'", wzSource, wzTarget); 1150 FileExitOnFailureDebugTrace(hr, "failed to create directory while copying file: '%ls' to: '%ls'", wzSource, wzTarget);
1136 1151
1137 // try to copy again 1152 // try to copy again
1138 if (!::CopyFileW(wzSource, wzTarget, fOverwrite)) 1153 if (!::CopyFileW(wzSource, wzTarget, fOverwrite))
1139 { 1154 {
1140 ExitOnLastErrorDebugTrace(hr, "failed to copy file: '%ls' to: '%ls'", wzSource, wzTarget); 1155 FileExitOnLastErrorDebugTrace(hr, "failed to copy file: '%ls' to: '%ls'", wzSource, wzTarget);
1141 } 1156 }
1142 } 1157 }
1143 else // no path was specified so just return the error 1158 else // no path was specified so just return the error
@@ -1186,7 +1201,7 @@ extern "C" HRESULT DAPI FileEnsureCopyWithRetry(
1186 break; // no reason to retry these errors. 1201 break; // no reason to retry these errors.
1187 } 1202 }
1188 } 1203 }
1189 ExitOnFailure(hr, "Failed to copy file: '%ls' to: '%ls' after %u retries.", wzSource, wzTarget, i); 1204 FileExitOnFailure(hr, "Failed to copy file: '%ls' to: '%ls' after %u retries.", wzSource, wzTarget, i);
1190 1205
1191LExit: 1206LExit:
1192 return hr; 1207 return hr;
@@ -1198,8 +1213,8 @@ LExit:
1198 1213
1199*******************************************************************/ 1214*******************************************************************/
1200extern "C" HRESULT DAPI FileEnsureMove( 1215extern "C" HRESULT DAPI FileEnsureMove(
1201 __in LPCWSTR wzSource, 1216 __in_z LPCWSTR wzSource,
1202 __in LPCWSTR wzTarget, 1217 __in_z LPCWSTR wzTarget,
1203 __in BOOL fOverwrite, 1218 __in BOOL fOverwrite,
1204 __in BOOL fAllowCopy 1219 __in BOOL fAllowCopy
1205 ) 1220 )
@@ -1260,12 +1275,12 @@ extern "C" HRESULT DAPI FileEnsureMove(
1260 *pwzLastSlash = L'\0'; // null terminate 1275 *pwzLastSlash = L'\0'; // null terminate
1261 hr = DirEnsureExists(wzTarget, NULL); 1276 hr = DirEnsureExists(wzTarget, NULL);
1262 *pwzLastSlash = L'\\'; // now put the slash back 1277 *pwzLastSlash = L'\\'; // now put the slash back
1263 ExitOnFailureDebugTrace(hr, "failed to create directory while moving file: '%ls' to: '%ls'", wzSource, wzTarget); 1278 FileExitOnFailureDebugTrace(hr, "failed to create directory while moving file: '%ls' to: '%ls'", wzSource, wzTarget);
1264 1279
1265 // try to move again 1280 // try to move again
1266 if (!::MoveFileExW(wzSource, wzTarget, dwFlags)) 1281 if (!::MoveFileExW(wzSource, wzTarget, dwFlags))
1267 { 1282 {
1268 ExitOnLastErrorDebugTrace(hr, "failed to move file: '%ls' to: '%ls'", wzSource, wzTarget); 1283 FileExitOnLastErrorDebugTrace(hr, "failed to move file: '%ls' to: '%ls'", wzSource, wzTarget);
1269 } 1284 }
1270 } 1285 }
1271 else // no path was specified so just return the error 1286 else // no path was specified so just return the error
@@ -1310,7 +1325,7 @@ extern "C" HRESULT DAPI FileEnsureMoveWithRetry(
1310 1325
1311 hr = FileEnsureMove(wzSource, wzTarget, fOverwrite, fAllowCopy); 1326 hr = FileEnsureMove(wzSource, wzTarget, fOverwrite, fAllowCopy);
1312 } 1327 }
1313 ExitOnFailure(hr, "Failed to move file: '%ls' to: '%ls' after %u retries.", wzSource, wzTarget, i); 1328 FileExitOnFailure(hr, "Failed to move file: '%ls' to: '%ls' after %u retries.", wzSource, wzTarget, i);
1314 1329
1315LExit: 1330LExit:
1316 return hr; 1331 return hr;
@@ -1323,8 +1338,8 @@ LExit:
1323 NOTE: uses ANSI functions internally so it is Win9x safe 1338 NOTE: uses ANSI functions internally so it is Win9x safe
1324********************************************************************/ 1339********************************************************************/
1325extern "C" HRESULT DAPI FileCreateTemp( 1340extern "C" HRESULT DAPI FileCreateTemp(
1326 __in LPCWSTR wzPrefix, 1341 __in_z LPCWSTR wzPrefix,
1327 __in LPCWSTR wzExtension, 1342 __in_z LPCWSTR wzExtension,
1328 __deref_opt_out_z LPWSTR* ppwzTempFile, 1343 __deref_opt_out_z LPWSTR* ppwzTempFile,
1329 __out_opt HANDLE* phTempFile 1344 __out_opt HANDLE* phTempFile
1330 ) 1345 )
@@ -1340,13 +1355,13 @@ extern "C" HRESULT DAPI FileCreateTemp(
1340 int i = 0; 1355 int i = 0;
1341 1356
1342 hr = StrAnsiAlloc(&pszTempPath, cchTempPath); 1357 hr = StrAnsiAlloc(&pszTempPath, cchTempPath);
1343 ExitOnFailure(hr, "failed to allocate memory for the temp path"); 1358 FileExitOnFailure(hr, "failed to allocate memory for the temp path");
1344 ::GetTempPathA(cchTempPath, pszTempPath); 1359 ::GetTempPathA(cchTempPath, pszTempPath);
1345 1360
1346 for (i = 0; i < 1000 && INVALID_HANDLE_VALUE == hTempFile; ++i) 1361 for (i = 0; i < 1000 && INVALID_HANDLE_VALUE == hTempFile; ++i)
1347 { 1362 {
1348 hr = StrAnsiAllocFormatted(&pszTempFile, "%s%ls%05d.%ls", pszTempPath, wzPrefix, i, wzExtension); 1363 hr = StrAnsiAllocFormatted(&pszTempFile, "%s%ls%05d.%ls", pszTempPath, wzPrefix, i, wzExtension);
1349 ExitOnFailure(hr, "failed to allocate memory for log file"); 1364 FileExitOnFailure(hr, "failed to allocate memory for log file");
1350 1365
1351 hTempFile = ::CreateFileA(pszTempFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); 1366 hTempFile = ::CreateFileA(pszTempFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1352 if (INVALID_HANDLE_VALUE == hTempFile) 1367 if (INVALID_HANDLE_VALUE == hTempFile)
@@ -1358,7 +1373,7 @@ extern "C" HRESULT DAPI FileCreateTemp(
1358 hr = S_OK; 1373 hr = S_OK;
1359 continue; 1374 continue;
1360 } 1375 }
1361 ExitOnFailureDebugTrace(hr, "failed to create file: %ls", pszTempFile); 1376 FileExitOnFailureDebugTrace(hr, "failed to create file: %hs", pszTempFile);
1362 } 1377 }
1363 } 1378 }
1364 1379
@@ -1387,8 +1402,8 @@ LExit:
1387 1402
1388*******************************************************************/ 1403*******************************************************************/
1389extern "C" HRESULT DAPI FileCreateTempW( 1404extern "C" HRESULT DAPI FileCreateTempW(
1390 __in LPCWSTR wzPrefix, 1405 __in_z LPCWSTR wzPrefix,
1391 __in LPCWSTR wzExtension, 1406 __in_z LPCWSTR wzExtension,
1392 __deref_opt_out_z LPWSTR* ppwzTempFile, 1407 __deref_opt_out_z LPWSTR* ppwzTempFile,
1393 __out_opt HANDLE* phTempFile 1408 __out_opt HANDLE* phTempFile
1394 ) 1409 )
@@ -1405,13 +1420,13 @@ extern "C" HRESULT DAPI FileCreateTempW(
1405 1420
1406 if (!::GetTempPathW(cchTempPath, wzTempPath)) 1421 if (!::GetTempPathW(cchTempPath, wzTempPath))
1407 { 1422 {
1408 ExitOnLastError(hr, "failed to get temp path"); 1423 FileExitOnLastError(hr, "failed to get temp path");
1409 } 1424 }
1410 1425
1411 for (i = 0; i < 1000 && INVALID_HANDLE_VALUE == hTempFile; ++i) 1426 for (i = 0; i < 1000 && INVALID_HANDLE_VALUE == hTempFile; ++i)
1412 { 1427 {
1413 hr = StrAllocFormatted(&pwzTempFile, L"%s%s%05d.%s", wzTempPath, wzPrefix, i, wzExtension); 1428 hr = StrAllocFormatted(&pwzTempFile, L"%s%s%05d.%s", wzTempPath, wzPrefix, i, wzExtension);
1414 ExitOnFailure(hr, "failed to allocate memory for temp filename"); 1429 FileExitOnFailure(hr, "failed to allocate memory for temp filename");
1415 1430
1416 hTempFile = ::CreateFileW(pwzTempFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); 1431 hTempFile = ::CreateFileW(pwzTempFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1417 if (INVALID_HANDLE_VALUE == hTempFile) 1432 if (INVALID_HANDLE_VALUE == hTempFile)
@@ -1423,7 +1438,7 @@ extern "C" HRESULT DAPI FileCreateTempW(
1423 hr = S_OK; 1438 hr = S_OK;
1424 continue; 1439 continue;
1425 } 1440 }
1426 ExitOnFailureDebugTrace(hr, "failed to create file: %ls", pwzTempFile); 1441 FileExitOnFailureDebugTrace(hr, "failed to create file: %ls", pwzTempFile);
1427 } 1442 }
1428 } 1443 }
1429 1444
@@ -1452,8 +1467,8 @@ LExit:
1452 1467
1453********************************************************************/ 1468********************************************************************/
1454extern "C" HRESULT DAPI FileIsSame( 1469extern "C" HRESULT DAPI FileIsSame(
1455 __in LPCWSTR wzFile1, 1470 __in_z LPCWSTR wzFile1,
1456 __in LPCWSTR wzFile2, 1471 __in_z LPCWSTR wzFile2,
1457 __out LPBOOL lpfSameFile 1472 __out LPBOOL lpfSameFile
1458 ) 1473 )
1459{ 1474{
@@ -1464,19 +1479,19 @@ extern "C" HRESULT DAPI FileIsSame(
1464 BY_HANDLE_FILE_INFORMATION fileInfo2 = { }; 1479 BY_HANDLE_FILE_INFORMATION fileInfo2 = { };
1465 1480
1466 hFile1 = ::CreateFileW(wzFile1, FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 1481 hFile1 = ::CreateFileW(wzFile1, FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1467 ExitOnInvalidHandleWithLastError(hFile1, hr, "Failed to open file 1. File = '%ls'", wzFile1); 1482 FileExitOnInvalidHandleWithLastError(hFile1, hr, "Failed to open file 1. File = '%ls'", wzFile1);
1468 1483
1469 hFile2 = ::CreateFileW(wzFile2, FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 1484 hFile2 = ::CreateFileW(wzFile2, FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1470 ExitOnInvalidHandleWithLastError(hFile2, hr, "Failed to open file 2. File = '%ls'", wzFile2); 1485 FileExitOnInvalidHandleWithLastError(hFile2, hr, "Failed to open file 2. File = '%ls'", wzFile2);
1471 1486
1472 if (!::GetFileInformationByHandle(hFile1, &fileInfo1)) 1487 if (!::GetFileInformationByHandle(hFile1, &fileInfo1))
1473 { 1488 {
1474 ExitWithLastError(hr, "Failed to get information for file 1. File = '%ls'", wzFile1); 1489 FileExitWithLastError(hr, "Failed to get information for file 1. File = '%ls'", wzFile1);
1475 } 1490 }
1476 1491
1477 if (!::GetFileInformationByHandle(hFile2, &fileInfo2)) 1492 if (!::GetFileInformationByHandle(hFile2, &fileInfo2))
1478 { 1493 {
1479 ExitWithLastError(hr, "Failed to get information for file 2. File = '%ls'", wzFile2); 1494 FileExitWithLastError(hr, "Failed to get information for file 2. File = '%ls'", wzFile2);
1480 } 1495 }
1481 1496
1482 *lpfSameFile = fileInfo1.dwVolumeSerialNumber == fileInfo2.dwVolumeSerialNumber && 1497 *lpfSameFile = fileInfo1.dwVolumeSerialNumber == fileInfo2.dwVolumeSerialNumber &&
@@ -1495,7 +1510,7 @@ LExit:
1495 hidden, or system attributes if necessary. 1510 hidden, or system attributes if necessary.
1496********************************************************************/ 1511********************************************************************/
1497extern "C" HRESULT DAPI FileEnsureDelete( 1512extern "C" HRESULT DAPI FileEnsureDelete(
1498 __in LPCWSTR wzFile 1513 __in_z LPCWSTR wzFile
1499 ) 1514 )
1500{ 1515{
1501 HRESULT hr = S_OK; 1516 HRESULT hr = S_OK;
@@ -1507,13 +1522,13 @@ extern "C" HRESULT DAPI FileEnsureDelete(
1507 { 1522 {
1508 if (!::SetFileAttributesW(wzFile, FILE_ATTRIBUTE_NORMAL)) 1523 if (!::SetFileAttributesW(wzFile, FILE_ATTRIBUTE_NORMAL))
1509 { 1524 {
1510 ExitOnLastError(hr, "Failed to remove attributes from file: %ls", wzFile); 1525 FileExitOnLastError(hr, "Failed to remove attributes from file: %ls", wzFile);
1511 } 1526 }
1512 } 1527 }
1513 1528
1514 if (!::DeleteFileW(wzFile)) 1529 if (!::DeleteFileW(wzFile))
1515 { 1530 {
1516 ExitOnLastError(hr, "Failed to delete file: %ls", wzFile); 1531 FileExitOnLastError(hr, "Failed to delete file: %ls", wzFile);
1517 } 1532 }
1518 } 1533 }
1519 1534
@@ -1525,7 +1540,7 @@ LExit:
1525 FileGetTime - Gets the file time of a specified file 1540 FileGetTime - Gets the file time of a specified file
1526********************************************************************/ 1541********************************************************************/
1527extern "C" HRESULT DAPI FileGetTime( 1542extern "C" HRESULT DAPI FileGetTime(
1528 __in LPCWSTR wzFile, 1543 __in_z LPCWSTR wzFile,
1529 __out_opt LPFILETIME lpCreationTime, 1544 __out_opt LPFILETIME lpCreationTime,
1530 __out_opt LPFILETIME lpLastAccessTime, 1545 __out_opt LPFILETIME lpLastAccessTime,
1531 __out_opt LPFILETIME lpLastWriteTime 1546 __out_opt LPFILETIME lpLastWriteTime
@@ -1535,11 +1550,11 @@ extern "C" HRESULT DAPI FileGetTime(
1535 HANDLE hFile = NULL; 1550 HANDLE hFile = NULL;
1536 1551
1537 hFile = ::CreateFileW(wzFile, FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL); 1552 hFile = ::CreateFileW(wzFile, FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL);
1538 ExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile); 1553 FileExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile);
1539 1554
1540 if (!::GetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime)) 1555 if (!::GetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime))
1541 { 1556 {
1542 ExitWithLastError(hr, "Failed to get file time for file. File = '%ls'", wzFile); 1557 FileExitWithLastError(hr, "Failed to get file time for file. File = '%ls'", wzFile);
1543 } 1558 }
1544 1559
1545LExit: 1560LExit:
@@ -1551,7 +1566,7 @@ LExit:
1551 FileSetTime - Sets the file time of a specified file 1566 FileSetTime - Sets the file time of a specified file
1552********************************************************************/ 1567********************************************************************/
1553extern "C" HRESULT DAPI FileSetTime( 1568extern "C" HRESULT DAPI FileSetTime(
1554 __in LPCWSTR wzFile, 1569 __in_z LPCWSTR wzFile,
1555 __in_opt const FILETIME *lpCreationTime, 1570 __in_opt const FILETIME *lpCreationTime,
1556 __in_opt const FILETIME *lpLastAccessTime, 1571 __in_opt const FILETIME *lpLastAccessTime,
1557 __in_opt const FILETIME *lpLastWriteTime 1572 __in_opt const FILETIME *lpLastWriteTime
@@ -1561,11 +1576,11 @@ extern "C" HRESULT DAPI FileSetTime(
1561 HANDLE hFile = NULL; 1576 HANDLE hFile = NULL;
1562 1577
1563 hFile = ::CreateFileW(wzFile, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL); 1578 hFile = ::CreateFileW(wzFile, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL);
1564 ExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile); 1579 FileExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile);
1565 1580
1566 if (!::SetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime)) 1581 if (!::SetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime))
1567 { 1582 {
1568 ExitWithLastError(hr, "Failed to set file time for file. File = '%ls'", wzFile); 1583 FileExitWithLastError(hr, "Failed to set file time for file. File = '%ls'", wzFile);
1569 } 1584 }
1570 1585
1571LExit: 1586LExit:
@@ -1578,7 +1593,7 @@ LExit:
1578 creation time of the file 1593 creation time of the file
1579********************************************************************/ 1594********************************************************************/
1580extern "C" HRESULT DAPI FileResetTime( 1595extern "C" HRESULT DAPI FileResetTime(
1581 __in LPCWSTR wzFile 1596 __in_z LPCWSTR wzFile
1582 ) 1597 )
1583{ 1598{
1584 HRESULT hr = S_OK; 1599 HRESULT hr = S_OK;
@@ -1586,16 +1601,16 @@ extern "C" HRESULT DAPI FileResetTime(
1586 FILETIME ftCreateTime; 1601 FILETIME ftCreateTime;
1587 1602
1588 hFile = ::CreateFileW(wzFile, FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); 1603 hFile = ::CreateFileW(wzFile, FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1589 ExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile); 1604 FileExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile);
1590 1605
1591 if (!::GetFileTime(hFile, &ftCreateTime, NULL, NULL)) 1606 if (!::GetFileTime(hFile, &ftCreateTime, NULL, NULL))
1592 { 1607 {
1593 ExitWithLastError(hr, "Failed to get file time for file. File = '%ls'", wzFile); 1608 FileExitWithLastError(hr, "Failed to get file time for file. File = '%ls'", wzFile);
1594 } 1609 }
1595 1610
1596 if (!::SetFileTime(hFile, NULL, NULL, &ftCreateTime)) 1611 if (!::SetFileTime(hFile, NULL, NULL, &ftCreateTime))
1597 { 1612 {
1598 ExitWithLastError(hr, "Failed to reset file time for file. File = '%ls'", wzFile); 1613 FileExitWithLastError(hr, "Failed to reset file time for file. File = '%ls'", wzFile);
1599 } 1614 }
1600 1615
1601LExit: 1616LExit:
@@ -1609,7 +1624,7 @@ LExit:
1609 1624
1610*******************************************************************/ 1625*******************************************************************/
1611extern "C" HRESULT DAPI FileExecutableArchitecture( 1626extern "C" HRESULT DAPI FileExecutableArchitecture(
1612 __in LPCWSTR wzFile, 1627 __in_z LPCWSTR wzFile,
1613 __out FILE_ARCHITECTURE *pArchitecture 1628 __out FILE_ARCHITECTURE *pArchitecture
1614 ) 1629 )
1615{ 1630{
@@ -1623,34 +1638,34 @@ extern "C" HRESULT DAPI FileExecutableArchitecture(
1623 hFile = ::CreateFileW(wzFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 1638 hFile = ::CreateFileW(wzFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1624 if (hFile == INVALID_HANDLE_VALUE) 1639 if (hFile == INVALID_HANDLE_VALUE)
1625 { 1640 {
1626 ExitWithLastError(hr, "Failed to open file: %ls", wzFile); 1641 FileExitWithLastError(hr, "Failed to open file: %ls", wzFile);
1627 } 1642 }
1628 1643
1629 if (!::ReadFile(hFile, &DosImageHeader, sizeof(DosImageHeader), &cbRead, NULL)) 1644 if (!::ReadFile(hFile, &DosImageHeader, sizeof(DosImageHeader), &cbRead, NULL))
1630 { 1645 {
1631 ExitWithLastError(hr, "Failed to read DOS header from file: %ls", wzFile); 1646 FileExitWithLastError(hr, "Failed to read DOS header from file: %ls", wzFile);
1632 } 1647 }
1633 1648
1634 if (DosImageHeader.e_magic != IMAGE_DOS_SIGNATURE) 1649 if (DosImageHeader.e_magic != IMAGE_DOS_SIGNATURE)
1635 { 1650 {
1636 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); 1651 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
1637 ExitOnRootFailure(hr, "Read invalid DOS header from file: %ls", wzFile); 1652 FileExitOnRootFailure(hr, "Read invalid DOS header from file: %ls", wzFile);
1638 } 1653 }
1639 1654
1640 if (INVALID_SET_FILE_POINTER == ::SetFilePointer(hFile, DosImageHeader.e_lfanew, NULL, FILE_BEGIN)) 1655 if (INVALID_SET_FILE_POINTER == ::SetFilePointer(hFile, DosImageHeader.e_lfanew, NULL, FILE_BEGIN))
1641 { 1656 {
1642 ExitWithLastError(hr, "Failed to seek the NT header in file: %ls", wzFile); 1657 FileExitWithLastError(hr, "Failed to seek the NT header in file: %ls", wzFile);
1643 } 1658 }
1644 1659
1645 if (!::ReadFile(hFile, &NtImageHeader, sizeof(NtImageHeader), &cbRead, NULL)) 1660 if (!::ReadFile(hFile, &NtImageHeader, sizeof(NtImageHeader), &cbRead, NULL))
1646 { 1661 {
1647 ExitWithLastError(hr, "Failed to read NT header from file: %ls", wzFile); 1662 FileExitWithLastError(hr, "Failed to read NT header from file: %ls", wzFile);
1648 } 1663 }
1649 1664
1650 if (NtImageHeader.Signature != IMAGE_NT_SIGNATURE) 1665 if (NtImageHeader.Signature != IMAGE_NT_SIGNATURE)
1651 { 1666 {
1652 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); 1667 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
1653 ExitOnRootFailure(hr, "Read invalid NT header from file: %ls", wzFile); 1668 FileExitOnRootFailure(hr, "Read invalid NT header from file: %ls", wzFile);
1654 } 1669 }
1655 1670
1656 if (IMAGE_SUBSYSTEM_NATIVE == NtImageHeader.OptionalHeader.Subsystem || 1671 if (IMAGE_SUBSYSTEM_NATIVE == NtImageHeader.OptionalHeader.Subsystem ||
@@ -1677,7 +1692,7 @@ extern "C" HRESULT DAPI FileExecutableArchitecture(
1677 { 1692 {
1678 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); 1693 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
1679 } 1694 }
1680 ExitOnFailure(hr, "Unexpected subsystem: %d machine type: %d specified in NT header from file: %ls", NtImageHeader.OptionalHeader.Subsystem, NtImageHeader.FileHeader.Machine, wzFile); 1695 FileExitOnFailure(hr, "Unexpected subsystem: %d machine type: %d specified in NT header from file: %ls", NtImageHeader.OptionalHeader.Subsystem, NtImageHeader.FileHeader.Machine, wzFile);
1681 1696
1682LExit: 1697LExit:
1683 if (hFile != INVALID_HANDLE_VALUE) 1698 if (hFile != INVALID_HANDLE_VALUE)
@@ -1706,7 +1721,7 @@ extern "C" HRESULT DAPI FileToString(
1706 1721
1707 // Check if the file is ANSI 1722 // Check if the file is ANSI
1708 hr = FileRead(&pbFullFileBuffer, &cbFullFileBuffer, wzFile); 1723 hr = FileRead(&pbFullFileBuffer, &cbFullFileBuffer, wzFile);
1709 ExitOnFailure(hr, "Failed to read file: %ls", wzFile); 1724 FileExitOnFailure(hr, "Failed to read file: %ls", wzFile);
1710 1725
1711 if (0 == cbFullFileBuffer) 1726 if (0 == cbFullFileBuffer)
1712 { 1727 {
@@ -1723,7 +1738,7 @@ extern "C" HRESULT DAPI FileToString(
1723 } 1738 }
1724 1739
1725 hr = StrAllocStringAnsi(&sczFileText, reinterpret_cast<LPCSTR>(pbFullFileBuffer + 3), cbFullFileBuffer - 3, CP_UTF8); 1740 hr = StrAllocStringAnsi(&sczFileText, reinterpret_cast<LPCSTR>(pbFullFileBuffer + 3), cbFullFileBuffer - 3, CP_UTF8);
1726 ExitOnFailure(hr, "Failed to convert file %ls from UTF-8 as its BOM indicated", wzFile); 1741 FileExitOnFailure(hr, "Failed to convert file %ls from UTF-8 as its BOM indicated", wzFile);
1727 1742
1728 *psczString = sczFileText; 1743 *psczString = sczFileText;
1729 sczFileText = NULL; 1744 sczFileText = NULL;
@@ -1737,7 +1752,7 @@ extern "C" HRESULT DAPI FileToString(
1737 } 1752 }
1738 1753
1739 hr = StrAllocString(psczString, reinterpret_cast<LPWSTR>(pbFullFileBuffer + 2), (cbFullFileBuffer - 2) / sizeof(WCHAR)); 1754 hr = StrAllocString(psczString, reinterpret_cast<LPWSTR>(pbFullFileBuffer + 2), (cbFullFileBuffer - 2) / sizeof(WCHAR));
1740 ExitOnFailure(hr, "Failed to allocate copy of string"); 1755 FileExitOnFailure(hr, "Failed to allocate copy of string");
1741 } 1756 }
1742 // No BOM, let's try to detect 1757 // No BOM, let's try to detect
1743 else 1758 else
@@ -1763,7 +1778,7 @@ extern "C" HRESULT DAPI FileToString(
1763 { 1778 {
1764 if (E_OUTOFMEMORY == hr) 1779 if (E_OUTOFMEMORY == hr)
1765 { 1780 {
1766 ExitOnFailure(hr, "Failed to convert file %ls from UTF-8", wzFile); 1781 FileExitOnFailure(hr, "Failed to convert file %ls from UTF-8", wzFile);
1767 } 1782 }
1768 } 1783 }
1769 else 1784 else
@@ -1780,7 +1795,7 @@ extern "C" HRESULT DAPI FileToString(
1780 } 1795 }
1781 1796
1782 hr = StrAllocString(psczString, reinterpret_cast<LPWSTR>(pbFullFileBuffer), cbFullFileBuffer / sizeof(WCHAR)); 1797 hr = StrAllocString(psczString, reinterpret_cast<LPWSTR>(pbFullFileBuffer), cbFullFileBuffer / sizeof(WCHAR));
1783 ExitOnFailure(hr, "Failed to allocate copy of string"); 1798 FileExitOnFailure(hr, "Failed to allocate copy of string");
1784 } 1799 }
1785 } 1800 }
1786 1801
@@ -1813,20 +1828,20 @@ extern "C" HRESULT DAPI FileFromString(
1813 { 1828 {
1814 case FILE_ENCODING_UTF8: 1829 case FILE_ENCODING_UTF8:
1815 hr = StrAnsiAllocString(&sczUtf8String, sczString, 0, CP_UTF8); 1830 hr = StrAnsiAllocString(&sczUtf8String, sczString, 0, CP_UTF8);
1816 ExitOnFailure(hr, "Failed to convert string to UTF-8 to write UTF-8 file"); 1831 FileExitOnFailure(hr, "Failed to convert string to UTF-8 to write UTF-8 file");
1817 1832
1818 cbFullFileBuffer = lstrlenA(sczUtf8String); 1833 cbFullFileBuffer = lstrlenA(sczUtf8String);
1819 pcbFullFileBuffer = reinterpret_cast<BYTE *>(sczUtf8String); 1834 pcbFullFileBuffer = reinterpret_cast<BYTE *>(sczUtf8String);
1820 break; 1835 break;
1821 case FILE_ENCODING_UTF8_WITH_BOM: 1836 case FILE_ENCODING_UTF8_WITH_BOM:
1822 hr = StrAnsiAllocString(&sczUtf8String, sczString, 0, CP_UTF8); 1837 hr = StrAnsiAllocString(&sczUtf8String, sczString, 0, CP_UTF8);
1823 ExitOnFailure(hr, "Failed to convert string to UTF-8 to write UTF-8 file"); 1838 FileExitOnFailure(hr, "Failed to convert string to UTF-8 to write UTF-8 file");
1824 1839
1825 cbStrLen = lstrlenA(sczUtf8String); 1840 cbStrLen = lstrlenA(sczUtf8String);
1826 cbFullFileBuffer = sizeof(UTF8BOM) + cbStrLen; 1841 cbFullFileBuffer = sizeof(UTF8BOM) + cbStrLen;
1827 1842
1828 pbFullFileBuffer = reinterpret_cast<BYTE *>(MemAlloc(cbFullFileBuffer, TRUE)); 1843 pbFullFileBuffer = reinterpret_cast<BYTE *>(MemAlloc(cbFullFileBuffer, TRUE));
1829 ExitOnNull(pbFullFileBuffer, hr, E_OUTOFMEMORY, "Failed to allocate memory for output file buffer"); 1844 FileExitOnNull(pbFullFileBuffer, hr, E_OUTOFMEMORY, "Failed to allocate memory for output file buffer");
1830 1845
1831 memcpy_s(pbFullFileBuffer, sizeof(UTF8BOM), UTF8BOM, sizeof(UTF8BOM)); 1846 memcpy_s(pbFullFileBuffer, sizeof(UTF8BOM), UTF8BOM, sizeof(UTF8BOM));
1832 memcpy_s(pbFullFileBuffer + sizeof(UTF8BOM), cbStrLen, sczUtf8String, cbStrLen); 1847 memcpy_s(pbFullFileBuffer + sizeof(UTF8BOM), cbStrLen, sczUtf8String, cbStrLen);
@@ -1841,7 +1856,7 @@ extern "C" HRESULT DAPI FileFromString(
1841 cbFullFileBuffer = sizeof(UTF16BOM) + cbStrLen; 1856 cbFullFileBuffer = sizeof(UTF16BOM) + cbStrLen;
1842 1857
1843 pbFullFileBuffer = reinterpret_cast<BYTE *>(MemAlloc(cbFullFileBuffer, TRUE)); 1858 pbFullFileBuffer = reinterpret_cast<BYTE *>(MemAlloc(cbFullFileBuffer, TRUE));
1844 ExitOnNull(pbFullFileBuffer, hr, E_OUTOFMEMORY, "Failed to allocate memory for output file buffer"); 1859 FileExitOnNull(pbFullFileBuffer, hr, E_OUTOFMEMORY, "Failed to allocate memory for output file buffer");
1845 1860
1846 memcpy_s(pbFullFileBuffer, sizeof(UTF16BOM), UTF16BOM, sizeof(UTF16BOM)); 1861 memcpy_s(pbFullFileBuffer, sizeof(UTF16BOM), UTF16BOM, sizeof(UTF16BOM));
1847 memcpy_s(pbFullFileBuffer + sizeof(UTF16BOM), cbStrLen, sczString, cbStrLen); 1862 memcpy_s(pbFullFileBuffer + sizeof(UTF16BOM), cbStrLen, sczString, cbStrLen);
@@ -1850,7 +1865,7 @@ extern "C" HRESULT DAPI FileFromString(
1850 } 1865 }
1851 1866
1852 hr = FileWrite(wzFile, dwFlagsAndAttributes, pcbFullFileBuffer, cbFullFileBuffer, NULL); 1867 hr = FileWrite(wzFile, dwFlagsAndAttributes, pcbFullFileBuffer, cbFullFileBuffer, NULL);
1853 ExitOnFailure(hr, "Failed to write file from string to: %ls", wzFile); 1868 FileExitOnFailure(hr, "Failed to write file from string to: %ls", wzFile);
1854 1869
1855LExit: 1870LExit:
1856 ReleaseStr(sczUtf8String); 1871 ReleaseStr(sczUtf8String);