Skip to content

Latest commit

 

History

History
255 lines (193 loc) · 5.8 KB

zip.c

File metadata and controls

255 lines (193 loc) · 5.8 KB
 
Jul 7, 2001
Jul 7, 2001
1
2
3
4
5
6
7
8
9
10
/*
* ZIP support routines for PhysicsFS.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
Jul 15, 2001
Jul 15, 2001
11
12
#include <string.h>
#include <unistd.h>
Jul 8, 2001
Jul 8, 2001
13
#include "physfs.h"
Jul 15, 2001
Jul 15, 2001
14
15
#include "unzip.h"
Jul 7, 2001
Jul 7, 2001
16
17
18
19
20
21
22
23
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
#if (!defined PHYSFS_SUPPORTS_ZIP)
#error PHYSFS_SUPPORTS_ZIP must be defined.
#endif
Jul 15, 2001
Jul 15, 2001
24
25
26
27
28
29
30
31
32
33
34
35
typedef struct
{
unzFile handle;
uLong totalEntries;
} ZIPinfo;
typedef struct
{
} ZIPfileinfo;
Jul 8, 2001
Jul 8, 2001
36
extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
Jul 8, 2001
Jul 8, 2001
37
static const FileFunctions __PHYSFS_FileFunctions_ZIP;
Jul 8, 2001
Jul 8, 2001
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
static int ZIP_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount)
{
} /* ZIP_read */
static int ZIP_eof(FileHandle *handle)
{
} /* ZIP_eof */
static int ZIP_tell(FileHandle *handle)
{
} /* ZIP_tell */
static int ZIP_seek(FileHandle *handle, int offset)
{
} /* ZIP_seek */
Jul 9, 2001
Jul 9, 2001
61
62
63
64
65
static int ZIP_fileLength(FileHandle *handle)
{
} /* ZIP_fileLength */
Jul 8, 2001
Jul 8, 2001
66
67
68
69
70
71
72
static int ZIP_fileClose(FileHandle *handle)
{
} /* ZIP_fileClose */
static int ZIP_isArchive(const char *filename, int forWriting)
{
Jul 15, 2001
Jul 15, 2001
73
74
75
76
77
78
79
80
81
82
83
84
int retval = 0;
unzFile unz = unzOpen(name);
unz_global_info global;
if (unz != NULL)
{
if (unzGetGlobalInfo(unz, &global) == UNZ_OK)
retval = 1;
unzClose(unz);
} /* if */
return(retval);
Jul 8, 2001
Jul 8, 2001
85
86
87
88
89
} /* ZIP_isArchive */
static DirHandle *ZIP_openArchive(const char *name, int forWriting)
{
Jul 15, 2001
Jul 15, 2001
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
unzFile unz = NULL;
DirHandle *retval = NULL;
unz_global_info global;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
errno = 0;
BAIL_IF_MACRO(access(name, R_OK) != 0, strerror(errno), NULL);
retval = malloc(sizeof (DirHandle));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
unz = unzOpen(name);
if ((unz == NULL) || (unzGetGlobalInfo(unz, &global) != UNZ_OK))
{
if (unz)
unzClose(unz);
Jul 16, 2001
Jul 16, 2001
107
free(retval);
Jul 15, 2001
Jul 15, 2001
108
109
110
111
112
113
114
115
116
117
118
119
BAIL_IF_MACRO(1, ERR_UNSUPPORTED_ARCHIVE, NULL);
} /* if */
retval->opaque = malloc(sizeof (ZIPinfo));
if (retval->opaque == NULL)
{
free(retval);
unzClose(unz);
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
} /* if */
((ZIPinfo *) (retval->opaque))->handle = unz;
Jul 16, 2001
Jul 16, 2001
120
((ZIPinfo *) (retval->opaque))->totalEntries = global.number_entry;
Jul 15, 2001
Jul 15, 2001
121
122
return(retval);
Jul 8, 2001
Jul 8, 2001
123
124
125
} /* ZIP_openArchive */
Jul 16, 2001
Jul 16, 2001
126
127
128
static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
const char *dirname,
int omitSymLinks)
Jul 8, 2001
Jul 8, 2001
129
{
Jul 15, 2001
Jul 15, 2001
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
ZIPinfo *zi = (ZIPinfo *) (h->opaque);
unzFile fh = zi->handle;
int i;
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
char buffer[256];
char *d;
/* jump to first file entry... */
BAIL_IF_MACRO(unzGoToFirstFile(fh) != UNZ_OK, ERR_IO_ERROR, NULL);
i = strlen(dirname);
d = malloc(i + 1);
strcpy(d, dirname);
if ((i > 0) && (d[i - 1] == '/')) /* no trailing slash. */
d[i - 1] = '\0';
for (i = 0; i < zi->totalEntries; i++)
{
char *ptr;
unzGetCurrentFileInfo(fh, NULL, buf, sizeof (buf), NULL, 0, NULL, 0);
ptr = strrchr(p, '/'); /* !!! check this! */
if (ptr == NULL)
{
if (*d != '\0')
continue; /* not for this dir; skip it. */
ptr = buf;
else
{
*ptr = '\0';
ptr++;
if (strcmp(buf, d) != 0)
continue; /* not for this dir; skip it. */
} /* else */
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
Jul 16, 2001
Jul 16, 2001
167
if (l == NULL)
Jul 15, 2001
Jul 15, 2001
168
169
170
171
172
173
174
175
176
break;
l->str = (char *) malloc(strlen(ptr) + 1);
if (l->str == NULL)
{
free(l);
break;
} /* if */
Jul 16, 2001
Jul 16, 2001
177
178
strcpy(l->str, ptr);
Jul 15, 2001
Jul 15, 2001
179
180
181
182
183
184
185
186
187
188
189
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
} /* for */
free(d);
return(retval);
Jul 8, 2001
Jul 8, 2001
190
191
192
} /* ZIP_enumerateFiles */
Jul 8, 2001
Jul 8, 2001
193
static int ZIP_exists(DirHandle *h, const char *name)
Jul 8, 2001
Jul 8, 2001
194
195
196
197
{
} /* ZIP_exists */
Jul 8, 2001
Jul 8, 2001
198
static int ZIP_isDirectory(DirHandle *h, const char *name)
Jul 8, 2001
Jul 8, 2001
199
200
201
202
{
} /* ZIP_isDirectory */
Jul 8, 2001
Jul 8, 2001
203
static int ZIP_isSymLink(DirHandle *h, const char *name)
Jul 8, 2001
Jul 8, 2001
204
205
206
207
{
} /* ZIP_isSymLink */
Jul 8, 2001
Jul 8, 2001
208
static FileHandle *ZIP_openRead(DirHandle *h, const char *filename)
Jul 8, 2001
Jul 8, 2001
209
210
211
212
{
} /* ZIP_openRead */
Jul 8, 2001
Jul 8, 2001
213
static void ZIP_dirClose(DirHandle *h)
Jul 8, 2001
Jul 8, 2001
214
215
216
{
} /* ZIP_dirClose */
Jul 7, 2001
Jul 7, 2001
217
Jul 8, 2001
Jul 8, 2001
218
static const FileFunctions __PHYSFS_FileFunctions_ZIP =
Jul 7, 2001
Jul 7, 2001
219
{
Jul 9, 2001
Jul 9, 2001
220
221
222
223
224
225
226
ZIP_read, /* read() method */
NULL, /* write() method */
ZIP_eof, /* eof() method */
ZIP_tell, /* tell() method */
ZIP_seek, /* seek() method */
ZIP_fileLength, /* fileLength() method */
ZIP_fileClose /* fileClose() method */
Jul 7, 2001
Jul 7, 2001
227
228
229
230
231
};
const DirFunctions __PHYSFS_DirFunctions_ZIP =
{
Jul 8, 2001
Jul 8, 2001
232
233
234
235
236
237
238
239
240
241
242
ZIP_isArchive, /* isArchive() method */
ZIP_openArchive, /* openArchive() method */
ZIP_enumerateFiles, /* enumerateFiles() method */
ZIP_exists, /* exists() method */
ZIP_isDirectory, /* isDirectory() method */
ZIP_isSymLink, /* isSymLink() method */
ZIP_openRead, /* openRead() method */
NULL, /* openWrite() method */
NULL, /* openAppend() method */
NULL, /* remove() method */
NULL, /* mkdir() method */
Jul 8, 2001
Jul 8, 2001
243
ZIP_dirClose /* dirClose() method */
Jul 7, 2001
Jul 7, 2001
244
245
};
Jul 8, 2001
Jul 8, 2001
246
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
Jul 7, 2001
Jul 7, 2001
247
248
{
"ZIP",
Jul 9, 2001
Jul 9, 2001
249
"PkZip/WinZip/Info-Zip compatible",
Jul 16, 2001
Jul 16, 2001
250
251
"Ryan C. Gordon (icculus@linuxgames.com)",
"http://www.icculus.org/~icculus/",
Jul 7, 2001
Jul 7, 2001
252
253
254
};
/* end of zip.c ... */