Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

getidblock.c

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

Generated on Tue Aug 5 12:06:13 2008 for 'LibPst' by  doxygen 1.3.9.1