Skip to content

Instantly share code, notes, and snippets.

@perillamint
Created July 11, 2019 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perillamint/0179fbab1ff230686b35423316e6f9a6 to your computer and use it in GitHub Desktop.
Save perillamint/0179fbab1ff230686b35423316e6f9a6 to your computer and use it in GitHub Desktop.
Dumpcode for Android JNI
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#define PRINTBUFSZ 512
#define REMAINBUFSZ(start, end) PRINTBUFSZ - (end - start)
#define PRINTLN(...) __android_log_print(ANDROID_LOG_INFO, "FUCKING HEXDUMP", "%s\n", __VA_ARGS__)
#define PRINTBUF(printbuf, printbufptr) PRINTLN(printbuf); printbufptr = printbuf
#define PRINTTOBUF(printbuf, printbufptr, ...) snprintf(printbufptr, REMAINBUFSZ(printbuf, printbufptr), __VA_ARGS__); printbufptr = findNull(printbufptr)
static int isprintable(unsigned char c) {
return (0x20 <= c && 0x7E >=c);
}
static unsigned char getPrintableChar (unsigned char c) {
if(isprintable(c)) {
return c;
} else {
return '.';
}
}
static unsigned char* findNull(unsigned char* foo) {
while(*foo != 0x00) {
foo++;
}
return foo;
}
void dumpcode(void *buf, int len) {
char *buff = (char*) buf;
char printbuf[PRINTBUFSZ];
char *printbufptr = printbuf;
PRINTLN("----------BEGIN DUMP----------");
int i = 0;
for(i = 0; i < len; i++) {
if(i % 16 == 0) {
#if UINTPTR_MAX == 0xFFFFFFFFFFFFFFFF
PRINTTOBUF(printbuf, printbufptr, "0x%016X ", (void*)&buff[i]);
#elif UINTPTR_MAX == 0xFFFFFFFF
PRINTTOBUF(printbuf, printbufptr, "0x%08X ", (void*)&buff[i]);
#else
PRINTTOBUF(printbuf, printbufptr, "%p ", (void*)&buff[i]);
#endif
}
PRINTTOBUF(printbuf, printbufptr, "%02X ", buff[i] & 0xFF);
if(i % 16 - 15 == 0)
{
PRINTTOBUF(printbuf, printbufptr, " ");
for(int j = i - 15; j <= i; j++) {
PRINTTOBUF(printbuf, printbufptr, "%c", getPrintableChar(buff[j]));
}
PRINTBUF(printbuf, printbufptr);
}
}
if(i % 16 != 0)
{
int spaces=(len-i+16-i%16)*3+2;
for(int j = 0; j < spaces; j++) {
PRINTTOBUF(printbuf, printbufptr, " ");
}
for(int j = i-i%16; j < len; j++) {
PRINTTOBUF(printbuf, printbufptr, "%c", getPrintableChar(buff[j]));
}
PRINTBUF(printbuf, printbufptr);
}
PRINTLN("---------- END DUMP ----------");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment