Skip to content

Instantly share code, notes, and snippets.

@allencch
Created March 6, 2018 00:40
Show Gist options
  • Save allencch/82427849bff46bebfe964275e16ff220 to your computer and use it in GitHub Desktop.
Save allencch/82427849bff46bebfe964275e16ff220 to your computer and use it in GitHub Desktop.
Hex edit target for Android
/*************
Copyright (c) 2012, Allen Choong
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************/
/**
* @author Allen Choong
* @version 0.0.1
* @date 2012-03-05
*
* This is a simple hex editor. The motivation is to make a
* cross platform hex editor, targeted on Android, build on Linux,
* that can edit the file through adb shell.
* Because using external hex editor, such as hexedit, ghex, bless,
* they work on Linux, with GUI. Though hexedit works as command-line, it
* requires ncurses. ncurses does not work on "adb shell".
*
* Functions
* Dump
* From where to where
* Edit byte
* Edit bytes
* Interactive
*
* Todo:
* Interactive mode
* Search like grep
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ACT_INTERACTIVE 1
#define ACT_DUMP 2
#define ACT_EDIT 3
#define TYPE_HEX 1
#define TYPE_INT 2
unsigned char* buf=NULL;
int bufSize;
struct Options {
int act;
int location;
char* editStr;
int start;
int length;
char* file;
} options;
int isHex(char* str) {
if(strncmp(str,"0x",2) == 0) {
return TYPE_HEX;
}
return TYPE_INT;
}
/**
* Convert string to integer
*/
int strToInt(char* str) {
int type = isHex(str);
int ret;
switch(type) {
case TYPE_HEX:
sscanf(str,"%x",&ret);
break;
case TYPE_INT:
ret = atoi(str);
break;
}
return ret;
}
//Edit bytes
int editBytes(int loc,const char* str) {
if(loc > bufSize) {
printf("Location is exceeding the buffer size: %d/%d\n",loc,bufSize);
return 0;
}
//Get number of tokens
int len = strlen(str);
int count = 0;
int i;
for(i=0;i<len-1;i++) {
if(isxdigit(str[i]) && isspace(str[i+1]))
count++;
}
//Last token
if(isxdigit(str[len-1]))
count++;
int* tokens = (int*)malloc(count*sizeof(int)); //Don't know why
//cannot use char*. Once using char*, if the strlen is at 10,
// then will cause fault when free()
//Convert string to tokens
char* temp = (char*)malloc(strlen(str)+1); //Create another copy
strcpy(temp,str);
i = 0;
char* saveptr;
char* pch = strtok_r(temp," ",&saveptr);
while(pch) {
sscanf(pch,"%x",&tokens[i++]);
pch = strtok_r(NULL," ",&saveptr);
}
//Write the tokens to the memory
for(i=0;i<count;i++) {
buf[loc+i] = tokens[i];
}
free(temp);
free(tokens);
return 1;
}
/**
* @brief Dump hex
* @param start Default is 0
* @param length Default is 0
*/
void dump(int start,int length) {
int i,j;
//Adjust the start
int start2 = start/16;
//Print the first line
printf("\t ");
for(i=0;i<16;i++) {
printf("%02X ",i);
}
printf("\n");
char temp[17] = {0};
for(i=start2*16;i<bufSize;i++) {
if(length > 0 && i>=(start2+length)*16) {
break;
}
if(i%16 == 0) { //Begining
printf("%08X ",i);
}
printf("%02x ",buf[i]);
//Store to the temp for display
if(isprint(buf[i])) {
temp[i%16] = buf[i];
temp[i%16+1] = '\0';
}
else {
temp[i%16] = '.';
temp[i%16+1] = '\0';
}
if(i % 16 == 15) {
printf("| %s\n",temp);
strcpy(temp,"");
}
else if(i == bufSize-1) {
for(j=i%16+1;j<16;j++) {
printf(" ");
}
printf("| %s\n",temp);
strcpy(temp,"");
}
}
}
//Open file
unsigned char* myopen(const char* file) {
FILE* f = fopen(file,"rb");
if(!f) {
printf("Cannot open file: %s\n",file);
exit(1);
}
//Get file size
fseek(f,0,SEEK_END);
bufSize = ftell(f);
rewind(f);
buf = (unsigned char*)malloc(sizeof(unsigned char) * bufSize);
int result = fread(buf,1,bufSize,f);
if(result != bufSize) {
printf("Reading file error\n");
exit(1);
}
fclose(f);
return buf;
}
//Save file
int mysave(const char* file) {
FILE* f = fopen(file,"wb");
if(!f) {
printf("Cannot open file to write: %s\n",file);
return 0;
}
fwrite(buf,1,bufSize,f);
fclose(f);
return 1;
}
void help() {
printf("Usage: hed [options] file\n");
printf("Options:\n");
//printf("-a\t\t\t\tInteractive\n");
printf(" -d [start length]\t\tDump\n");
printf(" -e location \"hex string\"\tEdit\n");
}
void getOptions(int argc,char** argv) {
int i;
if(argc < 2) {
help();
exit(0);
}
//Default
options.act = ACT_DUMP;
options.start = 0;
options.length = 0;
for(i=1;i<argc-1;i++) {
if(strcmp(argv[i],"-a") == 0) {
options.act = ACT_INTERACTIVE;
}
else if(strcmp(argv[i],"-e") == 0) { //The argument must be start from 0x (for the hex address, this is
// different from the "med"
if(i+2 > argc) {
help();
exit(0);
}
options.act = ACT_EDIT;
options.location = strToInt(argv[++i]);
options.editStr = argv[++i];
}
else if(strcmp(argv[i],"-d") == 0) {
if(i+2 > argc) {
help();
exit(0);
}
options.act = ACT_DUMP;
options.start = strToInt(argv[++i]);
options.length = strToInt(argv[++i]);
}
else if(strcmp(argv[i],"--help") == 0) {
help();
exit(0);
}
}
options.file = argv[argc-1];
}
void interpretOptions() {
myopen(options.file);
switch(options.act) {
case ACT_EDIT:
editBytes(options.location,options.editStr);
mysave(options.file);
break;
case ACT_DUMP:
dump(options.start,options.length);
break;
}
}
int main(int argc,char** argv) {
getOptions(argc,argv);
interpretOptions();
//Free memory
if(!buf)
free(buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment