Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sys/stat.h>
- #include <unistd.h>
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
- #include <io.h>
- #include <stdio.h>
- #include <fcntl.h>
- #endif
- using namespace std;
- typedef unsigned char byte;
- void dump_stream(istream& reader, int width=16){
- byte b;
- byte c[width+1];
- for(int i=0;i<width+1;i++){c[i]='\x0';}
- int col=0;
- int offset=0;
- printf(" |");
- for(int i=0;i<width;i++){
- printf("%02X ",i);
- }
- printf("\n--------+");
- for(int i=0;i<width;i++){
- printf("---");
- }
- printf("\n");
- printf("%08X|",offset);
- while(!reader.eof()){
- b=reader.get();
- printf("%02X ",b);
- if(b>=32 && b<=127){c[col]=b;}
- else{c[col]='.';}
- col++;
- if(col>width-1){
- offset+=width;
- printf("|%s|\n%08X|",c,offset);
- col=0;
- }
- }
- while(col<width){
- printf(" ");
- c[col]='\x0';
- col++;
- }
- offset+=width;
- printf("|%s|\n%08X|\n",c,offset);
- }
- bool exists(const char *name){
- struct stat buf;
- if(stat(name,&buf)!=-1){return true;}
- return false;
- }
- int pow(int x,int y){
- if(y==0){return 1;}
- int r=x;
- for(int i=0;i<y-1;i++){
- r*=x;
- }
- return r;
- }
- int parseInt(char* text){
- int len=strlen(text);
- int t=0;
- int r=0;
- for(int i=len-1;i>-1;i--){
- r+=((int)text[i]-48)*pow(10,t);
- t++;
- }
- return r;
- }
- int main(int argc, char *argv[]){
- int opt=0;
- int argn=0;
- int width=16;
- while((opt=getopt(argc, argv, "hw:"))!=-1){
- argn++;
- switch(opt){
- case 'h':
- printf("usage: %s [options] <filename/s>\n",argv[0]);
- printf("If <filename/s> is omitted, stdin is used.\n");
- printf("Options:\n");
- printf("-h Shows this text.\n");
- printf("-w<N> Sets the dump's width to N bytes.\n");
- return 0;
- case 'w':
- width=parseInt(optarg);
- break;
- }
- }
- if(argc>=2+argn){
- for(int i=1;i<argc-argn;i++){
- char *fname=argv[argn+i];
- if(!exists(fname)){
- printf("File \"%s\" not found.\n",fname);
- return 1;
- }
- ifstream reader;
- reader.open(fname,ios_base::binary);
- dump_stream(reader,width);
- reader.close();
- }
- }
- else{
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
- _setmode(_fileno(stdin), _O_BINARY);
- dump_stream(cin,width);
- #else
- ifstream reader;
- reader.open("/dev/stdin",ios_base::binary);
- dump_stream(reader,width);
- reader.close();
- #endif
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment