aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libical/sspm.c
blob: 36f69b506a0b36f6cb085ff212c4f3eb4bf73aa9 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
/* -*- Mode: C -*-
  ======================================================================
  FILE: sspm.c Parse Mime
  CREATOR: eric 25 June 2000
  
  $Id$
  $Locker$
    
 The contents of this file are subject to the Mozilla Public License
 Version 1.0 (the "License"); you may not use this file except in
 compliance with the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and
 limitations under the License.
 

 This program is free software; you can redistribute it and/or modify
 it under the terms of either: 

    The LGPL as published by the Free Software Foundation, version
    2.1, available at: http://www.fsf.org/copyleft/lesser.html

  Or:

    The Mozilla Public License Version 1.0. You may obtain a copy of
    the License at http://www.mozilla.org/MPL/

  The Initial Developer of the Original Code is Eric Busboom

 (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
 ======================================================================*/

#include <stdio.h>
#include <string.h>
#include "sspm.h"
#include <assert.h>
#include <ctype.h> /* for tolower */
#include <stdlib.h>   /* for malloc, free */


#ifdef DMALLOC
#include "dmalloc.h"
#endif

#define TMP_BUF_SIZE 1024


enum mime_state {
    UNKNOWN_STATE,
    IN_HEADER,
    END_OF_HEADER,
    IN_BODY,
    OPENING_PART,
    END_OF_PART,
    TERMINAL_END_OF_PART,
    END_OF_INPUT
};

struct mime_impl{
    struct sspm_part *parts; 
    size_t max_parts;
    int part_no;
    int level;
    struct sspm_action_map *actions;
    char* (*get_string)(char *s, size_t size, void* data);
    void* get_string_data;
    char temp[TMP_BUF_SIZE];
    enum mime_state state;
};

void sspm_free_header(struct sspm_header *header);
void* sspm_make_multipart_part(struct mime_impl *impl,struct sspm_header *header);
void sspm_read_header(struct mime_impl *impl,struct sspm_header *header);

char* sspm_strdup(char* str){

    char* s;

    s = strdup(str);

    return s;
}


struct  major_content_type_map 
{
    enum sspm_major_type type;
    char* str;
    
} major_content_type_map[]  = 
{
    {SSPM_MULTIPART_MAJOR_TYPE,"multipart" },
    {SSPM_TEXT_MAJOR_TYPE,"text" },
    {SSPM_TEXT_MAJOR_TYPE,"text" },
    {SSPM_IMAGE_MAJOR_TYPE,"image" },
    {SSPM_AUDIO_MAJOR_TYPE,"audio" },
    {SSPM_VIDEO_MAJOR_TYPE,"video" },
    {SSPM_APPLICATION_MAJOR_TYPE,"application" },
    {SSPM_MULTIPART_MAJOR_TYPE,"multipart" },
    {SSPM_MESSAGE_MAJOR_TYPE,"message" },
    {SSPM_UNKNOWN_MAJOR_TYPE,"" },
};

struct  minor_content_type_map 
{
    enum sspm_minor_type type;
    char* str;

} minor_content_type_map[]  = 
{
    {SSPM_ANY_MINOR_TYPE,"*" },
    {SSPM_PLAIN_MINOR_TYPE,"plain" },
    {SSPM_RFC822_MINOR_TYPE,"rfc822" },
    {SSPM_DIGEST_MINOR_TYPE,"digest" },
    {SSPM_CALENDAR_MINOR_TYPE,"calendar" },
    {SSPM_MIXED_MINOR_TYPE,"mixed" },
    {SSPM_RELATED_MINOR_TYPE,"related" },
    {SSPM_ALTERNATIVE_MINOR_TYPE,"alternative" },
    {SSPM_PARALLEL_MINOR_TYPE,  "parallel" },  
    {SSPM_UNKNOWN_MINOR_TYPE,"" } 
};




char* sspm_get_parameter(char* line, char* parameter)
{
    char *p,*s,*q;
    static char name[1024];
    
    /* Find where the parameter name is in the line */
    p = strstr(line,parameter);

    if( p == 0){
    return 0;
    }

    /* skip over the parameter name, the '=' and any blank spaces */

    p+=strlen(parameter);

    while(*p==' ' || *p == '='){
    p++;
    }

    /*now find the next semicolon*/

    s = strchr(p,';');

    /* Strip of leading quote */
    q = strchr(p,'\"');

    if(q !=0){
    p = q+1;
    }

    if(s != 0){
    strncpy(name,p,(size_t)s-(size_t)p);
    } else {
    strcpy(name,p);
    }

    /* Strip off trailing quote, if it exists */

    q = strrchr(name,'\"');

    if (q != 0){
    *q='\0';
    }
    
    return name;
}

