changeset 73:3cb02cb1e6cd stable-0-6-10

Patch from Robert Simpson to fix doubly-linked list in the cache_ptr code, and allow arrays of unicode strings (without converting them). More changes for Fedora packaging (#434727) Fixes for const correctness.
author Carl Byington <carl@five-ten-sg.com>
date Thu, 29 May 2008 18:51:02 -0700
parents c21e9c001256
children 6f82d13e9300
files AUTHORS ChangeLog NEWS configure.in libpst.spec.in src/Makefile.am src/debug.c src/define.h src/deltasearch.cpp src/dumpblocks.c src/generic.c src/generic.h src/getidblock.c src/libpst.c src/libpst.h src/lspst.c src/lzfu.c src/lzfu.h src/pst2dii.cpp.in src/pst2ldif.cpp src/readpst.c src/vbuf.c
diffstat 22 files changed, 818 insertions(+), 1238 deletions(-) [+]
line wrap: on
line diff
--- a/AUTHORS	Fri May 16 09:06:17 2008 -0700
+++ b/AUTHORS	Thu May 29 18:51:02 2008 -0700
@@ -18,3 +18,4 @@
     Sean Loaring <sloaring@tec-man.com>
     James Woodcock
     Joachim Metz <joachim.metz@gmail.com>
+    Robert Simpson <rsimpson@idiscoverglobal.com>
--- a/ChangeLog	Fri May 16 09:06:17 2008 -0700
+++ b/ChangeLog	Thu May 29 18:51:02 2008 -0700
@@ -1,10 +1,18 @@
-LibPST 0.6.9 (2008-05-161)
+LibPST 0.6.10 (2008-05-29)
+===============================
+        * Patch from Robert Simpson <rsimpson@idiscoverglobal.com>
+        fix doubly-linked list in the cache_ptr code, and allow
+	arrays of unicode strings (without converting them).
+        * More changes for Fedora packaging (#434727)
+	* Fixes for const correctness.
+
+LibPST 0.6.9 (2008-05-16)
 ===============================
         * Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit
-	compile.
-	* Signed/unsigned cleanup from 'CFLAGS=-Wextra ./configure'.
-	* Reindent vbuf.c to make it readable.
-	* Fix pst format documentation for 8 byte backpointers.
+        compile.
+        * Signed/unsigned cleanup from 'CFLAGS=-Wextra ./configure'.
+        * Reindent vbuf.c to make it readable.
+        * Fix pst format documentation for 8 byte backpointers.
 
 LibPST 0.6.8 (2008-03-05)
 ===============================
--- a/NEWS	Fri May 16 09:06:17 2008 -0700
+++ b/NEWS	Thu May 29 18:51:02 2008 -0700
@@ -1,4 +1,5 @@
-0.6.9   2008-05-16 Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
+0.6.10  2008-05-29 Patch from Robert Simpson for doubly-linked list and arrays of unicode strings.
+0.6.9   2008-05-16 Patch from Joachim Metz for 64 bit compile.
 0.6.8   2008-03-05 Initial version of pst2dii to convert to Summation dii load file format.
 0.6.7   2008-02-16 Ignore unknown attachments on some read messages; autoconf cleanup.
 0.6.6   2008-01-31 Code cleanup, switch from cvs to mercurial source control.
--- a/configure.in	Fri May 16 09:06:17 2008 -0700
+++ b/configure.in	Thu May 29 18:51:02 2008 -0700
@@ -1,5 +1,5 @@
 AC_PREREQ(2.59)
-AC_INIT(libpst,0.6.9,carl@five-ten-sg.com)
+AC_INIT(libpst,0.6.10,carl@five-ten-sg.com)
 AC_CONFIG_SRCDIR([config.h.in])
 AC_CONFIG_HEADER([config.h])
 
--- a/libpst.spec.in	Fri May 16 09:06:17 2008 -0700
+++ b/libpst.spec.in	Thu May 29 18:51:02 2008 -0700
@@ -47,8 +47,11 @@
 
 
 %changelog
+* Thu May 29 2008 Carl Byington <carl@five-ten-sg.com> - 0.6.10-1
+- Patch from Robert Simpson for doubly-linked list code and arrays of unicode strings.
+
 * Fri May 16 2008 Carl Byington <carl@five-ten-sg.com> - 0.6.9
-- Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
+- Patch from Joachim Metz for 64 bit compile.
 - Fix pst format documentation for 8 byte backpointers.
 
 * Wed Mar 05 2008 Carl Byington <carl@five-ten-sg.com> - 0.6.8
--- a/src/Makefile.am	Fri May 16 09:06:17 2008 -0700
+++ b/src/Makefile.am	Thu May 29 18:51:02 2008 -0700
@@ -5,15 +5,15 @@
 endif
 noinst_PROGRAMS     = deltasearch dumpblocks getidblock
 
-lspst_SOURCES       = $(hfiles) lspst.c      libpst.c timeconv.c libstrfunc.c debug.c        vbuf.c generic.c
-readpst_SOURCES     = $(hfiles) readpst.c    libpst.c timeconv.c libstrfunc.c debug.c lzfu.c vbuf.c generic.c
+lspst_SOURCES       = $(hfiles) lspst.c      libpst.c timeconv.c libstrfunc.c debug.c        vbuf.c
+readpst_SOURCES     = $(hfiles) readpst.c    libpst.c timeconv.c libstrfunc.c debug.c lzfu.c vbuf.c
 readpstlog_SOURCES  = $(hfiles) readpstlog.c                                  debug.c
-pst2ldif_SOURCES    = $(hfiles) pst2ldif.cpp libpst.c timeconv.c libstrfunc.c debug.c lzfu.c vbuf.c generic.c
-pst2dii_SOURCES	    = $(hfiles) pst2dii.cpp  libpst.c timeconv.c libstrfunc.c debug.c lzfu.c vbuf.c generic.c
+pst2ldif_SOURCES    = $(hfiles) pst2ldif.cpp libpst.c timeconv.c libstrfunc.c debug.c lzfu.c vbuf.c
+pst2dii_SOURCES	    = $(hfiles) pst2dii.cpp  libpst.c timeconv.c libstrfunc.c debug.c lzfu.c vbuf.c
 
 deltasearch_SOURCES = deltasearch.cpp debug.c
-dumpblocks_SOURCES  = dumpblocks.c    debug.c libpst.c libstrfunc.c timeconv.c vbuf.c generic.c
-getidblock_SOURCES  = getidblock.c    debug.c libpst.c libstrfunc.c timeconv.c vbuf.c generic.c
+dumpblocks_SOURCES  = dumpblocks.c    debug.c libpst.c libstrfunc.c timeconv.c vbuf.c
+getidblock_SOURCES  = getidblock.c    debug.c libpst.c libstrfunc.c timeconv.c vbuf.c
 
 if NEED_XGETOPT
     lspst_SOURCES       += XGetopt.c
--- a/src/debug.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/debug.c	Thu May 29 18:51:02 2008 -0700
@@ -28,16 +28,21 @@
 } *func_head=NULL, *func_ptr=NULL;
 
 
-void pst_debug_write_msg(struct pst_debug_item *item, char *fmt, va_list *ap, int size);
-void pst_debug_write_hex(struct pst_debug_item *item, unsigned char *buf, size_t size, int col);
+void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size);
+void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col);
 void * xmalloc(size_t size);
 
+size_t pst_debug_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
+    return fwrite(ptr, size, nitems, stream);
+}
+
+
 // the largest text size we will store in memory. Otherwise we
 // will do a debug_write, then create a new record, and write the
 // text body directly to the file
 #define MAX_MESSAGE_SIZE 4096
 
-void pst_debug(char *fmt, ...) {
+void pst_debug(const char *fmt, ...) {
     va_list ap;
     va_start(ap,fmt);
     vfprintf(stderr, fmt, ap);
@@ -46,7 +51,7 @@
 
 
 #define NUM_COL 30
-void pst_debug_hexdumper(FILE *out, unsigned char *buf, size_t size, int col, int delta) {
+void pst_debug_hexdumper(FILE *out, char *buf, size_t size, int col, int delta) {
     size_t off = 0, toff;
     int count = 0;
 
@@ -57,7 +62,7 @@
         fprintf(out, "%06X\t:", off+delta);
         toff = off;
         while (count < col && off < size) {
-            fprintf(out, "%02hhx ", buf[off]);
+            fprintf(out, "%02hhx ", (unsigned char)buf[off]);
             off++; count++;
         }
         off = toff;
@@ -85,7 +90,7 @@
 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0;
 
 
-void pst_debug_init(char* fname) {
+void pst_debug_init(const char* fname) {
     unsigned char version = DEBUG_VERSION;
     item_head = item_tail = NULL;
     curr_items = 0;
@@ -95,13 +100,13 @@
       fprintf(stderr, "Opening of file %s failed\n", fname);
       exit(1);
     }
-    fwrite(&version, 1, sizeof(char), debug_fp);
+    pst_debug_fwrite(&version, 1, sizeof(char), debug_fp);
 }
 
 
 // function must be called before pst_debug_msg. It sets up the
 // structure for the function that follows
-void pst_debug_msg_info(int line, char* file, int type) {
+void pst_debug_msg_info(int line, const char* file, int type) {
     char *x;
     if (!debug_fp) return;  // no file
     info_ptr = (struct pst_debug_item*) xmalloc(sizeof(struct pst_debug_item));
@@ -120,7 +125,7 @@
 }
 
 
-void pst_debug_msg_text(char* fmt, ...) {
+void pst_debug_msg_text(const char* fmt, ...) {
     va_list ap;
     int f, g;
     char x[2];
@@ -192,7 +197,7 @@
 }
 
 
-void pst_debug_hexdump(unsigned char *x, size_t y, int cols, int delta) {
+void pst_debug_hexdump(char *x, size_t y, int cols, int delta) {
     struct pst_debug_item *temp;
     if (!debug_fp) return;  // no file
     info_ptr = temp_list;
@@ -208,7 +213,7 @@
 }
 
 
-void pst_debug_func(char *function) {
+void pst_debug_func(const char *function) {
     func_ptr = xmalloc (sizeof(struct pst_debug_func));
     func_ptr->name = xmalloc(strlen(function)+1);
     strcpy(func_ptr->name, function);
@@ -264,7 +269,7 @@
     file_pos += index_size;
     // write the index first, we will re-write it later, but
     // we want to allocate the space
-    fwrite(index, index_size, 1, debug_fp);
+    pst_debug_fwrite(index, index_size, 1, debug_fp);
     index[index_ptr++] = curr_items;
 
     item_ptr = item_head;
@@ -286,26 +291,26 @@
         end=ptr;
         if (end > USHRT_MAX) { // bigger than can be stored in a short
             rec_type = 'L';
-            fwrite(&rec_type, 1, sizeof(char), debug_fp);
+            pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
             lfile_rec.type = item_ptr->type;
             lfile_rec.line = item_ptr->line;
             lfile_rec.funcname = funcname;
             lfile_rec.filename = filename;
             lfile_rec.text = text;
             lfile_rec.end = end;
-            fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+            pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
         } else {
             rec_type = 'M';
-            fwrite(&rec_type, 1, sizeof(char), debug_fp);
+            pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
             mfile_rec.type = item_ptr->type;
             mfile_rec.line = item_ptr->line;
             mfile_rec.funcname = funcname;
             mfile_rec.filename = filename;
             mfile_rec.text = text;
             mfile_rec.end = end;
-            fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
+            pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
         }
-        fwrite(buf, 1, ptr, debug_fp);
+        pst_debug_fwrite(buf, 1, ptr, debug_fp);
         if (buf) free(buf); buf = NULL;
         item_head = item_ptr->next;
         free(item_ptr->function);
@@ -319,7 +324,7 @@
 
     // we should now have a complete index
     fseek(debug_fp, index_pos, SEEK_SET);
-    fwrite(index, index_size, 1, debug_fp);
+    pst_debug_fwrite(index, index_size, 1, debug_fp);
     fseek(debug_fp, 0, SEEK_END);
     item_ptr = item_head = item_tail = NULL;
     free(index);
@@ -327,7 +332,7 @@
 }
 
 
-void pst_debug_write_msg(struct pst_debug_item *item, char *fmt, va_list *ap, int size) {
+void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size) {
     struct pst_debug_file_rec_l lfile_rec;
     struct pst_debug_file_rec_m mfile_rec;
     unsigned char rec_type;
@@ -339,54 +344,54 @@
     if (!debug_fp) return;  // no file
     index[0] = 1; //only one item in this index
     index_pos = ftell(debug_fp);
-    fwrite(index, index_size, 1, debug_fp);
+    pst_debug_fwrite(index, index_size, 1, debug_fp);
 
     index[1] = ftell(debug_fp);
 
     if (size > USHRT_MAX) { // bigger than can be stored in a short
         rec_type = 'L';
-        fwrite(&rec_type, 1, sizeof(char), debug_fp);
+        pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
         lfile_rec.type = item->type;
         lfile_rec.line = item->line;
         lfile_rec.funcname = 0;
         lfile_rec.filename = strlen(item->function)+1;
         lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
-        fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+        pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
     } else {
         rec_type = 'M';
-        fwrite(&rec_type, 1, sizeof(char), debug_fp);
+        pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
         mfile_rec.type = item->type;
         mfile_rec.line = item->line;
         mfile_rec.funcname = 0;
         mfile_rec.filename = strlen(item->function)+1;
         mfile_rec.text = mfile_rec.filename+strlen(item->file)+1;
-        fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
+        pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
     }
     file_pos = ftell(debug_fp);
-    fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
-    fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
+    pst_debug_fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
+    pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
     vfprintf(debug_fp, fmt, *ap);
-    fwrite(&zero, 1, 1, debug_fp);
+    pst_debug_fwrite(&zero, 1, 1, debug_fp);
 
     end = ftell(debug_fp)-file_pos;
 
     index[2] = ftell(debug_fp);
     fseek(debug_fp, index_pos, SEEK_SET);
-    fwrite(index, index_size, 1, debug_fp);
+    pst_debug_fwrite(index, index_size, 1, debug_fp);
     if (size > USHRT_MAX) {
-        fwrite(&rec_type, 1, sizeof(char), debug_fp);
+        pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
         lfile_rec.end = end;
-        fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+        pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
     } else {
-        fwrite(&rec_type, 1, sizeof(char), debug_fp);
+        pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
         mfile_rec.end = end;
-        fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
+        pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
     }
     fseek(debug_fp, 0, SEEK_END);
 }
 
 
-void pst_debug_write_hex(struct pst_debug_item *item, unsigned char *buf, size_t size, int col) {
+void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col) {
     struct pst_debug_file_rec_l lfile_rec;
     unsigned char rec_type;
     int index_size = 3 * sizeof(off_t);
@@ -397,33 +402,33 @@
     index[1] = 0; // valgrind, avoid writing uninitialized data
     index[2] = 0; // ""
     index_pos = ftell(debug_fp);
-    fwrite(index, index_size, 1, debug_fp);
+    pst_debug_fwrite(index, index_size, 1, debug_fp);
     index[1] = ftell(debug_fp);
 
     // always use the long
     rec_type = 'L';
-    fwrite(&rec_type, 1, sizeof(char), debug_fp);
+    pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
     lfile_rec.funcname = 0;
     lfile_rec.filename = strlen(item->function)+1;
     lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
     lfile_rec.end  = 0; // valgrind, avoid writing uninitialized data
     lfile_rec.line = item->line;
     lfile_rec.type = item->type;
-    fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+    pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
 
     file_pos = ftell(debug_fp);
-    fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
-    fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
+    pst_debug_fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
+    pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
 
     pst_debug_hexdumper(debug_fp, buf, size, col, 0);
-    fwrite(&zero, 1, 1, debug_fp);
+    pst_debug_fwrite(&zero, 1, 1, debug_fp);
     lfile_rec.end = ftell(debug_fp) - file_pos;
 
     index[2] = ftell(debug_fp);
     fseek(debug_fp, index_pos, SEEK_SET);
-    fwrite(index, index_size, 1, debug_fp);
-    fwrite(&rec_type, 1, sizeof(char), debug_fp);
-    fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+    pst_debug_fwrite(index, index_size, 1, debug_fp);
+    pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
+    pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
     fseek(debug_fp, 0, SEEK_END);
 }
 
--- a/src/define.h	Fri May 16 09:06:17 2008 -0700
+++ b/src/define.h	Thu May 29 18:51:02 2008 -0700
@@ -80,18 +80,19 @@
 #endif
 
 
-void pst_debug(char *fmt, ...);
-void pst_debug_hexdumper(FILE* out, unsigned char* buf, size_t size, int col, int delta);
+void pst_debug(const char *fmt, ...);
+void pst_debug_hexdumper(FILE* out, char* buf, size_t size, int col, int delta);
 void pst_debug_hexprint(char *data, int size);
 
-void pst_debug_init(char *fname);
-void pst_debug_msg_info (int line, char *file, int type);
-void pst_debug_msg_text(char* fmt, ...);
-void pst_debug_hexdump(unsigned char *x, size_t y, int cols, int delta);
-void pst_debug_func(char *function);
+void pst_debug_init(const char *fname);
+void pst_debug_msg_info (int line, const char *file, int type);
+void pst_debug_msg_text(const char* fmt, ...);
+void pst_debug_hexdump(char *x, size_t y, int cols, int delta);
+void pst_debug_func(const char *function);
 void pst_debug_func_ret();
 void pst_debug_close(void);
 void pst_debug_write();
+size_t pst_debug_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
 
 void * xmalloc(size_t size);
 
@@ -131,7 +132,7 @@
 #ifdef DEBUG_MODE_EMAIL
 #define DEBUG_EMAIL(x) MESSAGEPRINT(x, DEBUG_EMAIL_NO);
 #define DEBUG_EMAIL_HEXPRINT(x,y) {pst_debug_msg_info(__LINE__, __FILE__, 11);\
-                                   pst_debug_hexdump(x, y, 0x10, 0);}
+                                   pst_debug_hexdump((char*)x, y, 0x10, 0);}
 #else
 #define DEBUG_EMAIL(x) {}
 #define DEBUG_EMAIL_HEXPRINT(x,y) {}
@@ -176,10 +177,10 @@
 #ifdef DEBUG_MODE_HEXDUMP
 #define DEBUG_HEXDUMP(x, s)\
   {pst_debug_msg_info(__LINE__, __FILE__, DEBUG_HEXDUMP_NO);\
-   pst_debug_hexdump(x, s, 0x10, 0);}
+   pst_debug_hexdump((char*)x, s, 0x10, 0);}
 #define DEBUG_HEXDUMPC(x, s, c)\
   {pst_debug_msg_info(__LINE__, __FILE__, DEBUG_HEXDUMP_NO);\
-   pst_debug_hexdump(x, s, c, 0);}
+   pst_debug_hexdump((char*)x, s, c, 0);}
 #else
 #define DEBUG_HEXDUMP(x, s) {}
 #define DEBUG_HEXDUMPC(x, s, c) {}
--- a/src/deltasearch.cpp	Fri May 16 09:06:17 2008 -0700
+++ b/src/deltasearch.cpp	Thu May 29 18:51:02 2008 -0700
@@ -55,14 +55,14 @@
 		struct stat st;
 		fstat(fd, &st);
 		off_t size = st.st_size;
-		unsigned char buf[size];
+		char buf[size];
 		size_t s = read(fd, buf, size);
 		pst_debug_hexdumper(stdout, buf, s, 16, 0);
 		cout << endl;
 		cout << endl;
 		cout << " dump decrypted data " << endl;
 		for (off_t i=0; i<size; i++) {
-			buf[i] = comp_enc[buf[i]];
+			buf[i] = comp_enc[(unsigned char)buf[i]];
 		}
 		pst_debug_hexdumper(stdout, buf, s, 16, 0);
 		close(fd);
--- a/src/dumpblocks.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/dumpblocks.c	Thu May 29 18:51:02 2008 -0700
@@ -6,81 +6,82 @@
 #include "libpst.h"
 
 #define OUT_BUF 20
-int main(int argc, char **argv) {
-  pst_file pstfile;
-  pst_index_ll *ptr;
-  char *outdir=NULL, *file=NULL, *outname=NULL;
-  char *buf=NULL;
-  int c;
-  FILE *fp;
+int main(int argc, char **argv)
+{
+    pst_file pstfile;
+    pst_index_ll *ptr;
+    char *outdir = NULL, *file = NULL, *outname = NULL;
+    char *buf = NULL;
+    int c;
+    FILE *fp;
 
-  while ((c=getopt(argc, argv, "o:"))!=-1) {
-    switch(c) {
-    case 'o':
-      outdir=optarg;
-      break;
-    default:
-      printf("Unknown switch %c\n", c);
+    while ((c = getopt(argc, argv, "o:")) != -1) {
+        switch (c) {
+            case 'o':
+                outdir = optarg;
+                break;
+            default:
+                printf("Unknown switch %c\n", c);
+        }
     }
-  }
-  if (optind < argc) {
-    file = argv[optind];
-  } else {
-    printf("Usage: dumpblocks [options] pstfile\n");
-    printf("\tcopies the datablocks from the pst file into seperate files\n");
-    printf("Options: \n");
-    printf("\t-o target\tSpecify the output directory\n");
-    exit(1);
-  }
-  DEBUG_INIT("dumpblocks.log");
-  DEBUG_REGISTER_CLOSE();
-  DEBUG_ENT("main");
+    if (optind < argc) {
+        file = argv[optind];
+    } else {
+        printf("Usage: dumpblocks [options] pstfile\n");
+        printf("\tcopies the datablocks from the pst file into seperate files\n");
+        printf("Options: \n");
+        printf("\t-o target\tSpecify the output directory\n");
+        exit(1);
+    }
+    DEBUG_INIT("dumpblocks.log");
+    DEBUG_REGISTER_CLOSE();
+    DEBUG_ENT("main");
 
-  printf("Opening file %s\n",file);
-  if (pst_open(&pstfile, file)) {
-    printf("Failed to open file %s\n", file);
-    exit(1);
-  }
-
-  printf("Reading Indexes\n");
-  if (pst_load_index(&pstfile)) {
-    printf("Failed to load indexes in file %s\n", argv[1]);
-    exit(1);
-   }
-
-  if (outdir != NULL)
-    if (chdir(outdir)) {
-      printf("Failed to change into directory %s\n", outdir);
-      exit(1);
+    printf("Opening file %s\n", file);
+    if (pst_open(&pstfile, file)) {
+        printf("Failed to open file %s\n", file);
+        exit(1);
     }
 
-  ptr = pstfile.i_head;
-  outname = (char*) xmalloc(OUT_BUF);
-  printf("Saving blocks\n");
-  while (ptr != NULL) {
-    /*    if (pstfile.encryption == PST_ENC) {
-      c = pst_ff_getIDblock_dec(&pstfile, ptr->id, buf);
-      } else {*/
-    if ((ptr->id & 0x02)==0 && pstfile.encryption == PST_ENC) {
-      c = pst_ff_getIDblock_dec(&pstfile, ptr->id, &buf);
-    } else {
-      c = pst_ff_getIDblock(&pstfile, ptr->id, &buf);
+    printf("Reading Indexes\n");
+    if (pst_load_index(&pstfile)) {
+        printf("Failed to load indexes in file %s\n", argv[1]);
+        exit(1);
     }
 
-    if (c > 0) {
-      snprintf(outname, OUT_BUF, "%llx", ptr->id);
-      if ((fp = fopen(outname, "wb")) == NULL) {
-	printf("Failed to open file %s\n", outname);
-	continue;
-      }
-      fwrite(buf, 1, c, fp);
-      fclose(fp);
-    } else {
-      printf("Failed to read block id %#llx\n", ptr->id);
+    if (outdir != NULL)
+        if (chdir(outdir)) {
+            printf("Failed to change into directory %s\n", outdir);
+            exit(1);
+        }
+
+    ptr = pstfile.i_head;
+    outname = (char *) xmalloc(OUT_BUF);
+    printf("Saving blocks\n");
+    while (ptr != NULL) {
+        /*    if (pstfile.encryption == PST_ENC) {
+           c = pst_ff_getIDblock_dec(&pstfile, ptr->id, buf);
+           } else { */
+        if ((ptr->id & 0x02) == 0 && pstfile.encryption == PST_ENC) {
+            c = pst_ff_getIDblock_dec(&pstfile, ptr->id, &buf);
+        } else {
+            c = pst_ff_getIDblock(&pstfile, ptr->id, &buf);
+        }
+
+        if (c > 0) {
+            snprintf(outname, OUT_BUF, "%llx", ptr->id);
+            if ((fp = fopen(outname, "wb")) == NULL) {
+                printf("Failed to open file %s\n", outname);
+                continue;
+            }
+            pst_fwrite(buf, 1, c, fp);
+            fclose(fp);
+        } else {
+            printf("Failed to read block id %#llx\n", ptr->id);
+        }
+        ptr = ptr->next;
     }
-    ptr = ptr->next;
-  }
-  pst_close(&pstfile);
-  DEBUG_RET();
-  return 0;
+    pst_close(&pstfile);
+    DEBUG_RET();
+    return 0;
 }
--- a/src/generic.c	Fri May 16 09:06:17 2008 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,110 +0,0 @@
-
-
-#include <ctype.h>
-#include <errno.h>
-#include <malloc.h>
-#include <signal.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "generic.h"
-
-#ifdef WITH_DMALLOC
-#include <dmalloc.h>
-#endif
-
-
-
-
-void pDIE( char *fmt, ... )
-{
-    va_list ap;
-    va_start( ap, fmt );
-        //fprintf( stderr, "Fatal error (will segfault): ");
-    vfprintf( stderr, fmt, ap );
-    fprintf( stderr, "\n" );
-    va_end(ap);
-        raise( SIGSEGV );
-}
-
-void pWARN( char *fmt, ... )
-{
-    va_list ap;
-    va_start( ap, fmt );
-        fprintf( stderr, "WARNING: ");
-    vfprintf( stderr, fmt, ap );
-    fprintf( stderr, "\n" );
-    va_end(ap);
-}
-
-void *F_MALLOC( size_t size )
-{
-    void *result;
-
-    result = malloc( size );
-    ASSERT( NULL != result, "malloc() failure." );
-
-    return result;
-}
-
-void *F_REALLOC( void *p, size_t size )
-{
-    void *result;
-
-    //if( NULL != p ) hexdump((char*)p - 128, 0, 128, 1 );
-    if(!p) {
-        ASSERT( NULL != ( result = malloc( size ) ), "malloc() failure." );
-    }
-    else {
-        ASSERT( NULL != ( result = realloc( p, size ) ), "realloc() failure." );
-    }
-
-    //hexdump((char*)result - 128, 0, 128, 1 );
-    fflush(stderr);
-    return result;
-}
-
-
-
-int DEBUG_LEVEL = DB_INFO;
-
-void db_default( char *file, int line, int level, char *fmt, ... )
-{
-    va_list ap;
-    if( level <= DEBUG_LEVEL ) {
-        switch( level ) {
-            case DB_CRASH:
-                fprintf(stderr, "CRASH");
-                break;
-            case DB_ERR:
-                fprintf(stderr, "ERROR");
-                break;
-            case DB_WARN:
-                fprintf(stderr, "WARNING");
-                break;
-            case DB_INFO:
-            case DB_VERB:
-                break;
-            default:
-                fprintf(stderr, "DEBUG(%d)", level );
-        }
-
-        if( level <= DB_WARN )
-            fprintf(stderr, " (%s:%d)", file, line );
-
-        if( DB_INFO != level && DB_VERB != level )
-            fprintf(stderr, ": ");
-
-        va_start( ap, fmt );
-        vfprintf(stderr, fmt, ap );
-        fprintf(stderr, "\n" );
-        va_end( ap );
-    }
-}
-
-void (*dbfunc)(char *file, int line, int level, char *fmt, ...) = &db_default;
-
-
-
-
--- a/src/generic.h	Fri May 16 09:06:17 2008 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/* {{{ Generic.h - thigns every program does:
- *
- * - user output (log, debug, etc)
- * - crash and burn
- * - allocate memory (or explode)
- * }}} */
-#ifndef GENERIC_H
-#define GENERIC_H
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-/***************************************************/
-
-#define LOAD_DEBUG 1
-
-#define DIE(...) { fprintf(stderr, "Fatal Error at %s,%d: ", __FILE__, __LINE__); pDIE(__VA_ARGS__); }
-
-//#define WARN(...) { fprintf(stderr, "WARN: %s,%d: ", __FILE__, __LINE__); pWARN(__VA_ARGS__); }
-void pDIE( char *fmt, ... );
-//void pWARN( char *fmt, ... );
-
-#define WARN(...) DB( DB_WARN, __VA_ARGS__ )
-#define ASSERT(x,...) { if( !(x) ) DIE( __VA_ARGS__ ); }
-
-void *F_MALLOC( size_t size );
-void *F_REALLOC( void *p, size_t size );
-
-#define DO_DEBUG 0
-#define DEBUG(x) if( DO_DEBUG ) { x; }
-#define STUPID_CR "\r\n"
-
-#define DB_CRASH   0 // crashing
-#define DB_ERR     1 // error
-#define DB_WARN    2 // warning
-#define DB_INFO    3 // normal, but significant, condition
-#define DB_VERB	   4 // verbose information
-#define DB_0       5 // debug-level message
-#define DB_1       6 // debug-level message
-#define DB_2       7 // debug-level message
-
-extern int DEBUG_LEVEL;
-extern void (*dbfunc)(char *file, int line, int level, char *fmt, ...);
-
-#define DB(...) { dbfunc( __FILE__, __LINE__, __VA_ARGS__ ); }
-
-int set_db_function( void (*func)( char *file, int line, int level, char *fmt, ...) );
-
-#endif
--- a/src/getidblock.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/getidblock.c	Thu May 29 18:51:02 2008 -0700
@@ -15,126 +15,128 @@
 
 static void usage();
 
-int main(int argc, char ** argv) {
-  // pass the id number to display on the command line
-  char *fname, *sid;
-  pst_file pstfile;
-  unsigned int id;
-  int decrypt = 0, process = 0, binary = 0, c;
-  char *buf = NULL;
-  size_t readSize;
-  pst_item *item;
-  pst_desc_ll* ptr;
+int main(int argc, char **argv)
+{
+    // pass the id number to display on the command line
+    char *fname, *sid;
+    pst_file pstfile;
+    unsigned int id;
+    int decrypt = 0, process = 0, binary = 0, c;
+    char *buf = NULL;
+    size_t readSize;
+    pst_item *item;
+    pst_desc_ll *ptr;
 
-  DEBUG_INIT("getidblock.log");
-  DEBUG_REGISTER_CLOSE();
-  DEBUG_ENT("main");
+    DEBUG_INIT("getidblock.log");
+    DEBUG_REGISTER_CLOSE();
+    DEBUG_ENT("main");
 
-  while ((c = getopt(argc, argv, "bdp")) != -1) {
-	switch (c) {
-	case 'b':
-	  // enable binary output
-	  binary = 1;
-	  break;
-	case 'd':
-	  //enable decrypt
-	  decrypt = 1;
-	  break;
-	case 'p':
-	  // enable procesing of block
-	  process = 1;
-	  break;
-	default:
-	  usage();
-	  exit(EXIT_FAILURE);
-	}
-  }
+    while ((c = getopt(argc, argv, "bdp")) != -1) {
+        switch (c) {
+            case 'b':
+                // enable binary output
+                binary = 1;
+                break;
+            case 'd':
+                //enable decrypt
+                decrypt = 1;
+                break;
+            case 'p':
+                // enable procesing of block
+                process = 1;
+                break;
+            default:
+                usage();
+                exit(EXIT_FAILURE);
+        }
+    }
 
-  if (optind+1 >= argc) {
-	// no more items on the cmd
-	usage();
-	exit(EXIT_FAILURE);
-  }
-  fname = argv[optind];
-  sid = argv[optind+1];
-  id = (unsigned int)strtol(sid, NULL, 0);
+    if (optind + 1 >= argc) {
+        // no more items on the cmd
+        usage();
+        exit(EXIT_FAILURE);
+    }
+    fname = argv[optind];
+    sid = argv[optind + 1];
+    id = (unsigned int) strtol(sid, NULL, 0);
 
-  DEBUG_MAIN(("Opening file\n"));
-  memset(&pstfile, 0, sizeof(pstfile));
-  if (pst_open(&pstfile, fname)) {
-	DIE(("Error opening file\n"));
-  }
-
-  DEBUG_MAIN(("Loading Index\n"));
-  if (pst_load_index(&pstfile) != 0) {
-	DIE(("Error loading file index\n"));
-  }
+    DEBUG_MAIN(("Opening file\n"));
+    memset(&pstfile, 0, sizeof(pstfile));
+    if (pst_open(&pstfile, fname)) {
+        DIE(("Error opening file\n"));
+    }
 
-  //  if ((ptr = pst_getID(&pstfile, id)) == NULL) {
-  //	DIE(("id not found [%#x]\n", id));
-  //  }
-
-  DEBUG_MAIN(("Loading block\n"));
+    DEBUG_MAIN(("Loading Index\n"));
+    if (pst_load_index(&pstfile) != 0) {
+        DIE(("Error loading file index\n"));
+    }
+    //  if ((ptr = pst_getID(&pstfile, id)) == NULL) {
+    //    DIE(("id not found [%#x]\n", id));
+    //  }
 
-  if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) {
-	//	if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) {
-	DIE(("Error loading block\n"));
-  }
-  if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize);
+    DEBUG_MAIN(("Loading block\n"));
 
-  if (decrypt!=0)
-	if (pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) {
-	  DIE(("Error decrypting block\n"));
-	}
+    if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) {
+        //      if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) {
+        DIE(("Error loading block\n"));
+    }
+    if (binary == 0)
+        printf("Block %#x, size %#x[%i]\n", id, (unsigned int) readSize, (int) readSize);
 
-  DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
-  if (binary==0) {
-	pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
-  } else {
-	if (fwrite(buf, 1, readSize, stdout) != 0) {
-	  DIE(("Error occured during writing of buf to stdout\n"));
-	}
-  }
-  free(buf);
+    if (decrypt != 0)
+        if (pst_decrypt(buf, readSize, (int) pstfile.encryption) != 0) {
+            DIE(("Error decrypting block\n"));
+        }
 
-  if (process!=0) {
-	DEBUG_MAIN(("Parsing block...\n"));
-	ptr = pstfile.d_head;
-	while(ptr != NULL) {
-	  if (ptr->list_index != NULL && ptr->list_index->id == id)
-	break;
-	  if (ptr->desc != NULL && ptr->desc->id == id)
-	break;
-	  ptr = pst_getNextDptr(ptr);
-	}
-	if (ptr == NULL) {
-	  ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll));
-	  ptr->desc = pst_getID(&pstfile, id);
-	  ptr->list_index = NULL;
-	}
-	if (ptr != NULL) {
-	  if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
-	pst_freeItem(item);
-	} else {
-	  DEBUG_MAIN(("item not found with this ID\n"));
-	  printf("Cannot find the owning Record of this ID. Cannot parse\n");
-	}
-  }
+    DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
+    if (binary == 0) {
+        pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
+    } else {
+        if (fwrite(buf, 1, readSize, stdout) != 0) {
+            DIE(("Error occured during writing of buf to stdout\n"));
+        }
+    }
+    free(buf);
 
-  if(pst_close(&pstfile)!=0) {
-	DIE(("pst_close failed\n"));
-  }
+    if (process != 0) {
+        DEBUG_MAIN(("Parsing block...\n"));
+        ptr = pstfile.d_head;
+        while (ptr != NULL) {
+            if (ptr->list_index != NULL && ptr->list_index->id == id)
+                break;
+            if (ptr->desc != NULL && ptr->desc->id == id)
+                break;
+            ptr = pst_getNextDptr(ptr);
+        }
+        if (ptr == NULL) {
+            ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
+            ptr->desc = pst_getID(&pstfile, id);
+            ptr->list_index = NULL;
+        }
+        if (ptr != NULL) {
+            if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
+                pst_freeItem(item);
+        } else {
+            DEBUG_MAIN(("item not found with this ID\n"));
+            printf("Cannot find the owning Record of this ID. Cannot parse\n");
+        }
+    }
 
-  DEBUG_RET();
-  return 0;
+    if (pst_close(&pstfile) != 0) {
+        DIE(("pst_close failed\n"));
+    }
+
+    DEBUG_RET();
+    return 0;
 }
 
-void usage() {
-  printf("usage: getidblock [options] filename id\n");
-  printf("\tfilename - name of the file to access\n");
-  printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
-  printf("\toptions\n");
-  printf("\t\t-d\tDecrypt the block before printing\n");
-  printf("\t\t-p\tProcess the block before finishing.\n");
-  printf("\t\t\tView the debug log for information\n");
+void usage()
+{
+    printf("usage: getidblock [options] filename id\n");
+    printf("\tfilename - name of the file to access\n");
+    printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
+    printf("\toptions\n");
+    printf("\t\t-d\tDecrypt the block before printing\n");
+    printf("\t\t-p\tProcess the block before finishing.\n");
+    printf("\t\t\tView the debug log for information\n");
 }
--- a/src/libpst.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/libpst.c	Thu May 29 18:51:02 2008 -0700
@@ -869,14 +869,16 @@
                 cache_ptr->prev = NULL;
                 cache_ptr->next = cache_head;
                 cache_ptr->ptr  = parent;
+                if (cache_head)  cache_head->prev = cache_ptr;
+                if (!cache_tail) cache_tail = cache_ptr;
                 cache_head = cache_ptr;
-                if (!cache_tail) cache_tail = cache_ptr;
                 cache_count++;
                 if (cache_count > 100) {
                     DEBUG_INDEX(("trimming quick cache\n"));
                     //remove one from the end
                     cache_ptr  = cache_tail;
                     cache_tail = cache_ptr->prev;
+                    if (cache_tail) cache_tail->next = NULL;
                     free (cache_ptr);
                     cache_count--;
                 }
@@ -1291,11 +1293,11 @@
     int32_t  cur_list;
     int      block_type;
     uint32_t rec_size = 0;
-    unsigned char* list_start;
-    unsigned char* fr_ptr;
-    unsigned char* to_ptr;
-    unsigned char* ind2_end = NULL;
-    unsigned char* ind2_ptr = NULL;
+    char*    list_start;
+    char*    fr_ptr;
+    char*    to_ptr;
+    char*    ind2_end = NULL;
+    char*    ind2_ptr = NULL;
     pst_x_attrib_ll *mapptr;
     pst_block_hdr    block_hdr;
     pst_table3_rec   table3_rec;  //for type 3 (0x0101) blocks
@@ -1517,7 +1519,7 @@
 
         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
+            char* value_pointer = NULL;     // needed for block type 2 with values larger than 4 bytes
             size_t value_size = 0;
             if (block_type == 1) {
                 memcpy(&table_rec, fr_ptr, sizeof(table_rec));
@@ -1631,6 +1633,7 @@
                        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)0x101f ||
                        table_rec.ref_type == (uint16_t)0x1102) {
                 //contains index reference to data
                 LE32_CPU(table_rec.value);
@@ -3562,6 +3565,11 @@
                             list->items[x]->size));
                         DEBUG_HEXDUMP(list->items[x]->data, list->items[x]->size);
 
+                    } else if (list->items[x]->type == (uint32_t)0x101F) {
+                        DEBUG_EMAIL(("Unknown type %#x Array of Unicode Strings [size = %#x]\n", list->items[x]->id,
+                            list->items[x]->size));
+                        DEBUG_HEXDUMP(list->items[x]->data, list->items[x]->size);
+
                     } else if (list->items[x]->type == (uint32_t)0x1102) {
                         DEBUG_EMAIL(("Unknown type %#x Array of binary data blobs [size = %#x]\n", list->items[x]->id,
                             list->items[x]->size));
@@ -4184,7 +4192,7 @@
 }
 
 
-int pst_decrypt(unsigned char *buf, size_t size, unsigned char type) {
+int pst_decrypt(char *buf, size_t size, unsigned char type) {
     size_t x = 0;
     unsigned char y;
     DEBUG_ENT("pst_decrypt");
@@ -4196,9 +4204,9 @@
     if (type == PST_COMP_ENCRYPT) {
         x = 0;
         while (x < size) {
-            y = buf[x];
+            y = (unsigned char)(buf[x]);
             DEBUG_DECRYPT(("Transposing %#hhx to %#hhx [%#x]\n", buf[x], comp_enc[y], y));
-            buf[x] = comp_enc[y]; // transpose from encrypt array
+            buf[x] = (char)comp_enc[y]; // transpose from encrypt array
             x++;
         }
     } else {
@@ -4392,7 +4400,7 @@
 size_t pst_ff_compile_ID(pst_file *pf, uint64_t id, pst_holder *h, size_t size) {
     size_t z, a, b;
     uint16_t count, y;
-    char * buf3 = NULL, *buf2 = NULL, *t;
+    char *buf3 = NULL, *buf2 = NULL, *t;
     char *b_ptr;
     pst_block_hdr  block_hdr;
     pst_table3_rec table3_rec;  //for type 3 (0x0101) blocks
@@ -4550,7 +4558,7 @@
 }
 
 
-size_t pst_fwrite(const void*ptr, size_t size, size_t nmemb, FILE*stream) {
+size_t pst_fwrite(const void* ptr, size_t size, size_t nmemb, FILE *stream) {
     size_t r;
     DEBUG_ENT("pst_fwrite");
     if (ptr)
--- a/src/libpst.h	Fri May 16 09:06:17 2008 -0700
+++ b/src/libpst.h	Thu May 29 18:51:02 2008 -0700
@@ -597,7 +597,7 @@
 pst_index_ll*  pst_getID2(pst_index2_ll * ptr, uint64_t id);
 pst_desc_ll*   pst_getDptr(pst_file *pf, uint64_t id);
 size_t         pst_read_block_size(pst_file *pf, off_t offset, size_t size, char **buf);
-int            pst_decrypt(unsigned char *buf, size_t size, unsigned char type);
+int            pst_decrypt(char *buf, size_t size, unsigned char type);
 uint64_t       pst_getIntAt(pst_file *pf, char *buf);
 uint64_t       pst_getIntAtPos(pst_file *pf, off_t pos);
 size_t         pst_getAtPos(pst_file *pf, off_t pos, void* buf, size_t size);
--- a/src/lspst.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/lspst.c	Thu May 29 18:51:02 2008 -0700
@@ -270,7 +270,7 @@
         DEBUG_RET();
         return;
     }
-    while (fname = strpbrk(fname, "/\\:"))
+    while ((fname = strpbrk(fname, "/\\:")))
         *fname = '_';
     DEBUG_RET();
 }
--- a/src/lzfu.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/lzfu.c	Thu May 29 18:51:02 2008 -0700
@@ -40,7 +40,7 @@
 } lzfuheader;
 
 
-unsigned char* lzfu_decompress (unsigned char* rtfcomp, uint32_t compsize, size_t *size) {
+char* lzfu_decompress (char* rtfcomp, uint32_t compsize, size_t *size) {
 	// the dictionary buffer
 	unsigned char dict[4096];
 	// the dictionary pointer
@@ -52,7 +52,7 @@
 	// temp value for determining the bits in the flag
 	unsigned char flag_mask;
 	uint32_t i;
-	unsigned char *out_buf;
+	char *out_buf;
 	uint32_t out_ptr  = 0;
 	uint32_t out_size;
 	uint32_t in_ptr;
@@ -71,11 +71,11 @@
 	//printf("CRC       : %#x\n", lzfuhdr.dwCRC);
 	//printf("\n");
 	out_size = lzfuhdr.cbRawSize + 3;	// two braces and a null terminator
-	out_buf  = (unsigned char*)xmalloc(out_size);
+	out_buf  = (char*)xmalloc(out_size);
 	in_ptr	 = sizeof(lzfuhdr);
 	in_size  = (lzfuhdr.cbSize < compsize) ? lzfuhdr.cbSize : compsize;
 	while (in_ptr < in_size) {
-		flags = rtfcomp[in_ptr++];
+		flags = (unsigned char)(rtfcomp[in_ptr++]);
 		flag_mask = 1;
 		while (flag_mask) {
 			if (flag_mask & flags) {
@@ -99,7 +99,7 @@
 						c1 = dict[(offset+i)%4096];
 						dict[dict_length]=c1;
 						dict_length = (dict_length+1) % 4096;
-						if (out_ptr < out_size) out_buf[out_ptr++] = c1;
+						if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1;
 					}
 				}
 			} else {
@@ -109,7 +109,7 @@
 					char c1 = rtfcomp[in_ptr++];
 					dict[dict_length] = c1;
 					dict_length = (dict_length+1)%4096;
-					if (out_ptr < out_size) out_buf[out_ptr++] = c1;
+					if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1;
 				}
 			}
 			flag_mask <<= 1;
--- a/src/lzfu.h	Fri May 16 09:06:17 2008 -0700
+++ b/src/lzfu.h	Thu May 29 18:51:02 2008 -0700
@@ -3,6 +3,6 @@
 
 #include <stdint.h>
 
-unsigned char* lzfu_decompress (unsigned char* rtfcomp, uint32_t compsize, size_t *size);
+char* lzfu_decompress (char* rtfcomp, uint32_t compsize, size_t *size);
 
 #endif
--- a/src/pst2dii.cpp.in	Fri May 16 09:06:17 2008 -0700
+++ b/src/pst2dii.cpp.in	Thu May 29 18:51:02 2008 -0700
@@ -40,12 +40,12 @@
 };
 
 // global settings
-char*    convert = "@CONVERT@";     // fully qualified path of the convert program from image magick
-char*    prog_name = NULL;          // our arg0 name
-char*    bates_prefix = "";         // string to prefix bates numbers
-int      bates_index = 0;           // current bates sequence
-char*    output_directory = ".";
-char*    output_file = "load.dii";
+const char*    convert = "@CONVERT@";     // fully qualified path of the convert program from image magick
+const char*    prog_name = NULL;          // our arg0 name
+const char*    bates_prefix = "";         // string to prefix bates numbers
+int            bates_index = 0;           // current bates sequence
+const char*    output_directory = ".";
+const char*    output_file = "load.dii";
 char*    font_file = NULL;
 int      bates_color = 0xff0000;    // color of bates header stamp
 int      email_sequence = 0;        // current pdf sequence number
@@ -164,7 +164,7 @@
     }
     char *tt = t;
     bool fixed = false;
-    while (t = strpbrk(t, " /\\:")) {
+    while ((t = strpbrk(t, " /\\:"))) {
         // while there are characters in the second string that we don't want
         *t = '_'; //replace them with an underscore
         fixed = true;
@@ -214,7 +214,7 @@
         WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
     } else {
         if (current_attach->data)
-            fwrite(current_attach->data, 1, current_attach->size, fp);
+            pst_fwrite(current_attach->data, 1, current_attach->size, fp);
         else {
             (void)pst_attach_to_file(pst, current_attach, fp);
         }
@@ -227,8 +227,8 @@
 }
 
 
-static void print_pdf_short(char *line, int len, int color);
-static void print_pdf_short(char *line, int len, int color)
+static void print_pdf_short(const char *line, int len, int color);
+static void print_pdf_short(const char *line, int len, int color)
 {
     if (line_number >= line_max) {
         close_png();
@@ -241,23 +241,23 @@
     strex.charmap     = 0;
     strex.hdpi        = DPI;
     strex.vdpi        = DPI;
-    char x = line[len];
-    line[len] = '\0';
+    char xline[len+1];
+    memcpy(xline, line, len);
+    xline[len] = '\0';
     char *p;
-    char *l = line;
-    while (p = strchr(l, '&')) {
+    char *l = xline;
+    while ((p = strchr(l, '&'))) {
         *p = '\0';
         char *err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, l, &strex);
         if (err) printf(err);
         x_position += (brect[2]-brect[6]);
         l = p+1;
-        err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, "&amp;", &strex);
+        err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, (char*)"&amp;", &strex);
         if (err) printf(err);
         x_position += (brect[2]-brect[6]);
     }
     char *err = gdImageStringFTEx(image, &brect[0], color, font_file, sz, 0.0, x_position, y_position, l, &strex);
     if (err) printf(err);
-    line[len] = x;
     x_position += (brect[2]-brect[6]);
     col_number += len;
 }
@@ -273,8 +273,8 @@
 }
 
 
-static void print_pdf_single(char *line, int color);
-static void print_pdf_single(char *line, int color)
+static void print_pdf_single(const char *line, int color);
+static void print_pdf_single(const char *line, int color)
 {
     while (*line == '\t') {
         char blanks[5];
@@ -295,11 +295,11 @@
 }
 
 
-static void print_pdf_only(char *line, int color);
-static void print_pdf_only(char *line, int color)
+static void print_pdf_only(const char *line, int color);
+static void print_pdf_only(const char *line, int color)
 {
     char *p;
-    while (p = strchr(line, '\n')) {
+    while ((p = strchr(line, '\n'))) {
         *p = '\0';
         print_pdf_single(line, color);
         *p = '\n';
@@ -310,10 +310,10 @@
 }
 
 
-static void print_pdf(char *line);
-static void print_pdf(char *line)
+static void print_pdf(const char *line);
+static void print_pdf(const char *line)
 {
-    fwrite(line, 1, strlen(line), dii_file);
+    pst_fwrite(line, 1, strlen(line), dii_file);
     print_pdf_only(line, black);
 }
 
@@ -324,7 +324,7 @@
         png_open  = true;
         int brect[8];
         image = gdImageCreate(PAGE_WIDTH, PAGE_HEIGHT);
-        int w = gdImageColorAllocate(image, 255, 255, 255); // background color first one allocated
+                gdImageColorAllocate(image, 255, 255, 255); // background color first one allocated
         black = gdImageColorAllocate(image, 0, 0, 0);
         int r = (bates_color & 0xff0000) >> 16;
         int g = (bates_color & 0x00ff00) >> 8;
@@ -339,7 +339,7 @@
         strex.vdpi        = DPI;
 
         char line[LINE_SIZE];
-        char *err = gdImageStringFTEx(NULL, &brect[0], black, font_file, sz, 0.0, margin, margin, "LMgqQ", &strex);
+        char *err = gdImageStringFTEx(NULL, &brect[0], black, font_file, sz, 0.0, margin, margin, (char*)"LMgqQ", &strex);
         if (err) printf(err);
         line_height = (brect[3]-brect[7]) * 12/10;
         char_width  = (brect[2]-brect[6]) / 5;
@@ -401,22 +401,22 @@
 }
 
 
-static void write_simple(char *tag, const char *value);
-static void write_simple(char *tag, const char *value)
+static void write_simple(const char *tag, const char *value);
+static void write_simple(const char *tag, const char *value)
 {
     if (value) fprintf(dii_file, "@%s %s\n", tag, value);
 }
 
 
-static void write_simple(char *tag, string value);
-static void write_simple(char *tag, string value)
+static void write_simple(const char *tag, string value);
+static void write_simple(const char *tag, string value)
 {
     fprintf(dii_file, "@%s %s\n", tag, value.c_str());
 }
 
 
-static void write_simple(char *tag, const char *value, const char *value2);
-static void write_simple(char *tag, const char *value, const char *value2)
+static void write_simple(const char *tag, const char *value, const char *value2);
+static void write_simple(const char *tag, const char *value, const char *value2)
 {
     if (value) {
     if (value2) fprintf(dii_file, "@%s \"%s\" <%s>\n", tag, value, value2);
@@ -425,8 +425,8 @@
 }
 
 
-static string extract_header(char *headers, char *field);
-static string extract_header(char *headers, char *field)
+static string extract_header(char *headers, const char *field);
+static string extract_header(char *headers, const char *field)
 {
     string rc;
     int len = strlen(field) + 4;
@@ -530,7 +530,7 @@
 
     } else {
         //make up our own headers
-        char *temp = item->email->outlook_sender;
+        const char *temp = item->email->outlook_sender;
         if (!temp) temp = "";
         snprintf(line, sizeof(line), "From: \"%s\" <%s>\n", item->email->outlook_sender_name, temp);
         print_pdf(line);
--- a/src/pst2ldif.cpp	Fri May 16 09:06:17 2008 -0700
+++ b/src/pst2ldif.cpp	Thu May 29 18:51:02 2008 -0700
@@ -14,40 +14,40 @@
 #include <set>
 
 extern "C" {
-	#include "define.h"
-	#include "libstrfunc.h"
-	#include "libpst.h"
-	#include "common.h"
-	#include "timeconv.h"
-	#include "lzfu.h"
+    #include "define.h"
+    #include "libstrfunc.h"
+    #include "libpst.h"
+    #include "common.h"
+    #include "timeconv.h"
+    #include "lzfu.h"
 }
 
 int32_t   usage();
 int32_t   version();
-char *my_stristr(char *haystack, char *needle);
-char *check_filename(char *fname);
-char *single(char *str);
-char *folded(char *str);
-void  multi(char *fmt, char *str);
-char *rfc2426_escape(char *str);
-int32_t chr_count(char *str, char x);
+char       *my_stristr(char *haystack, char *needle);
+char       *check_filename(char *fname);
+const char *single(char *str);
+const char *folded(char *str);
+void        multi(const char *fmt, char *str);
+char       *rfc2426_escape(char *str);
+int32_t     chr_count(char *str, char x);
 
 char *prog_name;
 pst_file pstfile;
-char *ldap_base  = NULL;	// 'o=some.domain.tld, c=US'
-char *ldap_class = NULL;	// 'newPerson'
-char *ldap_org	 = NULL;	// 'o=some.domain.tld', computed from ldap_base
+char *ldap_base  = NULL;    // 'o=some.domain.tld, c=US'
+char *ldap_class = NULL;    // 'newPerson'
+char *ldap_org   = NULL;    // 'o=some.domain.tld', computed from ldap_base
 
 
 ////////////////////////////////////////////////
 // define our ordering
 struct ltstr {
-	bool operator()(char* s1, char* s2) const {
-		return strcasecmp(s1, s2) < 0;
-	}
+    bool operator()(const char* s1, const char* s2) const {
+        return strcasecmp(s1, s2) < 0;
+    }
 };
 // define our set
-typedef set<char *, ltstr>	 string_set;
+typedef set<const char *, ltstr>    string_set;
 // make a static set to hold the cn values
 static string_set all_strings;
 
@@ -58,48 +58,48 @@
 static void free_strings(string_set &s);
 static void free_strings(string_set &s)
 {
-	for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
-		free(*i);
-	}
-	s.clear();
+    for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
+        free((void*)*i);
+    }
+    s.clear();
 }
 
 
 ////////////////////////////////////////////////
 // helper to register a string in a string set
 //
-static char* register_string(string_set &s, char *name);
-static char* register_string(string_set &s, char *name) {
-	string_set::iterator i = s.find(name);
-	if (i != s.end()) return *i;
-	char *x = strdup(name);
-	s.insert(x);
-	return x;
+static const char* register_string(string_set &s, const char *name);
+static const char* register_string(string_set &s, const char *name) {
+    string_set::const_iterator i = s.find(name);
+    if (i != s.end()) return *i;
+    char *x = strdup(name);
+    s.insert(x);
+    return x;
 }
 
 ////////////////////////////////////////////////
 // register a global string
 //
-static char* register_string(char *name);
-static char* register_string(char *name) {
-	return register_string(all_strings, name);
+static const char* register_string(const char *name);
+static const char* register_string(const char *name) {
+    return register_string(all_strings, name);
 }
 
 
 ////////////////////////////////////////////////
 // make a unique string
 //
-static char* unique_string(char *name);
-static char* unique_string(char *name) {
-	int  unique = 2;
-	string_set::iterator i = all_strings.find(name);
-	if (i == all_strings.end()) return register_string(name);
-	while (true) {
-		char n[strlen(name)+10];
-		snprintf(n, sizeof(n), "%s %d", name, unique++);
-		string_set::iterator i = all_strings.find(n);
-		if (i == all_strings.end()) return register_string(n);
-	}
+static const char* unique_string(const char *name);
+static const char* unique_string(const char *name) {
+    int  unique = 2;
+    string_set::iterator i = all_strings.find(name);
+    if (i == all_strings.end()) return register_string(name);
+    while (true) {
+        char n[strlen(name)+10];
+        snprintf(n, sizeof(n), "%s %d", name, unique++);
+        string_set::iterator i = all_strings.find(n);
+        if (i == all_strings.end()) return register_string(n);
+    }
 }
 
 
@@ -108,484 +108,484 @@
 //
 static char *trim(char *name);
 static char *trim(char *name) {
-	char *p;
-	while (*name == ' ') name++;
-	p = name + strlen(name) - 1;
-	while ((p >= name) && (*p == ' ')) *p-- = '\0';
-	return name;
+    char *p;
+    while (*name == ' ') name++;
+    p = name + strlen(name) - 1;
+    while ((p >= name) && (*p == ' ')) *p-- = '\0';
+    return name;
 }
 
 
 static void process(pst_desc_ll *d_ptr);
 static void process(pst_desc_ll *d_ptr) {
-	pst_item *item = NULL;
-	while (d_ptr) {
-		if (d_ptr->desc) {
-			item = pst_parse_item(&pstfile, d_ptr);
-			DEBUG_INFO(("item pointer is %p\n", item));
-			if (item) {
-				if (item->message_store) {
-					// there should only be one message_store, and we have already done it
-					DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
-				}
+    pst_item *item = NULL;
+    while (d_ptr) {
+        if (d_ptr->desc) {
+            item = pst_parse_item(&pstfile, d_ptr);
+            DEBUG_INFO(("item pointer is %p\n", item));
+            if (item) {
+                if (item->message_store) {
+                    // there should only be one message_store, and we have already done it
+                    DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
+                }
 
-				if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) {
-					//if this is a non-empty folder other than deleted items, we want to recurse into it
-					fprintf(stderr, "entering folder %s\n", item->file_as);
-					process(d_ptr->child);
-				} else if (item->contact) {
-					// deal with a contact
-					if (item->type != PST_TYPE_CONTACT) {
-						DIE(("type should be contact\n"));
-					}
-					else if (item->contact == NULL) { // this is an incorrect situation. Inform user
-						DIE(("null item contact\n"));
-					} else {
-						char cn[1000];
-						snprintf(cn, sizeof(cn), "%s %s %s %s",
-							single(item->contact->display_name_prefix),
-							single(item->contact->first_name),
-							single(item->contact->surname),
-							single(item->contact->suffix));
-						if (strcmp(cn, "   ")) {
-//							  fprintf(stderr, "\n\n\n");
-//							  fprintf(stderr, "access_method %s\n",              item->contact->access_method);
-//							  fprintf(stderr, "account_name %s\n",               item->contact->account_name);
-//							  fprintf(stderr, "address1 %s\n",                   item->contact->address1);
-//							  fprintf(stderr, "address1_desc %s\n",              item->contact->address1_desc);
-//							  fprintf(stderr, "address1_transport %s\n",         item->contact->address1_transport);
-//							  fprintf(stderr, "address2 %s\n",                   item->contact->address2);
-//							  fprintf(stderr, "address2_desc %s\n",              item->contact->address2_desc);
-//							  fprintf(stderr, "address2_transport %s\n",         item->contact->address2_transport);
-//							  fprintf(stderr, "address3 %s\n",                   item->contact->address3);
-//							  fprintf(stderr, "address3_desc %s\n",              item->contact->address3_desc);
-//							  fprintf(stderr, "address3_transport %s\n",         item->contact->address3_transport);
-//							  fprintf(stderr, "assistant_name %s\n",             item->contact->assistant_name);
-//							  fprintf(stderr, "assistant_phone %s\n",            item->contact->assistant_phone);
-//							  fprintf(stderr, "billing_information %s\n",        item->contact->billing_information);
-//							  fprintf(stderr, "business_address %s\n",           item->contact->business_address);
-//							  fprintf(stderr, "business_city %s\n",              item->contact->business_city);
-//							  fprintf(stderr, "business_country %s\n",           item->contact->business_country);
-//							  fprintf(stderr, "business_fax %s\n",               item->contact->business_fax);
-//							  fprintf(stderr, "business_homepage %s\n",          item->contact->business_homepage);
-//							  fprintf(stderr, "business_phone %s\n",             item->contact->business_phone);
-//							  fprintf(stderr, "business_phone2 %s\n",            item->contact->business_phone2);
-//							  fprintf(stderr, "business_po_box %s\n",            item->contact->business_po_box);
-//							  fprintf(stderr, "business_postal_code %s\n",       item->contact->business_postal_code);
-//							  fprintf(stderr, "business_state %s\n",             item->contact->business_state);
-//							  fprintf(stderr, "business_street %s\n",            item->contact->business_street);
-//							  fprintf(stderr, "callback_phone %s\n",             item->contact->callback_phone);
-//							  fprintf(stderr, "car_phone %s\n",                  item->contact->car_phone);
-//							  fprintf(stderr, "company_main_phone %s\n",         item->contact->company_main_phone);
-//							  fprintf(stderr, "company_name %s\n",               item->contact->company_name);
-//							  fprintf(stderr, "computer_name %s\n",              item->contact->computer_name);
-//							  fprintf(stderr, "customer_id %s\n",                item->contact->customer_id);
-//							  fprintf(stderr, "def_postal_address %s\n",         item->contact->def_postal_address);
-//							  fprintf(stderr, "department %s\n",                 item->contact->department);
-//							  fprintf(stderr, "display_name_prefix %s\n",        item->contact->display_name_prefix);
-//							  fprintf(stderr, "first_name %s\n",                 item->contact->first_name);
-//							  fprintf(stderr, "followup %s\n",                   item->contact->followup);
-//							  fprintf(stderr, "free_busy_address %s\n",          item->contact->free_busy_address);
-//							  fprintf(stderr, "ftp_site %s\n",                   item->contact->ftp_site);
-//							  fprintf(stderr, "fullname %s\n",                   item->contact->fullname);
-//							  fprintf(stderr, "gov_id %s\n",                     item->contact->gov_id);
-//							  fprintf(stderr, "hobbies %s\n",                    item->contact->hobbies);
-//							  fprintf(stderr, "home_address %s\n",               item->contact->home_address);
-//							  fprintf(stderr, "home_city %s\n",                  item->contact->home_city);
-//							  fprintf(stderr, "home_country %s\n",               item->contact->home_country);
-//							  fprintf(stderr, "home_fax %s\n",                   item->contact->home_fax);
-//							  fprintf(stderr, "home_phone %s\n",                 item->contact->home_phone);
-//							  fprintf(stderr, "home_phone2 %s\n",                item->contact->home_phone2);
-//							  fprintf(stderr, "home_po_box %s\n",                item->contact->home_po_box);
-//							  fprintf(stderr, "home_postal_code %s\n",           item->contact->home_postal_code);
-//							  fprintf(stderr, "home_state %s\n",                 item->contact->home_state);
-//							  fprintf(stderr, "home_street %s\n",                item->contact->home_street);
-//							  fprintf(stderr, "initials %s\n",                   item->contact->initials);
-//							  fprintf(stderr, "isdn_phone %s\n",                 item->contact->isdn_phone);
-//							  fprintf(stderr, "job_title %s\n",                  item->contact->job_title);
-//							  fprintf(stderr, "keyword %s\n",                    item->contact->keyword);
-//							  fprintf(stderr, "language %s\n",                   item->contact->language);
-//							  fprintf(stderr, "location %s\n",                   item->contact->location);
-//							  fprintf(stderr, "manager_name %s\n",               item->contact->manager_name);
-//							  fprintf(stderr, "middle_name %s\n",                item->contact->middle_name);
-//							  fprintf(stderr, "mileage %s\n",                    item->contact->mileage);
-//							  fprintf(stderr, "mobile_phone %s\n",               item->contact->mobile_phone);
-//							  fprintf(stderr, "nickname %s\n",                   item->contact->nickname);
-//							  fprintf(stderr, "office_loc %s\n",                 item->contact->office_loc);
-//							  fprintf(stderr, "org_id %s\n",                     item->contact->org_id);
-//							  fprintf(stderr, "other_address %s\n",              item->contact->other_address);
-//							  fprintf(stderr, "other_city %s\n",                 item->contact->other_city);
-//							  fprintf(stderr, "other_country %s\n",              item->contact->other_country);
-//							  fprintf(stderr, "other_phone %s\n",                item->contact->other_phone);
-//							  fprintf(stderr, "other_po_box %s\n",               item->contact->other_po_box);
-//							  fprintf(stderr, "other_postal_code %s\n",          item->contact->other_postal_code);
-//							  fprintf(stderr, "other_state %s\n",                item->contact->other_state);
-//							  fprintf(stderr, "other_street %s\n",               item->contact->other_street);
-//							  fprintf(stderr, "pager_phone %s\n",                item->contact->pager_phone);
-//							  fprintf(stderr, "personal_homepage %s\n",          item->contact->personal_homepage);
-//							  fprintf(stderr, "pref_name %s\n",                  item->contact->pref_name);
-//							  fprintf(stderr, "primary_fax %s\n",                item->contact->primary_fax);
-//							  fprintf(stderr, "primary_phone %s\n",              item->contact->primary_phone);
-//							  fprintf(stderr, "profession %s\n",                 item->contact->profession);
-//							  fprintf(stderr, "radio_phone %s\n",                item->contact->radio_phone);
-//							  fprintf(stderr, "spouse_name %s\n",                item->contact->spouse_name);
-//							  fprintf(stderr, "suffix %s\n",                     item->contact->suffix);
-//							  fprintf(stderr, "surname %s\n",                    item->contact->surname);
-//							  fprintf(stderr, "telex %s\n",                      item->contact->telex);
-//							  fprintf(stderr, "transmittable_display_name %s\n", item->contact->transmittable_display_name);
-//							  fprintf(stderr, "ttytdd_phone %s\n",               item->contact->ttytdd_phone);
-							// have a valid cn
-							char *ucn = unique_string(folded(trim(cn)));
-							printf("dn: cn=%s, %s\n", ucn, ldap_base);
-							printf("cn: %s\n", ucn);
-							if (item->contact->first_name) {
-								snprintf(cn, sizeof(cn), "%s %s",
-									single(item->contact->display_name_prefix),
-									single(item->contact->first_name));
-								printf("givenName: %s\n", trim(cn));
-							}
-							if (item->contact->surname) {
-								snprintf(cn, sizeof(cn), "%s %s",
-									single(item->contact->surname),
-									single(item->contact->suffix));
-								printf("sn: %s\n", trim(cn));
-							}
-							else if (item->contact->company_name) {
-								printf("sn: %s\n", single(item->contact->company_name));
-							}
-							else
-								printf("sn: %s\n", ucn);    // use cn as sn if we cannot find something better
+                if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) {
+                    //if this is a non-empty folder other than deleted items, we want to recurse into it
+                    fprintf(stderr, "entering folder %s\n", item->file_as);
+                    process(d_ptr->child);
+                } else if (item->contact) {
+                    // deal with a contact
+                    if (item->type != PST_TYPE_CONTACT) {
+                        DIE(("type should be contact\n"));
+                    }
+                    else if (item->contact == NULL) { // this is an incorrect situation. Inform user
+                        DIE(("null item contact\n"));
+                    } else {
+                        char cn[1000];
+                        snprintf(cn, sizeof(cn), "%s %s %s %s",
+                            single(item->contact->display_name_prefix),
+                            single(item->contact->first_name),
+                            single(item->contact->surname),
+                            single(item->contact->suffix));
+                        if (strcmp(cn, "   ")) {
+//                            fprintf(stderr, "\n\n\n");
+//                            fprintf(stderr, "access_method %s\n",              item->contact->access_method);
+//                            fprintf(stderr, "account_name %s\n",               item->contact->account_name);
+//                            fprintf(stderr, "address1 %s\n",                   item->contact->address1);
+//                            fprintf(stderr, "address1_desc %s\n",              item->contact->address1_desc);
+//                            fprintf(stderr, "address1_transport %s\n",         item->contact->address1_transport);
+//                            fprintf(stderr, "address2 %s\n",                   item->contact->address2);
+//                            fprintf(stderr, "address2_desc %s\n",              item->contact->address2_desc);
+//                            fprintf(stderr, "address2_transport %s\n",         item->contact->address2_transport);
+//                            fprintf(stderr, "address3 %s\n",                   item->contact->address3);
+//                            fprintf(stderr, "address3_desc %s\n",              item->contact->address3_desc);
+//                            fprintf(stderr, "address3_transport %s\n",         item->contact->address3_transport);
+//                            fprintf(stderr, "assistant_name %s\n",             item->contact->assistant_name);
+//                            fprintf(stderr, "assistant_phone %s\n",            item->contact->assistant_phone);
+//                            fprintf(stderr, "billing_information %s\n",        item->contact->billing_information);
+//                            fprintf(stderr, "business_address %s\n",           item->contact->business_address);
+//                            fprintf(stderr, "business_city %s\n",              item->contact->business_city);
+//                            fprintf(stderr, "business_country %s\n",           item->contact->business_country);
+//                            fprintf(stderr, "business_fax %s\n",               item->contact->business_fax);
+//                            fprintf(stderr, "business_homepage %s\n",          item->contact->business_homepage);
+//                            fprintf(stderr, "business_phone %s\n",             item->contact->business_phone);
+//                            fprintf(stderr, "business_phone2 %s\n",            item->contact->business_phone2);
+//                            fprintf(stderr, "business_po_box %s\n",            item->contact->business_po_box);
+//                            fprintf(stderr, "business_postal_code %s\n",       item->contact->business_postal_code);
+//                            fprintf(stderr, "business_state %s\n",             item->contact->business_state);
+//                            fprintf(stderr, "business_street %s\n",            item->contact->business_street);
+//                            fprintf(stderr, "callback_phone %s\n",             item->contact->callback_phone);
+//                            fprintf(stderr, "car_phone %s\n",                  item->contact->car_phone);
+//                            fprintf(stderr, "company_main_phone %s\n",         item->contact->company_main_phone);
+//                            fprintf(stderr, "company_name %s\n",               item->contact->company_name);
+//                            fprintf(stderr, "computer_name %s\n",              item->contact->computer_name);
+//                            fprintf(stderr, "customer_id %s\n",                item->contact->customer_id);
+//                            fprintf(stderr, "def_postal_address %s\n",         item->contact->def_postal_address);
+//                            fprintf(stderr, "department %s\n",                 item->contact->department);
+//                            fprintf(stderr, "display_name_prefix %s\n",        item->contact->display_name_prefix);
+//                            fprintf(stderr, "first_name %s\n",                 item->contact->first_name);
+//                            fprintf(stderr, "followup %s\n",                   item->contact->followup);
+//                            fprintf(stderr, "free_busy_address %s\n",          item->contact->free_busy_address);
+//                            fprintf(stderr, "ftp_site %s\n",                   item->contact->ftp_site);
+//                            fprintf(stderr, "fullname %s\n",                   item->contact->fullname);
+//                            fprintf(stderr, "gov_id %s\n",                     item->contact->gov_id);
+//                            fprintf(stderr, "hobbies %s\n",                    item->contact->hobbies);
+//                            fprintf(stderr, "home_address %s\n",               item->contact->home_address);
+//                            fprintf(stderr, "home_city %s\n",                  item->contact->home_city);
+//                            fprintf(stderr, "home_country %s\n",               item->contact->home_country);
+//                            fprintf(stderr, "home_fax %s\n",                   item->contact->home_fax);
+//                            fprintf(stderr, "home_phone %s\n",                 item->contact->home_phone);
+//                            fprintf(stderr, "home_phone2 %s\n",                item->contact->home_phone2);
+//                            fprintf(stderr, "home_po_box %s\n",                item->contact->home_po_box);
+//                            fprintf(stderr, "home_postal_code %s\n",           item->contact->home_postal_code);
+//                            fprintf(stderr, "home_state %s\n",                 item->contact->home_state);
+//                            fprintf(stderr, "home_street %s\n",                item->contact->home_street);
+//                            fprintf(stderr, "initials %s\n",                   item->contact->initials);
+//                            fprintf(stderr, "isdn_phone %s\n",                 item->contact->isdn_phone);
+//                            fprintf(stderr, "job_title %s\n",                  item->contact->job_title);
+//                            fprintf(stderr, "keyword %s\n",                    item->contact->keyword);
+//                            fprintf(stderr, "language %s\n",                   item->contact->language);
+//                            fprintf(stderr, "location %s\n",                   item->contact->location);
+//                            fprintf(stderr, "manager_name %s\n",               item->contact->manager_name);
+//                            fprintf(stderr, "middle_name %s\n",                item->contact->middle_name);
+//                            fprintf(stderr, "mileage %s\n",                    item->contact->mileage);
+//                            fprintf(stderr, "mobile_phone %s\n",               item->contact->mobile_phone);
+//                            fprintf(stderr, "nickname %s\n",                   item->contact->nickname);
+//                            fprintf(stderr, "office_loc %s\n",                 item->contact->office_loc);
+//                            fprintf(stderr, "org_id %s\n",                     item->contact->org_id);
+//                            fprintf(stderr, "other_address %s\n",              item->contact->other_address);
+//                            fprintf(stderr, "other_city %s\n",                 item->contact->other_city);
+//                            fprintf(stderr, "other_country %s\n",              item->contact->other_country);
+//                            fprintf(stderr, "other_phone %s\n",                item->contact->other_phone);
+//                            fprintf(stderr, "other_po_box %s\n",               item->contact->other_po_box);
+//                            fprintf(stderr, "other_postal_code %s\n",          item->contact->other_postal_code);
+//                            fprintf(stderr, "other_state %s\n",                item->contact->other_state);
+//                            fprintf(stderr, "other_street %s\n",               item->contact->other_street);
+//                            fprintf(stderr, "pager_phone %s\n",                item->contact->pager_phone);
+//                            fprintf(stderr, "personal_homepage %s\n",          item->contact->personal_homepage);
+//                            fprintf(stderr, "pref_name %s\n",                  item->contact->pref_name);
+//                            fprintf(stderr, "primary_fax %s\n",                item->contact->primary_fax);
+//                            fprintf(stderr, "primary_phone %s\n",              item->contact->primary_phone);
+//                            fprintf(stderr, "profession %s\n",                 item->contact->profession);
+//                            fprintf(stderr, "radio_phone %s\n",                item->contact->radio_phone);
+//                            fprintf(stderr, "spouse_name %s\n",                item->contact->spouse_name);
+//                            fprintf(stderr, "suffix %s\n",                     item->contact->suffix);
+//                            fprintf(stderr, "surname %s\n",                    item->contact->surname);
+//                            fprintf(stderr, "telex %s\n",                      item->contact->telex);
+//                            fprintf(stderr, "transmittable_display_name %s\n", item->contact->transmittable_display_name);
+//                            fprintf(stderr, "ttytdd_phone %s\n",               item->contact->ttytdd_phone);
+                            // have a valid cn
+                            const char *ucn = unique_string(folded(trim(cn)));
+                            printf("dn: cn=%s, %s\n", ucn, ldap_base);
+                            printf("cn: %s\n", ucn);
+                            if (item->contact->first_name) {
+                                snprintf(cn, sizeof(cn), "%s %s",
+                                    single(item->contact->display_name_prefix),
+                                    single(item->contact->first_name));
+                                printf("givenName: %s\n", trim(cn));
+                            }
+                            if (item->contact->surname) {
+                                snprintf(cn, sizeof(cn), "%s %s",
+                                    single(item->contact->surname),
+                                    single(item->contact->suffix));
+                                printf("sn: %s\n", trim(cn));
+                            }
+                            else if (item->contact->company_name) {
+                                printf("sn: %s\n", single(item->contact->company_name));
+                            }
+                            else
+                                printf("sn: %s\n", ucn);    // use cn as sn if we cannot find something better
 
-							if (item->contact->job_title)
-								printf("personalTitle: %s\n", single(item->contact->job_title));
-							if (item->contact->company_name)
-								printf("company: %s\n", single(item->contact->company_name));
-							if (item->contact->address1  && *item->contact->address1)
-								printf("mail: %s\n", single(item->contact->address1));
-							if (item->contact->address2  && *item->contact->address2)
-								printf("mail: %s\n", single(item->contact->address2));
-							if (item->contact->address3  && *item->contact->address3)
-								printf("mail: %s\n", single(item->contact->address3));
-							if (item->contact->address1a && *item->contact->address1a)
-								printf("mail: %s\n", single(item->contact->address1a));
-							if (item->contact->address2a && *item->contact->address2a)
-								printf("mail: %s\n", single(item->contact->address2a));
-							if (item->contact->address3a && *item->contact->address3a)
-								printf("mail: %s\n", single(item->contact->address3a));
-							if (item->contact->business_address) {
-								if (item->contact->business_po_box)
-									printf("postalAddress: %s\n", single(item->contact->business_po_box));
-								if (item->contact->business_street)
-									multi("postalAddress: %s\n", item->contact->business_street);
-								if (item->contact->business_city)
-									printf("l: %s\n", single(item->contact->business_city));
-								if (item->contact->business_state)
-									printf("st: %s\n", single(item->contact->business_state));
-								if (item->contact->business_postal_code)
-									printf("postalCode: %s\n", single(item->contact->business_postal_code));
-							}
-							else if (item->contact->home_address) {
-								if (item->contact->home_po_box)
-									printf("postalAddress: %s\n", single(item->contact->home_po_box));
-								if (item->contact->home_street)
-									multi("postalAddress: %s\n", item->contact->home_street);
-								if (item->contact->home_city)
-									printf("l: %s\n", single(item->contact->home_city));
-								if (item->contact->home_state)
-									printf("st: %s\n", single(item->contact->home_state));
-								if (item->contact->home_postal_code)
-									printf("postalCode: %s\n", single(item->contact->home_postal_code));
-							}
-							else if (item->contact->other_address) {
-								if (item->contact->other_po_box)
-									printf("postalAddress: %s\n", single(item->contact->other_po_box));
-								if (item->contact->other_street)
-									multi("postalAddress: %s\n", item->contact->other_street);
-								if (item->contact->other_city)
-									printf("l: %s\n", single(item->contact->other_city));
-								if (item->contact->other_state)
-									printf("st: %s\n", single(item->contact->other_state));
-								if (item->contact->other_postal_code)
-									printf("postalCode: %s\n", single(item->contact->other_postal_code));
-							}
-							if (item->contact->business_fax)
-								printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax));
-							else if (item->contact->home_fax)
-								printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax));
+                            if (item->contact->job_title)
+                                printf("personalTitle: %s\n", single(item->contact->job_title));
+                            if (item->contact->company_name)
+                                printf("company: %s\n", single(item->contact->company_name));
+                            if (item->contact->address1  && *item->contact->address1)
+                                printf("mail: %s\n", single(item->contact->address1));
+                            if (item->contact->address2  && *item->contact->address2)
+                                printf("mail: %s\n", single(item->contact->address2));
+                            if (item->contact->address3  && *item->contact->address3)
+                                printf("mail: %s\n", single(item->contact->address3));
+                            if (item->contact->address1a && *item->contact->address1a)
+                                printf("mail: %s\n", single(item->contact->address1a));
+                            if (item->contact->address2a && *item->contact->address2a)
+                                printf("mail: %s\n", single(item->contact->address2a));
+                            if (item->contact->address3a && *item->contact->address3a)
+                                printf("mail: %s\n", single(item->contact->address3a));
+                            if (item->contact->business_address) {
+                                if (item->contact->business_po_box)
+                                    printf("postalAddress: %s\n", single(item->contact->business_po_box));
+                                if (item->contact->business_street)
+                                    multi("postalAddress: %s\n", item->contact->business_street);
+                                if (item->contact->business_city)
+                                    printf("l: %s\n", single(item->contact->business_city));
+                                if (item->contact->business_state)
+                                    printf("st: %s\n", single(item->contact->business_state));
+                                if (item->contact->business_postal_code)
+                                    printf("postalCode: %s\n", single(item->contact->business_postal_code));
+                            }
+                            else if (item->contact->home_address) {
+                                if (item->contact->home_po_box)
+                                    printf("postalAddress: %s\n", single(item->contact->home_po_box));
+                                if (item->contact->home_street)
+                                    multi("postalAddress: %s\n", item->contact->home_street);
+                                if (item->contact->home_city)
+                                    printf("l: %s\n", single(item->contact->home_city));
+                                if (item->contact->home_state)
+                                    printf("st: %s\n", single(item->contact->home_state));
+                                if (item->contact->home_postal_code)
+                                    printf("postalCode: %s\n", single(item->contact->home_postal_code));
+                            }
+                            else if (item->contact->other_address) {
+                                if (item->contact->other_po_box)
+                                    printf("postalAddress: %s\n", single(item->contact->other_po_box));
+                                if (item->contact->other_street)
+                                    multi("postalAddress: %s\n", item->contact->other_street);
+                                if (item->contact->other_city)
+                                    printf("l: %s\n", single(item->contact->other_city));
+                                if (item->contact->other_state)
+                                    printf("st: %s\n", single(item->contact->other_state));
+                                if (item->contact->other_postal_code)
+                                    printf("postalCode: %s\n", single(item->contact->other_postal_code));
+                            }
+                            if (item->contact->business_fax)
+                                printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax));
+                            else if (item->contact->home_fax)
+                                printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax));
 
-							if (item->contact->business_phone)
-								printf("telephoneNumber: %s\n", single(item->contact->business_phone));
-							if (item->contact->home_phone)
-								printf("homePhone: %s\n", single(item->contact->home_phone));
+                            if (item->contact->business_phone)
+                                printf("telephoneNumber: %s\n", single(item->contact->business_phone));
+                            if (item->contact->home_phone)
+                                printf("homePhone: %s\n", single(item->contact->home_phone));
 
-							if (item->contact->car_phone)
-								printf("mobile: %s\n", single(item->contact->car_phone));
-							else if (item->contact->mobile_phone)
-								printf("mobile: %s\n", single(item->contact->mobile_phone));
-							else if (item->contact->other_phone)
-								printf("mobile: %s\n", single(item->contact->other_phone));
+                            if (item->contact->car_phone)
+                                printf("mobile: %s\n", single(item->contact->car_phone));
+                            else if (item->contact->mobile_phone)
+                                printf("mobile: %s\n", single(item->contact->mobile_phone));
+                            else if (item->contact->other_phone)
+                                printf("mobile: %s\n", single(item->contact->other_phone));
 
 
-							if (item->comment)
-								printf("description: %s\n", single(item->comment));
+                            if (item->comment)
+                                printf("description: %s\n", single(item->comment));
 
-							printf("objectClass: %s\n\n", ldap_class);
-						}
-					}
-				}
-				else {
-					DEBUG_INFO(("item is not a contact\n"));
-				}
-			}
-			pst_freeItem(item);
-		}
-		d_ptr = d_ptr->next;
-	}
+                            printf("objectClass: %s\n\n", ldap_class);
+                        }
+                    }
+                }
+                else {
+                    DEBUG_INFO(("item is not a contact\n"));
+                }
+            }
+            pst_freeItem(item);
+        }
+        d_ptr = d_ptr->next;
+    }
 }
 
 
 int main(int argc, char** argv) {
-	pst_desc_ll *d_ptr;
-	char *fname = NULL;
-	char *temp = NULL;		  //temporary char pointer
-	char c;
-	char *d_log = NULL;
-	prog_name = argv[0];
-	pst_item *item = NULL;
+    pst_desc_ll *d_ptr;
+    char *fname = NULL;
+    char *temp = NULL;        //temporary char pointer
+    char c;
+    char *d_log = NULL;
+    prog_name = argv[0];
+    pst_item *item = NULL;
 
-	while ((c = getopt(argc, argv, "b:c:d:Vh"))!= -1) {
-		switch (c) {
-		case 'b':
-			ldap_base = optarg;
-			temp = strchr(ldap_base, ',');
-			if (temp) {
-				*temp = '\0';
-				ldap_org = strdup(ldap_base+2); // assume first 2 chars are o=
-				*temp = ',';
-			}
-			break;
-		case 'c':
-			ldap_class = optarg;
-			break;
-		case 'd':
-			d_log = optarg;
-			break;
-		case 'h':
-			usage();
-			exit(0);
-			break;
-		case 'V':
-			version();
-			exit(0);
-			break;
-		default:
-			usage();
-			exit(1);
-			break;
-		}
-	}
+    while ((c = getopt(argc, argv, "b:c:d:Vh"))!= -1) {
+        switch (c) {
+        case 'b':
+            ldap_base = optarg;
+            temp = strchr(ldap_base, ',');
+            if (temp) {
+                *temp = '\0';
+                ldap_org = strdup(ldap_base+2); // assume first 2 chars are o=
+                *temp = ',';
+            }
+            break;
+        case 'c':
+            ldap_class = optarg;
+            break;
+        case 'd':
+            d_log = optarg;
+            break;
+        case 'h':
+            usage();
+            exit(0);
+            break;
+        case 'V':
+            version();
+            exit(0);
+            break;
+        default:
+            usage();
+            exit(1);
+            break;
+        }
+    }
 
-	if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) {
-		fname = argv[optind];
-	} else {
-		usage();
-		exit(2);
-	}
+    if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) {
+        fname = argv[optind];
+    } else {
+        usage();
+        exit(2);
+    }
 
