Clean up some whitespace in the output
[mono.git] / mono / dis / util.c
1 /*
2  * util.c: Assorted utilities for the disassembler
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; code && table [i].str != NULL; i++)
49                 if (table [i].code & code) {
50                         code &= ~table [i].code;
51                         strcat (buffer, table [i].str);
52                 }
53
54         if (code)
55                 sprintf (buffer + strlen (buffer), "unknown-flag-%2x ", code);
56
57         return buffer;
58 }
59
60 /**
61  * hex_dump:
62  * @buffer: pointer to buffer to dump
63  * @base: numbering base to use
64  * @count: number of bytes to dump
65  */
66 void
67 hex_dump (const char *buffer, int base, int count)
68 {
69         int show_header = 1;
70         int i;
71
72         if (count < 0){
73                 count = -count;
74                 show_header = 0;
75         }
76         
77         for (i = 0; i < count; i++){
78                 if (show_header)
79                         if ((i % 16) == 0)
80                                 printf ("\n0x%08X: ", (unsigned char) base + i);
81
82                 printf ("%02X ", (unsigned char) (buffer [i]));
83         }
84         fflush (stdout);
85 }
86
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");
104         }
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, "   ");
117         }
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);
123 }
124