char* sspm_property_name(char* line)
{
    static char name[1024];
    char *c = strchr(line,':');

    if(c != 0){
    strncpy(name,line,(size_t)c-(size_t)line);
    name[(size_t)c-(size_t)line] = '\0';
    return name;
    } else {
    return 0;
    }
}

char* sspm_value(char* line)
{
    static char value[1024];

    char *c,*s, *p;

    /* Find the first colon and the next semicolon */

    c = strchr(line,':');
    s = strchr(c,';');

    /* Skip the colon */
    c++;

    if (s == 0){
    s = c+strlen(line);
    }

    for(p=value; c != s; c++){
    if(*c!=' ' && *c!='\n'){
        *(p++) = *c;
    }
    }

    *p='\0';

    return value;

}

char *mime_headers[] = {
    "Content-Type",
    "Content-Transfer-Encoding",
    "Content-Disposition",
    "Content-Id",
    "Mime-Version",
    0 
};


void* sspm_default_new_part()
{
    return 0;
}
void sspm_default_add_line(void *part, struct sspm_header *header, 
               char* line, size_t size)
{
}

void* sspm_default_end_part(void* part)
{
    return 0;
}

void sspm_default_free_part(void *part)
{
}



struct sspm_action_map sspm_action_map[] = 
{
    {SSPM_UNKNOWN_MAJOR_TYPE,SSPM_UNKNOWN_MINOR_TYPE,sspm_default_new_part,sspm_default_add_line,sspm_default_end_part,sspm_default_free_part},
};

int sspm_is_mime_header(char *line)
{
    char *name = sspm_property_name(line);
    int i;

    if(name == 0){ 
    return 0;
    }

    for(i = 0; mime_headers[i] != 0; i++){
    if(strcasecmp(name, mime_headers[i]) == 0)
        return 1;
    }
    
    return 0;
}

int sspm_is_mail_header(char* line)
{
    char *name = sspm_property_name(line);

    if (name != 0){
    return 1;
    }

    return 0;

}

int sspm_is_blank(char* line)
{
    char *p;
    char c =0;

    for(p=line; *p!=0; p++){
    if( ! (*p == ' '|| *p == '\t' || *p=='\n') ){
        c++;
    }
    }

    if (c==0){
    return 1;
    }

    return 0;
    
}

int sspm_is_continuation_line(char* line)
{
    if (line[0] == ' '|| line[0] == '\t' ) {
    return 1;
    }

    return 0;
}

int sspm_is_mime_boundary(char *line)
{
    if( line[0] == '-' && line[1] == '-') {
    return 1;
    } 

    return 0;
}

int sspm_is_mime_terminating_boundary(char *line)
{


    if (sspm_is_mime_boundary(line) &&
    strstr(line,"--\n")){
    return 1;
    } 

    return 0;
}

enum line_type {
    EMPTY,
    BLANK,
    MIME_HEADER,
    MAIL_HEADER,
    HEADER_CONTINUATION,
    BOUNDARY,
    TERMINATING_BOUNDARY,
    UNKNOWN_TYPE
};


enum line_type get_line_type(char* line){

    if (line == 0){
    return EMPTY;
    } else if(sspm_is_blank(line)){
    return BLANK;
    } else if (sspm_is_mime_header(line)){
    return MIME_HEADER;
    } else if (sspm_is_mail_header(line)){
    return MAIL_HEADER;
    } else if (sspm_is_continuation_line(line)){
    return HEADER_CONTINUATION;
    } else if (sspm_is_mime_terminating_boundary(line)){
    return TERMINATING_BOUNDARY;
    } else if (sspm_is_mime_boundary(line)) {
    return BOUNDARY;
    } else {
    return UNKNOWN_TYPE;
    }


}


struct sspm_action_map get_action(struct mime_impl *impl,
                  enum sspm_major_type major,
                  enum sspm_minor_type minor)
{
    int i;

    /* Read caller suppled action map */

