Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/certutil/main.c
8533 views
1
/*
2
* Copyright (C) 2022 Mohamad Al-Jaf
3
*
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
8
*
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
13
*
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17
*/
18
19
#include "windows.h"
20
#include "wincrypt.h"
21
22
#include "wine/debug.h"
23
24
WINE_DEFAULT_DEBUG_CHANNEL(certutil);
25
26
static HRESULT decode_hex(const WCHAR *from, const WCHAR *into)
27
{
28
HRESULT hres = S_OK;
29
HANDLE in = NULL, out = NULL;
30
char in_buffer[1024];
31
BYTE out_buffer[ARRAY_SIZE(in_buffer) / 2];
32
ULONG64 total_written = 0;
33
LARGE_INTEGER li;
34
35
if ((in = CreateFileW(from, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
36
!GetFileSizeEx(in, &li) ||
37
(out = CreateFileW(into, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
38
hres = HRESULT_FROM_WIN32(GetLastError());
39
40
if (hres == S_OK)
41
printf("Input Length = %I64u\n", li.QuadPart);
42
43
while (hres == S_OK)
44
{
45
DWORD in_dw, out_dw;
46
47
if (ReadFile(in, in_buffer, ARRAY_SIZE(in_buffer), &in_dw, NULL))
48
{
49
char *ptr = memchr(in_buffer, '\n', in_dw);
50
char *ptr2 = memchr(in_buffer, '\r', in_dw);
51
DWORD dw;
52
53
if (!in_dw) break; /* EOF */
54
if (ptr2 && (!ptr || ptr2 < ptr)) ptr = ptr2;
55
if (ptr)
56
{
57
/* rewind just after the eol character */
58
li.QuadPart = ptr + 1 - (in_buffer + in_dw);
59
if (!SetFilePointerEx(in, li, NULL, FILE_CURRENT))
60
{
61
hres = HRESULT_FROM_WIN32(GetLastError());
62
break;
63
}
64
in_dw = ptr - in_buffer;
65
}
66
out_dw = ARRAY_SIZE(out_buffer);
67
if (CryptStringToBinaryA(in_buffer, in_dw, CRYPT_STRING_HEX,
68
out_buffer, &out_dw, NULL, NULL) &&
69
WriteFile(out, out_buffer, out_dw, &dw, NULL))
70
{
71
if (dw == out_dw)
72
total_written += out_dw;
73
else
74
hres = HRESULT_FROM_WIN32(ERROR_CANTWRITE);
75
continue;
76
}
77
}
78
hres = HRESULT_FROM_WIN32(GetLastError());
79
}
80
CloseHandle(in);
81
CloseHandle(out);
82
83
if (hres == S_OK)
84
printf("Output Length = %I64u\n", total_written);
85
return hres;
86
}
87
88
int __cdecl wmain(int argc, WCHAR *argv[])
89
{
90
HRESULT hres = -1;
91
int i;
92
93
if (argc == 4 && !wcscmp(argv[1], L"-decodehex"))
94
hres = decode_hex(argv[2], argv[3]);
95
else /* not a recognized command */
96
{
97
WINE_FIXME("stub:");
98
for (i = 0; i < argc; i++)
99
WINE_FIXME(" %s", wine_dbgstr_w(argv[i]));
100
WINE_FIXME("\n");
101
102
return 0;
103
}
104
if (hres == S_OK)
105
printf("CertUtil: %ls command completed successfully.\n", argv[1]);
106
else
107
printf("CertUtil: %ls command FAILED: %08lx\n", argv[1], hres);
108
return hres;
109
}
110
111