forked from CloverHackyColor/CloverBootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.cpp
More file actions
executable file
·207 lines (167 loc) · 5.34 KB
/
Copy pathPlatform.cpp
File metadata and controls
executable file
·207 lines (167 loc) · 5.34 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
//
// Platform.cpp
// cpp_tests
//
// Created by jief on 23.02.20.
// Copyright © 2020 JF Knudsen. All rights reserved.
//
#include "Platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include <string>
#include <locale>
#include <codecvt>
#include <vector>
#include "../../../rEFIt_UEFI/cpp_foundation/utf8Conversion.h"
void CpuDeadLoop(void)
{
exit(1);
}
static char *dull_replace(const char *in, const char *pattern, const char *by, char* res)
{
// size_t outsize = strlen(in) + 1;
// TODO maybe avoid reallocing by counting the non-overlapping occurences of pattern
// char *res = malloc(outsize);
// use this to iterate over the output
size_t resoffset = 0;
const char *needle;
while ( (needle = strstr(in, pattern)) ) {
// copy everything up to the pattern
memcpy(res + resoffset, in, (size_t)(needle - in));
resoffset += (size_t)(needle - in);
// skip the pattern in the input-string
in = needle + strlen(pattern);
// adjust space for replacement
// outsize = outsize - strlen(pattern) + strlen(by);
// res = realloc(res, outsize);
// copy the pattern
memcpy(res + resoffset, by, strlen(by));
resoffset += strlen(by);
}
// copy the remaining input
strcpy(res + resoffset, in);
return res;
}
void DebugLog(int DebugMode, const char *FormatString, ...)
{
(void)DebugMode;
char* NewFormat = (char*)alloca(strlen(FormatString)+1);
dull_replace(FormatString, "%a", "%s", NewFormat);
va_list va;
va_start(va, FormatString);
vprintf(NewFormat, va);
va_end(va);
}
void* AllocatePool(UINTN AllocationSize)
{
return (void*)malloc((size_t)AllocationSize);
}
void* ReallocatePool(UINTN OldSize, UINTN NewSize, void* OldBuffer)
{
(void)OldSize;
if ( !OldBuffer ) return AllocatePool(NewSize);
return (void*)realloc(OldBuffer, (size_t)NewSize);
}
void FreePool(const void* Buffer)
{
free((void*)Buffer);
}
void CopyMem(void *Destination, void *Source, UINTN Length)
{
memmove(Destination, Source, (size_t)Length);
}
void PauseForKey(const wchar_t* msg)
{
printf("%ls", msg);
getchar();
}
UINTN AsciiStrLen(const char* String)
{
return (UINTN)strlen(String);
}
INTN AsciiStrCmp (const char *FirstString,const char *SecondString)
{
return (INTN)strcmp(FirstString, SecondString);
}
#if __WCHAR_MAX__ <= 0xFFFFu
#ifndef _MSC_VER
std::string utf16_to_utf8(const wchar_t* ws)
{
std::u16string s((const char16_t*)ws);
std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::codecvt_mode::little_endian>, char16_t> cnv;
std::string utf8 = cnv.to_bytes(s);
// if(cnv.converted() < s.size())
// throw std::runtime_error("incomplete conversion");
return utf8;
}
int is_surrogate(char16_t uc) { return (uc - 0xd800u) < 2048u; }
int is_high_surrogate(char16_t uc) { return (uc & 0xfffffc00) == 0xd800; }
int is_low_surrogate(char16_t uc) { return (uc & 0xfffffc00) == 0xdc00; }
char32_t surrogate_to_utf32(char16_t high, char16_t low) {
return char32_t((high << 10) + low - 0x35fdc00); // Safe cast, it fits in 32 bits
}
void convert_utf16_to_utf32(const char16_t* input, size_t input_size, std::vector<char32_t>* output)
{
const char16_t * const end = input + input_size;
while (input < end) {
const char16_t uc = *input++;
if (!is_surrogate(uc)) {
(*output).push_back(uc);
} else {
if (is_high_surrogate(uc) && input < end && is_low_surrogate(*input))
(*output).push_back(surrogate_to_utf32(uc, *input++));
else {
// ERROR
}
}
}
(*output).push_back(0);
}
#endif
#endif
UINTN StrLen(const wchar_t* String)
{
// wcslen seems not to work if sizeof(wchar_t) == 2
const wchar_t* p;
for ( p = String ; *p ; p++ );
return (UINTN)(p-String);
}
int StrCmp(const wchar_t* FirstString, const wchar_t* SecondString)
{
#if __WCHAR_MAX__ > 0xFFFFu || _MSC_VER
int ret = wcscmp(FirstString, SecondString);
return ret;
#else
// Looks like wcscmp doesn't work with Utf16, even if compiled with -fshort-wchar.
// So conversion to Utf32 needed first.
std::vector<char32_t> FirstStringUtf32;
std::vector<char32_t> SecondStringUtf32;
convert_utf16_to_utf32((const char16_t*)FirstString, StrLen(FirstString), &FirstStringUtf32);
convert_utf16_to_utf32((const char16_t*)SecondString, StrLen(FirstString), &SecondStringUtf32);
int ret = wcscmp((const wchar_t*)FirstStringUtf32.data(), (const wchar_t*)SecondStringUtf32.data());
return ret;
#endif
}
int StrnCmp(const wchar_t* FirstString, const wchar_t* SecondString, UINTN Length)
{
#if __WCHAR_MAX__ > 0xFFFFu
return wcsncmp(FirstString, SecondString, Length);
#else
#ifdef _MSC_VER
return wcsncmp(FirstString, SecondString, (size_t)Length);
#else
// Looks like wcscmp doesn't work with Utf16, even if compiled with -fshort-wchar.
// So conversion to Utf32 needed first.
std::vector<char32_t> FirstStringUtf32;
std::vector<char32_t> SecondStringUtf32;
convert_utf16_to_utf32((const char16_t*)FirstString, StrLen(FirstString), &FirstStringUtf32);
convert_utf16_to_utf32((const char16_t*)SecondString, StrLen(FirstString), &SecondStringUtf32);
int ret = wcsncmp((const wchar_t*)FirstStringUtf32.data(), (const wchar_t*)SecondStringUtf32.data(), Length);
//printf("wcsncmp=%d\n", ret);
return ret;
#endif
#endif
}