forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLoadHelpers.cpp
More file actions
148 lines (135 loc) · 4.51 KB
/
FileLoadHelpers.cpp
File metadata and controls
148 lines (135 loc) · 4.51 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "Codex/Utf8Codex.h"
HRESULT FileLoadHelpers::LoadScriptFromFile(LPCSTR filename, LPCWSTR& contents, bool* isUtf8Out, LPCWSTR* contentsRawOut, UINT* lengthBytesOut, bool printFileOpenError)
{
HRESULT hr = S_OK;
LPCWSTR contentsRaw = nullptr;
LPCUTF8 pRawBytes = nullptr;
UINT lengthBytes = 0;
bool isUtf8 = false;
contents = nullptr;
FILE * file;
//
// Open the file as a binary file to prevent CRT from handling encoding, line-break conversions,
// etc.
//
if (fopen_s(&file, filename, "rb") != 0)
{
if (printFileOpenError)
{
#ifdef _WIN32
DWORD lastError = GetLastError();
char16 wszBuff[512];
fprintf(stderr, "Error in opening file '%s' ", filename);
wszBuff[0] = 0;
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
lastError,
0,
wszBuff,
_countof(wszBuff),
nullptr))
{
fwprintf(stderr, _u(": %s"), wszBuff);
}
fwprintf(stderr, _u("\n"));
#elif defined(_POSIX_VERSION)
fprintf(stderr, "Error in opening file: ");
perror(filename);
#endif
IfFailGo(E_FAIL);
}
else
{
return E_FAIL;
}
}
//
// Determine the file length, in bytes.
//
fseek(file, 0, SEEK_END);
lengthBytes = ftell(file);
fseek(file, 0, SEEK_SET);
contentsRaw = (LPCWSTR)HeapAlloc(GetProcessHeap(), 0, lengthBytes + sizeof(WCHAR));
if (nullptr == contentsRaw)
{
fwprintf(stderr, _u("out of memory"));
IfFailGo(E_OUTOFMEMORY);
}
//
// Read the entire content as a binary block.
//
{
size_t readBytes = fread((void*)contentsRaw, sizeof(char), lengthBytes, file);
if (readBytes < lengthBytes)
{
fwprintf(stderr, _u("readBytes should be equal to lengthBytes"));
IfFailGo(E_FAIL);
}
}
fclose(file);
*(WCHAR*)((byte*)contentsRaw + lengthBytes) = _u('\0'); // Null terminate it. Could be LPCWSTR.
//
// Read encoding, handling any conversion to Unicode.
//
// Warning: The UNICODE buffer for parsing is supposed to be provided by the host.
// This is not a complete read of the encoding. Some encodings like UTF7, UTF1, EBCDIC, SCSU, BOCU could be
// wrongly classified as ANSI
//
pRawBytes = (byte*)contentsRaw;
if ((0xEF == *pRawBytes && 0xBB == *(pRawBytes + 1) && 0xBF == *(pRawBytes + 2)))
{
isUtf8 = true;
}
else if (0xFFFE == *contentsRaw || (0x0000 == *contentsRaw && 0xFEFF == *(contentsRaw + 1)))
{
// unicode unsupported
fwprintf(stderr, _u("unsupported file encoding"));
IfFailGo(E_UNEXPECTED);
}
else if (0xFEFF == *contentsRaw)
{
// unicode LE
contents = contentsRaw;
}
else
{
// Assume UTF8
isUtf8 = true;
}
if (isUtf8)
{
utf8::DecodeOptions decodeOptions = utf8::doAllowInvalidWCHARs;
UINT cUtf16Chars = utf8::ByteIndexIntoCharacterIndex(pRawBytes, lengthBytes, decodeOptions);
contents = (LPCWSTR)HeapAlloc(GetProcessHeap(), 0, (cUtf16Chars + (size_t)1) * sizeof(WCHAR));
if (nullptr == contents)
{
fwprintf(stderr, _u("out of memory"));
IfFailGo(E_OUTOFMEMORY);
}
utf8::DecodeUnitsIntoAndNullTerminate((char16*)contents, pRawBytes, pRawBytes + lengthBytes, decodeOptions);
}
Error:
if (SUCCEEDED(hr) && isUtf8Out)
{
Assert(contentsRawOut);
Assert(lengthBytesOut);
*isUtf8Out = isUtf8;
*contentsRawOut = contentsRaw;
*lengthBytesOut = lengthBytes;
}
else if (contentsRaw && (contentsRaw != contents)) // Otherwise contentsRaw is lost. Free it if it is different to contents.
{
HeapFree(GetProcessHeap(), 0, (void*)contentsRaw);
}
if (contents && FAILED(hr))
{
HeapFree(GetProcessHeap(), 0, (void*)contents);
contents = nullptr;
}
return hr;
}