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