00001
00002 #include "define.h"
00003
00004 int decrypt = 0, process = 0, binary = 0;
00005 pst_file pstfile;
00006
00007
00008 void usage();
00009 void usage()
00010 {
00011 printf("usage: getidblock [options] filename id\n");
00012 printf("\tfilename - name of the file to access\n");
00013 printf("\tid - ID of the block to fetch (0 to fetch all) - can begin with 0x for hex\n");
00014 printf("\toptions\n");
00015 printf("\t\t-d\tDecrypt the block before printing\n");
00016 printf("\t\t-p\tProcess the block before finishing.\n");
00017 printf("\t\t\tView the debug log for information\n");
00018 }
00019
00020
00021 void dumper(uint64_t id);
00022 void dumper(uint64_t id)
00023 {
00024 char *buf = NULL;
00025 size_t readSize;
00026 pst_desc_ll *ptr;
00027
00028 DEBUG_MAIN(("\n\n\nLooking at block index1 id %#"PRIx64"\n", id));
00029
00030 if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == 0) {
00031 DIE(("Error loading block\n"));
00032 }
00033
00034 if (decrypt)
00035 if (pst_decrypt(id, buf, readSize, (int) pstfile.encryption) != 0) {
00036 DIE(("Error decrypting block\n"));
00037 }
00038
00039 DEBUG_MAIN(("Printing block id %#"PRIx64", size %#"PRIx64"\n", id, (uint64_t)readSize));
00040 if (binary) {
00041 if (fwrite(buf, 1, readSize, stdout) != 0) {
00042 DIE(("Error occured during writing of buf to stdout\n"));
00043 }
00044 } else {
00045 printf("Block id %#"PRIx64", size %#"PRIx64"\n", id, (uint64_t)readSize);
00046 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
00047 }
00048 if (buf) free(buf);
00049
00050 if (process) {
00051 DEBUG_MAIN(("Parsing block id %#"PRIx64"\n", id));
00052 ptr = pstfile.d_head;
00053 while (ptr) {
00054 if (ptr->list_index && ptr->list_index->id == id)
00055 break;
00056 if (ptr->desc && ptr->desc->id == id)
00057 break;
00058 ptr = pst_getNextDptr(ptr);
00059 }
00060 if (!ptr) {
00061 ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
00062 ptr->desc = pst_getID(&pstfile, id);
00063 ptr->list_index = NULL;
00064 }
00065 pst_item *item = pst_parse_item(&pstfile, ptr, NULL);
00066 if (item) pst_freeItem(item);
00067 }
00068 }
00069
00070
00071 void dump_desc(pst_desc_ll *ptr);
00072 void dump_desc(pst_desc_ll *ptr)
00073 {
00074 while (ptr) {
00075 DEBUG_MAIN(("\n\n\nLooking at block desc id %#"PRIx64"\n", ptr->id));
00076 if (ptr->desc && ptr->desc->id) dumper(ptr->desc->id);
00077 if (ptr->list_index && ptr->list_index->id) dumper(ptr->list_index->id);
00078 if (ptr->child) dump_desc(ptr->child);
00079 ptr = ptr->next;
00080 }
00081 }
00082
00083
00084 int main(int argc, char* const* argv)
00085 {
00086
00087 char *fname, *sid;
00088 uint64_t id;
00089 int c;
00090
00091 DEBUG_INIT("getidblock.log");
00092 DEBUG_REGISTER_CLOSE();
00093 DEBUG_ENT("main");
00094
00095 while ((c = getopt(argc, argv, "bdp")) != -1) {
00096 switch (c) {
00097 case 'b':
00098
00099 binary = 1;
00100 break;
00101 case 'd':
00102
00103 decrypt = 1;
00104 break;
00105 case 'p':
00106
00107 process = 1;
00108 break;
00109 default:
00110 usage();
00111 exit(EXIT_FAILURE);
00112 }
00113 }
00114
00115 if (optind + 1 >= argc) {
00116
00117 usage();
00118 exit(EXIT_FAILURE);
00119 }
00120 fname = argv[optind];
00121 sid = argv[optind + 1];
00122 id = (uint64_t)strtoll(sid, NULL, 0);
00123
00124 DEBUG_MAIN(("Opening file\n"));
00125 memset(&pstfile, 0, sizeof(pstfile));
00126 if (pst_open(&pstfile, fname)) {
00127 DIE(("Error opening file\n"));
00128 }
00129
00130 DEBUG_MAIN(("Loading Index\n"));
00131 if (pst_load_index(&pstfile) != 0) {
00132 DIE(("Error loading file index\n"));
00133 }
00134
00135 if (id) {
00136 dumper(id);
00137 }
00138 else {
00139 pst_index_ll *ptr = pstfile.i_head;
00140 while (ptr) {
00141 dumper(ptr->id);
00142 ptr = ptr->next;
00143 }
00144 dump_desc(pstfile.d_head);
00145 }
00146
00147 if (pst_close(&pstfile) != 0) {
00148 DIE(("pst_close failed\n"));
00149 }
00150
00151 DEBUG_RET();
00152 return 0;
00153 }
00154