-	#ifdef DEBUG_ALL
-		// force a log file
-		if (!d_log) d_log = "pst2ldif.log";
-	#endif
-	DEBUG_INIT(d_log);
-	DEBUG_REGISTER_CLOSE();
-	DEBUG_ENT("main");
-	RET_DERROR(pst_open(&pstfile, fname), 1, ("Error opening File\n"));
-	RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
+    #ifdef DEBUG_ALL
+        // force a log file
+        if (!d_log) d_log = "pst2ldif.log";
+    #endif
+    DEBUG_INIT(d_log);
+    DEBUG_REGISTER_CLOSE();
+    DEBUG_ENT("main");
+    RET_DERROR(pst_open(&pstfile, fname), 1, ("Error opening File\n"));
+    RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
 
-	pst_load_extended_attributes(&pstfile);
+    pst_load_extended_attributes(&pstfile);
 
-	d_ptr = pstfile.d_head; // first record is main record
-	item  = (pst_item*)pst_parse_item(&pstfile, d_ptr);
-	if (!item || !item->message_store) {
-		DEBUG_RET();
-		DIE(("main: Could not get root record\n"));
-	}
+    d_ptr = pstfile.d_head; // first record is main record
+    item  = (pst_item*)pst_parse_item(&pstfile, d_ptr);
+    if (!item || !item->message_store) {
+        DEBUG_RET();
+        DIE(("main: Could not get root record\n"));
+    }
 
