Update dependencies from https://github.com/dotnet/corefx build 20191013.1
[mono-project.git] / mono / dis / util.c
blob52583687a517d335b455c4da8bf930e4f1a8e706
1 /**
2 * \file
3 * Assorted utilities for the disassembler
5 * Author:
6 * Miguel de Icaza (miguel@ximian.com)
8 * (C) 2001 Ximian, Inc (http://www.ximian.com)
9 */
10 #include <config.h>
11 #include <glib.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <math.h>
15 #include "util.h"
16 #include "mono/utils/mono-compiler.h"
17 #include "mono/utils/mono-math.h"
19 /**
20 * \param code code to lookup in table
21 * \param table table to decode code
23 * Warning: returns static buffer.
25 const char *
26 map (guint32 code, dis_map_t *table)
28 int i;
30 for (i = 0; table [i].str != NULL; i++)
31 if (table [i].code == code)
32 return table [i].str;
33 return "invalid-flags";
36 /**
37 * \param code bitfield
38 * \param table table to decode bitfield
40 * Warning: returns static buffer.
42 const char *
43 flags (guint32 code, dis_map_t *table)
45 static char buffer [1024];
46 int i;
48 buffer [0] = 0;
50 for (i = 0; code && table [i].str != NULL; i++)
51 if (table [i].code & code) {
52 code &= ~table [i].code;
53 strcat (buffer, table [i].str);
56 if (code)
57 sprintf (buffer + strlen (buffer), "unknown-flag-%2x ", code);
59 return buffer;
62 /**
63 * hex_dump:
64 * @buffer: pointer to buffer to dump
65 * @base: numbering base to use
66 * @count: number of bytes to dump
68 void
69 hex_dump (const char *buffer, int base, int count)
71 int show_header = 1;
72 int i;
74 if (count < 0){
75 count = -count;
76 show_header = 0;
79 for (i = 0; i < count; i++){
80 if (show_header)
81 if ((i % 16) == 0)
82 printf ("\n0x%08X: ", (unsigned char) base + i);
84 printf ("%02X ", (unsigned char) (buffer [i]));
86 fflush (stdout);
89 char*
90 data_dump (const char *data, int len, const char* prefix) {
91 int i, j;
92 GString *str;
93 if (!len)
94 return g_strdup (" ()\n");
95 str = g_string_new (" (");
96 for (i = 0; i + 15 < len; i += 16) {
97 if (i == 0)
98 g_string_append_printf (str, "\n");
99 g_string_append_printf (str, "%s", prefix);
100 for (j = 0; j < 16; ++j)
101 g_string_append_printf (str, "%02X ", (unsigned char) (data [i + j]));
102 g_string_append_printf (str, i == len - 16? ") // ": " // ");
103 for (j = 0; j < 16; ++j)
104 g_string_append_printf (str, "%c", data [i + j] >= 32 && data [i + j] <= 126? data [i + j]: '.');
105 g_string_append_printf (str, "\n");
107 if (i == len)
108 return g_string_free (str, FALSE);
109 if (len > 16)
110 g_string_append_printf (str, "%s", prefix);
111 j = i;
112 for (; i < len; ++i)
113 g_string_append_printf (str, "%02X ", (unsigned char) (data [i]));
114 if (len > 16) {
115 /* align */
116 int count = 16 - (len % 16);
117 for (i = 0; i < count; ++i)
118 g_string_append_printf (str, " ");
120 g_string_append_printf (str, ") // ");
121 for (i = j; i < len; ++i)
122 g_string_append_printf (str, "%c", data [i] >= 32 && data [i] <= 126? data [i]: '.');
123 g_string_append_printf (str, "\n");
124 return g_string_free (str, FALSE);