Build-Depends libpcap-dev instead of libpcap0.8-dev.
[yaz-ziffy.git] / src / fmemdmp.c
1 /*
2  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3  * fmemdmp.c - tracing utilities
4  *
5  * Copyright (c) 1998-2001 R. Carbone <rocco@ntop.org>
6  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23
24
25 /*
26  * Operating System include files
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include <ctype.h>
33
34 #include <time.h>
35 #if HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 #endif
38
39
40 /*
41  * Pretty print function.
42  *
43  * This function dumps a buffer in memory in the (pretty !!) format :
44  *
45  *   off:  printable          hexadecimal notation
46  * --------------------------------------------------------------------------
47  *
48  * Dump of memory area at address 0x10000444 for 51 bytes
49  *     0:  abcdefghijklmnop   61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
50  *    16:  qrstuvzxyw012345   71 72 73 74 75 76 7a 78 79 77 30 31 32 33 34 35
51  *    32:  6789~!@#$%^&*()_   36 37 38 39 7e 21 40 23 24 25 5e 26 2a 28 29 5f
52  *    48:  -+=                2d 2b 3d
53  *
54  * Dump of memory area at address 0x7fffbc03 for 16 bytes
55  *    0:  rocco@tecsiel.it    72 6f 63 63 6f 40 74 65 63 73 69 65 6c 2e 69 74
56  */
57 void fmemdmp (FILE * fd, char * ptr, int size, char * text)
58 {
59   int offset = 0;
60   int i = 0;
61   int bytes_in_a_line = 16;
62   unsigned int total;
63
64   if (! ptr || size <= 0)
65     return;
66
67   if (text && * text)
68     fprintf (fd, "\"%s\" at address %p for %d bytes\n",
69              text, ptr, size);
70
71   for (total = 0; total < size; total += bytes_in_a_line)
72     {
73       /*
74        * Print the offset
75        */
76       fprintf (fd, "%6d:  ", offset);
77       /*
78        * Print the bytes in a line (each byte in ASCII notation)
79        */
80       for (i = 0; i < bytes_in_a_line; i ++)
81         if (total + i < size)
82           fprintf (fd, "%c",
83                    isprint (* (ptr + total + i) & 0x000000ff)
84                    ? (* (ptr + total + i))
85                    : '.');
86         else
87           fprintf (fd, " "); /* 1 blank character */
88       /*
89        * Print the separator
90        */
91       fprintf (fd, "  ");
92       /*
93        * Print the bytes in a line (each byte in Hexadecimal notation)
94        */
95       for (i = 0; i < bytes_in_a_line && i < size; i ++)
96         if (total + i < size)
97           fprintf (fd, "%02x ",
98                    * (ptr + total + i) & 0x000000ff);
99         else
100           fprintf (fd, "   "); /* 3 more blanks characters */
101
102       fprintf (fd, "\n");
103       offset += bytes_in_a_line;
104     }
105   fflush (fd);
106 }