-	d_ptr = pst_getTopOfFolders(&pstfile, item);
-	if (!d_ptr) {
-		DEBUG_RET();
-		DIE(("Top of folders record not found. Cannot continue\n"));
-	}
+    d_ptr = pst_getTopOfFolders(&pstfile, item);
+    if (!d_ptr) {
+        DEBUG_RET();
+        DIE(("Top of folders record not found. Cannot continue\n"));
+    }
 
-	pst_freeItem(item);
+    pst_freeItem(item);
 
-	// write the ldap header
-	printf("dn: %s\n", ldap_base);
-	printf("o: %s\n", ldap_org);
-	printf("objectClass: organization\n\n");
-	printf("dn: cn=root, %s\n", ldap_base);
-	printf("cn: root\n");
-	printf("objectClass: %s\n\n", ldap_class);
+    // write the ldap header
+    printf("dn: %s\n", ldap_base);
+    printf("o: %s\n", ldap_org);
+    printf("objectClass: organization\n\n");
+    printf("dn: cn=root, %s\n", ldap_base);
+    printf("cn: root\n");
+    printf("objectClass: %s\n\n", ldap_class);
 
-	process(d_ptr->child);	// do the children of TOPF
-	pst_close(&pstfile);
-	DEBUG_RET();
-	free_strings(all_strings);
-	return 0;
+    process(d_ptr->child);  // do the children of TOPF
+    pst_close(&pstfile);
+    DEBUG_RET();
+    free_strings(all_strings);
+    return 0;
 }
 
 
 int usage() {
-	version();
-	printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
-	printf("OPTIONS:\n");
-	printf("\t-h\t- Help. This screen\n");
-	printf("\t-V\t- Version. Display program version\n");
-	printf("\t-b ldapbase\t- set the ldap base value\n");
-	printf("\t-c class   \t- set the class of the ldap objects\n");
-	return 0;
+    version();
+    printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
+    printf("OPTIONS:\n");
+    printf("\t-h\t- Help. This screen\n");
+    printf("\t-V\t- Version. Display program version\n");
+    printf("\t-b ldapbase\t- set the ldap base value\n");
+    printf("\t-c class   \t- set the class of the ldap objects\n");
+    return 0;
 }
 
 
 int version() {
-	printf("pst2ldif v%s\n", VERSION);
+    printf("pst2ldif v%s\n", VERSION);
 #if BYTE_ORDER == BIG_ENDIAN
-	printf("Big Endian implementation being used.\n");
+    printf("Big Endian implementation being used.\n");
 #elif BYTE_ORDER == LITTLE_ENDIAN
-	printf("Little Endian implementation being used.\n");
+    printf("Little Endian implementation being used.\n");
 #else
 #  error "Byte order not supported by this library"
 #endif
 #ifdef __GNUC__
-	printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
+    printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
 #endif
-	return 0;
+    return 0;
 }
 
 
 // my_stristr varies from strstr in that its searches are case-insensitive
 char * my_stristr(char *haystack, char *needle) {
-	char *x=haystack, *y=needle, *z = NULL;
-	if (haystack == NULL || needle == NULL)
-		return NULL;
-	while (*y != '\0' && *x != '\0') {
-		if (tolower(*y) == tolower(*x)) {
-			// move y on one
-			y++;
-			if (z == NULL) {
-		z = x; // store first position in haystack where a match is made
-			}
-		} else {
-			y = needle; // reset y to the beginning of the needle
-			z = NULL; // reset the haystack storage point
-		}
-		x++; // advance the search in the haystack
-	}
-	return z;
+    char *x=haystack, *y=needle, *z = NULL;
+    if (haystack == NULL || needle == NULL)
+        return NULL;
+    while (*y != '\0' && *x != '\0') {
+        if (tolower(*y) == tolower(*x)) {
+            // move y on one
+            y++;
+            if (z == NULL) {
+        z = x; // store first position in haystack where a match is made
+            }
+        } else {
+            y = needle; // reset y to the beginning of the needle
+            z = NULL; // reset the haystack storage point
+        }
+        x++; // advance the search in the haystack
+    }
+    return z;
 }
 
 
 char *check_filename(char *fname) {
-	char *t = fname;
-	if (t == NULL) {
-		return fname;
-	}
-	while ((t = strpbrk(t, "/\\:"))) {
-		// while there are characters in the second string that we don't want
-		*t = '_'; //replace them with an underscore
-	}
-	return fname;
+    char *t = fname;
+    if (t == NULL) {
+        return fname;
+    }
+    while ((t = strpbrk(t, "/\\:"))) {
+        // while there are characters in the second string that we don't want
+        *t = '_'; //replace them with an underscore
+    }
+    return fname;
 }
 
 
-char *single(char *str) {
-	if (!str) return "";
-	char *ret = rfc2426_escape(str);
-	char *n = strchr(ret, '\n');
-	if (n) *n = '\0';
-	return ret;
+const char *single(char *str) {
+    if (!str) return "";
+    char *ret = rfc2426_escape(str);
+    char *n = strchr(ret, '\n');
+    if (n) *n = '\0';
+    return ret;
 }
 
 
-char *folded(char *str) {
-	if (!str) return "";
-	char *ret = rfc2426_escape(str);
-	char *n = ret;
-	while (n = strchr(n, '\n')) {
-		*n = ' ';
-	}
-	n = ret;
-	while (n = strchr(n, ',')) {
-		*n = ' ';
-	}
-	return ret;
+const char *folded(char *str) {
+    if (!str) return "";
+    char *ret = rfc2426_escape(str);
+    char *n = ret;
+    while ((n = strchr(n, '\n'))) {
+        *n = ' ';
+    }
+    n = ret;
+    while ((n = strchr(n, ','))) {
+        *n = ' ';
+    }
+    return ret;
 }
 
 
-void multi(char *fmt, char *str) {
-	if (!str) return;
-	char *ret = rfc2426_escape(str);
-	char *n = ret;
-	while (n = strchr(ret, '\n')) {
-		*n = '\0';
-		printf(fmt, ret);
-		ret = n+1;
-	}
-	if (*ret) printf(fmt, ret);
+void multi(const char *fmt, char *str) {
+    if (!str) return;
+    char *ret = rfc2426_escape(str);
+    char *n = ret;
+    while ((n = strchr(ret, '\n'))) {
+        *n = '\0';
+        printf(fmt, ret);
+        ret = n+1;
+    }
+    if (*ret) printf(fmt, ret);
 }
 
 
 char *rfc2426_escape(char *str) {
-	static char* buf = NULL;
-	char *ret, *a, *b;
-	int x = 0, y, z;
-	if (str == NULL)
-		ret = str;
-	else {
+    static char* buf = NULL;
+    char *ret, *a, *b;
+    int x = 0, y, z;
+    if (str == NULL)
+        ret = str;
+    else {
 
-		// calculate space required to escape all the following characters
-		y = chr_count(str, '\\')
-		  + chr_count(str, ';');
-		z = chr_count(str, '\r');
-		if (y == 0 && z == 0)
-			// there isn't any extra space required
-			ret = str;
-		else {
-			x = strlen(str) + y - z + 1; // don't forget room for the NUL
-			buf = (char*) realloc(buf, x);
-			a = str;
-			b = buf;
-			while (*a != '\0') {
-				switch(*a) {
-					case '\\':
-					case ';' :
-						*(b++)='\\';
-						*b=*a;
-					break;
-					case '\r':  // skip cr
-						b--;
-						break;
-					default:
-						*b=*a;
-				}
-				b++;
-				a++;
-			}
-			*b = '\0'; // NUL-terminate the string (buf)
-			ret = buf;
-		}
-	}
-	return ret;
+        // calculate space required to escape all the following characters
+        y = chr_count(str, '\\')
+          + chr_count(str, ';');
+        z = chr_count(str, '\r');
+        if (y == 0 && z == 0)
+            // there isn't any extra space required
+            ret = str;
+        else {
+            x = strlen(str) + y - z + 1; // don't forget room for the NUL
+            buf = (char*) realloc(buf, x);
+            a = str;
+            b = buf;
+            while (*a != '\0') {
+                switch(*a) {
+                    case '\\':
+                    case ';' :
+                        *(b++)='\\';
+                        *b=*a;
+                    break;
+                    case '\r':  // skip cr
+                        b--;
+                        break;
+                    default:
+                        *b=*a;
+                }
+                b++;
+                a++;
+            }
+            *b = '\0'; // NUL-terminate the string (buf)
+            ret = buf;
+        }
+    }
+    return ret;
 }
 
 
 int chr_count(char *str, char x) {
-	int r = 0;
-	while (*str != '\0') {
-		if (*str == x)
-			r++;
-		str++;
-	}
-	return r;
+    int r = 0;
+    while (*str != '\0') {
+        if (*str == x)
+            r++;
+        str++;
+    }
+    return r;
 }
 
--- a/src/readpst.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/readpst.c	Thu May 29 18:51:02 2008 -0700
@@ -310,11 +310,11 @@
             return 1;
         }
 
-        while (0 != ( l = fread( buf, 1, 1024, fp))) {
-            if (0 != pst_decrypt( buf, l, PST_COMP_ENCRYPT))
+        while (0 != (l = fread(buf, 1, 1024, fp))) {
+            if (0 != pst_decrypt(buf, l, PST_COMP_ENCRYPT))
                 fprintf(stderr, "pst_decrypt() failed (I'll try to continue)\n");
 
-            if (l != fwrite( buf, 1, l, stdout)) {
+            if (l != pst_fwrite(buf, 1, l, stdout)) {
                 fprintf(stderr, "Couldn't output to stdout?\n");
                 DEBUG_RET();
                 return 1;
@@ -385,12 +385,11 @@
             fprintf(f, ">");
         if ((n = strchr(body, '\n'))) {
             n++;
-            fwrite(body, n-body, 1, f); //write just a line
-
+            pst_fwrite(body, n-body, 1, f); //write just a line
             body = n;
         }
     }
-    fwrite(body, strlen(body), 1, f);
+    pst_fwrite(body, strlen(body), 1, f);
     DEBUG_RET();
 }
 
@@ -682,7 +681,7 @@
 // headers for emails stored in their "Personal Folders" files.
 char *skip_header_prologue(char *headers) {
     const char *bad = "Microsoft Mail Internet Headers";
-    if ( strncmp(headers, bad, strlen(bad)) == 0 ) {
+    if (strncmp(headers, bad, strlen(bad)) == 0) {
         // Found the offensive header prologue
         char *pc = strchr(headers, '\n');
         return pc + 1;
@@ -727,7 +726,7 @@
         WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
     } else {
         if (current_attach->data)
-            fwrite(current_attach->data, 1, current_attach->size, fp);
+            pst_fwrite(current_attach->data, 1, current_attach->size, fp);
         else {
             (void)pst_attach_to_file(pst, current_attach, fp);
         }
@@ -775,7 +774,7 @@
         }
     }
     if (current_attach->data) {
-        fwrite(enc, 1, strlen(enc), f_output);
+        pst_fwrite(enc, 1, strlen(enc), f_output);
         DEBUG_EMAIL(("Attachment Size after encoding is %i\n", strlen(enc)));
         free(enc);  // caught by valgrind
     } else {
--- a/src/vbuf.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/vbuf.c	Thu May 29 18:51:02 2008 -0700
@@ -9,13 +9,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "define.h"
 #include "vbuf.h"
-#include "generic.h"
 
 #ifdef WITH_DMALLOC
 #include <dmalloc.h>
 #endif
 
+#define STUPID_CR "\r\n"
+#define ASSERT(x,...) { if( !(x) ) DIE(( __VA_ARGS__)); }
+
 
 int skip_nl(char *s)
 {
@@ -104,184 +108,6 @@
 }
 
 
-//int utf16_write( FILE* stream, const void *buf, size_t count ) // write utf-8 or iso_8869-1 to stream after converting it to utf-16
-//{
-//
-//      //TODO: if anything big comes through here we are sunk, should do it
-//      //bit-by-bit, not one-big-gulp
-//
-//      size_t inbytesleft, outbytesleft;
-//      char *inbuf, *outbuf;
-//      size_t icresult;
-//      size_t rl;
-//
-//      //do we have enough buffer space?
-//      if( !wwbuf || nwwbuf < (count * 2 + 2) ) {
-//              wwbuf = F_REALLOC( wwbuf, count * 2 +2 );
-//
-//              nwwbuf = count * 2 + 2;
-//      }
-//
-//      inbytesleft = count; outbytesleft = nwwbuf;
-//      inbuf = (char*)buf; outbuf = wwbuf;
-//
-////    fprintf(stderr, "X%s, %dX", (char*)buf, strlen( buf ));
-////    fflush(stderr);
-//
-//      if( (rl = strlen( buf ) + 1) != count ) {
-//              fprintf(stderr, "utf16_write(): reported buffer size (%d) does not match string length (%d)\n",
-//                              count,
-//                              rl);
-//
-//              //hexdump( (char*)buf, 0, count, 1 );
-//
-//              raise( SIGSEGV );
-//              inbytesleft = rl;
-//      }
-//
-////    fprintf(stderr, "  attempting to convert:\n");
-////    hexdump( (char*)inbuf, 0, count, 1 );
-//
-//      icresult = iconv( i8to16, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
-//
-////    fprintf(stderr, "  converted:\n");
-////    hexdump( (char*)buf, 0, count, 1 );
-//
-////    fprintf(stderr, "  to:\n");
-////    hexdump( (char*)wwbuf, 0, nwwbuf, 1 );
-//
-//      if( (size_t)-1  == icresult ) {
-//              fprintf(stderr, "utf16_write(): iconv failure(%d): %s\n", errno, strerror( errno ) );
-//              fprintf(stderr, "  attempted to convert:\n");
-//              hexdump( (char*)inbuf, 0, count, 1 );
-//
-//              fprintf(stderr, "  result:\n");
-//              hexdump( (char*)outbuf, 0, count, 1 );
-//
-//              fprintf(stderr, "I'm going to segfault now.\n");
-//              raise( SIGSEGV );
-//              exit(1);
-//      }
-//
-//      if( inbytesleft > 0 ) {
-//              fprintf(stderr, "utf16_write(): iconv returned a short count.\n");
-//              exit(1);
-//      }
-//
-//      return fwrite( wwbuf, nwwbuf - outbytesleft - 2, 1, stream  );
-//}
-
-//char *utf16buf = NULL;
-//int utf16buf_len = 0;
-//
-//int utf16_fprintf( FILE* stream, const char *fmt, ... )
-//{
-//      int result=0;
-//      va_list ap;
-//
-//      if( utf16buf == NULL ) {
-//              utf16buf = (char*)F_MALLOC( SZ_MAX + 1 );
-//
-//              utf16buf_len = SZ_MAX + 1;
-//      }
-//
-//      va_start( ap, fmt );
-//
-//      result = vsnprintf( utf16buf, utf16buf_len, fmt, ap );
-//
-//      if( result + 1 > utf16buf_len ) { //didn't have space, realloc() and try again
-//              fprintf(stderr, "utf16_fprintf(): buffer too small (%d), F_MALLOC(%d)\n", utf16buf_len, result);
-//              free( utf16buf );
-//              utf16buf_len = result + 1;
-//              utf16buf = (char*)F_MALLOC( utf16buf_len );
-//
-//              result = vsnprintf( utf16buf, utf16buf_len, fmt, ap );
-//      }
-//
-//
-//      //didn't have space...again...something weird is going on...
-//      ASSERT( result + 1 <= utf16buf_len,  "utf16_fprintf(): Unpossible error!\n");
-//
-//      if( 1 != utf16_write( stream, utf16buf, result + 1 ) )
-//              DIE( "Write error?  -> %s or %s\n", strerror( errno ), uerr_str( uerr_get() ) );
-//
-//      return result;
-//}
-//
-//int utf16to8( char *inbuf_o, char *outbuf_o, int length )
-//{
-//      int inbytesleft = length;
-//      int outbytesleft = length;
-//      char *inbuf = inbuf_o;
-//      char *outbuf = outbuf_o;
-//      int rlen = -1, tlen;
-//      int icresult = -1;
-//
-//      int i, strlen=-1;
-//
-//      DEBUG(
-//              fprintf(stderr, "  utf16to8(): attempting to convert:\n");
-//              //hexdump( (char*)inbuf_o, 0, length, 1 );
-//              fflush(stderr);
-//      );
-//
-//      for( i=0; i<length ; i+=2 ) {
-//              if( inbuf_o[i] == 0 && inbuf_o[i + 1] == 0 ) {
-//                      //fprintf(stderr, "End of string found at: %d\n", i );
-//                      strlen = i;
-//              }
-//      }
-//
-//      //hexdump( (char*)inbuf_o, 0, strlen, 1 );
-//
-//      if( -1 == strlen ) WARN("String is not zero-terminated.");
-//
-//      //iconv does not like it when the inbytesleft > actual string length
-//      //enum: zero terminated, length valid
-//      //      zero terminated, length short //we won't go beyond length ever, so this is same as NZT case
-//      //      zero terminated, length long
-//      //      not zero terminated
-//      //      TODO: MEMORY BUG HERE!
-//      for( tlen = 0; tlen <= inbytesleft - 2; tlen+=2 ) {
-//              if( inbuf_o[tlen] == 0 && inbuf_o[tlen+1] == 0 ){
-//                      rlen = tlen + 2;
-//                      tlen = rlen;
-//                      break;
-//              }
-//              if( tlen == inbytesleft )fprintf(stderr, "Space allocated for string > actual string length.  Go windows!\n");
-//      }
-//
-//      if( rlen >= 0 )
-//              icresult = iconv( i16to8, &inbuf, &rlen, &outbuf, &outbytesleft );
-//
-//      if( icresult == (size_t)-1 ) {
-//              fprintf(stderr, "utf16to8(): iconv failure(%d): %s\n", errno, strerror( errno ) );
-//              fprintf(stderr, "  attempted to convert:\n");
-//              hexdump( (char*)inbuf_o, 0, length, 1 );
-//              fprintf(stderr, "  result:\n");
-//              hexdump( (char*)outbuf_o, 0, length, 1 );
-//              fprintf(stderr, "  MyDirtyOut:\n");
-//              for( i=0; i<length; i++) {
-//                      if( inbuf_o[i] != '\0' ) fprintf(stderr, "%c", inbuf_o[i] );
-//              }
-//
-//              fprintf( stderr, "\n" );
-//              raise( SIGSEGV );
-//              exit(1);
-//      }
-//
-//      DEBUG(
-//              fprintf(stderr, "  result:\n");
-//              hexdump( (char*)outbuf_o, 0, length, 1 );
-//           )
-//
-//      //fprintf(stderr, "utf16to8() returning %s\n", outbuf );
-//
-//      return icresult;
-//}
-//
-
-
 int utf16_is_terminated(char *str, int length)
 {
     VSTR_STATIC(errbuf, 100);
@@ -295,7 +121,7 @@
 
     if (-1 == len) {
         vshexdump(errbuf, str, 0, length, 1);
-        WARN("String is not zero terminated (probably broken data from registry) %s.", errbuf->b);
+        WARN(("String is not zero terminated (probably broken data from registry) %s.", errbuf->b));
     }
 
     return (-1 == len) ? 0 : 1;
@@ -331,7 +157,7 @@
     } while ((size_t)-1 == icresult && E2BIG == errno);
 
     if (0 != vb_utf8to16T(dumpster, dest->b, dest->dlen))
-        DIE("Reverse conversion failed.");
+        DIE(("Reverse conversion failed."));
 
     if (icresult == (size_t)-1) {
         //TODO: error
@@ -373,64 +199,14 @@
     size_t icresult = (size_t)-1;
     char *stend;
 
-    DEBUG(fprintf(stderr, "  utf8to16(): attempting to convert:\n");
-          //hexdump( (char*)inbuf_o, 0, length, 1 );
-          fflush(stderr););
-
     stend = memchr(inbuf_o, '\0', iblen);
     ASSERT(NULL != stend, "utf8to16(): in string not zero terminated.");
-
     inbytesleft = (stend - inbuf_o + 1 < iblen) ? stend - inbuf_o + 1 : iblen;
-
-    //iconv does not like it when the inbytesleft > actual string length
-    //enum: zero terminated, length valid
-    //      zero terminated, length short //we won't go beyond length ever, so this is same as NZT case
-    //      zero terminated, length long
-    //      not zero terminated
-    //      TODO: MEMORY BUG HERE!
-    //
-    /*
-       for( tlen = 0; tlen <= inbytesleft - 2; tlen+=2 ) {
-       if( inbuf_o[tlen] == 0 && inbuf_o[tlen+1] == 0 ){
-       rlen = tlen + 2;
-       tlen = rlen;
-       break;
-       }
-       if( tlen == inbytesleft )fprintf(stderr, "Space allocated for string > actual string length.  Go windows!\n");
-       }
-     */
-
-    //if( rlen >= 0 )
     icresult = iconv(i8to16, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
 
     if (icresult == (size_t)-1) {
-        DIE("iconv failure(%d): %s\n", errno, strerror(errno));
-        //fprintf(stderr, "  attempted to convert:\n");
-        //hexdump( (char*)inbuf_o, 0, iblen, 1 );
-        //fprintf(stderr, "  result:\n");
-        //hexdump( (char*)outbuf_o, 0, oblen, 1 );
-        //fprintf(stderr, "  MyDirtyOut:\n");
-//              for( i=0; i<iblen; i++) {
-//                      if( inbuf_o[i] != '\0' ) fprintf(stderr, "%c", inbuf_o[i] );
-//              }
-//
-//              fprintf( stderr, "\n" );
-//              raise( SIGSEGV );
-//              exit(1);
+        DIE(("iconv failure(%d): %s\n", errno, strerror(errno)));
     }
-//      DEBUG(
-//              fprintf(stderr, "  result:\n");
-//              hexdump( (char*)outbuf_o, 0, oblen, 1 );
-//           )
-
-    //fprintf(stderr, "utf8to16() returning %s\n", outbuf );
-
-    //TODO: error
-    DEBUG(
-        if (icresult)
-            printf("Uhhhh...utf8to16() returning icresult == %d\n", icresult);
-    );
-
     if (icresult > (size_t)INT_MAX) {
         return (-1);
     }
@@ -461,31 +237,10 @@
     } while ((size_t)-1 == icresult && E2BIG == errno);
 
     if (icresult == (size_t)-1) {
-        WARN("iconv failure: %s", strerror(errno));
-        //ERR_UNIX( errno, "vb_utf8to16():iconv failure: %s", strerror( errno ) );
+        WARN(("iconv failure: %s", strerror(errno)));
         unicode_init();
         return -1;
-        /*
-           fprintf(stderr, "vb_utf8to16(): iconv failure(%d == %d?): %s\n", errno, E2BIG, strerror( errno ) );
-           fprintf(stderr, "  attempted to convert:\n");
-           hexdump( (char*)cin, 0, inlen, 1 );
-           fprintf(stderr, "  result:\n");
-           hexdump( (char*)bout->b, 0, bout->dlen, 1 );
-           fprintf(stderr, "  MyDirtyOut:\n");
-           for( i=0; i<inlen; i++) {
-           if( inbuf[i] != '\0' ) fprintf(stderr, "%c", inbuf[i] );
-           }
-
-           fprintf( stderr, "\n" );
-           raise( SIGSEGV );
-           exit(1);
-         */
     }
-    DEBUG(
-        if (icresult)
-            printf("Uhhhh...vb_utf8to16() returning icresult == %d\n", icresult);
-    );
-
     if (icresult > (size_t) INT_MAX) {
         return (-1);
     }
@@ -519,18 +274,15 @@
 
 vbuf *vballoc(size_t len)
 {
-    struct varbuf *result;
-
-    result = F_MALLOC(sizeof(struct varbuf));
-
-    result->dlen = 0;
-    result->blen = 0;
-    result->buf = NULL;
-
-    vbresize(result, len);
-
+    struct varbuf *result = malloc(sizeof(struct varbuf));
+    if (result) {
+        result->dlen = 0;
+        result->blen = 0;
+        result->buf = NULL;
+        vbresize(result, len);
+    }
+    else DIE(("malloc() failure"));
     return result;
-
 }
 
 
@@ -565,8 +317,8 @@
         return;
     }
 
-    vb->buf = F_REALLOC(vb->buf, len);
-    vb->b = vb->buf;
+    vb->buf  = realloc(vb->buf, len);
+    vb->b    = vb->buf;
     vb->blen = len;
 }
 
@@ -604,12 +356,11 @@
     if (vb->dlen + len > vb->blen) {
         if (vb->dlen + len < vb->blen * 1.5)
             len = vb->blen * 1.5;
-        char *nb = F_MALLOC(vb->blen + len);
-        //printf("vbgrow() got %p back from malloc(%d)\n", nb, vb->blen + len);
+        char *nb = malloc(vb->blen + len);
+        if (!nb) DIE(("malloc() failure"));
         vb->blen = vb->blen + len;
         memcpy(nb, vb->b, vb->dlen);
 
-        //printf("vbgrow() I am going to free %p\n", vb->buf );
         free(vb->buf);
         vb->buf = nb;
         vb->b = vb->buf;
@@ -962,45 +713,3 @@
 }
 
 
-// TODO: not sure how useful this stuff is here
-int fmyinput(char *prmpt, char *ibuf, int maxlen)
-{                               /* get user input */
-    printf("%s", prmpt);
-
-    fgets(ibuf, maxlen + 1, stdin);
-
-    ibuf[strlen(ibuf) - 1] = 0;
-
-    return (strlen(ibuf));
-}
-
-
-// String formatting and output to FILE *stream or just stdout, etc
-// TODO: a lot of old, unused stuff in here
-void vswinhex8(vstr * vs, unsigned char *hbuf, int start, int stop, int loff)   // Produce regedit-style hex output */
-{
-    int i;
-    int lineflag = 0;
-
-    for (i = start; i < stop; i++) {
-        loff += vscatprintf(vs, "%02x", hbuf[i]);
-        if (i < stop - 1) {
-            loff += vscatprintf(vs, ",");
-            switch (lineflag) {
-                case 0:
-                    if (loff >= 77) {
-                        lineflag = 1;
-                        loff = 0;
-                        vscatprintf(vs, "\\%s  ", STUPID_CR);
-                    }
-                    break;
-                case 1:
-                    if (loff >= 75) {
-                        loff = 0;
-                        vscatprintf(vs, "\\%s  ", STUPID_CR);
-                    }
-                    break;
-            }
-        }
-    }
-}