diff src/getidblock.c @ 73:3cb02cb1e6cd stable-0-6-10

Patch from Robert Simpson to fix doubly-linked list in the cache_ptr code, and allow arrays of unicode strings (without converting them). More changes for Fedora packaging (#434727) Fixes for const correctness.
author Carl Byington <carl@five-ten-sg.com>
date Thu, 29 May 2008 18:51:02 -0700
parents 7d5c637aaafb
children 987aa872294e
line wrap: on
line diff
--- a/src/getidblock.c	Fri May 16 09:06:17 2008 -0700
+++ b/src/getidblock.c	Thu May 29 18:51:02 2008 -0700
@@ -15,126 +15,128 @@
 
 static void usage();
 
-int main(int argc, char ** argv) {
-  // pass the id number to display on the command line
-  char *fname, *sid;
-  pst_file pstfile;
-  unsigned int id;
-  int decrypt = 0, process = 0, binary = 0, c;
-  char *buf = NULL;
-  size_t readSize;
-  pst_item *item;
-  pst_desc_ll* ptr;
+int main(int argc, char **argv)
+{
+    // pass the id number to display on the command line
+    char *fname, *sid;
+    pst_file pstfile;
+    unsigned int id;
+    int decrypt = 0, process = 0, binary = 0, c;
+    char *buf = NULL;
+    size_t readSize;
+    pst_item *item;
+    pst_desc_ll *ptr;
 
-  DEBUG_INIT("getidblock.log");
-  DEBUG_REGISTER_CLOSE();
-  DEBUG_ENT("main");
+    DEBUG_INIT("getidblock.log");
+    DEBUG_REGISTER_CLOSE();
+    DEBUG_ENT("main");
 
-  while ((c = getopt(argc, argv, "bdp")) != -1) {
-	switch (c) {
-	case 'b':
-	  // enable binary output
-	  binary = 1;
-	  break;
-	case 'd':
-	  //enable decrypt
-	  decrypt = 1;
-	  break;
-	case 'p':
-	  // enable procesing of block
-	  process = 1;
-	  break;
-	default:
-	  usage();
-	  exit(EXIT_FAILURE);
-	}
-  }
+    while ((c = getopt(argc, argv, "bdp")) != -1) {
+        switch (c) {
+            case 'b':
+                // enable binary output
+                binary = 1;
+                break;
+            case 'd':
+                //enable decrypt
+                decrypt = 1;
+                break;
+            case 'p':
+                // enable procesing of block
+                process = 1;
+                break;
+            default:
+                usage();
+                exit(EXIT_FAILURE);
+        }
+    }
 
-  if (optind+1 >= argc) {
-	// no more items on the cmd
-	usage();
-	exit(EXIT_FAILURE);
-  }
-  fname = argv[optind];
-  sid = argv[optind+1];
-  id = (unsigned int)strtol(sid, NULL, 0);
+    if (optind + 1 >= argc) {
+        // no more items on the cmd
+        usage();
+        exit(EXIT_FAILURE);
+    }
+    fname = argv[optind];
+    sid = argv[optind + 1];
+    id = (unsigned int) strtol(sid, NULL, 0);
 
-  DEBUG_MAIN(("Opening file\n"));
-  memset(&pstfile, 0, sizeof(pstfile));
-  if (pst_open(&pstfile, fname)) {
-	DIE(("Error opening file\n"));
-  }
-
-  DEBUG_MAIN(("Loading Index\n"));
-  if (pst_load_index(&pstfile) != 0) {
-	DIE(("Error loading file index\n"));
-  }
+    DEBUG_MAIN(("Opening file\n"));
+    memset(&pstfile, 0, sizeof(pstfile));
+    if (pst_open(&pstfile, fname)) {
+        DIE(("Error opening file\n"));
+    }
 
-  //  if ((ptr = pst_getID(&pstfile, id)) == NULL) {
-  //	DIE(("id not found [%#x]\n", id));
-  //  }
-
-  DEBUG_MAIN(("Loading block\n"));
+    DEBUG_MAIN(("Loading Index\n"));
+    if (pst_load_index(&pstfile) != 0) {
+        DIE(("Error loading file index\n"));
+    }
+    //  if ((ptr = pst_getID(&pstfile, id)) == NULL) {
+    //    DIE(("id not found [%#x]\n", id));
+    //  }
 
-  if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) {
-	//	if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) {
-	DIE(("Error loading block\n"));
-  }
-  if (binary==0) printf("Block %#x, size %#x[%i]\n",id, (unsigned int)readSize, (int) readSize);
+    DEBUG_MAIN(("Loading block\n"));
 
-  if (decrypt!=0)
-	if (pst_decrypt(buf, readSize, (int)pstfile.encryption) != 0) {
-	  DIE(("Error decrypting block\n"));
-	}
+    if ((readSize = pst_ff_getIDblock(&pstfile, id, &buf)) <= 0 || buf == NULL) {
+        //      if ((readSize = pst_read_block_size(&pstfile, ptr->offset, ptr->size, &buf, 1, 1)) < ptr->size) {
+        DIE(("Error loading block\n"));
+    }
+    if (binary == 0)
+        printf("Block %#x, size %#x[%i]\n", id, (unsigned int) readSize, (int) readSize);
 
-  DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
-  if (binary==0) {
-	pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
-  } else {
-	if (fwrite(buf, 1, readSize, stdout) != 0) {
-	  DIE(("Error occured during writing of buf to stdout\n"));
-	}
-  }
-  free(buf);
+    if (decrypt != 0)
+        if (pst_decrypt(buf, readSize, (int) pstfile.encryption) != 0) {
+            DIE(("Error decrypting block\n"));
+        }
 
-  if (process!=0) {
-	DEBUG_MAIN(("Parsing block...\n"));
-	ptr = pstfile.d_head;
-	while(ptr != NULL) {
-	  if (ptr->list_index != NULL && ptr->list_index->id == id)
-	break;
-	  if (ptr->desc != NULL && ptr->desc->id == id)
-	break;
-	  ptr = pst_getNextDptr(ptr);
-	}
-	if (ptr == NULL) {
-	  ptr = (pst_desc_ll*)xmalloc(sizeof(pst_desc_ll));
-	  ptr->desc = pst_getID(&pstfile, id);
-	  ptr->list_index = NULL;
-	}
-	if (ptr != NULL) {
-	  if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
-	pst_freeItem(item);
-	} else {
-	  DEBUG_MAIN(("item not found with this ID\n"));
-	  printf("Cannot find the owning Record of this ID. Cannot parse\n");
-	}
-  }
+    DEBUG_MAIN(("Printing block... [id %#x, size %#x]\n", id, readSize));
+    if (binary == 0) {
+        pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
+    } else {
+        if (fwrite(buf, 1, readSize, stdout) != 0) {
+            DIE(("Error occured during writing of buf to stdout\n"));
+        }
+    }
+    free(buf);
 
-  if(pst_close(&pstfile)!=0) {
-	DIE(("pst_close failed\n"));
-  }
+    if (process != 0) {
+        DEBUG_MAIN(("Parsing block...\n"));
+        ptr = pstfile.d_head;
+        while (ptr != NULL) {
+            if (ptr->list_index != NULL && ptr->list_index->id == id)
+                break;
+            if (ptr->desc != NULL && ptr->desc->id == id)
+                break;
+            ptr = pst_getNextDptr(ptr);
+        }
+        if (ptr == NULL) {
+            ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
+            ptr->desc = pst_getID(&pstfile, id);
+            ptr->list_index = NULL;
+        }
+        if (ptr != NULL) {
+            if ((item = pst_parse_item(&pstfile, ptr)) != NULL)
+                pst_freeItem(item);
+        } else {
+            DEBUG_MAIN(("item not found with this ID\n"));
+            printf("Cannot find the owning Record of this ID. Cannot parse\n");
+        }
+    }
 
-  DEBUG_RET();
-  return 0;
+    if (pst_close(&pstfile) != 0) {
+        DIE(("pst_close failed\n"));
+    }
+
+    DEBUG_RET();
+    return 0;
 }
 
-void usage() {
-  printf("usage: getidblock [options] filename id\n");
-  printf("\tfilename - name of the file to access\n");
-  printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
-  printf("\toptions\n");
-  printf("\t\t-d\tDecrypt the block before printing\n");
-  printf("\t\t-p\tProcess the block before finishing.\n");
-  printf("\t\t\tView the debug log for information\n");
+void usage()
+{
+    printf("usage: getidblock [options] filename id\n");
+    printf("\tfilename - name of the file to access\n");
+    printf("\tid - ID of the block to fetch - can begin with 0x for hex\n");
+    printf("\toptions\n");
+    printf("\t\t-d\tDecrypt the block before printing\n");
+    printf("\t\t-p\tProcess the block before finishing.\n");
+    printf("\t\t\tView the debug log for information\n");
 }