summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/zippath.c
blob: 1974fe973eeed4d6df62d749cac1acb871a5ebe4 (plain) (blame)
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/***************************************************************************

    zippath.c

    File/directory/path operations that work with ZIP files

***************************************************************************/

#include <ctype.h>
#include <stdlib.h>
#include <new>
#include "zippath.h"
#include "unzip.h"
#include "corestr.h"
#include "osdcore.h"


/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

struct zippath_returned_directory
{
	zippath_returned_directory *next;
	astring name;
};



class zippath_directory
{
public:
	zippath_directory()
		: returned_parent(false),
		  directory(NULL),
		  called_zip_first(false),
		  zipfile(NULL),
		  returned_dirlist(NULL) { }

	/* common */
	bool returned_parent;
	osd_directory_entry returned_entry;

	/* specific to normal directories */
	osd_directory *directory;

	/* specific to ZIP directories */
	bool called_zip_first;
	zip_file *zipfile;
	astring zipprefix;
	zippath_returned_directory *returned_dirlist;
};


/***************************************************************************
    FUNCTION PROTOTYPES
***************************************************************************/

static const zip_file_header *zippath_find_sub_path(zip_file *zipfile, const char *subpath, osd_dir_entry_type *type);
static int is_zip_file(const char *path);
static int is_zip_file_separator(char c);


/***************************************************************************
    PATH OPERATIONS
***************************************************************************/

//============================================================
//  is_path_separator
//============================================================

int is_path_separator(char c)
{
	return (c == '/') || (c == '\\');
}

/*-------------------------------------------------
    parse_parent_path - parses out the parent path
-------------------------------------------------*/

static void parse_parent_path(const char *path, int *beginpos, int *endpos)
{
	int length = strlen(path);
	int pos;

	/* skip over trailing path separators */
	pos = length - 1;
	while((pos > 0) && is_path_separator(path[pos]))
		pos--;

	/* return endpos */
	if (endpos != NULL)
		*endpos = pos;

	/* now skip until we find a path separator */
	while((pos >= 0) && !is_path_separator(path[pos]))
		pos--;

	/* return beginpos */
	if (beginpos != NULL)
		*beginpos = pos;
}



/*-------------------------------------------------
    zippath_parent - retrieves the parent directory
-------------------------------------------------*/

astring &zippath_parent(astring &dst, const char *path)
{
	int pos;
	parse_parent_path(path, &pos, NULL);

	/* return the result */
	return (pos >= 0) ? dst.cpy(path, pos + 1) : dst.reset();
}



/*-------------------------------------------------
    zippath_parent_basename - retrieves the parent
    directory basename
-------------------------------------------------*/

astring &zippath_parent_basename(astring &dst, const char *path)
{
	int beginpos, endpos;
	parse_parent_path(path, &beginpos, &endpos);

	return dst.cpy(path + beginpos + 1, endpos - beginpos);
}



/*-------------------------------------------------
    zippath_combine - combines two paths
-------------------------------------------------*/

astring &zippath_combine(astring &dst, const char *path1, const char *path2)
{
	if (!strcmp(path2, "."))
	{
		dst.cpy(path1);
	}
	else if (!strcmp(path2, ".."))
	{
		zippath_parent(dst, path1);
	}
	else if (osd_is_absolute_path(path2))
	{
		dst.cpy(path2);
	}
	else if ((path1[0] != '\0') && !is_path_separator(path1[strlen(path1) - 1]))
	{
		dst.cpy(path1).cat(PATH_SEPARATOR).cat(path2);
	}
	else
	{
		dst.cpy(path1).cat(path2);
	}
	return dst;
}



/***************************************************************************
    FILE OPERATIONS
***************************************************************************/

/*-------------------------------------------------
    file_error_from_zip_error - translates a
    file_error to a zip_error
-------------------------------------------------*/

static file_error file_error_from_zip_error(zip_error ziperr)
{
	file_error filerr;
	switch(ziperr)
	{
		case ZIPERR_NONE:
			filerr = FILERR_NONE;
			break;
		case ZIPERR_OUT_OF_MEMORY:
			filerr = FILERR_OUT_OF_MEMORY;
			break;
		case ZIPERR_BAD_SIGNATURE:
		case ZIPERR_DECOMPRESS_ERROR:
		case ZIPERR_FILE_TRUNCATED:
		case ZIPERR_FILE_CORRUPT:
		case ZIPERR_UNSUPPORTED:
		case ZIPERR_FILE_ERROR:
			filerr = FILERR_INVALID_DATA;
			break;
		case ZIPERR_BUFFER_TOO_SMALL:
		default:
			filerr = FILERR_FAILURE;
			break;
	}
	return filerr;
}


/*-------------------------------------------------
    create_core_file_from_zip - creates a core_file
    from a zip file entry
-------------------------------------------------*/

static file_error create_core_file_from_zip(zip_file *zip, const zip_file_header *header, core_file *&file)
{
	file_error filerr;
	zip_error ziperr;
	void *ptr;

	ptr = malloc(header->uncompressed_length);
	if (ptr == NULL)
	{
		filerr = FILERR_OUT_OF_MEMORY;
		goto done;
	}

	ziperr = zip_file_decompress(zip, ptr, header->uncompressed_length);
	if (ziperr != ZIPERR_NONE)
	{
		filerr = file_error_from_zip_error(ziperr);
		goto done;
	}

	filerr = core_fopen_ram_copy(ptr, header->uncompressed_length, OPEN_FLAG_READ, &file);
	if (filerr != FILERR_NONE)
		goto done;

done:
	if (ptr != NULL)
		free(ptr);
	return filerr;
}


/*-------------------------------------------------
    zippath_fopen - opens a zip path file
-------------------------------------------------*/

file_error zippath_fopen(const char *filename, UINT32 openflags, core_file *&file, astring &revised_path)
{
	file_error filerr = FILERR_NOT_FOUND;
	zip_error ziperr;
	zip_file *zip = NULL;
	const zip_file_header *header;
	osd_dir_entry_type entry_type;
	char *alloc_fullpath = NULL;
	int len;

	/* first, set up the two types of paths */
	astring mainpath(filename);
	astring subpath;
	file = NULL;

	/* loop through */
	while((file == NULL) && (mainpath.len() > 0)
		&& ((openflags == OPEN_FLAG_READ) || (subpath.len() == 0)))
	{
		/* is the mainpath a ZIP path? */
		if (is_zip_file(mainpath))
		{
			/* this file might be a zip file - lets take a look */
			ziperr = zip_file_open(mainpath, &zip);
			if (ziperr == ZIPERR_NONE)
			{
				/* it is a zip file - error if we're not opening for reading */
				if (openflags != OPEN_FLAG_READ)
				{
					filerr = FILERR_ACCESS_DENIED;
					goto done;
				}

				if (subpath.len() > 0)
					header = zippath_find_sub_path(zip, subpath, &entry_type);
				else
					header = zip_file_first_file(zip);

				if (header == NULL)
				{
					filerr = FILERR_NOT_FOUND;
					goto done;
				}

				/* attempt to read the file */
				filerr = create_core_file_from_zip(zip, header, file);
				if (filerr != FILERR_NONE)
					goto done;

				/* update subpath, if appropriate */
				if (subpath.len() == 0)
					subpath.cpy(header->filename);

				/* we're done */
				goto done;
			}
		}

		if (subpath.len() == 0)
			filerr = core_fopen(filename, openflags, &file);
		else
			filerr = FILERR_NOT_FOUND;

		/* if we errored, then go up a directory */
		if (filerr != FILERR_NONE)
		{
			/* go up a directory */
			astring temp;
			zippath_parent(temp, mainpath);

			/* append to the sub path */
			if (subpath.len() > 0)
			{
				astring temp2;
				temp2.cpysubstr(mainpath, temp.len()).cat(PATH_SEPARATOR).cat(subpath);
				subpath.cpy(temp2);
			}
			else
				subpath.cpysubstr(mainpath, temp.len());

			/* get the new main path, truncating path separators */
			len = temp.len();
			while (len > 0 && is_zip_file_separator(temp[len - 1]))
				len--;
			mainpath.cpysubstr(temp, 0, len);
		}
	}

done:
	/* store the revised path */
	revised_path.reset();
	if (filerr == FILERR_NONE)
	{
		/* cannonicalize mainpath */
		filerr = osd_get_full_path(&alloc_fullpath, mainpath);
		if (filerr == FILERR_NONE)
		{
			if (subpath.len() > 0)
				revised_path.cpy(alloc_fullpath).cat(PATH_SEPARATOR).cat(subpath);
			else
				revised_path.cpy(alloc_fullpath);
		}
	}

	if (zip != NULL)
		zip_file_close(zip);
	if (alloc_fullpath != NULL)
		osd_free(alloc_fullpath);
	return filerr;
}


/***************************************************************************
    DIRECTORY OPERATIONS
***************************************************************************/

/*-------------------------------------------------
    is_root - tests to see if this path is the root
-------------------------------------------------*/

static int is_root(const char *path)
{
	int i = 0;

	/* skip drive letter */
	if (isalpha(path[i]) && (path[i + 1] == ':'))
		i += 2;

	/* skip path separators */
	while (is_path_separator(path[i]))
		i++;

	return path[i] == '\0';
}



/*-------------------------------------------------
    is_zip_file - tests to see if this file is a
    ZIP file
-------------------------------------------------*/

static int is_zip_file(const char *path)
{
	const char *s = strrchr(path, '.');
	return (s != NULL) && !core_stricmp(s, ".zip");
}



/*-------------------------------------------------
    is_zip_file_separator - returns whether this
    character is a path separator within a ZIP file
-------------------------------------------------*/

static int is_zip_file_separator(char c)
{
	return (c == '/') || (c == '\\');
}



/*-------------------------------------------------
    is_zip_path_separator - returns whether this
    character is a path separator within a ZIP path
-------------------------------------------------*/

static int is_zip_path_separator(char c)
{
	return is_zip_file_separator(c) || is_path_separator(c);
}



/*-------------------------------------------------
    next_path_char - lexes out the next path
    character, normalizing separators as '/'
-------------------------------------------------*/

static char next_path_char(const char *s, int *pos)
{
	char result;

	/* skip over any initial separators */
	if (*pos == 0)
	{
		while(is_zip_file_separator(s[*pos]))
			(*pos)++;
	}

	/* are we at a path separator? */
	if (is_zip_file_separator(s[*pos]))
	{
		/* skip over path separators */
		while(is_zip_file_separator(s[*pos]))
			(*pos)++;

		/* normalize as '/' */
		result = '/';
	}
	else if (s[*pos] != '\0')
	{
		/* return character */
		result = tolower(s[(*pos)++]);
	}
	else
	{
		/* return NUL */
		result = '\0';
	}
	return result;
}




/*-------------------------------------------------
    zippath_find_sub_path - attempts to identify the
    type of a sub path in a zip file
-------------------------------------------------*/

static const zip_file_header *zippath_find_sub_path(zip_file *zipfile, const char *subpath, osd_dir_entry_type *type)
{
	int i, j;
	char c1, c2, last_char;
	const zip_file_header *header;

	for (header = zip_file_first_file(zipfile); header != NULL; header = zip_file_next_file(zipfile))
	{
		/* special case */
		if (subpath == NULL)
		{
			if (type != NULL)
				*type = ENTTYPE_FILE;
			return header;
		}

		i = 0;
		j = 0;
		last_char = '/';
                while(((c1 = next_path_char(header->filename, &i)) == (c2 = next_path_char(subpath, &j))) &&
                        ( c1 != '\0' && c2 != '\0' ))
                        last_char = c2;


		if (c2 == '\0')
		{
			if (c1 == '\0')
			{
				if (type != NULL)
					*type = ENTTYPE_FILE;
				return header;
			}
			else if ((last_char == '/') || (c1 == '/'))
			{
				if (type != NULL)
					*type = ENTTYPE_DIR;
				return header;
			}
		}
	}

	if (type != NULL)
		*type = ENTTYPE_NONE;
	return NULL;
}



