diff src/libpst.c @ 46:b2a7f2e0926a

more fixes for 64 bit format
author carl
date Sat, 12 Jan 2008 15:20:53 -0800
parents b961bcdadd0e
children 5fb8d997feed
line wrap: on
line diff
--- a/src/libpst.c	Tue Jan 08 16:33:23 2008 -0800
+++ b/src/libpst.c	Sat Jan 12 15:20:53 2008 -0800
@@ -34,62 +34,67 @@
 #define INDEX_TYPE32            0x0E
 #define INDEX_TYPE64            0x17
 
-#define FILE_SIZE_POINTER32     0xA8
-#define INDEX_POINTER32         0xC4
-#define INDEX_COUNT32           0xC0
-#define SECOND_POINTER32        0xBC
-#define SECOND_COUNT32          0xB8
-#define INDEX_TYPE_OFFSET32     0x0A
-#define ENC_OFFSET32            0x1CD
-
-#define FILE_SIZE_POINTER64     0xB8
-#define INDEX_POINTER64         0xF0
-#define INDEX_COUNT64           0xE8
-#define SECOND_POINTER64        0xE0
-#define SECOND_COUNT64          0xD8
-#define INDEX_TYPE_OFFSET64     0x0A
-#define ENC_OFFSET64            0x201
-
-#define FILE_SIZE_POINTER ((do_read64) ? FILE_SIZE_POINTER64 : FILE_SIZE_POINTER32)
-#define INDEX_POINTER     ((do_read64) ? INDEX_POINTER64     : INDEX_POINTER32)
-#define INDEX_COUNT       ((do_read64) ? INDEX_COUNT64       : INDEX_COUNT32)
-#define SECOND_POINTER    ((do_read64) ? SECOND_POINTER64    : SECOND_POINTER32)
-#define SECOND_COUNT      ((do_read64) ? SECOND_COUNT64      : SECOND_COUNT32)
-#define INDEX_TYPE_OFFSET ((do_read64) ? INDEX_TYPE_OFFSET64 : INDEX_TYPE_OFFSET32)
-#define INDEX_TYPE        ((do_read64) ? INDEX_TYPE64        : INDEX_TYPE32)
-#define ENC_OFFSET        ((do_read64) ? ENC_OFFSET64        : ENC_OFFSET32)
+#define FILE_SIZE_POINTER32     (off_t)0xA8
+#define INDEX_POINTER32         (off_t)0xC4
+#define INDEX_BACK32            (off_t)0xC0
+#define SECOND_POINTER32        (off_t)0xBC
+#define SECOND_BACK32           (off_t)0xB8
+#define INDEX_TYPE_OFFSET32     (off_t)0x0A
+#define ENC_OFFSET32            (off_t)0x1CD
+
+#define FILE_SIZE_POINTER64     (off_t)0xB8
+#define INDEX_POINTER64         (off_t)0xF0
+#define INDEX_BACK64            (off_t)0xE8
+#define SECOND_POINTER64        (off_t)0xE0
+#define SECOND_BACK64           (off_t)0xD8
+#define INDEX_TYPE_OFFSET64     (off_t)0x0A
+#define ENC_OFFSET64            (off_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 INDEX_TYPE_OFFSET ((pf->do_read64) ? INDEX_TYPE_OFFSET64 : INDEX_TYPE_OFFSET32)
+#define INDEX_TYPE        ((pf->do_read64) ? INDEX_TYPE64        : INDEX_TYPE32)
+#define ENC_OFFSET        ((pf->do_read64) ? ENC_OFFSET64        : ENC_OFFSET32)
 
 #define PST_SIGNATURE 0x4E444221
 
