-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.c
109 lines (89 loc) · 2.3 KB
/
fileio.c
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
/*
fileio.c --
File management.
*/
#include "shared.h"
uint8 *loadFromZipByName(char *archive, char *filename, int *filesize)
{
char name[PATH_MAX];
unsigned char *buffer;
int zerror = UNZ_OK;
unzFile zhandle;
unz_file_info zinfo;
zhandle = unzOpen(archive);
if(!zhandle) return (NULL);
/* Seek to first file in archive */
zerror = unzGoToFirstFile(zhandle);
if(zerror != UNZ_OK)
{
unzClose(zhandle);
return (NULL);
}
/* Get information about the file */
unzGetCurrentFileInfo(zhandle, &zinfo, &name[0], 0xff, NULL, 0, NULL, 0);
*filesize = zinfo.uncompressed_size;
/* Error: file size is zero */
if(*filesize <= 0)
{
unzClose(zhandle);
return (NULL);
}
/* Open current file */
zerror = unzOpenCurrentFile(zhandle);
if(zerror != UNZ_OK)
{
unzClose(zhandle);
return (NULL);
}
/* Allocate buffer and read in file */
buffer = malloc(*filesize);
if(!buffer) return (NULL);
zerror = unzReadCurrentFile(zhandle, buffer, *filesize);
/* Internal error: free buffer and close file */
if(zerror < 0 || zerror != *filesize)
{
free(buffer);
buffer = NULL;
unzCloseCurrentFile(zhandle);
unzClose(zhandle);
return (NULL);
}
/* Close current file and archive file */
unzCloseCurrentFile(zhandle);
unzClose(zhandle);
memcpy(filename, name, PATH_MAX);
return (buffer);
}
/*
Verifies if a file is a ZIP archive or not.
Returns: 1= ZIP archive, 0= not a ZIP archive
*/
int check_zip(char *filename)
{
uint8 buf[2];
FILE *fd = NULL;
fd = fopen(filename, "rb");
if(!fd) return (0);
fread(buf, 2, 1, fd);
fclose(fd);
if(memcmp(buf, "PK", 2) == 0) return (1);
return (0);
}
/*
Returns the size of a GZ compressed file.
*/
int gzsize(gzFile *gd)
{
#define CHUNKSIZE (0x10000)
int size = 0, length = 0;
unsigned char buffer[CHUNKSIZE];
gzrewind(gd);
do {
size = gzread(gd, buffer, CHUNKSIZE);
if(size <= 0) break;
length += size;
} while (!gzeof(gd));
gzrewind(gd);
return (length);
#undef CHUNKSIZE
}