dsniff  2.4b2
About: A collection of tools for network auditing
  Fossies Dox: dsniff-2.4b2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

decode_hex.c
Go to the documentation of this file.
1 /*
2  * decode_hex.c
3  *
4  * Hex dump, for debugging.
5  *
6  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
7  *
8  * $Id: decode_hex.c,v 1.5 2001/03/15 08:32:59 dugsong Exp $
9  */
10 
11 #include "config.h"
12 
13 #include <sys/param.h>
14 #include <sys/types.h>
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <ctype.h>
19 
20 #include "buf.h"
21 #include "decode.h"
22 
23 /* adapted from OpenBSD tcpdump: dump the buffer in emacs-hexl format */
24 
25 int
26 decode_hex(u_char *buf, int len, u_char *obuf, int olen)
27 {
28  struct buf inbuf, outbuf;
29  u_int i, j, k;
30  u_char c;
31 
32  buf_init(&inbuf, buf, len);
33  buf_init(&outbuf, obuf, olen);
34 
35  while ((i = buf_len(&inbuf)) > 0) {
36  i = i < 16 ? i : 16;
37  k = buf_tell(&inbuf);
38 
39  buf_putf(&outbuf, " %04x: ", k);
40 
41  for (j = 0; j < i; j++) {
42  buf_get(&inbuf, &c, 1);
43  buf_putf(&outbuf, "%02x", (u_int)c);
44  if ((j % 2) == 1)
45  buf_put(&outbuf, " ", 1);
46  }
47  for (; j < 16; j++) {
48  buf_put(&outbuf, " ", (j % 2) + 2);
49  }
50  buf_put(&outbuf, " ", 1);
51 
52  buf_seek(&inbuf, k, SEEK_SET);
53 
54  for (j = 0; j < i; j++) {
55  buf_get(&inbuf, &c, 1);
56  c = isprint(c) ? c : '.';
57  buf_putf(&outbuf, "%c", c);
58  }
59  buf_put(&outbuf, "\n", 1);
60  }
61  buf_end(&outbuf);
62 
63  return (buf_len(&outbuf));
64 }
buf_putf
int buf_putf(buf_t buf, const char *fmt,...)
Definition: buf.c:106
decode_hex
int decode_hex(u_char *buf, int len, u_char *obuf, int olen)
Definition: decode_hex.c:26
buf_init
void buf_init(buf_t buf, u_char *data, int len)
Definition: buf.c:24
buf_end
void buf_end(buf_t buf)
Definition: buf.c:121
buf_tell
#define buf_tell(b)
Definition: buf.h:43
buf_put
int buf_put(buf_t buf, void *src, int len)
Definition: buf.c:93
decode.h
buf.h
buf
Definition: buf.h:14
buf_get
int buf_get(buf_t buf, void *dst, int len)
Definition: buf.c:74
config.h
obuf
static char obuf[4096]
Definition: trigger.c:43
buf_seek
int buf_seek(buf_t buf, int off, int whence)
Definition: buf.c:60
buf_len
#define buf_len(b)
Definition: buf.h:34