18
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
#include <glib/gstring.h>
23
24
#include "mw_debug.h"
27
#define FRMT "%02x%02x "
28
#define BUF(n) ((unsigned char) buf[n])
29
#define FRMT2 FRMT1 FRMT1 " "
30
#define FRMT4 FRMT2 FRMT2
31
#define FRMT8 FRMT4 FRMT4
32
#define FRMT16 FRMT8 FRMT8
29
34
#define ADVANCE(b, n, c) {b += c; n -= c;}
33
38
/** writes hex pairs of buf to str */
34
static void t_pretty_print(GString *str, const char *buf, gsize len) {
37
g_string_append_printf(str,
38
FRMT FRMT FRMT FRMT FRMT FRMT FRMT FRMT "\n",
39
BUF(0), BUF(1), BUF(2), BUF(3),
40
BUF(4), BUF(5), BUF(6), BUF(7),
41
BUF(8), BUF(9), BUF(10), BUF(11),
42
BUF(12), BUF(13), BUF(14), BUF(15));
43
ADVANCE(buf, len, 16);
46
g_string_append_printf(str, FRMT "\n", BUF(0), BUF(1));
50
g_string_append_printf(str, FRMT, BUF(0), BUF(1));
54
g_string_append_printf(str, FRM "\n", BUF(0));
39
static void pretty_print(GString *str, const guchar *buf, gsize len) {
41
/* write a complete line */
42
g_string_append_printf(str, FRMT16,
43
buf[0], buf[1], buf[2], buf[3],
44
buf[4], buf[5], buf[6], buf[7],
45
buf[8], buf[9], buf[10], buf[11],
46
buf[12], buf[13], buf[14], buf[15]);
47
ADVANCE(buf, len, 16);
49
/* append \n to each line but the last */
50
if(len) g_string_append(str, "\n");
53
/* write an incomplete line */
55
g_string_append_printf(str, FRMT8,
56
buf[0], buf[1], buf[2], buf[3],
57
buf[4], buf[5], buf[6], buf[7]);
62
g_string_append_printf(str, FRMT4,
63
buf[0], buf[1], buf[2], buf[3]);
68
g_string_append_printf(str, FRMT2, buf[0], buf[1]);
73
g_string_append_printf(str, FRMT1, buf[0]);
62
void pretty_print(const char *buf, gsize len) {
80
void mw_debug_datav(const guchar *buf, gsize len,
81
const char *msg, va_list args) {
68
g_return_if_fail(buf != NULL);
84
g_return_if_fail(buf != NULL || len == 0);
70
86
str = g_string_new(NULL);
71
t_pretty_print(str, buf, len);
89
char *txt = g_strdup_vprintf(msg, args);
90
g_string_append_printf(str, "%s\n", txt);
93
pretty_print(str, buf, len);
73
96
g_string_free(str, TRUE);
79
void pretty_print_opaque(struct mwOpaque *o) {
81
pretty_print(o->data, o->len);
85
void mw_debug_mailme_v(struct mwOpaque *block,
86
const char *info, va_list args) {
101
void mw_debug_data(const guchar *buf, gsize len,
102
const char *msg, ...) {
105
g_return_if_fail(buf != NULL || len == 0);
108
mw_debug_datav(buf, len, msg, args);
114
void mw_debug_opaquev(struct mwOpaque *o, const char *txt, va_list args) {
115
g_return_if_fail(o != NULL);
116
mw_debug_datav(o->data, o->len, txt, args);
121
void mw_debug_opaque(struct mwOpaque *o, const char *txt, ...) {
124
g_return_if_fail(o != NULL);
127
mw_debug_opaquev(o, txt, args);
132
void mw_mailme_datav(const guchar *buf, gsize len,
133
const char *info, va_list args) {
99
139
str = g_string_new(MW_MAILME_MESSAGE "\n"
100
140
" Please send mail to: " MW_MAILME_ADDRESS "\n"
101
141
MW_MAILME_CUT_START "\n");
142
str = g_string_new(NULL);
103
144
txt = g_strdup_vprintf(info, args);
104
g_string_append(str, txt);
145
g_string_append_printf(str, "%s\n", txt);
107
g_string_append(str, "\n");
110
t_pretty_print(str, block->data, block->len);
148
if(buf && len) pretty_print(str, buf, len);
113
150
g_string_append(str, MW_MAILME_CUT_STOP);
115
152
g_debug(str->str);
116
153
g_string_free(str, TRUE);
156
mw_debug_datav(buf, len, info, args);
122
void mw_debug_mailme(struct mwOpaque *block,
123
const char *info, ...) {
125
va_start(args, info);
126
mw_debug_mailme_v(block, info, args);
163
void mw_mailme_data(const guchar *buf, gsize len,
164
const char *info, ...) {
166
va_start(args, info);
167
mw_mailme_datav(buf, len, info, args);
173
void mw_mailme_opaquev(struct mwOpaque *o, const char *info, va_list args) {
174
mw_mailme_datav(o->data, o->len, info, args);
179
void mw_mailme_opaque(struct mwOpaque *o, const char *info, ...) {
181
va_start(args, info);
182
mw_mailme_opaquev(o, info, args);