Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1.  
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #define BYTES_A_ROW         ((int)10)
  8. #define ADDR_COLUMN_WIDTH   ((int)(2*sizeof(unsigned char *)))  
  9.                 //num bytes of a pointer * 2 char; (1 byte = hex 2 chars)
  10. #define BYTE_COLUMN_WIDTH   ((int)(BYTES_A_ROW * 3))
  11.                 // 1 bytes is 2 hex chars + some spaces
  12. #define COLUMN_SEPARATOR    " "
  13.  
  14. typedef unsigned char byte_t;
  15. void printOut(unsigned long start_addr, int length);
  16. void printHeader();
  17.  
  18.  
  19. int main(int argc, char *argv[] )
  20.  
  21. {
  22.     int first_var = 0xaaaaaaaa; // do not change this declaration!!!
  23.     unsigned long start_addr;
  24.     int number_of_bytes;
  25.  if(argc == 3)
  26.     {
  27.         sscanf(argv[1], "%16lx", &start_addr);
  28.         sscanf(argv[2], "%3d", &number_of_bytes);
  29.         printf("\n\nnumber = %3d", number_of_bytes);
  30.         printHeader();
  31.         printOut(start_addr, number_of_bytes);
  32.     }
  33. else if(argc == 1)
  34.     {
  35.         // print some reference addresses
  36.         printf("FYI: address of main function in memory: %p\n", main );
  37.         printf("FYI: address of argc: %p\n", &argc );
  38.         printf("FYI: address of first local variable of main(): %p\n", &first_var );
  39.  
  40.         // read start address and memory size
  41.         printf("enter start address <hex-notation> of dump: ");
  42.         scanf("%16lx", &start_addr);
  43.    
  44.    
  45.         printf("enter number of bytes to dump <negative or positive value>: " );
  46.         scanf("%3d", &number_of_bytes);
  47.        
  48.         printHeader();
  49.         printOut(start_addr, number_of_bytes);
  50.     }
  51.     else
  52.     {
  53.         printf("please provide a valid number of arguments.\n");
  54.         return 0;
  55.     }
  56.    
  57.    
  58.    
  59.     printf("\n");    
  60.     return 0;
  61. }
  62.  
  63. void printHeader(){
  64.     int i;
  65.     // print table header
  66.     printf("\n%-*s", ADDR_COLUMN_WIDTH, "Address");
  67.     printf("%s", COLUMN_SEPARATOR);
  68.     printf("%-*s", BYTE_COLUMN_WIDTH, "Bytes");
  69.     printf("%s", COLUMN_SEPARATOR);
  70.     printf("%-*s\n", BYTES_A_ROW, "Chars");
  71.  
  72.     for( i= 0; i < ADDR_COLUMN_WIDTH; i++)
  73.         printf("-");
  74.     printf("%s", COLUMN_SEPARATOR);
  75.     for( i= 0; i < BYTE_COLUMN_WIDTH; i++)
  76.         printf("-");
  77.     printf("%s", COLUMN_SEPARATOR);
  78.     for( i= 0; i < BYTES_A_ROW; i++)    // number of chars is char-column is exactly BYTES_A_ROW
  79.         printf("-");
  80. }
  81.  
  82. void printOut(unsigned long int start_addr, int length)
  83. {
  84.     byte_t *ptr;
  85.     ptr = (unsigned char*) start_addr;
  86.     int j = abs(length);
  87.     int i;
  88.     char charString[BYTES_A_ROW];
  89.    
  90.  
  91.    
  92.     while(j>0)
  93.     {
  94.         //We start by printing the start address of this row
  95.         printf("\n%.6X ", (unsigned int) ptr);
  96.         //Then we loop through the next ? elements
  97.         for(i=0; i<BYTES_A_ROW; i++)
  98.         {
  99.             //check wether or not we want to see the next byte
  100.             if(j!=0)
  101.             {
  102.                 //print byte
  103.                 printf("%.2X ", *ptr); 
  104.                
  105.                 //Check wether or not the char represents a byte
  106.                 if(isprint(*ptr))   {charString[i] = *ptr;}
  107.                 else                {charString[i] = '.';}
  108.                
  109.                 //check whether to inc/dec the pointer and do it
  110.                 if(length > 0)  {ptr++;}
  111.                 else            {ptr--;}
  112.                
  113.                 //decrement j
  114.                 j--;           
  115.             }
  116.             else
  117.             {
  118.                 printf("   ");
  119.                
  120.             }          
  121.         }
  122.         printf(" %s", charString);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement