'LibPst'
getidblock.c
Go to the documentation of this file.
1 
2 #include "define.h"
3 
4 int process = 0, binary = 0;
6 
7 
8 void usage();
9 void usage()
10 {
11  printf("usage: getidblock [options] filename id\n");
12  printf("\tfilename - name of the file to access\n");
13  printf("\tid - ID of the block to fetch (0 to fetch all) - can begin with 0x for hex\n");
14  printf("\toptions\n");
15  printf("\t\t-p\tProcess the block before finishing.\n");
16  printf("\t\t-b\tDump the blocks in binary to stdout.\n");
17  printf("\t\t\tView the debug log for information\n");
18 }
19 
20 
21 void dumper(uint64_t i_id);
22 void dumper(uint64_t i_id)
23 {
24  char *buf = NULL;
25  size_t readSize;
26  pst_desc_tree *ptr;
27 
28  DEBUG_INFO(("\n\n\nLooking at block index1 id %#"PRIx64"\n", i_id));
29 
30  if ((readSize = pst_ff_getIDblock_dec(&pstfile, i_id, &buf)) <= 0 || buf == 0) {
31  DIE(("Error loading block\n"));
32  }
33 
34  DEBUG_INFO(("Printing block i_id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize));
35  if (binary) {
36  if (fwrite(buf, 1, readSize, stdout) != 0) {
37  DIE(("Error occurred during writing of buf to stdout\n"));
38  }
39  } else {
40  printf("Block id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize);
41  pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
42  }
43  if (buf) free(buf);
44 
45  if (process) {
46  DEBUG_INFO(("Parsing block id %#"PRIx64"\n", i_id));
47  ptr = pstfile.d_head;
48  while (ptr) {
49  if (ptr->assoc_tree && ptr->assoc_tree->i_id == i_id)
50  break;
51  if (ptr->desc && ptr->desc->i_id == i_id)
52  break;
53  ptr = pst_getNextDptr(ptr);
54  }
55  if (!ptr) {
56  ptr = (pst_desc_tree *) pst_malloc(sizeof(pst_desc_tree));
57  memset(ptr, 0, sizeof(pst_desc_tree));
58  ptr->desc = pst_getID(&pstfile, i_id);
59  }
60  pst_item *item = pst_parse_item(&pstfile, ptr, NULL);
61  if (item) pst_freeItem(item);
62  }
63 }
64 
65 
66 void dump_desc(pst_desc_tree *ptr, pst_desc_tree *parent);
68 {
69  while (ptr) {
70  uint64_t parent_d_id = (parent) ? parent->d_id : 0;
71  printf("Descriptor block d_id %#"PRIx64" parent d_id %#"PRIx64" children %i desc.i_id=%#"PRIx64", assoc tree.i_id=%#"PRIx64"\n",
72  ptr->d_id, parent_d_id, ptr->no_child,
73  (ptr->desc ? ptr->desc->i_id : (uint64_t)0),
74  (ptr->assoc_tree ? ptr->assoc_tree->i_id : (uint64_t)0));
75  if (ptr->desc && ptr->desc->i_id) dumper(ptr->desc->i_id);
76  if (ptr->assoc_tree && ptr->assoc_tree->i_id) dumper(ptr->assoc_tree->i_id);
77  if (ptr->child) dump_desc(ptr->child, ptr);
78  ptr = ptr->next;
79  }
80 }
81 
82 
83 int main(int argc, char* const* argv)
84 {
85  // pass the id number to display on the command line
86  char *fname, *sid;
87  uint64_t i_id;
88  int c;
89 
90  DEBUG_INIT("getidblock.log", NULL);
91  DEBUG_ENT("main");
92 
93  while ((c = getopt(argc, argv, "bp")) != -1) {
94  switch (c) {
95  case 'b':
96  // enable binary output
97  binary = 1;
98  break;
99  case 'p':
100  // enable processing of block
101  process = 1;
102  break;
103  default:
104  usage();
105  exit(EXIT_FAILURE);
106  }
107  }
108 
109  if (optind + 1 >= argc) {
110  // no more items on the cmd
111  usage();
112  exit(EXIT_FAILURE);
113  }
114  fname = argv[optind];
115  sid = argv[optind + 1];
116  i_id = (uint64_t)strtoll(sid, NULL, 0);
117 
118  DEBUG_INFO(("Opening file\n"));
119  memset(&pstfile, 0, sizeof(pstfile));
120  if (pst_open(&pstfile, fname, NULL)) {
121  DIE(("Error opening file\n"));
122  }
123 
124  DEBUG_INFO(("Loading Index\n"));
125  if (pst_load_index(&pstfile) != 0) {
126  DIE(("Error loading file index\n"));
127  }
128 
129  if (i_id) {
130  dumper(i_id);
131  }
132  else {
133  size_t i;
134  for (i = 0; i < pstfile.i_count; i++) {
136  }
137  dump_desc(pstfile.d_head, NULL);
138  }
139 
140  if (pst_close(&pstfile) != 0) {
141  DIE(("pst_close failed\n"));
142  }
143 
144  DEBUG_RET();
145  return 0;
146 }
147 
void dump_desc(pst_desc_tree *ptr, pst_desc_tree *parent)
Definition: getidblock.c:67
#define DIE(x)
Definition: define.h:160
void pst_debug_hexdumper(FILE *out, const char *buf, size_t size, int cols, int delta)
Definition: debug.c:119
void pst_freeItem(pst_item *item)
Free the item returned by pst_parse_item().
Definition: libpst.c:3381
void * pst_malloc(size_t size)
Definition: debug.c:169
int pst_close(pst_file *pf)
Close a pst file.
Definition: libpst.c:410
uint64_t d_id
Definition: libpst.h:127
#define DEBUG_INFO(x)
Definition: define.h:166
This contains the common mapi elements, and pointers to structures for each major mapi item type...
Definition: libpst.h:780
#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
struct pst_desc_tree * next
Definition: libpst.h:133
struct pst_desc_tree * child
Definition: libpst.h:135
int pst_open(pst_file *pf, const char *name, const char *charset)
Open a pst file.
Definition: libpst.c:315
pst_index_ll * pst_getID(pst_file *pf, uint64_t i_id)
Lookup the i_id in the index linked list, and return a pointer to the element.
Definition: libpst.c:3681
int process
Definition: getidblock.c:4
void dumper(uint64_t i_id)
Definition: getidblock.c:22
pst_index_ll * desc
Definition: libpst.h:129
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
int32_t no_child
Definition: libpst.h:131
pst_index_ll * assoc_tree
Definition: libpst.h:130
int binary
Definition: getidblock.c:4
int main(int argc, char *const *argv)
Definition: getidblock.c:83
pst_item * pst_parse_item(pst_file *pf, pst_desc_tree *d_ptr, pst_id2_tree *m_head)
Process a high level object from the pst file.
Definition: libpst.c:1249
pst_desc_tree * pst_getNextDptr(pst_desc_tree *d)
Walk the descriptor tree.
Definition: libpst.c:674
int optind
Definition: XGetopt.c:137
pst_desc_tree * d_head
the head and tail of the top level of the descriptor tree
Definition: libpst.h:907
void usage()
Definition: getidblock.c:9
#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
pst_index_ll * i_table
the array of index structures
Definition: libpst.h:904