This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlpcli_win.c
105 lines (97 loc) · 2.13 KB
/
lpcli_win.c
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
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include "lpcli.h"
int lpcli_clipboardcopy(const char *text)
{
const size_t len = strlen(text) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), text, len);
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
if (!SetClipboardData(CF_TEXT, hMem))
{
return LPCLI_FAIL;
}
CloseClipboard();
return LPCLI_OK;
}
// Todo: calculate the encoded length as characters entered
int lpcli_readpassword(const char *prompt, char *out, size_t outl)
{
printf("%s", prompt);
wchar_t input[MAX_INPUTWCS];
wint_t c;
int i;
for (i = 0; i < MAX_INPUTWCS - 1; i++)
{
c = _getwch();
if (c == L'\r')
{
//input[i++] = 0;
break;
}
input[i] = c;
}
input[i] = 0;
printf("\n");
int err = 0;
int len = WideCharToMultiByte(CP_UTF8, 0, input, i, out, outl - 1, NULL, NULL);
SecureZeroMemory(input, sizeof input);
if (len == 0 || len == 0xFFFD)
{
err = GetLastError();
}
out[len] = 0;
if (err != 0)
{
fprintf(stderr, "WideCharToMultiByte got error code %i\n", err);
return LPCLI_FAIL;
}
return LPCLI_OK;
}
static char **cmdline_to_argv_u8(int *argc)
{
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), argc);
int i;
int tlen = 0;
int utf8len[*argc];
int wlen[*argc];
for (i = 0; i < *argc; i++)
{
wlen[i] = wcslen(wargv[i]) + 1;
utf8len[i] = WideCharToMultiByte(CP_UTF8, 0, wargv[i], wlen[i], NULL, 0, NULL, NULL);
tlen += utf8len[i];
}
int argvsize = (*argc + 1) * sizeof(char *);
char *argvp = malloc(argvsize + tlen);
char **argv = (void *) argvp;
argvp += argvsize;
for (i = 0; i < *argc; i++)
{
WideCharToMultiByte(CP_UTF8, 0, wargv[i], wlen[i], argvp, utf8len[i], NULL, NULL);
SecureZeroMemory(wargv[i], wlen[i]);
argv[i] = argvp;
argvp += utf8len[i];
}
argv[*argc] = NULL;
LocalFree(wargv);
return argv;
}
void *lpcli_zeromemory(void *dst, size_t dstlen)
{
return SecureZeroMemory(dst, dstlen);
}
int main()
{
setlocale(LC_ALL, "");
int argc;
char **argv = cmdline_to_argv_u8(&argc);
int ret = lpcli_main(argc, argv);
free(argv);
return ret;
}