-int do_read64 = 0; //set this to 1 in order to try and read 64-bit pst files (Outlook 2003)
-
-struct _pst_table_ptr_struct32{
-  int32_t start;
-  int32_t u1;
-  int32_t offset;
+
+struct pst_table_ptr_struct32{
+  uint32_t start;
+  uint32_t u1;
+  uint32_t offset;
 };
-struct _pst_table_ptr_structn{
-  int64_t start;
-  int64_t u1;
-  int64_t offset;
+struct pst_table_ptr_structn{
+  uint64_t start;
+  uint64_t u1;
+  uint64_t offset;
 };
 
-typedef struct _pst_block_header {
-    int16_t type;
-    int16_t count;
+typedef struct pst_block_header {
+    uint16_t type;
+    uint16_t count;
 } pst_block_header;
 
-typedef struct _pst_id2_assoc {
+typedef struct pst_id2_assoc32 {
     uint32_t id2;
     uint32_t id;
-    int32_t  table2;
+    uint32_t table2;
+} pst_id2_assoc32;
+
+typedef struct pst_id2_assoc {
+    uint64_t id2;
+    uint64_t id;
+    uint64_t table2;
 } pst_id2_assoc;
 
 // this is an array of the un-encrypted values. the un-encrypted value is in the position
 // of the encrypted value. ie the encrypted value 0x13 represents 0x02
 //                     0     1     2     3     4     5     6     7
 //                     8     9     a     b     c     d     e     f
-unsigned char comp_enc [] =
+static unsigned char comp_enc [] =
   { 0x47, 0xf1, 0xb4, 0xe6, 0x0b, 0x6a, 0x72, 0x48,
     0x85, 0x4e, 0x9e, 0xeb, 0xe2, 0xf8, 0x94, 0x53, /*0x0f*/
     0xe0, 0xbb, 0xa0, 0x02, 0xe8, 0x5a, 0x09, 0xab,
@@ -124,26 +129,8 @@
     0xed, 0x9a, 0x64, 0x3f, 0xc1, 0x6c, 0xf9, 0xec}; /*0xff*/
 
 
-void dump_desc(off_t off, int depth, int i, pst_descn *desc_rec ) {
-    //desc_rec->d_id = 0x0102030405060708;
-    DEBUG_INDEX(("%08x [%i] Item(%#x) = [d_id = %#llx, desc_id = %#llx, "
-        "list_id = %#llx, parent_id = %#x, u1 = %#x] %#x %p %p\n",
-        off,
-        depth, i, desc_rec->d_id,
-        desc_rec->desc_id, desc_rec->list_id, desc_rec->parent_id, desc_rec->u1));
-    DEBUG_HEXDUMPC( (char*)desc_rec, sizeof( pst_descn ), 0x10 );
-    DEBUG_INDEX(("WTF? %d %x %x %x %x %x\n",
-        sizeof( uint32_t ),
-        (int)(&desc_rec->d_id) - (int)desc_rec,
-        (int)(&desc_rec->desc_id) - (int)desc_rec,
-        (int)(&desc_rec->list_id) - (int)desc_rec,
-        (int)(&desc_rec->parent_id) - (int)desc_rec,
-        (int)(&desc_rec->u1) - (int)desc_rec ));
-}
-
-
-int32_t pst_open(pst_file *pf, char *name, char *mode) {
-    uint32_t sig;
+int pst_open(pst_file *pf, char *name, char *mode) {
+    int32_t sig;
 
     unicode_init();
 
@@ -154,7 +141,7 @@
         DEBUG_RET();
         return -1;
     }
-    memset(pf, 0, sizeof(pst_file));
+    memset(pf, 0, sizeof(*pf));
 
 #ifdef _MSC_VER
     // set the default open mode for windows
@@ -167,28 +154,28 @@
     }
 
     // Check pst file magic
-    if (fread(&sig, sizeof(sig), 1, pf->fp) == 0) {
-        fclose(pf->fp);
+    if (fread(&sig, sizeof(sig), (size_t)1, pf->fp) == 0) {
+        (void)fclose(pf->fp);
         WARN(("cannot read signature from PST file. Closing on error\n"));
         DEBUG_RET();
         return -1;
     }
     LE32_CPU(sig);
     DEBUG_INFO(("sig = %X\n", sig));
-    if (sig != PST_SIGNATURE) {
-        fclose(pf->fp);
+    if (sig != (int32_t)PST_SIGNATURE) {
+        (void)fclose(pf->fp);
         WARN(("not a PST file that I know. Closing with error\n"));
         DEBUG_RET();
         return -1;
     }
 
     // read index type
-    do_read64 = 0;  // start with 32 bit format
-    _pst_getAtPos(pf->fp, INDEX_TYPE_OFFSET, &(pf->ind_type), sizeof(unsigned char));
+    pf->do_read64 = 0;  // start with 32 bit format
+    (void)pst_getAtPos(pf->fp, INDEX_TYPE_OFFSET, &(pf->ind_type), sizeof(pf->ind_type));
     DEBUG_INFO(("index_type = %i\n", pf->ind_type));
     if (pf->ind_type != INDEX_TYPE) {
         // try with 64 bit format
-        do_read64 = 1;
+        pf->do_read64 = 1;
         if (pf->ind_type != INDEX_TYPE) {
             WARN(("unknown .pst format, possibly newer than Outlook 2003 PST file?\n"));
             DEBUG_RET();
@@ -200,24 +187,24 @@
     }
 
     // read encryption setting
-    _pst_getAtPos(pf->fp, ENC_OFFSET, &(pf->encryption), sizeof(unsigned char));
+    (void)pst_getAtPos(pf->fp, ENC_OFFSET, &(pf->encryption), sizeof(pf->encryption));
     DEBUG_INFO(("encrypt = %i\n", pf->encryption));
 
-    pf->index2_count = _pst_getIntAtPos(pf->fp, SECOND_COUNT);
-    pf->index2       = _pst_getIntAtPos(pf->fp, SECOND_POINTER );
-    pf->size         = _pst_getIntAtPos( pf->fp, FILE_SIZE_POINTER );
-    DEBUG_INFO(("Pointer2 is %#X, count %i[%#x]\n", pf->index2, pf->index2_count, pf->index2_count));
-
-    pf->index1_count = _pst_getIntAtPos(pf->fp, INDEX_COUNT);
-    pf->index1       = _pst_getIntAtPos(pf->fp, INDEX_POINTER);
-    DEBUG_INFO(("Pointer1 is %#X, count %i[%#x]\n", pf->index1, pf->index1_count, pf->index1_count));
+    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 %#llx, count %lli[%#llx]\n", pf->index2, pf->index2_back, pf->index2_back));
+
+    pf->index1_back  = pst_getIntAtPos(pf, INDEX_BACK);
+    pf->index1       = pst_getIntAtPos(pf, INDEX_POINTER);
+    DEBUG_INFO(("Pointer1 is %#llx, count %lli[%#llx]\n", pf->index1, pf->index1_back, pf->index1_back));
 
     DEBUG_RET();
     return 0;
 }
 
 
-int32_t pst_close(pst_file *pf) {
+int pst_close(pst_file *pf) {
     DEBUG_ENT("pst_close");
     if (!pf->fp) {
         WARN(("cannot close NULL fp\n"));
@@ -230,9 +217,9 @@
         return -1;
     }
     // 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);
+    pst_free_id (pf->i_head);
+    pst_free_desc (pf->d_head);
+    pst_free_xattrib (pf->x_head);
     DEBUG_RET();
     return 0;
 }
@@ -247,24 +234,24 @@
     } else 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
-        ret = _pst_getDptr(pf, 0x2142);
+        ret = pst_getDptr(pf, (uint64_t)0x2142);
     } else {
-        ret = _pst_getDptr(pf, root->message_store->top_of_personal_folder->id);
+        ret = pst_getDptr(pf, root->message_store->top_of_personal_folder->id);
     }
     DEBUG_RET();
     return ret;
 }
 
 
-int32_t pst_attach_to_mem(pst_file *pf, pst_item_attach *attach, unsigned char **b){
-    int32_t size=0;
+size_t pst_attach_to_mem(pst_file *pf, pst_item_attach *attach, unsigned char **b){
+    size_t size=0;
     pst_index_ll *ptr;
     struct holder h = {b, NULL, 0, "", 0};
     DEBUG_ENT("pst_attach_to_mem");
-    if (attach->id_val != -1) {
-        ptr = _pst_getID(pf, attach->id_val);
+    if (attach->id_val != (uint64_t)-1) {
+        ptr = pst_getID(pf, attach->id_val);
         if (ptr) {
-            size = _pst_ff_getID2data(pf, ptr, &h);
+            size = pst_ff_getID2data(pf, ptr, &h);
         } else {
             DEBUG_WARN(("Couldn't find ID pointer. Cannot handle attachment\n"));
             size = 0;
@@ -278,15 +265,15 @@
 }
 
 
-int32_t pst_attach_to_file(pst_file *pf, pst_item_attach *attach, FILE* fp) {
+size_t pst_attach_to_file(pst_file *pf, pst_item_attach *attach, FILE* fp) {
     pst_index_ll *ptr;
     struct holder h = {NULL, fp, 0, "", 0};
-    int32_t size;
+    size_t size;
     DEBUG_ENT("pst_attach_to_file");
-    if (attach->id_val != -1) {
-        ptr = _pst_getID(pf, attach->id_val);
+    if (attach->id_val != (uint64_t)-1) {
+        ptr = pst_getID(pf, attach->id_val);
         if (ptr) {
-            size = _pst_ff_getID2data(pf, ptr, &h);
+            size = pst_ff_getID2data(pf, ptr, &h);
         } else {
             DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n"));
             size = 0;
@@ -295,27 +282,27 @@
     } else {
         // save the attachment to file
         size = attach->size;
-        pst_fwrite(attach->data, 1, size, fp);
+        (void)pst_fwrite(attach->data, (size_t)1, size, fp);
     }
     DEBUG_RET();
-    return 1;
+    return size;
 }
 
 
-int32_t pst_attach_to_file_base64(pst_file *pf, pst_item_attach *attach, FILE* fp) {
+size_t pst_attach_to_file_base64(pst_file *pf, pst_item_attach *attach, FILE* fp) {
     pst_index_ll *ptr;
     struct holder h = {NULL, fp, 1, "", 0};
-    int32_t size;
+    size_t size;
     char *c;
     DEBUG_ENT("pst_attach_to_file_base64");
-    if (attach->id_val != -1) {
-        ptr = _pst_getID(pf, attach->id_val);
+    if (attach->id_val != (uint64_t)-1) {
+        ptr = pst_getID(pf, attach->id_val);
         if (ptr) {
-            size = _pst_ff_getID2data(pf, ptr, &h);
+            size = pst_ff_getID2data(pf, ptr, &h);
             // will need to encode any bytes left over
-            c = base64_encode(h.base64_extra_chars, h.base64_extra);
+            c = base64_encode(h.base64_extra_chars, (size_t)h.base64_extra);
             if (c) {
-                pst_fwrite(c, 1, strlen(c), fp);
+                (void)pst_fwrite(c, (size_t)1, strlen(c), fp);
                 free(c);    // caught by valgrind
             }
         } else {
@@ -327,18 +314,18 @@
         // encode the attachment to the file
         c = base64_encode(attach->data, attach->size);
         if (c) {
-            pst_fwrite(c, 1, strlen(c), fp);
+            (void)pst_fwrite(c, (size_t)1, strlen(c), fp);
             free(c);    // caught by valgrind
         }
         size = attach->size;
     }
     DEBUG_RET();
-    return 1;
+    return size;
 }
 
 
-int32_t pst_load_index (pst_file *pf) {
-    int32_t  x;
+int pst_load_index (pst_file *pf) {
+    int  x;
     uint64_t y;
     DEBUG_ENT("pst_load_index");
     if (!pf) {
@@ -347,14 +334,14 @@
         return -1;
     }
 
-    x = _pst_build_id_ptr(pf, pf->index1, 0, pf->index1_count, 0, UINT64_MAX);
+    x = pst_build_id_ptr(pf, pf->index1, 0, pf->index1_back, 0, UINT64_MAX);
     DEBUG_INDEX(("build id ptr returns %i\n", x));
 
     y = 0;
-    x = _pst_build_desc_ptr(pf, pf->index2, 0, pf->index2_count, &y, 0x21, UINT64_MAX);
+    x = pst_build_desc_ptr(pf, pf->index2, 0, pf->index2_back, &y, (uint64_t)0x21, UINT64_MAX);
     DEBUG_INDEX(("build desc ptr returns %i\n", x));
 
-    DEBUG_CODE(_pst_printDptr(pf););
+    DEBUG_CODE((void)pst_printDptr(pf););
     DEBUG_RET();
     return 0;
 }
@@ -374,7 +361,7 @@
 }
 
 
-typedef struct _pst_x_attrib {
+typedef struct pst_x_attrib {
     uint16_t extended;
     uint16_t zero;
     uint16_t type;
@@ -382,18 +369,19 @@
 } pst_x_attrib;
 
 
-int32_t pst_load_extended_attributes(pst_file *pf) {
+int pst_load_extended_attributes(pst_file *pf) {
     // for PST files this will load up ID2 0x61 and check it's "list" attribute.
     pst_desc_ll *p;
     pst_num_array *na;
-    pst_index2_ll *list2 = NULL;
-    unsigned char * buffer=NULL, *headerbuffer=NULL;//, *tc;
+    pst_index2_ll *id2_head = NULL;
+    unsigned char *buffer=NULL, *headerbuffer=NULL;
+    size_t bsize=0, hsize=0, bptr=0;
     pst_x_attrib xattrib;
-    int32_t bptr = 0, bsize, hsize, tint, err=0, x;
+    int32_t tint, err=0, x;
     pst_x_attrib_ll *ptr, *p_head=NULL, *p_sh=NULL, *p_sh2=NULL;
 
     DEBUG_ENT("pst_loadExtendedAttributes");
-    if ((p = _pst_getDptr(pf, 0x61)) == NULL) {
+    if ((p = pst_getDptr(pf, (uint64_t)0x61)) == NULL) {
         DEBUG_WARN(("Cannot find DescID 0x61 for loading the Extended Attributes\n"));
         DEBUG_RET();
         return 0;
@@ -406,34 +394,36 @@
     }
 
     if (p->list_index) {
-        list2 = _pst_build_id2(pf, p->list_index, NULL);
-        _pst_printID2ptr(list2);
+        id2_head = pst_build_id2(pf, p->list_index, NULL);
+        (void)pst_printID2ptr(id2_head);
     } else {
         DEBUG_WARN(("Have not been able to fetch any id2 values for item 0x61. Brace yourself!\n"));
     }
 
-    na = _pst_parse_block(pf, p->desc->id, list2);
+    na = pst_parse_block(pf, p->desc->id, id2_head);
     if (!na) {
         DEBUG_WARN(("Cannot process desc block for item 0x61. Not loading extended Attributes\n"));
-        if (list2) _pst_free_id2(list2);
+        if (id2_head) pst_free_id2(id2_head);
         DEBUG_RET();
         return 0;
     }
 
     x = 0;
     while (x < na->count_item) {
-        if (na->items[x]->id == 0x0003) {
+        if (na->items[x]->id == (uint32_t)0x0003) {
             buffer = na->items[x]->data;
             bsize = na->items[x]->size;
-        } else if (na->items[x]->id == 0x0004) {
+        } else if (na->items[x]->id == (uint32_t)0x0004) {
             headerbuffer = na->items[x]->data;
             hsize = na->items[x]->size;
+        } else {
+            // leave them null
         }
         x++;
     }
 
     if (!buffer) {
-        if (na) _pst_free_list(na);
+        if (na) pst_free_list(na);
         DEBUG_WARN(("No extended attributes buffer found. Not processing\n"));
         DEBUG_RET();
         return 0;
@@ -447,10 +437,10 @@
     bptr += sizeof(xattrib);
 
     while (xattrib.type != 0 && bptr < bsize) {
-        ptr = (pst_x_attrib_ll*) xmalloc(sizeof(pst_x_attrib_ll));
-        memset(ptr, 0, sizeof(pst_x_attrib_ll));
+        ptr = (pst_x_attrib_ll*) xmalloc(sizeof(*ptr));
+        memset(ptr, 0, sizeof(*ptr));
         ptr->type = xattrib.type;
-        ptr->map = xattrib.map+0x8000;
+        ptr->map  = xattrib.map+0x8000;
         ptr->next = NULL;
         DEBUG_INDEX(("xattrib: ext = %#hx, zero = %#hx, type = %#hx, map = %#hx\n",
              xattrib.extended, xattrib.zero, xattrib.type, xattrib.map));
@@ -462,10 +452,10 @@
                 // copy the size of the header. It is 32 bit int
                 memcpy(&tint, &(headerbuffer[xattrib.extended]), sizeof(tint));
                 LE32_CPU(tint);
-                wt = (char*) xmalloc(tint+2); // plus 2 for a uni-code zero
-                memset(wt, 0, tint+2);
-                memcpy(wt, &(headerbuffer[xattrib.extended+sizeof(tint)]), tint);
-                ptr->data = _pst_wide_to_single(wt, tint);
+                wt = (char*) xmalloc((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, tint);
                 free(wt);
                 DEBUG_INDEX(("Read string (converted from UTF-16): %s\n", ptr->data));
             } else {
@@ -474,9 +464,9 @@
             ptr->mytype = PST_MAP_HEADER;
         } else {
             // contains the attribute code to map to.
-            ptr->data = (int*)xmalloc(sizeof(int32_t));
-            memset(ptr->data, 0, sizeof(int32_t));
-            *((int32_t*)ptr->data) = xattrib.extended;
+            ptr->data = (uint32_t*)xmalloc(sizeof(uint32_t));
+            memset(ptr->data, 0, sizeof(uint32_t));
+            *((uint32_t*)ptr->data) = xattrib.extended;
             ptr->mytype = PST_MAP_ATTRIB;
             DEBUG_INDEX(("Mapped attribute %#x to %#x\n", ptr->map, *((int32_t*)ptr->data)));
         }
@@ -509,8 +499,8 @@
         LE16_CPU(xattrib.map);
         bptr += sizeof(xattrib);
     }
-    if (list2) _pst_free_id2(list2);
-    if (na)    _pst_free_list(na);
+    if (id2_head) pst_free_id2(id2_head);
+    if (na)       pst_free_list(na);
     pf->x_head = p_head;
     DEBUG_RET();
     return 1;
@@ -536,20 +526,21 @@
 #define INDEX_COUNT_MAX64          20       // max active items
 #define DESC_COUNT_MAX64           15       // max active items
 
-#define BLOCK_SIZE               ((do_read64) ? BLOCK_SIZE64             : BLOCK_SIZE32)
-#define DESC_BLOCK_SIZE          ((do_read64) ? DESC_BLOCK_SIZE64        : DESC_BLOCK_SIZE32)
-#define ITEM_COUNT_OFFSET        ((do_read64) ? ITEM_COUNT_OFFSET64      : ITEM_COUNT_OFFSET32)
-#define LEVEL_INDICATOR_OFFSET   ((do_read64) ? LEVEL_INDICATOR_OFFSET64 : LEVEL_INDICATOR_OFFSET32)
-#define BACKLINK_OFFSET          ((do_read64) ? BACKLINK_OFFSET64        : BACKLINK_OFFSET32)
-#define ITEM_SIZE                ((do_read64) ? ITEM_SIZE64              : ITEM_SIZE32)
-#define DESC_SIZE                ((do_read64) ? DESC_SIZE64              : DESC_SIZE32)
-#define INDEX_COUNT_MAX          ((do_read64) ? INDEX_COUNT_MAX64        : INDEX_COUNT_MAX32)
-#define DESC_COUNT_MAX           ((do_read64) ? DESC_COUNT_MAX64         : DESC_COUNT_MAX32)
-
-
-int _pst_decode_desc( pst_descn *desc, char *buf ) {
-    int r;
-    if (do_read64) {
+#define BLOCK_SIZE               (size_t)((pf->do_read64) ? BLOCK_SIZE64             : BLOCK_SIZE32)
+#define DESC_BLOCK_SIZE          (size_t)((pf->do_read64) ? DESC_BLOCK_SIZE64        : DESC_BLOCK_SIZE32)
+#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_descn *desc, char *buf);
+static size_t pst_decode_desc(pst_file *pf, pst_descn *desc, char *buf) {
+    size_t r;
+    if (pf->do_read64) {
         DEBUG_INDEX(("Decoding desc64\n"));
         DEBUG_HEXDUMPC(buf, sizeof(pst_descn), 0x10);
         memcpy(desc, buf, sizeof(pst_descn));
@@ -580,37 +571,39 @@
 }
 
 
-int _pst_decode_table( struct _pst_table_ptr_structn *table, char *buf ) {
-    int r;
-    if (do_read64) {
+static size_t pst_decode_table(pst_file *pf, struct pst_table_ptr_structn *table, char *buf);
+static size_t pst_decode_table(pst_file *pf, struct pst_table_ptr_structn *table, char *buf) {
+    size_t r;
+    if (pf->do_read64) {
         DEBUG_INDEX(("Decoding table64\n"));
-        DEBUG_HEXDUMPC(buf, sizeof(struct _pst_table_ptr_structn), 0x10);
-        memcpy(table, buf, sizeof(struct _pst_table_ptr_structn));
+        DEBUG_HEXDUMPC(buf, sizeof(struct pst_table_ptr_structn), 0x10);
+        memcpy(table, buf, sizeof(struct pst_table_ptr_structn));
         LE64_CPU(table->start);
         LE64_CPU(table->u1);
         LE64_CPU(table->offset);
-        r =sizeof(struct _pst_table_ptr_structn);
+        r =sizeof(struct pst_table_ptr_structn);
     }
     else {
-        struct _pst_table_ptr_struct32 t32;
+        struct pst_table_ptr_struct32 t32;
         DEBUG_INDEX(("Decoding table32\n"));
-        DEBUG_HEXDUMPC(buf, sizeof( struct _pst_table_ptr_struct32), 0x10);
-        memcpy(&t32, buf, sizeof(struct _pst_table_ptr_struct32));
+        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);
+        r = sizeof(struct pst_table_ptr_struct32);
     }
     return r;
 }
 
 
-int _pst_decode_index( pst_index *index, char *buf ) {
-    int r;
-    if (do_read64) {
+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_INDEX(("Decoding index64\n"));
         DEBUG_HEXDUMPC(buf, sizeof(pst_index), 0x10);
         memcpy(index, buf, sizeof(pst_index));
@@ -625,10 +618,10 @@
         DEBUG_INDEX(("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);
+        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;
@@ -639,15 +632,43 @@
 }
 
 
-int32_t _pst_build_id_ptr(pst_file *pf, off_t offset, int32_t depth, int64_t linku1, uint64_t start_val, uint64_t end_val) {
-    struct _pst_table_ptr_structn table, table2;
+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_INDEX(("Decoding assoc64\n"));
+        DEBUG_HEXDUMPC(buf, sizeof(pst_id2_assoc), 0x10);
+        memcpy(assoc, buf, sizeof(pst_id2_assoc));
+        LE64_CPU(assoc->id2);
+        LE64_CPU(assoc->id);
+        LE64_CPU(assoc->table2);
+        r = sizeof(pst_id2_assoc);
+    } else {
+        pst_id2_assoc32 assoc32;
+        DEBUG_INDEX(("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.table2);
+        assoc->id2    = assoc32.id2;
+        assoc->id     = assoc32.id;
+        assoc->table2 = assoc32.table2;
+        r = sizeof(pst_id2_assoc32);
+    }
+    return r;
+}
+
+
+int pst_build_id_ptr(pst_file *pf, off_t offset, int32_t depth, uint64_t linku1, uint64_t start_val, uint64_t end_val) {
+    struct pst_table_ptr_structn 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_ENT("pst_build_id_ptr");
     DEBUG_INDEX(("offset %x depth %i linku1 %llx start %llx end %llx\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:%#llx, end:%#llx]\n", start_val, end_val));
@@ -655,7 +676,7 @@
         return -1;
     }
     DEBUG_INDEX(("Reading index block\n"));
-    if (_pst_read_block_size(pf, offset, BLOCK_SIZE, &buf, 0, 0) < BLOCK_SIZE) {
+    if (pst_read_block_size(pf, offset, BLOCK_SIZE, &buf, 0, 0) < BLOCK_SIZE) {
         DEBUG_WARN(("Failed to read %i bytes\n", BLOCK_SIZE));
         if (buf) free(buf);
         DEBUG_RET();
@@ -663,14 +684,14 @@
     }
     bptr = buf;
     DEBUG_HEXDUMPC(buf, BLOCK_SIZE, ITEM_SIZE32);
-    item_count = (int)(unsigned)(buf[ITEM_COUNT_OFFSET]);
+    item_count = (int32_t)(unsigned)(buf[ITEM_COUNT_OFFSET]);
     if (item_count > INDEX_COUNT_MAX) {
         DEBUG_WARN(("Item count %i too large, max is %i\n", item_count, INDEX_COUNT_MAX));
         if (buf) free(buf);
         DEBUG_RET();
         return -1;
     }
-    index.id = _getIntAt(buf+BACKLINK_OFFSET);
+    index.id = pst_getIntAt(pf, buf+BACKLINK_OFFSET);
     if (index.id != linku1) {
         DEBUG_WARN(("Backlink %#llx in this node does not match required %#llx\n", index.id, linku1));
         if (buf) free(buf);
@@ -682,10 +703,10 @@
         // this node contains leaf pointers
         x = 0;
         while (x < item_count) {
-            bptr += _pst_decode_index(&index, bptr);
+            bptr += pst_decode_index(pf, &index, bptr);
             x++;
             if (index.id == 0) break;
-            DEBUG_INDEX(("[%i]%i Item [id = %#x, offset = %#x, u1 = %#x, size = %i(%#x)]\n",
+            DEBUG_INDEX(("[%i]%i Item [id = %#llx, offset = %#llx, u1 = %#x, size = %i(%#x)]\n",
                         depth, x, index.id, index.offset, index.u1, index.size, index.size));
             // if (index.id & 0x02) DEBUG_INDEX(("two-bit set!!\n"));
             if ((index.id >= end_val) || (index.id < old)) {
@@ -695,7 +716,7 @@
                 return -1;
             }
             old = index.id;
-            if (x == 1) {   // first entry
+            if (x == (int32_t)1) {   // first entry
                 if ((start_val) && (index.id != start_val)) {
                     DEBUG_WARN(("This item isn't right. Must be corruption, or I got it wrong!\n"));
                     if (buf) free(buf);
@@ -717,16 +738,16 @@
         // this node contains node pointers
         x = 0;
         while (x < item_count) {
-            bptr += _pst_decode_table(&table, bptr);
+            bptr += pst_decode_table(pf, &table, bptr);
             x++;
             if (table.start == 0) break;
             if (x < item_count) {
-                _pst_decode_table(&table2, bptr);
+                (void)pst_decode_table(pf, &table2, bptr);
             }
             else {
                 table2.start = end_val;
             }
-            DEBUG_INDEX(("[%i] %i Index Table [start id = %#x, u1 = %#x, offset = %#x, end id = %#x]\n",
+            DEBUG_INDEX(("[%i] %i Index Table [start id = %#llx, u1 = %#llx, offset = %#llx, end id = %#llx]\n",
                         depth, x, table.start, table.u1, table.offset, table2.start));
             if ((table.start >= end_val) || (table.start < old)) {
                 DEBUG_WARN(("This table isn't right. Must be corruption, or I got it wrong!\n"));
@@ -735,7 +756,7 @@
                 return -1;
             }
             old = table.start;
-            if (x == 1) {   // first entry
+            if (x == (int32_t)1) {  // first entry
                 if ((start_val) && (table.start != start_val)) {
                     DEBUG_WARN(("This table isn't right. Must be corruption, or I got it wrong!\n"));
                     if (buf) free(buf);
@@ -743,7 +764,7 @@
                     return -1;
                 }
             }
-            _pst_build_id_ptr(pf, table.offset, depth+1, table.u1, table.start, table2.start);
+            (void)pst_build_id_ptr(pf, table.offset, depth+1, table.u1, table.start, table2.start);
         }
     }
     if (buf) free (buf);
@@ -765,16 +786,17 @@
     struct cache_list_node *next;
     struct cache_list_node *prev;
 };
-struct cache_list_node *cache_head;
-struct cache_list_node *cache_tail;
-struct cache_list_node *lostfound_head;
-int32_t cache_count;
+static struct cache_list_node *cache_head;
+static struct cache_list_node *cache_tail;
+static struct cache_list_node *lostfound_head;
+static int cache_count;
 
 
 /**
     add the d_ptr descriptor into the global tree
 */
-void record_descriptor(pst_file *pf, pst_desc_ll *d_ptr, uint64_t parent_id) {
+static void record_descriptor(pst_file *pf, pst_desc_ll *d_ptr, uint64_t parent_id);
+static void record_descriptor(pst_file *pf, pst_desc_ll *d_ptr, uint64_t parent_id) {
     struct cache_list_node *lostfound_ptr = NULL;
     struct cache_list_node *cache_ptr     = NULL;
     pst_desc_ll            *parent        = NULL;
@@ -797,14 +819,14 @@
         while (cache_ptr && (cache_ptr->ptr->id != parent_id)) {
             cache_ptr = cache_ptr->next;
         }
-        if (!cache_ptr && (parent = _pst_getDptr(pf, parent_id)) == NULL) {
+        if (!cache_ptr && (parent = pst_getDptr(pf, parent_id)) == NULL) {
             // check in the lost/found list
             lostfound_ptr = lostfound_head;
             while (lostfound_ptr && (lostfound_ptr->ptr->id != parent_id)) {
                 lostfound_ptr = lostfound_ptr->next;
             }
             if (!lostfound_ptr) {
-                DEBUG_WARN(("ERROR -- cannot find parent with id %#x. Adding to lost/found\n", parent_id));
+                DEBUG_WARN(("ERROR -- cannot find parent with id %#llx. Adding to lost/found\n", parent_id));
                 lostfound_ptr = (struct cache_list_node*) xmalloc(sizeof(struct cache_list_node));
                 lostfound_ptr->prev   = NULL;
                 lostfound_ptr->next   = lostfound_head;
@@ -813,7 +835,7 @@
                 lostfound_head = lostfound_ptr;
             } else {
                 parent = lostfound_ptr->ptr;
-                DEBUG_INDEX(("Found parent (%#x) in Lost and Found\n", parent->id));
+                DEBUG_INDEX(("Found parent (%#llx) in Lost and Found\n", parent->id));
             }
         }
 
@@ -851,8 +873,8 @@
     }
 }
 
-int32_t _pst_build_desc_ptr (pst_file *pf, off_t offset, int32_t depth, int64_t linku1, uint64_t *high_id, uint64_t start_val, uint64_t end_val) {
-    struct _pst_table_ptr_structn table, table2;
+int pst_build_desc_ptr (pst_file *pf, off_t offset, int32_t depth, uint64_t linku1, uint64_t *high_id, uint64_t start_val, uint64_t end_val) {
+    struct pst_table_ptr_structn table, table2;
     pst_descn desc_rec;
     pst_desc_ll *d_ptr=NULL, *parent=NULL;
     int32_t x, item_count;
@@ -871,26 +893,26 @@
         cache_count    = 0;
     }
 
-    DEBUG_ENT("_pst_build_desc_ptr");
-    DEBUG_INDEX(("offset %x depth %i linku1 %x start %x end %x\n", offset, depth, linku1, start_val, end_val));
+    DEBUG_ENT("pst_build_desc_ptr");
+    DEBUG_INDEX(("offset %llx depth %i linku1 %llx start %llx end %llx\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:%#x, end:%#x]\n", start_val, end_val));
+        DEBUG_WARN(("The end value is BEFORE the start value. This function will quit. Soz. [start:%#llx, end:%#llx]\n", start_val, end_val));
         DEBUG_RET();
         return -1;
     }
     DEBUG_INDEX(("Reading desc block\n"));
-    if (_pst_read_block_size(pf, offset, DESC_BLOCK_SIZE, &buf, 0, 0) < DESC_BLOCK_SIZE) {
+    if (pst_read_block_size(pf, offset, DESC_BLOCK_SIZE, &buf, 0, 0) < DESC_BLOCK_SIZE) {
         DEBUG_WARN(("Failed to read %i bytes\n", DESC_BLOCK_SIZE));
         if (buf) free(buf);
         DEBUG_RET();
         return -1;
     }
     bptr = buf;
-    item_count = (int)(unsigned)(buf[ITEM_COUNT_OFFSET]);
-
-    desc_rec.d_id = _getIntAt(buf+BACKLINK_OFFSET);
+    item_count = (int32_t)(unsigned)(buf[ITEM_COUNT_OFFSET]);
+
+    desc_rec.d_id = pst_getIntAt(pf, buf+BACKLINK_OFFSET);
     if (desc_rec.d_id != linku1) {
-        DEBUG_WARN(("Backlink %#x in this node does not match required %#x\n", desc_rec.d_id, linku1));
+        DEBUG_WARN(("Backlink %#llx in this node does not match required %#llx\n", desc_rec.d_id, linku1));
         if (buf) free(buf);
         DEBUG_RET();
         return -1;
@@ -906,10 +928,10 @@
         }
         x = 0;
         while (x < item_count) {
-            bptr += _pst_decode_desc(&desc_rec, bptr);
+            bptr += pst_decode_desc(pf, &desc_rec, bptr);
             x++;
             if (desc_rec.d_id == 0) break;
-            DEBUG_INDEX(("[%i] Item(%#x) = [d_id = %#x, desc_id = %#x, list_id = %#x, parent_id = %#x]\n",
+            DEBUG_INDEX(("[%i] Item(%#x) = [d_id = %#llx, desc_id = %#llx, list_id = %#llx, parent_id = %#x]\n",
                         depth, x, desc_rec.d_id, desc_rec.desc_id, desc_rec.list_id, desc_rec.parent_id));
             if ((desc_rec.d_id >= end_val) || (desc_rec.d_id < old)) {
                 DEBUG_WARN(("This item isn't right. Must be corruption, or I got it wrong!\n"));
@@ -919,7 +941,7 @@
                 return -1;
             }
             old = desc_rec.d_id;
-            if (x == 1) {   // first entry
+            if (x == (int32_t)1) {   // first entry
                 if (start_val && (desc_rec.d_id != start_val)) {
                     DEBUG_WARN(("This item isn't right. Must be corruption, or I got it wrong!\n"));
                     if (buf) free(buf);
@@ -929,13 +951,13 @@
             }
             // When duplicates found, just update the info.... perhaps this is correct functionality
             DEBUG_INDEX(("Searching for existing record\n"));
-            if (desc_rec.d_id <= *high_id && (d_ptr = _pst_getDptr(pf, desc_rec.d_id))) {
+            if (desc_rec.d_id <= *high_id && (d_ptr = pst_getDptr(pf, desc_rec.d_id))) {
                 DEBUG_INDEX(("Updating Existing Values\n"));
-                d_ptr->list_index = _pst_getID(pf, desc_rec.list_id);
-                d_ptr->desc = _pst_getID(pf, desc_rec.desc_id);
-                DEBUG_INDEX(("\tdesc = %#x\tlist_index=%#x\n",
-                        (d_ptr->desc==NULL?0:d_ptr->desc->id),
-                        (d_ptr->list_index==NULL?0:d_ptr->list_index->id)));
+                d_ptr->list_index = pst_getID(pf, desc_rec.list_id);
+                d_ptr->desc = pst_getID(pf, desc_rec.desc_id);
+                DEBUG_INDEX(("\tdesc = %#llx\tlist_index=%#llx\n",
+                        (d_ptr->desc==NULL?0LL:d_ptr->desc->id),
+                        (d_ptr->list_index==NULL?0LL:d_ptr->list_index->id)));
                 if (d_ptr->parent && desc_rec.parent_id != d_ptr->parent->id) {
                     DEBUG_INDEX(("WARNING -- Parent of record has changed. Moving it\n"));
                     //hmmm, we must move the record.
@@ -973,8 +995,8 @@
                 DEBUG_INDEX(("New Record\n"));
                 d_ptr             = (pst_desc_ll*) xmalloc(sizeof(pst_desc_ll));
                 d_ptr->id         = desc_rec.d_id;
-                d_ptr->list_index = _pst_getID(pf, desc_rec.list_id);
-                d_ptr->desc       = _pst_getID(pf, desc_rec.desc_id);
+                d_ptr->list_index = pst_getID(pf, desc_rec.list_id);
+                d_ptr->desc       = pst_getID(pf, desc_rec.desc_id);
                 d_ptr->prev       = NULL;
                 d_ptr->next       = NULL;
                 d_ptr->parent     = NULL;
@@ -989,7 +1011,7 @@
             lostfound_shd = NULL;
             while (lostfound_ptr) {
                 if (lostfound_ptr->parent == d_ptr->id) {
-                    DEBUG_INDEX(("Found a child  (%#x) of the current record. Joining to main structure.\n", lostfound_ptr->ptr->id));
+                    DEBUG_INDEX(("Found a child  (%#llx) of the current record. Joining to main structure.\n", lostfound_ptr->ptr->id));
                     parent = d_ptr;
                     d_ptr = lostfound_ptr->ptr;
                     parent->no_child++;
@@ -1020,16 +1042,16 @@
         }
         x = 0;
         while (x < item_count) {
-            bptr += _pst_decode_table(&table, bptr);
+            bptr += pst_decode_table(pf, &table, bptr);
             x++;
             if (table.start == 0) break;
             if (x < item_count) {
-                _pst_decode_table(&table2, bptr);
+                (void)pst_decode_table(pf, &table2, bptr);
             }
             else {
                 table2.start = end_val;
             }
-            DEBUG_INDEX(("[%i] %i Descriptor Table [start id = %#x, u1 = %#x, offset = %#x, end id = %#x]\n",
+            DEBUG_INDEX(("[%i] %i Descriptor Table [start id = %#llx, u1 = %#llx, offset = %#llx, end id = %#llx]\n",
                         depth, x, table.start, table.u1, table.offset, table2.start));
             if ((table.start >= end_val) || (table.start < old)) {
                 DEBUG_WARN(("This table isn't right. Must be corruption, or I got it wrong!\n"));
@@ -1038,15 +1060,15 @@
                 return -1;
             }
             old = table.start;
-            if (x == 1) {   // first entry
-                if (start_val != -1 && table.start != start_val) {
+            if (x == (int32_t)1) {   // first entry
+                if (start_val && (table.start != start_val)) {
                     DEBUG_WARN(("This table isn't right. Must be corruption, or I got it wrong!\n"));
                     if (buf) free(buf);
                     DEBUG_RET();
                     return -1;
                 }
             }
-            _pst_build_desc_ptr(pf, table.offset, depth+1, table.u1, high_id, table.start, table2.start);
+            (void)pst_build_desc_ptr(pf, table.offset, depth+1, table.u1, high_id, table.start, table2.start);
         }
     }
     if (depth == 0) {
@@ -1059,7 +1081,7 @@
         // free the lost and found
         while (lostfound_head) {
             lostfound_ptr = lostfound_head->next;
-            WARN(("unused lost/found item with parent %d))", lostfound_head->parent));
+            WARN(("unused lost/found item with parent %lld))", lostfound_head->parent));
             free(lostfound_head);
             lostfound_head = lostfound_ptr;
         }
@@ -1070,14 +1092,14 @@
 }
 
 
-pst_item* _pst_parse_item(pst_file *pf, pst_desc_ll *d_ptr) {
+pst_item* pst_parse_item(pst_file *pf, pst_desc_ll *d_ptr) {
     pst_num_array * list;
     pst_index2_ll *id2_head = NULL;
     pst_index_ll *id_ptr = NULL;
     pst_item *item = NULL;
     pst_item_attach *attach = NULL;
-    int x;
-    DEBUG_ENT("_pst_parse_item");
+    int32_t x;
+    DEBUG_ENT("pst_parse_item");
     if (!d_ptr) {
         DEBUG_WARN(("you cannot pass me a NULL! I don't want it!\n"));
         DEBUG_RET();
@@ -1091,16 +1113,16 @@
     }
 
     if (d_ptr->list_index) {
-        id2_head = _pst_build_id2(pf, d_ptr->list_index, NULL);
-        _pst_printID2ptr(id2_head);
+        id2_head = pst_build_id2(pf, d_ptr->list_index, NULL);
+        (void)pst_printID2ptr(id2_head);
     } else {
         DEBUG_WARN(("Have not been able to fetch any id2 values for this item. Brace yourself!\n"));
     }
 
-    list = _pst_parse_block(pf, d_ptr->desc->id, id2_head);
+    list = pst_parse_block(pf, d_ptr->desc->id, id2_head);
     if (!list) {
-        DEBUG_WARN(("_pst_parse_block() returned an error for d_ptr->desc->id [%#x]\n", d_ptr->desc->id));
-        if (id2_head) _pst_free_id2(id2_head);
+        DEBUG_WARN(("pst_parse_block() returned an error for d_ptr->desc->id [%#llx]\n", d_ptr->desc->id));
+        if (id2_head) pst_free_id2(id2_head);
         DEBUG_RET();
         return NULL;
     }
@@ -1108,18 +1130,18 @@
     item = (pst_item*) xmalloc(sizeof(pst_item));
     memset(item, 0, sizeof(pst_item));
 
-    if (_pst_process(list, item, NULL)) {
-        DEBUG_WARN(("_pst_process() returned non-zero value. That is an error\n"));
-        if (item)     _pst_freeItem(item);
-        if (list)     _pst_free_list(list);
-        if (id2_head) _pst_free_id2(id2_head);
+    if (pst_process(list, item, NULL)) {
+        DEBUG_WARN(("pst_process() returned non-zero value. That is an error\n"));
+        if (item)     pst_freeItem(item);
+        if (list)     pst_free_list(list);
+        if (id2_head) pst_free_id2(id2_head);
         DEBUG_RET();
         return NULL;
     }
-    if (list) _pst_free_list(list);
-    list = NULL; //_pst_process will free the items in the list
-
-    if ((id_ptr = _pst_getID2(id2_head, 0x671))) {
+    if (list) pst_free_list(list);
+    list = NULL; //pst_process will free the items in the list
+
+    if ((id_ptr = pst_getID2(id2_head, (uint64_t)0x671))) {
         // attachments exist - so we will process them
         while (item->attach) {
             attach = item->attach->next;
@@ -1128,10 +1150,10 @@
         }
 
         DEBUG_EMAIL(("ATTACHMENT processing attachment\n"));
-        if ((list = _pst_parse_block(pf, id_ptr->id, id2_head)) == NULL) {
+        if ((list = pst_parse_block(pf, id_ptr->id, id2_head)) == NULL) {
             DEBUG_WARN(("ERROR error processing main attachment record\n"));
-            if (item) _pst_freeItem(item);
-            if (id2_head) _pst_free_id2(id2_head);
+            if (item) pst_freeItem(item);
+            if (id2_head) pst_free_id2(id2_head);
             DEBUG_RET();
             return NULL;
         }
@@ -1145,15 +1167,15 @@
                 x++;
             }
 
-            if (_pst_process(list, item, item->attach)) {
-                DEBUG_WARN(("ERROR _pst_process() failed with attachments\n"));
-                if (item)     _pst_freeItem(item);
-                if (list)     _pst_free_list(list);
-                if (id2_head) _pst_free_id2(id2_head);
+            if (pst_process(list, item, item->attach)) {
+                DEBUG_WARN(("ERROR pst_process() failed with attachments\n"));
+                if (item)     pst_freeItem(item);
+                if (list)     pst_free_list(list);
+                if (id2_head) pst_free_id2(id2_head);
                 DEBUG_RET();
                 return NULL;
             }
-            if (list) _pst_free_list(list);
+            if (list) pst_free_list(list);
             list = NULL;
 
             // now we will have initial information of each attachment stored in item->attach...
@@ -1161,54 +1183,60 @@
             // each attachment
             attach = item->attach;
             while (attach) {
-              if ((id_ptr = _pst_getID2(id2_head, attach->id2_val))) {
+              if ((id_ptr = pst_getID2(id2_head, attach->id2_val))) {
                   // id_ptr is a record describing the attachment
                   // we pass NULL instead of id2_head cause we don't want it to
                   // load all the extra stuff here.
-                  if ((list = _pst_parse_block(pf, id_ptr->id, NULL)) == NULL) {
+                  if ((list = pst_parse_block(pf, id_ptr->id, NULL)) == NULL) {
                       DEBUG_WARN(("ERROR error processing an attachment record\n"));
                       attach = attach->next;
                       continue;
                   }
-                  if (_pst_process(list, item, attach)) {
-                      DEBUG_WARN(("ERROR _pst_process() failed with an attachment\n"));
-                      if (list) _pst_free_list(list);
+                  if (pst_process(list, item, attach)) {
+                      DEBUG_WARN(("ERROR pst_process() failed with an attachment\n"));
+                      if (list) pst_free_list(list);
                       list = NULL;
                       attach = attach->next;
                       continue;
                   }
-                  if (list) _pst_free_list(list);
+                  if (list) pst_free_list(list);
                   list = NULL;
-                  id_ptr = _pst_getID2(id2_head, attach->id2_val);
+                  id_ptr = pst_getID2(id2_head, attach->id2_val);
                   if (id_ptr) {
                       // id2_val has been updated to the ID2 value of the datablock containing the
                       // attachment data
                       attach->id_val = id_ptr->id;
                   } else {
-                      DEBUG_WARN(("have not located the correct value for the attachment [%#x]\n", attach->id2_val));
+                      DEBUG_WARN(("have not located the correct value for the attachment [%#llx]\n", attach->id2_val));
                   }
               } else {
-                  DEBUG_WARN(("ERROR cannot locate id2 value %#x\n", attach->id2_val));
+                  DEBUG_WARN(("ERROR cannot locate id2 value %#llx\n", attach->id2_val));
               }
               attach = attach->next;
             }
         }
     }
 
-    _pst_free_id2(id2_head);
-    id2_head = NULL;
+    if (id2_head) pst_free_id2(id2_head);
     DEBUG_RET();
     return item;
 }
 
 
-void freeall(unsigned char *buf, pst_block_offset_pointer *p1,
-                                 pst_block_offset_pointer *p2,
-                                 pst_block_offset_pointer *p3,
-                                 pst_block_offset_pointer *p4,
-                                 pst_block_offset_pointer *p5,
-                                 pst_block_offset_pointer *p6,
-                                 pst_block_offset_pointer *p7) {
+static void freeall(unsigned char *buf, pst_block_offset_pointer *p1,
+                                        pst_block_offset_pointer *p2,
+                                        pst_block_offset_pointer *p3,
+                                        pst_block_offset_pointer *p4,
+                                        pst_block_offset_pointer *p5,
+                                        pst_block_offset_pointer *p6,
+                                        pst_block_offset_pointer *p7);
+static void freeall(unsigned char *buf, pst_block_offset_pointer *p1,
+                                        pst_block_offset_pointer *p2,
+                                        pst_block_offset_pointer *p3,
+                                        pst_block_offset_pointer *p4,
+                                        pst_block_offset_pointer *p5,
+                                        pst_block_offset_pointer *p6,
+                                        pst_block_offset_pointer *p7) {
     if (buf) free(buf);
     if (p1->needfree) free(p1->from);
     if (p2->needfree) free(p2->from);
@@ -1220,7 +1248,7 @@
 }
 
 
-pst_num_array * _pst_parse_block(pst_file *pf, uint32_t block_id, pst_index2_ll *i2_head) {
+pst_num_array * pst_parse_block(pst_file *pf, uint64_t block_id, pst_index2_ll *i2_head) {
     unsigned char *buf = NULL;
     pst_num_array *na_ptr = NULL, *na_head = NULL;
     pst_block_offset_pointer block_offset1;
@@ -1230,20 +1258,18 @@
     pst_block_offset_pointer block_offset5;
     pst_block_offset_pointer block_offset6;
     pst_block_offset_pointer block_offset7;
-    uint32_t size;
-    uint32_t x;
-    uint32_t num_recs;
-    uint32_t count_rec;
-    uint32_t num_list;
-    uint32_t cur_list;
-    uint32_t block_type;
+    int32_t  x;
+    int      num_recs;
+    int      count_rec;
+    int32_t  num_list;
+    int32_t  cur_list;
+    int32_t  block_type;
     uint32_t rec_size = 0;
     uint32_t ind_ptr;
     unsigned char* list_start;
-    unsigned char* t_ptr;
     unsigned char* fr_ptr;
     unsigned char* to_ptr;
-    unsigned char* ind2_end;
+    unsigned char* ind2_end = NULL;
     unsigned char* ind2_ptr = NULL;
     size_t read_size=0;
     pst_x_attrib_ll *mapptr;
@@ -1285,9 +1311,9 @@
         uint32_t u1;
     } * type_d_rec;
 
-    DEBUG_ENT("_pst_parse_block");
-    if ((read_size = _pst_ff_getIDblock_dec(pf, block_id, &buf)) == 0) {
-        WARN(("Error reading block id %#x\n", block_id));
+    DEBUG_ENT("pst_parse_block");
+    if ((read_size = pst_ff_getIDblock_dec(pf, block_id, &buf)) == 0) {
+        WARN(("Error reading block id %#llx\n", block_id));
         if (buf) free (buf);
         DEBUG_RET();
         return NULL;
@@ -1309,10 +1335,10 @@
 
     ind_ptr = block_hdr.index_offset;
 
-    if (block_hdr.type == 0xBCEC) { //type 1
+    if (block_hdr.type == (uint16_t)0xBCEC) { //type 1
         block_type = 1;
 
-        if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, block_hdr.offset, &block_offset1)) {
+        if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, block_hdr.offset, &block_offset1)) {
             DEBUG_WARN(("internal error (bc.b5 offset %#x) in reading block id %#x\n", block_hdr.offset, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
@@ -1324,15 +1350,15 @@
         LE32_CPU(table_rec.value);
         DEBUG_EMAIL(("table_rec (type=%#hx, ref_type=%#hx, value=%#x)\n", table_rec.type, table_rec.ref_type, table_rec.value));
 
-        if (table_rec.type != 0x02B5) {
-            WARN(("Unknown second block constant - %#X for id %#x\n", table_rec.type, block_id));
+        if (table_rec.type != (uint16_t)0x02B5) {
+            WARN(("Unknown second block constant - %#hx for id %#llx\n", table_rec.type, block_id));
             DEBUG_HEXDUMPC(buf, sizeof(table_rec), 0x10);
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
             return NULL;
         }
 
-        if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset2)) {
+        if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset2)) {
             DEBUG_WARN(("internal error (bc.b5.desc offset) in reading block id %#x\n", table_rec.value, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
@@ -1343,10 +1369,10 @@
         num_list = (to_ptr - list_start)/sizeof(table_rec);
         num_recs = 1; // only going to be one object in these blocks
     }
-    else if (block_hdr.type == 0x7CEC) { //type 2
+    else if (block_hdr.type == (uint16_t)0x7CEC) { //type 2
         block_type = 2;
 
-        if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, block_hdr.offset, &block_offset3)) {
+        if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, block_hdr.offset, &block_offset3)) {
             DEBUG_WARN(("internal error (7c.7c offset %#x) in reading block id %#x\n", block_hdr.offset, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
@@ -1376,7 +1402,7 @@
         rec_size = seven_c_blk.rec_size;
         num_list = seven_c_blk.item_count;
 
-        if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, seven_c_blk.b_five_offset, &block_offset4)) {
+        if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, seven_c_blk.b_five_offset, &block_offset4)) {
             DEBUG_WARN(("internal error (7c.b5 offset %#x) in reading block id %#x\n", seven_c_blk.b_five_offset, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
@@ -1387,22 +1413,22 @@
         LE16_CPU(table_rec.ref_type);
         LE32_CPU(table_rec.value);
 
-        if (table_rec.type != 0x04B5) { // different constant than a type 1 record
-            WARN(("Unknown second block constant - %#X for id %#x\n", table_rec.type, block_id));
+        if (table_rec.type != (uint16_t)0x04B5) { // different constant than a type 1 record
+            WARN(("Unknown second block constant - %#hx for id %#llx\n", table_rec.type, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
             return NULL;
         }
 
-        if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset5)) {
-            DEBUG_WARN(("internal error (7c.5b.desc offset %#x) in reading block id %#x\n", table_rec.value, block_id));
+        if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset5)) {
+            DEBUG_WARN(("internal error (7c.5b.desc offset %#x) in reading block id %#llx\n", table_rec.value, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
             return NULL;
         }
         num_recs = (block_offset5.to - block_offset5.from) / 6; // this will give the number of records in this block
 
-        if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, seven_c_blk.ind2_offset, &block_offset6)) {
+        if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, seven_c_blk.ind2_offset, &block_offset6)) {
             DEBUG_WARN(("internal error (7c.ind2 offset %#x) in reading block id %#x\n", seven_c_blk.ind2_offset, block_id));
             freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
             DEBUG_RET();
@@ -1411,16 +1437,16 @@
         ind2_ptr = block_offset6.from;
         ind2_end = block_offset6.to;
     }
-    else if (block_hdr.index_offset == 0x0101) { //type 2
+    else if (block_hdr.index_offset == (uint16_t)0x0101) { //type 3
         unsigned char *buf2 = NULL;
-        int n = block_hdr.type; // count
-        int m = sizeof(table3_rec);
-        int i;
+        uint16_t n = block_hdr.type; // count
+        size_t   m = sizeof(table3_rec);
+        uint16_t i;
         block_type = 3;
         for (i=0; i<n; i++) {
             memcpy(&table3_rec, buf+8+i*m, m);
             LE32_CPU(table3_rec.id);
-            _pst_ff_getIDblock_dec(pf, table3_rec.id, &buf2);
+            (void)pst_ff_getIDblock_dec(pf, table3_rec.id, &buf2);
             if (buf2) free(buf2);
             buf2 = NULL;
         }
@@ -1428,7 +1454,7 @@
         DEBUG_RET();
         return NULL;
     } else {
-        WARN(("ERROR: Unknown block constant - %#X for id %#x\n", block_hdr.type, block_id));
+        WARN(("ERROR: Unknown block constant - %#hx for id %#llx\n", block_hdr.type, block_id));
         DEBUG_HEXDUMPC(buf, read_size,0x10);
         freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
         DEBUG_RET();
@@ -1441,8 +1467,8 @@
         memset(na_ptr, 0, sizeof(pst_num_array));
         na_ptr->next = na_head;
         na_head = na_ptr;
-        // allocate an array of count num_recs to contain sizeof(struct_pst_num_item)
-        na_ptr->items       = (struct _pst_num_item**) xmalloc(sizeof(struct _pst_num_item)*num_list);
+        // allocate an array of count num_recs to contain sizeof(structpst_num_item)
+        na_ptr->items       = (struct pst_num_item**) xmalloc(sizeof(struct pst_num_item)*num_list);
         na_ptr->count_item  = num_list;
         na_ptr->orig_count  = num_list;
         na_ptr->count_array = num_recs; // each record will have a record of the total number of records
@@ -1454,14 +1480,14 @@
         fr_ptr = list_start; // initialize fr_ptr to the start of the list.
         for (cur_list=0; cur_list<num_list; cur_list++) { //we will increase fr_ptr as we progress through index
             unsigned char* value_pointer = NULL;    // needed for block type 2 with values larger than 4 bytes
-            int value_size = 0;
-            if (block_type == 1) {
+            size_t value_size = 0;
+            if (block_type == (int32_t)1) {
                 memcpy(&table_rec, fr_ptr, sizeof(table_rec));
                 LE16_CPU(table_rec.type);
                 LE16_CPU(table_rec.ref_type);
                 //LE32_CPU(table_rec.value);    // done later, some may be order invariant
                 fr_ptr += sizeof(table_rec);
-            } else if (block_type == 2) {
+            } else if (block_type == (int32_t)2) {
                 // we will copy the table2_rec values into a table_rec record so that we can keep the rest of the code
                 memcpy(&table2_rec, fr_ptr, sizeof(table2_rec));
                 LE16_CPU(table2_rec.ref_type);
@@ -1473,8 +1499,8 @@
                 table_rec.ref_type = table2_rec.ref_type;
                 table_rec.value    = 0;
                 if ((ind2_end - ind2_ptr) >= (table2_rec.ind2_off + table2_rec.size)) {
-                    int n = table2_rec.size;
-                    int m = sizeof(table_rec.value);
+                    size_t n = table2_rec.size;
+                    size_t m = sizeof(table_rec.value);
                     if (n <= m) {
                         memcpy(&table_rec.value, ind2_ptr + table2_rec.ind2_off, n);
                     }
@@ -1492,7 +1518,7 @@
             } else {
                 WARN(("Missing code for block_type %i\n", block_type));
                 freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
-                if (na_head) _pst_free_list(na_head);
+                if (na_head) pst_free_list(na_head);
                 DEBUG_RET();
                 return NULL;
             }
@@ -1500,22 +1526,25 @@
                 x, table_rec.type, table_rec.ref_type, table_rec.value));
 
             if (!na_ptr->items[x]) {
-                na_ptr->items[x] = (struct _pst_num_item*) xmalloc(sizeof(struct _pst_num_item));
+                na_ptr->items[x] = (struct pst_num_item*) xmalloc(sizeof(struct pst_num_item));
             }
-            memset(na_ptr->items[x], 0, sizeof(struct _pst_num_item)); //init it
+            memset(na_ptr->items[x], 0, sizeof(struct pst_num_item)); //init it
 
             // check here to see if the id of the attribute is a mapped one
             mapptr = pf->x_head;
             while (mapptr && (mapptr->map < table_rec.type)) mapptr = mapptr->next;
             if (mapptr && (mapptr->map == table_rec.type)) {
                 if (mapptr->mytype == PST_MAP_ATTRIB) {
-                    na_ptr->items[x]->id = *((int*)mapptr->data);
+                    na_ptr->items[x]->id = *((uint32_t*)mapptr->data);
                     DEBUG_EMAIL(("Mapped attrib %#x to %#x\n", table_rec.type, na_ptr->items[x]->id));
                 } else if (mapptr->mytype == PST_MAP_HEADER) {
                     DEBUG_EMAIL(("Internet Header mapping found %#x\n", table_rec.type));
-                    na_ptr->items[x]->id = PST_ATTRIB_HEADER;
+                    na_ptr->items[x]->id = (uint32_t)PST_ATTRIB_HEADER;
                     na_ptr->items[x]->extra = mapptr->data;
                 }
+                else {
+                    // nothing, should be assertion failure here
+                }
             } else {
                 na_ptr->items[x]->id = table_rec.type;
             }
@@ -1542,27 +1571,27 @@
                 0x1102 - Array of Binary data
             */
 
-            if (table_rec.ref_type == 0x0002 ||
-                table_rec.ref_type == 0x0003 ||
-                table_rec.ref_type == 0x000b) {
+            if (table_rec.ref_type == (uint16_t)0x0002 ||
+                table_rec.ref_type == (uint16_t)0x0003 ||
+                table_rec.ref_type == (uint16_t)0x000b) {
                 //contains 32 bits of data
                 na_ptr->items[x]->size = sizeof(int32_t);
                 na_ptr->items[x]->type = table_rec.ref_type;
                 na_ptr->items[x]->data = xmalloc(sizeof(int32_t));
                 memcpy(na_ptr->items[x]->data, &(table_rec.value), sizeof(int32_t));
 
-            } else if (table_rec.ref_type == 0x0005 ||
-                       table_rec.ref_type == 0x000d ||
-                       table_rec.ref_type == 0x0014 ||
-                       table_rec.ref_type == 0x001e ||
-                       table_rec.ref_type == 0x001f ||
-                       table_rec.ref_type == 0x0040 ||
-                       table_rec.ref_type == 0x0048 ||
-                       table_rec.ref_type == 0x0102 ||
-                       table_rec.ref_type == 0x1003 ||
-                       table_rec.ref_type == 0x1014 ||
-                       table_rec.ref_type == 0x101e ||
-                       table_rec.ref_type == 0x1102) {
+            } else if (table_rec.ref_type == (uint16_t)0x0005 ||
+                       table_rec.ref_type == (uint16_t)0x000d ||
+                       table_rec.ref_type == (uint16_t)0x0014 ||
+                       table_rec.ref_type == (uint16_t)0x001e ||
+                       table_rec.ref_type == (uint16_t)0x001f ||
+                       table_rec.ref_type == (uint16_t)0x0040 ||
+                       table_rec.ref_type == (uint16_t)0x0048 ||
+                       table_rec.ref_type == (uint16_t)0x0102 ||
+                       table_rec.ref_type == (uint16_t)0x1003 ||
+                       table_rec.ref_type == (uint16_t)0x1014 ||
+                       table_rec.ref_type == (uint16_t)0x101e ||
+                       table_rec.ref_type == (uint16_t)0x1102) {
                 //contains index reference to data
                 LE32_CPU(table_rec.value);
                 if (value_pointer) {
@@ -1573,8 +1602,8 @@
                     na_ptr->items[x]->data = xmalloc(value_size);
                     memcpy(na_ptr->items[x]->data, value_pointer, value_size);
                 }
-                else if (_pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset7)) {
-                    if ((table_rec.value & 0xf) == 0xf) {
+                else if (pst_getBlockOffsetPointer(pf, i2_head, buf, read_size, ind_ptr, table_rec.value, &block_offset7)) {
+                    if ((table_rec.value & 0xf) == (uint32_t)0xf) {
                         DEBUG_WARN(("failed to get block offset for table_rec.value of %#x to be read later.\n", table_rec.value));
                         na_ptr->items[x]->size = 0;
                         na_ptr->items[x]->data = NULL;
@@ -1596,11 +1625,11 @@
                     memcpy(na_ptr->items[x]->data, block_offset7.from, value_size);
                     na_ptr->items[x]->data[value_size] = '\0';  // it might be a string, null terminate it.
                 }
-                if (table_rec.ref_type == 0xd) {
+                if (table_rec.ref_type == (uint16_t)0xd) {
                     // there is still more to do for the type of 0xD embedded objects
                     type_d_rec = (struct _type_d_rec*) na_ptr->items[x]->data;
                     LE32_CPU(type_d_rec->id);
-                    na_ptr->items[x]->size = _pst_ff_getID2block(pf, type_d_rec->id, i2_head, &(na_ptr->items[x]->data));
+                    na_ptr->items[x]->size = pst_ff_getID2block(pf, type_d_rec->id, i2_head, &(na_ptr->items[x]->data));
                     if (!na_ptr->items[x]->size){
                         DEBUG_WARN(("not able to read the ID2 data. Setting to be read later. %#x\n", type_d_rec->id));
                         na_ptr->items[x]->type = type_d_rec->id;    // fetch before freeing data, alias pointer
@@ -1608,16 +1637,23 @@
                         na_ptr->items[x]->data = NULL;
                     }
                 }
-                if (table_rec.ref_type == 0x1f) {
+                if (table_rec.ref_type == (uint16_t)0x1f) {
                     // there is more to do for the type 0x1f unicode strings
-                    VBUF_STATIC(strbuf, 1024);
-                    VBUF_STATIC(unibuf, 1024);
+                    static vbuf *strbuf = NULL;
+                    static vbuf *unibuf = NULL;
+                    if (!strbuf) strbuf=vballoc((size_t)1024);
+                    if (!unibuf) unibuf=vballoc((size_t)1024);
+
+                    // splint barfed on the following lines
+                    //VBUF_STATIC(strbuf, 1024);
+                    //VBUF_STATIC(unibuf, 1024);
+
                     //need UTF-16 zero-termination
                     vbset(strbuf, na_ptr->items[x]->data, na_ptr->items[x]->size);
-                    vbappend(strbuf, "\0\0", 2);
+                    vbappend(strbuf, "\0\0", (size_t)2);
                     DEBUG_INDEX(("Iconv in:\n"));
                     DEBUG_HEXDUMPC(strbuf->b, strbuf->dlen, 0x10);
-                    vb_utf16to8(unibuf, strbuf->b, strbuf->dlen);
+                    (void)vb_utf16to8(unibuf, strbuf->b, strbuf->dlen);
                     free(na_ptr->items[x]->data);
                     na_ptr->items[x]->size = unibuf->dlen;
                     na_ptr->items[x]->data = xmalloc(unibuf->dlen);
@@ -1627,9 +1663,9 @@
                 }
                 if (na_ptr->items[x]->type == 0) na_ptr->items[x]->type = table_rec.ref_type;
             } else {
-                WARN(("ERROR Unknown ref_type %#x\n", table_rec.ref_type));
+                WARN(("ERROR Unknown ref_type %#hx\n", table_rec.ref_type));
                 freeall(buf, &block_offset1, &block_offset2, &block_offset3, &block_offset4, &block_offset5, &block_offset6, &block_offset7);
-                if (na_head) _pst_free_list(na_head);
+                if (na_head) pst_free_list(na_head);
                 DEBUG_RET();
                 return NULL;
             }
@@ -1655,7 +1691,7 @@
 #define LIST_COPY(targ, type) {                               \
     targ = type realloc(targ, list->items[x]->size+1);        \
     memcpy(targ, list->items[x]->data, list->items[x]->size); \
-    memset(((char*)targ)+list->items[x]->size, 0, 1);         \
+    memset(((char*)targ)+list->items[x]->size, 0, (size_t)1); \
 }
 // malloc space and copy the current item's data and size
 #define LIST_COPY_SIZE(targ, type, mysize) {    \
@@ -1682,12 +1718,12 @@
 }
 
 
-int32_t _pst_process(pst_num_array *list , pst_item *item, pst_item_attach *attach) {
+int pst_process(pst_num_array *list , pst_item *item, pst_item_attach *attach) {
     int32_t x, t;
     int32_t next = 0;
     pst_item_extra_field *ef;
 
-    DEBUG_ENT("_pst_process");
+    DEBUG_ENT("pst_process");
     if (!item) {
         DEBUG_EMAIL(("item cannot be NULL.\n"));
         DEBUG_RET();
@@ -2025,7 +2061,7 @@
                     DEBUG_EMAIL(("Internet Header - "));
                     MALLOC_EMAIL(item);
                     LIST_COPY(item->email->header, (char*));
-                    //DEBUG_EMAIL(("%s\n", item->email->header));
+                    DEBUG_EMAIL(("%s\n", item->email->header));
                     DEBUG_EMAIL(("NOT PRINTED\n"));
                     break;
                 case 0x0C17: // PR_REPLY_REQUESTED
@@ -2364,7 +2400,7 @@
                     MOVE_NEXT(attach);
                     if (!list->items[x]->data) { //special case
                         attach->id2_val = list->items[x]->type;
-                        DEBUG_EMAIL(("Seen a Reference. The data hasn't been loaded yet. [%#x][%#x]\n",
+                        DEBUG_EMAIL(("Seen a Reference. The data hasn't been loaded yet. [%#llx][%#x]\n",
                                  attach->id2_val, list->items[x]->type));
                     } else {
                         LIST_COPY(attach->data, (char*));
@@ -2932,11 +2968,13 @@
                     break;
                 case 0x67F2: // ID2 value of the attachments proper record
                     DEBUG_EMAIL(("Attachment ID2 value - "));
-                    if (attach){
+                    if (attach) {
+                        uint32_t tempid;
                         MOVE_NEXT(attach);
-                        memcpy(&(attach->id2_val), list->items[x]->data, sizeof(attach->id2_val));
-                        LE32_CPU(attach->id2_val);
-                        DEBUG_EMAIL(("%#x\n", attach->id2_val));
+                        memcpy(&(tempid), list->items[x]->data, sizeof(tempid));
+                        LE32_CPU(tempid);
+                        attach->id2_val = tempid;
+                        DEBUG_EMAIL(("%#llx\n", attach->id2_val));
                     } else {
                         DEBUG_EMAIL(("NOT AN ATTACHMENT: %#x\n", list->items[x]->id));
                     }
@@ -3282,9 +3320,9 @@
 }
 
 
-int32_t _pst_free_list(pst_num_array *list) {
+void pst_free_list(pst_num_array *list) {
     pst_num_array *l;
-    DEBUG_ENT("_pst_free_list");
+    DEBUG_ENT("pst_free_list");
     while (list) {
         if (list->items) {
             int32_t x;
@@ -3301,39 +3339,36 @@
         free (l);
     }
     DEBUG_RET();
-    return 1;
 }
 
 
-int32_t _pst_free_id2(pst_index2_ll * head) {
+void pst_free_id2(pst_index2_ll * head) {
     pst_index2_ll *t;
-    DEBUG_ENT("_pst_free_id2");
+    DEBUG_ENT("pst_free_id2");
     while (head) {
         t = head->next;
         free (head);
         head = t;
     }
     DEBUG_RET();
-    return 1;
 }
 
 
-int32_t _pst_free_id (pst_index_ll *head) {
+void pst_free_id (pst_index_ll *head) {
     pst_index_ll *t;
-    DEBUG_ENT("_pst_free_id");
+    DEBUG_ENT("pst_free_id");
     while (head) {
         t = head->next;
         free(head);
         head = t;
     }
     DEBUG_RET();
-    return 1;
 }
 
 
-int32_t _pst_free_desc (pst_desc_ll *head) {
+void pst_free_desc (pst_desc_ll *head) {
     pst_desc_ll *t;
-    DEBUG_ENT("_pst_free_desc");
+    DEBUG_ENT("pst_free_desc");
     while (head) {
         while (head->child) {
             head = head->child;
@@ -3352,13 +3387,12 @@
         head = t;
     }
     DEBUG_RET();
-    return 1;
 }
 
 
-int32_t _pst_free_xattrib(pst_x_attrib_ll *x) {
+void pst_free_xattrib(pst_x_attrib_ll *x) {
     pst_x_attrib_ll *t;
-    DEBUG_ENT("_pst_free_xattrib");
+    DEBUG_ENT("pst_free_xattrib");
     while (x) {
         if (x->data) free(x->data);
         t = x->next;
@@ -3366,25 +3400,25 @@
         x = t;
     }
     DEBUG_RET();
-    return 1;
 }
 
 
-pst_index2_ll * _pst_build_id2(pst_file *pf, pst_index_ll* list, pst_index2_ll* head_ptr) {
+pst_index2_ll * pst_build_id2(pst_file *pf, pst_index_ll* list, pst_index2_ll* head_ptr) {
     pst_block_header block_head;
     pst_index2_ll *head = NULL, *tail = NULL;
-    int32_t x = 0, b_ptr = 0;
+    uint16_t x = 0;
+    char *b_ptr = NULL;
     char *buf = NULL;
     pst_id2_assoc id2_rec;
     pst_index_ll *i_ptr = NULL;
     pst_index2_ll *i2_ptr = NULL;
-    DEBUG_ENT("_pst_build_id2");
+    DEBUG_ENT("pst_build_id2");
 
     if (head_ptr) {
         head = head_ptr;
         while (head_ptr) head_ptr = (tail = head_ptr)->next;
     }
-    if (_pst_read_block_size(pf, list->offset, list->size, &buf, PST_NO_ENC, 0) < list->size) {
+    if (pst_read_block_size(pf, list->offset, list->size, &buf, PST_NO_ENC, 0) < list->size) {
         //an error occured in block read
         WARN(("block read error occured. offset = %#llx, size = %#llx\n", list->offset, list->size));
         if (buf) free(buf);
@@ -3397,29 +3431,30 @@
     LE16_CPU(block_head.type);
     LE16_CPU(block_head.count);
 
-    if (block_head.type != 0x0002) { // some sort of constant?
+    if (block_head.type != (uint16_t)0x0002) { // some sort of constant?
         WARN(("Unknown constant [%#x] at start of id2 values [offset %#llx].\n", block_head.type, list->offset));
         if (buf) free(buf);
         DEBUG_RET();
         return NULL;
     }
 
-    DEBUG_INDEX(("ID %#x is likely to be a description record. Count is %i (offset %#x)\n",
+    DEBUG_INDEX(("ID %#llx is likely to be a description record. Count is %i (offset %#llx)\n",
             list->id, block_head.count, list->offset));
     x = 0;
-    b_ptr = 0x04;
+    b_ptr = buf + ((pf->do_read64) ? 0x08 : 0x04);
     while (x < block_head.count) {
-        memcpy(&id2_rec, &(buf[b_ptr]), sizeof(id2_rec));
-        LE32_CPU(id2_rec.id2);
-        LE32_CPU(id2_rec.id);
-        LE32_CPU(id2_rec.table2);
-
-        b_ptr += sizeof(id2_rec);
-        DEBUG_INDEX(("\tid2 = %#x, id = %#x, table2 = %#x\n", id2_rec.id2, id2_rec.id, id2_rec.table2));
-        if ((i_ptr = _pst_getID(pf, id2_rec.id)) == NULL) {
-            DEBUG_WARN(("\t\t%#x - Not Found\n", id2_rec.id));
+        b_ptr += pst_decode_assoc(pf, &id2_rec, b_ptr);
+      //  memcpy(&id2_rec, &(buf[b_ptr]), sizeof(id2_rec));
+      //  LE32_CPU(id2_rec.id2);
+      //  LE32_CPU(id2_rec.id);
+      //  LE32_CPU(id2_rec.table2);
+      //
+      //  b_ptr += sizeof(id2_rec);
+        DEBUG_INDEX(("\tid2 = %#llx, id = %#llx, table2 = %#llx\n", id2_rec.id2, id2_rec.id, id2_rec.table2));
+        if ((i_ptr = pst_getID(pf, id2_rec.id)) == NULL) {
+            DEBUG_WARN(("\t\t%#llx - Not Found\n", id2_rec.id));
         } else {
-            DEBUG_INDEX(("\t\t%#x - Offset %#x, u1 %#x, Size %i(%#x)\n", i_ptr->id, i_ptr->offset, i_ptr->u1, i_ptr->size, i_ptr->size));
+            DEBUG_INDEX(("\t\t%#llx - Offset %#llx, u1 %#llx, Size %lli(%#llx)\n", i_ptr->id, i_ptr->offset, i_ptr->u1, i_ptr->size, i_ptr->size));
             // add it to the linked list
             i2_ptr = (pst_index2_ll*) xmalloc(sizeof(pst_index2_ll));
             i2_ptr->id2  = id2_rec.id2;
@@ -3429,13 +3464,13 @@
             if (tail)  tail->next = i2_ptr;
             tail = i2_ptr;
             if (id2_rec.table2 != 0) {
-                if ((i_ptr = _pst_getID(pf, id2_rec.table2)) == NULL) {
+                if ((i_ptr = pst_getID(pf, id2_rec.table2)) == NULL) {
                     DEBUG_WARN(("\tTable2 [%#x] not found\n", id2_rec.table2));
                 }
                 else {
                     DEBUG_INDEX(("\tGoing deeper for table2 [%#x]\n", id2_rec.table2));
-                    if ((i2_ptr = _pst_build_id2(pf, i_ptr, head))) {
-                    //  DEBUG_INDEX(("_pst_build_id2(): \t\tAdding new list onto end of current\n"));
+                    if ((i2_ptr = pst_build_id2(pf, i_ptr, head))) {
+                    //  DEBUG_INDEX(("pst_build_id2(): \t\tAdding new list onto end of current\n"));
                     //  if (!head)
                     //    head = i2_ptr;
                     //  if (tail)
@@ -3463,11 +3498,11 @@
 // This version of free does NULL check first
 #define SAFE_FREE(x) {if (x) free(x);}
 
-void _pst_freeItem(pst_item *item) {
+void pst_freeItem(pst_item *item) {
     pst_item_attach *t;
     pst_item_extra_field *et;
 
-    DEBUG_ENT("_pst_freeItem");
+    DEBUG_ENT("pst_freeItem");
     if (item) {
         if (item->email) {
             SAFE_FREE(item->email->arrival_date);
@@ -3657,19 +3692,19 @@
   * Or, the offset might end in 0xf, so it is an id2 pointer, in which case we read the id2 block.
   * Otherwise, the offset>>4 is an index into the table of offsets in the buffer.
 */
-int32_t _pst_getBlockOffsetPointer(pst_file *pf, pst_index2_ll *i2_head, unsigned char *buf, int32_t read_size, int32_t i_offset, int32_t offset, pst_block_offset_pointer *p) {
-    int32_t size;
+int pst_getBlockOffsetPointer(pst_file *pf, pst_index2_ll *i2_head, unsigned char *buf, size_t read_size, uint32_t i_offset, uint32_t offset, pst_block_offset_pointer *p) {
+    size_t size;
     pst_block_offset block_offset;
-    DEBUG_ENT("_pst_getBlockOffsetPointer");
+    DEBUG_ENT("pst_getBlockOffsetPointer");
     if (p->needfree) free(p->from);
     p->from = NULL;
     p->needfree = 0;
     if (!offset) {
         p->from = p->to = NULL;
     }
-    else if ((offset & 0xf) == 0xf) {
+    else if ((offset & 0xf) == (uint32_t)0xf) {
         DEBUG_WARN(("Found id2 %#x value. Will follow it\n", offset));
-        size = _pst_ff_getID2block(pf, offset, i2_head, &(p->from));
+        size = pst_ff_getID2block(pf, offset, i2_head, &(p->from));
         if (size) {
             p->to = p->from + size;
             p->needfree = 1;
@@ -3678,7 +3713,7 @@
             p->from = p->to = NULL;
         }
     }
-    else if (_pst_getBlockOffset(buf, read_size, i_offset, offset, &block_offset)) {
+    else if (pst_getBlockOffset(buf, read_size, i_offset, offset, &block_offset)) {
         p->from = p->to = NULL;
     }
     else {
@@ -3690,10 +3725,10 @@
 }
 
 
-int32_t _pst_getBlockOffset(unsigned char *buf, int32_t read_size, int32_t i_offset, int32_t offset, pst_block_offset *p) {
-    int32_t low = offset & 0xf;
-    int32_t of1 = offset >> 4;
-    DEBUG_ENT("_pst_getBlockOffset");
+int pst_getBlockOffset(unsigned char *buf, size_t read_size, uint32_t i_offset, uint32_t offset, pst_block_offset *p) {
+    uint32_t low = offset & 0xf;
+    uint32_t of1 = offset >> 4;
+    DEBUG_ENT("pst_getBlockOffset");
     if (!p || !buf || !i_offset || low || (i_offset+2+of1+sizeof(*p) > read_size)) {
         DEBUG_WARN(("p is NULL or buf is NULL or offset is 0 or offset has low bits or beyond read size (%p, %p, %#x, %i, %i)\n", p, buf, offset, read_size, i_offset));
         DEBUG_RET();
@@ -3713,16 +3748,16 @@
 }
 
 
-pst_index_ll* _pst_getID(pst_file* pf, uint64_t id) {
+pst_index_ll* pst_getID(pst_file* pf, uint64_t id) {
     pst_index_ll *ptr = NULL;
-    DEBUG_ENT("_pst_getID");
+    DEBUG_ENT("pst_getID");
     if (id == 0) {
         DEBUG_RET();
         return NULL;
     }
 
-    //if (id & 1) DEBUG_INDEX(("have odd id bit %#x\n", id));
-    //if (id & 2) DEBUG_INDEX(("have two id bit %#x\n", id));
+    //if (id & 1) DEBUG_INDEX(("have odd id bit %#llx\n", id));
+    //if (id & 2) DEBUG_INDEX(("have two id bit %#llx\n", id));
     id -= (id & 1);
 
     DEBUG_INDEX(("Trying to find %#llx\n", id));
@@ -3730,22 +3765,22 @@
     while (ptr && (ptr->id != id)) {
         ptr = ptr->next;
     }
-    if (ptr) {DEBUG_INDEX(("Found Value %#x\n", id));            }
-    else     {DEBUG_INDEX(("ERROR: Value %#x not found\n", id)); }
+    if (ptr) {DEBUG_INDEX(("Found Value %#llx\n", id));            }
+    else     {DEBUG_INDEX(("ERROR: Value %#llx not found\n", id)); }
     DEBUG_RET();
     return ptr;
 }
 
 
-pst_index_ll * _pst_getID2(pst_index2_ll *ptr, uint32_t id) {
-    DEBUG_ENT("_pst_getID2");
+pst_index_ll * pst_getID2(pst_index2_ll *ptr, uint64_t id) {
+    DEBUG_ENT("pst_getID2");
     DEBUG_INDEX(("Head = %p\n", ptr));
     DEBUG_INDEX(("Trying to find %#x\n", id));
     while (ptr && (ptr->id2 != id)) {
         ptr = ptr->next;
     }
     if (ptr) {
-        if (ptr->id) {DEBUG_INDEX(("Found value %#x\n", ptr->id->id));   }
+        if (ptr->id) {DEBUG_INDEX(("Found value %#llx\n", ptr->id->id));   }
         else         {DEBUG_INDEX(("Found value, though it is NULL!\n"));}
         DEBUG_RET();
         return ptr->id;
@@ -3764,9 +3799,9 @@
  *
  * @return pointer to the pst_desc_ll node in the descriptor tree
 */
-pst_desc_ll* _pst_getDptr(pst_file *pf, uint32_t id) {
+pst_desc_ll* pst_getDptr(pst_file *pf, uint64_t id) {
     pst_desc_ll *ptr = pf->d_head;
-    DEBUG_ENT("_pst_getDptr");
+    DEBUG_ENT("pst_getDptr");
     while (ptr && (ptr->id != id)) {
         if (ptr->child) {
             ptr = ptr->child;
@@ -3782,11 +3817,11 @@
 }
 
 
-int32_t _pst_printDptr(pst_file *pf) {
+int32_t pst_printDptr(pst_file *pf) {
     pst_desc_ll *ptr = pf->d_head;
     int32_t depth = 0;
     char spaces[100];
-    DEBUG_ENT("_pst_printDptr");
+    DEBUG_ENT("pst_printDptr");
     memset(spaces, ' ', 99);
     spaces[99] = '\0';
     while (ptr) {
@@ -3809,9 +3844,9 @@
 }
 
 
-int32_t _pst_printIDptr(pst_file* pf) {
+int32_t pst_printIDptr(pst_file* pf) {
     pst_index_ll *ptr = pf->i_head;
-    DEBUG_ENT("_pst_printIDptr");
+    DEBUG_ENT("pst_printIDptr");
     while (ptr) {
         DEBUG_INDEX(("%#x offset=%#x size=%#x\n", ptr->id, ptr->offset, ptr->size));
         ptr = ptr->next;
@@ -3821,8 +3856,8 @@
 }
 
 
-int32_t _pst_printID2ptr(pst_index2_ll *ptr) {
-    DEBUG_ENT("_pst_printID2ptr");
+int32_t pst_printID2ptr(pst_index2_ll *ptr) {
+    DEBUG_ENT("pst_printID2ptr");
     while (ptr) {
         DEBUG_INDEX(("%#x id=%#x\n", ptr->id2, (ptr->id!=NULL?ptr->id->id:0)));
         ptr = ptr->next;
@@ -3832,56 +3867,31 @@
 }
 
 
-size_t _pst_read_block(FILE *fp, off_t offset, void **buf) {
-    int16_t size16;
-    size_t size;
-    off_t fpos;
-    DEBUG_ENT("_pst_read_block");
-    DEBUG_READ(("Reading block from %#x\n", offset));
-    fpos = ftell(fp);
-    fseek(fp, offset, SEEK_SET);
-    fread(&size16, sizeof(int16_t), 1, fp);
-    LE16_CPU(size16);
-    size = size16;
-    fseek(fp, offset, SEEK_SET);
-    DEBUG_READ(("Allocating %i bytes\n", size));
-    if (*buf) {
-        DEBUG_READ(("Freeing old memory\n"));
-        free(*buf);
-    }
-    *buf = (void*)xmalloc(size);
-    size = fread(*buf, 1, size, fp);
-    fseek(fp, fpos, SEEK_SET);
-    DEBUG_RET();
-    return size;
-}
-
-
 // when the first byte of the block being read is 01, then we can assume
 // that it is a list of further ids to read and we will follow those ids
 // recursively calling this function until we have all the data
 // we could do decryption of the encrypted PST files here
-size_t _pst_read_block_size(pst_file *pf, off_t offset, size_t size, char **buf, int32_t do_enc, unsigned char is_index) {
+size_t pst_read_block_size(pst_file *pf, off_t offset, size_t size, char **buf, int32_t do_enc, unsigned char is_index) {
     off_t fpos;
-    int32_t x;
+    uint32_t x;
     int16_t count, y;
     char *buf2 = NULL, *buf3 = NULL;
     unsigned char fdepth;
     pst_index_ll *ptr = NULL;
     size_t rsize, z;
 
-    DEBUG_ENT("_pst_read_block_size");
+    DEBUG_ENT("pst_read_block_size");
     DEBUG_READ(("Reading block from %#x, %i bytes\n", offset, size));
 
     fpos = ftell(pf->fp);
-    fseek(pf->fp, offset, SEEK_SET);
+    (void)fseek(pf->fp, offset, SEEK_SET);
     if (*buf) {
         DEBUG_READ(("Freeing old memory\n"));
         free(*buf);
     }
 
     *buf = (void*) xmalloc(size+1); //plus one so that we can NUL terminate it later
-    rsize = fread(*buf, 1, size, pf->fp);
+    rsize = fread(*buf, (size_t)1, size, pf->fp);
     if (rsize != size) {
         DEBUG_WARN(("Didn't read all that I could. fread returned less [%i instead of %i]\n", rsize, size));
         if (feof(pf->fp)) {
@@ -3897,7 +3907,7 @@
     //  DEBUG_HEXDUMP(*buf, size);
 
     /*  if (is_index) {
-      DEBUG_READ(("_pst_read_block_size: ODD_BLOCK should be here\n"));
+      DEBUG_READ(("pst_read_block_size: ODD_BLOCK should be here\n"));
       DEBUG_READ(("\t: byte 0-1: %#x %#x\n", (*buf)[0], (*buf)[1]));
       }*/
 
@@ -3913,9 +3923,9 @@
         size = 0;
         y = 0;
         while (y < count) {
-            memcpy(&x, &(*buf)[0x08+(y*4)], sizeof(int32_t));
+            memcpy(&x, &(*buf)[0x08+(y*4)], sizeof(uint32_t));
             LE32_CPU(x);
-            if ((ptr = _pst_getID(pf, x)) == NULL) {
+            if ((ptr = pst_getID(pf, x)) == NULL) {
                 WARN(("Error. Cannot find ID [%#x] during multi-block read\n", x));
                 buf3 = (char*) realloc(buf3, size+1);
                 buf3[size] = '\0';
@@ -3924,7 +3934,7 @@
                 DEBUG_RET();
                 return size;
             }
-            if ((z = _pst_read_block_size(pf, ptr->offset, ptr->size, &buf2, do_enc, fdepth-1)) < ptr->size) {
+            if ((z = pst_read_block_size(pf, ptr->offset, ptr->size, &buf2, do_enc, fdepth-1)) < ptr->size) {
                 buf3 = (char*) realloc(buf3, size+1);
                 buf3[size] = '\0';
                 *buf = buf3;
@@ -3944,23 +3954,26 @@
         if (!buf3) {
             // this can happen if count == 0. We should create an empty buffer so we don't
             // confuse any clients
-            buf3 = (char*) xmalloc(1);
+            buf3 = (char*) xmalloc((size_t)1);
         }
         *buf = buf3;
-    } else if (do_enc && pf->encryption)
-        _pst_decrypt(*buf, size, pf->encryption);
+    } else if (do_enc && pf->encryption) {
+        (void)pst_decrypt(*buf, size, pf->encryption);
+    } else {
+        // nothing to do
+    }
 
     (*buf)[size] = '\0'; //should be byte after last one read
-    fseek(pf->fp, fpos, SEEK_SET);
+    (void)fseek(pf->fp, fpos, SEEK_SET);
     DEBUG_RET();
     return size;
 }
 
 
-int32_t _pst_decrypt(unsigned char *buf, size_t size, int32_t type) {
+int pst_decrypt(unsigned char *buf, size_t size, unsigned char type) {
     size_t x = 0;
     unsigned char y;
-    DEBUG_ENT("_pst_decrypt");
+    DEBUG_ENT("pst_decrypt");
     if (!buf) {
         DEBUG_RET();
         return -1;
@@ -3984,10 +3997,10 @@
 }
 
 
-int64_t _getIntAt(char *buf) {
-    int64_t buf64;
-    int32_t buf32;
-    if (do_read64) {
+uint64_t pst_getIntAt(pst_file *pf, char *buf) {
+    uint64_t buf64;
+    uint32_t buf32;
+    if (pf->do_read64) {
         memcpy(&buf64, buf, sizeof(buf64));
         LE64_CPU(buf64);
         return buf64;
@@ -4000,24 +4013,24 @@
 }
 
 
-int64_t _pst_getIntAtPos(FILE *fp, off_t pos ) {
-    int64_t buf64;
-    int32_t buf32;
-    if (do_read64) {
-        _pst_getAtPos(fp, pos, &buf64, sizeof(buf64));
+uint64_t pst_getIntAtPos(pst_file *pf, off_t pos ) {
+    uint64_t buf64;
+    uint32_t buf32;
+    if (pf->do_read64) {
+        (void)pst_getAtPos(pf->fp, pos, &buf64, sizeof(buf64));
         LE64_CPU(buf64);
         return buf64;
     }
     else {
-        _pst_getAtPos(fp, pos, &buf32, sizeof(buf32));
+        (void)pst_getAtPos(pf->fp, pos, &buf32, sizeof(buf32));
         LE32_CPU(buf32);
         return buf32;
     }
 }
 
 
-int32_t _pst_getAtPos(FILE *fp, off_t pos, void* buf, uint32_t size) {
-    DEBUG_ENT("_pst_getAtPos");
+int pst_getAtPos(FILE *fp, off_t pos, void* buf, size_t size) {
+    DEBUG_ENT("pst_getAtPos");
     if (fseek(fp, pos, SEEK_SET) == -1) {
         DEBUG_RET();
         return 1;
@@ -4031,8 +4044,8 @@
 }
 
 
-int32_t _pst_get (FILE *fp, void *buf, uint32_t size) {
-    DEBUG_ENT("_pst_get");
+int pst_get (FILE *fp, void *buf, size_t size) {
+    DEBUG_ENT("pst_get");
     if (fread(buf, 1,  size, fp) < size) {
         DEBUG_RET();
         return 1;
@@ -4042,14 +4055,14 @@
 }
 
 
-size_t _pst_ff_getIDblock_dec(pst_file *pf, uint32_t id, unsigned char **b) {
+size_t pst_ff_getIDblock_dec(pst_file *pf, uint64_t id, unsigned char **b) {
     size_t r;
-    DEBUG_ENT("_pst_ff_getIDblock_dec");
+    int noenc = (int)(id & 2);   // disable encryption
+    DEBUG_ENT("pst_ff_getIDblock_dec");
     DEBUG_INDEX(("for id %#x\n", id));
-    r = _pst_ff_getIDblock(pf, id, b);
-    int noenc = (id & 2);   // disable encryption
-    if ((pf->encryption) & !(noenc)) {
-        _pst_decrypt(*b, r, pf->encryption);
+    r = pst_ff_getIDblock(pf, id, b);
+    if ((pf->encryption) && !(noenc)) {
+        (void)pst_decrypt(*b, r, pf->encryption);
     }
     DEBUG_HEXDUMPC(*b, r, 16);
     DEBUG_RET();
@@ -4057,16 +4070,16 @@
 }
 
 
-size_t _pst_ff_getIDblock(pst_file *pf, uint32_t id, unsigned char** b) {
+size_t pst_ff_getIDblock(pst_file *pf, uint64_t id, unsigned char** b) {
     pst_index_ll *rec;
-    size_t rsize = 0;//, re_size=0;
-    DEBUG_ENT("_pst_ff_getIDblock");
-    if ((rec = _pst_getID(pf, id)) == NULL) {
+    size_t rsize = 0;
+    DEBUG_ENT("pst_ff_getIDblock");
+    if ((rec = pst_getID(pf, id)) == NULL) {
         DEBUG_INDEX(("Cannot find ID %#x\n", id));
         DEBUG_RET();
         return 0;
     }
-    fseek(pf->fp, rec->offset, SEEK_SET);
+    (void)fseek(pf->fp, rec->offset, SEEK_SET);
     if (*b) {
         DEBUG_INDEX(("freeing old memory in b\n"));
         free(*b);
@@ -4091,12 +4104,12 @@
 
 
 #define PST_PTR_BLOCK_SIZE 0x120
-size_t _pst_ff_getID2block(pst_file *pf, uint32_t id2, pst_index2_ll *id2_head, unsigned char** buf) {
+size_t pst_ff_getID2block(pst_file *pf, uint64_t id2, pst_index2_ll *id2_head, unsigned char** buf) {
     pst_index_ll* ptr;
     //  size_t ret;
     struct holder h = {buf, NULL, 0, "", 0};
-    DEBUG_ENT("_pst_ff_getID2block");
-    ptr = _pst_getID2(id2_head, id2);
+    DEBUG_ENT("pst_ff_getID2block");
+    ptr = pst_getID2(id2_head, id2);
 
     if (!ptr) {
         DEBUG_INDEX(("Cannot find id2 value %#x\n", id2));
@@ -4104,33 +4117,36 @@
         return 0;
     }
     DEBUG_RET();
-    return _pst_ff_getID2data(pf, ptr, &h);
+    return pst_ff_getID2data(pf, ptr, &h);
 }
 
 
-size_t _pst_ff_getID2data(pst_file *pf, pst_index_ll *ptr, struct holder *h) {
-    int32_t ret;
+size_t pst_ff_getID2data(pst_file *pf, pst_index_ll *ptr, struct holder *h) {
+    size_t ret;
     unsigned char *b = NULL, *t;
-    DEBUG_ENT("_pst_ff_getID2data");
+    DEBUG_ENT("pst_ff_getID2data");
     if (!(ptr->id & 0x02)) {
-        ret = _pst_ff_getIDblock_dec(pf, ptr->id, &b);
+        ret = pst_ff_getIDblock_dec(pf, ptr->id, &b);
         if (h->buf) {
             *(h->buf) = b;
         } else if ((h->base64 == 1) && h->fp) {
             t = base64_encode(b, ret);
             if (t) {
-                pst_fwrite(t, 1, strlen(t), h->fp);
+                (void)pst_fwrite(t, 1, strlen(t), h->fp);
                 free(t);    // caught by valgrind
             }
             free(b);
         } else if (h->fp) {
-            pst_fwrite(b, 1, ret, h->fp);
+            (void)pst_fwrite(b, 1, ret, h->fp);
             free(b);
+        } else {
+            // h-> does not specify any output
         }
+
     } else {
         // here we will assume it is a block that points to others
         DEBUG_READ(("Assuming it is a multi-block record because of it's id\n"));
-        ret = _pst_ff_compile_ID(pf, ptr->id, h, 0);
+        ret = pst_ff_compile_ID(pf, ptr->id, h, (size_t)0);
     }
     if (h->buf && *h->buf)
         (*(h->buf))[ret]='\0';
@@ -4139,15 +4155,15 @@
 }
 
 
-size_t _pst_ff_compile_ID(pst_file *pf, uint32_t id, struct holder *h, int32_t size) {
+size_t pst_ff_compile_ID(pst_file *pf, uint64_t id, struct holder *h, size_t size) {
     size_t z, a;
     uint16_t count, y;
     uint32_t x, b;
     unsigned char * buf3 = NULL, *buf2 = NULL, *t;
     unsigned char fdepth;
 
-    DEBUG_ENT("_pst_ff_compile_ID");
-    a = _pst_ff_getIDblock(pf, id, &buf3);
+    DEBUG_ENT("pst_ff_compile_ID");
+    a = pst_ff_getIDblock(pf, id, &buf3);
     if (!a) {
         if (buf3) free(buf3);
         return 0;
@@ -4156,19 +4172,21 @@
         //  if ((buf3)[0] != 0x1 && (buf3)[1] > 4) {
         DEBUG_WARN(("WARNING: buffer doesn't start with 0x1, but I expected it to or doesn't have it's two-bit set!\n"));
         DEBUG_WARN(("Treating as normal buffer\n"));
-        if (pf->encryption) _pst_decrypt(buf3, a, pf->encryption);
+        if (pf->encryption) (void)pst_decrypt(buf3, a, pf->encryption);
         if (h->buf)
             *(h->buf) = buf3;
         else if (h->base64 == 1 && h->fp) {
             t = base64_encode(buf3, a);
             if (t) {
-                pst_fwrite(t, 1, strlen(t), h->fp);
+                (void)pst_fwrite(t, 1, strlen(t), h->fp);
                 free(t);    // caught by valgrind
             }
             free(buf3);
         } else if (h->fp) {
-            pst_fwrite(buf3, 1, a, h->fp);
+            (void)pst_fwrite(buf3, 1, a, h->fp);
             free(buf3);
+        } else {
+            // h-> does not specify any output
         }
         DEBUG_RET();
         return a;
@@ -4184,24 +4202,23 @@
         memcpy(&x, &buf3[0x08+(y*4)], sizeof(int32_t));
         LE32_CPU(x);
         if (fdepth == 0x1) {
-            if ((z = _pst_ff_getIDblock(pf, x, &buf2)) == 0) {
+            if ((z = pst_ff_getIDblock(pf, x, &buf2)) == 0) {
                 DEBUG_WARN(("call to getIDblock returned zero %i\n", z));
                 if (buf2) free(buf2);
                 free(buf3);
                 return z;
             }
-            if (pf->encryption) _pst_decrypt(buf2, z, pf->encryption);
+            if (pf->encryption) (void)pst_decrypt(buf2, z, pf->encryption);
             if (h->buf) {
                 *(h->buf) = realloc(*(h->buf), size+z+1);
                 DEBUG_READ(("appending read data of size %i onto main buffer from pos %i\n", z, size));
                 memcpy(&((*(h->buf))[size]), buf2, z);
-            }
-            else if ((h->base64 == 1) && h->fp) {
+            } else if ((h->base64 == 1) && h->fp) {
                 // include any byte left over from the last one encoding
                 buf2 = (char*)realloc(buf2, z+h->base64_extra);
                 memmove(buf2+h->base64_extra, buf2, z);
                 memcpy(buf2, h->base64_extra_chars, h->base64_extra);
-                z+= h->base64_extra;
+                z += h->base64_extra;
 
                 b = z % 3; // find out how many bytes will be left over after the encoding.
                 // and save them
@@ -4210,19 +4227,20 @@
                 t = base64_encode(buf2, z-b);
                 if (t) {
                     DEBUG_READ(("writing %i bytes to file as base64 [%i]. Currently %i\n", z, strlen(t), size));
-                    pst_fwrite(t, 1, strlen(t), h->fp);
+                    (void)pst_fwrite(t, 1, strlen(t), h->fp);
                     free(t);    // caught by valgrind
                 }
-            }
-            else if (h->fp) {
+            } else if (h->fp) {
                 DEBUG_READ(("writing %i bytes to file. Currently %i\n", z, size));
-                pst_fwrite(buf2, 1, z, h->fp);
+                (void)pst_fwrite(buf2, 1, z, h->fp);
+            } else {
+                // h-> does not specify any output
             }
             size += z;
             y++;
         }
         else {
-            if ((z = _pst_ff_compile_ID(pf, x, h, size)) == 0) {
+            if ((z = pst_ff_compile_ID(pf, x, h, size)) == 0) {
                 DEBUG_WARN(("recursive called returned zero %i\n", z));
                 free(buf3);
                 DEBUG_RET();
@@ -4277,7 +4295,7 @@
 
 #endif //_MSC_VER
 
-int32_t pst_stricmp(char *a, char *b) {
+int pst_stricmp(char *a, char *b) {
     // compare strings case-insensitive.
     // returns -1 if a < b, 0 if a==b, 1 if a > b
     while(*a != '\0' && *b != '\0' && toupper(*a)==toupper(*b)) {
@@ -4292,10 +4310,10 @@
 }
 
 
-int32_t pst_strincmp(char *a, char *b, int32_t x) {
+int pst_strincmp(char *a, char *b, size_t x) {
     // compare upto x chars in string a and b case-insensitively
     // returns -1 if a < b, 0 if a==b, 1 if a > b
-    int32_t y = 0;
+    size_t y = 0;
     while (*a != '\0' && *b != '\0' && y < x && toupper(*a)==toupper(*b)) {
         a++; b++; y++;
     }
@@ -4323,10 +4341,10 @@
 }
 
 
-char * _pst_wide_to_single(char *wt, int32_t size) {
+char * pst_wide_to_single(char *wt, int32_t size) {
     // returns the first byte of each wide char. the size is the number of bytes in source
     char *x, *y;
-    DEBUG_ENT("_pst_wide_to_single");
+    DEBUG_ENT("pst_wide_to_single");
     x = xmalloc((size/2)+1);
     y = x;
     while (size != 0 && *wt != '\0') {
@@ -4396,9 +4414,8 @@
 
 int pst_chr_count(char *str, char x) {
     int r = 0;
-    while (*str != '\0') {
-        if (*str == x)
-            r++;
+    while (*str) {
+        if (*str == x) r++;
         str++;
     }
     return r;