-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileio.c
2855 lines (2413 loc) · 92.4 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
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
/*
Copyright (c) 1990-2009 Info-ZIP. All rights reserved.
See the accompanying file LICENSE, version 2009-Jan-02 or later
(the contents of which are also included in unzip.h) for terms of use.
If, for some reason, all these files are missing, the Info-ZIP license
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*---------------------------------------------------------------------------
fileio.c
This file contains routines for doing direct but relatively generic input/
output, file-related sorts of things, plus some miscellaneous stuff. Most
of the stuff has to do with opening, closing, reading and/or writing files.
Contains: open_input_file()
open_outfile() (not: VMS, AOS/VS, CMSMVS, MACOS, TANDEM)
undefer_input()
defer_leftover_input()
readbuf()
readbyte()
fillinbuf()
seek_zipf()
flush() (non-VMS)
is_vms_varlen_txt() (non-VMS, VMS_TEXT_CONV only)
disk_error() (non-VMS)
UzpMessagePrnt()
UzpMessageNull() (DLL only)
UzpInput()
UzpMorePause()
UzpPassword() (non-WINDLL)
handler()
dos_to_unix_time() (non-VMS, non-VM/CMS, non-MVS)
check_for_newer() (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
do_string()
makeword()
makelong()
makeint64()
fzofft()
str2iso() (CRYPT && NEED_STR2ISO, only)
str2oem() (CRYPT && NEED_STR2OEM, only)
memset() (ZMEM only)
memcpy() (ZMEM only)
zstrnicmp() (NO_STRNICMP only)
zstat() (REGULUS only)
plastchar() (_MBCS only)
uzmbclen() (_MBCS && NEED_UZMBCLEN, only)
uzmbschr() (_MBCS && NEED_UZMBSCHR, only)
uzmbsrchr() (_MBCS && NEED_UZMBSRCHR, only)
fLoadFarString() (SMALL_MEM only)
fLoadFarStringSmall() (SMALL_MEM only)
fLoadFarStringSmall2() (SMALL_MEM only)
zfstrcpy() (SMALL_MEM only)
zfstrcmp() (SMALL_MEM && !(SFX || FUNZIP) only)
---------------------------------------------------------------------------*/
#define __FILEIO_C /* identifies this source module */
#define UNZIP_INTERNAL
#include "unzip.h"
#ifdef WINDLL
# ifdef POCKET_UNZIP
# include "wince/intrface.h"
# else
# include "windll/windll.h"
# endif
# include <setjmp.h>
#endif
#include "crc32.h"
#include "crypt.h"
#include "ttyio.h"
/* setup of codepage conversion for decryption passwords */
#if CRYPT
# if (defined(CRYP_USES_ISO2OEM) && !defined(IZ_ISO2OEM_ARRAY))
# define IZ_ISO2OEM_ARRAY /* pull in iso2oem[] table */
# endif
# if (defined(CRYP_USES_OEM2ISO) && !defined(IZ_OEM2ISO_ARRAY))
# define IZ_OEM2ISO_ARRAY /* pull in oem2iso[] table */
# endif
#endif
#include "ebcdic.h" /* definition/initialization of ebcdic[] */
/*
Note: Under Windows, the maximum size of the buffer that can be used
with any of the *printf calls is 16,384, so win_fprintf was used to
feed the fprintf clone no more than 16K chunks at a time. This should
be valid for anything up to 64K (and probably beyond, assuming your
buffers are that big).
*/
#ifdef WINDLL
# define WriteError(buf,len,strm) \
(win_fprintf(pG, strm, (extent)len, (char far *)buf) != (int)(len))
#else /* !WINDLL */
# ifdef USE_FWRITE
# define WriteError(buf,len,strm) \
((extent)fwrite((char *)(buf),1,(extent)(len),strm) != (extent)(len))
# else
# define WriteError(buf,len,strm) \
((extent)write(fileno(strm),(char *)(buf),(extent)(len)) != (extent)(len))
# endif
#endif /* ?WINDLL */
/*
2005-09-16 SMS.
On VMS, when output is redirected to a file, as in a command like
"PIPE UNZIP -v > X.OUT", the output file is created with VFC record
format, and multiple calls to write() or fwrite() will produce multiple
records, even when there's no newline terminator in the buffer.
The result is unsightly output with spurious newlines. Using fprintf()
instead of write() here, and disabling a fflush(stdout) in UzpMessagePrnt()
below, together seem to solve the problem.
According to the C RTL manual, "The write and decc$record_write
functions always generate at least one record." Also, "[T]he fwrite
function always generates at least <number_items> records." So,
"fwrite(buf, len, 1, strm)" is much better ("1" record) than
"fwrite(buf, 1, len, strm)" ("len" (1-character) records, _really_
ugly), but neither is better than write(). Similarly, "The fflush
function always generates a record if there is unwritten data in the
buffer." Apparently fprintf() buffers the stuff somewhere, and puts
out a record (only) when it sees a newline.
*/
#ifdef VMS
# define WriteTxtErr(buf,len,strm) \
((extent)fprintf(strm, "%.*s", len, buf) != (extent)(len))
#else
# define WriteTxtErr(buf,len,strm) WriteError(buf,len,strm)
#endif
#if (defined(USE_DEFLATE64) && defined(__16BIT__))
static int partflush OF((__GPRO__ uch *rawbuf, ulg size, int unshrink));
#endif
#ifdef VMS_TEXT_CONV
static int is_vms_varlen_txt OF((__GPRO__ uch *ef_buf, unsigned ef_len));
#endif
static int disk_error OF((__GPRO));
/****************************/
/* Strings used in fileio.c */
/****************************/
static ZCONST char Far CannotOpenZipfile[] =
"error: cannot open zipfile [ %s ]\n %s\n";
#if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS) && !defined(MACOS))
#if (!defined(TANDEM))
#if (defined(ATH_BEO_THS_UNX) || defined(DOS_FLX_NLM_OS2_W32))
static ZCONST char Far CannotDeleteOldFile[] =
"error: cannot delete old %s\n %s\n";
#ifdef UNIXBACKUP
static ZCONST char Far CannotRenameOldFile[] =
"error: cannot rename old %s\n %s\n";
static ZCONST char Far BackupSuffix[] = "~";
#endif
#endif /* ATH_BEO_THS_UNX || DOS_FLX_NLM_OS2_W32 */
#ifdef NOVELL_BUG_FAILSAFE
static ZCONST char Far NovellBug[] =
"error: %s: stat() says does not exist, but fopen() found anyway\n";
#endif
static ZCONST char Far CannotCreateFile[] =
"error: cannot create %s\n %s\n";
#endif /* !TANDEM */
#endif /* !VMS && !AOS_VS && !CMS_MVS && !MACOS */
static ZCONST char Far ReadError[] = "error: zipfile read error\n";
static ZCONST char Far FilenameTooLongTrunc[] =
"warning: filename too long--truncating.\n";
#ifdef UNICODE_SUPPORT
static ZCONST char Far UFilenameTooLongTrunc[] =
"warning: Converted unicode filename too long--truncating.\n";
#endif
static ZCONST char Far ExtraFieldTooLong[] =
"warning: extra field too long (%d). Ignoring...\n";
#ifdef WINDLL
static ZCONST char Far DiskFullQuery[] =
"%s: write error (disk full?).\n";
#else
static ZCONST char Far DiskFullQuery[] =
"%s: write error (disk full?). Continue? (y/n/^C) ";
static ZCONST char Far ZipfileCorrupt[] =
"error: zipfile probably corrupt (%s)\n";
# ifdef SYMLINKS
static ZCONST char Far FileIsSymLink[] =
"%s exists and is a symbolic link%s.\n";
# endif
# ifdef MORE
static ZCONST char Far MorePrompt[] = "--More--(%lu)";
# endif
static ZCONST char Far QuitPrompt[] =
"--- Press `Q' to quit, or any other key to continue ---";
static ZCONST char Far HidePrompt[] = /* "\r \r"; */
"\r \r";
# if CRYPT
# ifdef MACOS
/* SPC: are names on MacOS REALLY so much longer than elsewhere ??? */
static ZCONST char Far PasswPrompt[] = "[%s]\n %s password: ";
# else
static ZCONST char Far PasswPrompt[] = "[%s] %s password: ";
# endif
static ZCONST char Far PasswPrompt2[] = "Enter password: ";
static ZCONST char Far PasswRetry[] = "password incorrect--reenter: ";
# endif /* CRYPT */
#endif /* !WINDLL */
/******************************/
/* Function open_input_file() */
/******************************/
int open_input_file(__G) /* return 1 if open failed */
__GDEF
{
/*
* open the zipfile for reading and in BINARY mode to prevent cr/lf
* translation, which would corrupt the bitstreams
*/
#ifdef VMS
G.zipfd = open(G.zipfn, O_RDONLY, 0, OPNZIP_RMS_ARGS);
#else /* !VMS */
#ifdef MACOS
G.zipfd = open(G.zipfn, 0);
#else /* !MACOS */
#ifdef CMS_MVS
G.zipfd = vmmvs_open_infile(__G);
#else /* !CMS_MVS */
#ifdef USE_STRM_INPUT
G.zipfd = fopen(G.zipfn, FOPR);
#else /* !USE_STRM_INPUT */
G.zipfd = open(G.zipfn, O_RDONLY | O_BINARY);
#endif /* ?USE_STRM_INPUT */
#endif /* ?CMS_MVS */
#endif /* ?MACOS */
#endif /* ?VMS */
#ifdef USE_STRM_INPUT
if (G.zipfd == NULL)
#else
/* if (G.zipfd < 0) */ /* no good for Windows CE port */
if (G.zipfd == -1)
#endif
{
Info(slide, 0x401, ((char *)slide, LoadFarString(CannotOpenZipfile),
G.zipfn, strerror(errno)));
return 1;
}
return 0;
} /* end function open_input_file() */
#if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS) && !defined(MACOS))
#if (!defined(TANDEM))
/***************************/
/* Function open_outfile() */
/***************************/
int open_outfile(__G) /* return 1 if fail */
__GDEF
{
#ifdef DLL
if (G.redirect_data)
return (redirect_outfile(__G) == FALSE);
#endif
#ifdef QDOS
QFilename(__G__ G.filename);
#endif
#if (defined(DOS_FLX_NLM_OS2_W32) || defined(ATH_BEO_THS_UNX))
#ifdef BORLAND_STAT_BUG
/* Borland 5.0's stat() barfs if the filename has no extension and the
* file doesn't exist. */
if (access(G.filename, 0) == -1) {
FILE *tmp = fopen(G.filename, "wb+");
/* file doesn't exist, so create a dummy file to keep stat() from
* failing (will be over-written anyway) */
fputc('0', tmp); /* just to have something in the file */
fclose(tmp);
}
#endif /* BORLAND_STAT_BUG */
#ifdef SYMLINKS
if (SSTAT(G.filename, &G.statbuf) == 0 ||
lstat(G.filename, &G.statbuf) == 0)
#else
if (SSTAT(G.filename, &G.statbuf) == 0)
#endif /* ?SYMLINKS */
{
Trace((stderr, "open_outfile: stat(%s) returns 0: file exists\n",
FnFilter1(G.filename)));
#ifdef UNIXBACKUP
if (uO.B_flag) { /* do backup */
char *tname;
z_stat tmpstat;
int blen, flen, tlen;
blen = strlen(BackupSuffix);
flen = strlen(G.filename);
tlen = flen + blen + 6; /* includes space for 5 digits */
if (tlen >= FILNAMSIZ) { /* in case name is too long, truncate */
tname = (char *)malloc(FILNAMSIZ);
if (tname == NULL)
return 1; /* in case we run out of space */
tlen = FILNAMSIZ - 1 - blen;
strcpy(tname, G.filename); /* make backup name */
tname[tlen] = '\0';
if (flen > tlen) flen = tlen;
tlen = FILNAMSIZ;
} else {
tname = (char *)malloc(tlen);
if (tname == NULL)
return 1; /* in case we run out of space */
strcpy(tname, G.filename); /* make backup name */
}
strcpy(tname+flen, BackupSuffix);
if (IS_OVERWRT_ALL) {
/* If there is a previous backup file, delete it,
* otherwise the following rename operation may fail.
*/
if (SSTAT(tname, &tmpstat) == 0)
unlink(tname);
} else {
/* Check if backupname exists, and, if it's true, try
* appending numbers of up to 5 digits (or the maximum
* "unsigned int" number on 16-bit systems) to the
* BackupSuffix, until an unused name is found.
*/
unsigned maxtail, i;
char *numtail = tname + flen + blen;
/* take account of the "unsigned" limit on 16-bit systems: */
maxtail = ( ((~0) >= 99999L) ? 99999 : (~0) );
switch (tlen - flen - blen - 1) {
case 4: maxtail = 9999; break;
case 3: maxtail = 999; break;
case 2: maxtail = 99; break;
case 1: maxtail = 9; break;
case 0: maxtail = 0; break;
}
/* while filename exists */
for (i = 0; (i < maxtail) && (SSTAT(tname, &tmpstat) == 0);)
sprintf(numtail,"%u", ++i);
}
if (rename(G.filename, tname) != 0) { /* move file */
Info(slide, 0x401, ((char *)slide,
LoadFarString(CannotRenameOldFile),
FnFilter1(G.filename), strerror(errno)));
free(tname);
return 1;
}
Trace((stderr, "open_outfile: %s now renamed into %s\n",
FnFilter1(G.filename), FnFilter2(tname)));
free(tname);
} else
#endif /* UNIXBACKUP */
{
#ifdef DOS_FLX_OS2_W32
if (!(G.statbuf.st_mode & S_IWRITE)) {
Trace((stderr,
"open_outfile: existing file %s is read-only\n",
FnFilter1(G.filename)));
chmod(G.filename, S_IREAD | S_IWRITE);
Trace((stderr, "open_outfile: %s now writable\n",
FnFilter1(G.filename)));
}
#endif /* DOS_FLX_OS2_W32 */
#ifdef NLM
/* Give the file read/write permission (non-POSIX shortcut) */
chmod(G.filename, 0);
#endif /* NLM */
if (unlink(G.filename) != 0) {
Info(slide, 0x401, ((char *)slide,
LoadFarString(CannotDeleteOldFile),
FnFilter1(G.filename), strerror(errno)));
return 1;
}
Trace((stderr, "open_outfile: %s now deleted\n",
FnFilter1(G.filename)));
}
}
#endif /* DOS_FLX_NLM_OS2_W32 || ATH_BEO_THS_UNX */
#ifdef RISCOS
if (SWI_OS_File_7(G.filename,0xDEADDEAD,0xDEADDEAD,G.lrec.ucsize)!=NULL) {
Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
FnFilter1(G.filename), strerror(errno)));
return 1;
}
#endif /* RISCOS */
#ifdef TOPS20
char *tfilnam;
if ((tfilnam = (char *)malloc(2*strlen(G.filename)+1)) == (char *)NULL)
return 1;
strcpy(tfilnam, G.filename);
upper(tfilnam);
enquote(tfilnam);
if ((G.outfile = fopen(tfilnam, FOPW)) == (FILE *)NULL) {
Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
tfilnam, strerror(errno)));
free(tfilnam);
return 1;
}
free(tfilnam);
#else /* !TOPS20 */
#ifdef MTS
if (uO.aflag)
G.outfile = zfopen(G.filename, FOPWT);
else
G.outfile = zfopen(G.filename, FOPW);
if (G.outfile == (FILE *)NULL) {
Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
FnFilter1(G.filename), strerror(errno)));
return 1;
}
#else /* !MTS */
#ifdef DEBUG
Info(slide, 1, ((char *)slide,
"open_outfile: doing fopen(%s) for reading\n", FnFilter1(G.filename)));
if ((G.outfile = zfopen(G.filename, FOPR)) == (FILE *)NULL)
Info(slide, 1, ((char *)slide,
"open_outfile: fopen(%s) for reading failed: does not exist\n",
FnFilter1(G.filename)));
else {
Info(slide, 1, ((char *)slide,
"open_outfile: fopen(%s) for reading succeeded: file exists\n",
FnFilter1(G.filename)));
fclose(G.outfile);
}
#endif /* DEBUG */
#ifdef NOVELL_BUG_FAILSAFE
if (G.dne && ((G.outfile = zfopen(G.filename, FOPR)) != (FILE *)NULL)) {
Info(slide, 0x401, ((char *)slide, LoadFarString(NovellBug),
FnFilter1(G.filename)));
fclose(G.outfile);
return 1; /* with "./" fix in checkdir(), should never reach here */
}
#endif /* NOVELL_BUG_FAILSAFE */
Trace((stderr, "open_outfile: doing fopen(%s) for writing\n",
FnFilter1(G.filename)));
{
#if defined(ATH_BE_UNX) || defined(AOS_VS) || defined(QDOS) || defined(TANDEM)
mode_t umask_sav = umask(0077);
#endif
#if defined(SYMLINKS) || defined(QLZIP)
/* These features require the ability to re-read extracted data from
the output files. Output files are created with Read&Write access.
*/
G.outfile = zfopen(G.filename, FOPWR);
#else
G.outfile = zfopen(G.filename, FOPW);
#endif
#if defined(ATH_BE_UNX) || defined(AOS_VS) || defined(QDOS) || defined(TANDEM)
umask(umask_sav);
#endif
}
if (G.outfile == (FILE *)NULL) {
Info(slide, 0x401, ((char *)slide, LoadFarString(CannotCreateFile),
FnFilter1(G.filename), strerror(errno)));
return 1;
}
Trace((stderr, "open_outfile: fopen(%s) for writing succeeded\n",
FnFilter1(G.filename)));
#endif /* !MTS */
#endif /* !TOPS20 */
#ifdef USE_FWRITE
#ifdef DOS_NLM_OS2_W32
/* 16-bit MSC: buffer size must be strictly LESS than 32K (WSIZE): bogus */
setbuf(G.outfile, (char *)NULL); /* make output unbuffered */
#else /* !DOS_NLM_OS2_W32 */
#ifndef RISCOS
#ifdef _IOFBF /* make output fully buffered (works just about like write()) */
setvbuf(G.outfile, (char *)slide, _IOFBF, WSIZE);
#else
setbuf(G.outfile, (char *)slide);
#endif
#endif /* !RISCOS */
#endif /* ?DOS_NLM_OS2_W32 */
#endif /* USE_FWRITE */
#ifdef OS2_W32
/* preallocate the final file size to prevent file fragmentation */
SetFileSize(G.outfile, G.lrec.ucsize);
#endif
return 0;
} /* end function open_outfile() */
#endif /* !TANDEM */
#endif /* !VMS && !AOS_VS && !CMS_MVS && !MACOS */
/*
* These functions allow NEXTBYTE to function without needing two bounds
* checks. Call defer_leftover_input() if you ever have filled G.inbuf
* by some means other than readbyte(), and you then want to start using
* NEXTBYTE. When going back to processing bytes without NEXTBYTE, call
* undefer_input(). For example, extract_or_test_member brackets its
* central section that does the decompression with these two functions.
* If you need to check the number of bytes remaining in the current
* file while using NEXTBYTE, check (G.csize + G.incnt), not G.csize.
*/
/****************************/
/* function undefer_input() */
/****************************/
void undefer_input(__G)
__GDEF
{
if (G.incnt > 0)
G.csize += G.incnt;
if (G.incnt_leftover > 0) {
/* We know that "(G.csize < MAXINT)" so we can cast G.csize to int:
* This condition was checked when G.incnt_leftover was set > 0 in
* defer_leftover_input(), and it is NOT allowed to touch G.csize
* before calling undefer_input() when (G.incnt_leftover > 0)
* (single exception: see read_byte()'s "G.csize <= 0" handling) !!
*/
G.incnt = G.incnt_leftover + (int)G.csize;
G.inptr = G.inptr_leftover - (int)G.csize;
G.incnt_leftover = 0;
} else if (G.incnt < 0)
G.incnt = 0;
} /* end function undefer_input() */
/***********************************/
/* function defer_leftover_input() */
/***********************************/
void defer_leftover_input(__G)
__GDEF
{
if ((zoff_t)G.incnt > G.csize) {
/* (G.csize < MAXINT), we can safely cast it to int !! */
if (G.csize < 0L)
G.csize = 0L;
G.inptr_leftover = G.inptr + (int)G.csize;
G.incnt_leftover = G.incnt - (int)G.csize;
G.incnt = (int)G.csize;
} else
G.incnt_leftover = 0;
G.csize -= G.incnt;
} /* end function defer_leftover_input() */
/**********************/
/* Function readbuf() */
/**********************/
unsigned readbuf(__G__ buf, size) /* return number of bytes read into buf */
__GDEF
char *buf;
register unsigned size;
{
register unsigned count;
unsigned n;
n = size;
while (size) {
if (G.incnt <= 0) {
if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) == 0)
return (n-size);
else if (G.incnt < 0) {
/* another hack, but no real harm copying same thing twice */
(*G.message)((zvoid *)&G,
(uch *)LoadFarString(ReadError), /* CANNOT use slide */
(ulg)strlen(LoadFarString(ReadError)), 0x401);
return 0; /* discarding some data; better than lock-up */
}
/* buffer ALWAYS starts on a block boundary: */
G.cur_zipfile_bufstart += INBUFSIZ;
G.inptr = G.inbuf;
}
count = MIN(size, (unsigned)G.incnt);
memcpy(buf, G.inptr, count);
buf += count;
G.inptr += count;
G.incnt -= count;
size -= count;
}
return n;
} /* end function readbuf() */
/***********************/
/* Function readbyte() */
/***********************/
int readbyte(__G) /* refill inbuf and return a byte if available, else EOF */
__GDEF
{
if (G.mem_mode)
return EOF;
if (G.csize <= 0) {
G.csize--; /* for tests done after exploding */
G.incnt = 0;
return EOF;
}
if (G.incnt <= 0) {
if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) == 0) {
return EOF;
} else if (G.incnt < 0) { /* "fail" (abort, retry, ...) returns this */
/* another hack, but no real harm copying same thing twice */
(*G.message)((zvoid *)&G,
(uch *)LoadFarString(ReadError),
(ulg)strlen(LoadFarString(ReadError)), 0x401);
echon();
#ifdef WINDLL
longjmp(dll_error_return, 1);
#else
DESTROYGLOBALS();
EXIT(PK_BADERR); /* totally bailing; better than lock-up */
#endif
}
G.cur_zipfile_bufstart += INBUFSIZ; /* always starts on block bndry */
G.inptr = G.inbuf;
defer_leftover_input(__G); /* decrements G.csize */
}
#if CRYPT
if (G.pInfo->encrypted) {
uch *p;
int n;
/* This was previously set to decrypt one byte beyond G.csize, when
* incnt reached that far. GRR said, "but it's required: why?" This
* was a bug in fillinbuf() -- was it also a bug here?
*/
for (n = G.incnt, p = G.inptr; n--; p++)
zdecode(*p);
}
#endif /* CRYPT */
--G.incnt;
return *G.inptr++;
} /* end function readbyte() */
#if defined(USE_ZLIB) || defined(USE_BZIP2)
/************************/
/* Function fillinbuf() */
/************************/
int fillinbuf(__G) /* like readbyte() except returns number of bytes in inbuf */
__GDEF
{
if (G.mem_mode ||
(G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) <= 0)
return 0;
G.cur_zipfile_bufstart += INBUFSIZ; /* always starts on a block boundary */
G.inptr = G.inbuf;
defer_leftover_input(__G); /* decrements G.csize */
#if CRYPT
if (G.pInfo->encrypted) {
uch *p;
int n;
for (n = G.incnt, p = G.inptr; n--; p++)
zdecode(*p);
}
#endif /* CRYPT */
return G.incnt;
} /* end function fillinbuf() */
#endif /* USE_ZLIB || USE_BZIP2 */
/************************/
/* Function seek_zipf() */
/************************/
int seek_zipf(__G__ abs_offset)
__GDEF
zoff_t abs_offset;
{
/*
* Seek to the block boundary of the block which includes abs_offset,
* then read block into input buffer and set pointers appropriately.
* If block is already in the buffer, just set the pointers. This function
* is used by do_seekable (process.c), extract_or_test_entrylist (extract.c)
* and do_string (fileio.c). Also, a slightly modified version is embedded
* within extract_or_test_entrylist (extract.c). readbyte() and readbuf()
* (fileio.c) are compatible. NOTE THAT abs_offset is intended to be the
* "proper offset" (i.e., if there were no extra bytes prepended);
* cur_zipfile_bufstart contains the corrected offset.
*
* Since seek_zipf() is never used during decompression, it is safe to
* use the slide[] buffer for the error message.
*
* returns PK error codes:
* PK_BADERR if effective offset in zipfile is negative
* PK_EOF if seeking past end of zipfile
* PK_OK when seek was successful
*/
zoff_t request = abs_offset + G.extra_bytes;
zoff_t inbuf_offset = request % INBUFSIZ;
zoff_t bufstart = request - inbuf_offset;
if (request < 0) {
Info(slide, 1, ((char *)slide, LoadFarStringSmall(SeekMsg),
G.zipfn, LoadFarString(ReportMsg)));
return(PK_BADERR);
} else if (bufstart != G.cur_zipfile_bufstart) {
Trace((stderr,
"fpos_zip: abs_offset = %s, G.extra_bytes = %s\n",
FmZofft(abs_offset, NULL, NULL),
FmZofft(G.extra_bytes, NULL, NULL)));
#ifdef USE_STRM_INPUT
zfseeko(G.zipfd, bufstart, SEEK_SET);
G.cur_zipfile_bufstart = zftello(G.zipfd);
#else /* !USE_STRM_INPUT */
G.cur_zipfile_bufstart = zlseek(G.zipfd, bufstart, SEEK_SET);
#endif /* ?USE_STRM_INPUT */
Trace((stderr,
" request = %s, (abs+extra) = %s, inbuf_offset = %s\n",
FmZofft(request, NULL, NULL),
FmZofft((abs_offset+G.extra_bytes), NULL, NULL),
FmZofft(inbuf_offset, NULL, NULL)));
Trace((stderr, " bufstart = %s, cur_zipfile_bufstart = %s\n",
FmZofft(bufstart, NULL, NULL),
FmZofft(G.cur_zipfile_bufstart, NULL, NULL)));
if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) <= 0)
return(PK_EOF);
G.incnt -= (int)inbuf_offset;
G.inptr = G.inbuf + (int)inbuf_offset;
} else {
G.incnt += (G.inptr-G.inbuf) - (int)inbuf_offset;
G.inptr = G.inbuf + (int)inbuf_offset;
}
return(PK_OK);
} /* end function seek_zipf() */
#ifndef VMS /* for VMS use code in vms.c */
/********************/
/* Function flush() */ /* returns PK error codes: */
/********************/ /* if tflag => always 0; PK_DISK if write error */
int flush(__G__ rawbuf, size, unshrink)
__GDEF
uch *rawbuf;
ulg size;
int unshrink;
#if (defined(USE_DEFLATE64) && defined(__16BIT__))
{
int ret;
/* On 16-bit systems (MSDOS, OS/2 1.x), the standard C library functions
* cannot handle writes of 64k blocks at once. For these systems, the
* blocks to flush are split into pieces of 32k or less.
*/
while (size > 0x8000L) {
ret = partflush(__G__ rawbuf, 0x8000L, unshrink);
if (ret != PK_OK)
return ret;
size -= 0x8000L;
rawbuf += (extent)0x8000;
}
return partflush(__G__ rawbuf, size, unshrink);
} /* end function flush() */
/************************/
/* Function partflush() */ /* returns PK error codes: */
/************************/ /* if tflag => always 0; PK_DISK if write error */
static int partflush(__G__ rawbuf, size, unshrink)
__GDEF
uch *rawbuf; /* cannot be ZCONST, gets passed to (*G.message)() */
ulg size;
int unshrink;
#endif /* USE_DEFLATE64 && __16BIT__ */
{
register uch *p;
register uch *q;
uch *transbuf;
#if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
ulg transbufsiz;
#endif
/* static int didCRlast = FALSE; moved to globals.h */
/*---------------------------------------------------------------------------
Compute the CRC first; if testing or if disk is full, that's it.
---------------------------------------------------------------------------*/
G.crc32val = crc32(G.crc32val, rawbuf, (extent)size);
#ifdef DLL
if ((G.statreportcb != NULL) &&
(*G.statreportcb)(__G__ UZ_ST_IN_PROGRESS, G.zipfn, G.filename, NULL))
return IZ_CTRLC; /* cancel operation by user request */
#endif
if (uO.tflag || size == 0L) /* testing or nothing to write: all done */
return PK_OK;
if (G.disk_full)
return PK_DISK; /* 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 (!G.pInfo->textmode) { /* write raw binary data */
/* 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.
*/
#ifdef DLL
if (G.redirect_data) {
#ifdef NO_SLIDE_REDIR
if (writeToMemory(__G__ rawbuf, (extent)size)) return PK_ERR;
#else
writeToMemory(__G__ rawbuf, (extent)size);
#endif
} else
#endif
if (!uO.cflag && WriteError(rawbuf, size, G.outfile))
return disk_error(__G);
else if (uO.cflag && (*G.message)((zvoid *)&G, rawbuf, size, 0))
return PK_OK;
} else { /* textmode: aflag is true */
if (unshrink) {
/* rawbuf = outbuf */
transbuf = G.outbuf2;
#if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
transbufsiz = TRANSBUFSIZ;
#endif
} else {
/* rawbuf = slide */
transbuf = G.outbuf;
#if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
transbufsiz = OUTBUFSIZ;
Trace((stderr, "\ntransbufsiz = OUTBUFSIZ = %u\n",
(unsigned)OUTBUFSIZ));
#endif
}
if (G.newfile) {
#ifdef VMS_TEXT_CONV
if (G.pInfo->hostnum == VMS_ && G.extra_field &&
is_vms_varlen_txt(__G__ G.extra_field,
G.lrec.extra_field_length))
G.VMS_line_state = 0; /* 0: ready to read line length */
else
G.VMS_line_state = -1; /* -1: don't treat as VMS text */
#endif
G.didCRlast = FALSE; /* no previous buffers written */
G.newfile = FALSE;
}
#ifdef VMS_TEXT_CONV
if (G.VMS_line_state >= 0)
{
p = rawbuf;
q = transbuf;
while ((extent)(p-rawbuf) < (extent)size) {
switch (G.VMS_line_state) {
/* 0: ready to read line length */
case 0:
G.VMS_line_length = 0;
if ((extent)(p-rawbuf) == (extent)size-1) {
/* last char */
G.VMS_line_length = (unsigned)(*p++);
G.VMS_line_state = 1;
} else {
G.VMS_line_length = makeword(p);
p += 2;
G.VMS_line_state = 2;
}
G.VMS_line_pad =
((G.VMS_line_length & 1) != 0); /* odd */
break;
/* 1: read one byte of length, need second */
case 1:
G.VMS_line_length += ((unsigned)(*p++) << 8);
G.VMS_line_state = 2;
break;
/* 2: ready to read VMS_line_length chars */
case 2:
{
extent remaining = (extent)size+(rawbuf-p);
extent outroom;
if (G.VMS_line_length < remaining) {
remaining = G.VMS_line_length;
G.VMS_line_state = 3;
}
outroom = transbuf+(extent)transbufsiz-q;
if (remaining >= outroom) {
remaining -= outroom;
for (;outroom > 0; p++, outroom--)
*q++ = native(*p);
#ifdef DLL
if (G.redirect_data) {
if (writeToMemory(__G__ transbuf,
(extent)(q-transbuf))) return PK_ERR;
} else
#endif
if (!uO.cflag && WriteError(transbuf,
(extent)(q-transbuf), G.outfile))
return disk_error(__G);
else if (uO.cflag && (*G.message)((zvoid *)&G,
transbuf, (ulg)(q-transbuf), 0))
return PK_OK;
q = transbuf;
/* fall through to normal case */
}
G.VMS_line_length -= remaining;
for (;remaining > 0; p++, remaining--)
*q++ = native(*p);
}
break;
/* 3: ready to PutNativeEOL */
case 3:
if (q > transbuf+(extent)transbufsiz-lenEOL) {
#ifdef DLL
if (G.redirect_data) {
if (writeToMemory(__G__ transbuf,
(extent)(q-transbuf))) return PK_ERR;
} else
#endif
if (!uO.cflag &&
WriteError(transbuf, (extent)(q-transbuf),
G.outfile))
return disk_error(__G);
else if (uO.cflag && (*G.message)((zvoid *)&G,
transbuf, (ulg)(q-transbuf), 0))
return PK_OK;
q = transbuf;
}
PutNativeEOL
G.VMS_line_state = G.VMS_line_pad ? 4 : 0;
break;
/* 4: ready to read pad byte */
case 4:
++p;
G.VMS_line_state = 0;
break;
}
} /* end while */
} else
#endif /* VMS_TEXT_CONV */