Skip to content

Commit dcae431

Browse files
refactor(win32): force CP_UTF8 to get rid of all wchar use
See https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page Signed-off-by: Coelacanthus <uwu@coelacanthus.name>
1 parent 22dcc49 commit dcae431

4 files changed

Lines changed: 21 additions & 42 deletions

File tree

driver/apdu/at_win32.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,35 @@ static void enumerate_com_ports(cJSON *data) {
3535
SP_DEVINFO_DATA devInfoData;
3636
DWORD i;
3737

38-
hDevInfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);
38+
hDevInfo = SetupDiGetClassDevsA(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);
3939
if (hDevInfo == INVALID_HANDLE_VALUE)
4040
return;
4141

4242
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
4343
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData); i++) {
44-
wchar_t portName[256] = {0};
45-
wchar_t friendlyName[256] = {0};
46-
char portNameMB[256] = {0};
47-
char friendlyNameMB[256] = {0};
44+
char portName[256] = {0};
45+
char friendlyName[256] = {0};
4846
DWORD size = sizeof(portName);
4947

5048
HKEY hKey = SetupDiOpenDevRegKey(hDevInfo, &devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
5149
if (hKey != INVALID_HANDLE_VALUE) {
5250
DWORD type;
53-
if (RegQueryValueExW(hKey, L"PortName", NULL, &type, (LPBYTE)portName, &size) != ERROR_SUCCESS
51+
if (RegQueryValueExA(hKey, L"PortName", NULL, &type, (LPBYTE)portName, &size) != ERROR_SUCCESS
5452
|| type != REG_SZ) {
5553
portName[0] = L'\0';
5654
}
5755
}
5856

59-
if (!SetupDiGetDeviceRegistryPropertyW(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)friendlyName,
57+
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)friendlyName,
6058
sizeof(friendlyName), NULL)) {
6159
friendlyName[0] = L'\0';
6260
}
6361

64-
WideCharToMultiByte(CP_UTF8, 0, portName, -1, portNameMB, sizeof(portNameMB), NULL, NULL);
65-
WideCharToMultiByte(CP_UTF8, 0, friendlyName, -1, friendlyNameMB, sizeof(friendlyNameMB), NULL, NULL);
66-
67-
if (starts_with(portNameMB, "COM")) {
62+
if (starts_with(portName, "COM")) {
6863
cJSON *item = cJSON_CreateObject();
6964
if (item) {
70-
cJSON_AddStringToObject(item, "env", portNameMB);
71-
cJSON_AddStringToObject(item, "name", friendlyNameMB[0] ? friendlyNameMB : portNameMB);
65+
cJSON_AddStringToObject(item, "env", portName);
66+
cJSON_AddStringToObject(item, "name", friendlyName[0] ? friendlyName : portName);
7267
cJSON_AddItemToArray(data, item);
7368
} else {
7469
cJSON_Delete(item);
@@ -171,13 +166,10 @@ static int apdu_interface_connect(struct euicc_ctx *ctx) {
171166

172167
logic_channel = 0;
173168

174-
char dev_ascii[64];
169+
char devname[64];
175170
snprintf(dev_ascii, sizeof(dev_ascii), "\\\\.\\%s", device);
176171

177-
wchar_t devname[64];
178-
mbstowcs(devname, dev_ascii, sizeof(devname) / sizeof(wchar_t));
179-
180-
hComm = CreateFileW(devname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
172+
hComm = CreateFile(devname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
181173

182174
if (hComm == INVALID_HANDLE_VALUE) {
183175
fprintf(stderr, "Failed to open device: %s, error: %lu\n", dev_ascii, GetLastError());

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/applet/chip DIR_LPAC_SRCS)
1111
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/applet/notification DIR_LPAC_SRCS)
1212
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/applet/profile DIR_LPAC_SRCS)
1313

14-
add_executable(lpac ${DIR_LPAC_SRCS})
14+
add_executable(lpac ${DIR_LPAC_SRCS} lpac.manifest)
1515
target_link_libraries(lpac euicc-drivers lpac-utils)
1616
target_include_directories(lpac PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
1717

src/lpac.manifest

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity type="win32" name="me.estk.lpac" version="6.0.0.0"/>
4+
<application>
5+
<windowsSettings>
6+
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
7+
</windowsSettings>
8+
</application>
9+
</assembly>

src/main.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,6 @@ void main_fini_euicc() {
126126
euicc_ctx_inited = 0;
127127
}
128128

129-
#ifdef WIN32
130-
static char **warg_to_arg(const int wargc, wchar_t **wargv) {
131-
char **argv = malloc(wargc * sizeof(char *));
132-
if (argv == NULL) {
133-
return NULL;
134-
}
135-
for (int i = 0; i < wargc; ++i) {
136-
const int size = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
137-
argv[i] = malloc(size);
138-
if (argv[i] == NULL) {
139-
for (int j = 0; j < i; ++j) {
140-
free(argv[j]);
141-
}
142-
free(argv);
143-
return NULL;
144-
}
145-
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], size, NULL, NULL);
146-
}
147-
return argv;
148-
}
149-
#endif
150-
151129
int main(int argc, char **argv) {
152130
int ret = 0;
153131

@@ -167,7 +145,7 @@ int main(int argc, char **argv) {
167145
euicc_ctx.http.interface = &euicc_driver_interface_http;
168146

169147
#ifdef WIN32
170-
argv = warg_to_arg(argc, CommandLineToArgvW(GetCommandLineW(), &argc));
148+
argv = CommandLineToArgvA(GetCommandLineA(), &argc);
171149
if (argv == NULL) {
172150
return -1;
173151
}

0 commit comments

Comments
 (0)