summaryrefslogtreecommitdiff
path: root/utils/container.c
blob: 26d4745a033afa6e0d8e5b8335800217aa6eb499 (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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * Copyright 2006 Rob Kendrick <rjek@rjek.com>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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, see <http://www.gnu.org/licenses/>.
 */

/* To build a stand-alone command-line utility to create and dismantal
 * these theme files, build this thusly:
 *
 * gcc -I../ -DNSTHEME -o themetool container.c
 *
 * [needs a c99 compiler]
 *
 * then for instance to create a theme file called mythemefilename
 * ./themetool --verbose --create -n"My theme name" mythemefilename\
 * --author "Myname" /path/to/directory/containing/theme/files/
 */

/** \file
 * Container format handling for themes etc. */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include "utils/config.h"
#include "utils/container.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"

#ifdef WITH_MMAP
#include <sys/mman.h>
#endif

#ifdef NSTHEME
bool verbose_log = true;
#endif

struct container_dirent {
	unsigned char	filename[64];
	u_int32_t	startoffset;
	u_int32_t	len;
	u_int32_t	flags1;
	u_int32_t	flags2;
};

struct container_header {
	u_int32_t	magic;	/* 0x4d54534e little endian */
	u_int32_t	parser;
	unsigned char	name[32];
	unsigned char	author[64];
	u_int32_t	diroffset;
};

struct container_ctx {
	FILE		*fh;
	bool		creating;
	bool		processed;
	struct container_header	header;
	unsigned int	entries;
	unsigned char 	*data;
	struct container_dirent *directory;
};

inline static size_t container_filelen(FILE *fd)
{
	long o = ftell(fd);
	long a;

	fseek(fd, 0, SEEK_END);
	a = ftell(fd);
	fseek(fd, o, SEEK_SET);
	if (a == -1) {
		LOG(("could not ascertain size of file in theme container; omitting"));
		return 0;
	}
	if (((unsigned long) a) > SIZE_MAX) {
		LOG(("overlarge file in theme container; possible truncation"));
		return SIZE_MAX;
	}
	return (size_t) a;
}

static void container_add_to_dir(struct container_ctx *ctx,
					const unsigned char *entryname,
					const u_int32_t offset,
					const u_int32_t length)
{
	struct container_dirent *temp;
	temp = realloc(ctx->directory, ctx->entries * 
			sizeof(struct container_dirent));
	if (temp == NULL) {
		printf("error adding entry for %s to theme container\n", entryname);
		return;
	}
	ctx->entries += 1;
	ctx->directory = temp;

	strncpy((char *)ctx->directory[ctx->entries - 1].filename,
				(char *)entryname, sizeof(ctx->directory[
				ctx->entries - 1].filename));
	ctx->directory[ctx->entries - 1].startoffset = offset;
	ctx->directory[ctx->entries - 1].len = length;
	ctx->directory[ctx->entries - 1].flags1 = 0;
	ctx->directory[ctx->entries - 1].flags2 = 0;
}

struct container_ctx *container_open(const char *filename)
{
	size_t val;
	struct container_ctx *ctx = calloc(sizeof(struct container_ctx), 1);

	ctx->fh = fopen(filename, "rb");

	if (ctx->fh == NULL) {
		free(ctx);
		return NULL;
	}

	/* we don't actually load any of the data (including directory)
	 * until we need to, such that _get_name and _get_author are as quick
	 * as possible.  When we have, this gets set to true.
	 */
	ctx->processed = false;

	val = fread(&ctx->header.magic, 4, 1, ctx->fh);
	if (val == 0)
		LOG(("empty read magic"));
	ctx->header.magic = ntohl(ctx->header.magic);

	val = fread(&ctx->header.parser, 4, 1, ctx->fh);
	if (val == 0)
		LOG(("empty read parser"));	
	ctx->header.parser = ntohl(ctx->header.parser);

	val = fread(ctx->header.name, 32, 1, ctx->fh);
	if (val == 0)
		LOG(("empty read name"));
	val = fread(ctx->header.author, 64, 1, ctx->fh);
	if (val == 0)
		LOG(("empty read author"));

	val = fread(&ctx->header.diroffset, 4, 1, ctx->fh);
	if (val == 0)
		LOG(("empty read diroffset"));
	ctx->header.diroffset = ntohl(ctx->header.diroffset);

	if (ctx->header.magic != 0x4e53544d || ctx->header.parser != 3) {
		fclose(ctx->fh);
		free(ctx);
		return NULL;
	}

	return ctx;
}

static void container_process(struct container_ctx *ctx)
{
	size_t val;
	unsigned char filename[64];
	u_int32_t start, len, flags1, flags2;

#ifdef WITH_MMAP
	ctx->data = mmap(NULL, ctx->header.diroffset, PROT_READ, MAP_PRIVATE,
				fileno(ctx->fh), 0);
#else
	ctx->data = malloc(ctx->header.diroffset);
	fseek(ctx->fh, 0, SEEK_SET);
	val = fread(ctx->data, ctx->header.diroffset, 1, ctx->fh);
	if (val == 0)
		LOG(("empty read diroffset"));
#endif
	fseek(ctx->fh, ctx->header.diroffset, SEEK_SET);
	/* now work through the directory structure taking it apart into
	 * our structure */
#define BEREAD(x) do { val = fread(&(x), 4, 1, ctx->fh); if (val == 0)\
		LOG(("empty read"));(x) = ntohl((x)); } while (0)
	do {
		val = fread(filename, 64, 1, ctx->fh);
		if (val == 0)
			LOG(("empty read filename"));
		BEREAD(start);
		BEREAD(len);
		BEREAD(flags1);
		BEREAD(flags2);
		if (filename[0] != '\0')
			container_add_to_dir(ctx, filename, start, len);
	} while (filename[0] != '\0');
#undef BEREAD
	ctx->processed = true;
}

