comparison src/libpst.c @ 39:2ad7ef0a3c4f stable-0-5-10

more valgrind fixes
author carl
date Mon, 20 Aug 2007 20:43:17 -0700
parents 6fe121a971c9
children be6d5329cc01
comparison
equal deleted inserted replaced
38:f5c024aa1dc5 39:2ad7ef0a3c4f
264 ptr = _pst_getID(pf, attach->id_val); 264 ptr = _pst_getID(pf, attach->id_val);
265 if (ptr) { 265 if (ptr) {
266 size = _pst_ff_getID2data(pf, ptr, &h); 266 size = _pst_ff_getID2data(pf, ptr, &h);
267 // will need to encode any bytes left over 267 // will need to encode any bytes left over
268 c = base64_encode(h.base64_extra_chars, h.base64_extra); 268 c = base64_encode(h.base64_extra_chars, h.base64_extra);
269 if (c) pst_fwrite(c, 1, strlen(c), fp); 269 if (c) {
270 pst_fwrite(c, 1, strlen(c), fp);
271 free(c); // caught by valgrind
272 }
270 } else { 273 } else {
271 DEBUG_WARN (("Couldn't find ID pointer. Cannot save attachement to Base64\n")); 274 DEBUG_WARN (("Couldn't find ID pointer. Cannot save attachement to Base64\n"));
272 size = 0; 275 size = 0;
273 } 276 }
274 attach->size = size; 277 attach->size = size;
275 } else { 278 } else {
276 // encode the attachment to the file 279 // encode the attachment to the file
277 c = base64_encode(attach->data, attach->size); 280 c = base64_encode(attach->data, attach->size);
278 if (c) pst_fwrite(c, 1, strlen(c), fp); 281 if (c) {
282 pst_fwrite(c, 1, strlen(c), fp);
283 free(c); // caught by valgrind
284 }
279 size = attach->size; 285 size = attach->size;
280 } 286 }
281 DEBUG_RET(); 287 DEBUG_RET();
282 return 1; 288 return 1;
283 } 289 }
952 } 958 }
953 959
954 item = (pst_item*) xmalloc(sizeof(pst_item)); 960 item = (pst_item*) xmalloc(sizeof(pst_item));
955 memset(item, 0, sizeof(pst_item)); 961 memset(item, 0, sizeof(pst_item));
956 962
957 if (_pst_process(list, item)) { 963 if (_pst_process(list, item, NULL)) {
958 DEBUG_WARN(("_pst_process() returned non-zero value. That is an error\n")); 964 DEBUG_WARN(("_pst_process() returned non-zero value. That is an error\n"));
959 if (item) free(item); 965 if (item) _pst_freeItem(item);
960 if (list) _pst_free_list(list); 966 if (list) _pst_free_list(list);
961 if (id2_head) _pst_free_id2(id2_head); 967 if (id2_head) _pst_free_id2(id2_head);
962 DEBUG_RET(); 968 DEBUG_RET();
963 return NULL; 969 return NULL;
964 } else { 970 }
965 if (list) _pst_free_list(list); 971 if (list) _pst_free_list(list);
966 list = NULL; //_pst_process will free the items in the list 972 list = NULL; //_pst_process will free the items in the list
967 }
968 973
969 if ((id_ptr = _pst_getID2(id2_head, 0x671))) { 974 if ((id_ptr = _pst_getID2(id2_head, 0x671))) {
970 // attachements exist - so we will process them 975 // attachements exist - so we will process them
971 while (item->attach) { 976 while (item->attach) {
972 attach = item->attach->next; 977 attach = item->attach->next;
975 } 980 }
976 981
977 DEBUG_EMAIL(("ATTACHEMENT processing attachement\n")); 982 DEBUG_EMAIL(("ATTACHEMENT processing attachement\n"));
978 if ((list = _pst_parse_block(pf, id_ptr->id, id2_head)) == NULL) { 983 if ((list = _pst_parse_block(pf, id_ptr->id, id2_head)) == NULL) {
979 DEBUG_WARN(("ERROR error processing main attachment record\n")); 984 DEBUG_WARN(("ERROR error processing main attachment record\n"));
980 if (item) free(item); 985 if (item) _pst_freeItem(item);
981 if (id2_head) _pst_free_id2(id2_head); 986 if (id2_head) _pst_free_id2(id2_head);
982 DEBUG_RET(); 987 DEBUG_RET();
983 return NULL; 988 return NULL;
984 } 989 }
985 else { 990 else {
990 attach->next = item->attach; 995 attach->next = item->attach;
991 item->attach = attach; 996 item->attach = attach;
992 x++; 997 x++;
993 } 998 }
994 999
995 if (_pst_process(list, item)) { 1000 if (_pst_process(list, item, item->attach)) {
996 DEBUG_WARN(("ERROR _pst_process() failed with attachments\n")); 1001 DEBUG_WARN(("ERROR _pst_process() failed with attachments\n"));
997 if (item) free(item); 1002 if (item) _pst_freeItem(item);
998 if (list) _pst_free_list(list); 1003 if (list) _pst_free_list(list);
999 if (id2_head) _pst_free_id2(id2_head); 1004 if (id2_head) _pst_free_id2(id2_head);
1000 DEBUG_RET(); 1005 DEBUG_RET();
1001 return NULL; 1006 return NULL;
1002 } 1007 }
1015 if ((list = _pst_parse_block(pf, id_ptr->id, NULL)) == NULL) { 1020 if ((list = _pst_parse_block(pf, id_ptr->id, NULL)) == NULL) {
1016 DEBUG_WARN(("ERROR error processing an attachment record\n")); 1021 DEBUG_WARN(("ERROR error processing an attachment record\n"));
1017 attach = attach->next; 1022 attach = attach->next;
1018 continue; 1023 continue;
1019 } 1024 }
1020 if (_pst_process(list, item)) { 1025 if (_pst_process(list, item, attach)) {
1021 DEBUG_WARN(("ERROR _pst_process() failed with an attachment\n")); 1026 DEBUG_WARN(("ERROR _pst_process() failed with an attachment\n"));
1022 if (list) _pst_free_list(list); 1027 if (list) _pst_free_list(list);
1023 list = NULL; 1028 list = NULL;
1024 attach = attach->next; 1029 attach = attach->next;
1025 continue; 1030 continue;
1026 } 1031 }
1027 if (list) _pst_free_list(list); 1032 if (list) _pst_free_list(list);
1028 list = NULL; 1033 list = NULL;
1029 if ((id_ptr = _pst_getID2(id2_head, attach->id2_val))) { 1034 id_ptr = _pst_getID2(id2_head, attach->id2_val);
1035 if (id_ptr) {
1030 // id2_val has been updated to the ID2 value of the datablock containing the 1036 // id2_val has been updated to the ID2 value of the datablock containing the
1031 // attachment data 1037 // attachment data
1032 attach->id_val = id_ptr->id; 1038 attach->id_val = id_ptr->id;
1033 } else { 1039 } else {
1034 DEBUG_WARN(("have not located the correct value for the attachment [%#x]\n", attach->id2_val)); 1040 DEBUG_WARN(("have not located the correct value for the attachment [%#x]\n", attach->id2_val));
1288 na_ptr->next = na_head; 1294 na_ptr->next = na_head;
1289 na_head = na_ptr; 1295 na_head = na_ptr;
1290 // allocate an array of count num_recs to contain sizeof(struct_pst_num_item) 1296 // allocate an array of count num_recs to contain sizeof(struct_pst_num_item)
1291 na_ptr->items = (struct _pst_num_item**) xmalloc(sizeof(struct _pst_num_item)*num_list); 1297 na_ptr->items = (struct _pst_num_item**) xmalloc(sizeof(struct _pst_num_item)*num_list);
1292 na_ptr->count_item = num_list; 1298 na_ptr->count_item = num_list;
1299 na_ptr->orig_count = num_list;
1293 na_ptr->count_array = num_recs; // each record will have a record of the total number of records 1300 na_ptr->count_array = num_recs; // each record will have a record of the total number of records
1294 for (x=0; x<num_list; x++) na_ptr->items[x] = NULL; 1301 for (x=0; x<num_list; x++) na_ptr->items[x] = NULL;
1295 x = 0; 1302 x = 0;
1296 1303
1297 DEBUG_EMAIL(("going to read %i (%#x) items\n", na_ptr->count_item, na_ptr->count_item)); 1304 DEBUG_EMAIL(("going to read %i (%#x) items\n", na_ptr->count_item, na_ptr->count_item));
1314 LE16_CPU(table2_rec.ind2_off); 1321 LE16_CPU(table2_rec.ind2_off);
1315 1322
1316 // table_rec and table2_rec are arranged differently, so assign the values across 1323 // table_rec and table2_rec are arranged differently, so assign the values across
1317 table_rec.type = table2_rec.type; 1324 table_rec.type = table2_rec.type;
1318 table_rec.ref_type = table2_rec.ref_type; 1325 table_rec.ref_type = table2_rec.ref_type;
1319 if ((ind2_end - ind2_ptr) <= (table2_rec.ind2_off + table2_rec.size)) { 1326 table_rec.value = 0;
1327 if ((ind2_end - ind2_ptr) >= (table2_rec.ind2_off + table2_rec.size)) {
1320 int n = table2_rec.size; 1328 int n = table2_rec.size;
1321 int m = sizeof(table_rec.value); 1329 int m = sizeof(table_rec.value);
1322 table_rec.value = 0;
1323 if (n <= m) { 1330 if (n <= m) {
1324 memcpy(&table_rec.value, ind2_ptr + table2_rec.ind2_off, n); 1331 memcpy(&table_rec.value, ind2_ptr + table2_rec.ind2_off, n);
1325 } 1332 }
1326 else { 1333 else {
1327 value_pointer = ind2_ptr + table2_rec.ind2_off; 1334 value_pointer = ind2_ptr + table2_rec.ind2_off;
1342 return NULL; 1349 return NULL;
1343 } 1350 }
1344 DEBUG_EMAIL(("reading block %i (type=%#x, ref_type=%#x, value=%#x)\n", 1351 DEBUG_EMAIL(("reading block %i (type=%#x, ref_type=%#x, value=%#x)\n",
1345 x, table_rec.type, table_rec.ref_type, table_rec.value)); 1352 x, table_rec.type, table_rec.ref_type, table_rec.value));
1346 1353
1347 na_ptr->items[x] = (struct _pst_num_item*) xmalloc(sizeof(struct _pst_num_item)); 1354 if (!na_ptr->items[x]) {
1355 na_ptr->items[x] = (struct _pst_num_item*) xmalloc(sizeof(struct _pst_num_item));
1356 }
1348 memset(na_ptr->items[x], 0, sizeof(struct _pst_num_item)); //init it 1357 memset(na_ptr->items[x], 0, sizeof(struct _pst_num_item)); //init it
1349 1358
1350 // check here to see if the id of the attribute is a mapped one 1359 // check here to see if the id of the attribute is a mapped one
1351 mapptr = pf->x_head; 1360 mapptr = pf->x_head;
1352 while (mapptr && (mapptr->map < table_rec.type)) mapptr = mapptr->next; 1361 while (mapptr && (mapptr->map < table_rec.type)) mapptr = mapptr->next;
1414 na_ptr->items[x]->type = table_rec.ref_type; 1423 na_ptr->items[x]->type = table_rec.ref_type;
1415 na_ptr->items[x]->data = xmalloc(value_size); 1424 na_ptr->items[x]->data = xmalloc(value_size);
1416 memcpy(na_ptr->items[x]->data, value_pointer, value_size); 1425 memcpy(na_ptr->items[x]->data, value_pointer, value_size);
1417 } 1426 }
1418 else if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset7)) { 1427 else if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset7)) {
1419 if (table_rec.value) { 1428 if ((table_rec.value & 0xf) == 0xf) {
1420 DEBUG_WARN(("failed to get block offset for table_rec.value of %#x\n", table_rec.value)); 1429 DEBUG_WARN(("failed to get block offset for table_rec.value of %#x to be read later.\n", table_rec.value));
1430 na_ptr->items[x]->size = 0;
1431 na_ptr->items[x]->data = NULL;
1432 na_ptr->items[x]->type = table_rec.value;
1421 } 1433 }
1422 na_ptr->count_item --; //we will be skipping a row 1434 else {
1423 continue; 1435 if (table_rec.value) {
1436 DEBUG_WARN(("failed to get block offset for table_rec.value of %#x\n", table_rec.value));
1437 }
1438 na_ptr->count_item --; //we will be skipping a row
1439 continue;
1440 }
1424 } 1441 }
1425 else { 1442 else {
1426 value_size = block_offset7.to - block_offset7.from; 1443 value_size = block_offset7.to - block_offset7.from;
1427 na_ptr->items[x]->size = value_size; 1444 na_ptr->items[x]->size = value_size;
1428 na_ptr->items[x]->type = table_rec.ref_type; 1445 na_ptr->items[x]->type = table_rec.ref_type;
1432 } 1449 }
1433 if (table_rec.ref_type == 0xd) { 1450 if (table_rec.ref_type == 0xd) {
1434 // there is still more to do for the type of 0xD 1451 // there is still more to do for the type of 0xD
1435 type_d_rec = (struct _type_d_rec*) na_ptr->items[x]->data; 1452 type_d_rec = (struct _type_d_rec*) na_ptr->items[x]->data;
1436 LE32_CPU(type_d_rec->id); 1453 LE32_CPU(type_d_rec->id);
1437 if ((na_ptr->items[x]->size = _pst_ff_getID2block(pf, type_d_rec->id, i2_head, &(na_ptr->items[x]->data)))==0){ 1454 na_ptr->items[x]->size = _pst_ff_getID2block(pf, type_d_rec->id, i2_head, &(na_ptr->items[x]->data));
1438 DEBUG_WARN(("not able to read the ID2 data. Setting to be read later. %#x\n", 1455 if (!na_ptr->items[x]->size){
1439 type_d_rec->id)); 1456 DEBUG_WARN(("not able to read the ID2 data. Setting to be read later. %#x\n", type_d_rec->id));
1457 na_ptr->items[x]->type = type_d_rec->id; // fetch before freeing data, alias pointer
1440 free(na_ptr->items[x]->data); 1458 free(na_ptr->items[x]->data);
1441 na_ptr->items[x]->size = 0;
1442 na_ptr->items[x]->data = NULL; 1459 na_ptr->items[x]->data = NULL;
1443 na_ptr->items[x]->type = type_d_rec->id;
1444 } 1460 }
1445 } 1461 }
1446 if (na_ptr->items[x]->type == 0) na_ptr->items[x]->type = table_rec.ref_type; 1462 if (na_ptr->items[x]->type == 0) na_ptr->items[x]->type = table_rec.ref_type;
1447 } else { 1463 } else {
1448 WARN(("ERROR Unknown ref_type %#x\n", table_rec.ref_type)); 1464 WARN(("ERROR Unknown ref_type %#x\n", table_rec.ref_type));
1492 next=0;\ 1508 next=0;\
1493 }\ 1509 }\
1494 } 1510 }
1495 1511
1496 1512
1497 int32_t _pst_process(pst_num_array *list , pst_item *item) { 1513 int32_t _pst_process(pst_num_array *list , pst_item *item, pst_item_attach *attach) {
1498 int32_t x, t; 1514 int32_t x, t;
1499 int32_t next = 0; 1515 int32_t next = 0;
1500 pst_item_attach *attach;
1501 pst_item_extra_field *ef; 1516 pst_item_extra_field *ef;
1502 1517
1503 DEBUG_ENT("_pst_process"); 1518 DEBUG_ENT("_pst_process");
1504 if (!item) { 1519 if (!item) {
1505 DEBUG_EMAIL(("item cannot be NULL.\n")); 1520 DEBUG_EMAIL(("item cannot be NULL.\n"));
1506 DEBUG_RET(); 1521 DEBUG_RET();
1507 return -1; 1522 return -1;
1508 } 1523 }
1509
1510 attach = item->attach; // a working variable
1511 1524
1512 while (list) { 1525 while (list) {
1513 x = 0; 1526 x = 0;
1514 while (x < list->count_item) { 1527 while (x < list->count_item) {
1515 // check here to see if the id is one that is mapped. 1528 // check here to see if the id is one that is mapped.
3099 3112
3100 int32_t _pst_free_list(pst_num_array *list) { 3113 int32_t _pst_free_list(pst_num_array *list) {
3101 pst_num_array *l; 3114 pst_num_array *l;
3102 DEBUG_ENT("_pst_free_list"); 3115 DEBUG_ENT("_pst_free_list");
3103 while (list) { 3116 while (list) {
3104 int32_t x = 0;
3105 if (list->items) { 3117 if (list->items) {
3106 while (x < list->count_item) { 3118 int32_t x;
3119 for (x=0; x < list->orig_count; x++) {
3107 if (list->items[x]) { 3120 if (list->items[x]) {
3108 if (list->items[x]->data) { 3121 if (list->items[x]->data) free(list->items[x]->data);
3109 free (list->items[x]->data); 3122 free(list->items[x]);
3110 }
3111 free (list->items[x]);
3112 } 3123 }
3113 x++;
3114 } 3124 }
3115 free(list->items); 3125 free(list->items);
3116 } 3126 }
3117 l = list; 3127 l = list;
3118 list = list->next; 3128 list = list->next;
3875 3885
3876 #define PST_PTR_BLOCK_SIZE 0x120 3886 #define PST_PTR_BLOCK_SIZE 0x120
3877 size_t _pst_ff_getID2block(pst_file *pf, u_int32_t id2, pst_index2_ll *id2_head, unsigned char** buf) { 3887 size_t _pst_ff_getID2block(pst_file *pf, u_int32_t id2, pst_index2_ll *id2_head, unsigned char** buf) {
3878 pst_index_ll* ptr; 3888 pst_index_ll* ptr;
3879 // size_t ret; 3889 // size_t ret;
3880 struct holder h = {buf, NULL, 0}; 3890 struct holder h = {buf, NULL, 0, "", 0};
3881 DEBUG_ENT("_pst_ff_getID2block"); 3891 DEBUG_ENT("_pst_ff_getID2block");
3882 ptr = _pst_getID2(id2_head, id2); 3892 ptr = _pst_getID2(id2_head, id2);
3883 3893
3884 if (!ptr) { 3894 if (!ptr) {
3885 DEBUG_INDEX(("Cannot find id2 value %#x\n", id2)); 3895 DEBUG_INDEX(("Cannot find id2 value %#x\n", id2));
3890 return _pst_ff_getID2data(pf, ptr, &h); 3900 return _pst_ff_getID2data(pf, ptr, &h);
3891 } 3901 }
3892 3902
3893 3903
3894 size_t _pst_ff_getID2data(pst_file *pf, pst_index_ll *ptr, struct holder *h) { 3904 size_t _pst_ff_getID2data(pst_file *pf, pst_index_ll *ptr, struct holder *h) {
3895 // if the attachment begins with 01 01, <= 256 bytes, it is stored in the record
3896 int32_t ret; 3905 int32_t ret;
3897 unsigned char *b = NULL, *t; 3906 unsigned char *b = NULL, *t;
3898 DEBUG_ENT("_pst_ff_getID2data"); 3907 DEBUG_ENT("_pst_ff_getID2data");
3899 if (!(ptr->id & 0x02)) { 3908 if (!(ptr->id & 0x02)) {
3900 ret = _pst_ff_getIDblock_dec(pf, ptr->id, &b); 3909 ret = _pst_ff_getIDblock_dec(pf, ptr->id, &b);
3901 if (h->buf) { 3910 if (h->buf) {
3902 *(h->buf) = b; 3911 *(h->buf) = b;
3903 } else if ((h->base64 == 1) && h->fp) { 3912 } else if ((h->base64 == 1) && h->fp) {
3904 t = base64_encode(b, ret); 3913 t = base64_encode(b, ret);
3905 if (t) pst_fwrite(t, 1, strlen(t), h->fp); 3914 if (t) {
3915 pst_fwrite(t, 1, strlen(t), h->fp);
3916 free(t); // caught by valgrind
3917 }
3906 free(b); 3918 free(b);
3907 } else if (h->fp) { 3919 } else if (h->fp) {
3908 pst_fwrite(b, 1, ret, h->fp); 3920 pst_fwrite(b, 1, ret, h->fp);
3909 free(b); 3921 free(b);
3910 } 3922 }
3911 //if ((*buf)[0] == 0x1) {
3912 // DEBUG_WARN(("WARNING: buffer starts with 0x1, but I didn't expect it to!\n"));
3913 //}
3914 } else { 3923 } else {
3915 // here we will assume it is a block that points to others 3924 // here we will assume it is a block that points to others
3916 DEBUG_READ(("Assuming it is a multi-block record because of it's id\n")); 3925 DEBUG_READ(("Assuming it is a multi-block record because of it's id\n"));
3917 ret = _pst_ff_compile_ID(pf, ptr->id, h, 0); 3926 ret = _pst_ff_compile_ID(pf, ptr->id, h, 0);
3918 } 3927 }
3929 u_int32_t x, b; 3938 u_int32_t x, b;
3930 unsigned char * buf3 = NULL, *buf2 = NULL, *t; 3939 unsigned char * buf3 = NULL, *buf2 = NULL, *t;
3931 unsigned char fdepth; 3940 unsigned char fdepth;
3932 3941
3933 DEBUG_ENT("_pst_ff_compile_ID"); 3942 DEBUG_ENT("_pst_ff_compile_ID");
3934 if ((a = _pst_ff_getIDblock(pf, id, &buf3))==0) { 3943 a = _pst_ff_getIDblock(pf, id, &buf3);
3944 if (!a) {
3935 if (buf3) free(buf3); 3945 if (buf3) free(buf3);
3936 return 0; 3946 return 0;
3937 } 3947 }
3938 if ((buf3[0] != 0x1)) { // if bit 8 is set) { 3948 if ((buf3[0] != 0x1)) { // if bit 8 is set) {
3939 // if ((buf3)[0] != 0x1 && (buf3)[1] > 4) { 3949 // if ((buf3)[0] != 0x1 && (buf3)[1] > 4) {
3942 if (pf->encryption) _pst_decrypt(buf3, a, pf->encryption); 3952 if (pf->encryption) _pst_decrypt(buf3, a, pf->encryption);
3943 if (h->buf) 3953 if (h->buf)
3944 *(h->buf) = buf3; 3954 *(h->buf) = buf3;
3945 else if (h->base64 == 1 && h->fp) { 3955 else if (h->base64 == 1 && h->fp) {
3946 t = base64_encode(buf3, a); 3956 t = base64_encode(buf3, a);
3947 if (t) pst_fwrite(t, 1, strlen(t), h->fp); 3957 if (t) {
3958 pst_fwrite(t, 1, strlen(t), h->fp);
3959 free(t); // caught by valgrind
3960 }
3948 free(buf3); 3961 free(buf3);
3949 } else if (h->fp) { 3962 } else if (h->fp) {
3950 pst_fwrite(buf3, 1, a, h->fp); 3963 pst_fwrite(buf3, 1, a, h->fp);
3951 free(buf3); 3964 free(buf3);
3952 } 3965 }
3986 b = z % 3; // find out how many bytes will be left over after the encoding. 3999 b = z % 3; // find out how many bytes will be left over after the encoding.
3987 // and save them 4000 // and save them
3988 memcpy(h->base64_extra_chars, &(buf2[z-b]), b); 4001 memcpy(h->base64_extra_chars, &(buf2[z-b]), b);
3989 h->base64_extra = b; 4002 h->base64_extra = b;
3990 t = base64_encode(buf2, z-b); 4003 t = base64_encode(buf2, z-b);
3991 if (t) pst_fwrite(t, 1, strlen(t), h->fp); 4004 if (t) {
3992 DEBUG_READ(("writing %i bytes to file as base64 [%i]. Currently %i\n", 4005 DEBUG_READ(("writing %i bytes to file as base64 [%i]. Currently %i\n", z, strlen(t), size));
3993 z, strlen(t), size)); 4006 pst_fwrite(t, 1, strlen(t), h->fp);
4007 free(t); // caught by valgrind
4008 }
3994 } 4009 }
3995 else if (h->fp) { 4010 else if (h->fp) {
3996 DEBUG_READ(("writing %i bytes to file. Currently %i\n", z, size)); 4011 DEBUG_READ(("writing %i bytes to file. Currently %i\n", z, size));
3997 pst_fwrite(buf2, 1, z, h->fp); 4012 pst_fwrite(buf2, 1, z, h->fp);
3998 } 4013 }