VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/Cmdline.c
blob: e5fe59691e8b4febaf9721675783cce9734ab75a (plain)
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
/*
 Legal Notice: Some portions of the source code contained in this file were
 derived from the source code of TrueCrypt 7.1a, which is
 Copyright (c) 2003-2012 TrueCrypt Developers Association and which is
 governed by the TrueCrypt License 3.0, also from the source code of
 Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux
 and which is governed by the 'License Agreement for Encryption for the Masses'
 Modifications and additions to the original source code (contained in this file)
 and all other portions of this file are Copyright (c) 2013-2016 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages. */

#include "Tcdefs.h"

#include <malloc.h>
#include <ctype.h>
#include "Cmdline.h"

#include "Resource.h"
#include "Crypto.h"
#include "Apidrvr.h"
#include "Dlgcode.h"
#include "Language.h"
#include <Strsafe.h>

#ifndef SRC_POS
#define SRC_POS (__FUNCTION__ ":" TC_TO_STRING(__LINE__))
#endif

/* Except in response to the WM_INITDIALOG message, the dialog box procedure
   should return nonzero if it processes the message, and zero if it does
   not. - see DialogProc */
BOOL CALLBACK CommandHelpDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER (lParam);		/* remove warning */
	UNREFERENCED_PARAMETER (wParam);		/* remove warning */

	switch (msg)
	{
	case WM_INITDIALOG:
		{
		wchar_t * tmp = err_malloc(8192 * sizeof (wchar_t));
		wchar_t tmp2[MAX_PATH * 2];
		argumentspec *as;
		int i;

		LocalizeDialog (hwndDlg, "IDD_COMMANDHELP_DLG");

		as = (argumentspec*) lParam;

		*tmp = 0;

		StringCchCopyW (tmp, 8192, L"VeraCrypt " _T(VERSION_STRING));
#ifdef _WIN64
		StringCchCatW (tmp, 8192, L"  (64-bit)");
#else
		StringCchCatW (tmp, 8192, L"  (32-bit)");
#endif
#if (defined(_DEBUG) || defined(DEBUG))
		StringCchCatW (tmp, 8192, L"  (debug)");
#endif

		StringCchCatW (tmp, 8192, L"\n\nCommand line options:\n\n");
		for (i = 0; i < as->arg_cnt; i ++)
		{
			if (!as->args[i].Internal)
			{
				StringCchPrintfW(tmp2, MAX_PATH * 2, L"%s\t%s\n", as->args[i].short_name, as->args[i].long_name);
				StringCchCatW(tmp, 8192, tmp2);
			}
		}

		StringCchCatW (tmp, 8192, L"\nExamples:\n\nMount a volume as X:\tveracrypt.exe /q /v volume.hc /l X\nDismount a volume X:\tveracrypt.exe /q /d X");

		SetWindowTextW (GetDlgItem (hwndDlg, IDC_COMMANDHELP_TEXT), tmp);

		TCfree(tmp);
		return 1;
		}

	case WM_COMMAND:
		EndDialog (hwndDlg, IDOK);
		return 1;
	case WM_CLOSE:
		EndDialog (hwndDlg, 0);
		return 1;
	}

	return 0;
}

int Win32CommandLine (wchar_t ***lpszArgs)
{
	int argumentCount;
	int i;

	LPWSTR *arguments = CommandLineToArgvW (GetCommandLineW(), &argumentCount);
	if (!arguments)
	{
		handleWin32Error (NULL, SRC_POS);
		return 0;
	}

	--argumentCount;
	if (argumentCount < 1)
	{
		LocalFree (arguments);
		return 0;
	}

	*lpszArgs = malloc (sizeof (wchar_t *) * argumentCount);
	if (!*lpszArgs)
		AbortProcess ("OUTOFMEMORY");

	for (i = 0; i < argumentCount; ++i)
	{
		wchar_t *arg = _wcsdup (arguments[i + 1]);
		if (!arg)
			AbortProcess ("OUTOFMEMORY");

		(*lpszArgs)[i] = arg;
	}

	LocalFree (arguments);
	return argumentCount;
}

int GetArgSepPosOffset (wchar_t *lpszArgument)
{
	if (lpszArgument[0] == L'/')
		return 1;

	return 0;
}

int GetArgumentID (argumentspec *as, wchar_t *lpszArgument)
{
	int i;

	for (i = 0; i < as->arg_cnt; i++)
	{
		if (_wcsicmp (as->args[i].long_name, lpszArgument) == 0)
		{
			return as->args[i].Id;
		}
	}

	for (i = 0; i < as->arg_cnt; i++)
	{
		if (as->args[i].short_name[0] == 0)
			continue;

		if (_wcsicmp (as->args[i].short_name, lpszArgument) == 0)
		{
			return as->args[i].Id;
		}
	}


	return -1;
}

int GetArgumentValue (wchar_t **lpszCommandLineArgs, int *nArgIdx,
		  int nNoCommandLineArgs, wchar_t *lpszValue, int nValueSize)
{
	*lpszValue = 0;

	if (*nArgIdx + 1 < nNoCommandLineArgs)
	{
		int x = GetArgSepPosOffset (lpszCommandLineArgs[*nArgIdx + 1]);
		if (x == 0)
		{
			/* Handles the case of space between parameter code
			   and value */
			StringCchCopyW (lpszValue, nValueSize, lpszCommandLineArgs[*nArgIdx + 1]);
			lpszValue[nValueSize - 1] = 0;
			(*nArgIdx)++;
			return HAS_ARGUMENT;
		}
	}

	return HAS_NO_ARGUMENT;
}
span> static const zip_uint8_t empty[1] = {'\0'}; int i; i = 0; for (; ef; ef = ef->next) { if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) { if (i < id_idx) { i++; continue; } if (lenp) *lenp = ef->size; if (ef->size > 0) return ef->data; else return empty; } } zip_error_set(error, ZIP_ER_NOENT, 0); return NULL; } zip_extra_field_t * _zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) { zip_extra_field_t *ef2, *tt, *tail; int duplicate; if (to == NULL) return from; for (tail = to; tail->next; tail = tail->next) ; for (; from; from = ef2) { ef2 = from->next; duplicate = 0; for (tt = to; tt; tt = tt->next) { if (tt->id == from->id && tt->size == from->size && (tt->size == 0 || memcmp(tt->data, from->data, tt->size) == 0)) { tt->flags |= (from->flags & ZIP_EF_BOTH); duplicate = 1; break; } } from->next = NULL; if (duplicate) _zip_ef_free(from); else tail = tail->next = from; } return to; } zip_extra_field_t * _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) { zip_extra_field_t *ef; if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) return NULL; ef->next = NULL; ef->flags = flags; ef->id = id; ef->size = size; if (size > 0) { if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) { free(ef); return NULL; } } else ef->data = NULL; return ef; } bool _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error) { zip_buffer_t *buffer; zip_extra_field_t *ef, *ef2, *ef_head; if ((buffer = _zip_buffer_new((zip_uint8_t *)data, len)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } ef_head = ef = NULL; while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) { zip_uint16_t fid, flen; zip_uint8_t *ef_data; fid = _zip_buffer_get_16(buffer); flen = _zip_buffer_get_16(buffer); ef_data = _zip_buffer_get(buffer, flen); if (ef_data == NULL) { zip_error_set(error, ZIP_ER_INCONS, 0); _zip_buffer_free(buffer); _zip_ef_free(ef_head); return false; } if ((ef2 = _zip_ef_new(fid, flen, ef_data, flags)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); _zip_buffer_free(buffer); _zip_ef_free(ef_head); return false; } if (ef_head) { ef->next = ef2; ef = ef2; } else ef_head = ef = ef2; } if (!_zip_buffer_eof(buffer)) { /* Android APK files align stored file data with padding in extra fields; ignore. */ /* see https://android.googlesource.com/platform/build/+/master/tools/zipalign/ZipAlign.cpp */ size_t glen = _zip_buffer_left(buffer); zip_uint8_t *garbage; garbage = _zip_buffer_get(buffer, glen); if (glen >= 4 || garbage == NULL || memcmp(garbage, "\0\0\0", glen) != 0) { zip_error_set(error, ZIP_ER_INCONS, 0); _zip_buffer_free(buffer); _zip_ef_free(ef_head); return false; } } _zip_buffer_free(buffer); if (ef_head_p) { *ef_head_p = ef_head; } else { _zip_ef_free(ef_head); } return true; } zip_extra_field_t * _zip_ef_remove_internal(zip_extra_field_t *ef) { zip_extra_field_t *ef_head; zip_extra_field_t *prev, *next; ef_head = ef; prev = NULL; while (ef) { if (ZIP_EF_IS_INTERNAL(ef->id)) { next = ef->next; if (ef_head == ef) ef_head = next; ef->next = NULL; _zip_ef_free(ef); if (prev) prev->next = next; ef = next; } else { prev = ef; ef = ef->next; } } return ef_head; } zip_uint16_t _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) { zip_uint16_t size; size = 0; for (; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) size = (zip_uint16_t)(size + 4 + ef->size); } return size; } int _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) { zip_uint8_t b[4]; zip_buffer_t *buffer = _zip_buffer_new(b, sizeof(b)); if (buffer == NULL) { return -1; } for (; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) { _zip_buffer_set_offset(buffer, 0); _zip_buffer_put_16(buffer, ef->id); _zip_buffer_put_16(buffer, ef->size); if (!_zip_buffer_ok(buffer)) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); _zip_buffer_free(buffer); return -1; } if (_zip_write(za, b, 4) < 0) { _zip_buffer_free(buffer); return -1; } if (ef->size > 0) { if (_zip_write(za, ef->data, ef->size) < 0) { _zip_buffer_free(buffer); return -1; } } } } _zip_buffer_free(buffer); return 0; } int _zip_read_local_ef(zip_t *za, zip_uint64_t idx) { zip_entry_t *e; unsigned char b[4]; zip_buffer_t *buffer; zip_uint16_t fname_len, ef_len; if (idx >= za->nentry) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } e = za->entry + idx; if (e->orig == NULL || e->orig->local_extra_fields_read) return 0; if (e->orig->offset + 26 > ZIP_INT64_MAX) { zip_error_set(&za->error, ZIP_ER_SEEK, EFBIG); return -1; } if (zip_source_seek(za->src, (zip_int64_t)(e->orig->offset + 26), SEEK_SET) < 0) { _zip_error_set_from_source(&za->error, za->src); return -1; } if ((buffer = _zip_buffer_new_from_source(za->src, sizeof(b), b, &za->error)) == NULL) { return -1; } fname_len = _zip_buffer_get_16(buffer); ef_len = _zip_buffer_get_16(buffer); if (!_zip_buffer_eof(buffer)) { _zip_buffer_free(buffer); zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return -1; } _zip_buffer_free(buffer); if (ef_len > 0) { zip_extra_field_t *ef; zip_uint8_t *ef_raw; if (zip_source_seek(za->src, fname_len, SEEK_CUR) < 0) { zip_error_set(&za->error, ZIP_ER_SEEK, errno); return -1; } ef_raw = _zip_read_data(NULL, za->src, ef_len, 0, &za->error); if (ef_raw == NULL) return -1; if (!_zip_ef_parse(ef_raw, ef_len, ZIP_EF_LOCAL, &ef, &za->error)) { free(ef_raw); return -1; } free(ef_raw); if (ef) { ef = _zip_ef_remove_internal(ef); e->orig->extra_fields = _zip_ef_merge(e->orig->extra_fields, ef); } } e->orig->local_extra_fields_read = 1; if (e->changes && e->changes->local_extra_fields_read == 0) { e->changes->extra_fields = e->orig->extra_fields; e->changes->local_extra_fields_read = 1; } return 0; }