    if (impl->actions != 0){
    for(i=0; impl->actions[i].major != SSPM_UNKNOWN_MAJOR_TYPE; i++){
        if((major == impl->actions[i].major &&
           minor == impl->actions[i].minor) ||
           (major == impl->actions[i].major &&
        minor == SSPM_ANY_MINOR_TYPE)){
        return impl->actions[i];
        }
    }
    }

    /* Else, read default action map */

    for(i=0; sspm_action_map[i].major != SSPM_UNKNOWN_MAJOR_TYPE; i++){
        if((major == sspm_action_map[i].major &&
           minor == sspm_action_map[i].minor) ||
           (major == sspm_action_map[i].major &&
        minor == SSPM_ANY_MINOR_TYPE)){
        break;
    }
    }
    
    return sspm_action_map[i];
}


char* sspm_lowercase(char* str)
{
    char* p = 0;
    char* new = sspm_strdup(str);

    if(str ==0){
    return 0;
    }

    for(p = new; *p!=0; p++){
    *p = tolower(*p);
    }

    return new;
}

enum sspm_major_type sspm_find_major_content_type(char* type)
{
    int i;

    char* ltype = sspm_lowercase(type);

    for (i=0; major_content_type_map[i].type !=  SSPM_UNKNOWN_MINOR_TYPE; i++){
    if(strncmp(ltype, major_content_type_map[i].str,
           strlen(major_content_type_map[i].str))==0){
        free(ltype);
        return major_content_type_map[i].type;
    }
    }
    free(ltype);
    return major_content_type_map[i].type; /* Should return SSPM_UNKNOWN_MINOR_TYPE */
}

enum sspm_minor_type sspm_find_minor_content_type(char* type)
{
    int i;
    char* ltype = sspm_lowercase(type);

    char *p = strchr(ltype,'/');

    if (p==0){
    return SSPM_UNKNOWN_MINOR_TYPE; 
    }

    p++; /* Skip the '/' */

    for (i=0; minor_content_type_map[i].type !=  SSPM_UNKNOWN_MINOR_TYPE; i++){
    if(strncmp(p, minor_content_type_map[i].str,
           strlen(minor_content_type_map[i].str))==0){
        free(ltype);
        return minor_content_type_map[i].type;
    }
    }
    
    free(ltype);
    return minor_content_type_map[i].type; /* Should return SSPM_UNKNOWN_MINOR_TYPE */
}

char* sspm_major_type_string(enum sspm_major_type type)
{
    int i;

    for (i=0; major_content_type_map[i].type !=  SSPM_UNKNOWN_MINOR_TYPE; 
     i++){

    if(type == major_content_type_map[i].type){
        return major_content_type_map[i].str;
    }
    }
    
    return major_content_type_map[i].str; /* Should return SSPM_UNKNOWN_MINOR_TYPE */
}

char* sspm_minor_type_string(enum sspm_major_type type)
{
    int i;
    for (i=0; minor_content_type_map[i].type !=  SSPM_UNKNOWN_MINOR_TYPE; 
     i++){
    if(type == minor_content_type_map[i].type){
        return minor_content_type_map[i].str;
    }
    }
    
    return minor_content_type_map[i].str; /* Should return SSPM_UNKNOWN_MINOR_TYPE */
}


/* Interpret a header line and add its data to the header
   structure. */
void sspm_build_header(struct sspm_header *header, char* line)
{
    char *prop;
    char *val;
    
    val = sspm_strdup(sspm_value(line));
    prop = sspm_strdup(sspm_property_name(line));

    if(strcmp(prop,"Content-Type") == 0){
    
    /* Create a new mime_header, fill in content-type
       and possibly boundary */
    
    char* boundary= sspm_get_parameter(line,"boundary");
    
    header->def = 0;
    header->major = sspm_find_major_content_type(val);
    header->minor = sspm_find_minor_content_type(val);
    
    if(header->minor == SSPM_UNKNOWN_MINOR_TYPE){
        char *p = strchr(val,'/');
        
        if (p != 0){
        p++; /* Skip the '/' */
        
        header->minor_text = sspm_strdup(p);
        } else {
        /* Error, malformed content type */
        header->minor_text = sspm_strdup("unknown");
        }
    }
    if (boundary != 0){
        header->boundary = sspm_strdup(boundary);
    }
    
    } else if(strcmp(prop,"Content-Transfer-Encoding")==0){
    char* encoding = sspm_value(line);
    char* lencoding = sspm_lowercase(encoding);

    if(strcmp(lencoding,"base64")==0){
        header->encoding = SSPM_BASE64_ENCODING;
    } else  if(strcmp(lencoding,"quoted-printable")==0){
        header->encoding = SSPM_QUOTED_PRINTABLE_ENCODING;
    } else  if(strcmp(lencoding,"binary")==0){
        header->encoding = SSPM_BINARY_ENCODING;
    } else  if(strcmp(lencoding,"7bit")==0){
        header->encoding = SSPM_7BIT_ENCODING;
    } else  if(strcmp(lencoding,"8bit")==0){
        header->encoding = SSPM_8BIT_ENCODING;
    } else {
        header->encoding = SSPM_UNKNOWN_ENCODING;
    }

    free(lencoding);

    header->def = 0;
    
    } else if(strcmp(prop,"Content-Id")==0){
    char* cid = sspm_value(line);
    header->content_id = sspm_strdup(cid);
    header->def = 0;
    
    }
    free(val);
    free(prop);
}

