mirror of
https://github.com/PoetryInCode/ViewBin.git
synced 2026-08-13 11:24:38 -04:00
initial commit
This commit is contained in:
commit
0a93d42379
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
./viewbin
|
||||
|
||||
.ccls-cache/
|
||||
7
Makefile
Normal file
7
Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
all: build ;
|
||||
|
||||
build: viewbin.c
|
||||
cc $^ -o viewbin
|
||||
|
||||
install:
|
||||
sudo cp viewbin /usr/local/bin/
|
||||
147
viewbin.c
Normal file
147
viewbin.c
Normal file
@ -0,0 +1,147 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define BINARY "%c%c%c%c%c%c%c%c"
|
||||
|
||||
#define BYTE_TO_BINARY(a) \
|
||||
(a & 0x80 ? '1' : '0'), \
|
||||
(a & 0x40 ? '1' : '0'), \
|
||||
(a & 0x20 ? '1' : '0'), \
|
||||
(a & 0x10 ? '1' : '0'), \
|
||||
(a & 0x08 ? '1' : '0'), \
|
||||
(a & 0x04 ? '1' : '0'), \
|
||||
(a & 0x02 ? '1' : '0'), \
|
||||
(a & 0x01 ? '1' : '0')
|
||||
|
||||
#define HEX 0
|
||||
#define INT 1
|
||||
#define CHAR 2
|
||||
#define BIN 3
|
||||
|
||||
int strcmp(const char *str1, const char *str2) {
|
||||
while(*str1 && *str1 == *str2) {
|
||||
++str1;
|
||||
++str2;
|
||||
}
|
||||
return (int)(unsigned char)(*str1) - (int)(unsigned char)(*str2);
|
||||
}
|
||||
|
||||
void help() {
|
||||
printf("\nviewbin [file name]: View the contents of a file as raw data\n");
|
||||
printf("\t-i,--integer\n\t\tDisplay the contents of the file in integer form\n");
|
||||
printf("\t-c,--char\n\t\tDisplays the contents of the file as characters\n");
|
||||
printf("\t-b,--binary\n\t\tDisplays the contents of the file in binary format\n");
|
||||
printf("\t-h,--help\n\t\tDisplays this help message\n\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int print_mode=HEX,file_index=1,file_found=0;
|
||||
if(!(argc > 1)) {
|
||||
printf("[ERROR] No file provided");
|
||||
help();
|
||||
return -1;
|
||||
} else {
|
||||
int i=1;
|
||||
while(i < argc) {
|
||||
if(argv[i][0] != '-') {
|
||||
file_found=1;
|
||||
}
|
||||
if(strcmp(argv[i],"-h") == 0 || strcmp(argv[i],"--help") == 0) {
|
||||
help();
|
||||
return -1;
|
||||
} else if(strcmp(argv[i],"-i") == 0 || strcmp(argv[i],"--integer") == 0) {
|
||||
print_mode=INT;
|
||||
if(!file_found) file_index++;
|
||||
} else if(strcmp(argv[i],"-c") == 0 || strcmp(argv[i],"--char") == 0) {
|
||||
print_mode=CHAR;
|
||||
if(!file_found) file_index++;
|
||||
} else if(strcmp(argv[i],"-b") == 0 || strcmp(argv[i],"--binary") == 0) {
|
||||
print_mode=BIN;
|
||||
if(!file_found) file_index++;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
FILE *file;
|
||||
if(file_found) {
|
||||
file = fopen(argv[file_index],"r");
|
||||
}
|
||||
if(!file) {
|
||||
printf("[ERROR] Could not open file");
|
||||
return -1;
|
||||
}
|
||||
fseek(file,0,SEEK_END);
|
||||
int length = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
struct winsize w;
|
||||
ioctl(0, TIOCGWINSZ, &w);
|
||||
|
||||
int width=w.ws_col, height=w.ws_row;
|
||||
|
||||
int chunk_size=3;
|
||||
if(print_mode == INT) {
|
||||
chunk_size=9;
|
||||
} else if(print_mode == BIN) {
|
||||
chunk_size=9;
|
||||
} else if(print_mode == CHAR) {
|
||||
chunk_size=2;
|
||||
}
|
||||
|
||||
int max_per_row=(int)((width-12)/chunk_size);
|
||||
int lines = length/max_per_row;
|
||||
|
||||
if(length%max_per_row != 0) {
|
||||
lines++;
|
||||
}
|
||||
|
||||
char *text = (char *)malloc(length*sizeof(char));
|
||||
size_t original = (size_t)text;
|
||||
fread(text,sizeof(char),length,file);
|
||||
int i=0,byte_index=0,bytes_shown=0;
|
||||
while(i < lines) {
|
||||
printf("[%08x]> ",byte_index);
|
||||
int a=0;
|
||||
bytes_shown=0;
|
||||
while(a<max_per_row) {
|
||||
if(text <= *(&text+1) || *text != '\0') {
|
||||
if(print_mode == HEX) {
|
||||
printf("%x%x ",(char)(*text & 0x0F),(char)(*text >> 4 & 0x0F));
|
||||
} else if(print_mode == INT) {
|
||||
printf("%08i ",(int)*text);
|
||||
} else if(print_mode == CHAR) {
|
||||
if(*text == '\n') {
|
||||
printf("\\n ");
|
||||
a++;
|
||||
} else if(*text == '\t') {
|
||||
printf("\\t ");
|
||||
a++;
|
||||
} else {
|
||||
printf("%c ",*text);
|
||||
}
|
||||
} else if(print_mode == BIN) {
|
||||
printf(BINARY" ",BYTE_TO_BINARY(*text));
|
||||
}
|
||||
bytes_shown++;
|
||||
text++;
|
||||
} else {
|
||||
break;
|
||||
break;
|
||||
}
|
||||
a++;
|
||||
}
|
||||
byte_index+=bytes_shown;
|
||||
if(i < lines-1) {
|
||||
printf("\n");
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if((12+(bytes_shown*chunk_size)+11) < width) {
|
||||
printf("<[%08x]",byte_index);
|
||||
} else {
|
||||
printf("\n<[%08x]",byte_index);
|
||||
}
|
||||
free((void *)original);
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user