static const struct container_dirent *container_lookup(
					struct container_ctx *ctx,
					const unsigned char *entryname)
{
	unsigned int i;

	for (i = 1; i <= ctx->entries; i++) {
		struct container_dirent *e = ctx->directory + i - 1;
		if (strcmp((char *)e->filename, (char *)entryname) == 0)
			return e;
	}

	return NULL;
}

const unsigned char *container_get(struct container_ctx *ctx,
					const unsigned char *entryname,
					u_int32_t *size)
{
	const struct container_dirent *e;

	if (ctx->processed == false)
		container_process(ctx);

	e = container_lookup(ctx, entryname);

	if (e == NULL)
		return NULL;

	*size = e->len;

	return &ctx->data[e->startoffset];
}

const unsigned char *container_iterate(struct container_ctx *ctx, int *state)
{
	struct container_dirent *e;
	unsigned char *r;

	if (ctx->processed == false)
		container_process(ctx);

	e = ctx->directory + *state;

	r = e->filename;

	if (r == NULL || r[0] == '\0')
		r = NULL;

	*state += 1;

	return r;
}

const unsigned char *container_get_name(struct container_ctx *ctx)
{
	return ctx->header.name;
}

const unsigned char *container_get_author(struct container_ctx *ctx)
{
	return ctx->header.author;
}


static void container_write_dir(struct container_ctx *ctx)
{
	size_t val;
	unsigned int i;
	u_int32_t tmp;
#define BEWRITE(x) do {tmp = htonl((x)); val = fwrite(&tmp, 4, 1, ctx->fh);\
		if (val == 0) LOG(("empty write")); } while(0)
	for (i = 1; i <= ctx->entries; i++) {
		struct container_dirent *e = ctx->directory + i - 1;
		val = fwrite(e->filename, 64, 1, ctx->fh);
		if (val == 0)
			LOG(("empty write filename"));
		BEWRITE(e->startoffset);
		BEWRITE(e->len);
		BEWRITE(e->flags1);
		BEWRITE(e->flags2);
	}
#undef BEWRITE
	/* empty entry signifies end of directory */
	tmp = 0;
	val = fwrite(&tmp, 4, 8, ctx->fh);
	if (val == 0)
		LOG(("empty write end"));
}

