-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytnef.c
1400 lines (1243 loc) · 42.8 KB
/
ytnef.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 <string.h>
#include <stdlib.h>
#include "ytnef.h"
#include "tnef-errors.h"
#include "mapi.h"
#include "mapidefs.h"
#include "mapitags.h"
#include "config.h"
#define RTF_PREBUF "{\\rtf1\\ansi\\mac\\deff0\\deftab720{\\fonttbl;}{\\f0\\fnil \\froman \\fswiss \\fmodern \\fscript \\fdecor MS Sans SerifSymbolArialTimes New RomanCourier{\\colortbl\\red0\\green0\\blue0\n\r\\par \\pard\\plain\\f0\\fs20\\b\\i\\u\\tab\\tx"
#define DEBUG(lvl, curlvl, msg) \
if ((lvl) >= (curlvl)) \
printf("DEBUG(%i/%i): %s\n", curlvl, lvl, msg);
#define DEBUG1(lvl, curlvl, msg, var1) \
if ((lvl) >= (curlvl)) { \
printf("DEBUG(%i/%i):", curlvl, lvl, msg); \
printf(msg, var1); \
printf("\n"); \
}
#define DEBUG2(lvl, curlvl, msg, var1, var2) \
if ((lvl) >= (curlvl)) { \
printf("DEBUG(%i/%i):", curlvl, lvl, msg); \
printf(msg, var1, var2); \
printf("\n"); \
}
#define DEBUG3(lvl, curlvl, msg, var1, var2, var3) \
if ((lvl) >= (curlvl)) { \
printf("DEBUG(%i/%i):", curlvl, lvl, msg); \
printf(msg, var1, var2,var3); \
printf("\n"); \
}
#define MIN(x,y) (((x)<(y))?(x):(y))
void TNEFFillMapi(TNEFStruct *TNEF, BYTE *data, DWORD size, MAPIProps *p);
void SetFlip(void);
int TNEFDefaultHandler STD_ARGLIST;
int TNEFAttachmentFilename STD_ARGLIST;
int TNEFAttachmentSave STD_ARGLIST;
int TNEFDetailedPrint STD_ARGLIST;
int TNEFHexBreakdown STD_ARGLIST;
int TNEFBody STD_ARGLIST;
int TNEFRendData STD_ARGLIST;
int TNEFDateHandler STD_ARGLIST;
int TNEFPriority STD_ARGLIST;
int TNEFVersion STD_ARGLIST;
int TNEFMapiProperties STD_ARGLIST;
int TNEFIcon STD_ARGLIST;
int TNEFSubjectHandler STD_ARGLIST;
int TNEFFromHandler STD_ARGLIST;
int TNEFRecipTable STD_ARGLIST;
int TNEFAttachmentMAPI STD_ARGLIST;
int TNEFSentFor STD_ARGLIST;
int TNEFMessageClass STD_ARGLIST;
int TNEFMessageID STD_ARGLIST;
int TNEFParentID STD_ARGLIST;
int TNEFOriginalMsgClass STD_ARGLIST;
int TNEFCodePage STD_ARGLIST;
BYTE *TNEFFileContents=NULL;
DWORD TNEFFileContentsSize;
BYTE *TNEFFileIcon=NULL;
DWORD TNEFFileIconSize;
TNEFHandler TNEFList[] = {
{attNull, "Null", TNEFDefaultHandler},
{attFrom, "From", TNEFFromHandler},
{attSubject, "Subject", TNEFSubjectHandler},
{attDateSent, "Date Sent", TNEFDateHandler},
{attDateRecd, "Date Received", TNEFDateHandler},
{attMessageStatus, "Message Status", TNEFDefaultHandler},
{attMessageClass, "Message Class", TNEFMessageClass},
{attMessageID, "Message ID", TNEFMessageID},
{attParentID, "Parent ID", TNEFParentID},
{attConversationID, "Conversation ID", TNEFDefaultHandler},
{attBody, "Body", TNEFBody},
{attPriority, "Priority", TNEFPriority},
{attAttachData, "Attach Data", TNEFAttachmentSave},
{attAttachTitle, "Attach Title", TNEFAttachmentFilename},
{attAttachMetaFile, "Attach Meta-File", TNEFIcon},
{attAttachCreateDate, "Attachment Create Date", TNEFDateHandler},
{attAttachModifyDate, "Attachment Modify Date", TNEFDateHandler},
{attDateModified, "Date Modified", TNEFDateHandler},
{attAttachTransportFilename, "Attachment Transport name", TNEFDefaultHandler},
{attAttachRenddata, "Attachment Display info", TNEFRendData},
{attMAPIProps, "MAPI Properties", TNEFMapiProperties},
{attRecipTable, "Recip Table", TNEFRecipTable},
{attAttachment, "Attachment", TNEFAttachmentMAPI},
{attTnefVersion, "TNEF Version", TNEFVersion},
{attOemCodepage, "OEM CodePage", TNEFCodePage},
{attOriginalMessageClass, "Original Message Class", TNEFOriginalMsgClass},
{attOwner, "Owner", TNEFDefaultHandler},
{attSentFor, "Sent For", TNEFSentFor},
{attDelegate, "Delegate", TNEFDefaultHandler},
{attDateStart, "Date Start", TNEFDateHandler},
{attDateEnd, "Date End", TNEFDateHandler},
{attAidOwner, "Aid Owner", TNEFDefaultHandler},
{attRequestRes, "Request Response", TNEFDefaultHandler} };
WORD SwapWord(BYTE *p)
{
WORD *word_ptr;
#ifdef WORDS_BIGENDIAN
BYTE bytes[2];
bytes[0] = p[1];
bytes[1] = p[0];
word_ptr = (WORD*)&(bytes[0]);
#else
word_ptr = (WORD*)p;
#endif
return *word_ptr;
}
DWORD SwapDWord(BYTE *p)
{
DWORD *dword_ptr;
#ifdef WORDS_BIGENDIAN
BYTE bytes[4];
bytes[0] = p[3];
bytes[1] = p[2];
bytes[2] = p[1];
bytes[3] = p[0];
dword_ptr = (DWORD*)&(bytes[0]);
#else
dword_ptr = (DWORD*)p;
#endif
return *dword_ptr;
}
DDWORD SwapDDWord(BYTE *p)
{
DDWORD *ddword_ptr;
#ifdef WORDS_BIGENDIAN
BYTE bytes[8];
bytes[0] = p[7];
bytes[1] = p[6];
bytes[2] = p[5];
bytes[3] = p[4];
bytes[4] = p[3];
bytes[5] = p[2];
bytes[6] = p[1];
bytes[7] = p[0];
ddword_ptr = (DDWORD*)&(bytes[0]);
#else
ddword_ptr = (DDWORD*)p;
#endif
return *ddword_ptr;
}
/* convert 16-bit unicode to UTF8 unicode */
char* to_utf8(int len, char* buf)
{
int i, j = 0;
/* worst case length */
char *utf8 = malloc(3 * len/2 + 1);
for (i=0; i<len-1; i+=2) {
unsigned int c = SwapWord(buf+i);
if (c <= 0x007f) {
utf8[j++] = 0x00 | ((c & 0x007f) >> 0);
}
else if (c < 0x07ff) {
utf8[j++] = 0xc0 | ((c & 0x07c0) >> 6);
utf8[j++] = 0x80 | ((c & 0x003f) >> 0);
}
else {
utf8[j++] = 0xe0 | ((c & 0xf000) >> 12);
utf8[j++] = 0x80 | ((c & 0x0fc0) >> 6);
utf8[j++] = 0x80 | ((c & 0x003f) >> 0);
}
}
/* just in case the original was not null terminated */
utf8[j++] = '\0';
return utf8;
}
// -----------------------------------------------------------------------------
int TNEFDefaultHandler STD_ARGLIST {
if (TNEF->Debug >= 1)
printf("%s: [%i] %s\n", TNEFList[id].name, size, data);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFCodePage STD_ARGLIST {
TNEF->CodePage.size = size;
TNEF->CodePage.data = calloc(size, sizeof(BYTE));
memcpy(TNEF->CodePage.data, data, size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFParentID STD_ARGLIST {
memcpy(TNEF->parentID, data, MIN(size,sizeof(TNEF->parentID)));
return 0;
}
// -----------------------------------------------------------------------------
int TNEFMessageID STD_ARGLIST {
memcpy(TNEF->messageID, data, MIN(size,sizeof(TNEF->messageID)));
return 0;
}
// -----------------------------------------------------------------------------
int TNEFBody STD_ARGLIST {
TNEF->body.size = size;
TNEF->body.data = calloc(size, sizeof(BYTE));
memcpy(TNEF->body.data, data, size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFOriginalMsgClass STD_ARGLIST {
TNEF->OriginalMessageClass.size = size;
TNEF->OriginalMessageClass.data = calloc(size, sizeof(BYTE));
memcpy(TNEF->OriginalMessageClass.data, data, size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFMessageClass STD_ARGLIST {
memcpy(TNEF->messageClass, data, MIN(size,sizeof(TNEF->messageClass)));
return 0;
}
// -----------------------------------------------------------------------------
int TNEFFromHandler STD_ARGLIST {
TNEF->from.data = calloc(size, sizeof(BYTE));
TNEF->from.size = size;
memcpy(TNEF->from.data, data,size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFSubjectHandler STD_ARGLIST {
TNEF->subject.data = calloc(size, sizeof(BYTE));
TNEF->subject.size = size;
memcpy(TNEF->subject.data, data,size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFRendData STD_ARGLIST {
Attachment *p;
// Find the last attachment.
p = &(TNEF->starting_attach);
while (p->next!=NULL) p=p->next;
// Add a new one
p->next = calloc(1,sizeof(Attachment));
p=p->next;
TNEFInitAttachment(p);
memcpy(&(p->RenderData), data, sizeof(renddata));
return 0;
}
// -----------------------------------------------------------------------------
int TNEFVersion STD_ARGLIST {
WORD major;
WORD minor;
major = SwapWord(data+2);
minor = SwapWord(data);
sprintf(TNEF->version, "TNEF%i.%i", major, minor);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFIcon STD_ARGLIST {
Attachment *p;
// Find the last attachment.
p = &(TNEF->starting_attach);
while (p->next!=NULL) p=p->next;
p->IconData.size = size;
p->IconData.data = calloc(size, sizeof(BYTE));
memcpy(p->IconData.data, data, size);
}
// -----------------------------------------------------------------------------
int TNEFRecipTable STD_ARGLIST {
DWORD count;
BYTE *d;
int current_row;
int propcount;
int current_prop;
d = data;
count = SwapDWord(d);
d += 4;
// printf("Recipient Table containing %u rows\n", count);
return 0;
for(current_row=0; current_row<count; current_row++) {
propcount = SwapDWord(d);
if (TNEF->Debug >= 1)
printf("> Row %i contains %i properties\n", current_row, propcount);
d+=4;
for(current_prop=0; current_prop<propcount; current_prop++) {
}
}
return 0;
}
// -----------------------------------------------------------------------------
int TNEFAttachmentMAPI STD_ARGLIST {
Attachment *p;
// Find the last attachment.
//
p = &(TNEF->starting_attach);
while (p->next!=NULL) p=p->next;
TNEFFillMapi(TNEF, data, size, &(p->MAPI));
return 0;
}
// -----------------------------------------------------------------------------
int TNEFMapiProperties STD_ARGLIST {
TNEFFillMapi(TNEF, data, size, &(TNEF->MapiProperties));
return 0;
}
void TNEFFillMapi(TNEFStruct *TNEF, BYTE *data, DWORD size, MAPIProps *p) {
int i,j;
DWORD num;
BYTE *d;
MAPIProperty *mp;
DWORD type;
DWORD length;
variableLength *vl;
WORD temp_word;
DWORD temp_dword;
DDWORD temp_ddword;
int count=-1;
int offset;
d = data;
p->count = SwapDWord(data);
d += 4;
p->properties = calloc(p->count, sizeof(MAPIProperty));
mp = p->properties;
for(i=0; i<p->count; i++) {
if (count == -1) {
mp->id = SwapDWord(d);
d+=4;
mp->custom = 0;
mp->count = 1;
mp->namedproperty = 0;
length = -1;
if (PROP_ID(mp->id) >= 0x8000) {
// Read the GUID
memcpy(&(mp->guid[0]), d, 16);
d+=16;
length = SwapDWord(d);
d+=sizeof(DWORD);
if (length > 0 && length < 128000) {
mp->namedproperty = length;
mp->propnames = calloc(length, sizeof(variableLength));
while (length > 0) {
type = SwapDWord(d);
mp->propnames[length-1].data = calloc(type, sizeof(BYTE));
mp->propnames[length-1].size = type;
d+=4;
for(j=0; j<(type>>1); j++) {
mp->propnames[length-1].data[j] = d[j*2];
}
d += type + ((type % 4) ? (4 - type%4) : 0);
length--;
}
} else {
// READ the type
type = SwapDWord(d);
d+=sizeof(DWORD);
mp->id = PROP_TAG(PROP_TYPE(mp->id), type);
}
mp->custom = 1;
}
//printf("Type id = %04x\n", PROP_TYPE(mp->id));
if (PROP_TYPE(mp->id) & MV_FLAG) {
mp->id = PROP_TAG(PROP_TYPE(mp->id) - MV_FLAG, PROP_ID(mp->id));
mp->count = SwapDWord(d);
d+=4;
count = 0;
}
mp->data = calloc(mp->count, sizeof(variableLength));
vl = mp->data;
} else {
i--;
count++;
vl = &(mp->data[count]);
}
switch (PROP_TYPE(mp->id)) {
case PT_BINARY:
case PT_OBJECT:
case PT_STRING8:
case PT_UNICODE:
// First number of objects (assume 1 for now)
if (count == -1) {
vl->size = SwapDWord(d);
d+=4;
}
// now size of object
vl->size = SwapDWord(d);
d+=4;
// now actual object
if (PROP_TYPE(mp->id) == PT_UNICODE) {
vl->data = to_utf8(vl->size, d);
}
else {
vl->data = calloc(vl->size, sizeof(BYTE));
memcpy(vl->data, d, vl->size);
}
// Make sure to read in a multiple of 4
num = vl->size;
offset = ((num % 4) ? (4 - num%4) : 0);
d += num + ((num % 4) ? (4 - num%4) : 0);
break;
case PT_I2:
// Read in 2 bytes, but proceed by 4 bytes
vl->size = 2;
vl->data = calloc(vl->size, sizeof(WORD));
temp_word = SwapWord(d);
memcpy(vl->data, &temp_word, vl->size);
d += 4;
break;
case PT_BOOLEAN:
case PT_LONG:
case PT_R4:
case PT_CURRENCY:
case PT_APPTIME:
case PT_ERROR:
vl->size = 4;
vl->data = calloc(vl->size, sizeof(BYTE));
temp_dword = SwapDWord(d);
memcpy(vl->data, &temp_dword, vl->size);
d += 4;
break;
case PT_DOUBLE:
case PT_I8:
case PT_SYSTIME:
vl->size = 8;
vl->data = calloc(vl->size, sizeof(BYTE));
temp_ddword = SwapDDWord(d);
memcpy(vl->data, &temp_ddword, vl->size);
d+=8;
break;
}
if (count == (mp->count-1)) {
count = -1;
}
if (count == -1) {
mp++;
}
}
if ((d-data) < size) {
if (TNEF->Debug >= 1) {
printf("ERROR DURING MAPI READ\n");
printf("Read %i bytes, Expected %i bytes\n", (d-data), size);
printf("%i bytes missing\n", size - (d-data));
}
} else if ((d-data) > size){
if (TNEF->Debug >= 1) {
printf("ERROR DURING MAPI READ\n");
printf("Read %i bytes, Expected %i bytes\n", (d-data), size);
printf("%i bytes extra\n", (d-data)-size);
}
}
return;
}
// -----------------------------------------------------------------------------
int TNEFSentFor STD_ARGLIST {
WORD name_length, addr_length;
BYTE *d;
d=data;
while ((d-data)<size) {
name_length = SwapWord(d);
d+=sizeof(WORD);
if (TNEF->Debug >= 1)
printf("Sent For : %s", d);
d+=name_length;
addr_length = SwapWord(d);
d+=sizeof(WORD);
if (TNEF->Debug >= 1)
printf("<%s>\n", d);
d+=addr_length;
}
}
// -----------------------------------------------------------------------------
int TNEFDateHandler STD_ARGLIST {
dtr *Date;
Attachment *p;
WORD *tmp_src, *tmp_dst;
int i;
p = &(TNEF->starting_attach);
switch (TNEFList[id].id) {
case attDateSent: Date = &(TNEF->dateSent); break;
case attDateRecd: Date = &(TNEF->dateReceived); break;
case attDateModified: Date = &(TNEF->dateModified); break;
case attDateStart: Date = &(TNEF->DateStart); break;
case attDateEnd: Date = &(TNEF->DateEnd); break;
case attAttachCreateDate:
while (p->next!=NULL) p=p->next;
Date = &(p->CreateDate);
break;
case attAttachModifyDate:
while (p->next!=NULL) p=p->next;
Date = &(p->ModifyDate);
break;
default:
if (TNEF->Debug >= 1)
printf("MISSING CASE\n");
return YTNEF_UNKNOWN_PROPERTY;
}
tmp_src = (WORD*)data;
tmp_dst = (WORD*)Date;
for(i=0;i<sizeof(dtr)/sizeof(WORD);i++) {
*tmp_dst++ = SwapWord((BYTE*)tmp_src++);
}
return 0;
}
void TNEFPrintDate(dtr Date) {
char days[7][15] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
char months[12][15] = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"};
if (Date.wDayOfWeek < 7)
printf("%s ", days[Date.wDayOfWeek]);
if ((Date.wMonth < 13) && (Date.wMonth>0))
printf("%s ", months[Date.wMonth-1]);
printf("%hu, %hu ", Date.wDay, Date.wYear);
if (Date.wHour>12)
printf("%hu:%02hu:%02hu pm", (Date.wHour-12),
Date.wMinute, Date.wSecond);
else if (Date.wHour == 12)
printf("%hu:%02hu:%02hu pm", (Date.wHour),
Date.wMinute, Date.wSecond);
else
printf("%hu:%02hu:%02hu am", Date.wHour,
Date.wMinute, Date.wSecond);
}
// -----------------------------------------------------------------------------
int TNEFHexBreakdown STD_ARGLIST {
int i;
if (TNEF->Debug == 0)
return;
printf("%s: [%i bytes] \n", TNEFList[id].name, size);
for(i=0; i<size; i++) {
printf("%02x ", data[i]);
if ((i+1)%16 == 0) printf("\n");
}
printf("\n");
}
// -----------------------------------------------------------------------------
int TNEFDetailedPrint STD_ARGLIST {
int i;
if (TNEF->Debug == 0)
return;
printf("%s: [%i bytes] \n", TNEFList[id].name, size);
for(i=0; i<size; i++) {
printf("%c", data[i]);
}
printf("\n");
}
// -----------------------------------------------------------------------------
int TNEFAttachmentFilename STD_ARGLIST {
Attachment *p;
p = &(TNEF->starting_attach);
while (p->next!=NULL) p=p->next;
p->Title.size = size;
p->Title.data = calloc(size, sizeof(BYTE));
memcpy(p->Title.data, data, size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFAttachmentSave STD_ARGLIST {
Attachment *p;
p = &(TNEF->starting_attach);
while (p->next!=NULL) p=p->next;
p->FileData.data = calloc(sizeof(unsigned char), size);
p->FileData.size = size;
memcpy(p->FileData.data, data, size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFPriority STD_ARGLIST {
DWORD value;
value = SwapDWord(data);
switch (value) {
case 3:
sprintf((TNEF->priority), "high");
break;
case 2:
sprintf((TNEF->priority), "normal");
break;
case 1:
sprintf((TNEF->priority), "low");
break;
default:
sprintf((TNEF->priority), "N/A");
break;
}
return 0;
}
// -----------------------------------------------------------------------------
int TNEFCheckForSignature(DWORD sig) {
DWORD signature = 0x223E9F78;
sig = SwapDWord((BYTE*)&sig);
if (signature == sig) {
return 0;
} else {
return YTNEF_NOT_TNEF_STREAM;
}
}
// -----------------------------------------------------------------------------
int TNEFGetKey(TNEFStruct *TNEF, WORD *key) {
if (TNEF->IO.ReadProc (&(TNEF->IO), sizeof(WORD),1, key) < 1) {
if (TNEF->Debug >= 1)
printf("Error reading Key\n");
return YTNEF_ERROR_READING_DATA;
}
*key = SwapWord((BYTE*)key);
DEBUG1(TNEF->Debug, 2, "Key = %i", *key);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFGetHeader(TNEFStruct *TNEF, DWORD *type, DWORD *size) {
BYTE component;
unsigned char temp[8];
int i;
WORD wtemp;
DEBUG(TNEF->Debug, 2, "About to read Component");
if (TNEF->IO.ReadProc(&(TNEF->IO), sizeof(BYTE),1, &component) < 1) {
return YTNEF_ERROR_READING_DATA;
}
DEBUG(TNEF->Debug, 2, "About to read type");
if (TNEF->IO.ReadProc(&(TNEF->IO), sizeof(DWORD), 1, type) < 1) {
if (TNEF->Debug >= 1)
printf("ERROR: Error reading type\n");
return YTNEF_ERROR_READING_DATA;
}
DEBUG1(TNEF->Debug, 2, "Type = %i", *type);
DEBUG(TNEF->Debug, 2, "About to read size");
if (TNEF->IO.ReadProc(&(TNEF->IO), sizeof(DWORD), 1, size) < 1) {
if (TNEF->Debug >= 1)
printf("ERROR: Error reading size\n");
return YTNEF_ERROR_READING_DATA;
}
DEBUG1(TNEF->Debug, 2, "Size = %i", *size);
*type = SwapDWord((BYTE*)type);
*size = SwapDWord((BYTE*)size);
return 0;
}
// -----------------------------------------------------------------------------
int TNEFRawRead(TNEFStruct *TNEF, BYTE *data, DWORD size, WORD *checksum) {
WORD temp;
int i;
if (TNEF->IO.ReadProc(&TNEF->IO, sizeof(BYTE), size, data) < size) {
if (TNEF->Debug >= 1)
printf("ERROR: Error reading data\n");
return YTNEF_ERROR_READING_DATA;
}
if (checksum != NULL) {
*checksum = 0;
for(i=0; i<size; i++) {
temp = data[i];
*checksum = (*checksum + temp);
}
}
return 0;
}
#define INITVARLENGTH(x) (x).data = NULL; (x).size = 0;
#define INITDTR(x) (x).wYear=0; (x).wMonth=0; (x).wDay=0; \
(x).wHour=0; (x).wMinute=0; (x).wSecond=0; \
(x).wDayOfWeek=0;
#define INITSTR(x) memset((x), 0, sizeof(x));
void TNEFInitMapi(MAPIProps *p)
{
p->count = 0;
p->properties = NULL;
}
void TNEFInitAttachment(Attachment *p)
{
INITDTR(p->Date);
INITVARLENGTH(p->Title);
INITVARLENGTH(p->MetaFile);
INITDTR(p->CreateDate);
INITDTR(p->ModifyDate);
INITVARLENGTH(p->TransportFilename);
INITVARLENGTH(p->FileData);
INITVARLENGTH(p->IconData);
memset(&(p->RenderData), 0, sizeof(renddata));
TNEFInitMapi(&(p->MAPI));
p->next = NULL;
}
void TNEFInitialize(TNEFStruct *TNEF)
{
INITSTR(TNEF->version);
INITVARLENGTH(TNEF->from);
INITVARLENGTH(TNEF->subject);
INITDTR(TNEF->dateSent);
INITDTR(TNEF->dateReceived);
INITSTR(TNEF->messageStatus);
INITSTR(TNEF->messageClass);
INITSTR(TNEF->messageID);
INITSTR(TNEF->parentID);
INITSTR(TNEF->conversationID);
INITVARLENGTH(TNEF->body);
INITSTR(TNEF->priority);
TNEFInitAttachment(&(TNEF->starting_attach));
INITDTR(TNEF->dateModified);
TNEFInitMapi(&(TNEF->MapiProperties));
INITVARLENGTH(TNEF->CodePage);
INITVARLENGTH(TNEF->OriginalMessageClass);
INITVARLENGTH(TNEF->Owner);
INITVARLENGTH(TNEF->SentFor);
INITVARLENGTH(TNEF->Delegate);
INITDTR(TNEF->DateStart);
INITDTR(TNEF->DateEnd);
INITVARLENGTH(TNEF->AidOwner);
TNEF->RequestRes=0;
TNEF->IO.data = NULL;
TNEF->IO.InitProc = NULL;
TNEF->IO.ReadProc = NULL;
TNEF->IO.CloseProc = NULL;
}
#undef INITVARLENGTH
#undef INITDTR
#undef INITSTR
#define FREEVARLENGTH(x) if ((x).size > 0) { \
free((x).data); (x).size =0; }
void TNEFFree(TNEFStruct *TNEF) {
/*Attachment *p, *store;
FREEVARLENGTH(TNEF->from);
FREEVARLENGTH(TNEF->subject);
FREEVARLENGTH(TNEF->body);
FREEVARLENGTH(TNEF->CodePage);
FREEVARLENGTH(TNEF->OriginalMessageClass);
FREEVARLENGTH(TNEF->Owner);
FREEVARLENGTH(TNEF->SentFor);
FREEVARLENGTH(TNEF->Delegate);
FREEVARLENGTH(TNEF->AidOwner);
TNEFFreeMapiProps(&(TNEF->MapiProperties));
p = TNEF->starting_attach.next;
while (p != NULL) {
TNEFFreeAttachment(p);
store = p->next;
free(p);
p=store;
}*/
}
void TNEFFreeAttachment(Attachment *p)
{
/*FREEVARLENGTH(p->Title);
FREEVARLENGTH(p->MetaFile);
FREEVARLENGTH(p->TransportFilename);
FREEVARLENGTH(p->FileData);
FREEVARLENGTH(p->IconData);
TNEFFreeMapiProps(&(p->MAPI));*/
}
void TNEFFreeMapiProps(MAPIProps *p)
{
/*int i,j;
for(i=0; i<p->count; i++) {
for(j=0; j<p->properties[i].count; j++) {
FREEVARLENGTH(p->properties[i].data[j]);
}
free(p->properties[i].data);
}
free(p->properties);
p->count = 0;*/
}
#undef FREEVARLENGTH
// Procedures to handle File IO
int TNEFFile_Open (TNEFIOStruct *IO) {
TNEFFileInfo *finfo;
finfo = (TNEFFileInfo*)IO->data;
DEBUG1(finfo->Debug, 3, "Opening %s", finfo->filename);
if ((finfo->fptr = fopen(finfo->filename, "rb")) == NULL) {
return -1;
} else {
return 0;
}
}
int TNEFFile_Read (TNEFIOStruct *IO, int size, int count, void *dest) {
TNEFFileInfo *finfo;
finfo = (TNEFFileInfo*)IO->data;
DEBUG2(finfo->Debug, 3, "Reading %i blocks of %i size", count, size);
if (finfo->fptr != NULL) {
return fread((BYTE*)dest, size, count, finfo->fptr);
} else {
return -1;
}
}
int TNEFFile_Close (TNEFIOStruct *IO) {
TNEFFileInfo *finfo;
finfo = (TNEFFileInfo*)IO->data;
DEBUG1(finfo->Debug, 3, "Closing file %s", finfo->filename);
if (finfo->fptr != NULL) {
fclose(finfo->fptr);
finfo->fptr = NULL;
}
return 0;
}
int TNEFParseFile(char *filename, TNEFStruct *TNEF) {
TNEFFileInfo finfo;
if (TNEF->Debug >= 1)
printf("Attempting to parse %s...\n", filename);
finfo.filename = filename;
finfo.fptr = NULL;
finfo.Debug = TNEF->Debug;
TNEF->IO.data = (void*)&finfo;
TNEF->IO.InitProc = TNEFFile_Open;
TNEF->IO.ReadProc = TNEFFile_Read;
TNEF->IO.CloseProc = TNEFFile_Close;
return TNEFParse(TNEF);
}
//-------------------------------------------------------------
// Procedures to handle Memory IO
int TNEFMemory_Open (TNEFIOStruct *IO) {
TNEFMemInfo *minfo;
minfo = (TNEFMemInfo*)IO->data;
minfo->ptr = minfo->dataStart;
return 0;
}
int TNEFMemory_Read (TNEFIOStruct *IO, int size, int count, void *dest) {
TNEFMemInfo *minfo;
int length;
long max;
minfo = (TNEFMemInfo*)IO->data;
length = count*size;
max = (minfo->dataStart + minfo->size) - (minfo->ptr);
if (length > max) {
return -1;
}
DEBUG1(minfo->Debug, 3, "Copying %i bytes", length);
memcpy(dest, minfo->ptr, length);
minfo->ptr+=length;
return count;
}
int TNEFMemory_Close (TNEFIOStruct *IO) {
// Do nothing, really...
}
int TNEFParseMemory(BYTE *memory, long size, TNEFStruct *TNEF) {
TNEFMemInfo minfo;
DEBUG(TNEF->Debug, 1, "Attempting to parse memory block...\n");
minfo.dataStart = memory;
minfo.ptr = memory;
minfo.size = size;
minfo.Debug = TNEF->Debug;
TNEF->IO.data = (void*)&minfo;
TNEF->IO.InitProc = TNEFMemory_Open;
TNEF->IO.ReadProc = TNEFMemory_Read;
TNEF->IO.CloseProc = TNEFMemory_Close;
return TNEFParse(TNEF);
}
int TNEFParse(TNEFStruct *TNEF) {
WORD key;
DWORD type;
DWORD size;
DWORD signature;
BYTE *data;
WORD checksum, header_checksum;
int i;
if (TNEF->IO.ReadProc == NULL) {
printf("ERROR: Setup incorrectly: No ReadProc\n");
return YTNEF_INCORRECT_SETUP;
}
if (TNEF->IO.InitProc != NULL) {
DEBUG(TNEF->Debug, 2, "About to initialize");
if (TNEF->IO.InitProc (&TNEF->IO) != 0) {
return YTNEF_CANNOT_INIT_DATA;
}
DEBUG(TNEF->Debug, 2, "Initialization finished");
}
DEBUG(TNEF->Debug, 2, "Reading Signature");
if (TNEF->IO.ReadProc (&TNEF->IO, sizeof(DWORD), 1, &signature) < 1) {
printf("ERROR: Error reading signature\n");
if (TNEF->IO.CloseProc != NULL) {
TNEF->IO.CloseProc (&TNEF->IO);
}
return YTNEF_ERROR_READING_DATA;
}
DEBUG(TNEF->Debug, 2, "Checking Signature");
if (TNEFCheckForSignature(signature) < 0) {
printf("ERROR: Signature does not match. Not TNEF.\n");
if (TNEF->IO.CloseProc != NULL) {
TNEF->IO.CloseProc (&TNEF->IO);
}
return YTNEF_NOT_TNEF_STREAM;
}
DEBUG(TNEF->Debug, 2, "Reading Key.");
if (TNEFGetKey(TNEF, &key) < 0) {
printf("ERROR: Unable to retrieve key.\n");
if (TNEF->IO.CloseProc != NULL) {
TNEF->IO.CloseProc (&TNEF->IO);
}
return YTNEF_NO_KEY;
}
DEBUG(TNEF->Debug, 2, "Starting Full Processing.");
while (TNEFGetHeader(TNEF, &type, &size) == 0) {
DEBUG2(TNEF->Debug, 2, "Header says type=%i, size=%i", type, size);
if (size > 0) {
data = calloc(size, sizeof(BYTE));
if (TNEFRawRead(TNEF, data, size, &header_checksum)< 0) {
printf("ERROR: Unable to read data.\n");
if (TNEF->IO.CloseProc != NULL) {