2010-03-30 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / dis / util.c
blob737f6fda774fbd6b8d437fb8026f42691dca73a8
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; code && table [i].str != NULL; i++)
49 if (table [i].code & code) {
50 code &= ~table [i].code;
51 strcat (buffer, table [i].str);
54 if (code)
55 sprintf (buffer + strlen (buffer), "unknown-flag-%2x ", code);
57 return buffer;
60 /**
61 * hex_dump:
62 * @buffer: pointer to buffer to dump
63 * @base: numbering base to use
64 * @count: number of bytes to dump
66 void
67 hex_dump (const char *buffer, int base, int count)
69 int show_header = 1;
70 int i;
72 if (count < 0){
73 count = -count;
74 show_header = 0;
77 for (i = 0; i < count; i++){
78 if (show_header)
79 if ((i % 16) == 0)
80 printf ("\n0x%08X: ", (unsigned char) base + i);
82 printf ("%02X ", (unsigned char) (buffer [i]));
84 fflush (stdout);
87 char*
88 data_dump (const char *data, int len, const char* prefix) {
89 int i, j;
90 GString *str;
91 if (!len)
92 return g_strdup (" ()\n");
93 str = g_string_new (" (");
94 for (i = 0; i + 15 < len; i += 16) {
95 if (i == 0)
96 g_string_append_printf (str, "\n");
97 g_string_append_printf (str, "%s", prefix);
98 for (j = 0; j < 16; ++j)
99 g_string_append_printf (str, "%02X ", (unsigned char) (data [i + j]));
100 g_string_append_printf (str, i == len - 16? ") // ": " // ");
101 for (j = 0; j < 16; ++j)
102 g_string_append_printf (str, "%c", data [i + j] >= 32 && data [i + j] <= 126? data [i + j]: '.');
103 g_string_append_printf (str, "\n");
105 if (i == len)
106 return g_string_free (str, FALSE);
107 if (len > 16)
108 g_string_append_printf (str, "%s", prefix);
109 j = i;
110 for (; i < len; ++i)
111 g_string_append_printf (str, "%02X ", (unsigned char) (data [i]));
112 if (len > 16) {
113 /* align */
114 int count = 16 - (len % 16);
115 for (i = 0; i < count; ++i)
116 g_string_append_printf (str, " ");
118 g_string_append_printf (str, ") // ");
119 for (i = j; i < len; ++i)
120 g_string_append_printf (str, "%c", data [i] >= 32 && data [i] <= 126? data [i]: '.');
121 g_string_append_printf (str, "\n");
122 return g_string_free (str, FALSE);