struct container_ctx *container_create(const char *filename,
					const unsigned char *name,
					const unsigned char *author)
{
	size_t val;
	struct container_ctx *ctx = calloc(sizeof(struct container_ctx), 1);

	ctx->fh = fopen(filename, "wb");

	if (ctx->fh == NULL) {
		free(ctx);
		return NULL;
	}

	ctx->creating = true;
	ctx->entries = 0;
	ctx->directory = NULL;
	ctx->header.parser = htonl(3);
	strncpy((char *)ctx->header.name, (char *)name, 32);
	strncpy((char *)ctx->header.author, (char *)author, 64);

	val = fwrite("NSTM", 4, 1, ctx->fh);
	if (val == 0)
		LOG(("empty write NSTM"));
	val = fwrite(&ctx->header.parser, 4, 1, ctx->fh);
	if (val == 0)
		LOG(("empty write parser"));
	val = fwrite(ctx->header.name, 32, 1, ctx->fh);
	if (val == 0)
		LOG(("empty write name"));
	val = fwrite(ctx->header.author, 64, 1, ctx->fh);
	if (val == 0)
		LOG(("empty write author"));

	ctx->header.diroffset = 108;

	/* skip over the directory offset for now, and fill it in later.
	 * we don't know where it'll be yet!
	 */

	fseek(ctx->fh, 108, SEEK_SET);

	return ctx;
}

void container_add(struct container_ctx *ctx, const unsigned char *entryname,
					const unsigned char *data,
					const u_int32_t datalen)
{
	size_t val;
	container_add_to_dir(ctx, entryname, ftell(ctx->fh), datalen);
	val = fwrite(data, datalen, 1, ctx->fh);
	if (val == 0)
		LOG(("empty write add file"));
}

void container_close(struct container_ctx *ctx)
{
	if (ctx->creating == true) {
		size_t flen, nflen, val;

		/* discover where the directory's going to go. */
		flen = container_filelen(ctx->fh);
		flen = (flen + 3) & (~3); /* round up to nearest 4 bytes */

		/* write this location to the header */
		fseek(ctx->fh, 104, SEEK_SET);
		nflen = htonl(flen);
		val = fwrite(&nflen, 4, 1, ctx->fh);
		if (val == 0)
			LOG(("empty write directory location"));

		/* seek to where the directory will be, and write it */
		fseek(ctx->fh, flen, SEEK_SET);
		container_write_dir(ctx);

	} else if (ctx->processed) {
#ifdef WITH_MMAP
		munmap(ctx->data, ctx->header.diroffset);
#else
		free(ctx->data);
#endif
	}

	fclose(ctx->fh);
	free(ctx);
}

#ifdef WITH_THEME_INSTALL

/**
 * install theme from container
 * \param themefile a file containing the containerized theme
 * \param dirbasename a directory basename including trailing path sep; the
 * full path of the theme is then a subdirectory of that
 * caller owns reference to returned string, NULL for error
 */

char *container_extract_theme(const char *themefile, const char *dirbasename)
{
	struct stat statbuf;
	struct container_ctx *cctx;
	FILE *fh;
	size_t val;
	const unsigned char *e, *d;
	char *themename, *dirname;
	char path[PATH_MAX];
	int state = 0;
	unsigned int i;
	u_int32_t flen;

	cctx = container_open(themefile);
	if (cctx == NULL) {
		warn_user("FileOpenError", themefile);
		return NULL;
	}
	themename = strdup((const char *)container_get_name(cctx));
	if (themename == NULL) {
		warn_user("NoMemory", 0);
		container_close(cctx);
		return NULL;
	}
	LOG(("theme name: %s", themename));
	LOG(("theme author: %s", container_get_author(cctx)));
	
	dirname = malloc(strlen(dirbasename) + strlen(themename) + 2);
	if (dirname == NULL) {
		warn_user(messages_get("NoMemory"), 0);
		free(themename);
		container_close(cctx);
		return NULL;
	}
	strcpy(dirname, dirbasename);
	strcat(dirname, themename);
	if (stat(dirname, &statbuf) != -1) {
		warn_user("DirectoryError", dirname);
		container_close(cctx);
		free(dirname);
		free(themename);
		return NULL;
	}
	mkdir(dirname, S_IRWXU);

	for (e = container_iterate(cctx, &state), i = 0; i < cctx->entries;
			e = container_iterate(cctx, &state), i++) {
		LOG(("extracting %s", e));
		snprintf(path, PATH_MAX, "%s/%s", dirname, e);
		fh = fopen(path, "wb");
		if (fh == NULL) {
			warn_user("FileOpenError", (char *)e);
		} else {
			d = container_get(cctx, e, &flen);
			val = fwrite(d, flen, 1, fh);
			if (val == 0)
				LOG(("empty write"));
			fclose(fh);
		}
	}
	LOG(("theme container unpacked"));
	container_close(cctx);
	free(dirname);
	return themename;

}

#endif

#ifdef TEST_RIG
int main(int argc, char *argv[])
{
	struct container_ctx *ctx = container_create("test.theme", "Test theme",
				"Rob Kendrick");
	u_int32_t size;
	int state = 0;
	char *n;

	container_add(ctx, "CHEESE", "This is a test of some cheese.", sizeof("This is a test of some cheese."));
	container_add(ctx, "FOO", "This is a test of some cheese.", sizeof("This is a test of some cheese."));

	container_close(ctx);

	ctx = container_open("test.theme");

	printf("Theme name: %s\n", container_get_name(ctx));
	printf("Theme author: %s\n", container_get_author(ctx));

	printf("Test string: %s\n", container_get(ctx, "CHEESE", &size));
	printf("Length of text: %d\n", size);

	while ( (n = container_iterate(ctx, &state)) ) {
		printf("%s\n", n);
	}

	container_close(ctx);

	exit(0);
}
#endif

#ifdef NSTHEME
	/* code to implement a simple container creator/extractor */
#include <getopt.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>

static bool verbose = false;

static void show_usage(const char *argv0)
{
	fprintf(stderr, "%s [options] <theme file> <directory>\n", argv0);
	fprintf(stderr, " --help       This text\n");
	fprintf(stderr, " --create     Create theme file from directory\n");
	fprintf(stderr, " --extract    Extract theme file into directory\n");
	fprintf(stderr, " --name x     Set theme's name when creating\n");
	fprintf(stderr, " --author x   Set theme's author when creating\n");
	fprintf(stderr, " --verbose    Print progress information\n");
	fprintf(stderr, "\nOne and only one of --create or --extract must be specified.\n");
}

static void extract_theme(const char *themefile, const char *dirname)
{
	struct stat statbuf;
	struct container_ctx *cctx;
	FILE *fh;
	const unsigned char *e, *d;
	char path[PATH_MAX];
	int i, state = 0;
	u_int32_t flen;


	if (stat(dirname, &statbuf) != -1) {
		fprintf(stderr, "error: directory '%s' already exists.\n",
			dirname);
		exit(1);
	}

	mkdir(dirname, S_IRWXU);

	cctx = container_open(themefile);
	if (cctx == NULL) {
		fprintf(stderr, "error: unable to open theme file '%s'\n",
			themefile);
		exit(1);
	}

	if (verbose == true) {
		printf("theme name: %s\n", container_get_name(cctx));
		printf("theme author: %s\n", container_get_author(cctx));
	}

	for (e = container_iterate(cctx, &state), i = 0; i < cctx->entries;
			e = container_iterate(cctx, &state), i++) {
		if (verbose == true)
			printf("extracting %s\n", e);
		snprintf(path, PATH_MAX, "%s/%s", dirname, e);
		fh = fopen(path, "wb");
		if (fh == NULL) {
			perror("warning: unable to open file for output");
		} else {
			d = container_get(cctx, e, &flen);
			fwrite(d, flen, 1, fh);
			fclose(fh);
		}
	}

	container_close(cctx);

}

