libassa  3.5.1
MemDump.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // MemDump.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1997-2002 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // $Source: /cvsroot/libassa/libassa/assa/MemDump.cpp,v $
13 // $Revision: 1.6 $
14 // $Locker: $
15 // $Author: vlg $
16 // $Date: 2006/07/20 02:30:54 $
17 //------------------------------------------------------------------------------
18 
19 #include "assa/Logger.h"
20 #include "assa/MemDump.h"
21 
22 using namespace ASSA;
23 
24 const char MemDump::m_empty_str[] = "Null";
25 
27 MemDump(const char* msg_, int len_) : m_dump (NULL)
28 {
29  register int i; // ptr into source buffer
30  register int j; // pair elements counter
31  register int k; // pairs counter [0;16[
32 
33  const char *p; // ptr into source buffer
34  char *hex; // hex ptr into destination buffer
35  char *ascii; // ascii ptr into destination buffer
36 
37  long final_len;
38 
39  /*--- Precondition --- */
40  if (len_ <= 0 || msg_ == (char*) NULL) {
41  DL((ASSAERR,"No data to process.\n"));
42  DL((ASSAERR,"Data length requested: %d <= 0!\n", len_));
43  return;
44  }
45  j = k = 1;
46 
47  /*---
48  Each row holds 16 bytes of data. It requres 74 characters maximum.
49  Here's some examples:
50 
51 0 1 2 3 4 5 6 7
52 0123456789012345678901234567890123456789012345678901234567890123456789012
53 -------------------------------------------------------------------------
54 3132 3037 3039 3039 3031 3130 3839 3033 1207090901108903
55 3038 3132 3030 3331 3030 0d0a 3839 3033 0812003100\r\n8903
56 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n
57 
58  If all 16 bytes are control characters, the ASCII representation
59  will extend line to 72 characters plus cartrige return and line
60  feed at the end of the line.
61 
62  If len_ is not multiple of 16, we add one more row and another row
63  just to be on a safe side.
64  ---*/
65 
66  final_len = (int (len_/16) + 1 + (len_ % 16 ? 1:0)) * 74;
67 
68  m_dump = new char[final_len];
69  memset (m_dump, ' ', final_len);
70 
71  p = msg_; // ptr to original image
72  hex = m_dump; // current ptr to hex image
73  ascii = m_dump + 41; // current ptr to ascii image
74 
75  for (i = 0; i < len_; i++)
76  {
77  sprintf(hex,"%01x%01x", p[i] >> 4 & 0x0f, p[i] & 0x0f);
78  hex+=2;
79 
80  if (p[i] == '\n') { sprintf(ascii,"\\n"); ascii+=2; }
81  else if (p[i] == '\t') { sprintf(ascii,"\\t"); ascii+=2; }
82  else if (p[i] == '\v') { sprintf(ascii,"\\v"); ascii+=2; }
83  else if (p[i] == '\b') { sprintf(ascii,"\\b"); ascii+=2; }
84  else if (p[i] == '\r') { sprintf(ascii,"\\r"); ascii+=2; }
85  else if (p[i] == '\f') { sprintf(ascii,"\\f"); ascii+=2; }
86  else if (p[i] == '\a') { sprintf(ascii,"\\a"); ascii+=2; }
87  else if (p[i] == '\0') { sprintf(ascii,"\\0"); ascii+=2; }
88  else {
89  sprintf (ascii++,"%c", ((p[i] < ' ' || p [i] > '~') ? '.' : p [i]));
90  }
91 
92  if (!(j++ % 2)) {
93  sprintf (hex++," ");
94  }
95 
96  k %= 16;
97 
98  if (!(k++)) {
99  *hex = ' ';
100  sprintf (ascii++,"\n");
101  hex = ascii;
102  ascii += 41;
103  }
104  }
105  *hex = ' ';
106  m_dump [final_len-1] = '\0';
107 }
108 
109 void
111 dump_to_log (unsigned long mask_, const char* info_, const char* msg_, int len_)
112 {
113  /* A very important shortcut (performance-wise)
114  * It saves on constructing unnecessary MemDump object when
115  * message logging for that particular group is disabled.
116  */
117 
118  if (LOGGER->group_enabled (static_cast<Group> (mask_)) && len_ > 0)
119  {
120  MemDump temp (msg_, len_);
121  DL((mask_, "(%d bytes) %s\n", len_, info_));
122  DL((mask_, "\n\n%s\n\n", temp.getMemDump ()));
123  }
124 }
125 
An abstraction to message logging facility.
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define LOGGER
A shortcut to locate a singleton object of class Logger.
Definition: Logger.h:136
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
static const char m_empty_str[]
static Null string
Definition: MemDump.h:47
MemDump(const char *msg_, int len_)
Constructor converts original binary image to hex and ascii representation, and stores resultant imag...
Definition: MemDump.cpp:27
const char * getMemDump() const
Obtain a pointer to the dump image (null-terminated char string).
Definition: MemDump.h:94
char * m_dump
pointer to converted image
Definition: MemDump.h:44
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.
Definition: MemDump.cpp:111
Definition: Acceptor.h:40
Group
Definition: LogMask.h:25
@ ASSAERR
ASSA and system errors
Definition: LogMask.h:34