File: qprint.c

package info (click to toggle)
qprint 1.1.dfsg.2-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 592 kB
  • sloc: ansic: 715; sh: 152; makefile: 151
file content (985 lines) | stat: -rw-r--r-- 13,836 bytes parent folder | download | duplicates (2)
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
/*2:*/
#line 70 "./qprint.w"

#include "config.h"                   

#define REVDATE "16th December 2014" \

#define TRUE 1
#define FALSE 0
#define LINELEN 72
#define MAXINLINE 256 \

#define ASCII_HORIZONTAL_TAB 9
#define ASCII_LINE_FEED 10
#define ASCII_CARRIAGE_RETURN 13
#define ASCII_SPACE 32
#define ASCII_0 48
#define ASCII_EQUAL_SIGN 61
#define ASCII_A 65
#define ASCII_LOWER_CASE_A 97 \
 \

#define Character_is_printable_ISO_8859(c) (((((c) >=0x20) &&((c) <=0x7E) ) ||((c) >=0xA1) ) )  \


#line 73 "./qprint.w"


/*4:*/
#line 100 "./qprint.w"

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#ifdef HAVE_STRING_H
#include <string.h> 
#else
#ifdef HAVE_STRINGS_H
#include <strings.h> 
#endif
#endif
#ifdef HAVE_GETOPT
#ifdef HAVE_UNISTD_H
#include <unistd.h> 
#endif
#else
#include "getopt.h"     
#endif

/*:4*/
#line 75 "./qprint.w"

/*5:*/
#line 122 "./qprint.w"

#ifdef _WIN32
#define FORCE_BINARY_IO
#include <io.h> 
#include <fcntl.h> 
#endif

/*:5*/
#line 76 "./qprint.w"

/*6:*/
#line 134 "./qprint.w"

typedef unsigned char byte;

static FILE*fi;
static FILE*fo;

/*:6*//*13:*/
#line 240 "./qprint.w"

typedef enum{Rule_1,Rule_2,Rule_3,Rule_4,Rule_EBCDIC}
character_encoding_rule;
static character_encoding_rule character_class[256];

/*:13*//*22:*/
#line 409 "./qprint.w"

static int current_line_length= 0;

/*:22*//*23:*/
#line 425 "./qprint.w"

static int pending_white_space= 0;

/*:23*//*32:*/
#line 608 "./qprint.w"

#if (SIZEOF_UNSIGNED_LONG == 8) || (SIZEOF_UNSIGNED_LONG_LONG == 0)





typedef unsigned long file_address_type;
#define FILE_ADDRESS_FORMAT_LENGTH "l"
#else




typedef unsigned long long file_address_type;
#define FILE_ADDRESS_FORMAT_LENGTH "ll"
#endif

/*:32*//*33:*/
#line 637 "./qprint.w"

static file_address_type decode_input_stream_position= 0;
static long decode_errors= 0;

/*:33*//*44:*/
#line 813 "./qprint.w"

static int decoding= FALSE;
static int encoding= FALSE;
static int binary_input= FALSE;
static int errcheck= TRUE;
static int EBCDIC_out= FALSE;
static int paranoid= FALSE;

/*:44*/
#line 77 "./qprint.w"

/*34:*/
#line 645 "./qprint.w"

static int read_decode_character(void);

/*:34*//*36:*/
#line 667 "./qprint.w"

static int hex_to_nybble(int ch);

/*:36*/
#line 78 "./qprint.w"


/*:2*//*8:*/
#line 149 "./qprint.w"

static void output_line_break(void)
{
static char line_break[3]= {ASCII_CARRIAGE_RETURN,ASCII_LINE_FEED,0};

fputs(line_break,fo);
current_line_length= 0;
}

/*:8*//*9:*/
#line 170 "./qprint.w"

static void check_line_length(int chars_required)
{
if((current_line_length+chars_required)>=(LINELEN-1)){
putc(ASCII_EQUAL_SIGN,fo);
output_line_break();
}
current_line_length+= chars_required;
}

/*:9*//*10:*/
#line 183 "./qprint.w"

