#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A function to display an error message and then exit
void fatal( char * message) {
char error_message[ 100 ] ;
strcpy ( error_message, "[!!] Fatal Error " ) ;
strncat ( error_message, message, 83 ) ;
perror ( error_message) ;
exit ( - 1 ) ;
}
// An error checked malloc() wrapper function
void * ec_malloc( unsigned int size) {
void * ptr;
ptr = malloc ( size) ;
if ( ptr == NULL )
fatal( "in ec_malloc() on memory allocation" ) ;
return ptr;
}
// dumps raw memory in hex byte and printable split format
void dump( const unsigned char * data_buffer, const unsigned int length) {
unsigned char byte;
unsigned int i, j;
for ( i= 0 ; i < length; i++ ) {
byte = data_buffer[ i] ;
printf ( "%02x " , data_buffer[ i] ) ; // display byte in hex
if ( ( ( i% 16 ) == 15 ) || ( i== length- 1 ) ) {
for ( j= 0 ; j < 15 - ( i% 16 ) ; j++ )
printf ( " " ) ;
printf ( "| " ) ;
for ( j= ( i- ( i% 16 ) ) ; j <= i; j++ ) { // display printable bytes from line
byte = data_buffer[ j] ;
if ( ( byte > 31 ) && ( byte < 127 ) ) // outside printable char range
printf ( "%c" , byte) ;
else
printf ( "." ) ;
}
printf ( "\n " ) ; // end of the dump line (each line 16 bytes)
} // end if
} // end for
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKLy8gQSBmdW5jdGlvbiB0byBkaXNwbGF5IGFuIGVycm9yIG1lc3NhZ2UgYW5kIHRoZW4gZXhpdAp2b2lkIGZhdGFsKGNoYXIgKm1lc3NhZ2UpIHsKICAgY2hhciBlcnJvcl9tZXNzYWdlWzEwMF07CgogICBzdHJjcHkoZXJyb3JfbWVzc2FnZSwgIlshIV0gRmF0YWwgRXJyb3IgIik7CiAgIHN0cm5jYXQoZXJyb3JfbWVzc2FnZSwgbWVzc2FnZSwgODMpOwogICBwZXJyb3IoZXJyb3JfbWVzc2FnZSk7CiAgIGV4aXQoLTEpOwp9CgovLyBBbi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QkJCXByaW50ZigiLiIpOwoJCQl9CgkJCXByaW50ZigiXG4iKTsgLy8gZW5kIG9mIHRoZSBkdW1wIGxpbmUgKGVhY2ggbGluZSAxNiBieXRlcykKCQl9IC8vIGVuZCBpZgoJfSAvLyBlbmQgZm9yCn0K
compilation info
prog.cpp: In function 'void* ec_malloc(unsigned int)':
prog.cpp:20:50: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
fatal("in ec_malloc() on memory allocation");
^
/usr/lib/gcc/i586-linux-gnu/5/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout