libassa  3.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes
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 ASSA::ASSAERR, DL, and m_dump.

                                    : m_dump (NULL)
{
    register int i;     // ptr into source buffer
    register int j;     // pair elements counter
    register int k;     // pairs counter [0;16[
    
    const char *p;      // ptr into source buffer
    char *hex;      // hex ptr into destination buffer
    char *ascii;        // ascii ptr into destination buffer
    
    long final_len;

    /*--- Precondition ---  */
    if (len_ <= 0 || msg_ == (char*) NULL) {
        DL((ASSAERR,"No data to process.\n"));
        DL((ASSAERR,"Data length requested: %d <= 0!\n", len_));
        return;
    }
    j = k = 1;

    /*---
      Each row holds 16 bytes of data. It requres 74 characters maximum.
      Here's some examples:

0         1         2         3         4         5         6         7
0123456789012345678901234567890123456789012345678901234567890123456789012
-------------------------------------------------------------------------
3132 3037 3039 3039 3031 3130 3839 3033  1207090901108903
3038 3132 3030 3331 3030 0d0a 3839 3033  0812003100\r\n8903
0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a  \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n

      If all 16 bytes are control characters, the ASCII representation
      will extend line to 72 characters plus cartrige return and line 
      feed at the end of the line.

      If len_ is not multiple of 16, we add one more row and another row
      just to be on a safe side.
      ---*/
    
    final_len = (int (len_/16) + 1 + (len_ % 16 ? 1:0)) * 74;
    
    m_dump = new char[final_len];
    memset (m_dump, ' ', final_len);
    
    p     = msg_;               // ptr to original image
    hex   = m_dump;             // current ptr to hex image
    ascii = m_dump + 41;        // current ptr to ascii image
    
    for (i = 0; i < len_; i++) 
    {
        sprintf(hex,"%01x%01x", p[i] >> 4 & 0x0f, p[i] & 0x0f); 
        hex+=2;

        if      (p[i] == '\n') { sprintf(ascii,"\\n"); ascii+=2; }
        else if (p[i] == '\t') { sprintf(ascii,"\\t"); ascii+=2; }
        else if (p[i] == '\v') { sprintf(ascii,"\\v"); ascii+=2; }
        else if (p[i] == '\b') { sprintf(ascii,"\\b"); ascii+=2; }
        else if (p[i] == '\r') { sprintf(ascii,"\\r"); ascii+=2; }
        else if (p[i] == '\f') { sprintf(ascii,"\\f"); ascii+=2; }
        else if (p[i] == '\a') { sprintf(ascii,"\\a"); ascii+=2; }
        else if (p[i] == '\0') { sprintf(ascii,"\\0"); ascii+=2; }
        else {
            sprintf (ascii++,"%c", ((p[i] < ' ' || p [i] > '~') ? '.' : p [i]));
        }
        
        if (!(j++ % 2)) {
            sprintf (hex++," ");
        }

        k %= 16;

        if (!(k++)) {
            *hex = ' ';
            sprintf (ascii++,"\n");
            hex = ascii;
            ascii +=  41;
        }
    }
    *hex = ' ';
    m_dump [final_len-1] = '\0';
}

Destructor releases image memory.

Definition at line 84 of file MemDump.h.

References m_dump, and m_empty_str.

{
    if (m_dump && m_dump != m_empty_str) {
        delete [] m_dump;
    }
    m_dump = NULL;
}

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 111 of file MemDump.cpp.

References DL, getMemDump(), and LOGGER.

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

{
    /* A very important shortcut (performance-wise)
     * It saves on constructing unnecessary MemDump object when
     * message logging for that particular group is disabled.
     */

    if (LOGGER->group_enabled (static_cast<Group> (mask_)) && len_ > 0) 
    {       
        MemDump temp (msg_, len_);
        DL((mask_, "(%d bytes) %s\n", len_, info_));
        DL((mask_, "\n\n%s\n\n", temp.getMemDump ()));
    }
}
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::io_ptrs::dump(), ASSA::xdrIOBuffer::dump(), and dump_to_log().

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

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:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines