2004-05-06 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mono / dis / util.c
1 /*
2  * util.c: Assorted utilities for the dissasembler
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc (http://www.ximian.com)
8  */
9 #include <config.h>
10 #include <glib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include "util.h"
14
15 /**
16  * map:
17  * @code: code to lookup in table
18  * @table: table to decode code
19  *
20  * Warning: returns static buffer.
21  */
22 const char *
23 map (guint32 code, dis_map_t *table)
24 {
25         int i;
26
27         for (i = 0; table [i].str != NULL; i++)
28                 if (table [i].code == code)
29                         return table [i].str;
30         return "invalid-flags";
31 }
32
33 /**
34  * flags:
35  * @code: bitfield
36  * @table: table to decode bitfield
37  *
38  * Warning: returns static buffer.
39  */
40 const char *
41 flags (guint32 code, dis_map_t *table)
42 {
43         static char buffer [1024];
44         int i;
45         
46         buffer [0] = 0;
47         
48         for (i = 0; table [i].str != NULL; i++)
49                 if (table [i].code & code) {
50                         if (buffer [0])
51                                 strcat (buffer, " ");
52                         strcat (buffer, table [i].str);
53                 }
54
55         return buffer;
56 }
57
58 /**
59  * hex_dump:
60  * @buffer: pointer to buffer to dump
61  * @base: numbering base to use
62  * @count: number of bytes to dump
63  */
64 void
65 hex_dump (const char *buffer, int base, int count)
66 {
67         int show_header = 1;
68         int i;
69
70         if (count < 0){
71                 count = -count;
72                 show_header = 0;
73         }
74         
75         for (i = 0; i < count; i++){
76                 if (show_header)
77                         if ((i % 16) == 0)
78                                 printf ("\n0x%08X: ", (unsigned char) base + i);
79
80                 printf ("%02X ", (unsigned char) (buffer [i]));
81         }
82         fflush (stdout);
83 }
84
85 char*
86 data_dump (const char *data, int len, const char* prefix) {
87         int i, j;
88         GString *str;
89         if (!len)
90                 return g_strdup (" ()\n");
91         str = g_string_new (" (");
92         for (i = 0; i + 15 < len; i += 16) {
93                 if (i == 0)
94                         g_string_sprintfa (str, "\n");
95                 g_string_sprintfa (str, "%s", prefix);
96                 for (j = 0; j < 16; ++j)
97                         g_string_sprintfa (str, "%02X ", (unsigned char) (data [i + j]));
98                 g_string_sprintfa (str, i == len - 16? ") // ": "  // ");
99                 for (j = 0; j < 16; ++j)
100                         g_string_sprintfa (str, "%c", data [i + j] >= 32 && data [i + j] <= 126? data [i + j]: '.');
101                 g_string_sprintfa (str, "\n");
102         }
103         if (i == len)
104                 return g_string_free (str, FALSE);
105         if (len > 16)
106                 g_string_sprintfa (str, "%s", prefix);
107         j = i;
108         for (; i < len; ++i)
109                 g_string_sprintfa (str, "%02X ", (unsigned char) (data [i]));
110         if (len > 16) {
111                 /* align */
112                 int count = 16 - (len % 16);
113                 for (i = 0; i < count; ++i)
114                         g_string_sprintfa (str, "   ");
115         }
116         g_string_sprintfa (str, ") // ");
117         for (i = j; i < len; ++i)
118                 g_string_sprintfa (str, "%c", data [i] >= 32 && data [i] <= 126? data [i]: '.');
119         g_string_sprintfa (str, "\n");
120         return g_string_free (str, FALSE);
121 }
122