2002-02-14 Jeffrey Stedfast <fejj@ximian.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, 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         g_assert_not_reached ();
31         return "";
32 }
33
34 /**
35  * flags:
36  * @code: bitfield
37  * @table: table to decode bitfield
38  *
39  * Warning: returns static buffer.
40  */
41 const char *
42 flags (guint32 code, map_t *table)
43 {
44         static char buffer [1024];
45         int i;
46         
47         buffer [0] = 0;
48         
49         for (i = 0; table [i].str != NULL; i++)
50                 if (table [i].code & code) {
51                         if (buffer [0])
52                                 strcat (buffer, " ");
53                         strcat (buffer, table [i].str);
54                 }
55
56         return buffer;
57 }
58
59 /**
60  * hex_dump:
61  * @buffer: pointer to buffer to dump
62  * @base: numbering base to use
63  * @count: number of bytes to dump
64  */
65 void
66 hex_dump (const char *buffer, int base, int count)
67 {
68         int show_header = 1;
69         int i;
70
71         if (count < 0){
72                 count = -count;
73                 show_header = 0;
74         }
75         
76         for (i = 0; i < count; i++){
77                 if (show_header)
78                         if ((i % 16) == 0)
79                                 printf ("\n0x%08x: ", (unsigned char) base + i);
80
81                 printf ("%02x ", (unsigned char) (buffer [i]));
82         }
83         fflush (stdout);
84 }
85