2009-03-22 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / dis / util.c
blobe5e467d09ffcb6914bd6f6814784dacfbfaa1e6d
1 /*
2 * util.c: Assorted utilities for the disassembler
4 * Author:
5 * Miguel de Icaza (miguel@ximian.com)
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"
15 /**
16 * map:
17 * @code: code to lookup in table
18 * @table: table to decode code
20 * Warning: returns static buffer.
22 const char *
23 map (guint32 code, dis_map_t *table)
25 int i;
27 for (i = 0; table [i].str != NULL; i++)
28 if (table [i].code == code)
29 return table [i].str;
30 return "invalid-flags";
33 /**
34 * flags:
35 * @code: bitfield
36 * @table: table to decode bitfield
38 * Warning: returns static buffer.
40 const char *
41 flags (guint32 code, dis_map_t *table)
43 static char buffer [1024];
44 int i;
46 buffer [0] = 0;
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);
55 return buffer;
58 /**
59 * hex_dump:
60 * @buffer: pointer to buffer to dump
61 * @base: numbering base to use
62 * @count: number of bytes to dump
64 void
65 hex_dump (const char *buffer, int base, int count)
67 int show_header = 1;
68 int i;
70 if (count < 0){
71 count = -count;
72 show_header = 0;
75 for (i = 0; i < count; i++){
76 if (show_header)
77 if ((i % 16) == 0)
78 printf ("\n0x%08X: ", (unsigned char) base + i);
80 printf ("%02X ", (unsigned char) (buffer [i]));
82 fflush (stdout);
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_append_printf (str, "\n");
95 g_string_append_printf (str, "%s", prefix);
96 for (j = 0; j < 16; ++j)
97 g_string_append_printf (str, "%02X ", (unsigned char) (data [i + j]));
98 g_string_append_printf (str, i == len - 16? ") // ": " // ");
99 for (j = 0; j < 16; ++j)
100 g_string_append_printf (str, "%c", data [i + j] >= 32 && data [i + j] <= 126? data [i + j]: '.');
101 g_string_append_printf (str, "\n");
103 if (i == len)
104 return g_string_free (str, FALSE);
105 if (len > 16)
106 g_string_append_printf (str, "%s", prefix);
107 j = i;
108 for (; i < len; ++i)
109 g_string_append_printf (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_append_printf (str, " ");
116 g_string_append_printf (str, ") // ");
117 for (i = j; i < len; ++i)
118 g_string_append_printf (str, "%c", data [i] >= 32 && data [i] <= 126? data [i]: '.');
119 g_string_append_printf (str, "\n");
120 return g_string_free (str, FALSE);