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