comparison src/getidblock.c @ 142:2189a6b8134e

improve character set handling - don't try to convert utf-8 to single byte for fields that were not originally unicode. if the conversion fails, leave the data in utf-8.
author Carl Byington <carl@five-ten-sg.com>
date Mon, 23 Feb 2009 20:40:51 -0800
parents fc11b1d1ad34
children fdc58ad2c758
comparison
equal deleted inserted replaced
141:fd4297884319 142:2189a6b8134e
1 1
2 #include "define.h" 2 #include "define.h"
3 3
4 static void usage(); 4 int decrypt = 0, process = 0, binary = 0;
5 pst_file pstfile;
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-d\tDecrypt the block before printing\n");
16 printf("\t\t-p\tProcess the block before finishing.\n");
17 printf("\t\t\tView the debug log for information\n");
18 }
19
20
21 void dumper(uint64_t id);
22 void dumper(uint64_t id)
23 {
24 char *buf = NULL;
25 size_t readSize;
26 pst_desc_ll *ptr;
27
28 DEBUG_MAIN(("\n\n\nLooking at block index1 id %#"PRIx64"\n", id));
29
30 if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == 0) {
31 DIE(("Error loading block\n"));
32 }
33
34 if (decrypt)
35 if (pst_decrypt(id, buf, readSize, (int) pstfile.encryption) != 0) {
36 DIE(("Error decrypting block\n"));
37 }
38
39 DEBUG_MAIN(("Printing block id %#"PRIx64", size %#x\n", id, readSize));
40 if (binary) {
41 if (fwrite(buf, 1, readSize, stdout) != 0) {
42 DIE(("Error occured during writing of buf to stdout\n"));
43 }
44 } else {
45 printf("Block id %#"PRIx64", size %#x\n", id, readSize);
46 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
47 }
48 if (buf) free(buf);
49
50 if (process) {
51 DEBUG_MAIN(("Parsing block id %#"PRIx64"\n", id));
52 ptr = pstfile.d_head;
53 while (ptr) {
54 if (ptr->list_index && ptr->list_index->id == id)
55 break;
56 if (ptr->desc && ptr->desc->id == id)
57 break;
58 ptr = pst_getNextDptr(ptr);
59 }
60 if (!ptr) {
61 ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
62 ptr->desc = pst_getID(&pstfile, id);
63 ptr->list_index = NULL;
64 }
65 pst_item *item = pst_parse_item(&pstfile, ptr);
66 if (item) pst_freeItem(item);
67 }
68 }
69
70
71 void dump_desc(pst_desc_ll *ptr);
72 void dump_desc(pst_desc_ll *ptr)
73 {
74 while (ptr) {
75 DEBUG_MAIN(("\n\n\nLooking at block desc id %#"PRIx64"\n", ptr->id));
76 if (ptr->desc && ptr->desc->id) dumper(ptr->desc->id);
77 if (ptr->list_index && ptr->list_index->id) dumper(ptr->list_index->id);
78 if (ptr->child) dump_desc(ptr->child);
79 ptr = ptr->next;
80 }
81 }
82
5 83
6 int main(int argc, char* const* argv) 84 int main(int argc, char* const* argv)
7 { 85 {
8 // pass the id number to display on the command line 86 // pass the id number to display on the command line
9 char *fname, *sid; 87 char *fname, *sid;
10 pst_file pstfile;
11 uint64_t id; 88 uint64_t id;
12 int decrypt = 0, process = 0, binary = 0, c; 89 int c;
13 char *buf = NULL;
14 size_t readSize;
15 pst_item *item;
16 pst_desc_ll *ptr;
17 90
18 DEBUG_INIT("getidblock.log"); 91 DEBUG_INIT("getidblock.log");
19 DEBUG_REGISTER_CLOSE(); 92 DEBUG_REGISTER_CLOSE();
20 DEBUG_ENT("main"); 93 DEBUG_ENT("main");
21 94
56 129
57 DEBUG_MAIN(("Loading Index\n")); 130 DEBUG_MAIN(("Loading Index\n"));
58 if (pst_load_index(&pstfile) != 0) { 131 if (pst_load_index(&pstfile) != 0) {
59 DIE(("Error loading file index\n")); 132 DIE(("Error loading file index\n"));
60 } 133 }
61 // if ((ptr = pst_getID(&pstfile, id)) == NULL) {
62 // DIE(("id not found [%#x]\n", id));
63 // }
64 134
65 DEBUG_MAIN(("Loading block\n")); 135 if (id) {
66 136 dumper(id);
67 if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) {
68 // if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) {
69 DIE(("Error loading block\n"));
70 } 137 }
71 if (binary == 0) 138 else {
72 printf("Block %#"PRIx64", size %#x[%i]\n", id, (unsigned int) readSize, (int) readSize); 139 pst_index_ll *ptr = pstfile.i_head;
73 140 while (ptr) {
74 if (decrypt != 0) 141 dumper(ptr->id);
75 if (pst_decrypt(id, buf, readSize, (int) pstfile.encryption) != 0) { 142 ptr = ptr->next;
76 DIE(("Error decrypting block\n"));
77 } 143 }
78 144 dump_desc(pstfile.d_head);
79 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
80 if (binary == 0) {
81 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
82 } else {
83 if (fwrite(buf, 1, readSize, stdout) != 0) {
84 DIE(("Error occured during writing of buf to stdout\n"));
85 }
86 }
87 free(buf);
88
89 if (process != 0) {
90 DEBUG_MAIN(("Parsing block...\n"));
91 ptr = pstfile.d_head;
92 while (ptr != NULL) {
93 if (ptr->list_index != NULL && ptr->list_index->id == id)
94 break;
95 if (ptr->desc != NULL && ptr->desc->id == id)
96 break;
97 ptr = pst_getNextDptr(ptr);
98 }
99 if (ptr == NULL) {
100 ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
101 ptr->desc = pst_getID(&pstfile, id);
102 ptr->list_index = NULL;
103 }
104 if (ptr != NULL) {
105 if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
106 pst_freeItem(item);
107 } else {
108 DEBUG_MAIN(("item not found with this ID\n"));
109 printf("Cannot find the owning Record of this ID. Cannot parse\n");
110 }
111 } 145 }
112 146
113 if (pst_close(&pstfile) != 0) { 147 if (pst_close(&pstfile) != 0) {
114 DIE(("pst_close failed\n")); 148 DIE(("pst_close failed\n"));
115 } 149 }
116 150
117 DEBUG_RET(); 151 DEBUG_RET();
118 return 0; 152 return 0;
119 } 153 }
120 154
121 void usage()
122 {
123 printf("usage: getidblock [options] filename id\n");
124 printf("\tfilename - name of the file to access\n");
125 printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
126 printf("\toptions\n");
127 printf("\t\t-d\tDecrypt the block before printing\n");
128 printf("\t\t-p\tProcess the block before finishing.\n");
129 printf("\t\t\tView the debug log for information\n");
130 }