/*-------------------------------------------------
    zippath_resolve - separates a ZIP path out into
    true path and ZIP entry components
-------------------------------------------------*/

static file_error zippath_resolve(const char *path, osd_dir_entry_type &entry_type, zip_file *&zipfile, astring &newpath)
{
	file_error err;
	osd_directory_entry *current_entry = NULL;
	osd_dir_entry_type current_entry_type;
	int went_up = FALSE;
	int i;
	
	newpath.reset();

	/* be conservative */
	entry_type = ENTTYPE_NONE;
	zipfile = NULL;

	astring apath(path);
	astring apath_trimmed;
	do
	{
		/* trim the path of trailing path separators */
		i = apath.len();
		while (i > 1 && is_path_separator(apath[i - 1]))
			i--;
		apath_trimmed.cpysubstr(apath, 0, i);

		/* stat the path */
		current_entry = osd_stat(apath_trimmed);

		/* did we find anything? */
		if (current_entry != NULL)
		{
			/* get the entry type and free the stat entry */
			current_entry_type = current_entry->type;
			free(current_entry);
			current_entry = NULL;
		}
		else
		{
			/* if we have not found the file or directory, go up */
			current_entry_type = ENTTYPE_NONE;
			went_up = TRUE;
			astring parent;
			apath.cpy(zippath_parent(parent, apath));
		}
	}
	while (current_entry_type == ENTTYPE_NONE && !is_root(apath));

	/* if we did not find anything, then error out */
	if (current_entry_type == ENTTYPE_NONE)
	{
		err = FILERR_NOT_FOUND;
		goto done;
	}

	/* is this file a ZIP file? */
	if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(apath_trimmed)
		&& (zip_file_open(apath_trimmed, &zipfile) == ZIPERR_NONE))
	{
		i = strlen(path + apath.len());
		while (i > 0 && is_zip_path_separator(path[apath.len() + i - 1]))
			i--;
		newpath.cpy(path + apath.len(), i);

		/* this was a true ZIP path - attempt to identify the type of path */
		zippath_find_sub_path(zipfile, newpath, &current_entry_type);
		if (current_entry_type == ENTTYPE_NONE)
		{
			err = FILERR_NOT_FOUND;
			goto done;
		}
	}
	else
	{
		/* this was a normal path */
		if (went_up)
		{
			err = FILERR_NOT_FOUND;
			goto done;
		}
		newpath.cpy(path);
	}

	/* success! */
	entry_type = current_entry_type;
	err = FILERR_NONE;

done:
	return err;
}


/*-------------------------------------------------
    zippath_opendir - opens a directory
-------------------------------------------------*/

file_error zippath_opendir(const char *path, zippath_directory **directory)
{
	file_error err;

	/* allocate a directory */
	zippath_directory *result = new(std::nothrow) zippath_directory;
	if (result == NULL)
	{
		err = FILERR_OUT_OF_MEMORY;
		goto done;
	}

	/* resolve the path */
	osd_dir_entry_type entry_type;
	err = zippath_resolve(path, entry_type, result->zipfile, result->zipprefix);
	if (err != FILERR_NONE)
		goto done;

	/* we have to be a directory */
	if (entry_type != ENTTYPE_DIR)
	{
		err = FILERR_NOT_FOUND;
		goto done;
	}

	/* was the result a ZIP? */
	if (result->zipfile == NULL)
	{
		/* a conventional directory */
		result->directory = osd_opendir(path);
		if (result->directory == NULL)
		{
			err = FILERR_FAILURE;
			goto done;
		}

		/* is this path the root? if so, pretend we've already returned the parent */
		if (is_root(path))
			result->returned_parent = true;
	}

done:
	if ((directory == NULL || err != FILERR_NONE) && result != NULL)
	{
		zippath_closedir(result);
		result = NULL;
	}
	if (directory != NULL)
		*directory = result;
	return err;
}


/*-------------------------------------------------
    zippath_closedir - closes a directory
-------------------------------------------------*/

