-
Notifications
You must be signed in to change notification settings - Fork 14
/
mime_alter.c
4268 lines (3381 loc) · 129 KB
/
mime_alter.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <syslog.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <regex.h>
#include "ffget.h"
#include "pldstr.h"
#include "strstack.h"
#include "boundary-stack.h"
#include "MIME_headers.h"
#include "filename-filters.h"
#include "mime_alter.h"
#include "qpe.h"
#include "logger.h"
#define AM_1K_BUFFER_SIZE 1024
#define AM_DISCLAIMER_FILENAME_LENGTH_MAX 256;
#define AM_DNORMAL (glb.debug > 0)
#define AM_VERBOSE (glb.verbose > 0)
#define DAM if (glb.debug > 0)
#define VAM if (glb.verbose > 0)
/* Globals */
unsigned int altermime_status_flags; // Status flags
unsigned char AM_encode64[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, 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, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 \
};
/* our base 64 decoder table */
static unsigned char b64[256]={
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,\
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,\
128, 0, 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, 128, 128, 128, 128, 128,\
128, 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, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \
};
static struct AM_globals glb;
/*-----------------------------------------------------------------\
Function Name : AM_version
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_version( void )
{
fprintf(stderr,"alterMIME: %s", LIBAM_VERSION);
return 0;
}
/*-----------------------------------------------------------------\
Function Name : AM_init
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_init( void )
{
glb.debug=0; /* Low level debugging */
glb.verbose=0; /* do we talk as we walk */
glb.paranoid=1; /* set paranoid to yes! */
glb.HTML_too=1; /* Add footer to the HTML email too */
glb.multipart_insert=0; /* do not insert into multipart attachments */
glb.nullify_all=0; /* Remove ALL filename'd attachments */
glb.alter_signed=0; /* Do we alter signed emails ? - default is NO */
glb.header_long_search=1; /* Search into email bodies for more attachments */
glb.force_into_b64=0; /* By default don't insert into B64 encoded bodies */
glb.force_for_bad_html=0; /* By default don't try append into broken HTML */
snprintf(glb.ldelimeter,sizeof(glb.ldelimeter),"\r\n");
glb.disclaimer_plain=NULL;
glb.disclaimer_plain_type=AM_DISCLAIMER_TYPE_NONE;
glb.disclaimer_HTML=NULL;
glb.disclaimer_HTML_type=AM_DISCLAIMER_TYPE_NONE;
glb.disclaimer_b64=NULL;
glb.disclaimer_b64_type=AM_DISCLAIMER_TYPE_NONE;
#ifdef ALTERMIME_PRETEXT
glb.pretext_insert=0;
/*
glb.pretext_plain=NULL;
glb.pretext_plain_type=AM_PRETEXT_TYPE_NONE;
glb.pretext_HTML=NULL;
glb.pretext_HTML_type=AM_PRETEXT_TYPE_NONE;
glb.pretext_64=NULL;
glb.pretext_64_type=AM_DISCLAIMER_TYPE_NONE;
*/
#endif
glb.headerbuffermax=0;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : AM_done
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_done( void )
{
if (glb.disclaimer_plain != NULL) free(glb.disclaimer_plain);
if (glb.disclaimer_HTML != NULL) free(glb.disclaimer_HTML);
if (glb.disclaimer_b64 != NULL) free(glb.disclaimer_b64);
#ifdef ALTERMIME_PRETEXT
if (glb.pretext_plain != NULL) free(glb.pretext_plain);
if (glb.pretext_HTML != NULL) free(glb.pretext_HTML);
if (glb.pretext_b64 != NULL) free(glb.pretext_b64);
#endif
return 0;
}
/*-----------------------------------------------------------------\
Function Name : AM_set_force_for_bad_html
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_set_force_for_bad_html( int level )
{
glb.force_for_bad_html = level;
return glb.force_for_bad_html;
}
/*-----------------------------------------------------------------\
Date Code: : 20070127-140050
Function Name : AM_set_force_into_b64
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Set to non-zero (1) if you want alterMIME to attempt to
insert disclaimers into BASE64 encoded texts. This isn't
on by default because there's the possibility of malforming
legitimate attachments.
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_set_force_into_b64( int level )
{
glb.force_into_b64 = level;
return glb.force_into_b64;
}
/*-----------------------------------------------------------------\
Function Name : AM_set_pretext_insert
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
#ifdef ALTERMIME_PRETEXT
int AM_set_pretext_insert( int level )
{
glb.pretext_insert = level;
//LOGGER_log("%s:%d:AM_set_pretext_insert:DEBUG: Pretext set to '%d'", FL, glb.pretext_insert);
return glb.pretext_insert;
}
#endif
/*-----------------------------------------------------------------\
Function Name : AM_set_debug
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_set_debug( int level )
{
glb.debug = level;
AM_set_verbose( level );
MIMEH_set_debug(level);
return level;
}
/*-----------------------------------------------------------------\
Function Name : AM_hbuffer_reset
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_hbuffer_reset( void )
{
int i;
for (i = 0; i < glb.headerbuffermax; i++)
{
if (glb.headerbuffer[i]) free(glb.headerbuffer[i]);
}
glb.headerbuffermax=0;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : *AM_hbuffer_add
Returns Type : char
----Parameter List
1. char *headerline,
2. FILE *f ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *AM_hbuffer_add( char *headerline, FILE *f )
{
int i;
if (glb.headerbuffermax == AM_HEADERBUFFER_MAX)
{
if (glb.headerbuffer[0])
{
fprintf(f,"%s",glb.headerbuffer[0]);
free(glb.headerbuffer[0]);
}
for (i = 0; i < (AM_HEADERBUFFER_MAX-1); i++)
{
glb.headerbuffer[i] = glb.headerbuffer[i+1];
}
glb.headerbuffermax = AM_HEADERBUFFER_MAX -1;
}
glb.headerbuffer[glb.headerbuffermax] = malloc(sizeof(char) *(AM_HEADERBUFFER_ITEM_SIZE +1));
if (!glb.headerbuffer[glb.headerbuffermax])
{
LOGGER_log("alterMIME: AM_hbuffer_add: Error: cannot allocate %d bytes for new header", AM_HEADERBUFFER_ITEM_SIZE +1);
return NULL;
}
strncpy(glb.headerbuffer[glb.headerbuffermax], headerline, AM_HEADERBUFFER_ITEM_SIZE);
glb.headerbuffermax++;
return glb.headerbuffer[glb.headerbuffermax-1];
}
/*------------------------------------------------------------------------
Procedure: AM_hbuffer_getmax ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int AM_hbuffer_getmax( void )
{
return glb.headerbuffermax;
}
/*------------------------------------------------------------------------
Procedure: AM_hbuffer_getline ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
char *AM_hbuffer_getline( int index )
{
if ((index >=0)&&(index < glb.headerbuffermax))
return glb.headerbuffer[index];
else return NULL;
}
/*------------------------------------------------------------------------
Procedure: AM_set_glb.paranoid ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int AM_set_paranoid( int level )
{
glb.paranoid = level;
return glb.paranoid;
}
/*-----------------------------------------------------------------\
Function Name : AM_set_header_long_search
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_set_header_long_search( int level )
{
glb.header_long_search = level;
return glb.header_long_search;
}
/*------------------------------------------------------------------------
Procedure: AM_set_glb.verbose ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int AM_set_verbose( int level )
{
glb.verbose = level;
return glb.verbose;
}
/*------------------------------------------------------------------------
Procedure: AM_set_HTMLtoo ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int AM_set_HTMLtoo( int level )
{
glb.HTML_too = level;
return glb.HTML_too;
}
/*------------------------------------------------------------------------
Procedure: AM_set_multipart_insert ID:1
Purpose: Set the multipart-insertion of dislcaimers to level. Multipart disclaimers are not on by default
because they would imply inserting disclaimers into previously forwarded messages.
Input: level
Output:
Errors:
------------------------------------------------------------------------*/
int AM_set_multipart_insert( int level )
{
glb.multipart_insert = level;
return glb.multipart_insert;
}
/*-----------------------------------------------------------------\
Function Name : *AM_set_disclaimer_plain
Returns Type : char
----Parameter List
1. char *filename,
2. int disclaimer_type ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Sets the disclaimer text and text-type for Plain-text disclaimers.
What we mean here is, if the disclaimer_type is 'TEXT' then we
don't interpret the contents of .disclaimer_plain as a filename
rather, we take it literally as the text to use for our disclaimer
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *AM_set_disclaimer_plain( char *filename, int disclaimer_type )
{
glb.disclaimer_plain = strdup( filename );
glb.disclaimer_plain_type = disclaimer_type;
return glb.disclaimer_plain;
}
/*-----------------------------------------------------------------\
Function Name : *AM_set_disclaimer_HTML
Returns Type : char
----Parameter List
1. char *filename,
2. int disclaimer_type ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *AM_set_disclaimer_HTML( char *filename, int disclaimer_type )
{
glb.disclaimer_HTML = strdup( filename );
glb.disclaimer_HTML_type = disclaimer_type;
return glb.disclaimer_HTML;
}
/*-----------------------------------------------------------------\
Date Code: : 20081106-065138
Function Name : *AM_set_disclaimer_b64
Returns Type : char
----Parameter List
1. char *filename,
2. int disclaimer_type ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *AM_set_disclaimer_b64( char *filename, int disclaimer_type )
{
glb.disclaimer_b64 = strdup( filename );
glb.disclaimer_b64_type = disclaimer_type;
return glb.disclaimer_b64;
}
/*-----------------------------------------------------------------\
Function Name : *AM_set_pretext_plain
Returns Type : char
----Parameter List
1. char *filename,
2. int pretext_type ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
#ifdef ALTERMIME_PRETEXT
int AM_set_pretext( int level )
{
glb.pretext_insert = level;
return 0;
}
/*
char *AM_set_pretext_plain( char *filename, int disclaimer_type )
{
glb.pretext_plain = strdup( filename );
glb.pretext_plain_type = disclaimer_type;
return glb.pretext_plain;
}
char *AM_set_pretext_HTML( char *filename, int disclaimer_type )
{
glb.pretext_HTML = strdup( filename );
glb.pretext_HTML_type = disclaimer_type;
return glb.pretext_HTML;
}
char *AM_set_pretext_64( char *filename, int disclaimer_type )
{
glb.pretext_b64 = strdup( filename );
glb.pretext_b64_type = disclaimer_type;
return glb.pretext_b64;
}
*/
#endif
/*-----------------------------------------------------------------\
Date Code: : 20081216-191307
Function Name : *AM_set_disclaimer_attachment
Returns Type : char
----Parameter List
1. char *filename,
2. int index ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *AM_set_disclaimer_attachment( char *filename, int index ) {
return NULL;
}
/*------------------------------------------------------------------------
Procedure: AM_set_nullifyall ID:1
Purpose:
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int AM_set_nullifyall( int level )
{
glb.nullify_all = level;
return glb.nullify_all;
}
/*------------------------------------------------------------------------
Procedure: AM_set_altersigned ID:1
Purpose: Turns on or off the option to alter signed email messages.
Default is to be -off-.
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int AM_set_altersigned( int level )
{
glb.alter_signed = level;
return glb.alter_signed;
}
/*-----------------------------------------------------------------\
Function Name : AM_ntorn
Returns Type : int
----Parameter List
1. char *in,
2. FILE *out ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Converts any singular occurances of \n into \r\n
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_ntorn( char *in, FILE *out )
{
char *p = in;
char lastchar=' ';
while (*p)
{
if ((*p == '\n')&&(lastchar != '\r')) fprintf(out, "\r");
fprintf(out, "%c", *p);
lastchar = *p;
p++;
}
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20081106-145300
Function Name : AM_adapt_linebreak
Returns Type : int
----Parameter List
1. char *in,
2. FILE *out,
3. char *lb ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
ASSUMES that the input buffer (*in) is PROPERLY formatted as
either \n or \r\n terminated text. This will NOT work properly
if you feed it data with malformed linebreaks.
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *AM_adapt_linebreak( char *in, char *lb ) {
char safe[]="\r\n";
char *newblock;
if (in == NULL) return NULL;
if ((lb==NULL)||(lb[0] == 0)) lb = safe;
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: Linebreak=%x:%x",FL,lb[0],lb[1]);
if (lb[0] == '\r') {
if (strchr(in,'\r')) {
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: email and disclaimer are CRLF, no change",FL);
return in;
} else {
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: chaging LF only to CRLF",FL);
newblock = strdup(in);
newblock = PLD_strreplace(&newblock, "\n", "\r\n", 0);
}
} else if (lb[0] == '\n') {
if (!strchr(in,'\r')) {
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: email and disclaimer are LF only, no change",FL);
return in;
} else {
newblock = strdup(in);
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: changing CRLF to LF (original size=%d)",FL,strlen(newblock));
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: %s", FL, newblock);
newblock = PLD_strreplace(&newblock, "\r\n", "\n", 0);
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: changing done, new size=%d",FL,strlen(newblock));
DAM LOGGER_log("%s:%d:AM_adapt_linebreak:DEBUG: %s", FL, newblock);
}
}
return newblock;
}
/*------------------------------------------------------------------------
Procedure: AM_base64_encodef ID:1
Purpose: encode to BASE64 an input stream to an output stream
Input: FILE *fin: Input stream
FILE *fout: Output stream
Output:
Errors:
------------------------------------------------------------------------*/
int AM_base64_encodef( FILE *fin, FILE *fout )
{
unsigned char inbuf[3];
unsigned char outbuf[4];
int cc;
int byte_count;
if (!fin)
{
LOGGER_log("AM_base64_encodef: Error: input file stream not open, please use AM_base64_encode(infile,outfile)");
return -1;
}
if (!fout)
{
LOGGER_log("AM_base64_encodef: Error: output file stream not open, please use AM_base64_encode(infile,outfile)");
return -1;
}
cc = 0; /* Character Count = 0 */
while ((byte_count = fread(inbuf, 1, 3, fin))) /* while we get more than 0 bytes */
{
size_t bc;
/* Split 3 bytes into 4 bytes by taking 6bit blocks out of the 24 bit (3x8bit)
* array */
outbuf[0] = AM_encode64[((inbuf[0] & 0xFC) >> 2)];
outbuf[1] = AM_encode64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)];
outbuf[2] = AM_encode64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)];
outbuf[3] = AM_encode64[(inbuf[2] & 0x3F)];
/* if we didn't get a full 3 bytes from the file read, then we need to then
* pad the output with '=' (base64 padding char) */
if ( byte_count < 3 )
{
if ( byte_count == 2 ) outbuf[3] = '=';
if ( byte_count == 1 ) outbuf[3] = outbuf[2] = '=';
bc = fwrite(outbuf, 1, 4, fout); /* write out the buffer */
if (bc != 4) LOGGER_log("%s:%d:AM_base64_encode:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
bc= fwrite( "\n", 1, 1, fout ); /* as this is the last line, put a trailing \n */
if (bc != 1) LOGGER_log("%s:%d:AM_base64_encode:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
break; /* exit out of the while loop */
}
bc = fwrite(outbuf, 1, 4, fout); /* normal operation buffer-write */
if (bc != 4) LOGGER_log("%s:%d:AM_base64_encode:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
cc += 4; /* increment the output char count by 4 */
if (cc > 76) /* if we have more than 76 chars, then put in a \n */
{
bc = fwrite( "\n", 1, 1, fout ); /* write the \n out */
if (bc != 1) LOGGER_log("%s:%d:AM_base64_encode:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
cc = 0; /* reset the output char count */
} /* if bytecount == 3 */
} /* while more data to read in */
return 0;
}
/*------------------------------------------------------------------------
Procedure: AM_base64_encode ID:1
Purpose: Encodes a given input stream into BASE64 and outputs to a given
output stream
Input: char *enc_fname: Input filename of the stream to encode
char *out_fname: Output filename of the stream to send to
Output:
Errors:
------------------------------------------------------------------------*/
int AM_base64_encode( char *enc_fname, char *out_fname )
{
FILE *fin, *fout;
fin = fopen( enc_fname, "rb" );
if (!fin)
{
LOGGER_log("AM_base64_encode: Cannot open \"%s\" for reading.(%s)",enc_fname,strerror(errno));
return -1;
}
fout = fopen ( out_fname, "wb" );
if (!fout)
{
LOGGER_log("AM_base64_encode: Cannot open \"%s\" for writing.(%s)",out_fname,strerror(errno));
fclose(fin);
return -1;
}
AM_base64_encodef( fin, fout ); /* encode and output */
fclose(fin); /* close the input file */
fclose(fout); /* close the output file */
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20070124-133319
Function Name : AM_base64_encode_from_buffer
Returns Type : int
----Parameter List
1. char *buffer,
2. size_t buffer_size,
3. char *out_fname ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Encodes a given buffer contents into BASE64 and writes to
file out_fname
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int AM_base64_encode_buffer_to_FILE( char *buffer, size_t buffer_size, FILE *fout)
{
unsigned char outbuf[4];
int cc;
int chars = strlen(buffer);
size_t bc;
if (fout == NULL) {
LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:ERROR: Output file stream was NULL", FL);
return -1;
}
cc = 0; /* Character Count = 0 */
DAM LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:DEBUG: Encoding %d chars", FL, chars);
while (chars > 0) /* while we get more than 0 bytes */
{
/* Split 3 bytes into 4 bytes by taking 6bit blocks out of the 24 bit (3x8bit)
* array */
if (chars >= 3) {
outbuf[0] = AM_encode64[((buffer[0] & 0xFC) >> 2)];
outbuf[1] = AM_encode64[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
outbuf[2] = AM_encode64[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
outbuf[3] = AM_encode64[(buffer[2] & 0x3F)];
buffer += 3;
chars -= 3;
} else {
/* if we didn't get a full 3 bytes from the file read, then we need to then
* pad the output with '=' (base64 padding char) */
DAM LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:DEBUG: Encoding remaining %d chars '%s'", FL, chars, buffer);
if ( chars == 2 ) {
DAM LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:DEBUG: '%c' '%c'", FL, buffer[0], buffer[1]);
outbuf[0] = AM_encode64[((buffer[0] & 0xFC) >> 2)];
outbuf[1] = AM_encode64[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
outbuf[2] = AM_encode64[((buffer[1] & 0x0F) << 2)];
outbuf[3] = '=';
}
if ( chars == 1 ) {
DAM LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:DEBUG: '%c'", FL, buffer[0]);
outbuf[0] = AM_encode64[((buffer[0] & 0xFC) >> 2)];
outbuf[1] = AM_encode64[((buffer[0] & 0x03) << 4)];
outbuf[3] = outbuf[2] = '=';
}
bc = fwrite(outbuf, 1, 4, fout); /* write out the buffer */
if (bc != 4) LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
bc = fwrite( "\n", 1, 1, fout ); /* as this is the last line, put a trailing \n */
if (bc != 1) LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
break; /* exit out of the while loop */
}
bc = fwrite(outbuf, 1, 4, fout); /* normal operation buffer-write */
if (bc != 4) LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
cc += 4; /* increment the output char count by 4 */
if (cc > 76) /* if we have more than 76 chars, then put in a \n */
{
bc = fwrite( "\n", 1, 1, fout ); /* write the \n out */
if (bc != 1) LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
cc = 0; /* reset the output char count */
} /* if bytecount == 3 */
} /* while more data to encode */
bc = fwrite("\n",1,1,fout);
if (bc != 1) LOGGER_log("%s:%d:AM_base64_encode_buffer_to_FILE:ERROR: Wrote %d bytes rather than %d to output", FL, bc);
return 0;
}
/*------------------------------------------------------------------------
Procedure: MDECODE_decode_short64 ID:1
Purpose: Decodes a BASE64 encoded realm
Input: char *realm : base64 encoded NUL terminated string
Output: decoded data is written to the buffer
Errors:
------------------------------------------------------------------------*/
int AM_base64_decode_buffer( char *buffer, size_t length )
{
int i;
int realm_size = length;
int stopcount = 0; /* How many stop (=) characters we've read in */
int c; /* a single char as retrieved using MDECODE_get_char() */
int char_count = 0; /* How many chars have been received */
char output[3]; /* The 4->3 byte output array */
char input[4]; /* The 4->3 byte input array */
char *outstring = buffer;
char_count = 0;
while (char_count < realm_size)
{
/* Initialise the decode buffer */
input[0] = input[1] = input[2] = input[3] = 0;
/* snatch 4 characters from the input */
for (i = 0; i < 4; i++) {