ASSA::MemDump Class Reference

#include <MemDump.h>

List of all members.

Public Member Functions

 MemDump (const char *msg_, int len_)
 Constructor converts original binary image to hex and ascii representation, and stores resultant image in internal buffer for later retrieval.
 ~MemDump ()
 Destructor releases image memory.
const char * getMemDump () const
 Obtain a pointer to the dump image (null-terminated char string).

Static Public Member Functions

static void dump_to_log (unsigned long mask_, const char *info_, const char *msg_, int len_)
 Write hex/ascii dump of a memory region to log file.

Private Attributes

char * m_dump
 pointer to converted image

Static Private Attributes

static const char m_empty_str [] = "Null"
 static Null string


Detailed Description

Definition at line 40 of file MemDump.h.


Constructor & Destructor Documentation

MemDump::MemDump const char *  msg_,
int  len_
 

Constructor converts original binary image to hex and ascii representation, and stores resultant image in internal buffer for later retrieval.

MemDump owns the image and will free the memory once it is out of scope

Parameters:
msg_ char pointer to original binary image.
len_ lenght of the binary image.

Definition at line 27 of file MemDump.cpp.

References DL, ASSA::ERROR, and m_dump.

00027                                     : m_dump (NULL)
00028 {
00029     register int i;     // ptr into source buffer
00030     register int j;     // pair elements counter
00031     register int k;     // pairs counter [0;16[
00032     
00033     const char *p;      // ptr into source buffer
00034     char *hex;      // hex ptr into destination buffer
00035     char *ascii;        // ascii ptr into destination buffer
00036     
00037     long final_len;
00038 
00039     /*--- Precondition ---  */
00040     if (len_ <= 0 || msg_ == (char*) NULL) {
00041         DL((ERROR,"No data to process.\n"));
00042         DL((ERROR,"Data length requested: %d <= 0!\n", len_));
00043         return;
00044     }
00045     j = k = 1;
00046 
00047     /*---
00048       Each row holds 16 bytes of data. It requres 74 characters maximum.
00049       Here's some examples:
00050 
00051 0         1         2         3         4         5         6         7
00052 0123456789012345678901234567890123456789012345678901234567890123456789012
00053 -------------------------------------------------------------------------
00054 3132 3037 3039 3039 3031 3130 3839 3033  1207090901108903
00055 3038 3132 3030 3331 3030 0d0a 3839 3033  0812003100\r\n8903
00056 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a  \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n
00057 
00058           If all 16 bytes are control characters, the ASCII representation
00059       will extend line to 72 characters plus cartrige return and line 
00060       feed at the end of the line.
00061 
00062       If len_ is not multiple of 16, we add one more row and another row
00063       just to be on a safe side.
00064       ---*/
00065     
00066     final_len = (int (len_/16) + 1 + (len_ % 16 ? 1:0)) * 74;
00067     
00068     m_dump = new char[final_len];
00069     memset(m_dump, ' ', final_len);
00070     
00071     p = msg_;           // ptr to original image
00072     hex = m_dump;           // current ptr to hex image
00073     ascii = m_dump + 41;        // current ptr to ascii image
00074     
00075     for(i=0; i<len_; i++) {
00076         sprintf(hex,"%01x%01x", p[i] >> 4 & 0x0f, p[i] & 0x0f); 
00077         hex+=2;
00078 
00079         if (p[i] == '\n') { sprintf(ascii,"\\n"); ascii+=2; }
00080         else if (p[i] == '\t') { sprintf(ascii,"\\t"); ascii+=2; }
00081         else if (p[i] == '\v') { sprintf(ascii,"\\v"); ascii+=2; }
00082         else if (p[i] == '\b') { sprintf(ascii,"\\b"); ascii+=2; }
00083         else if (p[i] == '\r') { sprintf(ascii,"\\r"); ascii+=2; }
00084         else if (p[i] == '\f') { sprintf(ascii,"\\f"); ascii+=2; }
00085         else if (p[i] == '\a') { sprintf(ascii,"\\a"); ascii+=2; }
00086         else if (p[i] == '\0') { sprintf(ascii,"\\0"); ascii+=2; }
00087         else sprintf(ascii++,"%c",((p[i]<' ' || p[i]>'~')?'.':p[i]));
00088         
00089         if (!(j++ % 2)) 
00090             sprintf(hex++," ");
00091         k %= 16;
00092         if (!(k++)) {
00093             *hex = ' ';
00094             sprintf(ascii++,"\n");
00095             hex = ascii;
00096             ascii +=  41;
00097         }
00098     }
00099     *hex = ' ';
00100     m_dump[final_len-1] = '\0';
00101     
00102 }

ASSA::MemDump::~MemDump  )  [inline]
 

Destructor releases image memory.

Definition at line 84 of file MemDump.h.

References m_dump, and m_empty_str.

00085 {
00086     if (m_dump && m_dump != m_empty_str) {
00087         delete [] m_dump;
00088     }
00089     m_dump = NULL;
00090 }


Member Function Documentation

void MemDump::dump_to_log unsigned long  mask_,
const char *  info_,
const char *  msg_,
int  len_
[static]
 

Write hex/ascii dump of a memory region to log file.

Parameters:
mask_ Debug mask as defined in debug.h
info_ Information string to annotate the hex memory dump
msg_ Pointer to the memory area
len_ Number of bytes

Definition at line 106 of file MemDump.cpp.

References DL, and LOGGER.

Referenced by ASSA::CharInBuffer::dump(), and ASSA::Socketbuf::underflow().

00107 {
00108     /* A very important shortcut (performance-wise)
00109      * It saves on constructing unnecessary MemDump object when
00110      * message logging for that particular group is disabled.
00111      */
00112 
00113     if (LOGGER->group_enabled (static_cast<Group> (mask_)) && len_ > 0) 
00114     {       
00115         MemDump temp (msg_, len_);
00116         DL((mask_, "(%d bytes) %s\n", len_, info_));
00117         DL((mask_, "\n\n%s\n\n", temp.getMemDump ()));
00118     }
00119 }

const char * ASSA::MemDump::getMemDump  )  const [inline]
 

Obtain a pointer to the dump image (null-terminated char string).

Returns:
pointer to converted memory area

Definition at line 94 of file MemDump.h.

References m_dump, and m_empty_str.

Referenced by ASSA::xdrIOBuffer::dump(), and ASSA::io_ptrs::dump().

00095 {
00096     return (m_dump ? (const char*) m_dump : m_empty_str);
00097 }


Member Data Documentation

char* ASSA::MemDump::m_dump [private]
 

pointer to converted image

Definition at line 44 of file MemDump.h.

Referenced by getMemDump(), MemDump(), and ~MemDump().

const char MemDump::m_empty_str = "Null" [static, private]
 

static Null string

Definition at line 47 of file MemDump.h.

Referenced by getMemDump(), and ~MemDump().


The documentation for this class was generated from the following files:
Generated on Thu Jun 22 12:39:21 2006 for libassa by  doxygen 1.4.6