-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_io.cpp
More file actions
1802 lines (1486 loc) · 62.2 KB
/
Copy pathfile_io.cpp
File metadata and controls
1802 lines (1486 loc) · 62.2 KB
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------
file_io.c
This file contains routines for doing direct input/output, file-related
sorts of things. Most of the system-specific code for unzip is contained
here, including the non-echoing password code for decryption (bottom).
---------------------------------------------------------------------------*/
#include "headers.h"
#define FILE_IO_C
#include <ChUnzip.h>
#if defined( CH_ARCH_16 )
#include <stdlib.h>
#include <dos.h>
#include <direct.h>
#endif
#include "MemDebug.h"
/************************************/
/* File_IO Local Prototypes, etc. */
/************************************/
/******************************/
/* Function open_input_file() */
/******************************/
int ChUnzip::open_input_file() /* return non-zero if open failed */
{
/*
* open the zipfile for reading and in BINARY mode to prevent cr/lf
* translation, which would corrupt the bitstreams
*/
if ( !m_pFile )
{
// m_pFile = ::new fstream( zipfn, ios::in | ios::binary, filebuf::sh_read );
m_pFile = ::new std::fstream( zipfn, std::ios::in | std::ios::binary );
}
else
{
// m_pFile->open( zipfn, ios::in | ios::binary, filebuf::sh_read );
m_pFile->open( zipfn, std::ios::in | std::ios::binary );
}
if (m_pFile->is_open() )
{
return 0;
}
else
{
return 1;
}
}
/**********************/
/* Function readbuf() */
/**********************/
int ChUnzip::readbuf( char* buf, register unsigned size)
//char *buf;
//register unsigned size;
{ /* return number of bytes read into buf */
register int count;
int n;
n = size;
while (size) {
if (incnt == 0) {
//if ((incnt = read(zipfd, (char *)inbuf, INBUFSIZ)) <= 0)
incnt = m_pFile->read( (char *)inbuf, INBUFSIZ).gcount();
if (incnt <= 0)
return (n-size);
/* buffer ALWAYS starts on a block boundary: */
cur_zipfile_bufstart += INBUFSIZ;
inptr = inbuf;
}
count = MIN(size, (unsigned)incnt);
memcpy(buf, inptr, count);
buf += count;
inptr += count;
incnt -= count;
size -= count;
}
return (n);
}
/*******************************/
/* Function dos_to_unix_time() */ /* only used for freshening/updating */
/*******************************/
time_t ChUnzip::dos_to_unix_time( unsigned ddate, unsigned dtime)
// unsigned ddate, dtime;
{
int yr, mo, dy, hh, mm, ss;
# define YRBASE 1970
static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int leap;
long m_time, days=0;
//extern long timezone; /* declared in <time.h> for MSC (& Borland?) */
/* dissect date */
yr = ((ddate >> 9) & 0x7f) + (1980 - YRBASE);
mo = ((ddate >> 5) & 0x0f) - 1;
dy = (ddate & 0x1f) - 1;
/* dissect time */
hh = (dtime >> 11) & 0x1f;
mm = (dtime >> 5) & 0x3f;
ss = (dtime & 0x1f) * 2;
/* leap = # of leap years from BASE up to but not including current year */
leap = ((yr + YRBASE - 1) / 4); /* leap year base factor */
/* calculate days from BASE to this year and add expired days this year */
days = (yr * 365) + (leap - 492) + yday[mo];
/* if year is a leap year and month is after February, add another day */
if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
++days; /* OK through 2199 */
/* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
m_time = ((long)(days + dy) * 86400L) + ((long)hh * 3600) + (mm * 60) + ss;
/* - 1; MS-DOS times always rounded up to nearest even second */
TRACE( "dos_to_unix_time:\n");
TRACE1( " m_time before timezone = %ld\n", m_time );
#if defined(CH_ARCH_32) && defined( CH_MSW )
{
TIME_ZONE_INFORMATION tzinfo;
DWORD res;
/* account for timezone differences */
res = GetTimeZoneInformation(&tzinfo);
if (res == TIME_ZONE_ID_STANDARD)
m_time += 60*(tzinfo.Bias + tzinfo.StandardBias);
else if (res == TIME_ZONE_ID_DAYLIGHT)
m_time += 60*(tzinfo.Bias + tzinfo.DaylightBias);
/* GRR: are other return-values possible? */
}
#else /* !WIN32 */
tzset(); /* set `timezone' variable */
m_time += timezone;
#endif /* ?WIN32 */
TRACE1( " m_time after timezone = %ld\n", m_time);
#if !defined(CH_ARCH_32) && !defined( CH_MSW )
if (localtime((time_t *)&m_time)->tm_isdst)
m_time -= 60L * 60L; /* adjust for daylight savings time */
#endif /* !WIN32 */
TRACE1( " m_time after DST = %ld\n", m_time);
return m_time;
} /* end function dos_to_unix_time() */
/******************************/
/* Function check_for_newer() */ /* only used for freshening/updating */
/******************************/
int ChUnzip::check_for_newer(char * filename) /* return 1 if existing file newer or equal; */
// char *filename; /* 0 if older; -1 if doesn't exist yet */
{
time_t existing, archive;
struct stat statbuf;
if (stat(filename, &statbuf))
return DOES_NOT_EXIST;
/* round up existing filetime to nearest 2 seconds for comparison */
existing = (statbuf.st_mtime & 1) ? statbuf.st_mtime+1 : statbuf.st_mtime;
archive = dos_to_unix_time(lrec.last_mod_file_date,
lrec.last_mod_file_time);
TRACE3( "check_for_newer: existing %ld, archive %ld, e-a %ld\n",
existing, archive, existing-archive);
return (existing >= archive);
} /* end function check_for_newer() */
/*********************************/
/* Function create_output_file() */
/*********************************/
int ChUnzip::open_outfile() /* return non-0 if creat failed */
{
/*---------------------------------------------------------------------------
Create the output file with appropriate permissions. If we've gotten to
this point and the file still exists, we have permission to blow it away.
---------------------------------------------------------------------------*/
if ( !m_pOutFile )
{
m_pOutFile = ::new std::fstream;
}
if ( m_pOutFile->is_open() )
{
m_pOutFile->close();
}
//if (!aflag)
if (!(UNZIP_OPT_EBCIDIC_ASCII & m_flOptions ) )
{
// m_pOutFile->open( filename, ios::out | ios::binary, filebuf::sh_none );
m_pOutFile->open( filename, std::ios::out | std::ios::binary );
// 0 denotes exclusive-shared.
}
else
{
// m_pOutFile->open( filename, ios::out | ios::binary, filebuf::sh_none );
m_pOutFile->open( filename, std::ios::out | std::ios::binary );
// 0 denotes exclusive-shared.
}
if ( m_pOutFile->is_open() )
{
return 0;
}
else
{
return -1;
}
}
int ChUnzip::readbyte() /* refill inbuf and return a byte if available, else EOF */
{
if (mem_mode || (incnt = m_pFile->read((char *)inbuf,INBUFSIZ).gcount()) <= 0)
return EOF;
cur_zipfile_bufstart += INBUFSIZ; /* always starts on a block boundary */
inptr = inbuf;
#ifdef CRYPT
if (pInfo->encrypted) {
uch *p;
int n;
for (n = (long)incnt > csize + 1 ? (int)csize + 1 : incnt,
p = inptr; n--; p++)
zdecode(*p);
}
#endif /* CRYPT */
--incnt;
return *inptr++;
} /* end function readbyte() */
/********************/
/* Function flush() */
/********************/
int ChUnzip::flush(uch * rawbuf, ulg size, int unshrink) /* cflag => always 0; 50 if write error */
{
register ulg crcval = crc32val;
register ulg n = size;
register uch *p, *q;
uch *transbuf;
ulg transbufsiz;
uch * outbuf = 0;
//static int didCRlast = FALSE;
/*---------------------------------------------------------------------------
Compute the CRC first; if testing or if disk is full, that's it.
---------------------------------------------------------------------------*/
p = rawbuf;
while (n--)
crcval = crc_32_tab[((uch)crcval ^ (*p++)) & 0xff] ^ (crcval >> 8);
crc32val = crcval;
if (( m_flOptions & UNZIP_OPT_TEST ) || size == 0L) /* testing or nothing to write: all done */
return 0;
if ( m_flOptions & UNZIP_OPT_DISPLAY_FILE )
{ // view window
}
if (disk_full)
return 50; /* disk already full: ignore rest of file */
/*---------------------------------------------------------------------------
Write the bytes rawbuf[0..size-1] to the output device, first converting
end-of-lines and ASCII/EBCDIC as needed. If SMALL_MEM or MED_MEM are NOT
defined, outbuf is assumed to be at least as large as rawbuf and is not
necessarily checked for overflow.
---------------------------------------------------------------------------*/
if (!pInfo->textmode) {
/* GRR: note that for standard MS-DOS compilers, size argument to
* fwrite() can never be more than 65534, so WriteError macro will
* have to be rewritten if size can ever be that large. For now,
* never more than 32K. Also note that write() returns an int, which
* doesn't necessarily limit size to 32767 bytes if write() is used
* on 16-bit systems but does make it more of a pain; however, because
* at least MSC 5.1 has a lousy implementation of fwrite() (as does
* DEC Ultrix cc), write() is used anyway.
*/
long iPos = m_pOutFile->tellg();
m_pOutFile->write( (char *)rawbuf, (int)size );
long iNewPos = m_pOutFile->tellg();
if ( ( iNewPos - iPos ) != (long)size )
{
disk_full = 1;
return 50;
}
//if (WriteError(rawbuf, size, outfile)) /* write raw binary data */
// return cflag? 0 : disk_error();
} else {
#if 0
if (unshrink)
{
/* rawbuf = outbuf */
transbuf = outbuf2;
transbufsiz = TRANSBUFSIZ;
} else
#endif
{
/* rawbuf = slide */
outbuf = new uch[ OUTBUFSIZ + 1]; /* extra: ASCIIZ */
if ( !outbuf )
{
return PK_MEM3;
}
transbuf = outbuf;
transbufsiz = OUTBUFSIZ;
TRACE1( "\ntransbufsiz = OUTBUFSIZ = %u\n", OUTBUFSIZ);
}
if (newfile) {
didCRlast = FALSE; /* no previous buffers written */
newfile = FALSE;
}
p = rawbuf;
if (*p == LF && didCRlast)
++p;
/*-----------------------------------------------------------------------
Algorithm: CR/LF => native; lone CR => native; lone LF => native.
This routine is only for non-raw-VMS, non-raw-VM/CMS files (i.e.,
stream-oriented files, not record-oriented).
-----------------------------------------------------------------------*/
for (didCRlast = FALSE, q = transbuf; p < rawbuf+size; ++p) {
if (*p == CR) { /* lone CR or CR/LF: EOL either way */
PutNativeEOL
if (p == rawbuf+size-1) /* last char in buffer */
didCRlast = TRUE;
else if (p[1] == LF) /* get rid of accompanying LF */
++p;
} else if (*p == LF) /* lone LF */
PutNativeEOL
else
#if defined( CH_UNIX )
if (*p != CTRLZ) /* lose all ^Z's */
#endif
*q++ = native(*p);
//#if (defined(SMALL_MEM) || defined(MED_MEM))
//# if (lenEOL == 1) /* don't check unshrink: both buffers small but equal */
//if (!unshrink)
//# endif
/* check for danger of buffer overflow and flush */
if (q > transbuf+transbufsiz-lenEOL) {
TRACE3( "p - rawbuf = %u q-transbuf = %u size = %lu\n",
(unsigned)(p-rawbuf), (unsigned)(q-transbuf), size );
long iPos = m_pOutFile->tellg();
m_pOutFile->write( (char *)transbuf, (unsigned)(q-transbuf) );
long iNewPos = m_pOutFile->tellg();
if ( (iNewPos - iPos ) != (long)(q-transbuf) )
{
disk_full = 1;
if ( outbuf )
{
delete []outbuf;
}
return 50;
}
//if (WriteError(transbuf, (unsigned)(q-transbuf), outfile))
// return cflag? 0 : disk_error();
q = transbuf;
continue;
}
//#endif /* SMALL_MEM || MED_MEM */
}
/*-----------------------------------------------------------------------
Done translating: write whatever we've got to file.
-----------------------------------------------------------------------*/
TRACE3( "p - rawbuf = %u q-transbuf = %u size = %lu\n",
(unsigned)(p-rawbuf), (unsigned)(q-transbuf), size);
if ( q > transbuf )
{
long iPos = m_pOutFile->tellg();
m_pOutFile->write( (char *)transbuf, (unsigned)(q-transbuf) );
long iNewPos = m_pOutFile->tellg();
if ( ( iNewPos - iPos ) != (long)(q-transbuf) )
{
disk_full = 1;
if ( outbuf )
{
delete []outbuf;
}
return 50;
}
}
//if (q > transbuf &&
// WriteError(transbuf, (unsigned)(q-transbuf), outfile))
// return cflag? 0 : disk_error();
}
if ( outbuf )
{
delete []outbuf;
}
return 0;
} /* end function flush() */
/************************/
/* Function do_string() */
/************************/
int ChUnzip::do_string( unsigned int len, int option) /* return PK-type error code */
//unsigned int len; /* without prototype, ush converted to this */
//int option;
{
long comment_bytes_left, block_length;
int error=PK_OK;
ush extra_len;
/*---------------------------------------------------------------------------
This function processes arbitrary-length (well, usually) strings. Three
options are allowed: SKIP, wherein the string is skipped (pretty logical,
eh?); DISPLAY, wherein the string is printed to standard output after un-
dergoing any necessary or unnecessary character conversions; and ZFILENAME,
wherein the string is put into the filename[] array after undergoing ap-
propriate conversions (including case-conversion, if that is indicated:
see the global variable pInfo->lcflag). The latter option should be OK,
since filename is now dimensioned at 1025, but we check anyway.
The string, by the way, is assumed to start at the current file-pointer
position; its length is given by len. So start off by checking length
of string: if zero, we're already done.
---------------------------------------------------------------------------*/
if (!len)
return PK_COOL;
switch (option) {
/*
* First case: print string on standard output. First set loop vari-
* ables, then loop through the comment in chunks of OUTBUFSIZ bytes,
* converting formats and printing as we go. The second half of the
* loop conditional was added because the file might be truncated, in
* which case comment_bytes_left will remain at some non-zero value for
* all time. outbuf and slide are used as scratch buffers because they
* are available (we should be either before or in between any file pro-
* cessing).
*/
case DISPLAY:
{
uch * outbuf = 0;
comment_bytes_left = len;
block_length = OUTBUFSIZ; /* for the while statement, first time */
while (comment_bytes_left > 0 && block_length > 0) {
outbuf = new uch[ OUTBUFSIZ + 1]; /* extra: ASCIIZ */
if ( !outbuf )
{
return PK_MEM3;
}
register uch *p = outbuf;
register uch *q = outbuf;
if ((block_length = readbuf((char *)outbuf,
(unsigned) MIN((long)OUTBUFSIZ, comment_bytes_left))) == 0)
return PK_EOF;
comment_bytes_left -= block_length;
/* this is why we allocated an extra byte for outbuf: */
outbuf[block_length] = '\0'; /* terminate w/zero: ASCIIZ */
/* remove all ASCII carriage returns comment before printing
* (since used before A_TO_N(), check for CR instead of '\r')
*/
while (*p) {
while (*p == CR)
++p;
*q++ = *p++;
}
/* could check whether (p - outbuf) == block_length here */
*q = '\0';
A_TO_N(outbuf); /* translate string to native */
/* ran out of local mem -- had to cheat */
//WriteStringToMsgWin(outbuf, bRealTimeMsgUpdate);
delete []outbuf;
}
break;
}
/*
* Second case: read string into filename[] array. The filename should
* never ever be longer than FILNAMSIZ-1 (1024), but for now we'll check,
* just to be sure.
*/
case ZFILENAME:
extra_len = 0;
if (len >= FILNAMSIZ) {
//FPRINTF(stderr, LoadFarString(FilenameTooLongTrunc));
error = PK_WARN;
extra_len = len - FILNAMSIZ + 1;
len = FILNAMSIZ - 1;
}
if (readbuf(filename, len) == 0)
return PK_EOF;
filename[len] = '\0'; /* terminate w/zero: ASCIIZ */
A_TO_N(filename); /* translate string to native */
if (pInfo->lcflag) /* replace with lowercase filename */
TOLOWER(filename, filename);
if (pInfo->vollabel && len > 8 && filename[8] == '.') {
char *p = filename+8;
while (*p++)
p[-1] = *p; /* disk label, and 8th char is dot: remove dot */
}
if (!extra_len) /* we're done here */
break;
/*
* We truncated the filename, so print what's left and then fall
* through to the SKIP routine.
*/
//FPRINTF(stderr, "[ %s ]\n", filename);
len = extra_len;
/* FALL THROUGH... */
/*
* Third case: skip string, adjusting readbuf's internal variables
* as necessary (and possibly skipping to and reading a new block of
* data).
*/
case SKIP:
LSEEK(cur_zipfile_bufstart + (inptr-inbuf) + len)
break;
/*
* Fourth case: assume we're at the start of an "extra field"; malloc
* storage for it and read data into the allocated space.
*/
case EXTRA_FIELD:
if (extra_field != (uch *)NULL)
free(extra_field);
if ((extra_field = (uch *)malloc(len)) == (uch *)NULL) {
//FPRINTF(stderr, LoadFarString(ExtraFieldTooLong), len);
LSEEK(cur_zipfile_bufstart + (inptr-inbuf) + len)
} else
if (readbuf((char *)extra_field, len) == 0)
return PK_EOF;
break;
} /* end switch (option) */
return error;
} /* end function do_string() */
/**********************/
/* Function mapattr() */
/**********************/
/* Identical to MS-DOS, OS/2 versions. */
/* However, NT has a lot of extra permission stuff, so this function should */
/* probably be extended in the future. */
int ChUnzip::mapattr()
{
/* set archive bit (file is not backed up): */
pInfo->file_attr = (unsigned)(crec.external_file_attributes | 32) & 0xff;
return 0;
} /* end function mapattr() */
/************************/
/* Function mapname() */
/************************/
/*
* There are presently two possibilities in OS/2: the output filesystem is
* FAT, or it is HPFS. If the former, we need to map to FAT, obviously, but
* we *also* must map to HPFS and store that version of the name in extended
* attributes. Either way, we need to map to HPFS, so the main mapname
* routine does that. In the case that the output file system is FAT, an
* extra filename-mapping routine is called in checkdir(). While it should
* be possible to determine the filesystem immediately upon entry to mapname(),
* it is conceivable that the DOS APPEND utility could be added to OS/2 some-
* day, allowing a FAT directory to be APPENDed to an HPFS drive/path. There-
* fore we simply check the filesystem at each path component.
*
* Note that when alternative IFS's become available/popular, everything will
* become immensely more complicated. For example, a Minix filesystem would
* have limited filename lengths like FAT but no extended attributes in which
* to store the longer versions of the names. A BSD Unix filesystem would
* support paths of length 1024 bytes or more, but it is not clear that FAT
* EAs would allow such long .LONGNAME fields or that OS/2 would properly
* restore such fields when moving files from FAT to the new filesystem.
*
* GRR: some or all of the following chars should be checked in either
* mapname (HPFS) or map2fat (FAT), depending: ,=^+'"[]<>|\t&
*/
int ChUnzip::mapname(int renamed) /* return 0 if no error, 1 if caution (filename trunc), */
// int renamed; /* 2 if warning (skip file because dir doesn't exist), */
{ /* 3 if error (skip file), 10 if no memory (skip file), */
/* IZ_VOL_LABEL if can't do vol label, IZ_CREATED_DIR */
char pathcomp[FILNAMSIZ]; /* path-component buffer */
char *pp, *cp=NULL; /* character pointers */
char *lastsemi = NULL; /* pointer to last semi-colon in pathcomp */
int quote = FALSE; /* flag: next char is literal */
int error = 0;
register unsigned workch; /* hold the character being tested */
int rootlen = 0; /* length of rootpath */
char *rootpath = 0; /* user's "extract-to" directory */
char *buildpathHPFS = 0; /* full path (so far) to extracted file, */
char *buildpathFAT = 0; /* both HPFS/EA (main) and FAT versions */
char *endHPFS = 0; /* corresponding pointers to end of */
char *endFAT = 0; /* buildpath ('\0') */
int created_dir; /* used by mapname(), checkdir() */
int renamed_fullpath; /* ditto */
int fnlen; /* ditto */
unsigned nLabelDrive; /* ditto */
int volflag = 0;
/*---------------------------------------------------------------------------
Initialize various pointers and counters and stuff.
---------------------------------------------------------------------------*/
/* can create path as long as not just freshening, or if user told us */
//create_dirs = (!fflag || renamed);
int create_dirs = (!(m_flOptions & UNZIP_OPT_FRESHEN_ONLY ) || renamed);
created_dir = FALSE; /* not yet */
renamed_fullpath = FALSE;
fnlen = strlen(filename);
if (renamed) {
cp = filename - 1; /* point to beginning of renamed name... */
while (*++cp)
if (*cp == '\\') /* convert backslashes to forward */
*cp = '/';
cp = filename;
/* use temporary rootpath if user gave full pathname */
if (filename[0] == '/') {
renamed_fullpath = TRUE;
pathcomp[0] = '/'; /* copy the '/' and terminate */
pathcomp[1] = '\0';
++cp;
} else if (isalpha(filename[0]) && filename[1] == ':') {
renamed_fullpath = TRUE;
pp = pathcomp;
*pp++ = *cp++; /* copy the "d:" (+ '/', possibly) */
*pp++ = *cp++;
if (*cp == '/')
*pp++ = *cp++; /* otherwise add "./"? */
*pp = '\0';
}
}
/* pathcomp is ignored unless renamed_fullpath is TRUE: */
if ((error = checkdir( pathcomp, INIT, rootlen,
rootpath, buildpathHPFS,
buildpathFAT, endHPFS,
endFAT, create_dirs,
nLabelDrive, volflag, created_dir,
fnlen, renamed_fullpath )) != 0) /* initialize path buffer */
return error; /* ...unless no mem or vol label on hard disk */
*pathcomp = '\0'; /* initialize translation buffer */
pp = pathcomp; /* point to translation buffer */
if (!renamed) { /* cp already set if renamed */
//if (jflag) /* junking directories */
if ( !(m_flOptions & UNZIP_OPT_PRESERVE_FILENAMES )) /* junking directories */
cp = (char *)strrchr(filename, '/');
if (cp == NULL) /* no '/' or not junking dirs */
cp = filename; /* point to internal zipfile-member pathname */
else
++cp; /* point to start of last component of path */
}
/*---------------------------------------------------------------------------
Begin main loop through characters in filename.
---------------------------------------------------------------------------*/
while ((workch = (uch)*cp++) != 0) {
if (quote) { /* if character quoted, */
*pp++ = (char)workch; /* include it literally */
quote = FALSE;
} else
switch (workch) {
case '/': /* can assume -j flag not given */
*pp = '\0';
if ((error = checkdir( pathcomp, APPEND_DIR, rootlen,
rootpath, buildpathHPFS,
buildpathFAT, endHPFS,
endFAT, create_dirs,
nLabelDrive, volflag, created_dir,
fnlen, renamed_fullpath ) ) > 1)
return error;
pp = pathcomp; /* reset conversion buffer for next piece */
lastsemi = NULL; /* leave directory semi-colons alone */
break;
case ':':
*pp++ = '_'; /* drive names not stored in zipfile, */
break; /* so no colons allowed */
case ';': /* start of VMS version? */
lastsemi = pp; /* remove VMS version later... */
*pp++ = ';'; /* but keep semicolon for now */
break;
case '\026': /* control-V quote for special chars */
quote = TRUE; /* set flag for next character */
break;
case ' ': /* keep spaces unless specifically */
/* NT cannot create filenames with spaces on FAT volumes */
//if (sflag || IsVolumeOldFAT(filename))
if ( !(m_flOptions & UNZIP_OPT_ALLOW_SPACE ) || IsVolumeOldFAT(filename) )
*pp++ = '_';
else
*pp++ = ' ';
break;
default:
/* allow European characters in filenames: */
if (isprint(workch) || (128 <= workch && workch <= 254))
*pp++ = (char)workch;
} /* end switch */
} /* end while loop */
*pp = '\0'; /* done with pathcomp: terminate it */
/* if not saving them, remove VMS version numbers (appended "###") */
//if (!V_flag && lastsemi) {
if (!(m_flOptions & UNZIP_OPT_STRIP_VMS_VER ) && lastsemi) {
pp = lastsemi + 1; /* semi-colon was kept: expect #'s after */
while (isdigit((uch)(*pp)))
++pp;
if (*pp == '\0') /* only digits between ';' and end: nuke */
*lastsemi = '\0';
}
/*---------------------------------------------------------------------------
Report if directory was created (and no file to create: filename ended
in '/'), check name to be sure it exists, and combine path and name be-
fore exiting.
---------------------------------------------------------------------------*/
if (filename[fnlen-1] == '/') {
checkdir( pathcomp, GETPATH, rootlen,
rootpath, buildpathHPFS,
buildpathFAT, endHPFS,
endFAT, create_dirs,
nLabelDrive, volflag, created_dir,
fnlen, renamed_fullpath );
//if (created_dir && QCOND2) {
if (created_dir && m_flOptions & UNZIP_OPT_QUIET) {
/* GRR: trailing '/'? need to strip or not? */
//FPRINTF(stdout, " creating: %-22s\n", filename);
/* HG: are we setting the date&time on a newly created dir? */
/* Not quite sure how to do this. It does not seem to */
/* be done in the MS-DOS version of mapname(). */
return IZ_CREATED_DIR; /* dir time already set */
}
return 2; /* dir existed already; don't look for data to extract */
}
if (*pathcomp == '\0') {
//FPRINTF(stderr, "mapname: conversion of %s failed\n", filename);
return 3;
}
checkdir( pathcomp, APPEND_NAME, rootlen,
rootpath, buildpathHPFS,
buildpathFAT, endHPFS,
endFAT, create_dirs,
nLabelDrive, volflag, created_dir,
fnlen, renamed_fullpath ); /* returns 1 if truncated: care? */
checkdir( pathcomp, GETPATH, rootlen,
rootpath, buildpathHPFS,
buildpathFAT, endHPFS,
endFAT, create_dirs,
nLabelDrive, volflag, created_dir,
fnlen, renamed_fullpath ) ;
TRACE2( "mapname returns with filename = [%s] (error = %d)\n\n",
filename, error );
if (pInfo->vollabel) { /* set the volume label now */
char drive[3];
/* Build a drive string, e.g. "b:" */
//drive[0] = 'a' + nLabelDrive - 1;
drive[1] = ':';
drive[2] = '\0';
//if (QCOND2)
if (m_flOptions & UNZIP_OPT_QUIET )
//FPRINTF(stdout, "labelling %s %-22s\n", drive, filename);
#if defined( CH_ARCH_16 )
#pragma message ( "SetVolumeLabel not implemented under win16" )
#else
if (!SetVolumeLabel(drive, filename)) {
//FPRINTF(stderr, "mapname: error setting volume label\n");
return 3;
}
#endif
return 2; /* success: skip the "extraction" quietly */
}
return error;
} /* end function mapname() */
/*****************************/
/* Function IsVolumeOldFAT() */
/*****************************/
/*
* Note: 8.3 limits on filenames apply only to old-style FAT filesystems.
* More recent versions of Windows (Windows NT 3.5 / Windows 4.0)
* can support long filenames (LFN) on FAT filesystems. Check the
* filesystem maximum component length field to detect LFN support.
* [GRR: this routine is only used to determine whether spaces in
* filenames are supported...]
*/
int ChUnzip::IsVolumeOldFAT(char *name)
{
#if defined( CH_ARCH_32 )
char *tmp0;
char rootPathName[4];
char tmp1[MAX_PATH], tmp2[MAX_PATH];
unsigned long maxCompLen, fileSysFlags;
unsigned long volSerNo;
if (isalpha(name[0]) && (name[1] == ':'))
tmp0 = name;
else
{
GetFullPathName(name, MAX_PATH, tmp1, &tmp0);
tmp0 = &tmp1[0];
}
strncpy(rootPathName, tmp0, 3); /* Build the root path name, */
rootPathName[3] = '\0'; /* e.g. "A:/" */
GetVolumeInformation(rootPathName, tmp1, MAX_PATH, &volSerNo,
&maxCompLen, &fileSysFlags, tmp2, MAX_PATH);
/* Long Filenames (LFNs) are available if the component length is > 12 */
return maxCompLen <= 12;
#else
return true;
#endif
}
/**********************/
/* Function map2fat() */ /* Identical to OS/2 version */
/**********************/
void ChUnzip::map2fat( char * pathcomp, char **pEndFAT)
{
char *ppc = pathcomp; /* variable pointer to pathcomp */
char *pEnd = *pEndFAT; /* variable pointer to buildpathFAT */
char *pBegin = *pEndFAT; /* constant pointer to start of this comp. */
char *last_dot = NULL; /* last dot not converted to underscore */
int dotname = FALSE; /* flag: path component begins with dot */
/* ("." and ".." don't count) */
register unsigned workch; /* hold the character being tested */
/* Only need check those characters which are legal in HPFS but not
* in FAT: to get here, must already have passed through mapname.
* (GRR: oops, small bug--if char was quoted, no longer have any
* knowledge of that.) Also must truncate path component to ensure
* 8.3 compliance...
*/
while ((workch = (uch)*ppc++) != 0) {
switch (workch) {
case '[':
case ']':
*pEnd++ = '_'; /* convert brackets to underscores */
break;
case '.':
if (pEnd == *pEndFAT) { /* nothing appended yet... */
if (*ppc == '\0') /* don't bother appending a */
break; /* "./" component to the path */
else if (*ppc == '.' && ppc[1] == '\0') { /* "../" */
*pEnd++ = '.'; /* add first dot, unchanged... */
++ppc; /* skip second dot, since it will */
} else { /* be "added" at end of if-block */
*pEnd++ = '_'; /* FAT doesn't allow null filename */
dotname = TRUE; /* bodies, so map .exrc -> _.exrc */
} /* (extra '_' now, "dot" below) */
} else if (dotname) { /* found a second dot, but still */
dotname = FALSE; /* have extra leading underscore: */
*pEnd = '\0'; /* remove it by shifting chars */
pEnd = *pEndFAT + 1; /* left one space (e.g., .p1.p2: */
while (pEnd[1]) { /* __p1 -> _p1_p2 -> _p1.p2 when */
*pEnd = pEnd[1]; /* finished) [opt.: since first */
++pEnd; /* two chars are same, can start */
} /* shifting at second position] */
}
last_dot = pEnd; /* point at last dot so far... */
*pEnd++ = '_'; /* convert dot to underscore for now */
break;
default:
*pEnd++ = (char)workch;
} /* end switch */
} /* end while loop */
*pEnd = '\0'; /* terminate buildpathFAT */
/* NOTE: keep in mind that pEnd points to the end of the path
* component, and *pEndFAT still points to the *beginning* of it...
* Also note that the algorithm does not try to get too fancy:
* if there are no dots already, the name either gets truncated
* at 8 characters or the last underscore is converted to a dot