comparison src/getidblock.c @ 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 7d5c637aaafb
children 987aa872294e
comparison
equal deleted inserted replaced
72:c21e9c001256 73:3cb02cb1e6cd
13 13
14 #include "libpst.h" 14 #include "libpst.h"
15 15
16 static void usage(); 16 static void usage();
17 17
18 int main(int argc, char ** argv) { 18 int main(int argc, char **argv)
19 // pass the id number to display on the command line 19 {
20 char *fname, *sid; 20 // pass the id number to display on the command line
21 pst_file pstfile; 21 char *fname, *sid;
22 unsigned int id; 22 pst_file pstfile;
23 int decrypt = 0, process = 0, binary = 0, c; 23 unsigned int id;
24 char *buf = NULL; 24 int decrypt = 0, process = 0, binary = 0, c;
25 size_t readSize; 25 char *buf = NULL;
26 pst_item *item; 26 size_t readSize;
27 pst_desc_ll* ptr; 27 pst_item *item;
28 pst_desc_ll *ptr;
28 29
29 DEBUG_INIT("getidblock.log"); 30 DEBUG_INIT("getidblock.log");
30 DEBUG_REGISTER_CLOSE(); 31 DEBUG_REGISTER_CLOSE();
31 DEBUG_ENT("main"); 32 DEBUG_ENT("main");
32 33
33 while ((c = getopt(argc, argv, "bdp")) != -1) { 34 while ((c = getopt(argc, argv, "bdp")) != -1) {
34 switch (c) { 35 switch (c) {
35 case 'b': 36 case 'b':
36 // enable binary output 37 // enable binary output
37 binary = 1; 38 binary = 1;
38 break; 39 break;
39 case 'd': 40 case 'd':
40 //enable decrypt 41 //enable decrypt
41 decrypt = 1; 42 decrypt = 1;
42 break; 43 break;
43 case 'p': 44 case 'p':
44 // enable procesing of block 45 // enable procesing of block
45 process = 1; 46 process = 1;
46 break; 47 break;
47 default: 48 default:
48 usage(); 49 usage();
49 exit(EXIT_FAILURE); 50 exit(EXIT_FAILURE);
50 } 51 }
51 } 52 }
52 53
53 if (optind+1 >= argc) { 54 if (optind + 1 >= argc) {
54 // no more items on the cmd 55 // no more items on the cmd
55 usage(); 56 usage();
56 exit(EXIT_FAILURE); 57 exit(EXIT_FAILURE);
57 } 58 }
58 fname = argv[optind]; 59 fname = argv[optind];
59 sid = argv[optind+1]; 60 sid = argv[optind + 1];
60 id = (unsigned int)strtol(sid, NULL, 0); 61 id = (unsigned int) strtol(sid, NULL, 0);
61 62
62 DEBUG_MAIN(("Opening file\n")); 63 DEBUG_MAIN(("Opening file\n"));
63 memset(&pstfile, 0, sizeof(pstfile)); 64 memset(&pstfile, 0, sizeof(pstfile));
64 if (pst_open(&pstfile, fname)) { 65 if (pst_open(&pstfile, fname)) {
65 DIE(("Error opening file\n")); 66 DIE(("Error opening file\n"));
66 } 67 }
67 68
68 DEBUG_MAIN(("Loading Index\n")); 69 DEBUG_MAIN(("Loading Index\n"));
69 if (pst_load_index(&pstfile) != 0) { 70 if (pst_load_index(&pstfile) != 0) {
70 DIE(("Error loading file index\n")); 71 DIE(("Error loading file index\n"));
71 } 72 }
73 // if ((ptr = pst_getID(&pstfile, id)) == NULL) {
74 // DIE(("id not found [%#x]\n", id));
75 // }
72 76
73 // if ((ptr = pst_getID(&pstfile, id)) == NULL) { 77 DEBUG_MAIN(("Loading block\n"));
74 // DIE(("id not found [%#x]\n", id));
75 // }
76 78
77 DEBUG_MAIN(("Loading block\n")); 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) {
81 DIE(("Error loading block\n"));
82 }
83 if (binary == 0)
84 printf("Block %#x, size %#x[%i]\n", id, (unsigned int) readSize, (int) readSize);
78 85
79 if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) { 86 if (decrypt != 0)
80 // if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) { 87 if (pst_decrypt(buf, readSize, (int) pstfile.encryption) != 0) {
81 DIE(("Error loading block\n")); 88 DIE(("Error decrypting block\n"));
82 } 89 }
83 if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize);
84 90
85 if (decrypt!=0) 91 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
86 if (pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) { 92 if (binary == 0) {
87 DIE(("Error decrypting block\n")); 93 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
88 } 94 } else {
95 if (fwrite(buf, 1, readSize, stdout) != 0) {
96 DIE(("Error occured during writing of buf to stdout\n"));
97 }
98 }
99 free(buf);
89 100
90 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize)); 101 if (process != 0) {
91 if (binary==0) { 102 DEBUG_MAIN(("Parsing block...\n"));
92 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0); 103 ptr = pstfile.d_head;
93 } else { 104 while (ptr != NULL) {
94 if (fwrite(buf, 1, readSize, stdout) != 0) { 105 if (ptr->list_index != NULL && ptr->list_index->id == id)
95 DIE(("Error occured during writing of buf to stdout\n")); 106 break;
96 } 107 if (ptr->desc != NULL && ptr->desc->id == id)
97 } 108 break;
98 free(buf); 109 ptr = pst_getNextDptr(ptr);
110 }
111 if (ptr == NULL) {
112 ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
113 ptr->desc = pst_getID(&pstfile, id);
114 ptr->list_index = NULL;
115 }
116 if (ptr != NULL) {
117 if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
118 pst_freeItem(item);
119 } else {
120 DEBUG_MAIN(("item not found with this ID\n"));
121 printf("Cannot find the owning Record of this ID. Cannot parse\n");
122 }
123 }
99 124
100 if (process!=0) { 125 if (pst_close(&pstfile) != 0) {
101 DEBUG_MAIN(("Parsing block...\n")); 126 DIE(("pst_close failed\n"));
102 ptr = pstfile.d_head; 127 }
103 while(ptr != NULL) {
104 if (ptr->list_index != NULL && ptr->list_index->id == id)
105 break;
106 if (ptr->desc != NULL && ptr->desc->id == id)
107 break;
108 ptr = pst_getNextDptr(ptr);
109 }
110 if (ptr == NULL) {
111 ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll));
112 ptr->desc = pst_getID(&pstfile, id);
113 ptr->list_index = NULL;
114 }
115 if (ptr != NULL) {
116 if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
117 pst_freeItem(item);
118 } else {
119 DEBUG_MAIN(("item not found with this ID\n"));
120 printf("Cannot find the owning Record of this ID. Cannot parse\n");
121 }
122 }
123 128
124 if(pst_close(&pstfile)!=0) { 129 DEBUG_RET();
125 DIE(("pst_close failed\n")); 130 return 0;
126 }
127
128 DEBUG_RET();
129 return 0;
130 } 131 }
131 132
132 void usage() { 133 void usage()
133 printf("usage: getidblock [options] filename id\n"); 134 {
134 printf("\tfilename - name of the file to access\n"); 135 printf("usage: getidblock [options] filename id\n");
135 printf("\tid - ID of the block to fetch - can begin with 0x for hex\n"); 136 printf("\tfilename - name of the file to access\n");
136 printf("\toptions\n"); 137 printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
137 printf("\t\t-d\tDecrypt the block before printing\n"); 138 printf("\toptions\n");
138 printf("\t\t-p\tProcess the block before finishing.\n"); 139 printf("\t\t-d\tDecrypt the block before printing\n");
139 printf("\t\t\tView the debug log for information\n"); 140 printf("\t\t-p\tProcess the block before finishing.\n");
141 printf("\t\t\tView the debug log for information\n");
140 } 142 }