static void create_theme(const char *themefile, const char *dirname,
				const unsigned char *name,
				const unsigned char *author)
{
	DIR *dir = opendir(dirname);
	FILE *fh;
	struct dirent *e;
	struct stat statbuf;
	struct container_ctx *cctx;
	unsigned char *data;
	char path[PATH_MAX];
	size_t flen;
	int t;

	if (dir == NULL) {
		perror("error: unable to open directory");
		exit(1);
	}

	cctx = container_create(themefile, name, author);

	errno = 0;	/* to distinguish between end of dir and err */

	while ((e = readdir(dir)) != NULL) {
		if (strcmp(e->d_name, ".") != 0 &&
			strcmp(e->d_name, "..") != 0) {
			/* not the metadirs, so we want to process this. */
			if (verbose == true)
				printf("adding %s\n", e->d_name);
			if (strlen(e->d_name) > 63) {
				fprintf(stderr,
			"warning: name truncated to length 63.\n");
			}

			snprintf(path, PATH_MAX, "%s/%s", dirname, e->d_name);

			stat(path, &statbuf);
			if (S_ISDIR(statbuf.st_mode)) {
				fprintf(stderr,
					"warning: skipping directory '%s'\n",
					e->d_name);
				continue;
			}

			fh = fopen(path, "rb");
			if (fh == NULL) {
				fprintf(stderr,
					"warning: unable to open, skipping.");
			} else {
				flen = statbuf.st_size;
				data = malloc(flen);
				t = fread(data, flen, 1, fh);
				fclose(fh);
				container_add(cctx, (unsigned char *)e->d_name,
						data, flen);
				free(data);
			}
		}
		errno = 0;
	}

	if (errno != 0) {
		perror("error: couldn't enumerate directory");
		closedir(dir);
		container_close(cctx);
		exit(1);
	}

	closedir(dir);
	container_close(cctx);
}

int main(int argc, char *argv[])
{
	static struct option l_opts[] = {
		{ "help", 0, 0, 'h' },
		{ "create", 0, 0, 'c' },
		{ "extract", 0, 0, 'x' },
		{ "name", 1, 0, 'n' },
		{ "author", 1, 0, 'a' },
		{ "verbose", 0, 0, 'v' },

		{ NULL, 0, 0, 0 }
	};

	static char *s_opts = "hcxn:a:v";
	int optch, optidx;
	bool creating = false, extracting = false;
	unsigned char name[32] = { '\0' }, author[64] = { '\0' };
	char *themefile, *dirname;

	while ((optch = getopt_long(argc, argv, s_opts, l_opts, &optidx)) != -1)
		switch (optch) {
		case 'h':
			show_usage(argv[0]);
			exit(0);
			break;
		case 'c':
			creating = true;
			break;
		case 'x':
			extracting = true;
			break;
		case 'n':
			strncpy((char *)name, optarg, 31);
			if (strlen(optarg) > 32)
				fprintf(stderr, "warning: theme name truncated to 32 characters.\n");
			break;
		case 'a':
			strncpy((char *)author, optarg, 63);
			if (strlen(optarg) > 64)
				fprintf(stderr, "warning: theme author truncated to 64 characters.\n");
			break;
		case 'v':
			verbose = true;
			break;
		default:
			show_usage(argv[0]);
			exit(1);
			break;
		}

	if (creating == extracting) {
		show_usage(argv[0]);
		exit(1);
	}

	if ((argc - optind) < 2) {
		show_usage(argv[0]);
		exit(1);
	}

	if (creating == true &&
		(strlen((char *)name) == 0 || strlen((char *)author) == 0)) {
		fprintf(stderr, "No theme name and/or author specified.\n");
		show_usage(argv[0]);
		exit(1);
	}

	themefile = strdup(argv[optind]);
	dirname = strdup(argv[optind + 1]);

	if (verbose == true)
		printf("%s '%s' %s directory '%s'\n",
			creating ? "creating" : "extracting", themefile,
			creating ? "from" : "to", dirname);

	if (creating) {
		if (verbose == true)
			printf("name = %s, author = %s\n", name, author);
		create_theme(themefile, dirname, name, author);
	} else {
		extract_theme(themefile, dirname);
	}

	return 0;
}
#endif