annotate src/getidblock.c @ 33:12cac756bc05 stable-0-5-5

enable -d option, but if not specified, don't generate a debug file
author carl
date Tue, 10 Jul 2007 20:23:55 -0700
parents c508ee15dfca
children 07177825c91b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 #include <stdio.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 #include <string.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 #ifndef __GNUC__
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 # include "XGetopt.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 #ifndef _WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 # include <unistd.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 #include "define.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 #include "libpst.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 static void usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17 int main(int argc, char ** argv) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 // pass the id number to display on the command line
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 char *fname, *sid;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20 pst_file pstfile;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 unsigned int id;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22 int decrypt = 0, process = 0, binary = 0, c;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23 unsigned char *buf = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
24 size_t readSize;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
25 pst_item *item;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
26 pst_desc_ll* ptr;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 DEBUG_INIT("getidblock.log");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29 DEBUG_REGISTER_CLOSE();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30 DEBUG_ENT("main");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 while ((c = getopt(argc, argv, "bdp")) != -1) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33 switch (c) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
34 case 'b':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
35 // enable binary output
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 binary = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 case 'd':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 //enable decrypt
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40 decrypt = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41 break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 case 'p':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
43 // enable procesing of block
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44 process = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45 break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 default:
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48 exit(EXIT_FAILURE);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 if (optind+1 >= argc) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53 // no more items on the cmd
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 exit(EXIT_FAILURE);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
56 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
57 fname = argv[optind];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
58 sid = argv[optind+1];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
59 id = (unsigned int)strtol(sid, NULL, 0);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
60
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
61 DEBUG_MAIN(("Opening file\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
62 memset(&pstfile, 0, sizeof(pstfile));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63 if (pst_open(&pstfile, fname, "r")!=0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
64 DIE(("Error opening file\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
65 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
66
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
67 DEBUG_MAIN(("Loading Index\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68 if (pst_load_index(&pstfile) != 0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69 DIE(("Error loading file index\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
70 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
72 // if ((ptr = _pst_getID(&pstfile, id)) == NULL) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 // DIE(("id not found [%#x]\n", id));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74 // }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 DEBUG_MAIN(("Loading block\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
77
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78 if ((readSize = _pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
79 // if ((readSize = _pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80 DIE(("Error loading block\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
81 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
82 if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
83
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
84 if (decrypt!=0)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
85 if (_pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
86 DIE(("Error decrypting block\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
87 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
88
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
89 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
90 if (binary==0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
91 _pst_debug_hexdump(stdout, buf, readSize, 0x10);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
92 } else {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
93 if (fwrite(buf, 1, readSize, stdout) != 0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
94 DIE(("Error occured during writing of buf to stdout\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
95 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 free(buf);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 if (process!=0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 DEBUG_MAIN(("Parsing block...\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 ptr = pstfile.d_head;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102 while(ptr != NULL) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 if (ptr->list_index != NULL && ptr->list_index->id == id)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105 if (ptr->desc != NULL && ptr->desc->id == id)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106 break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107 ptr = pst_getNextDptr(ptr);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109 if (ptr == NULL) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110 ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 ptr->desc = _pst_getID(&pstfile, id);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112 ptr->list_index = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 if (ptr != NULL) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115 if ((item = _pst_parse_item(&pstfile, ptr)) != NULL)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116 _pst_freeItem(item);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117 } else {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118 DEBUG_MAIN(("item not found with this ID\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 printf("Cannot find the owning Record of this ID. Cannot parse\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 if(pst_close(&pstfile)!=0) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
124 DIE(("pst_close failed\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
125 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
126
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
127 DEBUG_RET();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
128 return 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
129 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
130
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
131 void usage() {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
132 printf("usage: getidblock [options] filename id\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
133 printf("\tfilename - name of the file to access\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
134 printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
135 printf("\toptions\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
136 printf("\t\t-d\tDecrypt the block before printing\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
137 printf("\t\t-p\tProcess the block before finishing.\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
138 printf("\t\t\tView the debug log for information\n");
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
139 }