comparison libpst.c @ 12:3f627519a92d stable-0-5-2

properly ignore (second block zero) errors. that will just drop some unknown attachments, but the rest of the data is still found
author carl
date Sat, 18 Feb 2006 09:56:00 -0800
parents bf12a9d4524c
children
comparison
equal deleted inserted replaced
11:bf12a9d4524c 12:3f627519a92d
125 #ifdef _MSC_VER 125 #ifdef _MSC_VER
126 // set the default open mode for windows 126 // set the default open mode for windows
127 _fmode = _O_BINARY; 127 _fmode = _O_BINARY;
128 #endif //_MSC_VER 128 #endif //_MSC_VER
129 129
130 if (pf == NULL) { 130 if (!pf) {
131 WARN (("cannot be passed a NULL pst_file\n")); 131 WARN (("cannot be passed a NULL pst_file\n"));
132 DEBUG_RET(); 132 DEBUG_RET();
133 return -1; 133 return -1;
134 } 134 }
135 memset(pf, 0, sizeof(pst_file)); 135 memset(pf, 0, sizeof(pst_file));
187 } 187 }
188 188
189 189
190 int32_t pst_close(pst_file *pf) { 190 int32_t pst_close(pst_file *pf) {
191 DEBUG_ENT("pst_close"); 191 DEBUG_ENT("pst_close");
192 if (pf->fp == NULL) { 192 if (!pf->fp) {
193 WARN(("cannot close NULL fp\n")); 193 WARN(("cannot close NULL fp\n"));
194 DEBUG_RET(); 194 DEBUG_RET();
195 return -1; 195 return -1;
196 } 196 }
197 if (fclose(pf->fp)) { 197 if (fclose(pf->fp)) {
212 pst_desc_ll *ret; 212 pst_desc_ll *ret;
213 // pst_item *i; 213 // pst_item *i;
214 // char *a, *b; 214 // char *a, *b;
215 // int x,z; 215 // int x,z;
216 DEBUG_ENT("pst_getTopOfFolders"); 216 DEBUG_ENT("pst_getTopOfFolders");
217 if (root == NULL || root->message_store == NULL) { 217 if (!root || !root->message_store) {
218 DEBUG_INDEX(("There isn't a top of folder record here.\n")); 218 DEBUG_INDEX(("There isn't a top of folder record here.\n"));
219 ret = NULL; 219 ret = NULL;
220 } else if (root->message_store->top_of_personal_folder == NULL) { 220 } else if (!root->message_store->top_of_personal_folder) {
221 // this is the OST way 221 // this is the OST way
222 // ASSUMPTION: Top Of Folders record in PST files is *always* descid 0x2142 222 // ASSUMPTION: Top Of Folders record in PST files is *always* descid 0x2142
223 ret = _pst_getDptr(pf, 0x2142); 223 ret = _pst_getDptr(pf, 0x2142);
224 } else { 224 } else {
225 ret = _pst_getDptr(pf, root->message_store->top_of_personal_folder->id); 225 ret = _pst_getDptr(pf, root->message_store->top_of_personal_folder->id);
234 pst_index_ll *ptr; 234 pst_index_ll *ptr;
235 struct holder h = {b, NULL, 0, "", 0}; 235 struct holder h = {b, NULL, 0, "", 0};
236 DEBUG_ENT("pst_attach_to_mem"); 236 DEBUG_ENT("pst_attach_to_mem");
237 if (attach->id_val != -1) { 237 if (attach->id_val != -1) {
238 ptr = _pst_getID(pf, attach->id_val); 238 ptr = _pst_getID(pf, attach->id_val);
239 if (ptr != NULL) { 239 if (ptr) {
240 size = _pst_ff_getID2data(pf,ptr, &h); 240 size = _pst_ff_getID2data(pf, ptr, &h);
241 } else { 241 } else {
242 DEBUG_WARN(("Couldn't find ID pointer. Cannot handle attachment\n")); 242 DEBUG_WARN(("Couldn't find ID pointer. Cannot handle attachment\n"));
243 } 243 }
244 attach->size = size; // may aswell update it to what is correct for this instance 244 attach->size = size; // may aswell update it to what is correct for this instance
245 } else { 245 } else {
255 struct holder h = {NULL, fp, 0, "", 0}; 255 struct holder h = {NULL, fp, 0, "", 0};
256 int32_t size; 256 int32_t size;
257 DEBUG_ENT("pst_attach_to_file"); 257 DEBUG_ENT("pst_attach_to_file");
258 if (attach->id_val != -1) { 258 if (attach->id_val != -1) {
259 ptr = _pst_getID(pf, attach->id_val); 259 ptr = _pst_getID(pf, attach->id_val);
260 if (ptr != NULL) { 260 if (ptr) {
261 size = _pst_ff_getID2data(pf, ptr, &h); 261 size = _pst_ff_getID2data(pf, ptr, &h);
262 } else { 262 } else {
263 DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n")); 263 DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n"));
264 } 264 }
265 attach->size = size; 265 attach->size = size;
279 int32_t size; 279 int32_t size;
280 char *c; 280 char *c;
281 DEBUG_ENT("pst_attach_to_file_base64"); 281 DEBUG_ENT("pst_attach_to_file_base64");
282 if (attach->id_val != -1) { 282 if (attach->id_val != -1) {
283 ptr = _pst_getID(pf, attach->id_val); 283 ptr = _pst_getID(pf, attach->id_val);
284 if (ptr != NULL) { 284 if (ptr) {
285 size = _pst_ff_getID2data(pf, ptr, &h); 285 size = _pst_ff_getID2data(pf, ptr, &h);
286 // will need to encode any bytes left over 286 // will need to encode any bytes left over
287 c = base64_encode(h.base64_extra_chars, h.base64_extra); 287 c = base64_encode(h.base64_extra_chars, h.base64_extra);
288 pst_fwrite(c, 1, strlen(c), fp); 288 pst_fwrite(c, 1, strlen(c), fp);
289 } else { 289 } else {
303 303
304 int32_t pst_load_index (pst_file *pf) { 304 int32_t pst_load_index (pst_file *pf) {
305 int32_t x; 305 int32_t x;
306 u_int32_t y; 306 u_int32_t y;
307 DEBUG_ENT("pst_load_index"); 307 DEBUG_ENT("pst_load_index");
308 if (pf == NULL) { 308 if (!pf) {
309 WARN(("Cannot load index for a NULL pst_file\n")); 309 WARN(("Cannot load index for a NULL pst_file\n"));
310 DEBUG_RET(); 310 DEBUG_RET();
311 return -1; 311 return -1;
312 } 312 }
313 313
325 325
326 326
327 pst_desc_ll* pst_getNextDptr(pst_desc_ll* d) { 327 pst_desc_ll* pst_getNextDptr(pst_desc_ll* d) {
328 pst_desc_ll* r = NULL; 328 pst_desc_ll* r = NULL;
329 DEBUG_ENT("pst_getNextDptr"); 329 DEBUG_ENT("pst_getNextDptr");
330 if (d != NULL) { 330 if (d) {
331 if ((r = d->child) == NULL) { 331 if ((r = d->child) == NULL) {
332 while (d->next == NULL && d->parent != NULL) d = d->parent; 332 while (!d->next && d->parent) d = d->parent;
333 r = d->next; 333 r = d->next;
334 } 334 }
335 } 335 }
336 DEBUG_RET(); 336 DEBUG_RET();
337 return r; 337 return r;
362 if ((p = _pst_getDptr(pf, 0x61)) == NULL) { 362 if ((p = _pst_getDptr(pf, 0x61)) == NULL) {
363 DEBUG_WARN(("Cannot find DescID 0x61 for loading the Extended Attributes\n")); 363 DEBUG_WARN(("Cannot find DescID 0x61 for loading the Extended Attributes\n"));
364 DEBUG_RET(); 364 DEBUG_RET();
365 return 0; 365 return 0;
366 } 366 }
367 if (p->list_index != NULL) { 367 if (p->list_index) {
368 list2 = _pst_build_id2(pf, p->list_index, NULL); 368 list2 = _pst_build_id2(pf, p->list_index, NULL);
369 } 369 }
370 if (p->desc == NULL) { 370 if (!p->desc) {
371 DEBUG_WARN(("desc is NULL for item 0x61. Cannot load Extended Attributes\n")); 371 DEBUG_WARN(("desc is NULL for item 0x61. Cannot load Extended Attributes\n"));
372 DEBUG_RET(); 372 DEBUG_RET();
373 return 0; 373 return 0;
374 } 374 }
375 if ((na = _pst_parse_block(pf, p->desc->id, list2)) == NULL) { 375 if ((na = _pst_parse_block(pf, p->desc->id, list2)) == NULL) {
387 hsize = na->items[x]->size; 387 hsize = na->items[x]->size;
388 } 388 }
389 x++; 389 x++;
390 } 390 }
391 391
392 if (buffer == NULL) { 392 if (!buffer) {
393 DEBUG_WARN(("No extended attributes buffer found. Not processing\n")); 393 DEBUG_WARN(("No extended attributes buffer found. Not processing\n"));
394 DEBUG_RET(); 394 DEBUG_RET();
395 return 0; 395 return 0;
396 } 396 }
397 397
437 437
438 if (err==0) { 438 if (err==0) {
439 // add it to the list 439 // add it to the list
440 p_sh = p_head; 440 p_sh = p_head;
441 p_sh2 = NULL; 441 p_sh2 = NULL;
442 while (p_sh != NULL && ptr->map > p_sh->map) { 442 while (p_sh && ptr->map > p_sh->map) {
443 p_sh2 = p_sh; 443 p_sh2 = p_sh;
444 p_sh = p_sh->next; 444 p_sh = p_sh->next;
445 } 445 }
446 if (p_sh2 == NULL) { 446 if (!p_sh2) {
447 // needs to go before first item 447 // needs to go before first item
448 ptr->next = p_head; 448 ptr->next = p_head;
449 p_head = ptr; 449 p_head = ptr;
450 } else { 450 } else {
451 // it will go after p_sh2 451 // it will go after p_sh2
555 i_ptr->id = index.id; 555 i_ptr->id = index.id;
556 i_ptr->offset = index.offset; 556 i_ptr->offset = index.offset;
557 i_ptr->u1 = index.u1; 557 i_ptr->u1 = index.u1;
558 i_ptr->size = index.size; 558 i_ptr->size = index.size;
559 i_ptr->next = NULL; 559 i_ptr->next = NULL;
560 if (pf->i_tail != NULL) pf->i_tail->next = i_ptr; 560 if (pf->i_tail) pf->i_tail->next = i_ptr;
561 if (pf->i_head == NULL) pf->i_head = i_ptr; 561 if (!pf->i_head) pf->i_head = i_ptr;
562 pf->i_tail = i_ptr; 562 pf->i_tail = i_ptr;
563 } 563 }
564 } else { 564 } else {
565 // this node contains node pointers 565 // this node contains node pointers
566 x = 0; 566 x = 0;
691 d_ptr->list_index = _pst_getID(pf, desc_rec.list_id); 691 d_ptr->list_index = _pst_getID(pf, desc_rec.list_id);
692 d_ptr->desc = _pst_getID(pf, desc_rec.desc_id); 692 d_ptr->desc = _pst_getID(pf, desc_rec.desc_id);
693 DEBUG_INDEX(("\tdesc = %#x\tlist_index=%#x\n", 693 DEBUG_INDEX(("\tdesc = %#x\tlist_index=%#x\n",
694 (d_ptr->desc==NULL?0:d_ptr->desc->id), 694 (d_ptr->desc==NULL?0:d_ptr->desc->id),
695 (d_ptr->list_index==NULL?0:d_ptr->list_index->id))); 695 (d_ptr->list_index==NULL?0:d_ptr->list_index->id)));
696 if (d_ptr->parent != NULL && desc_rec.parent_id != d_ptr->parent->id) { 696 if (d_ptr->parent && desc_rec.parent_id != d_ptr->parent->id) {
697 DEBUG_INDEX(("WARNING -- Parent of record has changed. Moving it\n")); 697 DEBUG_INDEX(("WARNING -- Parent of record has changed. Moving it\n"));
698 //hmmm, we must move the record. 698 //hmmm, we must move the record.
699 // first we must remove from current location 699 // first we must remove from current location
700 // change previous record to point next to our next 700 // change previous record to point next to our next
701 // if no previous, then use parent's child 701 // if no previous, then use parent's child
702 // if no parent then change pf->d_head; 702 // if no parent then change pf->d_head;
703 // change next's prev to our prev 703 // change next's prev to our prev
704 // if no next then change parent's child_tail 704 // if no next then change parent's child_tail
705 // if no parent then change pf->d_tail 705 // if no parent then change pf->d_tail
706 if (d_ptr->prev != NULL) 706 if (d_ptr->prev)
707 d_ptr->prev->next = d_ptr->next; 707 d_ptr->prev->next = d_ptr->next;
708 else if (d_ptr->parent != NULL) 708 else if (d_ptr->parent)
709 d_ptr->parent->child = d_ptr->next; 709 d_ptr->parent->child = d_ptr->next;
710 else 710 else
711 pf->d_head = d_ptr->next; 711 pf->d_head = d_ptr->next;
712 712
713 if (d_ptr->next != NULL) 713 if (d_ptr->next)
714 d_ptr->next->prev = d_ptr->prev; 714 d_ptr->next->prev = d_ptr->prev;
715 else if (d_ptr->parent != NULL) 715 else if (d_ptr->parent)
716 d_ptr->parent->child_tail = d_ptr->prev; 716 d_ptr->parent->child_tail = d_ptr->prev;
717 else 717 else
718 pf->d_tail = d_ptr->prev; 718 pf->d_tail = d_ptr->prev;
719 719
720 d_ptr->prev = NULL; 720 d_ptr->prev = NULL;
723 723
724 // ok, now place in correct place 724 // ok, now place in correct place
725 DEBUG_INDEX(("Searching for parent\n")); 725 DEBUG_INDEX(("Searching for parent\n"));
726 if (desc_rec.parent_id == 0) { 726 if (desc_rec.parent_id == 0) {
727 DEBUG_INDEX(("No Parent\n")); 727 DEBUG_INDEX(("No Parent\n"));
728 if (pf->d_tail != NULL) pf->d_tail->next = d_ptr; 728 if (pf->d_tail) pf->d_tail->next = d_ptr;
729 if (pf->d_head == NULL) pf->d_head = d_ptr; 729 if (!pf->d_head) pf->d_head = d_ptr;
730 d_ptr->prev = pf->d_tail; 730 d_ptr->prev = pf->d_tail;
731 pf->d_tail = d_ptr; 731 pf->d_tail = d_ptr;
732 } else { 732 } else {
733 // check in the quick list 733 // check in the quick list
734 d_ptr_ptr = d_ptr_head; 734 d_ptr_ptr = d_ptr_head;
735 while (d_ptr_ptr != NULL && d_ptr_ptr->ptr->id != desc_rec.parent_id) { 735 while (d_ptr_ptr && (d_ptr_ptr->ptr->id != desc_rec.parent_id)) {
736 d_ptr_ptr = d_ptr_ptr->next; 736 d_ptr_ptr = d_ptr_ptr->next;
737 } 737 }
738 738
739 if (d_ptr_ptr == NULL && (d_par = _pst_getDptr(pf, desc_rec.parent_id)) == NULL) { 739 if (!d_ptr_ptr && (d_par = _pst_getDptr(pf, desc_rec.parent_id)) == NULL) {
740 // check in the lost/found list 740 // check in the lost/found list
741 lf_ptr = lf_head; 741 lf_ptr = lf_head;
742 while (lf_ptr != NULL && lf_ptr->ptr->id != desc_rec.parent_id) { 742 while (lf_ptr && lf_ptr->ptr->id != desc_rec.parent_id) {
743 lf_ptr = lf_ptr->next; 743 lf_ptr = lf_ptr->next;
744 } 744 }
745 if (lf_ptr == NULL) { 745 if (!lf_ptr) {
746 DEBUG_WARN(("ERROR -- not found parent with id %#x. Adding to lost/found\n", desc_rec.parent_id)); 746 DEBUG_WARN(("ERROR -- not found parent with id %#x. Adding to lost/found\n", desc_rec.parent_id));
747 lf_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll)); 747 lf_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll));
748 lf_ptr->prev = NULL; 748 lf_ptr->prev = NULL;
749 lf_ptr->next = lf_head; 749 lf_ptr->next = lf_head;
750 lf_ptr->parent = desc_rec.parent_id; 750 lf_ptr->parent = desc_rec.parent_id;
751 lf_ptr->ptr = d_ptr; 751 lf_ptr->ptr = d_ptr;
752 lf_head = lf_ptr; 752 lf_head = lf_ptr;
753 } else { 753 } else {
754 d_par = lf_ptr->ptr; 754 d_par = lf_ptr->ptr;
755 DEBUG_INDEX(("Found parent (%#x) in Lost and Found\n", d_par->id)); 755 DEBUG_INDEX(("Found parent (%#x) in Lost and Found\n", d_par->id));
756 } 756 }
757 } 757 }
758 758
759 if (d_ptr_ptr != NULL || d_par != NULL) { 759 if (d_ptr_ptr || d_par) {
760 if (d_ptr_ptr != NULL) 760 if (d_ptr_ptr)
761 d_par = d_ptr_ptr->ptr; 761 d_par = d_ptr_ptr->ptr;
762 else { 762 else {
763 //add the d_par to the cache 763 //add the d_par to the cache
764 DEBUG_INDEX(("Update - Cache addition\n")); 764 DEBUG_INDEX(("Update - Cache addition\n"));
765 d_ptr_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll)); 765 d_ptr_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll));
766 d_ptr_ptr->prev = NULL; 766 d_ptr_ptr->prev = NULL;
767 d_ptr_ptr->next = d_ptr_head; 767 d_ptr_ptr->next = d_ptr_head;
768 d_ptr_ptr->ptr = d_par; 768 d_ptr_ptr->ptr = d_par;
769 d_ptr_head = d_ptr_ptr; 769 d_ptr_head = d_ptr_ptr;
770 if (d_ptr_tail == NULL) d_ptr_tail = d_ptr_ptr; 770 if (!d_ptr_tail) d_ptr_tail = d_ptr_ptr;
771 d_ptr_count++; 771 d_ptr_count++;
772 if (d_ptr_count > 100) { 772 if (d_ptr_count > 100) {
773 //remove on from the end 773 //remove on from the end
774 d_ptr_ptr = d_ptr_tail; 774 d_ptr_ptr = d_ptr_tail;
775 d_ptr_tail = d_ptr_ptr->prev; 775 d_ptr_tail = d_ptr_ptr->prev;
778 } 778 }
779 } 779 }
780 DEBUG_INDEX(("Found a parent\n")); 780 DEBUG_INDEX(("Found a parent\n"));
781 d_par->no_child++; 781 d_par->no_child++;
782 d_ptr->parent = d_par; 782 d_ptr->parent = d_par;
783 if (d_par->child_tail != NULL) d_par->child_tail->next = d_ptr; 783 if (d_par->child_tail) d_par->child_tail->next = d_ptr;
784 if (d_par->child == NULL) d_par->child = d_ptr; 784 if (!d_par->child) d_par->child = d_ptr;
785 d_ptr->prev = d_par->child_tail; 785 d_ptr->prev = d_par->child_tail;
786 d_par->child_tail = d_ptr; 786 d_par->child_tail = d_ptr;
787 } 787 }
788 } 788 }
789 } 789 }
809 if (desc_rec.parent_id == 0) { 809 if (desc_rec.parent_id == 0) {
810 DEBUG_INDEX(("No Parent\n")); 810 DEBUG_INDEX(("No Parent\n"));
811 } else { 811 } else {
812 DEBUG_INDEX(("Record is its own parent. What is this world coming to?\n")); 812 DEBUG_INDEX(("Record is its own parent. What is this world coming to?\n"));
813 } 813 }
814 if (pf->d_tail != NULL) pf->d_tail->next = d_ptr; 814 if (pf->d_tail) pf->d_tail->next = d_ptr;
815 if (pf->d_head == NULL) pf->d_head = d_ptr; 815 if (!pf->d_head) pf->d_head = d_ptr;
816 d_ptr->prev = pf->d_tail; 816 d_ptr->prev = pf->d_tail;
817 pf->d_tail = d_ptr; 817 pf->d_tail = d_ptr;
818 } else { 818 } else {
819 d_ptr_ptr = d_ptr_head; 819 d_ptr_ptr = d_ptr_head;
820 while (d_ptr_ptr != NULL && d_ptr_ptr->ptr->id != desc_rec.parent_id) { 820 while (d_ptr_ptr && (d_ptr_ptr->ptr->id != desc_rec.parent_id)) {
821 d_ptr_ptr = d_ptr_ptr->next; 821 d_ptr_ptr = d_ptr_ptr->next;
822 } 822 }
823 if (d_ptr_ptr == NULL && (d_par = _pst_getDptr(pf, desc_rec.parent_id)) == NULL) { 823 if (!d_ptr_ptr && (d_par = _pst_getDptr(pf, desc_rec.parent_id)) == NULL) {
824 // check in the lost/found list 824 // check in the lost/found list
825 lf_ptr = lf_head; 825 lf_ptr = lf_head;
826 while (lf_ptr != NULL && lf_ptr->ptr->id != desc_rec.parent_id) { 826 while (lf_ptr && (lf_ptr->ptr->id != desc_rec.parent_id)) {
827 lf_ptr = lf_ptr->next; 827 lf_ptr = lf_ptr->next;
828 } 828 }
829 if (lf_ptr == NULL) { 829 if (!lf_ptr) {
830 DEBUG_WARN(("ERROR -- not found parent with id %#x. Adding to lost/found\n", desc_rec.parent_id)); 830 DEBUG_WARN(("ERROR -- not found parent with id %#x. Adding to lost/found\n", desc_rec.parent_id));
831 lf_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll)); 831 lf_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll));
832 lf_ptr->prev = NULL; 832 lf_ptr->prev = NULL;
833 lf_ptr->next = lf_head; 833 lf_ptr->next = lf_head;
834 lf_ptr->parent = desc_rec.parent_id; 834 lf_ptr->parent = desc_rec.parent_id;
835 lf_ptr->ptr = d_ptr; 835 lf_ptr->ptr = d_ptr;
836 lf_head = lf_ptr; 836 lf_head = lf_ptr;
837 } else { 837 } else {
838 d_par = lf_ptr->ptr; 838 d_par = lf_ptr->ptr;
839 DEBUG_INDEX(("Found parent (%#x) in Lost and Found\n", d_par->id)); 839 DEBUG_INDEX(("Found parent (%#x) in Lost and Found\n", d_par->id));
840 } 840 }
841 } 841 }
842 842
843 if (d_ptr_ptr != NULL || d_par != NULL) { 843 if (d_ptr_ptr || d_par) {
844 if (d_ptr_ptr != NULL) 844 if (d_ptr_ptr)
845 d_par = d_ptr_ptr->ptr; 845 d_par = d_ptr_ptr->ptr;
846 else { 846 else {
847 //add the d_par to the cache 847 //add the d_par to the cache
848 DEBUG_INDEX(("Normal - Cache addition\n")); 848 DEBUG_INDEX(("Normal - Cache addition\n"));
849 d_ptr_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll)); 849 d_ptr_ptr = (struct _pst_d_ptr_ll*) xmalloc(sizeof(struct _pst_d_ptr_ll));
850 d_ptr_ptr->prev = NULL; 850 d_ptr_ptr->prev = NULL;
851 d_ptr_ptr->next = d_ptr_head; 851 d_ptr_ptr->next = d_ptr_head;
852 d_ptr_ptr->ptr = d_par; 852 d_ptr_ptr->ptr = d_par;
853 d_ptr_head = d_ptr_ptr; 853 d_ptr_head = d_ptr_ptr;
854 if (d_ptr_tail == NULL) d_ptr_tail = d_ptr_ptr; 854 if (!d_ptr_tail) d_ptr_tail = d_ptr_ptr;
855 d_ptr_count++; 855 d_ptr_count++;
856 if (d_ptr_count > 100) { 856 if (d_ptr_count > 100) {
857 //remove one from the end 857 //remove one from the end
858 d_ptr_ptr = d_ptr_tail; 858 d_ptr_ptr = d_ptr_tail;
859 d_ptr_tail = d_ptr_ptr->prev; 859 d_ptr_tail = d_ptr_ptr->prev;
863 } 863 }
864 864
865 DEBUG_INDEX(("Found a parent\n")); 865 DEBUG_INDEX(("Found a parent\n"));
866 d_par->no_child++; 866 d_par->no_child++;
867 d_ptr->parent = d_par; 867 d_ptr->parent = d_par;
868 if (d_par->child_tail != NULL) d_par->child_tail->next = d_ptr; 868 if (d_par->child_tail) d_par->child_tail->next = d_ptr;
869 if (d_par->child == NULL) d_par->child = d_ptr; 869 if (!d_par->child) d_par->child = d_ptr;
870 d_ptr->prev = d_par->child_tail; 870 d_ptr->prev = d_par->child_tail;
871 d_par->child_tail = d_ptr; 871 d_par->child_tail = d_ptr;
872 } 872 }
873 } 873 }
874 } 874 }
875 // check here to see if d_ptr is the parent of any of the items in the lost / found list 875 // check here to see if d_ptr is the parent of any of the items in the lost / found list
876 lf_ptr = lf_head; 876 lf_ptr = lf_head;
877 lf_shd = NULL; 877 lf_shd = NULL;
878 while (lf_ptr != NULL) { 878 while (lf_ptr) {
879 if (lf_ptr->parent == d_ptr->id) { 879 if (lf_ptr->parent == d_ptr->id) {
880 DEBUG_INDEX(("Found a child (%#x) of the current record. Joining to main structure.\n", lf_ptr->ptr->id)); 880 DEBUG_INDEX(("Found a child (%#x) of the current record. Joining to main structure.\n", lf_ptr->ptr->id));
881 d_par = d_ptr; 881 d_par = d_ptr;
882 d_ptr = lf_ptr->ptr; 882 d_ptr = lf_ptr->ptr;
883 d_par->no_child++; 883 d_par->no_child++;
884 d_ptr->parent = d_par; 884 d_ptr->parent = d_par;
885 if (d_par->child_tail != NULL) d_par->child_tail->next = d_ptr; 885 if (d_par->child_tail) d_par->child_tail->next = d_ptr;
886 if (d_par->child == NULL) d_par->child = d_ptr; 886 if (!d_par->child) d_par->child = d_ptr;
887 d_ptr->prev = d_par->child_tail; 887 d_ptr->prev = d_par->child_tail;
888 d_par->child_tail = d_ptr; 888 d_par->child_tail = d_ptr;
889 if (lf_shd == NULL) 889 if (!lf_shd)
890 lf_head = lf_ptr->next; 890 lf_head = lf_ptr->next;
891 else 891 else
892 lf_shd->next = lf_ptr->next; 892 lf_shd->next = lf_ptr->next;
893 lf_tmp = lf_ptr->next; 893 lf_tmp = lf_ptr->next;
894 free(lf_ptr); 894 free(lf_ptr);
943 } 943 }
944 _pst_build_desc_ptr(pf, table.offset, depth+1, table.u1, high_id, table.start, table2.start); 944 _pst_build_desc_ptr(pf, table.offset, depth+1, table.u1, high_id, table.start, table2.start);
945 } 945 }
946 } 946 }
947 // ok, lets try freeing the d_ptr_head cache here 947 // ok, lets try freeing the d_ptr_head cache here
948 while (d_ptr_head != NULL) { 948 while (d_ptr_head) {
949 d_ptr_ptr = d_ptr_head->next; 949 d_ptr_ptr = d_ptr_head->next;
950 free(d_ptr_head); 950 free(d_ptr_head);
951 d_ptr_head = d_ptr_ptr; 951 d_ptr_head = d_ptr_ptr;
952 } 952 }
953 // TODO - need to free lost and found list also!! 953 // TODO - need to free lost and found list also!!
964 pst_index_ll *id_ptr = NULL; 964 pst_index_ll *id_ptr = NULL;
965 pst_item *item = NULL; 965 pst_item *item = NULL;
966 pst_item_attach *attach = NULL; 966 pst_item_attach *attach = NULL;
967 int x; 967 int x;
968 DEBUG_ENT("_pst_parse_item"); 968 DEBUG_ENT("_pst_parse_item");
969 if (d_ptr == NULL) { 969 if (!d_ptr) {
970 DEBUG_WARN(("you cannot pass me a NULL! I don't want it!\n")); 970 DEBUG_WARN(("you cannot pass me a NULL! I don't want it!\n"));
971 DEBUG_RET(); 971 DEBUG_RET();
972 return NULL; 972 return NULL;
973 } 973 }
974 974
975 if (d_ptr->list_index != NULL) { 975 if (d_ptr->list_index) {
976 id2_head = _pst_build_id2(pf, d_ptr->list_index, NULL); 976 id2_head = _pst_build_id2(pf, d_ptr->list_index, NULL);
977 _pst_printID2ptr(id2_head); 977 _pst_printID2ptr(id2_head);
978 } else { 978 } else {
979 DEBUG_WARN(("Have not been able to fetch any id2 values for this item. Brace yourself!\n")); 979 DEBUG_WARN(("Have not been able to fetch any id2 values for this item. Brace yourself!\n"));
980 } 980 }
981 981
982 if (d_ptr->desc == NULL) { 982 if (!d_ptr->desc) {
983 DEBUG_WARN(("why is d_ptr->desc == NULL? I don't want to do anything else with this record\n")); 983 DEBUG_WARN(("why is d_ptr->desc == NULL? I don't want to do anything else with this record\n"));
984 DEBUG_RET(); 984 DEBUG_RET();
985 return NULL; 985 return NULL;
986 } 986 }
987 987
1002 } else { 1002 } else {
1003 _pst_free_list(list); 1003 _pst_free_list(list);
1004 list = NULL; //_pst_process will free the items in the list 1004 list = NULL; //_pst_process will free the items in the list
1005 } 1005 }
1006 1006
1007 if ((id_ptr = _pst_getID2(id2_head, 0x671)) != NULL) { 1007 if ((id_ptr = _pst_getID2(id2_head, 0x671))) {
1008 // attachements exist - so we will process them 1008 // attachements exist - so we will process them
1009 while (item->attach != NULL) { 1009 while (item->attach) {
1010 attach = item->attach->next; 1010 attach = item->attach->next;
1011 free(item->attach); 1011 free(item->attach);
1012 item->attach = attach; 1012 item->attach = attach;
1013 } 1013 }
1014 1014
1015 DEBUG_EMAIL(("ATTACHEMENT processing attachement\n")); 1015 DEBUG_EMAIL(("ATTACHEMENT processing attachement\n"));
1016 if ((list = _pst_parse_block(pf, id_ptr->id, id2_head)) == NULL) { 1016 if ((list = _pst_parse_block(pf, id_ptr->id, id2_head)) == NULL) {
1017 DEBUG_WARN(("ERROR error processing main attachment record\n")); 1017 DEBUG_WARN(("ERROR error processing main attachment record\n"));
1018 DEBUG_RET(); 1018 // DEBUG_RET();
1019 return NULL; 1019 // return NULL;
1020 } 1020 }
1021 x = 0; 1021 else {
1022 while (x < list->count_array) { 1022 x = 0;
1023 attach = (pst_item_attach*) xmalloc (sizeof(pst_item_attach)); 1023 while (x < list->count_array) {
1024 memset (attach, 0, sizeof(pst_item_attach)); 1024 attach = (pst_item_attach*) xmalloc (sizeof(pst_item_attach));
1025 attach->next = item->attach; 1025 memset (attach, 0, sizeof(pst_item_attach));
1026 item->attach = attach; 1026 attach->next = item->attach;
1027 x++; 1027 item->attach = attach;
1028 } 1028 x++;
1029 item->current_attach = item->attach; 1029 }
1030 1030 item->current_attach = item->attach;
1031 if (_pst_process(list, item)) { 1031
1032 DEBUG_WARN(("ERROR _pst_process() failed with attachments\n")); 1032 if (_pst_process(list, item)) {
1033 DEBUG_WARN(("ERROR _pst_process() failed with attachments\n"));
1034 _pst_free_list(list);
1035 DEBUG_RET();
1036 return NULL;
1037 }
1033 _pst_free_list(list); 1038 _pst_free_list(list);
1034 DEBUG_RET(); 1039
1035 return NULL; 1040 // now we will have initial information of each attachment stored in item->attach...
1036 } 1041 // we must now read the secondary record for each based on the id2 val associated with
1037 _pst_free_list(list); 1042 // each attachment
1038 1043 attach = item->attach;
1039 // now we will have initial information of each attachment stored in item->attach... 1044 while (attach) {
1040 // we must now read the secondary record for each based on the id2 val associated with 1045 if ((id_ptr = _pst_getID2(id2_head, attach->id2_val))) {
1041 // each attachment 1046 // id_ptr is a record describing the attachment
1042 attach = item->attach; 1047 // we pass NULL instead of id2_head cause we don't want it to
1043 while (attach != NULL) { 1048 // load all the extra stuff here.
1044 if ((id_ptr = _pst_getID2(id2_head, attach->id2_val)) != NULL) { 1049 if ((list = _pst_parse_block(pf, id_ptr->id, NULL)) == NULL) {
1045 // id_ptr is a record describing the attachment 1050 DEBUG_WARN(("ERROR error processing an attachment record\n"));
1046 // we pass NULL instead of id2_head cause we don't want it to 1051 attach = attach->next;
1047 // load all the extra stuff here. 1052 continue;
1048 if ((list = _pst_parse_block(pf, id_ptr->id, NULL)) == NULL) { 1053 }
1049 DEBUG_WARN(("ERROR error processing an attachment record\n")); 1054 item->current_attach = attach;
1050 attach = attach->next; 1055 if (_pst_process(list, item)) {
1051 continue; 1056 DEBUG_WARN(("ERROR _pst_process() failed with an attachment\n"));
1057 _pst_free_list(list);
1058 attach = attach->next;
1059 continue;
1060 }
1061 _pst_free_list(list);
1062 if ((id_ptr = _pst_getID2(id2_head, attach->id2_val))) {
1063 // id2_val has been updated to the ID2 value of the datablock containing the
1064 // attachment data
1065 attach->id_val = id_ptr->id;
1066 } else {
1067 DEBUG_WARN(("have not located the correct value for the attachment [%#x]\n", attach->id2_val));
1068 }
1069 } else {
1070 DEBUG_WARN(("ERROR cannot locate id2 value %#x\n", attach->id2_val));
1052 } 1071 }
1053 item->current_attach = attach; 1072 attach = attach->next;
1054 if (_pst_process(list, item)) { 1073 }
1055 DEBUG_WARN(("ERROR _pst_process() failed with an attachment\n"));
1056 _pst_free_list(list);
1057 attach = attach->next;
1058 continue;
1059 }
1060 _pst_free_list(list);
1061 if ((id_ptr = _pst_getID2(id2_head, attach->id2_val)) != NULL) {
1062 // id2_val has been updated to the ID2 value of the datablock containing the
1063 // attachment data
1064 attach->id_val = id_ptr->id;
1065 } else {
1066 DEBUG_WARN(("have not located the correct value for the attachment [%#x]\n", attach->id2_val));
1067 }
1068 } else {
1069 DEBUG_WARN(("ERROR cannot locate id2 value %#x\n", attach->id2_val));
1070 }
1071 attach = attach->next;
1072 } 1074 }
1073 item->current_attach = item->attach; //reset back to first 1075 item->current_attach = item->attach; //reset back to first
1074 } 1076 }
1075 1077
1076 _pst_free_id2(id2_head); 1078 _pst_free_id2(id2_head);
1214 DEBUG_RET(); 1216 DEBUG_RET();
1215 return NULL; 1217 return NULL;
1216 } 1218 }
1217 1219
1218 if (table_rec.value == 0) { // this is for the 2nd index offset 1220 if (table_rec.value == 0) { // this is for the 2nd index offset
1219 WARN(("reference to second index block is zero. ERROR\n")); 1221 DEBUG_INFO(("reference to second index block is zero. ERROR\n"));
1220 if (buf) free(buf); 1222 if (buf) free(buf);
1221 DEBUG_RET(); 1223 DEBUG_RET();
1222 return NULL; 1224 return NULL;
1223 } 1225 }
1224 1226
1225 _pst_getBlockOffset(buf, read_size, ind_ptr, table_rec.value, &block_offset); 1227 _pst_getBlockOffset(buf, read_size, ind_ptr, table_rec.value, &block_offset);
1226 num_recs = (block_offset.to - block_offset.from) / 6; // this will give the number of records in this block 1228 num_recs = (block_offset.to - block_offset.from) / 6; // this will give the number of records in this block
1227 1229
1291 na_ptr->items[x] = (struct _pst_num_item*) xmalloc(sizeof(struct _pst_num_item)); 1293 na_ptr->items[x] = (struct _pst_num_item*) xmalloc(sizeof(struct _pst_num_item));
1292 memset(na_ptr->items[x], 0, sizeof(struct _pst_num_item)); //init it 1294 memset(na_ptr->items[x], 0, sizeof(struct _pst_num_item)); //init it
1293 1295
1294 // check here to see if the id of the attribute is a mapped one 1296 // check here to see if the id of the attribute is a mapped one
1295 mapptr = pf->x_head; 1297 mapptr = pf->x_head;
1296 while (mapptr != NULL && mapptr->map < table_rec.type) mapptr = mapptr->next; 1298 while (mapptr && (mapptr->map < table_rec.type)) mapptr = mapptr->next;
1297 if (mapptr != NULL && mapptr->map == table_rec.type) { 1299 if (mapptr && (mapptr->map == table_rec.type)) {
1298 if (mapptr->mytype == PST_MAP_ATTRIB) { 1300 if (mapptr->mytype == PST_MAP_ATTRIB) {
1299 na_ptr->items[x]->id = *((int*)mapptr->data); 1301 na_ptr->items[x]->id = *((int*)mapptr->data);
1300 DEBUG_EMAIL(("Mapped attrib %#x to %#x\n", table_rec.type, na_ptr->items[x]->id)); 1302 DEBUG_EMAIL(("Mapped attrib %#x to %#x\n", table_rec.type, na_ptr->items[x]->id));
1301 } else if (mapptr->mytype == PST_MAP_HEADER) { 1303 } else if (mapptr->mytype == PST_MAP_HEADER) {
1302 DEBUG_EMAIL(("Internet Header mapping found %#x\n", table_rec.type)); 1304 DEBUG_EMAIL(("Internet Header mapping found %#x\n", table_rec.type));
1419 } 1421 }
1420 DEBUG_EMAIL(("increasing ind2_ptr by %i [%#x] bytes. Was %#x, Now %#x\n", rec_size, rec_size, ind2_ptr, ind2_ptr+rec_size)); 1422 DEBUG_EMAIL(("increasing ind2_ptr by %i [%#x] bytes. Was %#x, Now %#x\n", rec_size, rec_size, ind2_ptr, ind2_ptr+rec_size));
1421 ind2_ptr += rec_size; 1423 ind2_ptr += rec_size;
1422 count_rec++; 1424 count_rec++;
1423 } 1425 }
1424 if (buf != NULL) free(buf); 1426 if (buf) free(buf);
1425 DEBUG_RET(); 1427 DEBUG_RET();
1426 return na_head; 1428 return na_head;
1427 } 1429 }
1428 1430
1429 1431
1430 // check if item->email is NULL, and init if so 1432 // check if item->email is NULL, and init if so
1431 #define MALLOC_EMAIL(x) { if (x->email == NULL) { x->email = (pst_item_email*) xmalloc(sizeof(pst_item_email)); memset (x->email, 0, sizeof(pst_item_email));} } 1433 #define MALLOC_EMAIL(x) { if (!x->email) { x->email = (pst_item_email*) xmalloc(sizeof(pst_item_email)); memset(x->email, 0, sizeof(pst_item_email) );} }
1432 #define MALLOC_FOLDER(x) { if (x->folder == NULL) { x->folder = (pst_item_folder*) xmalloc(sizeof(pst_item_folder)); memset (x->folder, 0, sizeof(pst_item_folder));} } 1434 #define MALLOC_FOLDER(x) { if (!x->folder) { x->folder = (pst_item_folder*) xmalloc(sizeof(pst_item_folder)); memset(x->folder, 0, sizeof(pst_item_folder) );} }
1433 #define MALLOC_CONTACT(x) { if (x->contact == NULL) { x->contact = (pst_item_contact*) xmalloc(sizeof(pst_item_contact)); memset(x->contact, 0, sizeof(pst_item_contact));} } 1435 #define MALLOC_CONTACT(x) { if (!x->contact) { x->contact = (pst_item_contact*) xmalloc(sizeof(pst_item_contact)); memset(x->contact, 0, sizeof(pst_item_contact) );} }
1434 #define MALLOC_MESSAGESTORE(x) { if (x->message_store == NULL) { x->message_store = (pst_item_message_store*) xmalloc(sizeof(pst_item_message_store)); memset(x->message_store, 0, sizeof(pst_item_message_store)); } } 1436 #define MALLOC_MESSAGESTORE(x) { if (!x->message_store) { x->message_store = (pst_item_message_store*) xmalloc(sizeof(pst_item_message_store)); memset(x->message_store, 0, sizeof(pst_item_message_store));} }
1435 #define MALLOC_JOURNAL(x) { if (x->journal == NULL) { x->journal = (pst_item_journal*) xmalloc(sizeof(pst_item_journal)); memset(x->journal, 0, sizeof(pst_item_journal));} } 1437 #define MALLOC_JOURNAL(x) { if (!x->journal) { x->journal = (pst_item_journal*) xmalloc(sizeof(pst_item_journal)); memset(x->journal, 0, sizeof(pst_item_journal) );} }
1436 #define MALLOC_APPOINTMENT(x) { if (x->appointment == NULL) { x->appointment = (pst_item_appointment*) xmalloc(sizeof(pst_item_appointment)); memset(x->appointment, 0, sizeof(pst_item_appointment)); } } 1438 #define MALLOC_APPOINTMENT(x) { if (!x->appointment) { x->appointment = (pst_item_appointment*) xmalloc(sizeof(pst_item_appointment)); memset(x->appointment, 0, sizeof(pst_item_appointment) );} }
1437 // malloc space and copy the current item's data -- plus one on the size for good luck (and string termination) 1439 // malloc space and copy the current item's data -- plus one on the size for good luck (and string termination)
1438 #define LIST_COPY(targ, type) { \ 1440 #define LIST_COPY(targ, type) { \
1439 targ = type realloc(targ, list->items[x]->size+1); \ 1441 targ = type realloc(targ, list->items[x]->size+1); \
1440 memset(targ, 0, list->items[x]->size+1); \ 1442 memset(targ, 0, list->items[x]->size+1); \
1441 memcpy(targ, list->items[x]->data, list->items[x]->size); \ 1443 memcpy(targ, list->items[x]->data, list->items[x]->size); \
1444 1446
1445 /* free(list->items[x]->data); \ 1447 /* free(list->items[x]->data); \
1446 list->items[x]->data=NULL; \*/ 1448 list->items[x]->data=NULL; \*/
1447 1449
1448 //#define INC_CHECK_X() { if (++x >= list->count_item) break; } 1450 //#define INC_CHECK_X() { if (++x >= list->count_item) break; }
1449 #define NULL_CHECK(x) { if (x == NULL) { DEBUG_EMAIL(("NULL_CHECK: Null Found\n")); break;} } 1451 #define NULL_CHECK(x) { if (!x) { DEBUG_EMAIL(("NULL_CHECK: Null Found\n")); break;} }
1450 1452
1451 #define MOVE_NEXT(targ) { \ 1453 #define MOVE_NEXT(targ) { \
1452 if (next){\ 1454 if (next){\
1453 if ((char*)targ == NULL) {\ 1455 if (!targ) {\
1454 DEBUG_EMAIL(("MOVE_NEXT: Target is NULL. Will stop processing this option\n"));\ 1456 DEBUG_EMAIL(("MOVE_NEXT: Target is NULL. Will stop processing this option\n"));\
1455 break;\ 1457 break;\
1456 }\ 1458 }\
1457 targ = targ->next;\ 1459 targ = targ->next;\
1458 if ((char*)targ == NULL) {\ 1460 if (!targ) {\
1459 DEBUG_EMAIL(("MOVE_NEXT: Target is NULL after next. Will stop processing this option\n"));\ 1461 DEBUG_EMAIL(("MOVE_NEXT: Target is NULL after next. Will stop processing this option\n"));\
1460 break;\ 1462 break;\
1461 }\ 1463 }\
1462 next=0;\ 1464 next=0;\
1463 }\ 1465 }\
1469 int32_t next = 0; 1471 int32_t next = 0;
1470 pst_item_attach *attach; 1472 pst_item_attach *attach;
1471 pst_item_extra_field *ef; 1473 pst_item_extra_field *ef;
1472 1474
1473 DEBUG_ENT("_pst_process"); 1475 DEBUG_ENT("_pst_process");
1474 if (item == NULL) { 1476 if (!item) {
1475 DEBUG_EMAIL(("item cannot be NULL.\n")); 1477 DEBUG_EMAIL(("item cannot be NULL.\n"));
1476 DEBUG_RET(); 1478 DEBUG_RET();
1477 return -1; 1479 return -1;
1478 } 1480 }
1479 1481
1480 attach = item->current_attach; // a working variable 1482 attach = item->current_attach; // a working variable
1481 1483
1482 while (list != NULL) { 1484 while (list) {
1483 x = 0; 1485 x = 0;
1484 while (x < list->count_item) { 1486 while (x < list->count_item) {
1485 // check here to see if the id is one that is mapped. 1487 // check here to see if the id is one that is mapped.
1486 DEBUG_EMAIL(("#%d - id: %#x type: %#x length: %#x\n", x, list->items[x]->id, list->items[x]->type, list->items[x]->size)); 1488 DEBUG_EMAIL(("#%d - id: %#x type: %#x length: %#x\n", x, list->items[x]->id, list->items[x]->type, list->items[x]->size));
1487 1489
2220 break; 2222 break;
2221 case 0x3701: // PR_ATTACH_DATA_OBJ binary data of attachment 2223 case 0x3701: // PR_ATTACH_DATA_OBJ binary data of attachment
2222 DEBUG_EMAIL(("Binary Data [Size %i] - ", list->items[x]->size)); 2224 DEBUG_EMAIL(("Binary Data [Size %i] - ", list->items[x]->size));
2223 NULL_CHECK(attach); 2225 NULL_CHECK(attach);
2224 MOVE_NEXT(attach); 2226 MOVE_NEXT(attach);
2225 if (list->items[x]->data == NULL) { //special case 2227 if (!list->items[x]->data) { //special case
2226 attach->id2_val = list->items[x]->type; 2228 attach->id2_val = list->items[x]->type;
2227 DEBUG_EMAIL(("Seen a Reference. The data hasn't been loaded yet. [%#x][%#x]\n", 2229 DEBUG_EMAIL(("Seen a Reference. The data hasn't been loaded yet. [%#x][%#x]\n",
2228 attach->id2_val, list->items[x]->type)); 2230 attach->id2_val, list->items[x]->type));
2229 } else { 2231 } else {
2230 LIST_COPY(attach->data, (char*)); 2232 LIST_COPY(attach->data, (char*));
2801 DEBUG_EMAIL_HEXPRINT((char*)item->record_key, 16); 2803 DEBUG_EMAIL_HEXPRINT((char*)item->record_key, 16);
2802 //INC_CHECK_X(); 2804 //INC_CHECK_X();
2803 break; 2805 break;
2804 case 0x67F2: // ID2 value of the attachments proper record 2806 case 0x67F2: // ID2 value of the attachments proper record
2805 DEBUG_EMAIL(("Attachment ID2 value - ")); 2807 DEBUG_EMAIL(("Attachment ID2 value - "));
2806 if (attach != NULL){ 2808 if (attach){
2807 MOVE_NEXT(attach); 2809 MOVE_NEXT(attach);
2808 memcpy(&(attach->id2_val), list->items[x]->data, sizeof(attach->id2_val)); 2810 memcpy(&(attach->id2_val), list->items[x]->data, sizeof(attach->id2_val));
2809 LE32_CPU(attach->id2_val); 2811 LE32_CPU(attach->id2_val);
2810 DEBUG_EMAIL(("%#x\n", attach->id2_val)); 2812 DEBUG_EMAIL(("%#x\n", attach->id2_val));
2811 } else { 2813 } else {
3128 list->items[x]->type)); 3130 list->items[x]->type));
3129 } else { 3131 } else {
3130 DEBUG_EMAIL(("Unknown Not Printable [%#x]\n", 3132 DEBUG_EMAIL(("Unknown Not Printable [%#x]\n",
3131 list->items[x]->type)); 3133 list->items[x]->type));
3132 } 3134 }
3133 if (list->items[x]->data != NULL) { 3135 if (list->items[x]->data) {
3134 free(list->items[x]->data); 3136 free(list->items[x]->data);
3135 list->items[x]->data = NULL; 3137 list->items[x]->data = NULL;
3136 } 3138 }
3137 //INC_CHECK_X(); 3139 //INC_CHECK_X();
3138 } 3140 }
3149 3151
3150 int32_t _pst_free_list(pst_num_array *list) { 3152 int32_t _pst_free_list(pst_num_array *list) {
3151 int32_t x = 0; 3153 int32_t x = 0;
3152 pst_num_array *l; 3154 pst_num_array *l;
3153 DEBUG_ENT("_pst_free_list"); 3155 DEBUG_ENT("_pst_free_list");
3154 while (list != NULL) { 3156 while (list) {
3155 while (x < list->count_item) { 3157 while (x < list->count_item) {
3156 if (list->items[x]->data != NULL) { 3158 if (list->items[x]->data) {
3157 free (list->items[x]->data); 3159 free (list->items[x]->data);
3158 } 3160 }
3159 if (list->items[x] != NULL) { 3161 if (list->items[x]) {
3160 free (list->items[x]); 3162 free (list->items[x]);
3161 } 3163 }
3162 x++; 3164 x++;
3163 } 3165 }
3164 if (list->items != NULL) { 3166 if (list->items) {
3165 free(list->items); 3167 free(list->items);
3166 } 3168 }
3167 l = list; 3169 l = list;
3168 list = list->next; 3170 list = list->next;
3169 free (l); 3171 free (l);
3175 3177
3176 3178
3177 int32_t _pst_free_id2(pst_index2_ll * head) { 3179 int32_t _pst_free_id2(pst_index2_ll * head) {
3178 pst_index2_ll *t; 3180 pst_index2_ll *t;
3179 DEBUG_ENT("_pst_free_id2"); 3181 DEBUG_ENT("_pst_free_id2");
3180 while (head != NULL) { 3182 while (head) {
3181 t = head->next; 3183 t = head->next;
3182 free (head); 3184 free (head);
3183 head = t; 3185 head = t;
3184 } 3186 }
3185 DEBUG_RET(); 3187 DEBUG_RET();
3188 3190
3189 3191
3190 int32_t _pst_free_id (pst_index_ll *head) { 3192 int32_t _pst_free_id (pst_index_ll *head) {
3191 pst_index_ll *t; 3193 pst_index_ll *t;
3192 DEBUG_ENT("_pst_free_id"); 3194 DEBUG_ENT("_pst_free_id");
3193 while (head != NULL) { 3195 while (head) {
3194 t = head->next; 3196 t = head->next;
3195 free(head); 3197 free(head);
3196 head = t; 3198 head = t;
3197 } 3199 }
3198 DEBUG_RET(); 3200 DEBUG_RET();
3201 3203
3202 3204
3203 int32_t _pst_free_desc (pst_desc_ll *head) { 3205 int32_t _pst_free_desc (pst_desc_ll *head) {
3204 pst_desc_ll *t; 3206 pst_desc_ll *t;
3205 DEBUG_ENT("_pst_free_desc"); 3207 DEBUG_ENT("_pst_free_desc");
3206 while (head != NULL) { 3208 while (head) {
3207 while (head->child != NULL) { 3209 while (head->child) {
3208 head = head->child; 3210 head = head->child;
3209 } 3211 }
3210 3212
3211 // point t to the next item 3213 // point t to the next item
3212 t = head->next; 3214 t = head->next;
3213 if (t == NULL && head->parent != NULL) { 3215 if (!t && head->parent) {
3214 t = head->parent; 3216 t = head->parent;
3215 t->child = NULL; // set the child to NULL so we don't come back here again! 3217 t->child = NULL; // set the child to NULL so we don't come back here again!
3216 } 3218 }
3217 3219
3218 if (head != NULL) 3220 if (head) free(head);
3219 free(head); 3221 else DIE(("head is NULL"));
3220 else {
3221 DIE(("head is NULL"));
3222 }
3223 3222
3224 head = t; 3223 head = t;
3225 } 3224 }
3226 DEBUG_RET(); 3225 DEBUG_RET();
3227 return 1; 3226 return 1;
3229 3228
3230 3229
3231 int32_t _pst_free_xattrib(pst_x_attrib_ll *x) { 3230 int32_t _pst_free_xattrib(pst_x_attrib_ll *x) {
3232 pst_x_attrib_ll *t; 3231 pst_x_attrib_ll *t;
3233 DEBUG_ENT("_pst_free_xattrib"); 3232 DEBUG_ENT("_pst_free_xattrib");
3234 while (x != NULL) { 3233 while (x) {
3235 if (x->data) free(x->data); 3234 if (x->data) free(x->data);
3236 t = x->next; 3235 t = x->next;
3237 free(x); 3236 free(x);
3238 x = t; 3237 x = t;
3239 } 3238 }
3301 if ((i_ptr = _pst_getID(pf, id2_rec.table2)) == NULL) { 3300 if ((i_ptr = _pst_getID(pf, id2_rec.table2)) == NULL) {
3302 DEBUG_WARN(("\tTable2 [%#x] not found\n", id2_rec.table2)); 3301 DEBUG_WARN(("\tTable2 [%#x] not found\n", id2_rec.table2));
3303 } 3302 }
3304 else { 3303 else {
3305 DEBUG_INDEX(("\tGoing deeper for table2 [%#x]\n", id2_rec.table2)); 3304 DEBUG_INDEX(("\tGoing deeper for table2 [%#x]\n", id2_rec.table2));
3306 if ((i2_ptr = _pst_build_id2(pf, i_ptr, head)) != NULL) { 3305 if ((i2_ptr = _pst_build_id2(pf, i_ptr, head))) {
3307 // DEBUG_INDEX(("_pst_build_id2(): \t\tAdding new list onto end of current\n")); 3306 // DEBUG_INDEX(("_pst_build_id2(): \t\tAdding new list onto end of current\n"));
3308 // if (head == NULL) 3307 // if (!head)
3309 // head = i2_ptr; 3308 // head = i2_ptr;
3310 // if (tail != NULL) 3309 // if (tail)
3311 // tail->next = i2_ptr; 3310 // tail->next = i2_ptr;
3312 // while (i2_ptr->next != NULL) 3311 // while (i2_ptr->next)
3313 // i2_ptr = i2_ptr->next; 3312 // i2_ptr = i2_ptr->next;
3314 // tail = i2_ptr; 3313 // tail = i2_ptr;
3315 } 3314 }
3316 // need to re-establish tail 3315 // need to re-establish tail
3317 DEBUG_INDEX(("Returned from depth\n")); 3316 DEBUG_INDEX(("Returned from depth\n"));
3328 return head; 3327 return head;
3329 } 3328 }
3330 3329
3331 3330
3332 // This version of free does NULL check first 3331 // This version of free does NULL check first
3333 #define SAFE_FREE(x) {if (x != NULL) free(x);} 3332 #define SAFE_FREE(x) {if (x) free(x);}
3334 3333
3335 void _pst_freeItem(pst_item *item) { 3334 void _pst_freeItem(pst_item *item) {
3336 pst_item_attach *t; 3335 pst_item_attach *t;
3337 pst_item_extra_field *et; 3336 pst_item_extra_field *et;
3338 3337
3339 DEBUG_ENT("_pst_freeItem"); 3338 DEBUG_ENT("_pst_freeItem");
3340 if (item != NULL) { 3339 if (item) {
3341 if (item->email) { 3340 if (item->email) {
3342 SAFE_FREE(item->email->arrival_date); 3341 SAFE_FREE(item->email->arrival_date);
3343 SAFE_FREE(item->email->body); 3342 SAFE_FREE(item->email->body);
3344 SAFE_FREE(item->email->cc_address); 3343 SAFE_FREE(item->email->cc_address);
3345 SAFE_FREE(item->email->common_name); 3344 SAFE_FREE(item->email->common_name);
3368 SAFE_FREE(item->email->sender2_access); 3367 SAFE_FREE(item->email->sender2_access);
3369 SAFE_FREE(item->email->sender2_address); 3368 SAFE_FREE(item->email->sender2_address);
3370 SAFE_FREE(item->email->sent_date); 3369 SAFE_FREE(item->email->sent_date);
3371 SAFE_FREE(item->email->sentmail_folder); 3370 SAFE_FREE(item->email->sentmail_folder);
3372 SAFE_FREE(item->email->sentto_address); 3371 SAFE_FREE(item->email->sentto_address);
3373 if (item->email->subject != NULL) 3372 if (item->email->subject)
3374 SAFE_FREE(item->email->subject->subj); 3373 SAFE_FREE(item->email->subject->subj);
3375 SAFE_FREE(item->email->subject); 3374 SAFE_FREE(item->email->subject);
3376 free(item->email); 3375 free(item->email);
3377 } 3376 }
3378 if (item->folder) { 3377 if (item->folder) {
3476 SAFE_FREE(item->contact->transmittable_display_name); 3475 SAFE_FREE(item->contact->transmittable_display_name);
3477 SAFE_FREE(item->contact->ttytdd_phone); 3476 SAFE_FREE(item->contact->ttytdd_phone);
3478 SAFE_FREE(item->contact->wedding_anniversary); 3477 SAFE_FREE(item->contact->wedding_anniversary);
3479 free(item->contact); 3478 free(item->contact);
3480 } 3479 }
3481 while (item->attach != NULL) { 3480 while (item->attach) {
3482 SAFE_FREE(item->attach->filename1); 3481 SAFE_FREE(item->attach->filename1);
3483 SAFE_FREE(item->attach->filename2); 3482 SAFE_FREE(item->attach->filename2);
3484 SAFE_FREE(item->attach->mimetype); 3483 SAFE_FREE(item->attach->mimetype);
3485 SAFE_FREE(item->attach->data); 3484 SAFE_FREE(item->attach->data);
3486 t = item->attach->next; 3485 t = item->attach->next;
3487 free(item->attach); 3486 free(item->attach);
3488 item->attach = t; 3487 item->attach = t;
3489 } 3488 }
3490 while (item->extra_fields != NULL) { 3489 while (item->extra_fields) {
3491 SAFE_FREE(item->extra_fields->field_name); 3490 SAFE_FREE(item->extra_fields->field_name);
3492 SAFE_FREE(item->extra_fields->value); 3491 SAFE_FREE(item->extra_fields->value);
3493 et = item->extra_fields->next; 3492 et = item->extra_fields->next;
3494 free(item->extra_fields); 3493 free(item->extra_fields);
3495 item->extra_fields = et; 3494 item->extra_fields = et;
3522 3521
3523 3522
3524 int32_t _pst_getBlockOffset(char *buf, int32_t read_size, int32_t i_offset, int32_t offset, pst_block_offset *p) { 3523 int32_t _pst_getBlockOffset(char *buf, int32_t read_size, int32_t i_offset, int32_t offset, pst_block_offset *p) {
3525 int32_t of1 = offset>>4; 3524 int32_t of1 = offset>>4;
3526 DEBUG_ENT("_pst_getBlockOffset"); 3525 DEBUG_ENT("_pst_getBlockOffset");
3527 if ((p == NULL) || (buf == NULL) || (i_offset == 0) || (i_offset+2+of1+sizeof(*p) > read_size)) { 3526 if (!p || !buf || (i_offset == 0) || (i_offset+2+of1+sizeof(*p) > read_size)) {
3528 DEBUG_WARN(("p is NULL or buf is NULL or offset is 0 (%p, %p, %#x, %i, %i)\n", p, buf, offset, read_size, i_offset)); 3527 DEBUG_WARN(("p is NULL or buf is NULL or offset is 0 (%p, %p, %#x, %i, %i)\n", p, buf, offset, read_size, i_offset));
3529 DEBUG_RET(); 3528 DEBUG_RET();
3530 return -1; 3529 return -1;
3531 } 3530 }
3532 memcpy(&(p->from), &(buf[(i_offset+2)+of1]), sizeof(p->from)); 3531 memcpy(&(p->from), &(buf[(i_offset+2)+of1]), sizeof(p->from));
3554 // it isn't based on sound principles either. 3553 // it isn't based on sound principles either.
3555 // update: seems that the last two sig bits are flags. u tell me! 3554 // update: seems that the last two sig bits are flags. u tell me!
3556 id &= 0xFFFFFFFE; // remove least sig. bit. seems that it might work if I do this 3555 id &= 0xFFFFFFFE; // remove least sig. bit. seems that it might work if I do this
3557 3556
3558 DEBUG_INDEX(("Trying to find %#x\n", id)); 3557 DEBUG_INDEX(("Trying to find %#x\n", id));
3559 if (ptr == NULL) ptr = pf->i_head; 3558 if (!ptr) ptr = pf->i_head;
3560 while (ptr && (ptr->id != id)) { 3559 while (ptr && (ptr->id != id)) {
3561 ptr = ptr->next; 3560 ptr = ptr->next;
3562 if (ptr == NULL) { 3561 }
3563 break; 3562 if (ptr) {DEBUG_INDEX(("Found Value %#x\n", ptr->id));}
3564 } 3563 else {DEBUG_INDEX(("ERROR: Value not found\n")); }
3565 }
3566 if (ptr) {
3567 DEBUG_INDEX(("Found Value %#x\n", ptr->id));
3568 } else {
3569 DEBUG_INDEX(("ERROR: Value not found\n"));
3570 }
3571 DEBUG_RET(); 3564 DEBUG_RET();
3572 return ptr; 3565 return ptr;
3573 } 3566 }
3574 3567
3575 3568
3579 DEBUG_INDEX(("Trying to find %#x\n", id)); 3572 DEBUG_INDEX(("Trying to find %#x\n", id));
3580 while (ptr && (ptr->id2 != id)) { 3573 while (ptr && (ptr->id2 != id)) {
3581 ptr = ptr->next; 3574 ptr = ptr->next;
3582 } 3575 }
3583 if (ptr) { 3576 if (ptr) {
3584 if (ptr->id) { 3577 if (ptr->id) {DEBUG_INDEX(("Found value %#x\n", ptr->id->id)); }
3585 DEBUG_INDEX(("Found value %#x\n", ptr->id->id)); 3578 else {DEBUG_INDEX(("Found value, though it is NULL!\n"));}
3586 } else {
3587 DEBUG_INDEX(("Found value, though it is NULL!\n"));
3588 }
3589 DEBUG_RET(); 3579 DEBUG_RET();
3590 return ptr->id; 3580 return ptr->id;
3591 } 3581 }
3592 DEBUG_INDEX(("ERROR Not Found\n")); 3582 DEBUG_INDEX(("ERROR Not Found\n"));
3593 DEBUG_RET(); 3583 DEBUG_RET();
3601 while (ptr && (ptr->id != id)) { 3591 while (ptr && (ptr->id != id)) {
3602 if (ptr->child) { 3592 if (ptr->child) {
3603 ptr = ptr->child; 3593 ptr = ptr->child;
3604 continue; 3594 continue;
3605 } 3595 }
3606 while (ptr->next == NULL && ptr->parent != NULL) { 3596 while (!ptr->next && ptr->parent) {
3607 ptr = ptr->parent; 3597 ptr = ptr->parent;
3608 } 3598 }
3609 ptr = ptr->next; 3599 ptr = ptr->next;
3610 } 3600 }
3611 DEBUG_RET(); 3601 DEBUG_RET();
3618 int32_t depth = 0; 3608 int32_t depth = 0;
3619 char spaces[100]; 3609 char spaces[100];
3620 DEBUG_ENT("_pst_printDptr"); 3610 DEBUG_ENT("_pst_printDptr");
3621 memset(spaces, ' ', 99); 3611 memset(spaces, ' ', 99);
3622 spaces[99] = '\0'; 3612 spaces[99] = '\0';
3623 while (ptr != NULL) { 3613 while (ptr) {
3624 DEBUG_INDEX(("%s%#x [%i] desc=%#x, list=%#x\n", &(spaces[(99-depth<0?0:99-depth)]), ptr->id, ptr->no_child, 3614 DEBUG_INDEX(("%s%#x [%i] desc=%#x, list=%#x\n", &(spaces[(99-depth<0?0:99-depth)]), ptr->id, ptr->no_child,
3625 (ptr->desc==NULL?0:ptr->desc->id), 3615 (ptr->desc==NULL?0:ptr->desc->id),
3626 (ptr->list_index==NULL?0:ptr->list_index->id))); 3616 (ptr->list_index==NULL?0:ptr->list_index->id)));
3627 if (ptr->child != NULL) { 3617 if (ptr->child) {
3628 depth++; 3618 depth++;
3629 ptr = ptr->child; 3619 ptr = ptr->child;
3630 continue; 3620 continue;
3631 } 3621 }
3632 while (ptr->next == NULL && ptr->parent != NULL) { 3622 while (!ptr->next && ptr->parent) {
3633 depth--; 3623 depth--;
3634 ptr = ptr->parent; 3624 ptr = ptr->parent;
3635 } 3625 }
3636 ptr = ptr->next; 3626 ptr = ptr->next;
3637 } 3627 }
3641 3631
3642 3632
3643 int32_t _pst_printIDptr(pst_file* pf) { 3633 int32_t _pst_printIDptr(pst_file* pf) {
3644 pst_index_ll *ptr = pf->i_head; 3634 pst_index_ll *ptr = pf->i_head;
3645 DEBUG_ENT("_pst_printIDptr"); 3635 DEBUG_ENT("_pst_printIDptr");
3646 while (ptr != NULL) { 3636 while (ptr) {
3647 DEBUG_INDEX(("%#x offset=%#x size=%#x\n", ptr->id, ptr->offset, ptr->size)); 3637 DEBUG_INDEX(("%#x offset=%#x size=%#x\n", ptr->id, ptr->offset, ptr->size));
3648 ptr = ptr->next; 3638 ptr = ptr->next;
3649 } 3639 }
3650 DEBUG_RET(); 3640 DEBUG_RET();
3651 return 0; 3641 return 0;
3652 } 3642 }
3653 3643
3654 3644
3655 int32_t _pst_printID2ptr(pst_index2_ll *ptr) { 3645 int32_t _pst_printID2ptr(pst_index2_ll *ptr) {
3656 DEBUG_ENT("_pst_printID2ptr"); 3646 DEBUG_ENT("_pst_printID2ptr");
3657 while (ptr != NULL) { 3647 while (ptr) {
3658 DEBUG_INDEX(("%#x id=%#x\n", ptr->id2, (ptr->id!=NULL?ptr->id->id:0))); 3648 DEBUG_INDEX(("%#x id=%#x\n", ptr->id2, (ptr->id!=NULL?ptr->id->id:0)));
3659 ptr = ptr->next; 3649 ptr = ptr->next;
3660 } 3650 }
3661 DEBUG_RET(); 3651 DEBUG_RET();
3662 return 0; 3652 return 0;
3671 fpos = ftell(fp); 3661 fpos = ftell(fp);
3672 fseek(fp, offset, SEEK_SET); 3662 fseek(fp, offset, SEEK_SET);
3673 fread(&size, sizeof(int16_t), 1, fp); 3663 fread(&size, sizeof(int16_t), 1, fp);
3674 fseek(fp, offset, SEEK_SET); 3664 fseek(fp, offset, SEEK_SET);
3675 DEBUG_READ(("Allocating %i bytes\n", size)); 3665 DEBUG_READ(("Allocating %i bytes\n", size));
3676 if (*buf != NULL) { 3666 if (*buf) {
3677 DEBUG_READ(("Freeing old memory\n")); 3667 DEBUG_READ(("Freeing old memory\n"));
3678 free(*buf); 3668 free(*buf);
3679 } 3669 }
3680 *buf = (void*)xmalloc(size); 3670 *buf = (void*)xmalloc(size);
3681 size = fread(*buf, 1, size, fp); 3671 size = fread(*buf, 1, size, fp);
3698 size_t rsize, z; 3688 size_t rsize, z;
3699 DEBUG_ENT("_pst_read_block_size"); 3689 DEBUG_ENT("_pst_read_block_size");
3700 DEBUG_READ(("Reading block from %#x, %i bytes\n", offset, size)); 3690 DEBUG_READ(("Reading block from %#x, %i bytes\n", offset, size));
3701 fpos = ftell(pf->fp); 3691 fpos = ftell(pf->fp);
3702 fseek(pf->fp, offset, SEEK_SET); 3692 fseek(pf->fp, offset, SEEK_SET);
3703 if (*buf != NULL) { 3693 if (*buf) {
3704 DEBUG_READ(("Freeing old memory\n")); 3694 DEBUG_READ(("Freeing old memory\n"));
3705 free(*buf); 3695 free(*buf);
3706 } 3696 }
3707 3697
3708 *buf = (void*) xmalloc(size+1); //plus one so that we can NULL terminate it later 3698 *buf = (void*) xmalloc(size+1); //plus one so that we can NULL terminate it later
3763 memcpy(&(buf3[size]), buf2, z); 3753 memcpy(&(buf3[size]), buf2, z);
3764 size += z; 3754 size += z;
3765 y++; 3755 y++;
3766 } 3756 }
3767 free(*buf); 3757 free(*buf);
3768 if (buf2 != NULL) 3758 if (buf2) free(buf2);
3769 free(buf2); 3759 if (!buf3) {
3770 if (buf3 == NULL) {
3771 // this can happen if count == 0. We should create an empty buffer so we don't 3760 // this can happen if count == 0. We should create an empty buffer so we don't
3772 // confuse any clients 3761 // confuse any clients
3773 buf3 = (char*) xmalloc(1); 3762 buf3 = (char*) xmalloc(1);
3774 } 3763 }
3775 *buf = buf3; 3764 *buf = buf3;
3785 3774
3786 int32_t _pst_decrypt(unsigned char *buf, size_t size, int32_t type) { 3775 int32_t _pst_decrypt(unsigned char *buf, size_t size, int32_t type) {
3787 size_t x = 0; 3776 size_t x = 0;
3788 unsigned char y; 3777 unsigned char y;
3789 DEBUG_ENT("_pst_decrypt"); 3778 DEBUG_ENT("_pst_decrypt");
3790 if (buf == NULL) { 3779 if (!buf) {
3791 DEBUG_RET(); 3780 DEBUG_RET();
3792 return -1; 3781 return -1;
3793 } 3782 }
3794 3783
3795 if (type == PST_COMP_ENCRYPT) { 3784 if (type == PST_COMP_ENCRYPT) {
3858 DEBUG_INDEX(("Cannot find ID %#x\n", id)); 3847 DEBUG_INDEX(("Cannot find ID %#x\n", id));
3859 DEBUG_RET(); 3848 DEBUG_RET();
3860 return 0; 3849 return 0;
3861 } 3850 }
3862 fseek(pf->fp, rec->offset, SEEK_SET); 3851 fseek(pf->fp, rec->offset, SEEK_SET);
3863 if (*b != NULL) { 3852 if (*b) {
3864 DEBUG_INDEX(("freeing old memory in b\n")); 3853 DEBUG_INDEX(("freeing old memory in b\n"));
3865 free(*b); 3854 free(*b);
3866 } 3855 }
3867 3856
3868 DEBUG_INDEX(("id = %#x, record size = %#x, offset = %#x\n", id, rec->size, rec->offset)); 3857 DEBUG_INDEX(("id = %#x, record size = %#x, offset = %#x\n", id, rec->size, rec->offset));
3889 // size_t ret; 3878 // size_t ret;
3890 struct holder h = {buf, NULL, 0}; 3879 struct holder h = {buf, NULL, 0};
3891 DEBUG_ENT("_pst_ff_getID2block"); 3880 DEBUG_ENT("_pst_ff_getID2block");
3892 ptr = _pst_getID2(id2_head, id2); 3881 ptr = _pst_getID2(id2_head, id2);
3893 3882
3894 if (ptr == NULL) { 3883 if (!ptr) {
3895 DEBUG_INDEX(("Cannot find id2 value %#x\n", id2)); 3884 DEBUG_INDEX(("Cannot find id2 value %#x\n", id2));
3896 DEBUG_RET(); 3885 DEBUG_RET();
3897 return 0; 3886 return 0;
3898 } 3887 }
3899 DEBUG_RET(); 3888 DEBUG_RET();
3906 int32_t ret; 3895 int32_t ret;
3907 unsigned char *b = NULL, *t; 3896 unsigned char *b = NULL, *t;
3908 DEBUG_ENT("_pst_ff_getID2data"); 3897 DEBUG_ENT("_pst_ff_getID2data");
3909 if (!(ptr->id & 0x02)) { 3898 if (!(ptr->id & 0x02)) {
3910 ret = _pst_ff_getIDblock_dec(pf, ptr->id, &b); 3899 ret = _pst_ff_getIDblock_dec(pf, ptr->id, &b);
3911 if (h->buf != NULL) { 3900 if (h->buf) {
3912 *(h->buf) = b; 3901 *(h->buf) = b;
3913 } else if (h->base64 == 1 && h->fp != NULL) { 3902 } else if ((h->base64 == 1) && h->fp) {
3914 t = base64_encode(b, ret); 3903 t = base64_encode(b, ret);
3915 pst_fwrite(t, 1, strlen(t), h->fp); 3904 pst_fwrite(t, 1, strlen(t), h->fp);
3916 free(b); 3905 free(b);
3917 } else if (h->fp != NULL) { 3906 } else if (h->fp) {
3918 pst_fwrite(b, 1, ret, h->fp); 3907 pst_fwrite(b, 1, ret, h->fp);
3919 free(b); 3908 free(b);
3920 } 3909 }
3921 //if ((*buf)[0] == 0x1) { 3910 //if ((*buf)[0] == 0x1) {
3922 // DEBUG_WARN(("WARNING: buffer starts with 0x1, but I didn't expect it to!\n")); 3911 // DEBUG_WARN(("WARNING: buffer starts with 0x1, but I didn't expect it to!\n"));
3924 } else { 3913 } else {
3925 // here we will assume it is a block that points to others 3914 // here we will assume it is a block that points to others
3926 DEBUG_READ(("Assuming it is a multi-block record because of it's id\n")); 3915 DEBUG_READ(("Assuming it is a multi-block record because of it's id\n"));
3927 ret = _pst_ff_compile_ID(pf, ptr->id, h, 0); 3916 ret = _pst_ff_compile_ID(pf, ptr->id, h, 0);
3928 } 3917 }
3929 if (h->buf != NULL && *h->buf != NULL) 3918 if (h->buf && *h->buf)
3930 (*(h->buf))[ret]='\0'; 3919 (*(h->buf))[ret]='\0';
3931 DEBUG_RET(); 3920 DEBUG_RET();
3932 return ret; 3921 return ret;
3933 } 3922 }
3934 3923
3945 return 0; 3934 return 0;
3946 if ((buf3[0] != 0x1)) { // if bit 8 is set) { 3935 if ((buf3[0] != 0x1)) { // if bit 8 is set) {
3947 // if ((buf3)[0] != 0x1 && (buf3)[1] > 4) { 3936 // if ((buf3)[0] != 0x1 && (buf3)[1] > 4) {
3948 DEBUG_WARN(("WARNING: buffer doesn't start with 0x1, but I expected it to or doesn't have it's two-bit set!\n")); 3937 DEBUG_WARN(("WARNING: buffer doesn't start with 0x1, but I expected it to or doesn't have it's two-bit set!\n"));
3949 DEBUG_WARN(("Treating as normal buffer\n")); 3938 DEBUG_WARN(("Treating as normal buffer\n"));
3950 if (pf->encryption) 3939 if (pf->encryption) _pst_decrypt(buf3, a, pf->encryption);
3951 _pst_decrypt(buf3, a, pf->encryption); 3940 if (h->buf)
3952 if (h->buf != NULL)
3953 *(h->buf) = buf3; 3941 *(h->buf) = buf3;
3954 else if (h->base64 == 1 && h->fp != NULL) { 3942 else if (h->base64 == 1 && h->fp) {
3955 t = base64_encode(buf3, a); 3943 t = base64_encode(buf3, a);
3956 pst_fwrite(t, 1, strlen(t), h->fp); 3944 pst_fwrite(t, 1, strlen(t), h->fp);
3957 free(buf3); 3945 free(buf3);
3958 } else if (h->fp != NULL) { 3946 } else if (h->fp) {
3959 pst_fwrite(buf3, 1, a, h->fp); 3947 pst_fwrite(buf3, 1, a, h->fp);
3960 free(buf3); 3948 free(buf3);
3961 } 3949 }
3962 DEBUG_RET(); 3950 DEBUG_RET();
3963 return a; 3951 return a;
3973 memcpy(&x, &buf3[0x08+(y*4)], sizeof(int32_t)); 3961 memcpy(&x, &buf3[0x08+(y*4)], sizeof(int32_t));
3974 LE32_CPU(x); 3962 LE32_CPU(x);
3975 if (fdepth == 0x1) { 3963 if (fdepth == 0x1) {
3976 if ((z = _pst_ff_getIDblock(pf, x, &buf2)) == 0) { 3964 if ((z = _pst_ff_getIDblock(pf, x, &buf2)) == 0) {
3977 DEBUG_WARN(("call to getIDblock returned zero %i\n", z)); 3965 DEBUG_WARN(("call to getIDblock returned zero %i\n", z));
3978 if (buf2 != NULL) 3966 if (buf2) free(buf2);
3979 free(buf2);
3980 free(buf3); 3967 free(buf3);
3981 return z; 3968 return z;
3982 } 3969 }
3983 if (pf->encryption) _pst_decrypt(buf2, z, pf->encryption); 3970 if (pf->encryption) _pst_decrypt(buf2, z, pf->encryption);
3984 if (h->buf != NULL) { 3971 if (h->buf) {
3985 *(h->buf) = realloc(*(h->buf), size+z+1); 3972 *(h->buf) = realloc(*(h->buf), size+z+1);
3986 DEBUG_READ(("appending read data of size %i onto main buffer from pos %i\n", z, size)); 3973 DEBUG_READ(("appending read data of size %i onto main buffer from pos %i\n", z, size));
3987 memcpy(&((*(h->buf))[size]), buf2, z); 3974 memcpy(&((*(h->buf))[size]), buf2, z);
3988 } 3975 }
3989 else if (h->base64 == 1 && h->fp != NULL) { 3976 else if ((h->base64 == 1) && h->fp) {
3990 // include any byte left over from the last one encoding 3977 // include any byte left over from the last one encoding
3991 buf2 = (char*)realloc(buf2, z+h->base64_extra); 3978 buf2 = (char*)realloc(buf2, z+h->base64_extra);
3992 memmove(buf2+h->base64_extra, buf2, z); 3979 memmove(buf2+h->base64_extra, buf2, z);
3993 memcpy(buf2, h->base64_extra_chars, h->base64_extra); 3980 memcpy(buf2, h->base64_extra_chars, h->base64_extra);
3994 z+= h->base64_extra; 3981 z+= h->base64_extra;
4000 t = base64_encode(buf2, z-b); 3987 t = base64_encode(buf2, z-b);
4001 pst_fwrite(t, 1, strlen(t), h->fp); 3988 pst_fwrite(t, 1, strlen(t), h->fp);
4002 DEBUG_READ(("writing %i bytes to file as base64 [%i]. Currently %i\n", 3989 DEBUG_READ(("writing %i bytes to file as base64 [%i]. Currently %i\n",
4003 z, strlen(t), size)); 3990 z, strlen(t), size));
4004 } 3991 }
4005 else if (h->fp != NULL) { 3992 else if (h->fp) {
4006 DEBUG_READ(("writing %i bytes to file. Currently %i\n", z, size)); 3993 DEBUG_READ(("writing %i bytes to file. Currently %i\n", z, size));
4007 pst_fwrite(buf2, 1, z, h->fp); 3994 pst_fwrite(buf2, 1, z, h->fp);
4008 } 3995 }
4009 size += z; 3996 size += z;
4010 y++; 3997 y++;
4019 size = z; 4006 size = z;
4020 y++; 4007 y++;
4021 } 4008 }
4022 } 4009 }
4023 free(buf3); 4010 free(buf3);
4024 if (buf2 != NULL) 4011 if (buf2) free(buf2);
4025 free(buf2);
4026 DEBUG_RET(); 4012 DEBUG_RET();
4027 return size; 4013 return size;
4028 } 4014 }
4029 4015
4030 4016
4099 4085
4100 4086
4101 size_t pst_fwrite(const void*ptr, size_t size, size_t nmemb, FILE*stream) { 4087 size_t pst_fwrite(const void*ptr, size_t size, size_t nmemb, FILE*stream) {
4102 size_t r; 4088 size_t r;
4103 DEBUG_ENT("pst_fwrite"); 4089 DEBUG_ENT("pst_fwrite");
4104 if (ptr != NULL) 4090 if (ptr)
4105 r = fwrite(ptr, size, nmemb, stream); 4091 r = fwrite(ptr, size, nmemb, stream);
4106 else { 4092 else {
4107 r = 0; 4093 r = 0;
4108 DEBUG_WARN(("An attempt to write a NULL Pointer was made\n")); 4094 DEBUG_WARN(("An attempt to write a NULL Pointer was made\n"));
4109 } 4095 }