void zippath_closedir(zippath_directory *directory)
{
	if (directory->directory != NULL)
		osd_closedir(directory->directory);

	if (directory->zipfile != NULL)
		zip_file_close(directory->zipfile);

	while (directory->returned_dirlist != NULL)
	{
		zippath_returned_directory *dirlist = directory->returned_dirlist;
		directory->returned_dirlist = directory->returned_dirlist->next;
		delete dirlist;
	}

	delete directory;
}


/*-------------------------------------------------
    get_relative_path - checks to see if a specified
    header is in the zippath_directory, and if so
    returns the relative path
-------------------------------------------------*/

static const char *get_relative_path(zippath_directory *directory, const zip_file_header *header)
{
	const char *result = NULL;
	int len = directory->zipprefix.len();

	if ((len <= strlen(header->filename))
		&& !strncmp(directory->zipprefix, header->filename, len))
	{
		result = &header->filename[len];
		while(is_zip_file_separator(*result))
			result++;
	}

	return result;
}


/*-------------------------------------------------
    zippath_readdir - reads a directory
-------------------------------------------------*/

const osd_directory_entry *zippath_readdir(zippath_directory *directory)
{
	const osd_directory_entry *result = NULL;
	const zip_file_header *header;
	const char *relpath;
	const char *separator;
	const char *s;
	zippath_returned_directory *rdent;

	if (!directory->returned_parent)
	{
		/* first thing's first - return parent directory */
		directory->returned_parent = true;
		memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
		directory->returned_entry.name = "..";
		directory->returned_entry.type = ENTTYPE_DIR;
		result = &directory->returned_entry;
	}
	else if (directory->directory != NULL)
	{
		/* a normal directory read */
		do
		{
			result = osd_readdir(directory->directory);
		}
		while((result != NULL) && (!strcmp(result->name, ".") || !strcmp(result->name, "..")));

		/* special case - is this entry a ZIP file?  if so we need to return it as a "directory" */
		if ((result != NULL) && is_zip_file(result->name))
		{
			/* copy; but change the entry type */
			directory->returned_entry = *result;
			directory->returned_entry.type = ENTTYPE_DIR;
			result = &directory->returned_entry;
		}
	}
	else if (directory->zipfile != NULL)
	{
		do
		{
			/* a zip file read */
			do
			{
				if (!directory->called_zip_first)
					header = zip_file_first_file(directory->zipfile);
				else
					header = zip_file_next_file(directory->zipfile);
				directory->called_zip_first = true;
				relpath = NULL;
			}
			while((header != NULL) && ((relpath = get_relative_path(directory, header)) == NULL));

			if (relpath != NULL)
			{
				/* we've found a ZIP entry; but this may be an entry deep within the target directory */
				for (s = relpath; *s && !is_zip_file_separator(*s); s++)
					;
				separator = *s ? s : NULL;

				if (separator != NULL)
				{
					/* a nested entry; loop through returned_dirlist to see if we've returned the parent directory */
					for (rdent = directory->returned_dirlist; rdent != NULL; rdent = rdent->next)
					{
						if (!core_strnicmp(rdent->name, relpath, separator - relpath))
							break;
					}

					if (rdent == NULL)
					{
						/* we've found a new directory; add this to returned_dirlist */
						rdent = new zippath_returned_directory;
						rdent->next = directory->returned_dirlist;
						rdent->name.cpy(relpath, separator - relpath);
						directory->returned_dirlist = rdent;

						/* ...and return it */
						memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
						directory->returned_entry.name = rdent->name;
						directory->returned_entry.type = ENTTYPE_DIR;
						result = &directory->returned_entry;
					}
				}
				else
				{
					/* a real file */
					memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
					directory->returned_entry.name = relpath;
					directory->returned_entry.type = ENTTYPE_FILE;
					directory->returned_entry.size = header->uncompressed_length;
					result = &directory->returned_entry;
				}
			}
		}
		while((relpath != NULL) && (result == NULL));
	}
	return result;
}



/*-------------------------------------------------
    zippath_is_zip - returns TRUE if this path is
    a ZIP path or FALSE if not
-------------------------------------------------*/

int zippath_is_zip(zippath_directory *directory)
{
	return directory->zipfile != NULL;
}