static void emit_literally(int ch)
{
check_line_length(1);
putc(ch,fo);
}

/*:10*//*11:*/
#line 194 "./qprint.w"

static void emit_hex_encoded(int ch)
{
static char hex[16]= {ASCII_0,ASCII_0+1,ASCII_0+2,ASCII_0+3,
ASCII_0+4,ASCII_0+5,ASCII_0+6,
ASCII_0+7,ASCII_0+8,ASCII_0+9,
ASCII_A,ASCII_A+1,ASCII_A+2,ASCII_A+3,
ASCII_A+4,ASCII_A+5};

check_line_length(3);
putc(ASCII_EQUAL_SIGN,fo);
putc(hex[(ch>>4)&0xF],fo);
putc(hex[ch&0xF],fo);
}

/*:11*//*12:*/
#line 221 "./qprint.w"


static void encode(void)
{
int i,ch;

/*14:*/
#line 253 "./qprint.w"


/*15:*/
#line 265 "./qprint.w"

for(i= 0;i<=255;i++){
character_class[i]= Rule_1;
}

/*:15*/
#line 255 "./qprint.w"
;
/*16:*/
#line 273 "./qprint.w"

for(i= 33;i<=60;i++){
character_class[i]= Rule_2;
}

for(i= 62;i<=126;i++){
character_class[i]= Rule_2;
}

/*:16*/
#line 256 "./qprint.w"
;
/*17:*/
#line 285 "./qprint.w"

character_class[ASCII_HORIZONTAL_TAB]= Rule_3;
character_class[ASCII_SPACE]= Rule_3;

/*:17*/
#line 257 "./qprint.w"
;
/*18:*/
#line 295 "./qprint.w"

character_class[ASCII_LINE_FEED]= Rule_4;
character_class[ASCII_CARRIAGE_RETURN]= Rule_4;

/*:18*/
#line 258 "./qprint.w"
;
/*19:*/
#line 306 "./qprint.w"

character_class[33]= 
character_class[34]= 
character_class[35]= 
character_class[36]= 
character_class[64]= 
character_class[91]= 
character_class[92]= 
character_class[93]= 
character_class[94]= 
character_class[96]= 
character_class[123]= 
character_class[124]= 
character_class[125]= 
character_class[126]= Rule_EBCDIC;

/*:19*/
#line 259 "./qprint.w"
;

/*:14*/
#line 227 "./qprint.w"
;

while((ch= getc(fi))!=EOF){
/*20:*/
#line 331 "./qprint.w"

switch(character_class[ch]){
case Rule_1:
/*24:*/
#line 434 "./qprint.w"

if(pending_white_space!=0){
emit_literally(pending_white_space);
pending_white_space= 0;
}

/*:24*/
#line 334 "./qprint.w"
;
emit_hex_encoded(ch);
break;

case Rule_2:
/*24:*/
#line 434 "./qprint.w"

if(pending_white_space!=0){
emit_literally(pending_white_space);
pending_white_space= 0;
}

/*:24*/
#line 339 "./qprint.w"
;
if(paranoid){
emit_hex_encoded(ch);
}else{
emit_literally(ch);
}
break;

case Rule_3:
if(paranoid){
emit_hex_encoded(ch);
}else{
/*24:*/
#line 434 "./qprint.w"

if(pending_white_space!=0){
emit_literally(pending_white_space);
pending_white_space= 0;
}

/*:24*/
#line 351 "./qprint.w"
;
pending_white_space= ch;
}
break;

case Rule_4:
if(binary_input){




/*24:*/
#line 434 "./qprint.w"

if(pending_white_space!=0){
emit_literally(pending_white_space);
pending_white_space= 0;
}

/*:24*/
#line 362 "./qprint.w"
;
emit_hex_encoded(ch);
}else{
/*25:*/
#line 449 "./qprint.w"

{
int chn= getc(fi);

if(chn!=EOF){
if((chn==ASCII_LINE_FEED)||(chn==ASCII_CARRIAGE_RETURN)){
if(chn==ch){
ungetc(chn,fi);
}
}else{
ungetc(chn,fi);
}
}
}

/*:25*/
#line 365 "./qprint.w"
;



if(pending_white_space!=0){
emit_hex_encoded(pending_white_space);
pending_white_space= 0;
}
output_line_break();
}
break;

case Rule_EBCDIC:
/*24:*/
#line 434 "./qprint.w"

if(pending_white_space!=0){
emit_literally(pending_white_space);
pending_white_space= 0;
}

/*:24*/
#line 378 "./qprint.w"
;


if(EBCDIC_out||paranoid){
emit_hex_encoded(ch);
}else{
emit_literally(ch);
}
break;
}

/*:20*/
#line 230 "./qprint.w"
;
}
/*24:*/
#line 434 "./qprint.w"

if(pending_white_space!=0){
emit_literally(pending_white_space);
pending_white_space= 0;
}

/*:24*/
#line 232 "./qprint.w"
;
/*26:*/
#line 471 "./qprint.w"


if(current_line_length> 0){
putc(ASCII_EQUAL_SIGN,fo);
output_line_break();
}


/*:26*/
#line 233 "./qprint.w"
;
}

/*:12*//*21:*/
#line 395 "./qprint.w"

static int is_end_of_line_sequence(int ch)
{
if((ch==ASCII_CARRIAGE_RETURN)||(ch==ASCII_LINE_FEED)){
/*25:*/
#line 449 "./qprint.w"

{
int chn= getc(fi);

if(chn!=EOF){
if((chn==ASCII_LINE_FEED)||(chn==ASCII_CARRIAGE_RETURN)){
if(chn==ch){
ungetc(chn,fi);
}
}else{
ungetc(chn,fi);
}
}
}

/*:25*/
#line 399 "./qprint.w"
;
return TRUE;
}
return FALSE;
}

/*:21*//*28:*/
#line 487 "./qprint.w"


static void decode(void)
{
int ch,ch1,ch2;

while((ch= read_decode_character())!=EOF){
switch(ch){

case ASCII_EQUAL_SIGN:
/*29:*/
#line 529 "./qprint.w"

ch1= read_decode_character();
/*31:*/
#line 579 "./qprint.w"

while(/*30:*/
#line 566 "./qprint.w"

((ch1==ASCII_SPACE)||(ch1==ASCII_HORIZONTAL_TAB))

/*:30*/
#line 580 "./qprint.w"
){
ch1= read_decode_character();
if(is_end_of_line_sequence(ch1)){
break;
}
if(!/*30:*/
#line 566 "./qprint.w"

((ch1==ASCII_SPACE)||(ch1==ASCII_HORIZONTAL_TAB))

/*:30*/
#line 585 "./qprint.w"
){
if(ch1==EOF){
break;
}
/*39:*/
#line 747 "./qprint.w"

if(errcheck){
if(/*41:*/
#line 797 "./qprint.w"

('a'==0x61)

/*:41*/
#line 749 "./qprint.w"
&&
Character_is_printable_ISO_8859(ch1)){

fprintf(stderr,
"Error: invalid character \"%c\" in soft line break sequence at byte %"

FILE_ADDRESS_FORMAT_LENGTH

"u (0x%"

FILE_ADDRESS_FORMAT_LENGTH

"X) of input.\n",
ch1,decode_input_stream_position-1,
decode_input_stream_position-1);
}else{


fprintf(stderr,
"Error: invalid character \"0x%02X\" in soft line break sequence at byte %"

FILE_ADDRESS_FORMAT_LENGTH

"u (0x%"

FILE_ADDRESS_FORMAT_LENGTH

"X) of input.\n",
ch1,decode_input_stream_position-1,
decode_input_stream_position-1);
}
}

/*:39*/
#line 589 "./qprint.w"
;
decode_errors++;
ch1= ASCII_SPACE;
}
}

/*:31*/
#line 531 "./qprint.w"
;
if(ch1==EOF){
fprintf(stderr,"Error: unexpected end of file after soft line break sequence at byte %"

FILE_ADDRESS_FORMAT_LENGTH

"u (0x%"

FILE_ADDRESS_FORMAT_LENGTH

"X) of input.\n",
decode_input_stream_position-1,
decode_input_stream_position-1);
decode_errors++;
}
if(is_end_of_line_sequence(ch1)||(ch1==EOF)){
ch= EOF;
}else{
int n1,n2;

n1= hex_to_nybble(ch1);
ch2= read_decode_character();
n2= hex_to_nybble(ch2);
if(n1==EOF||n2==EOF){
/*38:*/
#line 701 "./qprint.w"

if(errcheck){
if(/*41:*/
#line 797 "./qprint.w"

('a'==0x61)

/*:41*/
#line 703 "./qprint.w"
&&
Character_is_printable_ISO_8859(ch1)&&
Character_is_printable_ISO_8859(ch2)){

fprintf(stderr,
"Error: bad equal sign escape \"=%c%c\" at byte %"

FILE_ADDRESS_FORMAT_LENGTH

"u (0x%"

FILE_ADDRESS_FORMAT_LENGTH

"X) of input.\n",
ch1,ch2,decode_input_stream_position-3,
decode_input_stream_position-3);
}else{


fprintf(stderr,
"Error: bad equal sign escape \"= 0x%02X 0x%02X\" at byte %"

FILE_ADDRESS_FORMAT_LENGTH

"u (0x%"

FILE_ADDRESS_FORMAT_LENGTH

"X) of input.\n",
ch1,ch2,decode_input_stream_position-3,
decode_input_stream_position-3);
}
}

/*:38*/
#line 555 "./qprint.w"
;
decode_errors++;
}
ch= (n1<<4)|n2;
}

/*:29*/
#line 497 "./qprint.w"
;
if(ch!=EOF){
putc(ch,fo);
}
break;

case ASCII_CARRIAGE_RETURN:
case ASCII_LINE_FEED:
/*25:*/
#line 449 "./qprint.w"

{
int chn= getc(fi);

if(chn!=EOF){
if((chn==ASCII_LINE_FEED)||(chn==ASCII_CARRIAGE_RETURN)){
if(chn==ch){
ungetc(chn,fi);
}
}else{
ungetc(chn,fi);
}
}
}

/*:25*/
#line 505 "./qprint.w"
;
putc('\n',fo);
break;

default:
putc(ch,fo);
break;
}
}
}

