forked from KermitProject/ckermit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckcfns.c
7248 lines (6716 loc) · 212 KB
/
ckcfns.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
char *fnsv = "C-Kermit functions, 10.0.243, 23 Sep 2022";
char *nm[] = { "Disabled", "Local only", "Remote only", "Enabled" };
/* C K C F N S -- System-independent Kermit protocol support functions. */
/* ...Part 1 (others moved to ckcfn2,3 to make this module smaller) */
/*
Author: Frank da Cruz <[email protected]>,
Columbia University Academic Information Systems, New York City (1974-2011)
The Kermit Project, Bronx NY (2011-????)
Copyright (C) 1985, 2022,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
Last update: Fri Sep 23 15:27:55 2022
(CR -> CK_CR and space unsigned long -> CK_OFF_T)
*/
/*
System-dependent primitives defined in:
ck?tio.c -- terminal (communications) i/o
cx?fio.c -- file i/o, directory structure
*/
#include "ckcsym.h" /* Needed for Stratus VOS */
#include "ckcasc.h" /* ASCII symbols */
#include "ckcdeb.h" /* Debug formats, typedefs, etc. */
#include "ckcker.h" /* Symbol definitions for Kermit */
#include "ckcxla.h" /* Character set symbols */
#include "ckcnet.h" /* VMS definition of TCPSOCKET */
#ifdef OS2
#ifdef OS2ONLY
#include <os2.h>
#endif /* OS2ONLY */
#include "ckocon.h"
#endif /* OS2 */
int docrc = 0; /* Accumulate CRC for \v(crc16) */
long crc16 = 0L; /* File CRC = \v(crc16) */
int gnferror = 0; /* gnfile() failure reason */
extern CHAR feol;
extern int byteorder, xflg, what, fmask, cxseen, czseen, nscanfile, sysindex;
extern int xcmdsrc, dispos, matchfifo;
extern int inserver;
extern int nolinks;
#ifdef VMSORUNIX
extern int zgfs_dir;
#ifdef CKSYMLINK
extern int zgfs_link;
#endif /* CKSYMLINK */
#endif /* VMSORUNIX */
#ifndef NOXFER
#ifndef NOICP
#ifndef NOSPL
extern char * clcmds;
extern int haveurl;
#ifdef CK_APC
extern int apcactive, adl_ask;
#endif /* CK_APC */
#endif /* NOSPL */
#endif /* NOICP */
extern int remfile;
/* (move these prototypes to the appropriate .h files...) */
#ifdef COMMENT
/* Not used */
#ifdef VMS
_PROTOTYP( int getvnum, (char *) );
#endif /* VMS */
#endif /* COMMENT */
_PROTOTYP( static int bgetpkt, (int) );
#ifndef NOCSETS
_PROTOTYP( int lookup, (struct keytab[], char *, int, int *) );
#endif /* NOCSETS */
#ifndef NOSPL
_PROTOTYP( int zzstring, (char *, char **, int *) );
#endif /* NOSPL */
#ifdef OS2
#include <io.h>
#ifdef OS2ONLY
#include <os2.h>
#endif /* OS2ONLY */
#endif /* OS2 */
/* Externals from ckcmai.c */
extern int srvcdmsg, srvidl, idletmo;
extern char * cdmsgfile[];
extern int spsiz, spmax, rpsiz, timint, srvtim, rtimo, npad, ebq, ebqflg,
rpt, rptq, rptflg, capas, keep, fncact, pkttim, autopar, spsizr, xitsta;
extern int pktnum, bctr, bctu, bctf, bctl, clfils, sbufnum, protocol,
size, osize, spktl, nfils, ckwarn, timef, spsizf, sndtyp, rcvtyp, success;
extern int parity, turn, network, whatru, fsecs, justone, slostart,
ckdelay, displa, mypadn, moving, recursive, nettype;
extern int rpsizf;
extern long filcnt;
extern CK_OFF_T
tfc, fsize, sendstart, rs_len, flci, flco, tlci, tlco, calibrate;
extern long filrej, oldcps, cps, peakcps, ccu, ccp, filestatus;
extern int fblksiz, frecl, frecfm, forg, fcctrl, fdispla, skipbup;
extern int spackets, rpackets, timeouts, retrans, crunched, wmax, wcur;
extern int hcflg, binary, fncnv, b_save, f_save, server;
extern int nakstate, discard, rejection, local, xfermode, interrupted;
extern int rq, rqf, sq, wslots, wslotn, wslotr, winlo, urpsiz, rln;
extern int fnspath, fnrpath, eofmethod, diractive, whatru2, wearealike;
extern int atcapr, atcapb, atcapu;
extern int lpcapr, lpcapb, lpcapu;
extern int swcapr, swcapb, swcapu;
extern int lscapr, lscapb, lscapu;
extern int rscapr, rscapb, rscapu;
extern int rptena, rptmin;
extern int sseqtbl[];
extern int numerrs, nzxopts;
extern long rptn;
extern int maxtry;
extern int stdouf;
extern int sendmode;
extern int carrier, ttprty;
extern int g_fnrpath;
#ifdef TCPSOCKET
extern int ttnproto;
#endif /* TCPSOCKET */
#ifndef NOSPL
extern int sndxin, sndxhi, sndxlo;
#endif /* NOSPL */
extern int g_binary, g_fncnv;
#ifdef GFTIMER
extern CKFLOAT fpfsecs;
#endif /* GFTIMER */
#ifdef OS2
extern struct zattr iattr;
#endif /* OS2 */
#ifdef PIPESEND
extern int usepipes;
#endif /* PIPESEND */
extern int pipesend;
#ifdef STREAMING
extern int streamrq, streaming, streamed, streamok;
#endif /* STREAMING */
extern int reliable, clearrq, cleared, urclear;
extern int
atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso;
extern int bigsbsiz, bigrbsiz;
extern char *versio;
extern char *filefile;
extern char whoareu[], * cksysid;
#ifndef NOSERVER
extern int ngetpath;
extern char * getpath[];
extern int fromgetpath;
#endif /* NOSERVER */
#ifdef CK_LOGIN
extern int isguest;
#endif /* CK_LOGIN */
extern int srvcmdlen;
extern CHAR *srvcmd, * epktmsg;
extern CHAR padch, mypadc, eol, seol, ctlq, myctlq, sstate, myrptq;
extern CHAR *data, padbuf[], stchr, mystch;
extern CHAR *srvptr;
extern CHAR *rdatap;
extern char *cmarg, *cmarg2, **cmlist, filnam[], ofilnam[];
extern char *rfspec, *prfspec, *rrfspec, *prrfspec, *sfspec, *psfspec, *rfspec;
extern char fspec[];
extern int fspeclen;
#ifndef NOMSEND
extern struct filelist * filehead, * filenext;
extern int addlist;
#endif /* NOMSEND */
_PROTOTYP( int lslook, (unsigned int b) ); /* Locking Shift Lookahead */
_PROTOTYP( int szeof, (CHAR *s) );
_PROTOTYP( VOID fnlist, (void) );
#endif /* NOXFER */
extern CK_OFF_T ffc;
/* Character set Translation */
#ifndef NOCSETS
extern int tcharset, fcharset, dcset7, dcset8;
extern int fcs_save, tcs_save;
extern int ntcsets, xlatype, cseqtab[];
extern struct csinfo tcsinfo[], fcsinfo[];
extern int r_cset, s_cset, afcset[];
#ifdef UNICODE
extern int ucsorder, fileorder;
#endif /* UNICODE */
_PROTOTYP( CHAR ident, (CHAR) ); /* Identity translation function */
/* Arrays of and pointers to character translation functions */
#ifdef CK_ANSIC
extern CHAR (*rx)(CHAR); /* Pointer to input character translation function */
extern CHAR (*sx)(CHAR); /* Pointer to output character translation function */
extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Byte-to-Byte Send */
extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Byte-to-Byte Recv */
#ifdef UNICODE
extern int (*xut)(USHORT); /* Translation function UCS to TCS */
extern int (*xuf)(USHORT); /* Translation function UCS to FCS */
extern USHORT (*xtu)(CHAR); /* Translation function TCS to UCS */
extern USHORT (*xfu)(CHAR); /* Translation function FCS to UCS */
#endif /* UNICODE */
#else /* The same declarations again for non-ANSI comilers... */
extern CHAR (*rx)();
extern CHAR (*sx)();
extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();
extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();
#ifdef UNICODE
extern int (*xut)();
extern int (*xuf)();
extern USHORT (*xtu)();
extern USHORT (*xfu)();
#endif /* UNICODE */
#endif /* CK_ANSIC */
#endif /* NOCSETS */
/* (PWP) external def. of things used in buffered file input and output */
#ifdef DYNAMIC
extern char *zinbuffer, *zoutbuffer;
#else
extern char zinbuffer[], zoutbuffer[];
#endif /* DYNAMIC */
extern char *zinptr, *zoutptr;
extern int zincnt, zoutcnt, zobufsize, xfrxla;
extern long crcta[], crctb[]; /* CRC-16 generation tables */
extern int rseqtbl[]; /* Rec'd-packet sequence # table */
#ifndef NOXFER
/* Criteria used by gnfile()... */
char sndafter[19] = { NUL, NUL };
char sndbefore[19] = { NUL, NUL };
char sndnafter[19] = { NUL, NUL };
char sndnbefore[19] = { NUL, NUL };
char *sndexcept[NSNDEXCEPT] = { NULL, NULL };
char *rcvexcept[NSNDEXCEPT] = { NULL, NULL };
CK_OFF_T sndsmaller = (CK_OFF_T)-1;
CK_OFF_T sndlarger = (CK_OFF_T)-1;
/* Variables defined in this module but shared by other modules. */
int xfrbel = 1;
char * ofperms = ""; /* Output file permissions */
int autopath = 0; /* SET RECEIVE PATHNAMES AUTO flag */
#ifdef CALIBRATE
#define CAL_O 3
#define CAL_M 253
int cal_j = 0;
CHAR
cal_a[] = {
16, 45, 98, 3, 52, 41, 14, 7, 76,165,122, 11,104, 77,166, 15,
160, 93, 18, 19,112, 85, 54, 23,232,213, 90, 27, 12, 81,126, 31,
4,205, 34, 35,144, 73,110, 39, 28,133,218, 43,156, 65,102, 47,
84, 61, 50, 51,208,117, 86, 55, 8,245, 74, 59, 44,125,222, 63,
80, 1,162, 67,116,105,206, 71,120, 9,250, 75, 88, 97, 6, 79,
100,221, 82, 83, 36, 89, 94, 87, 40, 21,106, 91,236,145,150, 95,
228, 33,130, 99,148,137,198,103,108,169, 42,107,184,129, 78,111,
0, 49,114,115, 32,121,254,119,172, 57,138,123,152,177, 22,127,
240,193, 2,131,176, 5, 38,135,204,229, 10,139,200,161,174,143,
128, 17,146,147, 68,153, 30,151, 72,217,170,155, 24,209, 62,159,
64,225,194,163,244,201, 70,167,216,197,234,171,188,109,230,175,
212,113,178,179,132,185,190,183,136,249,202,187, 92,241,118,191,
48,237, 66,195, 96,233,142,199,248, 37, 58,203, 60, 13,134,207,
20, 29,210,211,164,149,182,215,220, 25, 26,219,124,157,246,223,
180,141,226,227,192,101,238,231, 56, 69,154,235,252,173, 46,239,
224,253,242,243,196, 53,214,247,168,181,186,251,140,189,158,255
};
#endif /* CALIBRATE */
char * rf_err = "Error receiving file"; /* rcvfil() error message */
#ifdef CK_SPEED
short ctlp[256] = { /* Control-Prefix table */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* C0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* G0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, /* DEL */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* C1 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* G1 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 /* 255 */
};
#endif /* CK_SPEED */
int sndsrc; /* Flag for where to get names of files to send: */
/* -1: znext() function */
/* 0: stdin */
/* >0: list in cmlist or other list */
/* -9: calibrate */
int memstr; /* Flag for input from memory string */
int funcstr; /* Flag for input from function */
int bestlen = 0;
int maxsend = 0;
int gnf_binary = 0; /* Prevailing xfer mode for gnfile */
#ifdef pdp11
#define MYINITLEN 32
#else
#define MYINITLEN 100
#endif /* pdp11 */
CHAR myinit[MYINITLEN]; /* Copy of my Send-Init data */
/* Variables local to this module */
#ifdef TLOG
#ifndef XYZ_INTERNAL
static
#endif /* XYZ_INTERNAL */
char *fncnam[] = {
"rename", "replace", "backup", "append", "discard", "ask",
"update", "dates-differ", ""
};
#endif /* TLOG */
static char *memptr; /* Pointer for memory strings */
#ifdef VMS
extern int batch;
#else
extern int backgrd;
#endif /* VMS */
#ifdef CK_CTRLZ
static int lastchar = 0;
#endif /* CK_CTRLZ */
#ifdef CK_ANSIC
static int (*funcptr)(void); /* Pointer for function strings */
#else
static int (*funcptr)();
#endif /* CK_ANSIC */
#ifdef pdp11
#define CMDSTRL 50
static char cmdstr[50]; /* System command string. */
#else
#ifdef BIGBUFOK
#define CMDSTRL 6144
#else
#define CMDSTRL 1024
#endif /* BIGBUFOK */
static char cmdstr[CMDSTRL+1];
#endif /* pdp11 */
static int drain; /* For draining stacked-up ACKs. */
static int first; /* Flag for first char from input */
static CHAR t; /* Current character */
#ifdef COMMENT
static CHAR next; /* Next character */
#endif /* COMMENT */
static int ebqsent = 0; /* 8th-bit prefix bid that I sent */
static int lsstate = 0; /* Locking shift state */
static int lsquote = 0; /* Locking shift quote */
extern int quiet;
/* E N C S T R -- Encode a string from memory. */
/*
Call this instead of getpkt() if source is a string, rather than a file.
Note: Character set translation is never done in this case.
*/
#ifdef COMMENT
#define ENCBUFL 200
#ifndef pdp11
CHAR encbuf[ENCBUFL];
#else
/* This is gross, but the pdp11 root segment is out of space */
/* Will allocate it in ckuusr.c. */
extern CHAR encbuf[];
#endif /* pdp11 */
#endif /* COMMENT */
/*
Encode packet data from a string in memory rather than from a file; thus
this routine is used for administrative packets, not with Data packets.
Returns the length of the encoded string on success, -1 if the string could
not be completely encoded into the currently negotiated data field length.
*/
int
#ifdef CK_ANSIC
encstr(CHAR *s)
#else /* CK_ANSIC */
encstr(s) CHAR* s;
#endif /* CK_ANSIC */
{
/*
Recoded 30 Jul 1994 to use the regular data buffer and the negotiated
maximum packet size. Previously we were limited to the length of encbuf[].
Also, to return a failure code if the entire encoded string would not fit.
Modified 14 Jul 1998 to return length of encoded string.
Modified 18 Oct 2021 to not truncate filename in F packet.
*/
int m, rc, slen; char *p;
/* data is a pointer to send-packet data declared in ckcmai.c */
if (!data) { /* Watch out for null pointers. */
debug(F100,"SERIOUS ERROR: encstr data == NULL","",0);
return(-1);
}
if (!s) s = (CHAR *)""; /* Our argument string */
slen = strlen((char *)s); /* Length of source string */
debug(F111,"encstr",s,slen);
rc = 0; /* Return code. */
m = memstr; p = memptr; /* Save these. */
memptr = (char *)s; /* Point to the string. */
/* debug(F101,"encstr memptr 1","",memptr); */
memstr = 1; /* Flag memory string as source. */
first = 1; /* Initialize character lookahead. */
*data = NUL; /* In case s is empty */
debug(F101,"encstr pktnum","",pktnum); /* (apparently not reliable) */
debug(F101,"encstr spsiz","",spsiz);
#ifndef COMMENT
/*
28 October 2021: If the user gives a command like SET SEND PACKET-LENGTH 10,
the filename will be truncated if it's longer than that. Now we use the
packet size our Kermit partner is willing to accept (rpsiz). This can
still be truncated but much less likely because filenames tend to be
shorter then 90 bytes.
*/
debug(F101,"encstr rpsiz","",rpsiz);
debug(F101,"encstr urpsiz","",urpsiz);
rc = getpkt(rpsiz,0); /* Fill a packet from the string. */
#else
rc = getpkt(spsiz,0); /* (this can truncate) */
#endif /* COMMENT */
debug(F101,"encstr getpkt rc","",rc);
if (rc > -1 && memptr < (char *)(s + slen)) { /* Means we didn't encode */
rc = -1; /* the whole string. */
debug(F101,"encstr string too big","",size);
}
debug(F101,"encstr getpkt rc","",rc);
memstr = m; /* Restore memory string flag */
memptr = p; /* and pointer */
first = 1; /* Put this back as we found it. */
return(rc);
}
/* Output functions passed to 'decode': */
int /* Put character in server command buffer */
#ifdef CK_ANSIC
putsrv(char c)
#else
putsrv(c) register char c;
#endif /* CK_ANSIC */
/* putsrv */ {
*srvptr++ = c;
*srvptr = '\0'; /* Make sure buffer is null-terminated */
return(0);
}
int /* Output character to console. */
#ifdef CK_ANSIC
puttrm(char c)
#else
puttrm(c) register char c;
#endif /* CK_ANSIC */
/* puttrm */ {
extern int rcdactive;
#ifndef NOSPL
extern char * qbufp; /* If REMOTE QUERY active, */
extern int query, qbufn; /* also store response in */
if (query && qbufn++ < 1024) { /* query buffer. */
*qbufp++ = c;
*qbufp = NUL;
}
if (!query || !xcmdsrc)
#endif /* NOSPL */
/*
This routine is used (among other things) for printing the server's answer
to a REMOTE command. But REMOTE CD is special because it isn't really
asking for an answer from the server. Thus some people want to suppress
the confirmation message (e.g. when the server sends back the actual path
of the directory CD'd to), and expect to be able to do this with SET QUIET
ON. But they would not want SET QUIET ON to suppress the other server
replies, which are pointless without their answers. Thus the "rcdactive"
flag (REMOTE CD command is active). Thu Oct 10 16:38:21 2002
*/
if (!(quiet && rcdactive)) /* gross, yuk */
conoc(c);
return(0);
}
#endif /* NOXFER */
int /* Output char to file. */
#ifdef CK_ANSIC
putmfil(char c) /* Just like putfil but to ZMFILE */
#else /* rather than ZOFILE... */
putmfil(c) register char c;
#endif /* CK_ANSIC */
/* putmfil */ {
debug(F000,"putfil","",c);
if (zchout(ZMFILE, (char) (c & fmask)) < 0) {
czseen = 1;
debug(F101,"putfil zchout write error, setting czseen","",1);
return(-1);
}
return(0);
}
int /* Output char to nowhere. */
#ifdef CK_ANSIC
putnowhere(char c)
#else
putnowhere(c) register char c;
#endif /* CK_ANSIC */
/* putnowhere */ {
return(0);
}
int /* Output char to file. */
#ifdef CK_ANSIC
putfil(char c)
#else
putfil(c) register char c;
#endif /* CK_ANSIC */
/* putfil */ {
debug(F000,"putfil","",c);
if (zchout(ZOFILE, (char) (c & fmask)) < 0) {
czseen = 1; /* If write error... */
debug(F101,"putfil zchout write error, setting czseen","",1);
return(-1);
}
return(0);
}
/*
The following function is a wrapper for putfil(). The only reason for its
existence is to be passed as a function pointer to decode(), which treats
putfil() itself specially -- bypassing it and using an internal macro
instead to speed things up. Use zputfil() instead of putfil() in cases where
we do not want this to happen, e.g. when we need to send output to the file
with a mixture of zchout() and zsout()/zsoutl() calls (as is the case with
incoming short-form REMOTE command replies redirected to a file), which would
otherwise result in data written to the file out of order.
*/
int
#ifdef CK_ANSIC
zputfil(char c)
#else
zputfil(c) register char c;
#endif /* CK_ANSIC */
/* zputfil */ {
return(putfil(c));
}
#ifndef NOXFER
/* D E C O D E -- Kermit packet decoding procedure */
/*
Call with string to be decoded and an output function.
Returns 0 on success, -1 on failure (e.g. disk full).
This is the "inner loop" when receiving files, and must be coded as
efficiently as possible. Note some potential problems: if a packet
is badly formed, having a prefixed sequence ending prematurely, this
function, as coded, could read past the end of the packet. This has
never happened, thus the additional (time-consuming) tests have not
been added.
*/
static CHAR *xdbuf; /* Global version of decode()'s buffer pointer */
/* for use by translation functions. */
/* Function for pushing a character onto decode()'s input stream. */
VOID
#ifdef CK_ANSIC
zdstuff(CHAR c)
#else
zdstuff(c) CHAR c;
#endif /* CK_ANSIC */
/* zdstuff */ {
xdbuf--; /* Back up the pointer. */
*xdbuf = c; /* Stuff the character. */
}
#ifdef CKTUNING
/*
Trimmed-down packet decoder for binary-mode no-parity transfers.
decode() is the full version.
*/
int
#ifdef CK_ANSIC
bdecode(CHAR *buf, int (*fn)(char))
#else
bdecode(buf,fn) register CHAR *buf; register int (*fn)();
#endif /* CK_ANSIC */
/* bdecode */ {
register unsigned int a, a7; /* Various copies of current char */
int ccpflg; /* For Ctrl-unprefixing stats */
int t; /* Int version of character */
int len;
long z; /* For CRC calculation */
CHAR c; /* Current character */
if (!binary || parity || fn != putfil) /* JUST IN CASE */
return(decode(buf,fn,1));
debug(F100,"BDECODE","",0);
xdbuf = buf; /* Global copy of source pointer. */
len = rln; /* Number of bytes in data field */
while (len > 0) {
a = *xdbuf++ & 0xff; /* Get next character */
len--;
rpt = 0; /* Initialize repeat count. */
if (a == rptq && rptflg) { /* Got a repeat prefix? */
rpt = xunchar(*xdbuf++ & 0xFF); /* Yes, get the repeat count, */
rptn += rpt;
a = *xdbuf++ & 0xFF; /* and get the prefixed character. */
len -= 2;
}
ccpflg = 0; /* Control prefix flag. */
if (a == ctlq) { /* If control prefix, */
a = *xdbuf++ & 0xFF; /* get its operand */
len--;
a7 = a & 0x7F; /* and its low 7 bits. */
if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') { /* Controllify */
a = ctl(a); /* if in control range. */
a7 = a & 0x7F;
ccpflg = 1; /* Note that we did this */
ccp++; /* Count for stats */
}
} else a7 = a & 0x7f; /* Not control quote */
if (a7 < 32 || a7 == 127) /* A bare control character? */
if (!ccpflg) ccu++; /* Count it */
if (!rpt) rpt = 1;
for (; rpt > 0; rpt--) { /* Output the char RPT times */
#ifdef CALIBRATE
if (calibrate) {
ffc++;
continue;
}
#endif /* CALIBRATE */
#ifdef OS2
if (xflg && !remfile) { /* Write to virtual screen */
char _a;
_a = a & fmask;
t = conoc(_a);
if (t < 1)
t = -1;
} else
#endif /* OS2 */
t = zmchout(a & fmask); /* zmchout is a macro */
if (t < 0) {
debug(F101,"bdecode write error - errno","",errno);
return(-1);
}
ffc++; /* Count the character */
if (docrc && !remfile) { /* Update file CRC */
c = a; /* Force conversion to unsigned char */
z = crc16 ^ (long)c;
crc16 = (crc16 >> 8) ^
(crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
}
}
#ifdef CK_CTRLZ
lastchar = a;
#endif /* CK_CTRLZ */
}
return(0);
}
#endif /* CKTUNING */
#endif /* NOXFER */
/* P N B Y T E -- Output next byte to file or other destination */
static CK_OFF_T offc = 0L;
static int
#ifdef CK_ANSIC
pnbyte(CHAR c, int (*fn)(char))
#else
pnbyte(c,fn) CHAR c; int (*fn)();
#endif /* CK_ANSIC */
/* pnbyte */ {
int rc;
long z;
#ifdef OS2
#ifndef NOXFER
if (xflg && !remfile) { /* Write to virtual screen */
char _a;
_a = c & fmask;
rc = conoc(_a);
if (rc < 1)
return(-1);
} else
#endif /* NOXFER */
#endif /* OS2 */
{
if (fn == putfil) { /* Execute output function */
rc = zmchout(c); /* to-file macro (fast) */
} else if (!fn) {
rc = putchar(c); /* to-screen macro (fast) */
} else {
rc = (*fn)(c); /* function call (not as fast) */
}
if (rc < 0)
return(rc);
}
/*
Both xgnbyte() and xpnbyte() increment ffc (the file byte counter).
During file transfer, only one of these functions is called. However,
the TRANSLATE command is likely to call them both. offc, therefore,
contains the output byte count, necessary for handling the UCS-2 BOM.
NOTE: It might be safe to just test for W_SEND, FTP or not.
*/
if ((what & (W_FTP|W_SEND)) != (W_FTP|W_SEND)) {
offc++; /* Count the byte */
ffc++; /* Count the byte */
}
#ifndef NOXFER
if (docrc && !xflg && !remfile) { /* Update file CRC */
z = crc16 ^ (long)c;
crc16 = (crc16 >> 8) ^
(crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);
}
#endif /* NOXFER */
return(1);
}
/*
X P N B Y T E -- Translate and put next byte to file.
Only for Unicode. Call with next untranslated byte from incoming
byte stream, which can be in any Transfer Character Set: UCS-2, UTF-8,
Latin-1, Latin-Hebrew, etc. Translates to the file character set and
writes bytes to the output file. Call with character to translate
as an int, plus the transfer character set (to translate from) and the
file character set (to translate to), or -1,0,0 to reset the UCS-2 byte
number (which should be done at the beginning of a file). If the transfer
(source) character-set is UCS-2, bytes MUST arrive in Big-Endian order.
Returns:
-1: On error
0: Nothing to write (mid-sequence)
>0: Number of bytes written.
*/
#ifdef KANJI
static int jstate = 0, jx = 0; /* For outputting JIS-7 */
static char jbuf[16] = { NUL, NUL };
#endif /* KANJI */
int
#ifdef CK_ANSIC
xpnbyte(int a, int tcs, int fcs, int (*fn)(char))
#else
xpnbyte(a,tcs,fcs,fn) int a, tcs, fcs; int (*fn)();
#endif /* CK_ANSIC */
/* xpnbyte */ {
#ifdef UNICODE
extern int ucsbom; /* Byte order */
#endif /* UNICODE */
/* CHAR c; */ /* Unsigned char worker */
static union ck_short uc, eu, sj; /* UCS-2, EUC, and Shift-JIS workers */
USHORT ch; /* ditto... */
USHORT * us = NULL; /* ditto... */
int c7, rc, haveuc = 0; /* Return code and UCS-2 flag */
int utferror = 0; /* UTF-8 error */
static int bn = 0; /* UCS-2 byte number */
int swapping = 0; /* Swapping UCS bytes to output? */
/* swapping must be 0 or 1 */
if (a == -1 && (tcs | fcs) == 0) { /* Reset in case previous run */
bn = 0; /* left bn at 1... */
offc = (CK_OFF_T)0;
debug(F101,"xpnbyte RESET","",bn);
return(0);
}
debug(F001,"xpnbyte a","",a);
#ifdef UNICODE
/*
byteorder = hardware byte order of this machine.
ucsorder = override by SET FILE UCS BYTE-ORDER command.
fileorder = byte order of UCS-2 input file detected from BOM.
swapping applies only when output charset is UCS-2.
*/
if (ucsorder != 1 && ucsorder != 0) /* Also just in case... */
ucsorder = byteorder;
if ((byteorder && !ucsorder) || (!byteorder && fileorder))
swapping = 1; /* Swapping bytes to output */
#ifdef COMMENT
debug(F101,"xpnbyte ucsorder","",ucsorder);
debug(F101,"xpnbyte swapping","",swapping);
#endif /* COMMENT */
if (tcs == TC_UTF8) { /* 'a' is from a UTF-8 stream */
ch = a;
if (fcs == TC_UTF8) /* Output is UTF-8 too */
return(pnbyte(ch,fn)); /* so just copy. */
rc = utf8_to_ucs2(ch,&us); /* Otherwise convert to UCS-2 */
if (rc == 0) { /* Done with this sequence */
uc.x_short = *us; /* We have a Unicode */
haveuc = 1;
} else if (rc < 0) { /* Error */
debug(F101,"xpnbyte UTF-8 conversion error","",rc);
haveuc = 1; /* Replace by U+FFFD */
uc.x_short = *us;
utferror = 1;
} else /* Sequence incomplete */
return(0);
} else if (tcs == TC_UCS2) { /* 'a' is UCS-2 */
/* Here we have incoming UCS-2 in guaranteed Big Endian order */
/* so we must exchange bytes if local machine is Little Endian. */
switch (bn) { /* Which byte? */
case 0: /* High... */
uc.x_char[byteorder] = (unsigned)a & 0xff;
bn++;
return(0); /* Wait for next */
case 1: /* Low... */
uc.x_char[1-byteorder] = (unsigned)a & 0xff;
bn = 0; /* Done with sequence */
haveuc = 1; /* Have a Unicode */
}
} else
#endif /* UNICODE */
#ifdef KANJI /* Whether UNICODE is defined or not */
if (tcs == TC_JEUC) { /* Incoming Japanese EUC */
int bad = 0;
static int kanji = 0; /* Flags set in case 0 for case 1 */
static int kana = 0;
switch (bn) { /* Byte number */
case 0: /* Byte 0 */
eu.x_short = 0;
if ((a & 0x80) == 0) {
sj.x_short = (unsigned)a & 0xff; /* Single byte */
kanji = kana = 0;
} else { /* Double byte */
c7 = a & 0x7f;
if (c7 > 0x20 && c7 < 0x7f) { /* Kanji */
eu.x_char[byteorder] = (CHAR) a; /* Store first byte */
bn++; /* Set up for second byte */
kanji = 1;
kana = 0;
return(0);
} else if (a == 0x8e) { /* SS2 -- Katakana prefix */
eu.x_char[byteorder] = (CHAR) a; /* Save it */
bn++;
kana = 1;
kanji = 0;
return(0);
} else {
bad++;
}
}
break;
case 1: /* Byte 1 */
bn = 0;
if (kanji) {
eu.x_char[1-byteorder] = (CHAR) a;
sj.x_short = eu_to_sj(eu.x_short);
break;
} else if (kana) {
sj.x_short = (CHAR) (a | 0x80);
break;
} else { /* (shouldn't happen) */
bad++;
}
}
/* Come here with one Shift-JIS character */
#ifdef UNICODE
if (bad) {
uc.x_short = 0xfffd;
} else {
uc.x_short = sj_to_un(sj.x_short); /* Convert to Unicode */
}
haveuc = 1;
#endif /* UNICODE */
} else
#endif /* KANJI */
#ifdef UNICODE
uc.x_short = (unsigned)a & 0xff; /* Latin-1 or whatever... */
/* Come here with uc = the character to be translated. */
/* If (haveuc) it's UCS-2 in native order, otherwise it's a byte. */
debug(F101,"xpnbyte haveuc","",haveuc);
if (haveuc) { /* If we have a Unicode... */
debug(F001,"xpnbyte uc.x_short","[A]",uc.x_short);
debug(F101,"xpnbyte feol","",feol);
if (what & W_XFER) { /* If transferring a file */
if (feol && uc.x_short == CK_CR) { /* handle eol conversion. */
return(0);
} else if (feol && uc.x_short == LF) {
uc.x_short = feol;
}
}
debug(F001,"xpnbyte uc.x_short","[B]",uc.x_short);
if (fcs == FC_UCS2) { /* And FCS is UCS-2 */
/* Write out the bytes in the appropriate byte order */
int count = 0;
#ifndef IKSDONLY
#ifdef OS2
extern int k95stdout,wherex[],wherey[];
extern unsigned char colorcmd;
union {
USHORT ucs2;
UCHAR bytes[2];
} output;
#endif /* OS2 */
#endif /* IKSDONLY */
if (!offc && ucsbom) { /* Beginning of file? */
#ifndef IKSDONLY
#ifdef OS2
if (fn == NULL && !k95stdout && !inserver) {
offc++;
#ifdef COMMENT
/* Don't print the BOM to the display */
output.bytes[0] = (!ucsorder ? 0xff : 0xfe);
output.bytes[1] = (!ucsorder ? 0xfe : 0xff);
VscrnWrtUCS2StrAtt(VCMD,
&output.ucs2,
1,
wherey[VCMD],
wherex[VCMD],
&colorcmd
);
#endif /* COMMENT */
} else
#endif /* OS2 */
#endif /* IKSDONLY */
{
if ((rc = pnbyte((ucsorder ? 0xff : 0xfe),fn)) < 0)
return(rc); /* BOM */
if ((rc = pnbyte((ucsorder ? 0xfe : 0xff),fn)) < 0)
return(rc);
}
count += 2;
}
if (utferror) {
#ifndef IKSDONLY
#ifdef OS2
if (fn == NULL && !k95stdout && !inserver) {
offc++;
output.bytes[0] = (!ucsorder ? 0xfd : 0xff);
output.bytes[1] = (!ucsorder ? 0xff : 0xfd);
VscrnWrtUCS2StrAtt(VCMD,
&output.ucs2,
1,
wherey[VCMD],
wherex[VCMD],
&colorcmd
);
} else
#endif /* OS2 */
#endif /* IKSDONLY */
{
if ((rc = pnbyte((ucsorder ? 0xfd : 0xff),fn)) < 0)
return(rc);