char* sspm_get_next_line(struct mime_impl *impl)
{
    char* s;
    s = impl->get_string(impl->temp,TMP_BUF_SIZE,impl->get_string_data);
    
    if(s == 0){
    impl->state = END_OF_INPUT;
    }
    return s;
}


void sspm_store_part(struct mime_impl *impl, struct sspm_header header,
              int level, void *part)
{
    
    impl->parts[impl->part_no].header = header;
    impl->parts[impl->part_no].level = level;
    impl->parts[impl->part_no].data = part;  
    impl->part_no++;
}

void sspm_set_error(struct sspm_header* header, enum sspm_error error,
            char* message)
{
    header->error = error;

    if(header->error_text!=0){
    free(header->error_text);
    }

    header->def = 0;

    if(message != 0){
    header->error_text = sspm_strdup(message);  
    } else {
    header->error_text = 0;
    }

}

void* sspm_make_part(struct mime_impl *impl,
        struct sspm_header *header, 
        struct sspm_header *parent_header)
{

    /* For a single part type, read to the boundary, if there is a
   boundary. Otherwise, read until the end of input.  This routine
   assumes that the caller has read the header and has left the input
   at the first blank line */

    char *line;
    void *part, *end_part;
    int end = 0;

    struct sspm_action_map action = get_action(
    impl,
    header->major,
    header->minor);

    part =action.new_part();

    impl->state = IN_BODY;

    while(end == 0 && (line = sspm_get_next_line(impl)) != 0){
    
    if(sspm_is_mime_boundary(line)){
        
        /* If there is a boundary, then this must be a multipart
               part, so there must be a parent_header. */
        if(parent_header == 0){
        char* boundary;
        end = 1;
        end_part = 0;

        sspm_set_error(header,SSPM_UNEXPECTED_BOUNDARY_ERROR,line);

        /* Read until the paired terminating boundary */
        if((boundary = (char*)malloc(strlen(line)+5)) == 0){
            fprintf(stderr,"Out of memory");
            abort();
        }
        strcpy(boundary,line);
        strcat(boundary,"--");
        while((line = sspm_get_next_line(impl)) != 0){
            /*printf("Error: %s\n",line);*/
            if(strcmp(boundary,line)==0){
            break;
            }
        }
        free(boundary);

        break;
        }
        
        if(strncmp((line+2),parent_header->boundary,
               sizeof(parent_header->boundary)) == 0){
        end_part = action.end_part(part);

        if(sspm_is_mime_boundary(line)){
            impl->state = END_OF_PART;
        } else if ( sspm_is_mime_terminating_boundary(line)){
            impl->state = TERMINAL_END_OF_PART;
        }
        end = 1;
        } else {
        /* Error, this is not the correct terminating boundary*/

        /* read and discard until we get the right boundary.  */
            char* boundary;
            char msg[256];

            snprintf(msg,256,
                 "Expected: %s--. Got: %s",
                 parent_header->boundary,line);

            sspm_set_error(parent_header,
              SSPM_WRONG_BOUNDARY_ERROR,msg);

            /* Read until the paired terminating boundary */
            if((boundary = (char*)malloc(strlen(line)+5)) == 0){
            fprintf(stderr,"Out of memory");
            abort();
            }        
            strcpy(boundary,line);
            strcat(boundary,"--");
            while((line = sspm_get_next_line(impl)) != 0){
            /*printf("Error: %s\n",line);*/
            if(strcmp(boundary,line)==0){
                break;
            }
            }
            free(boundary);

        }   
    } else {
        size_t size;
        char* data;
        char* rtrn=0;
        size = strlen(line);

        data = (char*)malloc(size+2);
        if (header->encoding == SSPM_BASE64_ENCODING){
        rtrn = decode_base64(data,line,&size); 
        } else if(header->encoding == SSPM_QUOTED_PRINTABLE_ENCODING){
        rtrn = decode_quoted_printable(data,line,&size); 
        } 

        if(rtrn == 0){
        strcpy(data,line);
        }

        /* add a end-of-string after the data, just in case binary
               data from decode64 gets passed to a tring handling
               routine in add_line  */
        data[size+1]='\0';

        action.add_line(part,header,data,size);

        free(data);
    }
    }

