Mercurial > libpst
annotate src/getidblock.c @ 59:7d5c637aaafb
General cleanup and code fixes.
Use autoscan to cleanup our autoconf system.
Use autoconf to detect when we need to use our XGetopt files and other header files.
Decode BCC field.
Fix missing LE32_CPU byte swapping for FILETIME types.
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 14 Feb 2008 14:55:32 -0800 |
parents | 034641c26ab9 |
children | 3cb02cb1e6cd |
rev | line source |
---|---|
48 | 1 #include "define.h" |
2 | |
16 | 3 #include <stdio.h> |
4 #include <string.h> | |
5 | |
6 #ifndef __GNUC__ | |
7 # include "XGetopt.h" | |
8 #endif | |
9 | |
10 #ifndef _WIN32 | |
11 # include <unistd.h> | |
12 #endif | |
13 | |
14 #include "libpst.h" | |
15 | |
16 static void usage(); | |
17 | |
18 int main(int argc, char ** argv) { | |
19 // pass the id number to display on the command line | |
20 char *fname, *sid; | |
21 pst_file pstfile; | |
22 unsigned int id; | |
23 int decrypt = 0, process = 0, binary = 0, c; | |
52 | 24 char *buf = NULL; |
16 | 25 size_t readSize; |
26 pst_item *item; | |
27 pst_desc_ll* ptr; | |
28 | |
29 DEBUG_INIT("getidblock.log"); | |
30 DEBUG_REGISTER_CLOSE(); | |
31 DEBUG_ENT("main"); | |
32 | |
33 while ((c = getopt(argc, argv, "bdp")) != -1) { | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
34 switch (c) { |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
35 case 'b': |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
36 // enable binary output |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
37 binary = 1; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
38 break; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
39 case 'd': |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
40 //enable decrypt |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
41 decrypt = 1; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
42 break; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
43 case 'p': |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
44 // enable procesing of block |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
45 process = 1; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
46 break; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
47 default: |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
48 usage(); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
49 exit(EXIT_FAILURE); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
50 } |
16 | 51 } |
52 | |
53 if (optind+1 >= argc) { | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
54 // no more items on the cmd |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
55 usage(); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
56 exit(EXIT_FAILURE); |
16 | 57 } |
58 fname = argv[optind]; | |
59 sid = argv[optind+1]; | |
60 id = (unsigned int)strtol(sid, NULL, 0); | |
61 | |
62 DEBUG_MAIN(("Opening file\n")); | |
63 memset(&pstfile, 0, sizeof(pstfile)); | |
59
7d5c637aaafb
General cleanup and code fixes.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
64 if (pst_open(&pstfile, fname)) { |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
65 DIE(("Error opening file\n")); |
16 | 66 } |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
67 |
16 | 68 DEBUG_MAIN(("Loading Index\n")); |
69 if (pst_load_index(&pstfile) != 0) { | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
70 DIE(("Error loading file index\n")); |
16 | 71 } |
72 | |
46 | 73 // if ((ptr = pst_getID(&pstfile, id)) == NULL) { |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
74 // DIE(("id not found [%#x]\n", id)); |
16 | 75 // } |
76 | |
77 DEBUG_MAIN(("Loading block\n")); | |
78 | |
46 | 79 if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) { |
80 // if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) { | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
81 DIE(("Error loading block\n")); |
16 | 82 } |
83 if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize); | |
84 | |
85 if (decrypt!=0) | |
46 | 86 if (pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) { |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
87 DIE(("Error decrypting block\n")); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
88 } |
16 | 89 |
90 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize)); | |
91 if (binary==0) { | |
46 | 92 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0); |
16 | 93 } else { |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
94 if (fwrite(buf, 1, readSize, stdout) != 0) { |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
95 DIE(("Error occured during writing of buf to stdout\n")); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
96 } |
16 | 97 } |
98 free(buf); | |
99 | |
100 if (process!=0) { | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
101 DEBUG_MAIN(("Parsing block...\n")); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
102 ptr = pstfile.d_head; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
103 while(ptr != NULL) { |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
104 if (ptr->list_index != NULL && ptr->list_index->id == id) |
16 | 105 break; |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
106 if (ptr->desc != NULL && ptr->desc->id == id) |
16 | 107 break; |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
108 ptr = pst_getNextDptr(ptr); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
109 } |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
110 if (ptr == NULL) { |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
111 ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll)); |
46 | 112 ptr->desc = pst_getID(&pstfile, id); |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
113 ptr->list_index = NULL; |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
114 } |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
115 if (ptr != NULL) { |
46 | 116 if ((item = pst_parse_item(&pstfile, ptr)) != NULL) |
117 pst_freeItem(item); | |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
118 } else { |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
119 DEBUG_MAIN(("item not found with this ID\n")); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
120 printf("Cannot find the owning Record of this ID. Cannot parse\n"); |
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
121 } |
16 | 122 } |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
123 |
16 | 124 if(pst_close(&pstfile)!=0) { |
34
07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
carl
parents:
16
diff
changeset
|
125 DIE(("pst_close failed\n")); |
16 | 126 } |
127 | |
128 DEBUG_RET(); | |
129 return 0; | |
130 } | |
131 | |
132 void usage() { | |
133 printf("usage: getidblock [options] filename id\n"); | |
134 printf("\tfilename - name of the file to access\n"); | |
135 printf("\tid - ID of the block to fetch - can begin with 0x for hex\n"); | |
136 printf("\toptions\n"); | |
137 printf("\t\t-d\tDecrypt the block before printing\n"); | |
138 printf("\t\t-p\tProcess the block before finishing.\n"); | |
139 printf("\t\t\tView the debug log for information\n"); | |
140 } |