/*:28*//*35:*/
#line 652 "./qprint.w"

static int read_decode_character(void)
{
int ch;

ch= getc(fi);
if(ch!=EOF){
decode_input_stream_position++;
}
return ch;
}

/*:35*//*37:*/
#line 674 "./qprint.w"


static int hex_to_nybble(int ch)
{
if((ch>=ASCII_0)&&(ch<=(ASCII_0+9))){
return ch-'0';
}else if((ch>=ASCII_A)&&(ch<=(ASCII_A+5))){
return 10+(ch-ASCII_A);
}else if((ch>=ASCII_LOWER_CASE_A)&&(ch<=(ASCII_LOWER_CASE_A+5))){
return 10+(ch-ASCII_LOWER_CASE_A);
}
return EOF;
}

/*:37*//*49:*/
#line 1038 "./qprint.w"


static void usage(void)
{

printf("%s  --  Encode/decode file as Quoted-Printable.  Call:\n",PRODUCT);
printf("            %s [-e / -d] [options] [infile] [outfile]\n",PRODUCT);
printf("\n");
printf("Options:\n");
printf("           -b, --binary      Treat input as pure binary file\n");
printf("           --copyright       Print copyright information\n");
printf("           -d, --decode      Decode Quoted-Printable encoded file\n");
printf("           -e, --encode      Encode file into Quoted-Printable\n");
printf("           -i, --ebcdic      EBCDIC-compatible encoding output\n");
printf("           -n, --noerrcheck  Ignore errors when decoding\n");
printf("           -p, --paranoid    Paranoid: quote even printable characters\n");
printf("           -u, --help        Print this message\n");
printf("           --version         Print version number\n");
printf("\n");
printf("by John Walker\n");
printf("http://www.fourmilab.ch/\n");
}

