-
Notifications
You must be signed in to change notification settings - Fork 26
/
MIME_headers.c
3214 lines (2645 loc) · 108 KB
/
MIME_headers.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
/*-----------------------------------------------------------------------
**
**
** MIME_headers
**
** Written by Paul L Daniels, originally for the Xamime project
** (http://www.xamime.com) but since spawned off to the ripMIME/alterMIME
** family of email parsing tools.
**
** Copyright PLD, 1999,2000,2001,2002,2003
** Licence: BSD
** For more information on the licence and copyrights of this code, please
** email [email protected]
** CHANGES
** 2003-Jun-24: PLD: Added subject parsing
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <ctype.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "ffget.h"
#include "pldstr.h"
#include "libmime-decoders.h"
#include "logger.h"
#include "strstack.h"
#include "boundary-stack.h"
#include "filename-filters.h"
#include "MIME_headers.h"
#ifndef FL
#define FL __FILE__, __LINE__
#endif
// Debug precodes
#define MIMEH_DPEDANTIC ((glb.debug >= _MIMEH_DEBUG_PEDANTIC))
#define MIMEH_DNORMAL ((glb.debug >= _MIMEH_DEBUG_NORMAL ))
#define DMIMEH if ((glb.debug >= _MIMEH_DEBUG_NORMAL))
char *MIMEH_defect_description_array[_MIMEH_DEFECT_ARRAY_SIZE];
struct MIMEH_globals {
int doubleCR;
int doubleCR_save;
char doubleCRname[_MIMEH_STRLEN_MAX +1];
char appledouble_filename[_MIMEH_STRLEN_MAX +1];
char subject[_MIMEH_STRLEN_MAX +1];
char *headerline;
char *headerline_original; // Holds the original header-form without decoding.
int save_headers;
int save_headers_original;
int test_mailbox;
int debug;
int webform;
int doubleCR_count;
int header_fix;
int verbose;
int verbose_contenttype;
int header_longsearch; // keep searching until valid headers are found - this is used to filter out qmail bounced emails - breaks RFC's but people are wanting it :-(
int longsearch_limit; // how many segments do we attempt to look ahead...
char output_dir[_MIMEH_STRLEN_MAX +1];
FILE *header_file;
FILE *original_header_file;
int original_header_save_to_file;
};
static struct MIMEH_globals glb;
/*-----------------------------------------------------------------\
Function Name : MIMEH_version
Returns Type : int
----Parameter List
1. void,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_version(void)
{
fprintf(stdout,"mimeheaders: %s\n", MIMEH_VERSION);
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_init
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_init( void )
{
glb.doubleCR = 0;
glb.headerline = NULL;
glb.headerline_original = NULL;
glb.header_file = NULL;
glb.original_header_file = NULL;
glb.original_header_save_to_file = 0;
glb.save_headers = 0;
glb.save_headers_original = 0;
glb.test_mailbox = 0;
glb.debug = 0;
glb.webform = 0;
glb.doubleCR_count = 0;
glb.doubleCR_save = 1;
glb.header_fix = 1;
glb.verbose = 0;
glb.verbose_contenttype = 0;
glb.output_dir[0]='\0';
glb.doubleCRname[0]='\0';
glb.appledouble_filename[0]='\0';
glb.header_longsearch=0;
glb.longsearch_limit=1;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_get_doubleCR
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_get_doubleCR( void )
{
return glb.doubleCR;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_doubleCR
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_doubleCR( int level )
{
glb.doubleCR = level;
return glb.doubleCR;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_headerfix
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_headerfix( int level )
{
glb.header_fix = level;
return glb.header_fix;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_doubleCR_save
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_doubleCR_save( int level )
{
glb.doubleCR_save = level;
return glb.doubleCR_save;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_get_doubleCR_save
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_get_doubleCR_save( void )
{
return glb.doubleCR_save;
}
/*-----------------------------------------------------------------\
Function Name : *MIMEH_get_doubleCR_name
Returns Type : char
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *MIMEH_get_doubleCR_name( void )
{
return glb.doubleCRname;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_debug
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_debug( int level )
{
glb.debug = level;
return glb.debug;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_outputdir
Returns Type : int
----Parameter List
1. char *dir ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_outputdir( char *dir )
{
if (dir) snprintf(glb.output_dir,_MIMEH_STRLEN_MAX,"%s",dir);
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_webform
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_webform( int level )
{
glb.webform = level;
return glb.webform;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_mailbox
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_mailbox( int level )
{
glb.test_mailbox = level;
return level;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_verbosity
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_verbosity( int level )
{
glb.verbose = level;
return level;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_verbosity_contenttype
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_verbosity_contenttype( int level )
{
glb.verbose_contenttype = level;
return level;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_get_verbosity_contenttype
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_get_verbosity_contenttype( void )
{
return glb.verbose_contenttype;
}
/*------------------------------------------------------------------------
Procedure: MIMEH_set_headers_save ID:1
Purpose: Sets MIMEH's headers save file (where MIMEH will save the
headers it reads in from the mailpack)
Input:
Output:
Errors:
------------------------------------------------------------------------*/
int MIMEH_set_headers_save( FILE *f )
{
glb.header_file = f;
glb.save_headers = 1;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_headers_original_save_to_file
Returns Type : int
----Parameter List
1. FILE *f ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_headers_original_save_to_file( FILE *f )
{
if (f == NULL) glb.original_header_save_to_file = 0;
else glb.original_header_save_to_file = 1;
glb.original_header_file = f;
return glb.original_header_save_to_file;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_headers_nosave
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_headers_nosave( void )
{
glb.header_file = NULL;
glb.save_headers = 0;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_get_headers_save
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_get_headers_save( void )
{
return glb.save_headers;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_headers_save_original
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_headers_save_original( int level )
{
glb.save_headers_original = level;
return glb.save_headers_original;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_get_headers_ptr
Returns Type : int
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *MIMEH_get_headers_ptr( void )
{
return glb.headerline;
}
/*-----------------------------------------------------------------\
Function Name : *MIMEH_get_headers_original_ptr
Returns Type : char
----Parameter List
1. void ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *MIMEH_get_headers_original_ptr( void )
{
return glb.headerline_original;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_header_longsearch
Returns Type : int
----Parameter List
1. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
The header long-search is a facility switch that will make the
header searching to continue on until it either reaches the end of
the file or it finds valid (??) headers to work on.
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_header_longsearch( int level )
{
glb.header_longsearch = level;
return glb.header_longsearch;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_set_defect
Returns Type : int
----Parameter List
1. struct MIMEH_header_info *hinfo,
2. int defect , The defect code to set
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_set_defect( struct MIMEH_header_info *hinfo, int defect )
{
if ((defect >= 0)&&(defect < _MIMEH_DEFECT_ARRAY_SIZE))
{
hinfo->defects[defect]++;
hinfo->header_defect_count++;
DMIMEH LOGGER_log("%s:%d:MIMEH_set_defect:DEBUG: Setting defect index '%d' to '%d'",FL, defect, hinfo->defects[defect]);
}
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_is_contenttype
Returns Type : int
----Parameter List
1. int range_type,
2. int content_type ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_is_contenttype( int range_type, int content_type )
{
int diff;
diff = content_type -range_type;
if ((diff < _CTYPE_RANGE)&&(diff > 0)) return 1;
else return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_is_binary
Returns Type : int
----Parameter List
1. struct FFGET_FILE *f ,
------------------
Exit Codes : 1 = yes, it's binary, 0 = no.
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_is_binary( char *fname )
{
char buffer[1024];
int read_count;
FILE *f;
f = fopen(fname,"r");
if (!f) return 1;
read_count = fread(buffer, 1, 1024, f);
fclose(f);
while (read_count)
{
read_count--;
if (buffer[read_count] == 0) return 1;
}
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_are_headers_RFC822
Returns Type : int
----Parameter List
1. char *fname ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_are_headers_RFC822( char *headers )
{
char conditions[7][16] = { "received", "from", "subject", "date", "content", "boundary" };
int hitcount = 0;
int condition_item;
char *lc_headers = NULL;
if (headers == NULL)
{
DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG: Headers are NULL");
return 0;
}
DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG:----\n%s\n----",FL,headers);
lc_headers = strdup(headers);
if (lc_headers == NULL) return 0;
//PLD_strlower((unsigned char *)lc_headers);
PLD_strlower(lc_headers);
DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG:----(lowercase)----\n%s\n----",FL,lc_headers);
for (condition_item=0; condition_item < 6; condition_item++)
{
char *p;
DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG: Condition test item[%d] = '%s'",FL,condition_item,conditions[condition_item]);
p = strstr(lc_headers, conditions[condition_item]);
if (p != NULL)
{
if (p > lc_headers)
{
if ((*(p-1) == '\n')||(*(p-1) == '\r'))
hitcount++;
} else if (p == lc_headers) hitcount++;
}
}
if (lc_headers != NULL) free(lc_headers);
return hitcount;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_save_doubleCR
Returns Type : int
----Parameter List
1. FFGET_FILE *f ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_save_doubleCR( FFGET_FILE *f )
{
//char c;
int c;
FILE *fo;
struct stat st;
// Determine a file name we can use.
do {
glb.doubleCR_count++;
snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/doubleCR.%d",glb.output_dir,glb.doubleCR_count);
}
while (stat(glb.doubleCRname, &st) == 0);
fo = fopen(glb.doubleCRname,"w");
if (!fo)
{
LOGGER_log("%s:%d:MIMEH_save_doubleCR:ERROR: unable to open '%s' to write (%s)", FL,glb.doubleCRname,strerror(errno));
return -1;
}
if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_save_doubleCR:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname);
while (1)
{
c = FFGET_fgetc(f);
fprintf(fo,"%c",c);
if ((c == EOF)||(c == '\n'))
{
break;
}
}
fclose(fo);
return 0;
}
/*-----------------------------------------------------------------\
Function Name : *
Returns Type : char
----Parameter List
1. MIMEH_absorb_whitespace( char *p ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char * MIMEH_absorb_whitespace( char *p )
{
if (p)
{
while ((*p != '\0')&&((*p == ' ')||(*p == '\t'))) p++;
}
return p;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_strip_comments
Returns Type : int
----Parameter List
1. char *input ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Removes comments from RFC[2]822 headers
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_strip_comments( char *input )
{
char *p,*p_org;
int in_quote=0;
if (input == NULL) return 0;
p = p_org = input;
do {
char *q = NULL;
// Locate (if any) the first occurance of the (
while ((p_org != NULL)&&((*p_org != '(')||(in_quote==1)))
{
switch (*p_org) {
case '"':
in_quote ^= 1;
break;
case '\n':
case '\r':
in_quote = 0;
break;
case '\0':
p_org = NULL;
break;
}
if (p_org) p_org++;
}
p = p_org;
if ((p != NULL)&&(in_quote == 0))
{
int stop_searching = 0;
DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: Located open ( at %s",FL,p);
// If we did locate an opening parenthesis, look for the closing one
// NOTE - we cannot have a breaking \n or \r inbetween
// q = strpbrk(p, ")\n\r");
q = p;
while ( (q != NULL) && (stop_searching == 0) )
{
switch (*q) {
case '\0':
stop_searching = 1;
q = NULL;
break;
case '\n':
case '\r':
stop_searching = 1;
in_quote = 0;
break;
case '"':
in_quote ^= 1;
break;
case ')':
DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: Located closing ) at %s",FL,q);
if (in_quote == 0) stop_searching = 1;
break;
}
if ((q != NULL)&&(stop_searching == 0)) q++;
}
// If we've got both opening and closing, then we need to remove
// the contents of the comment, including the parenthesis
if (q != NULL)
{
if (*q != ')')
{
// if we found a \n or \r between the two (), then jump out
// and move p to the next position.
p_org++;
continue;
} else {
// Move q to the first char after the closing parenthesis
q++;
DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: located closing ) at %s ",FL, q);
// While there's more chars in string, copy them to where
// the opening parenthesis is
while (*q != '\0')
{
*p = *q;
p++;
q++;
} // While q != '\0'
DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: char copy done",FL);
// Terminate the string
*p = '\0';
} // if q !=/= ')'
} else break; // if q == NULL
} // if p == NULL
} while ((p != NULL)&&(p_org != NULL)); // do-while more comments to remove
DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: Final string = '%s'",FL,input);
return 0;
}
/*
* Case insensitive strstr() without calling on system libs
* which can vary between OSs for the strcasestr() call at times
*/
char *MIMEH_strcasestr(char *haystack, char *needle)
{
char *hs, *ne;
char *result = NULL;
hs = strdup(haystack);
PLD_strlower(hs);
ne = strdup(needle);
PLD_strlower(ne);
if (hs && ne) {
result = strstr(hs, ne);
result = result -hs +haystack;
}
if (hs) free(hs);
if (ne) free(ne);
return result;
}
int MIMEH_check_ct(char *q)
{
char *p=q;
p++;
if(*p!='\0' && MIMEH_strcasestr(p,"Content-Type:")==p)return 1;
p++;
if(*p!='\0' && MIMEH_strcasestr(p,"Content-Type:")==p)return 1;
return 0;
}
/*-----------------------------------------------------------------\
Function Name : MIMEH_fix_header_mistakes
Returns Type : int
----Parameter List
1. char *data ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Some headers are broken in their wrapping, ie, they fail to
put a leading space at the start of the next wrapped data line; ie
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="yxnjjhyk.xml"
Which causes normal header processing to not locate the filename.
This function will see if there are any lines with a trailing ; that
do not have a leading space on the next line and subsequently replace
the \r\n chars after the ; with blanks, effectively pulling the line up
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int MIMEH_fix_header_mistakes( char *data )
{
int result = 0;
char *p;
DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Checking and fixing headers in '%s'",FL,data);
if (glb.header_fix == 0) return result;
p = data;
while (p) {