MatteoTosato87

Simple web server with bug - hacking.h

Dec 24th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. /* hacking.h - simple web server */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. // A function to display an error message and then exit
  8. void fatal(char *message) {
  9.    char error_message[100];
  10.  
  11.    strcpy(error_message, "[!!] Fatal Error ");
  12.    strncat(error_message, message, 83);
  13.    perror(error_message);
  14.    exit(-1);
  15. }
  16.  
  17. // An error checked malloc() wrapper function
  18. void *ec_malloc(unsigned int size) {
  19.    void *ptr;
  20.    ptr = malloc(size);
  21.    if(ptr == NULL)
  22.       fatal("in ec_malloc() on memory allocation");
  23.    return ptr;
  24. }
  25.  
  26. // dumps raw memory in hex byte and printable split format
  27. void dump(const unsigned char *data_buffer, const unsigned int length) {
  28.     unsigned char byte;
  29.     unsigned int i, j;
  30.     for(i=0; i < length; i++) {
  31.         byte = data_buffer[i];
  32.         printf("%02x ", data_buffer[i]);  // display byte in hex
  33.         if(((i%16)==15) || (i==length-1)) {
  34.             for(j=0; j < 15-(i%16); j++)
  35.                 printf("   ");
  36.             printf("| ");
  37.             for(j=(i-(i%16)); j <= i; j++) {  // display printable bytes from line
  38.                 byte = data_buffer[j];
  39.                 if((byte > 31) && (byte < 127)) // outside printable char range
  40.                     printf("%c", byte);
  41.                 else
  42.                     printf(".");
  43.             }
  44.             printf("\n"); // end of the dump line (each line 16 bytes)
  45.         } // end if
  46.     } // end for
  47. }
Add Comment
Please, Sign In to add comment