Mercurial > libpst
comparison src/getidblock.c @ 34:07177825c91b
fix signed/unsigned to allow very small pst files with only leaf nodes
author | carl |
---|---|
date | Thu, 12 Jul 2007 14:59:13 -0700 |
parents | c508ee15dfca |
children | b2a7f2e0926a |
comparison
equal
deleted
inserted
replaced
33:12cac756bc05 | 34:07177825c91b |
---|---|
28 DEBUG_INIT("getidblock.log"); | 28 DEBUG_INIT("getidblock.log"); |
29 DEBUG_REGISTER_CLOSE(); | 29 DEBUG_REGISTER_CLOSE(); |
30 DEBUG_ENT("main"); | 30 DEBUG_ENT("main"); |
31 | 31 |
32 while ((c = getopt(argc, argv, "bdp")) != -1) { | 32 while ((c = getopt(argc, argv, "bdp")) != -1) { |
33 switch (c) { | 33 switch (c) { |
34 case 'b': | 34 case 'b': |
35 // enable binary output | 35 // enable binary output |
36 binary = 1; | 36 binary = 1; |
37 break; | 37 break; |
38 case 'd': | 38 case 'd': |
39 //enable decrypt | 39 //enable decrypt |
40 decrypt = 1; | 40 decrypt = 1; |
41 break; | 41 break; |
42 case 'p': | 42 case 'p': |
43 // enable procesing of block | 43 // enable procesing of block |
44 process = 1; | 44 process = 1; |
45 break; | 45 break; |
46 default: | 46 default: |
47 usage(); | 47 usage(); |
48 exit(EXIT_FAILURE); | 48 exit(EXIT_FAILURE); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 if (optind+1 >= argc) { | 52 if (optind+1 >= argc) { |
53 // no more items on the cmd | 53 // no more items on the cmd |
54 usage(); | 54 usage(); |
55 exit(EXIT_FAILURE); | 55 exit(EXIT_FAILURE); |
56 } | 56 } |
57 fname = argv[optind]; | 57 fname = argv[optind]; |
58 sid = argv[optind+1]; | 58 sid = argv[optind+1]; |
59 id = (unsigned int)strtol(sid, NULL, 0); | 59 id = (unsigned int)strtol(sid, NULL, 0); |
60 | 60 |
61 DEBUG_MAIN(("Opening file\n")); | 61 DEBUG_MAIN(("Opening file\n")); |
62 memset(&pstfile, 0, sizeof(pstfile)); | 62 memset(&pstfile, 0, sizeof(pstfile)); |
63 if (pst_open(&pstfile, fname, "r")!=0) { | 63 if (pst_open(&pstfile, fname, "r")!=0) { |
64 DIE(("Error opening file\n")); | 64 DIE(("Error opening file\n")); |
65 } | 65 } |
66 | 66 |
67 DEBUG_MAIN(("Loading Index\n")); | 67 DEBUG_MAIN(("Loading Index\n")); |
68 if (pst_load_index(&pstfile) != 0) { | 68 if (pst_load_index(&pstfile) != 0) { |
69 DIE(("Error loading file index\n")); | 69 DIE(("Error loading file index\n")); |
70 } | 70 } |
71 | 71 |
72 // if ((ptr = _pst_getID(&pstfile, id)) == NULL) { | 72 // if ((ptr = _pst_getID(&pstfile, id)) == NULL) { |
73 // DIE(("id not found [%#x]\n", id)); | 73 // DIE(("id not found [%#x]\n", id)); |
74 // } | 74 // } |
75 | 75 |
76 DEBUG_MAIN(("Loading block\n")); | 76 DEBUG_MAIN(("Loading block\n")); |
77 | 77 |
78 if ((readSize = _pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) { | 78 if ((readSize = _pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) { |
79 // if ((readSize = _pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) { | 79 // if ((readSize = _pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) { |
80 DIE(("Error loading block\n")); | 80 DIE(("Error loading block\n")); |
81 } | 81 } |
82 if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize); | 82 if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize); |
83 | 83 |
84 if (decrypt!=0) | 84 if (decrypt!=0) |
85 if (_pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) { | 85 if (_pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) { |
86 DIE(("Error decrypting block\n")); | 86 DIE(("Error decrypting block\n")); |
87 } | 87 } |
88 | 88 |
89 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize)); | 89 DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize)); |
90 if (binary==0) { | 90 if (binary==0) { |
91 _pst_debug_hexdump(stdout, buf, readSize, 0x10); | 91 _pst_debug_hexdump(stdout, buf, readSize, 0x10, 0); |
92 } else { | 92 } else { |
93 if (fwrite(buf, 1, readSize, stdout) != 0) { | 93 if (fwrite(buf, 1, readSize, stdout) != 0) { |
94 DIE(("Error occured during writing of buf to stdout\n")); | 94 DIE(("Error occured during writing of buf to stdout\n")); |
95 } | 95 } |
96 } | 96 } |
97 free(buf); | 97 free(buf); |
98 | 98 |
99 if (process!=0) { | 99 if (process!=0) { |
100 DEBUG_MAIN(("Parsing block...\n")); | 100 DEBUG_MAIN(("Parsing block...\n")); |
101 ptr = pstfile.d_head; | 101 ptr = pstfile.d_head; |
102 while(ptr != NULL) { | 102 while(ptr != NULL) { |
103 if (ptr->list_index != NULL && ptr->list_index->id == id) | 103 if (ptr->list_index != NULL && ptr->list_index->id == id) |
104 break; | 104 break; |
105 if (ptr->desc != NULL && ptr->desc->id == id) | 105 if (ptr->desc != NULL && ptr->desc->id == id) |
106 break; | 106 break; |
107 ptr = pst_getNextDptr(ptr); | 107 ptr = pst_getNextDptr(ptr); |
108 } | 108 } |
109 if (ptr == NULL) { | 109 if (ptr == NULL) { |
110 ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll)); | 110 ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll)); |
111 ptr->desc = _pst_getID(&pstfile, id); | 111 ptr->desc = _pst_getID(&pstfile, id); |
112 ptr->list_index = NULL; | 112 ptr->list_index = NULL; |
113 } | 113 } |
114 if (ptr != NULL) { | 114 if (ptr != NULL) { |
115 if ((item = _pst_parse_item(&pstfile, ptr)) != NULL) | 115 if ((item = _pst_parse_item(&pstfile, ptr)) != NULL) |
116 _pst_freeItem(item); | 116 _pst_freeItem(item); |
117 } else { | 117 } else { |
118 DEBUG_MAIN(("item not found with this ID\n")); | 118 DEBUG_MAIN(("item not found with this ID\n")); |
119 printf("Cannot find the owning Record of this ID. Cannot parse\n"); | 119 printf("Cannot find the owning Record of this ID. Cannot parse\n"); |
120 } | 120 } |
121 } | 121 } |
122 | 122 |
123 if(pst_close(&pstfile)!=0) { | 123 if(pst_close(&pstfile)!=0) { |
124 DIE(("pst_close failed\n")); | 124 DIE(("pst_close failed\n")); |
125 } | 125 } |
126 | 126 |
127 DEBUG_RET(); | 127 DEBUG_RET(); |
128 return 0; | 128 return 0; |
129 } | 129 } |