    if (end == 0){
    /* End the part if the input is exhausted */
    end_part = action.end_part(part);
    }

    return end_part;
}


void* sspm_make_multipart_subpart(struct mime_impl *impl,
                struct sspm_header *parent_header)
{
    struct sspm_header header;
    char *line;
    void* part;

    if(parent_header->boundary == 0){
    /* Error. Multipart headers must have a boundary*/
    
    sspm_set_error(parent_header,SSPM_NO_BOUNDARY_ERROR,0);
    /* read all of the reamining lines */
    while((line = sspm_get_next_line(impl)) != 0){
    }  

    return 0;
    }


    /* Step 1: Read the opening boundary */

    if(get_line_type(impl->temp) != BOUNDARY){
    while((line=sspm_get_next_line(impl)) != 0 ){
        if(sspm_is_mime_boundary(line)){

        assert(parent_header != 0);

        /* Check if it is the right boundary */
        if(!sspm_is_mime_terminating_boundary(line) &&
           strncmp((line+2),parent_header->boundary, 
               sizeof(parent_header->boundary)) 
           == 0){
            /* The +2 in strncmp skips over the leading "--" */
            
            break;
        } else {
            /* Got the wrong boundary, so read and discard
                       until we get the right boundary.  */
            char* boundary;
            char msg[256];
            
            snprintf(msg,256,
                 "Expected: %s. Got: %s",
                 parent_header->boundary,line);

            sspm_set_error(parent_header,
                   SSPM_WRONG_BOUNDARY_ERROR,msg);

            /* Read until the paired terminating boundary */
            if((boundary = (char*)malloc(strlen(line)+5)) == 0){
            fprintf(stderr,"Out of memory");
            abort();
            }
            strcpy(boundary,line);
            strcat(boundary,"--");
            while((line = sspm_get_next_line(impl)) != 0){
            /*printf("Error: %s\n",line);*/
            if(strcmp(boundary,line)==0){
                break;
            }
            }
            free(boundary);
            
            return 0;
        }
        }
    }
    }

    /* Step 2: Get the part header */
    sspm_read_header(impl,&header);

    /* If the header is still listed as default, there was probably an
       error */
    if(header.def == 1 && header.error != SSPM_NO_ERROR){
    sspm_set_error(&header,SSPM_NO_HEADER_ERROR,0);
    return 0;
    }

    if(header.error!= SSPM_NO_ERROR){
    sspm_store_part(impl,header,impl->level,0);
    return 0;
    }   

    /* Step 3: read the body */
    
    if(header.major == SSPM_MULTIPART_MAJOR_TYPE){
    struct sspm_header *child_header;
    child_header = &(impl->parts[impl->part_no].header);

    /* Store the multipart part */
    sspm_store_part(impl,header,impl->level,0);

    /* now get all of the sub-parts */
    part = sspm_make_multipart_part(impl,child_header);

    if(get_line_type(impl->temp) != TERMINATING_BOUNDARY){

        sspm_set_error(child_header,SSPM_NO_BOUNDARY_ERROR,impl->temp);
        return 0;
    }
    
    sspm_get_next_line(impl); /* Step past the terminating boundary */

    } else {
    part = sspm_make_part(impl, &header,parent_header);

    memset(&(impl->parts[impl->part_no]), 0, sizeof(struct sspm_part));

    sspm_store_part(impl,header,impl->level,part);

    }

    return part;
}

void* sspm_make_multipart_part(struct mime_impl *impl,struct sspm_header *header)
{
    void *part=0;

    /* Now descend a level into each of the children of this part */
    impl->level++;

    /* Now we are working on the CHILD */
    memset(&(impl->parts[impl->part_no]), 0, sizeof(struct sspm_part));

    do{
    part = sspm_make_multipart_subpart(impl,header);

    if (part==0){
        /* Clean up the part in progress */
        impl->parts[impl->part_no].header.major 
        = SSPM_NO_MAJOR_TYPE;
        impl->parts[impl->part_no].header.minor 
        = SSPM_NO_MINOR_TYPE;

    }
    

    } while (get_line_type(impl->temp) != TERMINATING_BOUNDARY &&
    impl->state != END_OF_INPUT);

    impl->level--;

    return 0;
}


void sspm_read_header(struct mime_impl *impl,struct sspm_header *header)
{
#define BUF_SIZE 1024
#define MAX_HEADER_LINES 25

    char *buf;
    char header_lines[MAX_HEADER_LINES][BUF_SIZE]; /* HACK, hard limits */
    int current_line = -1;
    int end = 0;

    memset(header_lines,0,sizeof(header_lines));
    memset(header,0,sizeof(struct sspm_header));

    /* Set up default header */
    header->def = 1;
    header->major = SSPM_TEXT_MAJOR_TYPE;
    header->minor = SSPM_PLAIN_MINOR_TYPE;
    header->error = SSPM_NO_ERROR;
    header->error_text = 0;

    /* Read all of the lines into memory */
    while(end==0&& (buf=sspm_get_next_line(impl)) != 0){

    enum line_type line_type = get_line_type(buf);
    
    switch(line_type){
        case BLANK: {
        end = 1;
        impl->state = END_OF_HEADER;
        break;
        }

        case MAIL_HEADER:
        case MIME_HEADER: {     
        impl->state = IN_HEADER;
        current_line++;
        
        assert(strlen(buf) < BUF_SIZE);
        
        strcpy(header_lines[current_line],buf);
        
        break;
        }
        
        case HEADER_CONTINUATION: {
        char* last_line, *end;
        char *buf_start;

        if(current_line < 0){
            /* This is not really a continuation line, since
                       we have not see any header line yet */
            sspm_set_error(header,SSPM_MALFORMED_HEADER_ERROR,buf);
            return;
        }

        last_line = header_lines[current_line];
        end = (char*) ( (size_t)strlen(last_line)+
                      (size_t)last_line);
        
        impl->state = IN_HEADER;

        
        /* skip over the spaces in buf start, and remove the new
           line at the end of the lat line */
        if (last_line[strlen(last_line)-1] == '\n'){
            last_line[strlen(last_line)-1] = '\0';
        }
        buf_start = buf;
        while(*buf_start == ' ' ||*buf_start == '\t' ){
            buf_start++;
        }
        
        assert( strlen(buf_start) + strlen(last_line) < BUF_SIZE);
        
        strcat(last_line,buf_start);
        
        break;
        }
        
        default: {
        sspm_set_error(header,SSPM_MALFORMED_HEADER_ERROR,buf);
        return;
        }
    }
    }
    

    for(current_line = 0;
    current_line < MAX_HEADER_LINES && header_lines[current_line][0] != 0;
    current_line++){
    
    sspm_build_header(header,header_lines[current_line]);
    }


}

/* Root routine for parsing mime entries*/
int sspm_parse_mime(struct sspm_part *parts, 
            size_t max_parts,
            struct sspm_action_map *actions,
            char* (*get_string)(char *s, size_t size, void* data),
            void *get_string_data,
            struct sspm_header *first_header
    )
{
    struct mime_impl impl;
    struct sspm_header header;
    void *part;
    int i;

    /* Initialize all of the data */
    memset(&impl,0,sizeof(struct mime_impl));
    memset(&header,0,sizeof(struct sspm_header));

    for(i = 0; i<(int)max_parts; i++){
    parts[i].header.major = SSPM_NO_MAJOR_TYPE;
    parts[i].header.minor = SSPM_NO_MINOR_TYPE;
    }
    
    impl.parts = parts;
    impl.max_parts = max_parts;
    impl.part_no = 0;
    impl.actions = actions;
    impl.get_string = get_string;
    impl.get_string_data = get_string_data;

    /* Read the header of the message. This will be the email header,
       unless first_header is specified. But ( HACK) that var is not
       currently being used */
    sspm_read_header(&impl,&header);

    if(header.major == SSPM_MULTIPART_MAJOR_TYPE){
    struct sspm_header *child_header;
    child_header = &(impl.parts[impl.part_no].header);
    
    sspm_store_part(&impl,header,impl.level,0);

    part = sspm_make_multipart_part(&impl,child_header);

    } else {
    part = sspm_make_part(&impl, &header, 0);

    memset(&(impl.parts[impl.part_no]), 0, sizeof(struct sspm_part));
    
    sspm_store_part(&impl,header,impl.level,part);
    }

    return 0;
}

