File: options.c

package info (click to toggle)
udftools 2.3-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,628 kB
  • sloc: ansic: 15,554; sh: 4,170; makefile: 57
file content (362 lines) | stat: -rw-r--r-- 11,317 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (C) 2017-2021  Pali Rohár <pali.rohar@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <getopt.h>

#include "libudffs.h"
#include "options.h"

static struct option long_options[] = {
	{ "help", no_argument, NULL, OPT_HELP },
	{ "blocksize", required_argument, NULL, OPT_BLK_SIZE },
	{ "startblock", required_argument, NULL, OPT_START_BLOCK },
	{ "lastblock", required_argument, NULL, OPT_LAST_BLOCK },
	{ "vatblock", required_argument, NULL, OPT_VAT_BLOCK },
	{ "force", no_argument, NULL, OPT_FORCE },
	{ "no-write", no_argument, NULL, OPT_NO_WRITE },
	{ "uuid", required_argument, NULL, OPT_UUID },
	{ "lvid", required_argument, NULL, OPT_LVID },
	{ "vid", required_argument, NULL, OPT_VID },
	{ "vsid", required_argument, NULL, OPT_VSID },
	{ "fsid", required_argument, NULL, OPT_FSID },
	{ "fullvsid", required_argument, NULL, OPT_FULLVSID },
	{ "owner", required_argument, NULL, OPT_OWNER },
	{ "organization", required_argument, NULL, OPT_ORG },
	{ "contact", required_argument, NULL, OPT_CONTACT },
	{ "appid", required_argument, NULL, OPT_APPID },
	{ "impid", required_argument, NULL, OPT_IMPID },
	{ "locale", no_argument, NULL, OPT_LOCALE },
	{ "u8", no_argument, NULL, OPT_UNICODE8 },
	{ "u16", no_argument, NULL, OPT_UNICODE16 },
	{ "utf8", no_argument, NULL, OPT_UTF8 },
	{ 0, 0, NULL, 0 },
};

static void usage(void)
{
	fprintf(stderr, "udflabel from " PACKAGE_NAME " " PACKAGE_VERSION "\n"
		"Usage:\n"
		"\tudflabel [encoding-options] [block-options] [identifier-options] device [new-label]\n"
		"\n"
		"When all Identifier Options and new UDF Label are omitted then show current UDF Label.\n"
		"Otherwise set new Identifier Options. New UDF Label is synonym for both --lvid and --vid.\n"
		"\n"
		"Options:\n"
		"\t--help, -h         Display this help\n"
		"\n"
		"Block Options:\n"
		"\t--blocksize=, -b   Size of blocks in bytes (512, 1024, 2048, 4096, 8192, 16384, 32768; default: detect)\n"
		"\t--startblock=      Block location where the UDF filesystem starts (default: last session from TOC or 0)\n"
		"\t--lastblock=       Block location where the UDF filesystem ends (default: VAT block or last device block)\n"
		"\t--vatblock=        Block location of the Virtual Allocation Table (default: detect)\n"
		"\t--force            Force updating UDF disks without write support (useful only for disk images)\n"
		"\t--no-write, -n     Not really, do not write to device, just simulate\n"
		"\n"
		"Identifier Options:\n"
		"\t--uuid=, -u        New UDF UUID, first 16 characters of Volume Set Identifier\n"
		"\t--lvid=            New Logical Volume Identifier\n"
		"\t--vid=             New Volume Identifier\n"
		"\t--vsid=            New 17.-127. character of Volume Set Identifier\n"
		"\t--fsid=            New File Set Identifier\n"
		"\t--fullvsid=        New full Volume Set Identifier, overwrite --uuid and --vsid\n"
		"\t--owner=           New Owner name\n"
		"\t--organization=    New Organization name\n"
		"\t--contact=         New Contact information\n"
		"\t--appid=           New Application Identifier\n"
		"\t--impid=           New Developer Identifier for Implementation Identifier\n"
		"\n"
		"Encoding Options:\n"
		"\t--locale           Identifier options are encoded according to current locale (default)\n"
		"\t--u8               Identifier options are encoded in Latin1\n"
		"\t--u16              Identifier options are encoded in UTF-16BE\n"
		"\t--utf8             Identifier options are encoded in UTF-8\n"
	);
	exit(1);
}

static void process_uuid_arg(const char *arg, char *new_uuid)
{
	int i;
	time_t cur_time;
	uint32_t uuid_time;

	if (strcmp(arg, "random") == 0)
	{
		cur_time = time(NULL);
		if (cur_time != (time_t)-1 && cur_time >= 0)
			uuid_time = cur_time & 0xFFFFFFFF;
		else
			uuid_time = randu32();
		snprintf(new_uuid, 17, "%08"PRIx32"%08"PRIx32"", uuid_time, randu32());
		return;
	}

	if (strlen(arg) != 16)
	{
		fprintf(stderr, "%s: Error: Option --uuid is not 16 bytes long\n", appname);
		exit(1);
	}

	for (i = 0; i < 16; ++i)
	{
		if (!isxdigit(arg[i]) || (!isdigit(arg[i]) && !islower(arg[i])))
		{
			fprintf(stderr, "%s: Error: Option --uuid is not in lowercase hexadecimal digit format\n", appname);
			exit(1);
		}
	}

	memcpy(new_uuid, arg, 17);
}

static void process_vid_lvid_arg(struct udf_disc *disc, int option, const char *arg, dstring *new_lvid, dstring *new_vid)
{
	if (option != OPT_VID)
	{
		if (encode_string(disc, new_lvid, arg, 128) == (size_t)-1)
		{
			if (option == OPT_LVID)
				fprintf(stderr, "%s: Error: Option --lvid is too long\n", appname);
			else
				fprintf(stderr, "%s: Error: Label is too long\n", appname);
			exit(1);
		}
	}

	if (option != OPT_LVID)
	{
		if (encode_string(disc, new_vid, arg, 32) == (size_t)-1)
		{
			if (option == OPT_VID)
			{
				fprintf(stderr, "%s: Error: Option --vid is too long\n", appname);
				exit(1);
			}
			/* This code was not triggered by --vid option, do not throw error but rather store truncated --lvid */
			memcpy(new_vid, new_lvid, 32);
			new_vid[31] = 31;
		}
	}
}

void parse_args(int argc, char *argv[], struct udf_disc *disc, char **filename, int *force, dstring *new_lvid, dstring *new_vid, dstring *new_fsid, dstring *new_fullvsid, char *new_uuid, dstring *new_vsid, dstring *new_owner, dstring *new_org, dstring *new_contact, char *new_appid, char *new_impid)
{
	int failed;
	int ret;
	size_t len;

	while ((ret = getopt_long(argc, argv, "b:nu:h", long_options, NULL)) != EOF)
	{
		switch (ret)
		{
			case OPT_HELP:
			case 'h':
				usage();
				break;
			case OPT_BLK_SIZE:
			case 'b':
				disc->blocksize = strtou32(optarg, 0, &failed);
				if (failed || disc->blocksize < 512 || disc->blocksize > 32768 || (disc->blocksize & (disc->blocksize - 1)))
				{
					fprintf(stderr, "%s: Error: Invalid value for option --blocksize\n", appname);
					exit(1);
				}
				break;
			case OPT_START_BLOCK:
				disc->start_block = strtou32(optarg, 0, &failed);
				if (failed)
				{
					fprintf(stderr, "%s: Error: Invalid value for option --startblock\n", appname);
					exit(1);
				}
				break;
			case OPT_LAST_BLOCK:
				disc->last_block = strtou32(optarg, 0, &failed);
				if (failed)
				{
					fprintf(stderr, "%s: Error: Invalid value for option --lastblock\n", appname);
					exit(1);
				}
				break;
			case OPT_VAT_BLOCK:
				disc->vat_block = strtou32(optarg, 0, &failed);
				if (failed)
				{
					fprintf(stderr, "%s: Error: Invalid value for option --vatblock\n", appname);
					exit(1);
				}
				break;
			case OPT_FORCE:
				*force = 1;
				break;
			case OPT_NO_WRITE:
			case 'n':
				disc->flags |= FLAG_NO_WRITE;
				break;
			case OPT_UUID:
			case 'u':
				process_uuid_arg(optarg, new_uuid);
				new_fullvsid[0] = 0xFF;
				break;
			case OPT_VID:
			case OPT_LVID:
				process_vid_lvid_arg(disc, ret, optarg, new_lvid, new_vid);
				break;
			case OPT_VSID:
				len = encode_string(disc, new_vsid, optarg, 128);
				if (len == (size_t)-1 || len > 127-16 || (new_vsid[0] == 16 && len > 127-16*2))
				{
					fprintf(stderr, "%s: Error: Option --vsid is too long\n", appname);
					exit(1);
				}
				new_fullvsid[0] = 0xFF;
				break;
			case OPT_FULLVSID:
				if (encode_string(disc, new_fullvsid, optarg, 128) == (size_t)-1)
				{
					fprintf(stderr, "%s: Error: Option --fullvsid is too long\n", appname);
					exit(1);
				}
				new_uuid[0] = 0;
				new_vsid[0] = 0xFF;
				break;
			case OPT_FSID:
				if (encode_string(disc, new_fsid, optarg, 32) == (size_t)-1)
				{
					fprintf(stderr, "%s: Error: Option --fsid is too long\n", appname);
					exit(1);
				}
				break;
			case OPT_OWNER:
				if (encode_string(disc, new_owner, optarg, 36) == (size_t)-1)
				{
					fprintf(stderr, "%s: Error: Option --owner is too long\n", appname);
					exit(1);
				}
				break;
			case OPT_ORG:
				if (encode_string(disc, new_org, optarg, 36) == (size_t)-1)
				{
					fprintf(stderr, "%s: Error: Option --organization is too long\n", appname);
					exit(1);
				}
				break;
			case OPT_CONTACT:
				if (encode_string(disc, new_contact, optarg, 36) == (size_t)-1)
				{
					fprintf(stderr, "%s: Error: Option --contact is too long\n", appname);
					exit(1);
				}
				break;
			case OPT_APPID:
			case OPT_IMPID:
				len = strlen(optarg);
				if (len > 23)
				{
					fprintf(stderr, "%s: Error: Option --%s is too long\n", appname, ret == OPT_APPID ? "appid" : "impid");
					exit(1);
				}
				if (len > 0)
				{
					if (len < 2)
					{
						fprintf(stderr, "%s: Error: Option --%s is too short\n", appname, ret == OPT_APPID ? "appid" : "impid");
						exit(1);
					}
					if (optarg[0] != '*')
					{
						fprintf(stderr, "%s: Error: Option --%s does not start with '*'\n", appname, ret == OPT_APPID ? "appid" : "impid");
						exit(1);
					}
				}
				memcpy(ret == OPT_APPID ? new_appid : new_impid, optarg, len < 23 ? len+1 : 23);
				for (; len > 0; len--)
				{
					if ((unsigned char)optarg[len-1] > 127)
					{
						fprintf(stderr, "%s: Error: Option --%s is not 7bit ASCII string\n", appname, ret == OPT_APPID ? "appid" : "impid");
						exit(1);
					}
				}
				break;
			case OPT_UNICODE8:
				disc->flags &= ~FLAG_CHARSET;
				disc->flags |= FLAG_UNICODE8;
				if (strcmp(argv[1], "--u8") != 0)
				{
					fprintf(stderr, "%s: Error: Option --u8 must be specified as first argument\n", appname);
					exit(1);
				}
				break;
			case OPT_UNICODE16:
				disc->flags &= ~FLAG_CHARSET;
				disc->flags |= FLAG_UNICODE16;
				if (strcmp(argv[1], "--u16") != 0)
				{
					fprintf(stderr, "%s: Error: Option --u16 must be specified as first argument\n", appname);
					exit(1);
				}
				break;
			case OPT_UTF8:
				disc->flags &= ~FLAG_CHARSET;
				disc->flags |= FLAG_UTF8;
				if (strcmp(argv[1], "--utf8") != 0)
				{
					fprintf(stderr, "%s: Error: Option --utf8 must be specified as first argument\n", appname);
					exit(1);
				}
				break;
			case OPT_LOCALE:
				disc->flags &= ~FLAG_CHARSET;
				disc->flags |= FLAG_LOCALE;
				if (strcmp(argv[1], "--locale") != 0)
				{
					fprintf(stderr, "%s: Error: Option --locale must be specified as first argument\n", appname);
					exit(1);
				}
				break;
			default:
				usage();
				break;
		}
	}

	if (optind+1 != argc && optind+2 != argc)
		usage();

	*filename = argv[optind];

	if (optind+2 == argc)
		process_vid_lvid_arg(disc, -1, argv[optind+1], new_lvid, new_vid);
}