diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:21:57 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:21:57 -0700 |
commit | 13a294f044ef0a89b2dcbfbb5d4d4c792673348e (patch) | |
tree | d9b377b4d8c00633c3da4e96659bfba9b08287f2 /contrib/minizip/minizip.c | |
parent | 7c2a874e50b871d04fbd19501f7b42cff55e5abc (diff) | |
download | zlib-1.2.0.1.tar.gz zlib-1.2.0.1.tar.bz2 zlib-1.2.0.1.zip |
zlib 1.2.0.1v1.2.0.1
Diffstat (limited to 'contrib/minizip/minizip.c')
-rw-r--r-- | contrib/minizip/minizip.c | 230 |
1 files changed, 160 insertions, 70 deletions
diff --git a/contrib/minizip/minizip.c b/contrib/minizip/minizip.c index 5e492d2..5792a1e 100644 --- a/contrib/minizip/minizip.c +++ b/contrib/minizip/minizip.c | |||
@@ -17,6 +17,12 @@ | |||
17 | 17 | ||
18 | #include "zip.h" | 18 | #include "zip.h" |
19 | 19 | ||
20 | #ifdef WIN32 | ||
21 | #define USEWIN32IOAPI | ||
22 | #include "iowin32.h" | ||
23 | #endif | ||
24 | |||
25 | |||
20 | 26 | ||
21 | #define WRITEBUFFERSIZE (16384) | 27 | #define WRITEBUFFERSIZE (16384) |
22 | #define MAXFILENAME (256) | 28 | #define MAXFILENAME (256) |
@@ -55,12 +61,16 @@ uLong filetime(f, tmzip, dt) | |||
55 | struct stat s; /* results of stat() */ | 61 | struct stat s; /* results of stat() */ |
56 | struct tm* filedate; | 62 | struct tm* filedate; |
57 | time_t tm_t=0; | 63 | time_t tm_t=0; |
58 | 64 | ||
59 | if (strcmp(f,"-")!=0) | 65 | if (strcmp(f,"-")!=0) |
60 | { | 66 | { |
61 | char name[MAXFILENAME]; | 67 | char name[MAXFILENAME+1]; |
62 | int len = strlen(f); | 68 | int len = strlen(f); |
63 | strcpy(name, f); | 69 | |
70 | strncpy(name, f,MAXFILENAME-1); | ||
71 | /* strncpy doesnt append the trailing NULL, of the string is too long. */ | ||
72 | name[ MAXFILENAME ] = '\0'; | ||
73 | |||
64 | if (name[len - 1] == '/') | 74 | if (name[len - 1] == '/') |
65 | name[len - 1] = '\0'; | 75 | name[len - 1] = '\0'; |
66 | /* not all systems allow stat'ing a file with / appended */ | 76 | /* not all systems allow stat'ing a file with / appended */ |
@@ -98,10 +108,10 @@ uLong filetime(f, tmzip, dt) | |||
98 | int check_exist_file(filename) | 108 | int check_exist_file(filename) |
99 | const char* filename; | 109 | const char* filename; |
100 | { | 110 | { |
101 | FILE* ftestexist; | 111 | FILE* ftestexist; |
102 | int ret = 1; | 112 | int ret = 1; |
103 | ftestexist = fopen(filename,"rb"); | 113 | ftestexist = fopen(filename,"rb"); |
104 | if (ftestexist==NULL) | 114 | if (ftestexist==NULL) |
105 | ret = 0; | 115 | ret = 0; |
106 | else | 116 | else |
107 | fclose(ftestexist); | 117 | fclose(ftestexist); |
@@ -110,59 +120,107 @@ int check_exist_file(filename) | |||
110 | 120 | ||
111 | void do_banner() | 121 | void do_banner() |
112 | { | 122 | { |
113 | printf("MiniZip 0.15, demo of zLib + Zip package written by Gilles Vollant\n"); | 123 | printf("MiniZip 0.15, demo of zLib + Zip package written by Gilles Vollant\n"); |
114 | printf("more info at http://wwww.winimage/zLibDll/unzip.htm\n\n"); | 124 | printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); |
115 | } | 125 | } |
116 | 126 | ||
117 | void do_help() | 127 | void do_help() |
118 | { | 128 | { |
119 | printf("Usage : minizip [-o] file.zip [files_to_add]\n\n") ; | 129 | printf("Usage : minizip [-o] file.zip [files_to_add]\n\n") ; |
130 | } | ||
131 | |||
132 | /* calculate the CRC32 of a file, | ||
133 | because to encrypt a file, we need known the CRC32 of the file before */ | ||
134 | int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc) | ||
135 | { | ||
136 | unsigned long calculate_crc=0; | ||
137 | int err=ZIP_OK; | ||
138 | FILE * fin = fopen(filenameinzip,"rb"); | ||
139 | unsigned long size_read = 0; | ||
140 | unsigned long total_read = 0; | ||
141 | if (fin==NULL) | ||
142 | { | ||
143 | err = ZIP_ERRNO; | ||
144 | } | ||
145 | |||
146 | if (err == ZIP_OK) | ||
147 | do | ||
148 | { | ||
149 | err = ZIP_OK; | ||
150 | size_read = (int)fread(buf,1,size_buf,fin); | ||
151 | if (size_read < size_buf) | ||
152 | if (feof(fin)==0) | ||
153 | { | ||
154 | printf("error in reading %s\n",filenameinzip); | ||
155 | err = ZIP_ERRNO; | ||
156 | } | ||
157 | |||
158 | if (size_read>0) | ||
159 | calculate_crc = crc32(calculate_crc,buf,size_read); | ||
160 | total_read += size_read; | ||
161 | |||
162 | } while ((err == ZIP_OK) && (size_read>0)); | ||
163 | |||
164 | if (fin) | ||
165 | fclose(fin); | ||
166 | |||
167 | *result_crc=calculate_crc; | ||
168 | printf("file %s crc %x\n",filenameinzip,calculate_crc); | ||
169 | return err; | ||
120 | } | 170 | } |
121 | 171 | ||
122 | int main(argc,argv) | 172 | int main(argc,argv) |
123 | int argc; | 173 | int argc; |
124 | char *argv[]; | 174 | char *argv[]; |
125 | { | 175 | { |
126 | int i; | 176 | int i; |
127 | int opt_overwrite=0; | 177 | int opt_overwrite=0; |
128 | int opt_compress_level=Z_DEFAULT_COMPRESSION; | 178 | int opt_compress_level=Z_DEFAULT_COMPRESSION; |
129 | int zipfilenamearg = 0; | 179 | int zipfilenamearg = 0; |
130 | char filename_try[MAXFILENAME]; | 180 | char filename_try[MAXFILENAME+16]; |
131 | int zipok; | 181 | int zipok; |
132 | int err=0; | 182 | int err=0; |
133 | int size_buf=0; | 183 | int size_buf=0; |
134 | void* buf=NULL, | 184 | void* buf=NULL; |
185 | const char* password=NULL; | ||
135 | 186 | ||
136 | 187 | ||
137 | do_banner(); | 188 | do_banner(); |
138 | if (argc==1) | 189 | if (argc==1) |
139 | { | 190 | { |
140 | do_help(); | 191 | do_help(); |
141 | exit(0); | ||
142 | return 0; | 192 | return 0; |
143 | } | 193 | } |
144 | else | 194 | else |
145 | { | 195 | { |
146 | for (i=1;i<argc;i++) | 196 | for (i=1;i<argc;i++) |
147 | { | 197 | { |
148 | if ((*argv[i])=='-') | 198 | if ((*argv[i])=='-') |
149 | { | 199 | { |
150 | const char *p=argv[i]+1; | 200 | const char *p=argv[i]+1; |
151 | 201 | ||
152 | while ((*p)!='\0') | 202 | while ((*p)!='\0') |
153 | { | 203 | { |
154 | char c=*(p++);; | 204 | char c=*(p++);; |
155 | if ((c=='o') || (c=='O')) | 205 | if ((c=='o') || (c=='O')) |
156 | opt_overwrite = 1; | 206 | opt_overwrite = 1; |
207 | if ((c=='a') || (c=='A')) | ||
208 | opt_overwrite = 2; | ||
157 | if ((c>='0') && (c<='9')) | 209 | if ((c>='0') && (c<='9')) |
158 | opt_compress_level = c-'0'; | 210 | opt_compress_level = c-'0'; |
159 | } | 211 | |
160 | } | 212 | if (((c=='p') || (c=='P')) && (i+1<argc)) |
161 | else | 213 | { |
162 | if (zipfilenamearg == 0) | 214 | password=argv[i+1]; |
215 | i++; | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | else | ||
220 | if (zipfilenamearg == 0) | ||
163 | zipfilenamearg = i ; | 221 | zipfilenamearg = i ; |
164 | } | 222 | } |
165 | } | 223 | } |
166 | 224 | ||
167 | size_buf = WRITEBUFFERSIZE; | 225 | size_buf = WRITEBUFFERSIZE; |
168 | buf = (void*)malloc(size_buf); | 226 | buf = (void*)malloc(size_buf); |
@@ -172,16 +230,19 @@ int main(argc,argv) | |||
172 | return ZIP_INTERNALERROR; | 230 | return ZIP_INTERNALERROR; |
173 | } | 231 | } |
174 | 232 | ||
175 | if (zipfilenamearg==0) | 233 | if (zipfilenamearg==0) |
176 | zipok=0; | 234 | zipok=0; |
177 | else | 235 | else |
178 | { | 236 | { |
179 | int i,len; | 237 | int i,len; |
180 | int dot_found=0; | 238 | int dot_found=0; |
181 | 239 | ||
182 | zipok = 1 ; | 240 | zipok = 1 ; |
183 | strcpy(filename_try,argv[zipfilenamearg]); | 241 | strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); |
184 | len=strlen(filename_try); | 242 | /* strncpy doesnt append the trailing NULL, of the string is too long. */ |
243 | filename_try[ MAXFILENAME ] = '\0'; | ||
244 | |||
245 | len=(int)strlen(filename_try); | ||
185 | for (i=0;i<len;i++) | 246 | for (i=0;i<len;i++) |
186 | if (filename_try[i]=='.') | 247 | if (filename_try[i]=='.') |
187 | dot_found=1; | 248 | dot_found=1; |
@@ -189,36 +250,52 @@ int main(argc,argv) | |||
189 | if (dot_found==0) | 250 | if (dot_found==0) |
190 | strcat(filename_try,".zip"); | 251 | strcat(filename_try,".zip"); |
191 | 252 | ||
253 | if (opt_overwrite==2) | ||
254 | { | ||
255 | /* if the file don't exist, we not append file */ | ||
256 | if (check_exist_file(filename_try)==0) | ||
257 | opt_overwrite=1; | ||
258 | } | ||
259 | else | ||
192 | if (opt_overwrite==0) | 260 | if (opt_overwrite==0) |
193 | if (check_exist_file(filename_try)!=0) | 261 | if (check_exist_file(filename_try)!=0) |
194 | { | 262 | { |
195 | char rep; | 263 | char rep=0; |
196 | do | 264 | do |
197 | { | 265 | { |
198 | char answer[128]; | 266 | char answer[128]; |
199 | printf("The file %s exist. Overwrite ? [y]es, [n]o : ",filename_try); | 267 | printf("The file %s exist. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try); |
200 | scanf("%1s",answer); | 268 | scanf("%1s",answer); |
201 | rep = answer[0] ; | 269 | rep = answer[0] ; |
202 | if ((rep>='a') && (rep<='z')) | 270 | if ((rep>='a') && (rep<='z')) |
203 | rep -= 0x20; | 271 | rep -= 0x20; |
204 | } | 272 | } |
205 | while ((rep!='Y') && (rep!='N')); | 273 | while ((rep!='Y') && (rep!='N') && (rep!='A')); |
206 | if (rep=='N') | 274 | if (rep=='N') |
207 | zipok = 0; | 275 | zipok = 0; |
208 | } | 276 | if (rep=='A') |
277 | opt_overwrite = 2; | ||
278 | } | ||
209 | } | 279 | } |
210 | 280 | ||
211 | if (zipok==1) | 281 | if (zipok==1) |
212 | { | 282 | { |
213 | zipFile zf; | 283 | zipFile zf; |
214 | int errclose; | 284 | int errclose; |
215 | zf = zipOpen(filename_try,0); | 285 | #ifdef USEWIN32IOAPI |
286 | zlib_filefunc_def ffunc; | ||
287 | fill_win32_filefunc(&ffunc); | ||
288 | zf = zipOpen2(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); | ||
289 | #else | ||
290 | zf = zipOpen(filename_try,(opt_overwrite==2) ? 2 : 0); | ||
291 | #endif | ||
292 | |||
216 | if (zf == NULL) | 293 | if (zf == NULL) |
217 | { | 294 | { |
218 | printf("error opening %s\n",filename_try); | 295 | printf("error opening %s\n",filename_try); |
219 | err= ZIP_ERRNO; | 296 | err= ZIP_ERRNO; |
220 | } | 297 | } |
221 | else | 298 | else |
222 | printf("creating %s\n",filename_try); | 299 | printf("creating %s\n",filename_try); |
223 | 300 | ||
224 | for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) | 301 | for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) |
@@ -229,19 +306,31 @@ int main(argc,argv) | |||
229 | int size_read; | 306 | int size_read; |
230 | const char* filenameinzip = argv[i]; | 307 | const char* filenameinzip = argv[i]; |
231 | zip_fileinfo zi; | 308 | zip_fileinfo zi; |
309 | unsigned long crcFile=0; | ||
232 | 310 | ||
233 | zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = | 311 | zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = |
234 | zi.tmz_date.tm_mday = zi.tmz_date.tm_min = zi.tmz_date.tm_year = 0; | 312 | zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; |
235 | zi.dosDate = 0; | 313 | zi.dosDate = 0; |
236 | zi.internal_fa = 0; | 314 | zi.internal_fa = 0; |
237 | zi.external_fa = 0; | 315 | zi.external_fa = 0; |
238 | filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); | 316 | filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); |
239 | 317 | ||
240 | 318 | /* | |
241 | err = zipOpenNewFileInZip(zf,filenameinzip,&zi, | 319 | err = zipOpenNewFileInZip(zf,filenameinzip,&zi, |
242 | NULL,0,NULL,0,NULL /* comment*/, | 320 | NULL,0,NULL,0,NULL / * comment * /, |
243 | (opt_compress_level != 0) ? Z_DEFLATED : 0, | 321 | (opt_compress_level != 0) ? Z_DEFLATED : 0, |
244 | opt_compress_level); | 322 | opt_compress_level); |
323 | */ | ||
324 | if ((password != NULL) && (err==ZIP_OK)) | ||
325 | err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); | ||
326 | |||
327 | err = zipOpenNewFileInZip3(zf,filenameinzip,&zi, | ||
328 | NULL,0,NULL,0,NULL /* comment*/, | ||
329 | (opt_compress_level != 0) ? Z_DEFLATED : 0, | ||
330 | opt_compress_level,0, | ||
331 | /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ | ||
332 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, | ||
333 | password,crcFile); | ||
245 | 334 | ||
246 | if (err != ZIP_OK) | 335 | if (err != ZIP_OK) |
247 | printf("error in opening %s in zipfile\n",filenameinzip); | 336 | printf("error in opening %s in zipfile\n",filenameinzip); |
@@ -259,7 +348,7 @@ int main(argc,argv) | |||
259 | do | 348 | do |
260 | { | 349 | { |
261 | err = ZIP_OK; | 350 | err = ZIP_OK; |
262 | size_read = fread(buf,1,size_buf,fin); | 351 | size_read = (int)fread(buf,1,size_buf,fin); |
263 | if (size_read < size_buf) | 352 | if (size_read < size_buf) |
264 | if (feof(fin)==0) | 353 | if (feof(fin)==0) |
265 | { | 354 | { |
@@ -275,15 +364,17 @@ int main(argc,argv) | |||
275 | printf("error in writing %s in the zipfile\n", | 364 | printf("error in writing %s in the zipfile\n", |
276 | filenameinzip); | 365 | filenameinzip); |
277 | } | 366 | } |
278 | 367 | ||
279 | } | 368 | } |
280 | } while ((err == ZIP_OK) && (size_read>0)); | 369 | } while ((err == ZIP_OK) && (size_read>0)); |
281 | 370 | ||
282 | fclose(fin); | 371 | if (fin) |
372 | fclose(fin); | ||
373 | |||
283 | if (err<0) | 374 | if (err<0) |
284 | err=ZIP_ERRNO; | 375 | err=ZIP_ERRNO; |
285 | else | 376 | else |
286 | { | 377 | { |
287 | err = zipCloseFileInZip(zf); | 378 | err = zipCloseFileInZip(zf); |
288 | if (err!=ZIP_OK) | 379 | if (err!=ZIP_OK) |
289 | printf("error in closing %s in the zipfile\n", | 380 | printf("error in closing %s in the zipfile\n", |
@@ -297,6 +388,5 @@ int main(argc,argv) | |||
297 | } | 388 | } |
298 | 389 | ||
299 | free(buf); | 390 | free(buf); |
300 | exit(0); | 391 | return 0; |
301 | return 0; /* to avoid warning */ | ||
302 | } | 392 | } |