00001 #include "define.h"
00002
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <unistd.h>
00006 #include "libpst.h"
00007
00008 #define OUT_BUF 20
00009 int main(int argc, char **argv)
00010 {
00011 pst_file pstfile;
00012 pst_index_ll *ptr;
00013 char *outdir = NULL, *file = NULL, *outname = NULL;
00014 char *buf = NULL;
00015 int c;
00016 FILE *fp;
00017
00018 while ((c = getopt(argc, argv, "o:")) != -1) {
00019 switch (c) {
00020 case 'o':
00021 outdir = optarg;
00022 break;
00023 default:
00024 printf("Unknown switch %c\n", c);
00025 }
00026 }
00027 if (optind < argc) {
00028 file = argv[optind];
00029 } else {
00030 printf("Usage: dumpblocks [options] pstfile\n");
00031 printf("\tcopies the datablocks from the pst file into separate files\n");
00032 printf("Options: \n");
00033 printf("\t-o target\tSpecify the output directory\n");
00034 exit(1);
00035 }
00036 DEBUG_INIT("dumpblocks.log");
00037 DEBUG_REGISTER_CLOSE();
00038 DEBUG_ENT("main");
00039
00040 printf("Opening file %s\n", file);
00041 if (pst_open(&pstfile, file)) {
00042 printf("Failed to open file %s\n", file);
00043 exit(1);
00044 }
00045
00046 printf("Reading Indexes\n");
00047 if (pst_load_index(&pstfile)) {
00048 printf("Failed to load indexes in file %s\n", argv[1]);
00049 exit(1);
00050 }
00051
00052 if (outdir != NULL)
00053 if (chdir(outdir)) {
00054 printf("Failed to change into directory %s\n", outdir);
00055 exit(1);
00056 }
00057
00058 ptr = pstfile.i_head;
00059 outname = (char *) xmalloc(OUT_BUF);
00060 printf("Saving blocks\n");
00061 while (ptr != NULL) {
00062
00063
00064
00065 if ((ptr->id & 0x02) == 0 && pstfile.encryption == PST_ENC) {
00066 c = pst_ff_getIDblock_dec(&pstfile, ptr->id, &buf);
00067 } else {
00068 c = pst_ff_getIDblock(&pstfile, ptr->id, &buf);
00069 }
00070
00071 if (c > 0) {
00072 snprintf(outname, OUT_BUF, "%#"PRIx64, ptr->id);
00073 if ((fp = fopen(outname, "wb")) == NULL) {
00074 printf("Failed to open file %s\n", outname);
00075 continue;
00076 }
00077 pst_fwrite(buf, 1, c, fp);
00078 fclose(fp);
00079 } else {
00080 printf("Failed to read block id %#"PRIx64"\n", ptr->id);
00081 }
00082 ptr = ptr->next;
00083 }
00084 pst_close(&pstfile);
00085 DEBUG_RET();
00086 return 0;
00087 }