forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIOHelper.cpp
More file actions
1609 lines (1408 loc) · 52.1 KB
/
FileIOHelper.cpp
File metadata and controls
1609 lines (1408 loc) · 52.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// //
// FileIOHelper.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// TODO: consider including an empty blob singleton (possibly UTF-8/16 too). //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/Support/FileIOHelper.h"
#include "dxc/Support/Global.h"
#include "dxc/Support/Unicode.h"
#include "dxc/Support/WinFunctions.h"
#include "dxc/Support/WinIncludes.h"
#include "dxc/Support/microcom.h"
#include "dxc/dxcapi.h"
#include <algorithm>
#include <memory>
#ifdef _WIN32
#include <intsafe.h>
#endif
// CP_UTF8 is defined in WinNls.h, but others we use are not defined there.
// See matching value definitions here:
// https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
// We detect all these through BOM.
#define CP_UTF16LE 1200
#define CP_UTF16BE 1201
#define CP_UTF32LE 12000
#define CP_UTF32BE 12001
// Alias for CP_UTF16LE, which is the only one we actually handle.
#define CP_UTF16 CP_UTF16LE
struct HeapMalloc : public IMalloc {
public:
ULONG STDMETHODCALLTYPE AddRef() override { return 1; }
ULONG STDMETHODCALLTYPE Release() override { return 1; }
STDMETHODIMP QueryInterface(REFIID iid, void **ppvObject) override {
return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
}
void *STDMETHODCALLTYPE Alloc(SIZE_T cb) override {
return HeapAlloc(GetProcessHeap(), 0, cb);
}
void *STDMETHODCALLTYPE Realloc(void *pv, SIZE_T cb) override {
return HeapReAlloc(GetProcessHeap(), 0, pv, cb);
}
void STDMETHODCALLTYPE Free(void *pv) override {
HeapFree(GetProcessHeap(), 0, pv);
}
SIZE_T STDMETHODCALLTYPE GetSize(void *pv) override {
return HeapSize(GetProcessHeap(), 0, pv);
}
int STDMETHODCALLTYPE DidAlloc(void *pv) override {
return -1; // don't know
}
void STDMETHODCALLTYPE HeapMinimize(void) override {}
};
static HeapMalloc g_HeapMalloc;
namespace hlsl {
IMalloc *GetGlobalHeapMalloc() throw() { return &g_HeapMalloc; }
HRESULT ReadBinaryFile(IMalloc *pMalloc, LPCWSTR pFileName, void **ppData,
DWORD *pDataSize) throw() {
HANDLE hFile = CreateFileW(pFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE) {
return HRESULT_FROM_WIN32(GetLastError());
}
CHandle h(hFile);
LARGE_INTEGER FileSize;
if (!GetFileSizeEx(hFile, &FileSize)) {
return HRESULT_FROM_WIN32(GetLastError());
}
if (FileSize.u.HighPart != 0) {
return DXC_E_INPUT_FILE_TOO_LARGE;
}
char *pData = (char *)pMalloc->Alloc(FileSize.u.LowPart);
if (!pData) {
return E_OUTOFMEMORY;
}
DWORD BytesRead;
if (!ReadFile(hFile, pData, FileSize.u.LowPart, &BytesRead, nullptr)) {
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
pMalloc->Free(pData);
return hr;
}
DXASSERT(FileSize.u.LowPart == BytesRead, "ReadFile operation failed");
*ppData = pData;
*pDataSize = FileSize.u.LowPart;
return S_OK;
}
HRESULT ReadBinaryFile(LPCWSTR pFileName, void **ppData,
DWORD *pDataSize) throw() {
return ReadBinaryFile(GetGlobalHeapMalloc(), pFileName, ppData, pDataSize);
}
HRESULT WriteBinaryFile(LPCWSTR pFileName, const void *pData,
DWORD DataSize) throw() {
HANDLE hFile = CreateFileW(pFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE) {
return HRESULT_FROM_WIN32(GetLastError());
}
CHandle h(hFile);
DWORD BytesWritten;
if (!WriteFile(hFile, pData, DataSize, &BytesWritten, nullptr)) {
return HRESULT_FROM_WIN32(GetLastError());
}
DXASSERT(DataSize == BytesWritten, "WriteFile operation failed");
return S_OK;
}
UINT32 DxcCodePageFromBytes(const char *bytes, size_t byteLen) throw() {
UINT32 codePage;
if (byteLen >= 4) {
// Now try to use the BOM to check for Unicode encodings
char bom[4] = {bytes[0], bytes[1], bytes[2], bytes[3]};
if (memcmp(bom, "\xef\xbb\xbf", 3) == 0) {
codePage = CP_UTF8;
} else if (byteLen > 4 && memcmp(bom, "\xff\xfe\x00\x00", 4) == 0) {
// byteLen > 4 to avoid mistaking empty UTF-16 LE BOM + null-terminator
// for UTF-32 LE BOM without null-terminator.
// If it's an empty UTF-32 LE with no null-termination,
// it's harmless to interpret as empty UTF-16 LE with null-termination.
codePage = CP_UTF32LE;
} else if (memcmp(bom, "\x00\x00\xfe\xff", 4) == 0) {
codePage = CP_UTF32BE;
} else if (memcmp(bom, "\xff\xfe", 2) == 0) {
codePage = CP_UTF16LE;
} else if (memcmp(bom, "\xfe\xff", 2) == 0) {
codePage = CP_UTF16BE;
} else {
codePage = CP_ACP;
}
} else {
codePage = CP_ACP;
}
return codePage;
}
unsigned GetBomLengthFromCodePage(UINT32 codePage) {
switch (codePage) {
case CP_UTF8:
return 3;
case CP_UTF32LE:
case CP_UTF32BE:
return 4;
case CP_UTF16LE:
case CP_UTF16BE:
return 2;
default:
return 0;
}
}
static unsigned CharSizeFromCodePage(UINT32 codePage) {
switch (codePage) {
case CP_UTF32LE:
case CP_UTF32BE:
return 4;
case CP_UTF16LE:
case CP_UTF16BE:
return 2;
default:
return 1;
}
}
// We do not handle translation from these code page values.
static bool IsUnsupportedUtfCodePage(UINT32 codePage) {
switch (codePage) {
#ifdef _WIN32
case CP_UTF32LE:
#endif
case CP_UTF32BE:
case CP_UTF16BE:
return true;
}
return false;
}
unsigned GetBomLengthFromBytes(const char *bytes, size_t byteLen) throw() {
return GetBomLengthFromCodePage(DxcCodePageFromBytes(bytes, byteLen));
}
#define IsSizeWcharAligned(size) (((size) & (sizeof(wchar_t) - 1)) == 0)
template <typename _char>
bool IsUtfBufferNullTerminated(LPCVOID pBuffer, SIZE_T size) {
return (size >= sizeof(_char) && (size & (sizeof(_char) - 1)) == 0 &&
reinterpret_cast<const _char *>(
pBuffer)[(size / sizeof(_char)) - 1] == 0);
}
static bool IsBufferNullTerminated(LPCVOID pBuffer, SIZE_T size,
UINT32 codePage) {
switch (codePage) {
case DXC_CP_UTF8:
return IsUtfBufferNullTerminated<char>(pBuffer, size);
case DXC_CP_WIDE:
return IsUtfBufferNullTerminated<wchar_t>(pBuffer, size);
default:
return false;
}
}
template <typename _char>
bool IsUtfBufferEmptyString(LPCVOID pBuffer, SIZE_T size) {
return (size == 0 || (size == sizeof(_char) &&
reinterpret_cast<const _char *>(pBuffer)[0] == 0));
}
static bool IsBufferEmptyString(LPCVOID pBuffer, SIZE_T size, UINT32 codePage) {
switch (codePage) {
case DXC_CP_UTF8:
return IsUtfBufferEmptyString<char>(pBuffer, size);
case DXC_CP_WIDE:
return IsUtfBufferEmptyString<wchar_t>(pBuffer, size);
default:
return IsUtfBufferEmptyString<char>(pBuffer, size);
}
}
class DxcBlobNoEncoding_Impl : public IDxcBlobEncoding {
public:
typedef IDxcBlobEncoding Base;
static const UINT32 CodePage = CP_ACP;
};
class DxcBlobWide_Impl : public IDxcBlobWide {
public:
static const UINT32 CodePage = DXC_CP_WIDE;
typedef IDxcBlobWide Base;
virtual LPCWSTR STDMETHODCALLTYPE GetStringPointer(void) override {
if (GetBufferSize() < sizeof(wchar_t)) {
return L""; // Special case for empty string blob
}
DXASSERT(IsSizeWcharAligned(GetBufferSize()),
"otherwise, buffer size is not even multiple of wchar_t");
DXASSERT(*(const wchar_t *)((const BYTE *)GetBufferPointer() +
GetBufferSize() - sizeof(wchar_t)) == L'\0',
"otherwise buffer is not null terminated.");
return (LPCWSTR)GetBufferPointer();
}
virtual SIZE_T STDMETHODCALLTYPE GetStringLength(void) override {
SIZE_T bufSize = GetBufferSize();
return bufSize ? (bufSize / sizeof(wchar_t)) - 1 : 0;
}
};
class DxcBlobUtf8_Impl : public IDxcBlobUtf8 {
public:
static const UINT32 CodePage = CP_UTF8;
typedef IDxcBlobUtf8 Base;
virtual LPCSTR STDMETHODCALLTYPE GetStringPointer(void) override {
if (GetBufferSize() < sizeof(char)) {
return ""; // Special case for empty string blob
}
DXASSERT(*((const char *)GetBufferPointer() + GetBufferSize() - 1) == '\0',
"otherwise buffer is not null terminated.");
return (LPCSTR)GetBufferPointer();
}
virtual SIZE_T STDMETHODCALLTYPE GetStringLength(void) override {
SIZE_T bufSize = GetBufferSize();
return bufSize ? (bufSize / sizeof(char)) - 1 : 0;
}
};
template <typename _T> class InternalDxcBlobEncoding_Impl : public _T {
private:
DXC_MICROCOM_TM_REF_FIELDS() // an underlying m_pMalloc that owns this
LPCVOID m_Buffer = nullptr;
IUnknown *m_Owner =
nullptr; // IMalloc when MallocFree is true, owning the buffer
SIZE_T m_BufferSize;
unsigned m_EncodingKnown : 1;
unsigned m_MallocFree : 1;
UINT32 m_CodePage;
public:
DXC_MICROCOM_ADDREF_IMPL(m_dwRef)
ULONG STDMETHODCALLTYPE Release() override {
// Because blobs are also used by tests and utilities, we avoid using TLS.
ULONG result = (ULONG)--m_dwRef;
if (result == 0) {
CComPtr<IMalloc> pTmp(m_pMalloc);
this->InternalDxcBlobEncoding_Impl::~InternalDxcBlobEncoding_Impl();
pTmp->Free(this);
}
return result;
}
DXC_MICROCOM_TM_CTOR(InternalDxcBlobEncoding_Impl)
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
void **ppvObject) override {
return DoBasicQueryInterface<IDxcBlob, IDxcBlobEncoding, typename _T::Base>(
this, iid, ppvObject);
}
~InternalDxcBlobEncoding_Impl() {
if (m_MallocFree) {
((IMalloc *)m_Owner)->Free(const_cast<void *>(m_Buffer));
}
if (m_Owner != nullptr) {
m_Owner->Release();
}
}
static HRESULT CreateFromBlob(IDxcBlob *pBlob, IMalloc *pMalloc,
bool encodingKnown, UINT32 codePage,
InternalDxcBlobEncoding_Impl **pEncoding) {
*pEncoding = InternalDxcBlobEncoding_Impl::Alloc(pMalloc);
if (*pEncoding == nullptr) {
return E_OUTOFMEMORY;
}
DXASSERT(_T::CodePage == CP_ACP ||
(encodingKnown && _T::CodePage == codePage),
"encoding must match type");
pBlob->AddRef();
(*pEncoding)->m_Owner = pBlob;
(*pEncoding)->m_Buffer = pBlob->GetBufferPointer();
(*pEncoding)->m_BufferSize = pBlob->GetBufferSize();
(*pEncoding)->m_EncodingKnown = encodingKnown;
(*pEncoding)->m_MallocFree = 0;
(*pEncoding)->m_CodePage = codePage;
(*pEncoding)->AddRef();
return S_OK;
}
static HRESULT CreateFromMalloc(LPCVOID buffer, IMalloc *pIMalloc,
SIZE_T bufferSize, bool encodingKnown,
UINT32 codePage,
InternalDxcBlobEncoding_Impl **pEncoding) {
*pEncoding = InternalDxcBlobEncoding_Impl::Alloc(pIMalloc);
if (*pEncoding == nullptr) {
*pEncoding = nullptr;
return E_OUTOFMEMORY;
}
DXASSERT(_T::CodePage == CP_ACP ||
(encodingKnown && _T::CodePage == codePage),
"encoding must match type");
DXASSERT(buffer || bufferSize == 0,
"otherwise, nullptr with non-zero size provided");
pIMalloc->AddRef();
(*pEncoding)->m_Owner = pIMalloc;
(*pEncoding)->m_Buffer = buffer;
(*pEncoding)->m_BufferSize = bufferSize;
(*pEncoding)->m_EncodingKnown = encodingKnown;
(*pEncoding)->m_MallocFree = buffer != nullptr;
(*pEncoding)->m_CodePage = codePage;
(*pEncoding)->AddRef();
return S_OK;
}
void AdjustPtrAndSize(unsigned offset, unsigned size) {
DXASSERT(offset < m_BufferSize, "else caller will overflow");
DXASSERT(offset + size <= m_BufferSize, "else caller will overflow");
m_Buffer = (const uint8_t *)m_Buffer + offset;
m_BufferSize = size;
}
virtual LPVOID STDMETHODCALLTYPE GetBufferPointer(void) override {
return const_cast<LPVOID>(m_Buffer);
}
virtual SIZE_T STDMETHODCALLTYPE GetBufferSize(void) override {
return m_BufferSize;
}
virtual HRESULT STDMETHODCALLTYPE GetEncoding(BOOL *pKnown,
UINT32 *pCodePage) override {
*pKnown = m_EncodingKnown ? TRUE : FALSE;
*pCodePage = m_CodePage;
return S_OK;
}
// Relatively dangerous API. This means the buffer should be pinned for as
// long as this object is alive.
void ClearFreeFlag() { m_MallocFree = 0; }
};
typedef InternalDxcBlobEncoding_Impl<DxcBlobNoEncoding_Impl>
InternalDxcBlobEncoding;
typedef InternalDxcBlobEncoding_Impl<DxcBlobWide_Impl> InternalDxcBlobWide;
typedef InternalDxcBlobEncoding_Impl<DxcBlobUtf8_Impl> InternalDxcBlobUtf8;
static HRESULT CodePageBufferToWide(UINT32 codePage, LPCVOID bufferPointer,
SIZE_T bufferSize,
CDxcMallocHeapPtr<WCHAR> &wideNewCopy,
UINT32 *pConvertedCharCount) {
*pConvertedCharCount = 0;
// If the buffer is empty, don't dereference bufferPointer at all.
// Keep the null terminator post-condition.
if (IsBufferEmptyString(bufferPointer, bufferSize, codePage)) {
if (!wideNewCopy.Allocate(1))
return E_OUTOFMEMORY;
wideNewCopy.m_pData[0] = L'\0';
DXASSERT(*pConvertedCharCount == 0, "else didn't init properly");
return S_OK;
}
if (IsUnsupportedUtfCodePage(codePage))
return DXC_E_STRING_ENCODING_FAILED;
// Calculate the length of the buffer in wchar_t elements.
int numToConvertWide =
MultiByteToWideChar(codePage, MB_ERR_INVALID_CHARS, (LPCSTR)bufferPointer,
bufferSize, nullptr, 0);
if (numToConvertWide == 0)
return HRESULT_FROM_WIN32(GetLastError());
// Add an extra character in case we need it for null-termination
unsigned buffSizeWide;
IFR(Int32ToUInt32(numToConvertWide, &buffSizeWide));
IFR(UInt32Add(buffSizeWide, 1, &buffSizeWide));
IFR(UInt32Mult(buffSizeWide, sizeof(WCHAR), &buffSizeWide));
wideNewCopy.AllocateBytes(buffSizeWide);
IFROOM(wideNewCopy.m_pData);
int numActuallyConvertedWide =
MultiByteToWideChar(codePage, MB_ERR_INVALID_CHARS, (LPCSTR)bufferPointer,
bufferSize, wideNewCopy, buffSizeWide);
if (numActuallyConvertedWide == 0)
return HRESULT_FROM_WIN32(GetLastError());
if (numActuallyConvertedWide < 0)
return E_OUTOFMEMORY;
// If all we have is null terminator, return with zero count.
if (wideNewCopy.m_pData[0] == L'\0') {
DXASSERT(*pConvertedCharCount == 0, "else didn't init properly");
return S_OK;
}
if ((UINT32)numActuallyConvertedWide < (buffSizeWide / sizeof(wchar_t)) &&
wideNewCopy.m_pData[numActuallyConvertedWide - 1] != L'\0') {
wideNewCopy.m_pData[numActuallyConvertedWide++] = L'\0';
}
*pConvertedCharCount = (UINT32)numActuallyConvertedWide;
return S_OK;
}
static HRESULT CodePageBufferToUtf8(UINT32 codePage, LPCVOID bufferPointer,
SIZE_T bufferSize, IMalloc *pMalloc,
CDxcMallocHeapPtr<char> &utf8NewCopy,
UINT32 *pConvertedCharCount) {
*pConvertedCharCount = 0;
CDxcMallocHeapPtr<WCHAR> wideNewCopy(pMalloc);
UINT32 wideCharCount = 0;
const WCHAR *wideChars = nullptr;
if (codePage == DXC_CP_WIDE) {
if (!IsSizeWcharAligned(bufferSize))
throw hlsl::Exception(DXC_E_STRING_ENCODING_FAILED,
"Error in encoding argument specified");
wideChars = (const WCHAR *)bufferPointer;
wideCharCount = bufferSize / sizeof(wchar_t);
} else if (bufferSize) {
IFR(CodePageBufferToWide(codePage, bufferPointer, bufferSize, wideNewCopy,
&wideCharCount));
wideChars = wideNewCopy.m_pData;
}
// If the buffer is empty, don't dereference bufferPointer at all.
// Keep the null terminator post-condition.
if (IsUtfBufferEmptyString<wchar_t>(wideChars, wideCharCount)) {
if (!utf8NewCopy.Allocate(1))
return E_OUTOFMEMORY;
DXASSERT(*pConvertedCharCount == 0, "else didn't init properly");
utf8NewCopy.m_pData[0] = '\0';
return S_OK;
}
int numToConvertUtf8 = WideCharToMultiByte(
CP_UTF8, 0, wideChars, wideCharCount, NULL, 0, NULL, NULL);
if (numToConvertUtf8 == 0)
return HRESULT_FROM_WIN32(GetLastError());
UINT32 buffSizeUtf8;
IFR(Int32ToUInt32(numToConvertUtf8, &buffSizeUtf8));
if (!IsBufferNullTerminated(wideChars, wideCharCount * sizeof(wchar_t),
DXC_CP_WIDE)) {
// If original size doesn't include null-terminator,
// we have to add one to the converted buffer.
IFR(UInt32Add(buffSizeUtf8, 1, &buffSizeUtf8));
}
utf8NewCopy.AllocateBytes(buffSizeUtf8);
IFROOM(utf8NewCopy.m_pData);
int numActuallyConvertedUtf8 =
WideCharToMultiByte(CP_UTF8, 0, wideChars, wideCharCount, utf8NewCopy,
buffSizeUtf8, NULL, NULL);
if (numActuallyConvertedUtf8 == 0)
return HRESULT_FROM_WIN32(GetLastError());
if (numActuallyConvertedUtf8 < 0)
return E_OUTOFMEMORY;
if ((UINT32)numActuallyConvertedUtf8 < buffSizeUtf8 &&
utf8NewCopy.m_pData[numActuallyConvertedUtf8 - 1] != '\0') {
utf8NewCopy.m_pData[numActuallyConvertedUtf8++] = '\0';
}
*pConvertedCharCount = (UINT32)numActuallyConvertedUtf8;
return S_OK;
}
static bool TryCreateEmptyBlobUtf(UINT32 codePage, IMalloc *pMalloc,
IDxcBlobEncoding **ppBlobEncoding) {
if (codePage == CP_UTF8) {
InternalDxcBlobUtf8 *internalUtf8;
if (DXC_FAILED(InternalDxcBlobUtf8::CreateFromMalloc(
nullptr, pMalloc, 0, true, codePage, &internalUtf8)))
return false;
*ppBlobEncoding = internalUtf8;
return true;
} else if (codePage == DXC_CP_WIDE) {
InternalDxcBlobWide *internalWide;
if (DXC_FAILED(InternalDxcBlobWide::CreateFromMalloc(
nullptr, pMalloc, 0, true, codePage, &internalWide)))
return false;
*ppBlobEncoding = internalWide;
return true;
}
return false;
}
static bool TryCreateBlobUtfFromBlob(IDxcBlob *pFromBlob, UINT32 codePage,
IMalloc *pMalloc,
IDxcBlobEncoding **ppBlobEncoding) {
// Try to create a IDxcBlobUtf8 or IDxcBlobWide
if (IsBlobNullOrEmpty(pFromBlob)) {
return TryCreateEmptyBlobUtf(codePage, pMalloc, ppBlobEncoding);
} else if (IsBufferNullTerminated(pFromBlob->GetBufferPointer(),
pFromBlob->GetBufferSize(), codePage)) {
if (codePage == CP_UTF8) {
InternalDxcBlobUtf8 *internalUtf8;
if (DXC_FAILED(InternalDxcBlobUtf8::CreateFromBlob(
pFromBlob, pMalloc, true, codePage, &internalUtf8)))
return false;
*ppBlobEncoding = internalUtf8;
return true;
} else if (codePage == DXC_CP_WIDE) {
InternalDxcBlobWide *internalWide;
if (DXC_FAILED(InternalDxcBlobWide::CreateFromBlob(
pFromBlob, pMalloc, true, codePage, &internalWide)))
return false;
*ppBlobEncoding = internalWide;
return true;
}
}
return false;
}
HRESULT DxcCreateBlob(LPCVOID pPtr, SIZE_T size, bool bPinned, bool bCopy,
bool encodingKnown, UINT32 codePage, IMalloc *pMalloc,
IDxcBlobEncoding **ppBlobEncoding) throw() {
IFRBOOL(!(bPinned && bCopy), E_INVALIDARG);
IFRBOOL(ppBlobEncoding, E_INVALIDARG);
*ppBlobEncoding = nullptr;
bool bNullTerminated =
encodingKnown ? IsBufferNullTerminated(pPtr, size, codePage) : false;
unsigned bomSize = (encodingKnown && codePage != CP_ACP)
? GetBomLengthFromBytes((const char *)pPtr, size)
: 0;
if (bomSize) {
// Adjust pPtr and size to skip BOM.
// When !encodingKnown or codePage == CP_ACP, BOM will be skipped when
// interpreting as text and translating to unicode, since at this point,
// the buffer could be an arbitrary binary blob.
// There is an odd corner case with BOM detection where an empty
// non-null-terminated UTF-32 LE buffer with BOM would be interpreted
// as an empty null-terminated UTF-16 LE buffer with a BOM.
// This won't matter in the end, since both cases are empty buffers, and
// will map to the empty buffer case with the desired codePage setting.
pPtr = (const char *)pPtr + bomSize;
size -= bomSize;
}
bool emptyString = !pPtr || !size;
if (bNullTerminated) {
DXASSERT_NOMSG(pPtr && size && encodingKnown);
emptyString = size == CharSizeFromCodePage(codePage);
}
if (!pMalloc)
pMalloc = DxcGetThreadMallocNoRef();
// Handle empty blob
if (emptyString) {
if (encodingKnown &&
TryCreateEmptyBlobUtf(codePage, pMalloc, ppBlobEncoding))
return S_OK;
InternalDxcBlobEncoding *pInternalEncoding;
IFR(InternalDxcBlobEncoding::CreateFromMalloc(
nullptr, pMalloc, 0, encodingKnown, codePage, &pInternalEncoding));
*ppBlobEncoding = pInternalEncoding;
if (pPtr && !(bCopy || bPinned)) {
// Free memory as we're not taking ownership of it
pMalloc->Free(const_cast<void *>(pPtr));
}
return S_OK;
}
if (bPinned) {
if (encodingKnown) {
if (bNullTerminated) {
if (codePage == CP_UTF8) {
InternalDxcBlobUtf8 *internalUtf8;
IFR(InternalDxcBlobUtf8::CreateFromMalloc(pPtr, pMalloc, size, true,
codePage, &internalUtf8));
*ppBlobEncoding = internalUtf8;
internalUtf8->ClearFreeFlag();
return S_OK;
} else if (codePage == DXC_CP_WIDE) {
InternalDxcBlobWide *internalWide;
IFR(InternalDxcBlobWide::CreateFromMalloc(pPtr, pMalloc, size, true,
codePage, &internalWide));
*ppBlobEncoding = internalWide;
internalWide->ClearFreeFlag();
return S_OK;
}
}
}
InternalDxcBlobEncoding *internalEncoding;
IFR(InternalDxcBlobEncoding::CreateFromMalloc(
pPtr, pMalloc, size, encodingKnown, codePage, &internalEncoding));
*ppBlobEncoding = internalEncoding;
internalEncoding->ClearFreeFlag();
return S_OK;
}
void *pData = const_cast<void *>(pPtr);
SIZE_T newSize = size;
CDxcMallocHeapPtr<char> heapCopy(pMalloc);
if (bCopy) {
if (encodingKnown) {
if (!bNullTerminated) {
if (codePage == CP_UTF8) {
newSize += sizeof(char);
bNullTerminated = true;
} else if (codePage == DXC_CP_WIDE) {
newSize += sizeof(wchar_t);
bNullTerminated = true;
}
}
}
heapCopy.AllocateBytes(newSize);
pData = heapCopy.m_pData;
if (pData == nullptr)
return E_OUTOFMEMORY;
if (pPtr)
memcpy(pData, pPtr, size);
else
memset(pData, 0, size);
}
if (bNullTerminated && codePage == CP_UTF8) {
if (bCopy && newSize > size)
((char *)pData)[newSize - 1] = 0;
InternalDxcBlobUtf8 *internalUtf8;
IFR(InternalDxcBlobUtf8::CreateFromMalloc(pData, pMalloc, newSize, true,
codePage, &internalUtf8));
*ppBlobEncoding = internalUtf8;
} else if (bNullTerminated && codePage == DXC_CP_WIDE) {
if (bCopy && newSize > size)
((wchar_t *)pData)[(newSize / sizeof(wchar_t)) - 1] = 0;
InternalDxcBlobWide *internalWide;
IFR(InternalDxcBlobWide::CreateFromMalloc(pData, pMalloc, newSize, true,
codePage, &internalWide));
*ppBlobEncoding = internalWide;
} else {
InternalDxcBlobEncoding *internalEncoding;
IFR(InternalDxcBlobEncoding::CreateFromMalloc(
pData, pMalloc, newSize, encodingKnown, codePage, &internalEncoding));
*ppBlobEncoding = internalEncoding;
}
if (bCopy)
heapCopy.Detach();
return S_OK;
}
HRESULT
DxcCreateBlobEncodingFromBlob(IDxcBlob *pFromBlob, UINT32 offset, UINT32 length,
bool encodingKnown, UINT32 codePage,
IMalloc *pMalloc,
IDxcBlobEncoding **ppBlobEncoding) throw() {
IFRBOOL(pFromBlob, E_POINTER);
IFRBOOL(ppBlobEncoding, E_POINTER);
*ppBlobEncoding = nullptr;
if (!pMalloc)
pMalloc = DxcGetThreadMallocNoRef();
InternalDxcBlobEncoding *internalEncoding;
if (offset || length) {
UINT32 end;
IFR(UInt32Add(offset, length, &end));
SIZE_T blobSize = pFromBlob->GetBufferSize();
if (end > blobSize)
return E_INVALIDARG;
IFR(InternalDxcBlobEncoding::CreateFromBlob(
pFromBlob, pMalloc, encodingKnown, codePage, &internalEncoding));
internalEncoding->AdjustPtrAndSize(offset, length);
*ppBlobEncoding = internalEncoding;
return S_OK;
}
if (!encodingKnown || codePage == CP_UTF8) {
IDxcBlobUtf8 *pBlobUtf8;
if (SUCCEEDED(pFromBlob->QueryInterface(&pBlobUtf8))) {
*ppBlobEncoding = pBlobUtf8;
return S_OK;
}
}
if (!encodingKnown || codePage == DXC_CP_WIDE) {
IDxcBlobWide *pBlobWide;
if (SUCCEEDED(pFromBlob->QueryInterface(&pBlobWide))) {
*ppBlobEncoding = pBlobWide;
return S_OK;
}
}
CComPtr<IDxcBlobEncoding> pBlobEncoding;
if (SUCCEEDED(pFromBlob->QueryInterface(&pBlobEncoding))) {
BOOL thisEncodingKnown;
UINT32 thisEncoding;
IFR(pBlobEncoding->GetEncoding(&thisEncodingKnown, &thisEncoding));
bool encodingMatches =
thisEncodingKnown && encodingKnown && codePage == thisEncoding;
if (!encodingKnown && thisEncodingKnown) {
codePage = thisEncoding;
encodingKnown = thisEncodingKnown;
encodingMatches = true;
}
if (encodingMatches) {
if (!TryCreateBlobUtfFromBlob(pFromBlob, codePage, pMalloc,
ppBlobEncoding)) {
*ppBlobEncoding = pBlobEncoding.Detach();
}
return S_OK;
}
if (encodingKnown) {
if (TryCreateBlobUtfFromBlob(pFromBlob, codePage, pMalloc,
ppBlobEncoding)) {
return S_OK;
}
IFR(InternalDxcBlobEncoding::CreateFromBlob(pFromBlob, pMalloc, true,
codePage, &internalEncoding));
*ppBlobEncoding = internalEncoding;
return S_OK;
}
DXASSERT(!encodingKnown && !thisEncodingKnown, "otherwise, missing case");
*ppBlobEncoding = pBlobEncoding.Detach();
return S_OK;
}
if (encodingKnown &&
TryCreateBlobUtfFromBlob(pFromBlob, codePage, pMalloc, ppBlobEncoding)) {
return S_OK;
}
IFR(InternalDxcBlobEncoding::CreateFromBlob(pFromBlob, pMalloc, encodingKnown,
codePage, &internalEncoding));
*ppBlobEncoding = internalEncoding;
return S_OK;
}
HRESULT DxcCreateBlobFromBlob(IDxcBlob *pBlob, UINT32 offset, UINT32 length,
IDxcBlob **ppResult) throw() {
IFRBOOL(ppResult, E_POINTER);
*ppResult = nullptr;
IDxcBlobEncoding *pResult;
IFR(DxcCreateBlobEncodingFromBlob(pBlob, offset, length, false, 0,
DxcGetThreadMallocNoRef(), &pResult));
*ppResult = pResult;
return S_OK;
}
HRESULT
DxcCreateBlobOnMalloc(LPCVOID pData, IMalloc *pIMalloc, UINT32 size,
IDxcBlob **ppResult) throw() {
IFRBOOL(ppResult, E_POINTER);
*ppResult = nullptr;
IDxcBlobEncoding *pResult;
IFR(DxcCreateBlob(pData, size, false, false, false, 0, pIMalloc, &pResult));
*ppResult = pResult;
return S_OK;
}
HRESULT
DxcCreateBlobOnHeapCopy(LPCVOID pData, UINT32 size,
IDxcBlob **ppResult) throw() {
IFRBOOL(ppResult, E_POINTER);
*ppResult = nullptr;
IDxcBlobEncoding *pResult;
IFR(DxcCreateBlob(pData, size, false, true, false, 0,
DxcGetThreadMallocNoRef(), &pResult));
*ppResult = pResult;
return S_OK;
}
HRESULT
DxcCreateBlobFromFile(IMalloc *pMalloc, LPCWSTR pFileName, UINT32 *pCodePage,
IDxcBlobEncoding **ppBlobEncoding) throw() {
if (pFileName == nullptr || ppBlobEncoding == nullptr) {
return E_POINTER;
}
LPVOID pData;
DWORD dataSize;
*ppBlobEncoding = nullptr;
HRESULT hr = ReadBinaryFile(pMalloc, pFileName, &pData, &dataSize);
if (FAILED(hr))
return hr;
bool known = (pCodePage != nullptr);
UINT32 codePage = (pCodePage != nullptr) ? *pCodePage : 0;
hr = DxcCreateBlob(pData, dataSize, false, false, known, codePage, pMalloc,
ppBlobEncoding);
if (FAILED(hr))
pMalloc->Free(pData);
return hr;
}
HRESULT DxcCreateBlobFromFile(LPCWSTR pFileName, UINT32 *pCodePage,
IDxcBlobEncoding **ppBlobEncoding) throw() {
return DxcCreateBlobFromFile(DxcGetThreadMallocNoRef(), pFileName, pCodePage,
ppBlobEncoding);
}
HRESULT
DxcCreateBlobWithEncodingSet(IMalloc *pMalloc, IDxcBlob *pBlob, UINT32 codePage,
IDxcBlobEncoding **ppBlobEncoding) throw() {
return DxcCreateBlobEncodingFromBlob(pBlob, 0, 0, true, codePage, pMalloc,
ppBlobEncoding);
}
HRESULT
DxcCreateBlobWithEncodingSet(IDxcBlob *pBlob, UINT32 codePage,
IDxcBlobEncoding **ppBlobEncoding) throw() {
return DxcCreateBlobEncodingFromBlob(pBlob, 0, 0, true, codePage, nullptr,
ppBlobEncoding);
}
HRESULT
DxcCreateBlobWithEncodingFromPinned(LPCVOID pText, UINT32 size, UINT32 codePage,
IDxcBlobEncoding **pBlobEncoding) throw() {
return DxcCreateBlob(pText, size, true, false, true, codePage, nullptr,
pBlobEncoding);
}
HRESULT DxcCreateBlobFromPinned(LPCVOID pText, UINT32 size,
IDxcBlob **pBlob) throw() {
CComPtr<IDxcBlobEncoding> pBlobEncoding;
DxcCreateBlob(pText, size, true, false, false, CP_ACP, nullptr,
&pBlobEncoding);
return pBlobEncoding.QueryInterface(pBlob);
}
HRESULT
DxcCreateBlobWithEncodingFromStream(IStream *pStream, bool newInstanceAlways,
UINT32 codePage,
IDxcBlobEncoding **ppBlobEncoding) throw() {
IFRBOOL(ppBlobEncoding, E_POINTER);
*ppBlobEncoding = nullptr;
if (pStream == nullptr) {
return S_OK;
}
// Try to reuse the existing stream.
if (!newInstanceAlways) {
CComPtr<IDxcBlobEncoding> blobEncoding;
if (SUCCEEDED(pStream->QueryInterface(&blobEncoding))) {
*ppBlobEncoding = blobEncoding.Detach();
return S_OK;
}
}
// Layer over the blob if possible.
CComPtr<IDxcBlob> blob;
if (SUCCEEDED(pStream->QueryInterface(&blob))) {
return DxcCreateBlobWithEncodingSet(blob, codePage, ppBlobEncoding);
}
// Create a copy of contents, last resort.
// TODO: implement when we find this codepath internally
return E_NOTIMPL;
}
HRESULT
DxcCreateBlobWithEncodingOnHeapCopy(LPCVOID pText, UINT32 size, UINT32 codePage,
IDxcBlobEncoding **pBlobEncoding) throw() {
return DxcCreateBlob(pText, size, false, true, true, codePage, nullptr,
pBlobEncoding);
}
HRESULT
DxcCreateBlobWithEncodingOnMalloc(LPCVOID pText, IMalloc *pIMalloc, UINT32 size,
UINT32 codePage,
IDxcBlobEncoding **pBlobEncoding) throw() {
return DxcCreateBlob(pText, size, false, false, true, codePage, pIMalloc,
pBlobEncoding);
}
HRESULT
DxcCreateBlobWithEncodingOnMallocCopy(
IMalloc *pIMalloc, LPCVOID pText, UINT32 size, UINT32 codePage,
IDxcBlobEncoding **ppBlobEncoding) throw() {
return DxcCreateBlob(pText, size, false, true, true, codePage, pIMalloc,
ppBlobEncoding);
}
HRESULT
DxcGetBlobAsUtf8(IDxcBlob *pBlob, IMalloc *pMalloc,
IDxcBlobUtf8 **pBlobEncoding, UINT32 defaultCodePage) throw() {
IFRBOOL(pBlob, E_POINTER);
IFRBOOL(pBlobEncoding, E_POINTER);
*pBlobEncoding = nullptr;
if (SUCCEEDED(pBlob->QueryInterface(pBlobEncoding)))
return S_OK;
HRESULT hr;
CComPtr<IDxcBlobEncoding> pSourceBlob;
UINT32 codePage = CP_ACP;
BOOL known = FALSE;
if (SUCCEEDED(pBlob->QueryInterface(&pSourceBlob))) {
if (FAILED(hr = pSourceBlob->GetEncoding(&known, &codePage)))
return hr;
}
const char *bufferPointer = (const char *)pBlob->GetBufferPointer();
SIZE_T blobLen = pBlob->GetBufferSize();
unsigned bomSize = 0;
if (!known || codePage == CP_ACP) {
// Try to determine encoding from BOM.
// If encoding was known, any BOM should have been stripped already.
codePage = DxcCodePageFromBytes(bufferPointer, blobLen);
bomSize = GetBomLengthFromCodePage(codePage);
// BOM exists, adjust pointer and size to strip.
bufferPointer += bomSize;
blobLen -= bomSize;
// If no BOM, use encoding option if specified
if (codePage == CP_ACP && defaultCodePage != CP_ACP) {
codePage = defaultCodePage;
}
}
if (!pMalloc)
pMalloc = DxcGetThreadMallocNoRef();
CDxcMallocHeapPtr<char> utf8NewCopy(pMalloc);
UINT32 utf8CharCount = 0;
// Reuse or copy the underlying blob depending on null-termination
if (codePage == CP_UTF8) {
utf8CharCount = blobLen;
if (IsBufferNullTerminated(bufferPointer, blobLen, CP_UTF8)) {
// Already null-terminated, reference other blob's memory
InternalDxcBlobUtf8 *internalEncoding;
hr = InternalDxcBlobUtf8::CreateFromBlob(pBlob, pMalloc, true, CP_UTF8,
&internalEncoding);
if (SUCCEEDED(hr)) {
// Adjust if buffer has BOM; blobLen is already adjusted.
if (bomSize)
internalEncoding->AdjustPtrAndSize(bomSize, blobLen);
*pBlobEncoding = internalEncoding;
}
return hr;
} else if (utf8CharCount > 0) {
// Copy to new buffer and null-terminate
if (!utf8NewCopy.Allocate(utf8CharCount + 1))
return E_OUTOFMEMORY;
memcpy(utf8NewCopy.m_pData, bufferPointer, utf8CharCount);
utf8NewCopy.m_pData[utf8CharCount++] = 0;
}