/*:49*//*51:*/
#line 1076 "./qprint.w"


int main(int argc,char*argv[])
{
extern char*optarg;
extern int optind;

int f,opt;
#ifdef FORCE_BINARY_IO
int in_std= TRUE,out_std= TRUE;
#endif
char*cp;






fi= stdin;
fo= stdout;

/*45:*/
#line 837 "./qprint.w"

for(;;){

opt= getopt(argc,argv,"bdeinpu-:");
if(opt==-1){
break;
}

if(opt=='-'){





if(islower(optarg[0])){
opt= toupper(optarg[0]);
}
}

switch(opt){



case'b':
case'B':
binary_input= TRUE;
break;

case'C':
printf("This program is in the public domain.\n");
return 0;

case'd':
case'D':
decoding= TRUE;
break;

case'e':
encoding= TRUE;
break;

case'E':
/*46:*/
#line 921 "./qprint.w"

switch(optarg[1]){
case'b':
EBCDIC_out= TRUE;
break;

case'n':
encoding= TRUE;
break;

default:

fprintf(stderr,"Invalid option: --%s\n",optarg);
return 2;
}

/*:46*/
#line 879 "./qprint.w"
;
break;

case'H':
usage();
return 0;

case'i':
EBCDIC_out= TRUE;
break;

case'n':
case'N':
errcheck= FALSE;
break;

case'p':

case'P':
paranoid= TRUE;
break;

case'u':
case'?':
usage();
return 0;

case'V':
/*50:*/
#line 1064 "./qprint.w"

printf("%s %s\n",PRODUCT,VERSION);
printf("Last revised: %s\n",REVDATE);
printf("The latest version is always available\n");
printf("at http://www.fourmilab.ch/webtools/qprint/\n");

/*:50*/
#line 907 "./qprint.w"
;
return 0;

default:

fprintf(stderr,"Invalid option: --%s\n",optarg);
return 2;
}
}

/*:45*/
#line 1097 "./qprint.w"
;
/*47:*/
#line 942 "./qprint.w"

if(encoding&&decoding){

fprintf(stderr,"Cannot simultaneously encode and decode.\n");
return 2;
}
if(!(encoding||decoding)){

fprintf(stderr,"Please specify --encode (-e) or --decode (-d).\n");
return 2;
}

/*:47*/
#line 1098 "./qprint.w"
;
/*48:*/
#line 969 "./qprint.w"

f= 0;
for(;optind<argc;optind++){
cp= argv[optind];
switch(f){













case 0:
if(strcmp(cp,"-")!=0){
if((fi= fopen(cp,
#ifdef FORCE_BINARY_IO
"rb"
#else
"r"
#endif
))==NULL){

fprintf(stderr,"Cannot open input file %s\n",cp);
return 2;
}
#ifdef FORCE_BINARY_IO
in_std= FALSE;
#endif
}
f++;
break;

case 1:
if(strcmp(cp,"-")!=0){
if((fo= fopen(cp,
#ifdef FORCE_BINARY_IO
(decoding&&(!binary_input))?"w":"wb"
#else
"w"
#endif
))==NULL){

fprintf(stderr,"Cannot open output file %s\n",cp);
return 2;
}
#ifdef FORCE_BINARY_IO
out_std= FALSE;
#endif
}
f++;
break;

default:

fprintf(stderr,"Too many file names specified.\n");
usage();
return 2;
}
}

/*:48*/
#line 1099 "./qprint.w"
;
/*52:*/
#line 1123 "./qprint.w"

#ifdef FORCE_BINARY_IO
if(in_std){
#ifdef _WIN32
_setmode(_fileno(fi),O_BINARY);
#endif
}
if(((!decoding)||binary_input)&&out_std){
#ifdef _WIN32
_setmode(_fileno(fo),O_BINARY);
#endif
}
#endif

/*:52*/
#line 1100 "./qprint.w"
;

if(decoding){
decode();
}else{
encode();
}
return decode_errors?1:0;
}

/*:51*/