'LibPst'
dumpblocks.c
Go to the documentation of this file.
1 #include "define.h"
2 
3 #define OUT_BUF 20
4 
5 int main(int argc, char* const* argv)
6 {
8  size_t i;
9  char *outdir = NULL, *file = NULL, *outname = NULL;
10  char *buf = NULL;
11  int c;
12  FILE *fp;
13 
14  while ((c = getopt(argc, argv, "o:")) != -1) {
15  switch (c) {
16  case 'o':
17  outdir = optarg;
18  break;
19  default:
20  printf("Unknown switch %c\n", c);
21  }
22  }
23  if (optind < argc) {
24  file = argv[optind];
25  } else {
26  printf("Usage: dumpblocks [options] pstfile\n");
27  printf("\tcopies the datablocks from the pst file into separate files\n");
28  printf("Options: \n");
29  printf("\t-o target\tSpecify the output directory\n");
30  exit(1);
31  }
32  DEBUG_INIT("dumpblocks.log", NULL);
33  DEBUG_ENT("main");
34 
35  printf("Opening file %s\n", file);
36  if (pst_open(&pstfile, file, NULL)) {
37  printf("Failed to open file %s\n", file);
38  exit(1);
39  }
40 
41  printf("Reading Indexes\n");
42  if (pst_load_index(&pstfile)) {
43  printf("Failed to load indexes in file %s\n", argv[1]);
44  exit(1);
45  }
46 
47  if (outdir != NULL)
48  if (chdir(outdir)) {
49  printf("Failed to change into directory %s\n", outdir);
50  exit(1);
51  }
52 
53  outname = (char *) pst_malloc(OUT_BUF);
54  printf("Saving blocks\n");
55  for (i = 0; i < pstfile.i_count; i++) {
56  pst_index_ll *ptr = &pstfile.i_table[i];
57  size_t c = pst_ff_getIDblock_dec(&pstfile, ptr->i_id, &buf);
58  if (c) {
59  snprintf(outname, OUT_BUF, "%#"PRIx64, ptr->i_id);
60  if ((fp = fopen(outname, "wb")) == NULL) {
61  printf("Failed to open file %s\n", outname);
62  continue;
63  }
64  pst_fwrite(buf, 1, c, fp);
65  fclose(fp);
66  } else {
67  printf("Failed to read block i_id %#"PRIx64"\n", ptr->i_id);
68  }
69  }
71  DEBUG_RET();
72  return 0;
73 }
void * pst_malloc(size_t size)
Definition: debug.c:169
int pst_close(pst_file *pf)
Close a pst file.
Definition: libpst.c:410
#define DEBUG_ENT(x)
Definition: define.h:171
int getopt(int argc, char *const *argv, char *optstring)
Definition: XGetopt.c:139
#define DEBUG_RET()
Definition: define.h:176
pst_file pstfile
Definition: getidblock.c:5
size_t i_count
Definition: libpst.h:905
uint64_t i_id
Definition: libpst.h:110
int pst_open(pst_file *pf, const char *name, const char *charset)
Open a pst file.
Definition: libpst.c:315
#define OUT_BUF
Definition: dumpblocks.c:3
size_t pst_ff_getIDblock_dec(pst_file *pf, uint64_t i_id, char **buf)
Get an ID block from file using pst_ff_getIDblock() and decrypt if necessary.
Definition: libpst.c:3985
int main(int argc, char *const *argv)
Definition: dumpblocks.c:5
int optind
Definition: XGetopt.c:137
#define DEBUG_INIT(fname, mutex)
Definition: define.h:182
int pst_load_index(pst_file *pf)
Load the index entries from the pst file.
Definition: libpst.c:652
size_t pst_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
fwrite with checking for null pointer.
Definition: libpst.c:4261
char * optarg
Definition: XGetopt.c:136
pst_index_ll * i_table
the array of index structures
Definition: libpst.h:904