Author Topic: [C]HexDump  (Read 2846 times)

0 Members and 1 Guest are viewing this topic.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
[C]HexDump
« on: November 25, 2010, 08:13:36 pm »
A simple hexa dumper, I made it when coding some crypting soft and wanted to see changes:

Code: [Select]
/*
    Ca0s HexDump v1
    [st4ck-3rr0r.blogspot.com]
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#define COLUMNAS 16

int x, printToScreen=0;
char *originalFileName;
char *dumpFileName;
FILE *originalFile, *dumpFile;
struct stat myStat;
char *byte, *byte2;

int main(int argc, char *argv[])
{
  printf("\nCa0s HexDump v1\n\n");
  for(x=0; x<argc; x++)
  {
        if((strcmp(argv[x], "-file")==0) && (argc>(x+1))) originalFileName=argv[x+1];
        if((strcmp(argv[x], "-w")==0) && (argc>(x+1))) dumpFileName=argv[x+1];
        if(strcmp(argv[x], "-p")==0) printToScreen=1;
  }
  if((originalFileName==NULL) || (dumpFileName==NULL))
  {
        printf("Mandatory args:\n\t-file FILE\t\tFile to hex-dump\n\t-w FILE\t\tFile for logging result\nOptional args:\n\t-p\t\t\tPrints dump on shell\n");
        return 0;
  }
  originalFile=fopen(originalFileName, "rb");
  if(originalFile==NULL)
  {
        printf("Error opening file.\n");
        return 0;
  }
  dumpFile=fopen(dumpFileName, "w");
  if(dumpFile==NULL)
  {
        printf("Error opening log file.\n");
        return 0;
  }
  fstat(fileno(originalFile), &myStat);
  printf("File: %s\nSize: %d bytes\n\n", originalFileName, myStat.st_size);
  fprintf(dumpFile, "Ca0s HexDump v1\nFile: %s\nSize: %d bytes\n\n", originalFileName, myStat.st_size);
  byte=(char *)malloc(1);
  byte2=(char *)malloc(1);
  int cuenta=0;
  char *textDump=(char *)malloc(20*sizeof(char));
  for(x=0; x<myStat.st_size; x++)
  {
        fread(byte2, 1, 1, originalFile);
        memcpy(byte, byte2, 1);
        if(*byte==0x00) strncat(textDump, "..", 2);
        else if(*byte==0x0A) strncat(textDump, "\\n", 2);
        else strncat(textDump, byte, 1);
        fprintf(dumpFile, "%02hX ", (unsigned char)*byte);
        if(printToScreen==1) printf("%02hx ", *byte);
        cuenta++;
        if(cuenta>=COLUMNAS)
        {
            fprintf(dumpFile, "\t%s\n", textDump);
            if(printToScreen==1) printf("\t%s\n", textDump);
            memset(textDump, 0, sizeof(textDump));
            cuenta=0;
        }
  }
  if(cuenta!=0)
  {
           fprintf(dumpFile, "\t%s\n", textDump);
           if(printToScreen==1) printf("\t%s\n", textDump);
  }
  if(printToScreen==1) printf("\n\n");
  fclose(originalFile);
  fclose(dumpFile);
  printf("Finished. Dump saved in %s.\n", dumpFileName);
  return 0;
}


Usage: hexdump -file [file to dump] -w [file to write dump] -p {print to screen, not mandatory}
« Last Edit: November 25, 2010, 08:14:11 pm by ca0s »