-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibpst.c
4548 lines (4196 loc) · 197 KB
/
libpst.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
/***
* libpst.c
* Part of the LibPST project
* Written by David Smith
*/
#include "define.h"
#include "reverse_crc.c"
extern char *fname;
// switch to maximal packing for our own internal structures
// use the same code as in libpst.h
#ifdef _MSC_VER
#pragma pack(push, 1)
#endif
#if defined(__GNUC__) || defined (__SUNPRO_C) || defined(__SUNPRO_CC)
#pragma pack(1)
#endif
#define ASSERT(x) { if(!(x)) raise( SIGSEGV ); }
#define INDEX_TYPE32 0x0E
#define INDEX_TYPE32A 0x0F // unknown, but assumed to be similar for now
#define INDEX_TYPE64 0x17
#define INDEX_TYPE64A 0x15 // http://sourceforge.net/projects/libpff/
#define INDEX_TYPE_OFFSET (int64_t)0x0A
#define FILE_SIZE_POINTER32 (int64_t)0xA8
#define INDEX_POINTER32 (int64_t)0xC4
#define INDEX_BACK32 (int64_t)0xC0
#define SECOND_POINTER32 (int64_t)0xBC
#define SECOND_BACK32 (int64_t)0xB8
#define ENC_TYPE32 (int64_t)0x1CD
#define FILE_SIZE_POINTER64 (int64_t)0xB8
#define INDEX_POINTER64 (int64_t)0xF0
#define INDEX_BACK64 (int64_t)0xE8
#define SECOND_POINTER64 (int64_t)0xE0
#define SECOND_BACK64 (int64_t)0xD8
#define ENC_TYPE64 (int64_t)0x201
#define FILE_SIZE_POINTER ((pf->do_read64) ? FILE_SIZE_POINTER64 : FILE_SIZE_POINTER32)
#define INDEX_POINTER ((pf->do_read64) ? INDEX_POINTER64 : INDEX_POINTER32)
#define INDEX_BACK ((pf->do_read64) ? INDEX_BACK64 : INDEX_BACK32)
#define SECOND_POINTER ((pf->do_read64) ? SECOND_POINTER64 : SECOND_POINTER32)
#define SECOND_BACK ((pf->do_read64) ? SECOND_BACK64 : SECOND_BACK32)
#define ENC_TYPE ((pf->do_read64) ? ENC_TYPE64 : ENC_TYPE32)
#define PST_SIGNATURE 0x4E444221
typedef struct pst_block_offset {
uint16_t from;
uint16_t to;
} pst_block_offset;
typedef struct pst_block_offset_pointer {
char *from;
char *to;
int needfree;
} pst_block_offset_pointer;
typedef struct pst_holder {
char **buf;
FILE *fp;
int base64; // bool, are we encoding into base64
int base64_line_count; // base64 bytes emitted on the current line
size_t base64_extra; // count of bytes held in base64_extra_chars
char base64_extra_chars[2]; // up to two pending unencoded bytes
} pst_holder;
typedef struct pst_subblock {
char *buf;
size_t read_size;
size_t i_offset;
} pst_subblock;
typedef struct pst_subblocks {
size_t subblock_count;
pst_subblock *subs;
} pst_subblocks;
typedef struct pst_mapi_element {
uint32_t mapi_id;
char *data;
uint32_t type;
size_t size;
char *extra;
} pst_mapi_element;
typedef struct pst_mapi_object {
int32_t count_elements; // count of active elements
int32_t orig_count; // originally allocated elements
int32_t count_objects; // number of mapi objects in the list
struct pst_mapi_element **elements;
struct pst_mapi_object *next;
} pst_mapi_object;
typedef struct pst_desc32 {
uint32_t d_id;
uint32_t desc_id;
uint32_t tree_id;
uint32_t parent_d_id;
} pst_desc32;
typedef struct pst_index32 {
uint32_t id;
uint32_t offset;
uint16_t size;
int16_t u1;
} pst_index32;
struct pst_table_ptr_struct32{
uint32_t start;
uint32_t u1;
uint32_t offset;
};
typedef struct pst_desc {
uint64_t d_id;
uint64_t desc_id;
uint64_t tree_id;
uint32_t parent_d_id; // not 64 bit
uint32_t u1; // padding
} pst_desc;
typedef struct pst_index {
uint64_t id;
uint64_t offset;
uint16_t size;
int16_t u0;
int32_t u1;
} pst_index;
struct pst_table_ptr_struct{
uint64_t start;
uint64_t u1;
uint64_t offset;
};
typedef struct pst_block_header {
uint16_t type;
uint16_t count;
} pst_block_header;
typedef struct pst_id2_assoc32 {
uint32_t id2;
uint32_t id;
uint32_t child_id;
} pst_id2_assoc32;
typedef struct pst_id2_assoc {
uint32_t id2; // only 32 bit here
uint16_t unknown1;
uint16_t unknown2;
uint64_t id;
uint64_t child_id;
} pst_id2_assoc;
typedef struct pst_table3_rec32 {
uint32_t id;
} pst_table3_rec32; //for type 3 (0x0101) blocks
typedef struct pst_table3_rec {
uint64_t id;
} pst_table3_rec; //for type 3 (0x0101) blocks
typedef struct pst_block_hdr {
uint16_t index_offset;
uint16_t type;
uint32_t offset;
} pst_block_hdr;
/** for "compressible" encryption, just a simple substitution cipher,
* plaintext = comp_enc[ciphertext];
* for "strong" encryption, this is the first rotor of an Enigma 3 rotor cipher.
*/
static unsigned char comp_enc [] = {
0x47, 0xf1, 0xb4, 0xe6, 0x0b, 0x6a, 0x72, 0x48, 0x85, 0x4e, 0x9e, 0xeb, 0xe2, 0xf8, 0x94, 0x53,
0xe0, 0xbb, 0xa0, 0x02, 0xe8, 0x5a, 0x09, 0xab, 0xdb, 0xe3, 0xba, 0xc6, 0x7c, 0xc3, 0x10, 0xdd,
0x39, 0x05, 0x96, 0x30, 0xf5, 0x37, 0x60, 0x82, 0x8c, 0xc9, 0x13, 0x4a, 0x6b, 0x1d, 0xf3, 0xfb,
0x8f, 0x26, 0x97, 0xca, 0x91, 0x17, 0x01, 0xc4, 0x32, 0x2d, 0x6e, 0x31, 0x95, 0xff, 0xd9, 0x23,
0xd1, 0x00, 0x5e, 0x79, 0xdc, 0x44, 0x3b, 0x1a, 0x28, 0xc5, 0x61, 0x57, 0x20, 0x90, 0x3d, 0x83,
0xb9, 0x43, 0xbe, 0x67, 0xd2, 0x46, 0x42, 0x76, 0xc0, 0x6d, 0x5b, 0x7e, 0xb2, 0x0f, 0x16, 0x29,
0x3c, 0xa9, 0x03, 0x54, 0x0d, 0xda, 0x5d, 0xdf, 0xf6, 0xb7, 0xc7, 0x62, 0xcd, 0x8d, 0x06, 0xd3,
0x69, 0x5c, 0x86, 0xd6, 0x14, 0xf7, 0xa5, 0x66, 0x75, 0xac, 0xb1, 0xe9, 0x45, 0x21, 0x70, 0x0c,
0x87, 0x9f, 0x74, 0xa4, 0x22, 0x4c, 0x6f, 0xbf, 0x1f, 0x56, 0xaa, 0x2e, 0xb3, 0x78, 0x33, 0x50,
0xb0, 0xa3, 0x92, 0xbc, 0xcf, 0x19, 0x1c, 0xa7, 0x63, 0xcb, 0x1e, 0x4d, 0x3e, 0x4b, 0x1b, 0x9b,
0x4f, 0xe7, 0xf0, 0xee, 0xad, 0x3a, 0xb5, 0x59, 0x04, 0xea, 0x40, 0x55, 0x25, 0x51, 0xe5, 0x7a,
0x89, 0x38, 0x68, 0x52, 0x7b, 0xfc, 0x27, 0xae, 0xd7, 0xbd, 0xfa, 0x07, 0xf4, 0xcc, 0x8e, 0x5f,
0xef, 0x35, 0x9c, 0x84, 0x2b, 0x15, 0xd5, 0x77, 0x34, 0x49, 0xb6, 0x12, 0x0a, 0x7f, 0x71, 0x88,
0xfd, 0x9d, 0x18, 0x41, 0x7d, 0x93, 0xd8, 0x58, 0x2c, 0xce, 0xfe, 0x24, 0xaf, 0xde, 0xb8, 0x36,
0xc8, 0xa1, 0x80, 0xa6, 0x99, 0x98, 0xa8, 0x2f, 0x0e, 0x81, 0x65, 0x73, 0xe4, 0xc2, 0xa2, 0x8a,
0xd4, 0xe1, 0x11, 0xd0, 0x08, 0x8b, 0x2a, 0xf2, 0xed, 0x9a, 0x64, 0x3f, 0xc1, 0x6c, 0xf9, 0xec
};
/** for "strong" encryption, this is the second rotor of an Enigma 3 rotor cipher.
*/
static unsigned char comp_high1 [] = {
0x41, 0x36, 0x13, 0x62, 0xa8, 0x21, 0x6e, 0xbb, 0xf4, 0x16, 0xcc, 0x04, 0x7f, 0x64, 0xe8, 0x5d,
0x1e, 0xf2, 0xcb, 0x2a, 0x74, 0xc5, 0x5e, 0x35, 0xd2, 0x95, 0x47, 0x9e, 0x96, 0x2d, 0x9a, 0x88,
0x4c, 0x7d, 0x84, 0x3f, 0xdb, 0xac, 0x31, 0xb6, 0x48, 0x5f, 0xf6, 0xc4, 0xd8, 0x39, 0x8b, 0xe7,
0x23, 0x3b, 0x38, 0x8e, 0xc8, 0xc1, 0xdf, 0x25, 0xb1, 0x20, 0xa5, 0x46, 0x60, 0x4e, 0x9c, 0xfb,
0xaa, 0xd3, 0x56, 0x51, 0x45, 0x7c, 0x55, 0x00, 0x07, 0xc9, 0x2b, 0x9d, 0x85, 0x9b, 0x09, 0xa0,
0x8f, 0xad, 0xb3, 0x0f, 0x63, 0xab, 0x89, 0x4b, 0xd7, 0xa7, 0x15, 0x5a, 0x71, 0x66, 0x42, 0xbf,
0x26, 0x4a, 0x6b, 0x98, 0xfa, 0xea, 0x77, 0x53, 0xb2, 0x70, 0x05, 0x2c, 0xfd, 0x59, 0x3a, 0x86,
0x7e, 0xce, 0x06, 0xeb, 0x82, 0x78, 0x57, 0xc7, 0x8d, 0x43, 0xaf, 0xb4, 0x1c, 0xd4, 0x5b, 0xcd,
0xe2, 0xe9, 0x27, 0x4f, 0xc3, 0x08, 0x72, 0x80, 0xcf, 0xb0, 0xef, 0xf5, 0x28, 0x6d, 0xbe, 0x30,
0x4d, 0x34, 0x92, 0xd5, 0x0e, 0x3c, 0x22, 0x32, 0xe5, 0xe4, 0xf9, 0x9f, 0xc2, 0xd1, 0x0a, 0x81,
0x12, 0xe1, 0xee, 0x91, 0x83, 0x76, 0xe3, 0x97, 0xe6, 0x61, 0x8a, 0x17, 0x79, 0xa4, 0xb7, 0xdc,
0x90, 0x7a, 0x5c, 0x8c, 0x02, 0xa6, 0xca, 0x69, 0xde, 0x50, 0x1a, 0x11, 0x93, 0xb9, 0x52, 0x87,
0x58, 0xfc, 0xed, 0x1d, 0x37, 0x49, 0x1b, 0x6a, 0xe0, 0x29, 0x33, 0x99, 0xbd, 0x6c, 0xd9, 0x94,
0xf3, 0x40, 0x54, 0x6f, 0xf0, 0xc6, 0x73, 0xb8, 0xd6, 0x3e, 0x65, 0x18, 0x44, 0x1f, 0xdd, 0x67,
0x10, 0xf1, 0x0c, 0x19, 0xec, 0xae, 0x03, 0xa1, 0x14, 0x7b, 0xa9, 0x0b, 0xff, 0xf8, 0xa3, 0xc0,
0xa2, 0x01, 0xf7, 0x2e, 0xbc, 0x24, 0x68, 0x75, 0x0d, 0xfe, 0xba, 0x2f, 0xb5, 0xd0, 0xda, 0x3d
};
/** for "strong" encryption, this is the third rotor of an Enigma 3 rotor cipher.
*/
static unsigned char comp_high2 [] = {
0x14, 0x53, 0x0f, 0x56, 0xb3, 0xc8, 0x7a, 0x9c, 0xeb, 0x65, 0x48, 0x17, 0x16, 0x15, 0x9f, 0x02,
0xcc, 0x54, 0x7c, 0x83, 0x00, 0x0d, 0x0c, 0x0b, 0xa2, 0x62, 0xa8, 0x76, 0xdb, 0xd9, 0xed, 0xc7,
0xc5, 0xa4, 0xdc, 0xac, 0x85, 0x74, 0xd6, 0xd0, 0xa7, 0x9b, 0xae, 0x9a, 0x96, 0x71, 0x66, 0xc3,
0x63, 0x99, 0xb8, 0xdd, 0x73, 0x92, 0x8e, 0x84, 0x7d, 0xa5, 0x5e, 0xd1, 0x5d, 0x93, 0xb1, 0x57,
0x51, 0x50, 0x80, 0x89, 0x52, 0x94, 0x4f, 0x4e, 0x0a, 0x6b, 0xbc, 0x8d, 0x7f, 0x6e, 0x47, 0x46,
0x41, 0x40, 0x44, 0x01, 0x11, 0xcb, 0x03, 0x3f, 0xf7, 0xf4, 0xe1, 0xa9, 0x8f, 0x3c, 0x3a, 0xf9,
0xfb, 0xf0, 0x19, 0x30, 0x82, 0x09, 0x2e, 0xc9, 0x9d, 0xa0, 0x86, 0x49, 0xee, 0x6f, 0x4d, 0x6d,
0xc4, 0x2d, 0x81, 0x34, 0x25, 0x87, 0x1b, 0x88, 0xaa, 0xfc, 0x06, 0xa1, 0x12, 0x38, 0xfd, 0x4c,
0x42, 0x72, 0x64, 0x13, 0x37, 0x24, 0x6a, 0x75, 0x77, 0x43, 0xff, 0xe6, 0xb4, 0x4b, 0x36, 0x5c,
0xe4, 0xd8, 0x35, 0x3d, 0x45, 0xb9, 0x2c, 0xec, 0xb7, 0x31, 0x2b, 0x29, 0x07, 0x68, 0xa3, 0x0e,
0x69, 0x7b, 0x18, 0x9e, 0x21, 0x39, 0xbe, 0x28, 0x1a, 0x5b, 0x78, 0xf5, 0x23, 0xca, 0x2a, 0xb0,
0xaf, 0x3e, 0xfe, 0x04, 0x8c, 0xe7, 0xe5, 0x98, 0x32, 0x95, 0xd3, 0xf6, 0x4a, 0xe8, 0xa6, 0xea,
0xe9, 0xf3, 0xd5, 0x2f, 0x70, 0x20, 0xf2, 0x1f, 0x05, 0x67, 0xad, 0x55, 0x10, 0xce, 0xcd, 0xe3,
0x27, 0x3b, 0xda, 0xba, 0xd7, 0xc2, 0x26, 0xd4, 0x91, 0x1d, 0xd2, 0x1c, 0x22, 0x33, 0xf8, 0xfa,
0xf1, 0x5a, 0xef, 0xcf, 0x90, 0xb6, 0x8b, 0xb5, 0xbd, 0xc0, 0xbf, 0x08, 0x97, 0x1e, 0x6c, 0xe2,
0x61, 0xe0, 0xc6, 0xc1, 0x59, 0xab, 0xbb, 0x58, 0xde, 0x5f, 0xdf, 0x60, 0x79, 0x7e, 0xb2, 0x8a
};
static size_t pst_append_holder(pst_holder *h, size_t size, char **buf, size_t z);
static int pst_build_desc_ptr(pst_file *pf, int64_t offset, int32_t depth, uint64_t linku1, uint64_t start_val, uint64_t end_val);
static pst_id2_tree* pst_build_id2(pst_file *pf, pst_index_ll* list);
static int pst_build_id_ptr(pst_file *pf, int64_t offset, int32_t depth, uint64_t linku1, uint64_t start_val, uint64_t end_val);
static int pst_chr_count(char *str, char x);
static size_t pst_ff_compile_ID(pst_file *pf, uint64_t i_id, pst_holder *h, size_t size);
static size_t pst_ff_getIDblock(pst_file *pf, uint64_t i_id, char** buf);
static size_t pst_ff_getID2block(pst_file *pf, uint64_t id2, pst_id2_tree *id2_head, char** buf);
static size_t pst_ff_getID2data(pst_file *pf, pst_index_ll *ptr, pst_holder *h);
static size_t pst_finish_cleanup_holder(pst_holder *h, size_t size);
static void pst_free_attach(pst_item_attach *attach);
static void pst_free_desc (pst_desc_tree *head);
static void pst_free_id2(pst_id2_tree * head);
static void pst_free_id (pst_index_ll *head);
static void pst_free_list(pst_mapi_object *list);
static void pst_free_xattrib(pst_x_attrib_ll *x);
static size_t pst_getAtPos(pst_file *pf, int64_t pos, void* buf, size_t size);
static int pst_getBlockOffsetPointer(pst_file *pf, pst_id2_tree *i2_head, pst_subblocks *subblocks, uint32_t offset, pst_block_offset_pointer *p);
static int pst_getBlockOffset(char *buf, size_t read_size, uint32_t i_offset, uint32_t offset, pst_block_offset *p);
static pst_id2_tree* pst_getID2(pst_id2_tree * ptr, uint64_t id);
static pst_desc_tree* pst_getDptr(pst_file *pf, uint64_t d_id);
static uint64_t pst_getIntAt(pst_file *pf, char *buf);
static uint64_t pst_getIntAtPos(pst_file *pf, int64_t pos);
static pst_mapi_object* pst_parse_block(pst_file *pf, uint64_t block_id, pst_id2_tree *i2_head);
static void pst_printDptr(pst_file *pf, pst_desc_tree *ptr);
static void pst_printID2ptr(pst_id2_tree *ptr);
static int pst_process(uint64_t block_id, pst_mapi_object *list, pst_item *item, pst_item_attach *attach);
static size_t pst_read_block_size(pst_file *pf, int64_t offset, size_t size, char **buf);
static int pst_decrypt(uint64_t i_id, char *buf, size_t size, unsigned char type);
static int pst_stricmp(char *a, char *b);
static int pst_strincmp(char *a, char *b, size_t x);
static char* pst_wide_to_single(char *wt, size_t size);
int pst_open(pst_file *pf, const char *name, const char *charset) {
int32_t sig;
pst_unicode_init();
DEBUG_ENT("pst_open");
if (!pf) {
WARN (("cannot be passed a NULL pst_file\n"));
DEBUG_RET();
return -1;
}
memset(pf, 0, sizeof(*pf));
// pf->charset = charset;
if ((pf->fp = fopen(name, "rb")) == NULL) {
perror("Error opening PST file");
DEBUG_RET();
return -1;
}
// Check pst file magic
if (pst_getAtPos(pf, 0, &sig, sizeof(sig)) != sizeof(sig)) {
(void)fclose(pf->fp);
DEBUG_WARN(("cannot read signature from PST file. Closing with error\n"));
DEBUG_RET();
return -1;
}
LE32_CPU(sig);
DEBUG_INFO(("sig = %X\n", sig));
if (sig != (int32_t)PST_SIGNATURE) {
(void)fclose(pf->fp);
DEBUG_WARN(("not a PST file that I know. Closing with error\n"));
DEBUG_RET();
return -1;
}
// read index type
(void)pst_getAtPos(pf, INDEX_TYPE_OFFSET, &(pf->ind_type), sizeof(pf->ind_type));
DEBUG_INFO(("index_type = %i\n", pf->ind_type));
switch (pf->ind_type) {
case INDEX_TYPE32 :
case INDEX_TYPE32A :
pf->do_read64 = 0;
break;
case INDEX_TYPE64 :
case INDEX_TYPE64A :
pf->do_read64 = 1;
break;
default:
(void)fclose(pf->fp);
DEBUG_WARN(("unknown .pst format, possibly newer than Outlook 2003 PST file?\n"));
DEBUG_RET();
return -1;
}
// read encryption setting
(void)pst_getAtPos(pf, ENC_TYPE, &(pf->encryption), sizeof(pf->encryption));
DEBUG_INFO(("encrypt = %i\n", pf->encryption));
pf->index2_back = pst_getIntAtPos(pf, SECOND_BACK);
pf->index2 = pst_getIntAtPos(pf, SECOND_POINTER);
pf->size = pst_getIntAtPos(pf, FILE_SIZE_POINTER);
DEBUG_INFO(("Pointer2 is %#"PRIx64", back pointer2 is %#"PRIx64"\n", pf->index2, pf->index2_back));
pf->index1_back = pst_getIntAtPos(pf, INDEX_BACK);
pf->index1 = pst_getIntAtPos(pf, INDEX_POINTER);
DEBUG_INFO(("Pointer1 is %#"PRIx64", back pointer2 is %#"PRIx64"\n", pf->index1, pf->index1_back));
DEBUG_RET();
pf->cwd = pst_malloc(PATH_MAX+1);
getcwd(pf->cwd, PATH_MAX+1);
pf->fname = strdup(name);
return 0;
}
int pst_reopen(pst_file *pf) {
char cwd[PATH_MAX];
if (!getcwd(cwd, PATH_MAX)) return -1;
if (chdir(pf->cwd)) return -1;
if (!freopen(pf->fname, "rb", pf->fp)) return -1;
if (chdir(cwd)) return -1;
return 0;
}
int pst_close(pst_file *pf) {
DEBUG_ENT("pst_close");
if (!pf->fp) {
DEBUG_RET();
return 0;
}
if (fclose(pf->fp)) {
DEBUG_WARN(("fclose returned non-zero value\n"));
}
// free the paths
free(pf->cwd);
free(pf->fname);
// we must free the id linklist and the desc tree
pst_free_id(pf->i_head);
pst_free_desc(pf->d_head);
pst_free_xattrib(pf->x_head);
DEBUG_RET();
return 0;
}
/**
* add a pst descriptor node to a linked list of such nodes.
*
* @param node pointer to the node to be added to the list
* @param head pointer to the list head pointer
* @param tail pointer to the list tail pointer
*/
static void add_descriptor_to_list(pst_desc_tree *node, pst_desc_tree **head, pst_desc_tree **tail);
static void add_descriptor_to_list(pst_desc_tree *node, pst_desc_tree **head, pst_desc_tree **tail)
{
DEBUG_ENT("add_descriptor_to_list");
//DEBUG_INFO(("Added node %#"PRIx64" parent %#"PRIx64" real parent %#"PRIx64" prev %#"PRIx64" next %#"PRIx64"\n",
// node->id, node->parent_d_id,
// (node->parent ? node->parent->id : (uint64_t)0),
// (node->prev ? node->prev->id : (uint64_t)0),
// (node->next ? node->next->id : (uint64_t)0)));
if (*tail) (*tail)->next = node;
if (!(*head)) *head = node;
node->prev = *tail;
node->next = NULL;
*tail = node;
DEBUG_RET();
}
/**
* add a pst descriptor node into the global tree.
*
* @param pf global pst file pointer
* @param node pointer to the new node to be added to the tree
*/
static void record_descriptor(pst_file *pf, pst_desc_tree *node);
static void record_descriptor(pst_file *pf, pst_desc_tree *node)
{
DEBUG_ENT("record_descriptor");
// finish node initialization
node->parent = NULL;
node->child = NULL;
node->child_tail = NULL;
node->no_child = 0;
// find any orphan children of this node, and collect them
pst_desc_tree *n = pf->d_head;
while (n) {
if (n->parent_d_id == node->d_id) {
// found a child of this node
DEBUG_INFO(("Found orphan child %#"PRIx64" of parent %#"PRIx64"\n", n->d_id, node->d_id));
pst_desc_tree *nn = n->next;
pst_desc_tree *pp = n->prev;
node->no_child++;
n->parent = node;
add_descriptor_to_list(n, &node->child, &node->child_tail);
if (pp) pp->next = nn; else pf->d_head = nn;
if (nn) nn->prev = pp; else pf->d_tail = pp;
n = nn;
}
else {
n = n->next;
}
}
// now hook this node into the global tree
if (node->parent_d_id == 0) {
// add top level node to the descriptor tree
//DEBUG_INFO(("Null parent\n"));
add_descriptor_to_list(node, &pf->d_head, &pf->d_tail);
}
else if (node->parent_d_id == node->d_id) {
// add top level node to the descriptor tree
DEBUG_INFO(("%#"PRIx64" is its own parent. What is this world coming to?\n", node->d_id));
add_descriptor_to_list(node, &pf->d_head, &pf->d_tail);
} else {
//DEBUG_INFO(("Searching for parent %#"PRIx64" of %#"PRIx64"\n", node->parent_d_id, node->d_id));
pst_desc_tree *parent = pst_getDptr(pf, node->parent_d_id);
if (parent) {
//DEBUG_INFO(("Found parent %#"PRIx64"\n", node->parent_d_id));
parent->no_child++;
node->parent = parent;
add_descriptor_to_list(node, &parent->child, &parent->child_tail);
}
else {
DEBUG_INFO(("No parent %#"PRIx64", have an orphan child %#"PRIx64"\n", node->parent_d_id, node->d_id));
add_descriptor_to_list(node, &pf->d_head, &pf->d_tail);
}
}
DEBUG_RET();
}
/**
* make a deep copy of part of the id2 mapping tree, for use
* by an attachment containing an embedded rfc822 message.
*
* @param head pointer to the subtree to be copied
* @return pointer to the new copy of the subtree
*/
static pst_id2_tree* deep_copy(pst_id2_tree *head);
static pst_id2_tree* deep_copy(pst_id2_tree *head)
{
if (!head) return NULL;
pst_id2_tree* me = (pst_id2_tree*) pst_malloc(sizeof(pst_id2_tree));
me->id2 = head->id2;
me->id = head->id;
me->child = deep_copy(head->child);
me->next = deep_copy(head->next);
return me;
}
pst_desc_tree* pst_getTopOfFolders(pst_file *pf, const pst_item *root) {
pst_desc_tree *topnode;
uint32_t topid;
DEBUG_ENT("pst_getTopOfFolders");
if (!root || !root->message_store) {
DEBUG_INFO(("There isn't a top of folder record here.\n"));
DEBUG_RET();
return NULL;
}
if (!root->message_store->top_of_personal_folder) {
// this is the OST way
// ASSUMPTION: Top Of Folders record in PST files is *always* descid 0x2142
topid = 0x2142;
} else {
topid = root->message_store->top_of_personal_folder->id;
}
DEBUG_INFO(("looking for top of folder descriptor %#"PRIx32"\n", topid));
topnode = pst_getDptr(pf, (uint64_t)topid);
if (!topnode) {
// add dummy top record to pickup orphan children
topnode = (pst_desc_tree*) pst_malloc(sizeof(pst_desc_tree));
topnode->d_id = topid;
topnode->parent_d_id = 0;
topnode->assoc_tree = NULL;
topnode->desc = NULL;
record_descriptor(pf, topnode); // add to the global tree
}
DEBUG_RET();
return topnode;
}
pst_binary pst_attach_to_mem(pst_file *pf, pst_item_attach *attach) {
pst_index_ll *ptr;
pst_binary rc;
pst_holder h = {&rc.data, NULL, 0, 0, 0};
rc.size = 0;
rc.data = NULL;
DEBUG_ENT("pst_attach_to_mem");
if ((!attach->data.data) && (attach->i_id != (uint64_t)-1)) {
ptr = pst_getID(pf, attach->i_id);
if (ptr) {
rc.size = pst_ff_getID2data(pf, ptr, &h);
} else {
DEBUG_WARN(("Couldn't find ID pointer. Cannot handle attachment\n"));
}
} else {
rc = attach->data;
attach->data.data = NULL; // prevent pst_free_item() from trying to free this
attach->data.size = 0; // since we have given that buffer to the caller
}
DEBUG_RET();
return rc;
}
size_t pst_attach_to_file(pst_file *pf, pst_item_attach *attach, FILE* fp) {
pst_index_ll *ptr;
pst_holder h = {NULL, fp, 0, 0, 0};
size_t size = 0;
DEBUG_ENT("pst_attach_to_file");
if ((!attach->data.data) && (attach->i_id != (uint64_t)-1)) {
ptr = pst_getID(pf, attach->i_id);
if (ptr) {
size = pst_ff_getID2data(pf, ptr, &h);
} else {
DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n"));
}
} else {
size = attach->data.size;
if (attach->data.data && size) {
// save the attachment to the file
(void)pst_fwrite(attach->data.data, (size_t)1, size, fp);
}
}
DEBUG_RET();
return size;
}
size_t pst_attach_to_file_base64(pst_file *pf, pst_item_attach *attach, FILE* fp) {
pst_index_ll *ptr;
pst_holder h = {NULL, fp, 1, 0, 0};
size_t size = 0;
DEBUG_ENT("pst_attach_to_file_base64");
if ((!attach->data.data) && (attach->i_id != (uint64_t)-1)) {
ptr = pst_getID(pf, attach->i_id);
if (ptr) {
size = pst_ff_getID2data(pf, ptr, &h);
} else {
DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to Base64\n"));
}
} else {
size = attach->data.size;
if (attach->data.data && size) {
// encode the attachment to the file
char *c = pst_base64_encode(attach->data.data, size);
if (c) {
(void)pst_fwrite(c, (size_t)1, strlen(c), fp);
free(c); // caught by valgrind
}
}
}
DEBUG_RET();
return size;
}
int pst_load_index (pst_file *pf) {
int x;
DEBUG_ENT("pst_load_index");
if (!pf) {
DEBUG_WARN(("Cannot load index for a NULL pst_file\n"));
DEBUG_RET();
return -1;
}
x = pst_build_id_ptr(pf, pf->index1, 0, pf->index1_back, 0, UINT64_MAX);
DEBUG_INFO(("build id ptr returns %i\n", x));
x = pst_build_desc_ptr(pf, pf->index2, 0, pf->index2_back, (uint64_t)0x21, UINT64_MAX);
DEBUG_INFO(("build desc ptr returns %i\n", x));
pst_printDptr(pf, pf->d_head);
DEBUG_RET();
return 0;
}
pst_desc_tree* pst_getNextDptr(pst_desc_tree* d) {
pst_desc_tree* r = NULL;
DEBUG_ENT("pst_getNextDptr");
if (d) {
if ((r = d->child) == NULL) {
while (!d->next && d->parent) d = d->parent;
r = d->next;
}
}
DEBUG_RET();
return r;
}
typedef struct pst_x_attrib {
uint32_t extended;
uint16_t type;
uint16_t map;
} pst_x_attrib;
/** Try to load the extended attributes from the pst file.
@return true(1) or false(0) to indicate whether the extended attributes have been loaded
*/
int pst_load_extended_attributes(pst_file *pf) {
// for PST files this will load up d_id 0x61 and check it's "assoc_tree" attribute.
pst_desc_tree *p;
pst_mapi_object *list;
pst_id2_tree *id2_head = NULL;
char *buffer=NULL, *headerbuffer=NULL;
size_t bsize=0, hsize=0, bptr=0;
pst_x_attrib xattrib;
int32_t tint, x;
pst_x_attrib_ll *ptr, *p_head=NULL;
DEBUG_ENT("pst_loadExtendedAttributes");
p = pst_getDptr(pf, (uint64_t)0x61);
if (!p) {
DEBUG_WARN(("Cannot find d_id 0x61 for loading the Extended Attributes\n"));
DEBUG_RET();
return 0;
}
if (!p->desc) {
DEBUG_WARN(("descriptor is NULL for d_id 0x61. Cannot load Extended Attributes\n"));
DEBUG_RET();
return 0;
}
if (p->assoc_tree) {
id2_head = pst_build_id2(pf, p->assoc_tree);
pst_printID2ptr(id2_head);
} else {
DEBUG_WARN(("Have not been able to fetch any id2 values for d_id 0x61. Brace yourself!\n"));
}
list = pst_parse_block(pf, p->desc->i_id, id2_head);
if (!list) {
DEBUG_WARN(("Cannot process desc block for item 0x61. Not loading extended Attributes\n"));
pst_free_id2(id2_head);
DEBUG_RET();
return 0;
}
DEBUG_INFO(("look thru d_id 0x61 list of mapi objects\n"));
for (x=0; x < list->count_elements; x++) {
DEBUG_INFO(("#%d - mapi-id: %#x type: %#x length: %#x\n", x, list->elements[x]->mapi_id, list->elements[x]->type, list->elements[x]->size));
if (list->elements[x]->data) {
DEBUG_HEXDUMPC(list->elements[x]->data, list->elements[x]->size, 0x10);
}
if (list->elements[x]->mapi_id == (uint32_t)0x0003) {
buffer = list->elements[x]->data;
bsize = list->elements[x]->size;
} else if (list->elements[x]->mapi_id == (uint32_t)0x0004) {
headerbuffer = list->elements[x]->data;
hsize = list->elements[x]->size;
} else {
// leave them null
}
}
if (!buffer) {
pst_free_list(list);
DEBUG_WARN(("No extended attributes buffer found. Not processing\n"));
DEBUG_RET();
return 0;
}
while (bptr < bsize) {
int err = 0;
xattrib.extended= PST_LE_GET_UINT32(buffer+bptr), bptr += 4;
xattrib.type = PST_LE_GET_UINT16(buffer+bptr), bptr += 2;
xattrib.map = PST_LE_GET_UINT16(buffer+bptr), bptr += 2;
ptr = (pst_x_attrib_ll*) pst_malloc(sizeof(*ptr));
memset(ptr, 0, sizeof(*ptr));
ptr->map = xattrib.map+0x8000;
ptr->next = NULL;
DEBUG_INFO(("xattrib: ext = %#"PRIx32", type = %#"PRIx16", map = %#"PRIx16"\n",
xattrib.extended, xattrib.type, xattrib.map));
if (xattrib.type & 0x0001) { // if the Bit 1 is set
// pointer to Unicode field in buffer
if (xattrib.extended < hsize) {
char *wt;
// copy the size of the header. It is 32 bit int
memcpy(&tint, &(headerbuffer[xattrib.extended]), sizeof(tint));
LE32_CPU(tint);
wt = (char*) pst_malloc((size_t)(tint+2)); // plus 2 for a uni-code zero
memset(wt, 0, (size_t)(tint+2));
memcpy(wt, &(headerbuffer[xattrib.extended+sizeof(tint)]), (size_t)tint);
ptr->data = pst_wide_to_single(wt, (size_t)tint);
free(wt);
DEBUG_INFO(("Mapped attribute %#"PRIx32" to %s\n", ptr->map, ptr->data));
} else {
DEBUG_INFO(("Cannot read outside of buffer [%i !< %i]\n", xattrib.extended, hsize));
err = 1;
}
ptr->mytype = PST_MAP_HEADER;
} else {
// contains the attribute code to map to.
ptr->data = (uint32_t*)pst_malloc(sizeof(uint32_t));
memset(ptr->data, 0, sizeof(uint32_t));
*((uint32_t*)ptr->data) = xattrib.extended;
ptr->mytype = PST_MAP_ATTRIB;
DEBUG_INFO(("Mapped attribute %#"PRIx32" to %#"PRIx32"\n", ptr->map, *((uint32_t*)ptr->data)));
}
if (!err) {
// add it to the list
pst_x_attrib_ll *p_sh = p_head;
pst_x_attrib_ll *p_sh2 = NULL;
while (p_sh && (ptr->map > p_sh->map)) {
p_sh2 = p_sh;
p_sh = p_sh->next;
}
if (!p_sh2) {
// needs to go before first item
ptr->next = p_head;
p_head = ptr;
} else {
// it will go after p_sh2
ptr->next = p_sh2->next;
p_sh2->next = ptr;
}
} else {
free(ptr);
}
}
pst_free_id2(id2_head);
pst_free_list(list);
pf->x_head = p_head;
DEBUG_RET();
return 1;
}
#define ITEM_COUNT_OFFSET32 0x1f0 // count byte
#define LEVEL_INDICATOR_OFFSET32 0x1f3 // node or leaf
#define BACKLINK_OFFSET32 0x1f8 // backlink u1 value
#define ITEM_SIZE32 12
#define DESC_SIZE32 16
#define INDEX_COUNT_MAX32 41 // max active items
#define DESC_COUNT_MAX32 31 // max active items
#define ITEM_COUNT_OFFSET64 0x1e8 // count byte
#define LEVEL_INDICATOR_OFFSET64 0x1eb // node or leaf
#define BACKLINK_OFFSET64 0x1f8 // backlink u1 value
#define ITEM_SIZE64 24
#define DESC_SIZE64 32
#define INDEX_COUNT_MAX64 20 // max active items
#define DESC_COUNT_MAX64 15 // max active items
#define BLOCK_SIZE 512 // index blocks
#define DESC_BLOCK_SIZE 512 // descriptor blocks
#define ITEM_COUNT_OFFSET (size_t)((pf->do_read64) ? ITEM_COUNT_OFFSET64 : ITEM_COUNT_OFFSET32)
#define LEVEL_INDICATOR_OFFSET (size_t)((pf->do_read64) ? LEVEL_INDICATOR_OFFSET64 : LEVEL_INDICATOR_OFFSET32)
#define BACKLINK_OFFSET (size_t)((pf->do_read64) ? BACKLINK_OFFSET64 : BACKLINK_OFFSET32)
#define ITEM_SIZE (size_t)((pf->do_read64) ? ITEM_SIZE64 : ITEM_SIZE32)
#define DESC_SIZE (size_t)((pf->do_read64) ? DESC_SIZE64 : DESC_SIZE32)
#define INDEX_COUNT_MAX (int32_t)((pf->do_read64) ? INDEX_COUNT_MAX64 : INDEX_COUNT_MAX32)
#define DESC_COUNT_MAX (int32_t)((pf->do_read64) ? DESC_COUNT_MAX64 : DESC_COUNT_MAX32)
static size_t pst_decode_desc(pst_file *pf, pst_desc *desc, char *buf);
static size_t pst_decode_desc(pst_file *pf, pst_desc *desc, char *buf) {
size_t r;
if (pf->do_read64) {
DEBUG_INFO(("Decoding desc64\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_desc), 0x10);
memcpy(desc, buf, sizeof(pst_desc));
LE64_CPU(desc->d_id);
LE64_CPU(desc->desc_id);
LE64_CPU(desc->tree_id);
LE32_CPU(desc->parent_d_id);
LE32_CPU(desc->u1);
r = sizeof(pst_desc);
}
else {
pst_desc32 d32;
DEBUG_INFO(("Decoding desc32\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_desc32), 0x10);
memcpy(&d32, buf, sizeof(pst_desc32));
LE32_CPU(d32.d_id);
LE32_CPU(d32.desc_id);
LE32_CPU(d32.tree_id);
LE32_CPU(d32.parent_d_id);
desc->d_id = d32.d_id;
desc->desc_id = d32.desc_id;
desc->tree_id = d32.tree_id;
desc->parent_d_id = d32.parent_d_id;
desc->u1 = 0;
r = sizeof(pst_desc32);
}
return r;
}
static size_t pst_decode_table(pst_file *pf, struct pst_table_ptr_struct *table, char *buf);
static size_t pst_decode_table(pst_file *pf, struct pst_table_ptr_struct *table, char *buf) {
size_t r;
if (pf->do_read64) {
DEBUG_INFO(("Decoding table64\n"));
DEBUG_HEXDUMPC(buf, sizeof(struct pst_table_ptr_struct), 0x10);
memcpy(table, buf, sizeof(struct pst_table_ptr_struct));
LE64_CPU(table->start);
LE64_CPU(table->u1);
LE64_CPU(table->offset);
r =sizeof(struct pst_table_ptr_struct);
}
else {
struct pst_table_ptr_struct32 t32;
DEBUG_INFO(("Decoding table32\n"));
DEBUG_HEXDUMPC(buf, sizeof( struct pst_table_ptr_struct32), 0x10);
memcpy(&t32, buf, sizeof(struct pst_table_ptr_struct32));
LE32_CPU(t32.start);
LE32_CPU(t32.u1);
LE32_CPU(t32.offset);
table->start = t32.start;
table->u1 = t32.u1;
table->offset = t32.offset;
r = sizeof(struct pst_table_ptr_struct32);
}
return r;
}
static size_t pst_decode_index(pst_file *pf, pst_index *index, char *buf);
static size_t pst_decode_index(pst_file *pf, pst_index *index, char *buf) {
size_t r;
if (pf->do_read64) {
DEBUG_INFO(("Decoding index64\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_index), 0x10);
memcpy(index, buf, sizeof(pst_index));
LE64_CPU(index->id);
LE64_CPU(index->offset);
LE16_CPU(index->size);
LE16_CPU(index->u0);
LE32_CPU(index->u1);
r = sizeof(pst_index);
} else {
pst_index32 index32;
DEBUG_INFO(("Decoding index32\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_index32), 0x10);
memcpy(&index32, buf, sizeof(pst_index32));
LE32_CPU(index32.id);
LE32_CPU(index32.offset);
LE16_CPU(index32.size);
LE16_CPU(index32.u1);
index->id = index32.id;
index->offset = index32.offset;
index->size = index32.size;
index->u0 = 0;
index->u1 = index32.u1;
r = sizeof(pst_index32);
}
return r;
}
static size_t pst_decode_assoc(pst_file *pf, pst_id2_assoc *assoc, char *buf);
static size_t pst_decode_assoc(pst_file *pf, pst_id2_assoc *assoc, char *buf) {
size_t r;
if (pf->do_read64) {
DEBUG_INFO(("Decoding assoc64\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_id2_assoc), 0x10);
memcpy(assoc, buf, sizeof(pst_id2_assoc));
LE32_CPU(assoc->id2);
LE64_CPU(assoc->id);
LE64_CPU(assoc->child_id);
r = sizeof(pst_id2_assoc);
} else {
pst_id2_assoc32 assoc32;
DEBUG_INFO(("Decoding assoc32\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_id2_assoc32), 0x10);
memcpy(&assoc32, buf, sizeof(pst_id2_assoc32));
LE32_CPU(assoc32.id2);
LE32_CPU(assoc32.id);
LE32_CPU(assoc32.child_id);
assoc->id2 = assoc32.id2;
assoc->id = assoc32.id;
assoc->child_id = assoc32.child_id;
r = sizeof(pst_id2_assoc32);
}
return r;
}
static size_t pst_decode_type3(pst_file *pf, pst_table3_rec *table3_rec, char *buf);
static size_t pst_decode_type3(pst_file *pf, pst_table3_rec *table3_rec, char *buf) {
size_t r;
DEBUG_ENT("pst_decode_type3");
if (pf->do_read64) {
DEBUG_INFO(("Decoding table3 64\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_table3_rec), 0x10);
memcpy(table3_rec, buf, sizeof(pst_table3_rec));
LE64_CPU(table3_rec->id);
r = sizeof(pst_table3_rec);
} else {
pst_table3_rec32 table3_rec32;
DEBUG_INFO(("Decoding table3 32\n"));
DEBUG_HEXDUMPC(buf, sizeof(pst_table3_rec32), 0x10);
memcpy(&table3_rec32, buf, sizeof(pst_table3_rec32));
LE32_CPU(table3_rec32.id);
table3_rec->id = table3_rec32.id;
r = sizeof(pst_table3_rec32);
}
DEBUG_RET();
return r;
}
/** Process the index1 b-tree from the pst file and create the
* pf->i_head linked list from it. This tree holds the location
* (offset and size) of lower level objects (0xbcec descriptor
* blocks, etc) in the pst file.
*/
static int pst_build_id_ptr(pst_file *pf, int64_t offset, int32_t depth, uint64_t linku1, uint64_t start_val, uint64_t end_val) {
struct pst_table_ptr_struct table, table2;
pst_index_ll *i_ptr=NULL;
pst_index index;
int32_t x, item_count;
uint64_t old = start_val;
char *buf = NULL, *bptr;
DEBUG_ENT("pst_build_id_ptr");
DEBUG_INFO(("offset %#"PRIx64" depth %i linku1 %#"PRIx64" start %#"PRIx64" end %#"PRIx64"\n", offset, depth, linku1, start_val, end_val));
if (end_val <= start_val) {
DEBUG_WARN(("The end value is BEFORE the start value. This function will quit. Soz. [start:%#"PRIx64", end:%#"PRIx64"]\n", start_val, end_val));
DEBUG_RET();
return -1;
}
DEBUG_INFO(("Reading index block\n"));
if (pst_read_block_size(pf, offset, BLOCK_SIZE, &buf) < BLOCK_SIZE) {
DEBUG_WARN(("Failed to read %i bytes\n", BLOCK_SIZE));
if (buf) free(buf);
DEBUG_RET();
return -1;
}
bptr = buf;