comparison src/readpst.c @ 164:ab384fed78c5

Compensate for iconv conversion to utf-7 that produces strings that are not null terminated. Don't produce empty attachment files in separate mode.
author Carl Byington <carl@five-ten-sg.com>
date Mon, 16 Mar 2009 18:31:39 -0700
parents 581fab9f1dc7
children 40e9de445038
comparison
equal deleted inserted replaced
163:03fbb0269f3c 164:ab384fed78c5
135 if (!d_ptr->desc) { 135 if (!d_ptr->desc) {
136 DEBUG_WARN(("main: ERROR item's desc record is NULL\n")); 136 DEBUG_WARN(("main: ERROR item's desc record is NULL\n"));
137 ff.skip_count++; 137 ff.skip_count++;
138 } 138 }
139 else { 139 else {
140 DEBUG_MAIN(("main: Desc Email ID %#"PRIx64" [d_ptr->d_id = %#"PRIx64"]\n", d_ptr->desc->id, d_ptr->d_id)); 140 DEBUG_MAIN(("main: Desc Email ID %#"PRIx64" [d_ptr->d_id = %#"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_id));
141 141
142 item = pst_parse_item(&pstfile, d_ptr, NULL); 142 item = pst_parse_item(&pstfile, d_ptr, NULL);
143 DEBUG_MAIN(("main: About to process item\n")); 143 DEBUG_MAIN(("main: About to process item\n"));
144 if (item && item->subject.str) { 144 if (item && item->subject.str) {
145 DEBUG_EMAIL(("item->subject = %s\n", item->subject.str)); 145 DEBUG_EMAIL(("item->subject = %s\n", item->subject.str));
408 408
409 409
410 void write_email_body(FILE *f, char *body) { 410 void write_email_body(FILE *f, char *body) {
411 char *n = body; 411 char *n = body;
412 DEBUG_ENT("write_email_body"); 412 DEBUG_ENT("write_email_body");
413 DEBUG_INFO(("buffer pointer %p\n", body));
413 while (n) { 414 while (n) {
414 if (strncmp(body, "From ", 5) == 0) 415 if (strncmp(body, "From ", 5) == 0)
415 fprintf(f, ">"); 416 fprintf(f, ">");
416 if ((n = strchr(body, '\n'))) { 417 if ((n = strchr(body, '\n'))) {
417 n++; 418 n++;
729 // use the 8.3 filename (filename1) 730 // use the 8.3 filename (filename1)
730 char *attach_filename = (attach->filename2.str) ? attach->filename2.str 731 char *attach_filename = (attach->filename2.str) ? attach->filename2.str
731 : attach->filename1.str; 732 : attach->filename1.str;
732 DEBUG_ENT("write_separate_attachment"); 733 DEBUG_ENT("write_separate_attachment");
733 734
735 if (!attach->data) {
736 // make sure we can fetch data from the id
737 pst_index_ll *ptr = pst_getID(pst, attach->i_id);
738 if (!ptr) {
739 DEBUG_WARN(("Couldn't find i_id %#"PRIx64". Cannot save attachment to file\n", attach->i_id));
740 DEBUG_RET();
741 return;
742 }
743 }
744
734 check_filename(f_name); 745 check_filename(f_name);
735 if (!attach_filename) { 746 if (!attach_filename) {
736 // generate our own (dummy) filename for the attachement 747 // generate our own (dummy) filename for the attachement
737 temp = xmalloc(strlen(f_name)+15); 748 temp = xmalloc(strlen(f_name)+15);
738 sprintf(temp, "%s-attach%i", f_name, attach_num); 749 sprintf(temp, "%s-attach%i", f_name, attach_num);
753 DEBUG_EMAIL(("Saving attachment to %s\n", temp)); 764 DEBUG_EMAIL(("Saving attachment to %s\n", temp));
754 if (!(fp = fopen(temp, "w"))) { 765 if (!(fp = fopen(temp, "w"))) {
755 WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp)); 766 WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
756 } else { 767 } else {
757 if (attach->data) 768 if (attach->data)
758 pst_fwrite(attach->data, 1, attach->size, fp); 769 pst_fwrite(attach->data, (size_t)1, attach->size, fp);
759 else { 770 else {
760 (void)pst_attach_to_file(pst, attach, fp); 771 (void)pst_attach_to_file(pst, attach, fp);
761 } 772 }
762 fclose(fp); 773 fclose(fp);
763 } 774 }
770 { 781 {
771 pst_index_ll *ptr; 782 pst_index_ll *ptr;
772 DEBUG_ENT("write_embedded_message"); 783 DEBUG_ENT("write_embedded_message");
773 fprintf(f_output, "\n--%s\n", boundary); 784 fprintf(f_output, "\n--%s\n", boundary);
774 fprintf(f_output, "Content-Type: %s\n\n", attach->mimetype.str); 785 fprintf(f_output, "Content-Type: %s\n\n", attach->mimetype.str);
775 ptr = pst_getID(pf, attach->id_val); 786 ptr = pst_getID(pf, attach->i_id);
776 787
777 pst_desc_ll d_ptr; 788 pst_desc_ll d_ptr;
778 d_ptr.d_id = 0; 789 d_ptr.d_id = 0;
779 d_ptr.parent_d_id = 0; 790 d_ptr.parent_d_id = 0;
780 d_ptr.assoc_tree = NULL; 791 d_ptr.assoc_tree = NULL;
797 void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst) 808 void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst)
798 { 809 {
799 char *attach_filename; 810 char *attach_filename;
800 char *enc = NULL; // base64 encoded attachment 811 char *enc = NULL; // base64 encoded attachment
801 DEBUG_ENT("write_inline_attachment"); 812 DEBUG_ENT("write_inline_attachment");
802 DEBUG_EMAIL(("Attachment Size is %i, pointer %p, id %d\n", attach->size, attach->data, attach->id_val)); 813 DEBUG_EMAIL(("Attachment Size is %"PRIu64", id %#"PRIx64"\n", (uint64_t)attach->size, attach->i_id));
803 if (attach->data) { 814 if (attach->data) {
804 enc = base64_encode (attach->data, attach->size); 815 enc = base64_encode (attach->data, attach->size);
805 if (!enc) { 816 if (!enc) {
806 DEBUG_EMAIL(("ERROR base64_encode returned NULL. Must have failed\n")); 817 DEBUG_EMAIL(("ERROR base64_encode returned NULL. Must have failed\n"));
807 DEBUG_RET(); 818 DEBUG_RET();
808 return; 819 return;
809 } 820 }
810 } 821 }
811 else { 822 else {
812 // make sure we can fetch data from the id 823 // make sure we can fetch data from the id
813 pst_index_ll *ptr = pst_getID(pst, attach->id_val); 824 pst_index_ll *ptr = pst_getID(pst, attach->i_id);
814 if (!ptr) { 825 if (!ptr) {
815 DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n")); 826 DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n"));
816 DEBUG_RET(); 827 DEBUG_RET();
817 return; 828 return;
818 } 829 }
1021 // is not utf-8, and the data came from a unicode (utf16) field 1032 // is not utf-8, and the data came from a unicode (utf16) field
1022 // and is now in utf-8. 1033 // and is now in utf-8.
1023 size_t rc; 1034 size_t rc;
1024 DEBUG_EMAIL(("Convert %s utf-8 to %s\n", mime, charset)); 1035 DEBUG_EMAIL(("Convert %s utf-8 to %s\n", mime, charset));
1025 vbuf *newer = vballoc(2); 1036 vbuf *newer = vballoc(2);
1026 rc = vb_utf8to8bit(newer, body->str, strlen(body->str) + 1, charset); 1037 rc = vb_utf8to8bit(newer, body->str, strlen(body->str), charset);
1027 if (rc == (size_t)-1) { 1038 if (rc == (size_t)-1) {
1028 // unable to convert, change the charset to utf8 1039 // unable to convert, change the charset to utf8
1029 free(newer->b); 1040 free(newer->b);
1030 DEBUG_EMAIL(("Failed to convert %s utf-8 to %s\n", mime, charset)); 1041 DEBUG_EMAIL(("Failed to convert %s utf-8 to %s\n", mime, charset));
1031 charset = "utf-8"; 1042 charset = "utf-8";
1032 } 1043 }
1033 else { 1044 else {
1045 // null terminate the output string
1046 vbgrow(newer, 1);
1047 newer->b[newer->dlen] = '\0';
1034 free(body->str); 1048 free(body->str);
1035 body->str = newer->b; 1049 body->str = newer->b;
1036 } 1050 }
1037 free(newer); 1051 free(newer);
1038 } 1052 }
1321 if (!attach->data && attach->mimetype.str && !strcmp(attach->mimetype.str, RFC822)) { 1335 if (!attach->data && attach->mimetype.str && !strcmp(attach->mimetype.str, RFC822)) {
1322 DEBUG_EMAIL(("seem to have special embedded message attachment\n")); 1336 DEBUG_EMAIL(("seem to have special embedded message attachment\n"));
1323 find_rfc822_headers(extra_mime_headers); 1337 find_rfc822_headers(extra_mime_headers);
1324 write_embedded_message(f_output, attach, boundary, pst, extra_mime_headers); 1338 write_embedded_message(f_output, attach, boundary, pst, extra_mime_headers);
1325 } 1339 }
1326 else if (mode == MODE_SEPARATE && !mode_MH) 1340 else if (attach->data || attach->i_id) {
1327 write_separate_attachment(f_name, attach, ++attach_num, pst); 1341 if (mode == MODE_SEPARATE && !mode_MH)
1328 else 1342 write_separate_attachment(f_name, attach, ++attach_num, pst);
1329 write_inline_attachment(f_output, attach, boundary, pst); 1343 else
1344 write_inline_attachment(f_output, attach, boundary, pst);
1345 }
1330 } 1346 }
1331 } 1347 }
1332 1348
1333 // end of this mail message 1349 // end of this mail message
1334 if (mode != MODE_SEPARATE) { /* do not add a boundary after the last attachment for mode_MH */ 1350 if (mode != MODE_SEPARATE) { /* do not add a boundary after the last attachment for mode_MH */