void sspm_free_parts(struct sspm_part *parts, size_t max_parts)
{
     int i;
    
    for(i = 0; i<(int)max_parts && parts[i].header.major != SSPM_NO_MAJOR_TYPE;
    i++){
    sspm_free_header(&(parts[i].header));
    }
}

void sspm_free_header(struct sspm_header *header)
{
    if(header->boundary!=0){
    free(header->boundary);
    }
    if(header->minor_text!=0){
    free(header->minor_text);
    }
    if(header->charset!=0){
    free(header->charset);
    }
    if(header->filename!=0){
    free(header->filename);
    }
    if(header->content_id!=0){
    free(header->content_id);
    }
    if(header->error_text!=0){
    free(header->error_text);
    }
}

/***********************************************************************
The remaining code is beased on code from the mimelite distribution,
which has the following notice:

| Authorship:
|    Copyright (c) 1994 Gisle Hannemyr.
|    Permission is granted to hack, make and distribute copies of this
|    program as long as this copyright notice is not removed.
|    Flames, bug reports, comments and improvements to:
|       snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway
|       email: Inet: gisle@oslonett.no

The code is heavily modified by Eric Busboom. 

***********************************************************************/

unsigned char *decode_quoted_printable(unsigned char *dest, 
                       unsigned char *src,
                       size_t *size)
{
    int cc;
    size_t i=0;

    while (*src != 0 && i < *size) {
    if (*src == '=') {

        src++; 
        if (!*src) {
        break;
        }

        /* remove soft line breaks*/
        if ((*src == '\n') || (*src == '\r')){
        src++;
        if ((*src == '\n') || (*src == '\r')){
            src++;
        }
        continue;
        }

        cc  = isdigit(*src) ? (*src - '0') : (*src - 55);
        cc *= 0x10;
        src++; 
        if (!*src) {
        break;
        }
        cc += isdigit(*src) ? (*src - '0') : (*src - 55);

        *dest = cc;

    } else {
        *dest = *src;
    }
    
    dest++;
    src++;
    i++;
    }
    
    *dest = '\0';
    
    *size = i;
    return(dest);
}

unsigned char *decode_base64(unsigned char *dest, 
                       unsigned char *src,
                       size_t *size)
{
    int cc;
    unsigned char buf[4] = {0,0,0,0};  
    int p = 0;
    int valid_data = 0;
    size_t size_out=0;
    
    while (*src && p<(int)*size && (cc!=  -1)) {
    
    /* convert a character into the Base64 alphabet */
    cc = *src++;
    
    if  ((cc >= 'A') && (cc <= 'Z')) cc = cc - 'A';
    else if ((cc >= 'a') && (cc <= 'z')) cc = cc - 'a' + 26;
    else if ((cc >= '0') && (cc <= '9')) cc = cc - '0' + 52;
    else if  (cc == '/')             cc = 63;
    else if  (cc == '+')             cc = 62;
    else                                 cc = -1;
    
    assert(cc<64);

    /* If we've reached the end, fill the remaining slots in
       the bucket and do a final conversion */
    if(cc== -1){
        if(valid_data == 0){
        return 0;
        }

        while(p%4!=3){
        p++;
        buf[p%4] = 0;
        }
    } else {
        buf[p%4] = cc;
        size_out++;
        valid_data = 1;
    }

    
    /* When we have 4 base64 letters, convert them into three
       bytes */
    if (p%4 == 3) {
        *dest++ =(buf[0]<< 2)|((buf[1] & 0x30) >> 4);
        *dest++ =((buf[1] & 0x0F) << 4)|((buf[2] & 0x3C) >> 2);
        *dest++ =((buf[2] & 0x03) << 6)|(buf[3] & 0x3F);

        memset(buf,0,4);
    }

    p++;

    }
    /* Calculate the size of the converted data*/
   *size = ((int)(size_out/4))*3;
    if(size_out%4 == 2) *size+=1;
    if(size_out%4 == 3) *size+=2;

    return(dest);
}