17 |
* along with PTD; if not, write to the Free Software Foundation, |
* along with PTD; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
*/ |
20 |
|
#include <windows.h> |
21 |
#include <assert.h> |
#include <assert.h> |
22 |
#include <stdlib.h> |
#include <stdlib.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
37 |
}; |
}; |
38 |
|
|
39 |
enum { |
enum { |
40 |
PK_FLAG_ENCRYPTED = 1, |
PK_FLAG_ENCRYPTED = 1, /* archive is encrypted. */ |
41 |
PK_FLAG_8KDICT = 2, |
PK_FLAG_8KDICT = 2, /* used 8k dictionary */ |
42 |
PK_FLAG_SF = 4 |
PK_FLAG_SF = 4 |
43 |
}; |
}; |
44 |
|
|
55 |
unsigned long u_size; |
unsigned long u_size; |
56 |
unsigned short f_len; |
unsigned short f_len; |
57 |
unsigned short extra_len; |
unsigned short extra_len; |
58 |
char * name; |
char *name; |
59 |
FILE * fp; |
FILE *fp; |
60 |
} PK_local_hdr; |
} PK_local_hdr; |
61 |
|
|
62 |
typedef struct { |
typedef struct { |
77 |
unsigned short int_attr; |
unsigned short int_attr; |
78 |
unsigned long ext_attr; |
unsigned long ext_attr; |
79 |
unsigned long lochdr_off; |
unsigned long lochdr_off; |
80 |
char * name; |
char *name; |
81 |
size_t size; |
size_t size; |
82 |
} PK_file_hdr; |
} PK_file_hdr; |
83 |
|
|
84 |
|
|
85 |
typedef struct { |
typedef struct { |
86 |
unsigned long magic; |
unsigned long magic; |
87 |
unsigned short disc_nr; |
unsigned short disc_nr; |
91 |
unsigned long size_of_dir; |
unsigned long size_of_dir; |
92 |
unsigned long off_dir; |
unsigned long off_dir; |
93 |
unsigned short comment_len; |
unsigned short comment_len; |
94 |
char * comment; |
char *comment; |
95 |
} PK_end_hdr; |
} PK_end_hdr; |
96 |
|
|
97 |
|
|
98 |
typedef struct { |
typedef struct { |
99 |
union { |
union { |
100 |
PK_local_hdr * local; |
PK_local_hdr *local; |
101 |
PK_file_hdr * file; |
PK_file_hdr *file; |
102 |
PK_end_hdr * end; |
PK_end_hdr *end; |
103 |
} u; |
} u; |
104 |
unsigned long pkttype; |
unsigned long pkttype; |
105 |
} PK_packet; |
} PK_packet; |
106 |
|
|
107 |
|
|
108 |
struct PK_file_list { |
struct PK_file_list { |
109 |
struct PK_file_list * next; |
struct PK_file_list *next; |
110 |
PK_local_hdr * hdr; |
PK_local_hdr *hdr; |
111 |
size_t off; |
size_t off; |
112 |
size_t len; |
size_t len; |
113 |
char d[1]; |
char *d; |
114 |
}; |
}; |
115 |
|
|
116 |
|
|
170 |
}; |
}; |
171 |
|
|
172 |
|
|
173 |
|
/* Update the crc in @crc with the buffer @buf and the length @buflen. */ |
174 |
static unsigned long |
static unsigned long |
175 |
update_crc( unsigned long crc, unsigned char * buf, size_t buflen ) |
update_crc (unsigned long crc, unsigned char *buf, size_t buflen) |
176 |
{ |
{ |
177 |
crc ^= 0xffffffffL; |
crc ^= 0xffffffffL; |
178 |
while( buflen-- ) |
while (buflen--) |
179 |
crc = crc_table[((int)(crc) ^ (*buf++)) & 0xff] ^ ((crc) >> 8); |
crc = crc_table[((int)(crc) ^ (*buf++)) & 0xff] ^ ((crc) >> 8); |
180 |
crc ^= 0xffffffffL; |
crc ^= 0xffffffffL; |
181 |
return crc; |
return crc; |
183 |
|
|
184 |
|
|
185 |
static unsigned short |
static unsigned short |
186 |
read_16( FILE * in ) |
read_16 (FILE *in) |
187 |
{ |
{ |
188 |
unsigned short u; |
unsigned short u; |
189 |
u = fgetc( in ) ; |
u = fgetc (in) ; |
190 |
u |= fgetc( in ) << 8; |
u |= fgetc (in) << 8; |
191 |
return u; |
return u; |
192 |
} |
} |
193 |
|
|
194 |
|
|
195 |
static void |
static void |
196 |
write_16( FILE * out, unsigned short a ) |
write_16 (FILE *out, unsigned short a) |
197 |
{ |
{ |
198 |
fputc( a , out ); |
fputc (a , out); |
199 |
fputc( a >> 8, out ); |
fputc (a >> 8, out); |
200 |
} |
} |
201 |
|
|
202 |
|
|
203 |
static unsigned long |
static unsigned long |
204 |
read_32( FILE * in ) |
read_32 (FILE *in) |
205 |
{ |
{ |
206 |
unsigned long u; |
unsigned long u; |
207 |
u = fgetc( in ) ; |
u = fgetc (in) ; |
208 |
u |= fgetc( in ) << 8; |
u |= fgetc (in) << 8; |
209 |
u |= fgetc( in ) << 16; |
u |= fgetc (in) << 16; |
210 |
u |= fgetc( in ) << 24; |
u |= fgetc (in) << 24; |
211 |
return u; |
return u; |
212 |
} |
} |
213 |
|
|
214 |
|
|
215 |
static void |
static void |
216 |
write_32( FILE * out, unsigned long a ) |
write_32 (FILE * out, unsigned long a) |
217 |
{ |
{ |
218 |
fputc( a , out ); |
fputc (a , out); |
219 |
fputc( a >> 8 , out ); |
fputc (a >> 8 , out); |
220 |
fputc( a >> 16, out ); |
fputc (a >> 16, out); |
221 |
fputc( a >> 24, out ); |
fputc (a >> 24, out); |
222 |
} |
} |
223 |
|
|
224 |
|
|
|
#if 0 /* not used */ |
|
|
static void |
|
|
date_from_dos( unsigned short t, int *day, int *mon, int *year ) |
|
|
{ |
|
|
if( mon ) |
|
|
*mon = t % 32; |
|
|
if( day ) |
|
|
*day = (t>>5) % 16; |
|
|
if( year ) |
|
|
*year = (t>>9) % 64; |
|
|
} |
|
|
#endif |
|
|
|
|
225 |
static unsigned short |
static unsigned short |
226 |
date_to_dos( time_t t ) |
date_to_dos (time_t t) |
227 |
{ |
{ |
228 |
struct tm * tm; |
struct tm * tm; |
229 |
tm = localtime( &t ); |
tm = localtime (&t); |
230 |
return tm->tm_mday | (tm->tm_mon+1)<<5 | ((tm->tm_year-80)<<9); |
return tm->tm_mday | (tm->tm_mon+1)<<5 | ((tm->tm_year-80)<<9); |
231 |
} |
} |
232 |
|
|
233 |
|
|
234 |
static unsigned short |
static unsigned short |
235 |
time_to_dos( time_t t ) |
time_to_dos (time_t t) |
236 |
{ |
{ |
237 |
struct tm * tm; |
struct tm * tm; |
238 |
tm = localtime( &t ); |
tm = localtime (&t); |
239 |
return (tm->tm_hour<<11) | (tm->tm_min<<5) | (tm->tm_sec>>1); |
return (tm->tm_hour<<11) | (tm->tm_min<<5) | (tm->tm_sec>>1); |
240 |
} |
} |
241 |
|
|
242 |
|
|
243 |
static char * |
static char* |
244 |
m_strdup (const char * s) |
xstrdup (const char * s) |
245 |
{ |
{ |
246 |
char * p = new char[strlen (s)+1]; |
char *p = new char[strlen (s)+1]; |
247 |
if (p) |
if (!p) |
248 |
strcpy (p, s); |
abort (); |
249 |
|
strcpy (p, s); |
250 |
return p; |
return p; |
251 |
} |
} |
252 |
|
|
253 |
|
|
254 |
static PK_local_hdr * |
static PK_local_hdr* |
255 |
create_local_header( const char * name, FILE * fp ) |
create_local_header (const char *name, FILE *fp) |
256 |
{ |
{ |
257 |
struct stat statbuf; |
struct stat statbuf; |
258 |
PK_local_hdr * a; |
PK_local_hdr *a; |
259 |
|
|
260 |
if( fstat( fileno( fp ), &statbuf ) ) |
if (fstat (fileno (fp), &statbuf)) |
261 |
return NULL; |
return NULL; |
262 |
a = new PK_local_hdr; |
a = new PK_local_hdr; |
263 |
if( !a ) |
if (!a) |
264 |
return NULL; |
return NULL; |
265 |
memset (a, 0, sizeof *a); |
memset (a, 0, sizeof *a); |
266 |
a->magic = PK_LOCAL_SIG; |
a->magic = PK_LOCAL_SIG; |
267 |
a->ver = 0 | (10 << 8); |
a->ver = 0 | (10 << 8); |
268 |
a->flags = 0; |
a->flags = 0; |
269 |
a->method = 0; |
a->method = 0; |
270 |
a->fdate = date_to_dos( statbuf.st_mtime ); |
a->fdate = date_to_dos (statbuf.st_mtime); |
271 |
a->ftime = time_to_dos( statbuf.st_mtime ); |
a->ftime = time_to_dos (statbuf.st_mtime); |
272 |
a->crc = 0; |
a->crc = 0; |
273 |
a->c_size = statbuf.st_size; |
a->c_size = statbuf.st_size; |
274 |
a->u_size = statbuf.st_size; |
a->u_size = statbuf.st_size; |
275 |
a->f_len = strlen( name ); |
a->f_len = strlen (name); |
276 |
a->name = m_strdup( name ); |
a->name = xstrdup (name); |
277 |
a->extra_len = 0; |
a->extra_len = 0; |
278 |
a->fp = fp; |
a->fp = fp; |
279 |
return a; |
return a; |
281 |
|
|
282 |
|
|
283 |
static int |
static int |
284 |
write_local_header( FILE * out, PK_local_hdr * hdr ) |
write_local_header (FILE *out, PK_local_hdr *hdr) |
285 |
{ |
{ |
286 |
long crc_pos = 0, curr_pos = 0; |
long crc_pos = 0, curr_pos = 0; |
287 |
|
|
288 |
write_32( out, hdr->magic ); |
write_32 (out, hdr->magic); |
289 |
write_16( out, hdr->ver ); |
write_16 (out, hdr->ver); |
290 |
write_16( out, hdr->flags ); |
write_16 (out, hdr->flags); |
291 |
write_16( out, hdr->method ); |
write_16 (out, hdr->method); |
292 |
write_16( out, hdr->ftime ); |
write_16 (out, hdr->ftime); |
293 |
write_16( out, hdr->fdate ); |
write_16 (out, hdr->fdate); |
294 |
crc_pos = ftell( out ); |
crc_pos = ftell (out); |
295 |
write_32( out, hdr->crc ); |
write_32 (out, hdr->crc); |
296 |
write_32( out, hdr->c_size ); |
write_32 (out, hdr->c_size); |
297 |
write_32( out, hdr->u_size ); |
write_32 (out, hdr->u_size); |
298 |
write_16( out, hdr->f_len ); |
write_16 (out, hdr->f_len); |
299 |
write_16( out, hdr->extra_len ); |
write_16 (out, hdr->extra_len); |
300 |
if( hdr->f_len ) |
if (hdr->f_len > 0) |
301 |
fwrite( hdr->name, 1, hdr->f_len, out ); |
fwrite (hdr->name, 1, hdr->f_len, out); |
302 |
if( hdr->extra_len ) |
if (hdr->extra_len) |
303 |
fwrite( NULL, 1, hdr->extra_len, out ); /* xxx */ |
fwrite (NULL, 1, hdr->extra_len, out); /* xxx */ |
304 |
while (!feof (hdr->fp)) |
while (!feof (hdr->fp)) { |
305 |
{ |
unsigned char buf[2048]; |
|
unsigned char buf[1024]; |
|
306 |
unsigned long nread; |
unsigned long nread; |
307 |
nread = fread( buf, 1, sizeof buf-1, hdr->fp ); |
nread = fread (buf, 1, sizeof buf-1, hdr->fp); |
308 |
if( nread ) { |
if (nread > 0) { |
309 |
/*fprintf( stderr, "** nread=%d\n", nread );*/ |
/*fprintf (stderr, "** nread=%d\n", nread);*/ |
310 |
hdr->crc = update_crc( hdr->crc, buf, nread ); |
hdr->crc = update_crc (hdr->crc, buf, nread); |
311 |
fwrite( buf, 1, nread, out ); |
fwrite (buf, 1, nread, out); |
312 |
} |
} |
313 |
} |
} |
314 |
curr_pos = ftell( out ); |
curr_pos = ftell (out); |
315 |
fseek( out, crc_pos, SEEK_SET ); |
fseek (out, crc_pos, SEEK_SET); |
316 |
write_32( out, hdr->crc ); |
write_32 (out, hdr->crc); |
317 |
fseek( out, curr_pos, SEEK_SET ); |
fseek (out, curr_pos, SEEK_SET); |
318 |
return 0; |
return 0; |
319 |
} |
} |
320 |
|
|
321 |
|
|
322 |
static int |
static int |
323 |
create_missing_dirs( const char * name ) |
create_missing_dirs (const char *name) |
324 |
{ |
{ |
325 |
struct stat dirbuf; |
struct stat dirbuf; |
326 |
char *p, *dir; |
char *p, *dir; |
327 |
int rc; |
int rc; |
328 |
|
|
329 |
p = strrchr( name, '/' ); |
p = strrchr (name, '/'); |
330 |
if( !p ) |
if (!p) |
331 |
return PKERR_GENERAL; |
return PKERR_GENERAL; |
332 |
dir = new char[(p-name)+1]; |
dir = new char[(p-name)+1]; |
333 |
if( !dir ) |
if (!dir) |
334 |
return -1; |
abort (); |
335 |
memset (dir, 0, (p-name)+1); |
memset (dir, 0, (p-name)+1); |
336 |
strncpy( dir, name, (p-name) ); |
strncpy (dir, name, (p-name)); |
337 |
if( !stat( dir, &dirbuf ) && ( dirbuf.st_mode & _S_IFDIR ) ) { |
if (!stat (dir, &dirbuf) && (dirbuf.st_mode & _S_IFDIR)) |
338 |
delete []dir; |
rc = 0; |
339 |
return 0; |
else |
340 |
} |
rc = mkdir (dir); |
|
rc = mkdir( dir ); |
|
341 |
delete []dir; |
delete []dir; |
342 |
return rc; |
return rc; |
343 |
} |
} |
344 |
|
|
345 |
|
|
346 |
static PK_local_hdr * |
static PK_local_hdr* |
347 |
read_local_header( FILE * in, unsigned long magic, int create ) |
read_local_header (FILE *in, unsigned long magic, int create) |
348 |
{ |
{ |
349 |
PK_local_hdr * hdr; |
PK_local_hdr *hdr; |
350 |
FILE * out = NULL; |
FILE *out = NULL; |
351 |
size_t n; |
size_t n; |
352 |
|
|
353 |
if( magic != PK_LOCAL_SIG ) |
if (magic != PK_LOCAL_SIG) |
354 |
return NULL; |
return NULL; |
355 |
hdr = new PK_local_hdr; |
hdr = new PK_local_hdr; |
356 |
if( !hdr ) |
if (!hdr) |
357 |
return NULL; |
return NULL; |
358 |
memset (hdr, 0, sizeof * hdr); |
memset (hdr, 0, sizeof * hdr); |
359 |
hdr->magic = magic; |
hdr->magic = magic; |
360 |
hdr->ver = read_16( in ); |
hdr->ver = read_16 (in); |
361 |
hdr->flags = read_16( in ); |
hdr->flags = read_16 (in); |
362 |
hdr->method = read_16( in ); |
hdr->method = read_16 (in); |
363 |
hdr->ftime = read_16( in ); |
hdr->ftime = read_16 (in); |
364 |
hdr->fdate = read_16( in ); |
hdr->fdate = read_16 (in); |
365 |
hdr->crc = read_32( in ); |
hdr->crc = read_32 (in); |
366 |
hdr->c_size = read_32( in ); |
hdr->c_size = read_32 (in); |
367 |
hdr->u_size = read_32( in ); |
hdr->u_size = read_32 (in); |
368 |
hdr->f_len = read_16( in ); |
hdr->f_len = read_16 (in); |
369 |
hdr->extra_len = read_16( in ); |
hdr->extra_len = read_16 (in); |
370 |
|
|
371 |
/*if( debug ) { |
/*if( debug ) { |
372 |
printf("=====BEGIN LOCAL HEADER=====\n"); |
printf("=====BEGIN LOCAL HEADER=====\n"); |
386 |
printf( "filename len=%d extra_len=%d\n", hdr->f_len, hdr->extra_len ); |
printf( "filename len=%d extra_len=%d\n", hdr->f_len, hdr->extra_len ); |
387 |
}*/ |
}*/ |
388 |
|
|
389 |
if( hdr->f_len ) { |
if (hdr->f_len) { |
390 |
hdr->name = new char[hdr->f_len+1]; |
hdr->name = new char[hdr->f_len+1]; |
391 |
if( !hdr->name ) |
if (!hdr->name) |
392 |
return NULL; |
abort (); |
393 |
memset (hdr->name, 0, hdr->f_len+1); |
memset (hdr->name, 0, hdr->f_len+1); |
394 |
fread( hdr->name, 1, hdr->f_len, in ); |
fread (hdr->name, 1, hdr->f_len, in); |
395 |
/*if( debug ) |
/*if (debug) |
396 |
printf( "filename %s\n", hdr->name );*/ |
printf ("filename %s\n", hdr->name);*/ |
397 |
} |
} |
398 |
/*if( debug ) |
/*if (debug) |
399 |
printf( "skip extra header (%d)\n", hdr->extra_len );*/ |
printf ("skip extra header (%d)\n", hdr->extra_len);*/ |
400 |
n = hdr->extra_len; |
n = hdr->extra_len; |
401 |
while( n-- ) |
while (n--) |
402 |
fgetc( in ); |
fgetc (in); |
403 |
/*if( debug ) |
/*if (debug) |
404 |
printf( "skip compressed data (%lu)\n", hdr->c_size );*/ |
printf ("skip compressed data (%lu)\n", hdr->c_size);*/ |
405 |
if( create && hdr->name ) { |
if (create && hdr->name) { |
406 |
create_missing_dirs( hdr->name ); |
create_missing_dirs (hdr->name); |
407 |
out = fopen( hdr->name, "wb" ); |
out = fopen (hdr->name, "wb"); |
408 |
/*if( debug ) |
/*if (debug) |
409 |
printf( "create output `%s' (status %s)\n", hdr->name, |
printf( "create output `%s' (status %s)\n", hdr->name, |
410 |
out? "success" : "failed" );*/ |
out? "success" : "failed" );*/ |
411 |
} |
} |
412 |
n = hdr->c_size; |
n = hdr->c_size; |
413 |
while( n-- ) { |
while (n--) { |
414 |
int c = fgetc( in ); |
int c = fgetc (in); |
415 |
if( out ) |
if (out) |
416 |
fputc( c, out ); |
fputc (c, out); |
417 |
} |
} |
418 |
if( out ) |
if (out) |
419 |
fclose( out ); |
fclose (out); |
420 |
return hdr; |
return hdr; |
421 |
} |
} |
422 |
|
|
423 |
|
|
424 |
static PK_file_hdr * |
static PK_file_hdr* |
425 |
create_file_header( PK_local_hdr * loc, long loc_off ) |
create_file_header (PK_local_hdr * loc, long loc_off) |
426 |
{ |
{ |
427 |
PK_file_hdr * a; |
PK_file_hdr * a; |
428 |
|
|
429 |
a = new PK_file_hdr; |
a = new PK_file_hdr; |
430 |
if( !a ) |
if (!a) |
431 |
return NULL; |
abort (); |
432 |
memset (a, 0, sizeof *a); |
memset (a, 0, sizeof *a); |
433 |
a->magic = PK_FILE_SIG; |
a->magic = PK_FILE_SIG; |
434 |
a->ver_made = loc->ver; |
a->ver_made = loc->ver; |
448 |
a->ext_attr = 0; |
a->ext_attr = 0; |
449 |
a->lochdr_off = loc_off; |
a->lochdr_off = loc_off; |
450 |
a->size = 46 + a->f_len; |
a->size = 46 + a->f_len; |
451 |
a->name = m_strdup( loc->name ); |
a->name = xstrdup (loc->name); |
452 |
return a; |
return a; |
453 |
} |
} |
454 |
|
|
455 |
|
|
456 |
static int |
static int |
457 |
write_file_header( FILE * out, PK_file_hdr * hdr ) |
write_file_header (FILE *out, PK_file_hdr *hdr) |
458 |
{ |
{ |
459 |
write_32( out, hdr->magic ); |
write_32 (out, hdr->magic); |
460 |
write_16( out, hdr->ver_made ); |
write_16 (out, hdr->ver_made); |
461 |
write_16( out, hdr->ver_ext ); |
write_16 (out, hdr->ver_ext); |
462 |
write_16( out, hdr->flags ); |
write_16 (out, hdr->flags); |
463 |
write_16( out, hdr->method ); |
write_16 (out, hdr->method); |
464 |
write_16( out, hdr->ftime ); |
write_16 (out, hdr->ftime); |
465 |
write_16( out, hdr->fdate ); |
write_16 (out, hdr->fdate); |
466 |
write_32( out, hdr->crc ); |
write_32 (out, hdr->crc); |
467 |
write_32( out, hdr->c_size ); |
write_32 (out, hdr->c_size); |
468 |
write_32( out, hdr->u_size ); |
write_32 (out, hdr->u_size); |
469 |
write_16( out, hdr->f_len ); |
write_16 (out, hdr->f_len); |
470 |
write_16( out, hdr->extra_len ); |
write_16 (out, hdr->extra_len); |
471 |
write_16( out, hdr->comment_len ); |
write_16 (out, hdr->comment_len); |
472 |
write_16( out, hdr->disc_nr ); |
write_16 (out, hdr->disc_nr); |
473 |
write_16( out, hdr->int_attr ); |
write_16 (out, hdr->int_attr); |
474 |
write_32( out, hdr->ext_attr ); |
write_32 (out, hdr->ext_attr); |
475 |
write_32( out, hdr->lochdr_off ); |
write_32 (out, hdr->lochdr_off); |
476 |
if( hdr->f_len ) |
if (hdr->f_len > 0) |
477 |
fwrite( hdr->name, 1, hdr->f_len, out ); |
fwrite (hdr->name, 1, hdr->f_len, out); |
478 |
if( hdr->extra_len ) |
if (hdr->extra_len > 0) |
479 |
fwrite( NULL, 1, hdr->extra_len, out ); /* xxx */ |
fwrite (NULL, 1, hdr->extra_len, out); /* xxx */ |
480 |
if( hdr->comment_len ) |
if (hdr->comment_len > 0) |
481 |
fwrite( NULL, 1, hdr->comment_len, out ); /* xxx */ |
fwrite (NULL, 1, hdr->comment_len, out); /* xxx */ |
482 |
return 0; |
return 0; |
483 |
} |
} |
484 |
|
|
485 |
|
|
486 |
static PK_file_hdr * |
static PK_file_hdr* |
487 |
read_file_header( FILE * in, unsigned long magic ) |
read_file_header (FILE * in, unsigned long magic) |
488 |
{ |
{ |
489 |
PK_file_hdr * hdr; |
PK_file_hdr * hdr; |
490 |
size_t n; |
size_t n; |
491 |
|
|
492 |
if( magic != PK_FILE_SIG ) |
if (magic != PK_FILE_SIG) |
493 |
return NULL; |
return NULL; |
494 |
hdr = new PK_file_hdr; |
hdr = new PK_file_hdr; |
495 |
if( !hdr ) |
if (!hdr) |
496 |
return NULL; |
return NULL; |
497 |
memset (hdr, 0, sizeof * hdr); |
memset (hdr, 0, sizeof * hdr); |
498 |
hdr->magic = magic; |
hdr->magic = magic; |
536 |
printf( "disk offset=%lu\n", hdr->lochdr_off ); |
printf( "disk offset=%lu\n", hdr->lochdr_off ); |
537 |
}*/ |
}*/ |
538 |
|
|
539 |
if( hdr->f_len ) { |
if (hdr->f_len > 0) { |
540 |
hdr->name = new char[hdr->f_len + 1]; |
hdr->name = new char[hdr->f_len + 1]; |
541 |
if( !hdr->name ) |
if (!hdr->name) |
542 |
return NULL; |
abort (); |
543 |
memset (hdr->name, 0, hdr->f_len+1); |
memset (hdr->name, 0, hdr->f_len+1); |
544 |
fread( hdr->name, 1, hdr->f_len, in ); |
fread (hdr->name, 1, hdr->f_len, in); |
545 |
/*if( debug ) |
/*if( debug ) |
546 |
printf( "filename=%s\n", hdr->name );*/ |
printf( "filename=%s\n", hdr->name );*/ |
547 |
} |
} |
548 |
n = hdr->extra_len; |
n = hdr->extra_len; |
549 |
while( n-- ) |
while (n--) |
550 |
fgetc( in ); |
fgetc (in); |
551 |
n = hdr->comment_len; |
n = hdr->comment_len; |
552 |
while( n-- ) |
while (n--) |
553 |
fgetc( in ); |
fgetc (in); |
554 |
return hdr; |
return hdr; |
555 |
} |
} |
556 |
|
|
557 |
|
|
558 |
static PK_end_hdr * |
static PK_end_hdr* |
559 |
create_end_header( size_t ntotal, size_t nsize, long off_dir ) |
create_end_header (size_t ntotal, size_t nsize, long off_dir) |
560 |
{ |
{ |
561 |
PK_end_hdr * a; |
PK_end_hdr *a; |
562 |
|
|
563 |
a = new PK_end_hdr; |
a = new PK_end_hdr; |
564 |
if( !a ) |
if (!a) |
565 |
return NULL; |
abort (); |
566 |
memset (a, 0, sizeof * a); |
memset (a, 0, sizeof * a); |
567 |
a->magic = PK_END_SIG; |
a->magic = PK_END_SIG; |
568 |
a->disc_nr = 0; |
a->disc_nr = 0; |
577 |
|
|
578 |
|
|
579 |
static int |
static int |
580 |
write_end_header( FILE * out, PK_end_hdr * hdr ) |
write_end_header (FILE *out, PK_end_hdr *hdr) |
581 |
{ |
{ |
582 |
write_32( out, hdr->magic ); |
write_32 (out, hdr->magic); |
583 |
write_16( out, hdr->disc_nr ); |
write_16 (out, hdr->disc_nr); |
584 |
write_16( out, hdr->disc_nr_cd ); |
write_16 (out, hdr->disc_nr_cd); |
585 |
write_16( out, hdr->total_dirs ); |
write_16 (out, hdr->total_dirs); |
586 |
write_16( out, hdr->total_dirs_disc ); |
write_16 (out, hdr->total_dirs_disc); |
587 |
write_32( out, hdr->size_of_dir ); |
write_32 (out, hdr->size_of_dir); |
588 |
write_32( out, hdr->off_dir ); |
write_32 (out, hdr->off_dir); |
589 |
write_16( out, hdr->comment_len ); |
write_16 (out, hdr->comment_len); |
590 |
if( hdr->comment_len ) |
if (hdr->comment_len > 0) |
591 |
fwrite( hdr->comment, 1, hdr->comment_len, out ); |
fwrite (hdr->comment, 1, hdr->comment_len, out); |
592 |
return 0; |
return 0; |
593 |
} |
} |
594 |
|
|
595 |
|
|
596 |
static PK_end_hdr * |
static PK_end_hdr* |
597 |
read_end_header( FILE * in, unsigned long magic ) |
read_end_header (FILE * in, unsigned long magic) |
598 |
{ |
{ |
599 |
PK_end_hdr * hdr; |
PK_end_hdr * hdr; |
600 |
|
|
602 |
return NULL; |
return NULL; |
603 |
hdr = new PK_end_hdr; |
hdr = new PK_end_hdr; |
604 |
if( !hdr ) |
if( !hdr ) |
605 |
return NULL; |
abort (); |
606 |
memset (hdr, 0, sizeof *hdr); |
memset (hdr, 0, sizeof *hdr); |
607 |
hdr->magic = magic; |
hdr->magic = magic; |
608 |
hdr->disc_nr = read_16( in ); |
hdr->disc_nr = read_16( in ); |
624 |
printf( "size of directory=%lu\n", hdr->size_of_dir ); |
printf( "size of directory=%lu\n", hdr->size_of_dir ); |
625 |
}*/ |
}*/ |
626 |
|
|
627 |
if( hdr->comment_len ) { |
if (hdr->comment_len > 0) { |
628 |
hdr->comment = new char[hdr->comment_len+1]; |
hdr->comment = new char[hdr->comment_len+1]; |
629 |
if( !hdr->comment ) |
if( !hdr->comment ) |
630 |
return NULL; |
abort (); |
631 |
memset (hdr->comment, 0, hdr->comment_len+1); |
memset (hdr->comment, 0, hdr->comment_len+1); |
632 |
fread( hdr->comment, 1, hdr->comment_len, in ); |
fread( hdr->comment, 1, hdr->comment_len, in ); |
633 |
/*if( debug ) |
/*if( debug ) |
638 |
|
|
639 |
|
|
640 |
static void |
static void |
641 |
free_packet( PK_packet * pkt ) |
free_packet (PK_packet *pkt) |
642 |
{ |
{ |
643 |
switch( pkt->pkttype ) { |
switch( pkt->pkttype ) { |
644 |
case PK_LOCAL_SIG: |
case PK_LOCAL_SIG: |
645 |
if( pkt->u.local->name ) |
if (pkt->u.local->name) |
646 |
delete []pkt->u.local->name; |
delete []pkt->u.local->name; |
647 |
delete pkt->u.local; |
delete pkt->u.local; |
648 |
break; |
break; |
649 |
case PK_FILE_SIG: |
case PK_FILE_SIG: |
650 |
if( pkt->u.file->name ) |
if (pkt->u.file->name) |
651 |
delete[] pkt->u.file->name; |
delete[] pkt->u.file->name; |
652 |
delete pkt->u.file; |
delete pkt->u.file; |
653 |
break; |
break; |
654 |
case PK_END_SIG: |
case PK_END_SIG: |
655 |
if( pkt->u.end->comment ) |
if (pkt->u.end->comment) |
656 |
delete [] pkt->u.end->comment; |
delete [] pkt->u.end->comment; |
657 |
delete pkt->u.end; |
delete pkt->u.end; |
658 |
break; |
break; |
662 |
|
|
663 |
|
|
664 |
int |
int |
665 |
pk_archiv_parse( FILE * in ) |
pk_archiv_parse (FILE *in) |
666 |
{ |
{ |
667 |
unsigned long magic; |
unsigned long magic; |
668 |
PK_packet pkt; |
PK_packet pkt; |
703 |
|
|
704 |
|
|
705 |
static int |
static int |
706 |
is_directory( const char * fname ) |
is_directory (const char * fname) |
707 |
{ |
{ |
708 |
struct stat statbuf; |
struct stat statbuf; |
709 |
if( stat( fname, &statbuf ) ) |
if (stat (fname, &statbuf)) |
710 |
return 0; |
return 0; |
711 |
return statbuf.st_mode & S_IFDIR; |
return statbuf.st_mode & S_IFDIR; |
712 |
} |
} |
713 |
|
|
714 |
|
|
715 |
int |
int |
716 |
pk_archiv_create( struct PK_file_list * list, const char * output ) |
pk_archiv_create (struct PK_file_list *list, const char *output) |
717 |
{ |
{ |
718 |
struct PK_file_list * r; |
struct PK_file_list *r; |
719 |
PK_file_hdr * f; |
PK_file_hdr *f; |
720 |
PK_end_hdr * e; |
PK_end_hdr *e; |
721 |
PK_packet pkt; |
PK_packet pkt; |
722 |
FILE * fp, * out; |
FILE *fp, *out; |
723 |
long foff = 0, n = 0; |
long foff = 0, n = 0; |
724 |
size_t size = 0; |
size_t size = 0; |
725 |
|
|
726 |
out = fopen( output, "wb" ); |
out = fopen (output, "wb"); |
727 |
if( !out ) |
if (!out) |
728 |
return PKERR_FILE; |
return PKERR_FILE; |
729 |
|
|
730 |
for( r = list; r; r = r->next ) |
for (r = list; r; r = r->next) { |
731 |
{ |
fprintf (stderr, "process file `%s'\n", r->d); |
732 |
/*fprintf( stderr, "process file `%s'\n", r->d );*/ |
if (is_directory (r->d)) |
|
if( is_directory( r->d ) ) |
|
733 |
continue; |
continue; |
734 |
fp = fopen( r->d, "rb" ); |
fp = fopen (r->d, "rb"); |
735 |
if( !fp ) |
if (!fp) { |
|
{ |
|
736 |
fclose (out); |
fclose (out); |
737 |
return PKERR_FILE; |
return PKERR_FILE; |
738 |
} |
} |
739 |
r->off = ftell( out ); |
r->off = ftell (out); |
740 |
r->hdr = create_local_header( r->d, fp ); |
r->hdr = create_local_header( r->d, fp ); |
741 |
if (!r->hdr) |
if (!r->hdr) { |
|
{ |
|
742 |
fclose (fp); |
fclose (fp); |
743 |
fclose (out); |
fclose (out); |
744 |
return PKERR_GENERAL; |
return PKERR_GENERAL; |
745 |
} |
} |
746 |
if( r->off ) |
if (r->off) |
747 |
r->off += r->hdr->c_size; |
r->off += r->hdr->c_size; |
748 |
write_local_header( out, r->hdr ); |
write_local_header (out, r->hdr); |
749 |
n++; |
n++; |
750 |
fclose( fp ); |
fclose (fp); |
751 |
} |
} |
752 |
foff = ftell( out ); |
foff = ftell( out ); |
753 |
for( r = list; r; r = r->next ) { |
for (r = list; r; r = r->next) { |
754 |
if( is_directory( r->d ) ) |
if (is_directory (r->d)) |
755 |
continue; |
continue; |
756 |
fp = fopen( r->d, "rb" ); |
fp = fopen (r->d, "rb"); |
757 |
if( !fp ) |
if (!fp) { |
|
{ |
|
758 |
fclose (out); |
fclose (out); |
759 |
return PKERR_FILE; |
return PKERR_FILE; |
760 |
} |
} |
761 |
f = create_file_header( r->hdr, r->off ); |
f = create_file_header (r->hdr, r->off); |
|
if (!f) |
|
|
{ |
|
|
fclose (out); |
|
|
fclose (fp); |
|
|
return PKERR_GENERAL; |
|
|
} |
|
762 |
size += f->size; |
size += f->size; |
763 |
write_file_header( out, f ); |
write_file_header (out, f); |
764 |
fclose (fp); |
fclose (fp); |
765 |
pkt.u.file = f; |
pkt.u.file = f; |
766 |
free_packet (&pkt); |
free_packet (&pkt); |
767 |
} |
} |
768 |
|
|
769 |
e = create_end_header (n, size, foff); |
e = create_end_header (n, size, foff); |
|
if (!e) |
|
|
{ |
|
|
fclose (out); |
|
|
return PKERR_GENERAL; |
|
|
} |
|
770 |
write_end_header (out, e); |
write_end_header (out, e); |
771 |
fclose (out); |
fclose (out); |
772 |
return 0; |
return 0; |
774 |
|
|
775 |
|
|
776 |
void |
void |
777 |
pk_list_add( struct PK_file_list **list, const char * name ) |
pk_list_add (struct PK_file_list **list, const char *name) |
778 |
{ |
{ |
779 |
struct PK_file_list * l; |
struct PK_file_list * l; |
780 |
|
|
781 |
l = new PK_file_list; |
l = new PK_file_list; |
782 |
if (l) |
if (!l) |
783 |
memset (l, 0, sizeof *l); |
abort (); |
784 |
if( l ) { |
memset (l, 0, sizeof *l); |
785 |
l->next = *list; |
l->next = *list; |
786 |
strcpy( l->d, name ); |
l->d = new char[strlen (name)+2]; |
787 |
*list = l; |
if (!l->d) |
788 |
} |
abort (); |
789 |
|
strcpy (l->d, name); |
790 |
|
*list = l; |
791 |
} |
} |
792 |
|
|
793 |
|
|
794 |
void |
void |
795 |
pk_list_free( struct PK_file_list * list ) |
pk_list_free (struct PK_file_list *list) |
796 |
{ |
{ |
797 |
struct PK_file_list * l; |
struct PK_file_list * l; |
798 |
PK_packet pkt; |
PK_packet pkt; |
799 |
|
|
800 |
while( list ) { |
while (list) { |
801 |
l = list->next; |
l = list->next; |
802 |
if( list->hdr ) { |
if (list->hdr) { |
803 |
pkt.pkttype = PK_LOCAL_SIG; |
pkt.pkttype = PK_LOCAL_SIG; |
804 |
pkt.u.local = list->hdr; |
pkt.u.local = list->hdr; |
805 |
free_packet( &pkt ); |
free_packet (&pkt); |
806 |
} |
} |
807 |
|
if (list->d) |
808 |
|
delete []list->d; |
809 |
delete list; |
delete list; |
810 |
list = l; |
list = l; |
811 |
} |
} |
812 |
} |
} |
813 |
|
|
814 |
|
|
815 |
|
#ifdef TEST |
816 |
|
|
817 |
|
static void |
818 |
|
test (const char *patt, const char *path, const char *out) |
819 |
|
{ |
820 |
|
PK_FILE_LIST list = NULL; |
821 |
|
WIN32_FIND_DATA findbuf; |
822 |
|
HANDLE hd; |
823 |
|
int rc = 0; |
824 |
|
|
825 |
|
hd = FindFirstFile (patt, &findbuf ); |
826 |
|
if( !hd ) |
827 |
|
return; |
828 |
|
|
829 |
|
if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) { |
830 |
|
char p[256]; |
831 |
|
_snprintf (p, sizeof (p)-1, "%s\\%s", path, findbuf.cFileName); |
832 |
|
pk_list_add (&list, p); |
833 |
|
} |
834 |
|
while( FindNextFile( hd, &findbuf ) ) { |
835 |
|
if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) { |
836 |
|
char p[256]; |
837 |
|
_snprintf (p, sizeof (p)-1, "%s\\%s", path, findbuf.cFileName); |
838 |
|
pk_list_add (&list, p); |
839 |
|
} |
840 |
|
} |
841 |
|
|
842 |
|
rc = pk_archiv_create (list, out); |
843 |
|
|
844 |
|
pk_list_free (list); |
845 |
|
FindClose (hd); |
846 |
|
} |
847 |
|
|
848 |
|
int |
849 |
|
main (int argc, char **argv) |
850 |
|
{ |
851 |
|
test ("c:\\gnupg\\*", "c:\\gnupg", "c:\\gnupg\\\\test\\sample.zip"); |
852 |
|
return 0; |
853 |
|
} |
854 |
|
#endif |