New test.
[mono.git] / mono / dis / declsec.c
1 /*
2  * declsec.h:  Support for the new declarative security attribute
3  *      metadata format (2.0)
4  *
5  * Author:
6  *      Sebastien Pouliot  <sebastien@ximian.com>
7  *
8  * Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9  */
10
11 #include <glib.h>
12 #include <math.h>
13
14 #include "mono/metadata/blob.h"
15 #include "mono/metadata/metadata.h"
16 #include "mono/metadata/mono-endian.h"
17 #include "mono/utils/mono-compiler.h"
18
19 #include "declsec.h"
20
21 static char*
22 declsec_20_get_classname (const char* p, const char **rptr)
23 {
24         int apos, cpos = 0;
25         char *c;
26         char *a;
27         char *result;
28         GString *res = g_string_new ("");
29         int len = mono_metadata_decode_value (p, &p);
30
31         c = (char *) p;
32         while ((*c++ != ',') && (cpos++ < len));
33         c++;
34
35         apos = cpos;
36         a = c;
37         while ((*a++ != ',') && (apos++ < len));
38
39         if (apos - cpos > 1) {
40                 g_string_sprintfa (res, "[%.*s]%.*s", apos - cpos, c, cpos, p);
41         } else {
42                 /* in-assembly type aren't fully qualified (no comma) */
43                 g_string_sprintfa (res, "%.*s", cpos - 1, p);
44         }
45
46         p += len;
47         if (rptr)
48                 *rptr = p;
49
50         result = res->str;
51         g_string_free (res, FALSE);
52         return result;
53 }
54
55 static gboolean
56 declsec_20_write_type (GString *str, char type)
57 {
58         switch (type) {
59         case MONO_TYPE_SZARRAY:
60                 g_string_append (str, "[]");
61                 break;
62         case MONO_TYPE_SYSTEM_TYPE:
63                 g_string_append (str, "type");
64                 break;
65         default:
66                 g_warning ("TODO type %d - please fill a bug report on this!", type);
67                 return FALSE;
68         }
69         return TRUE;
70 }
71
72 static const char*
73 declsec_20_write_value (GString *str, char type, const char *value)
74 {
75         switch (type) {
76         case MONO_TYPE_U1:
77                 g_string_sprintfa (str, "%d", (unsigned char)*value);
78                 return value + 1;
79         case MONO_TYPE_I1:
80                 g_string_sprintfa (str, "%d", *value);
81                 return value + 1;
82         case MONO_TYPE_BOOLEAN:
83                 g_string_sprintfa (str, "%s", *value ? "true" : "false");
84                 return value + 1;
85         case MONO_TYPE_CHAR:
86                 g_string_sprintfa (str, "0x%04X", read16 (value));
87                 return value + 2;
88         case MONO_TYPE_U2:
89                 g_string_sprintfa (str, "%d", read16 (value));
90                 return value + 2;
91         case MONO_TYPE_I2:
92                 g_string_sprintfa (str, "%d", (gint16)read16 (value));
93                 return value + 2;
94         case MONO_TYPE_U4:
95                 g_string_sprintfa (str, "%d", read32 (value));
96                 return value + 4;
97         case MONO_TYPE_I4:
98                 g_string_sprintfa (str, "%d", (gint32)read32 (value));
99                 return value + 4;
100         case MONO_TYPE_U8:
101                 g_string_sprintfa (str, "%lld", (long long)read64 (value));
102                 return value + 8;
103         case MONO_TYPE_I8:
104                 g_string_sprintfa (str, "%lld", (long long)read64 (value));
105                 return value + 8;
106         case MONO_TYPE_R4: {
107                 float val;
108                 int inf;
109                 readr4 (value, &val);
110                 inf = isinf (val);
111                 if (inf == -1) 
112                         g_string_sprintfa (str, "0xFF800000"); /* negative infinity */
113                 else if (inf == 1)
114                         g_string_sprintfa (str, "0x7F800000"); /* positive infinity */
115                 else if (isnan (val))
116                         g_string_sprintfa (str, "0xFFC00000"); /* NaN */
117                 else
118                         g_string_sprintfa (str, "%.8g", val);
119                 return value + 4;
120         }
121         case MONO_TYPE_R8: {
122                 double val;
123                 int inf;
124                 readr8 (value, &val);
125                 inf = isinf (val);
126                 if (inf == -1) 
127                         g_string_sprintfa (str, "0xFFF00000000000000"); /* negative infinity */
128                 else if (inf == 1)
129                         g_string_sprintfa (str, "0x7FFF0000000000000"); /* positive infinity */
130                 else if (isnan (val))
131                         g_string_sprintfa (str, "0xFFF80000000000000"); /* NaN */
132                 else
133                         g_string_sprintfa (str, "%.17g", val);
134                 return value + 8;
135         }
136         case MONO_TYPE_STRING:
137                 if (*value == (char)0xff) {
138                         g_string_append (str, "nullref");
139                         return value + 1;
140                 } else {
141                         int len = mono_metadata_decode_value (value, &value);
142                         g_string_sprintfa (str, "'%.*s'", len, value);
143                         return value + len;
144                 }
145         case MONO_TYPE_SYSTEM_TYPE: {
146                 char *cname = declsec_20_get_classname (value, NULL);
147                 int len = mono_metadata_decode_value (value, &value);
148                 g_string_append (str, cname);
149                 g_free (cname);
150                 return value + len;
151         }
152         }
153         return 0;
154 }
155
156 char*
157 dump_declsec_entry20 (MonoImage *m, const char* p, const char *indent)
158 {
159         int i, num;
160         char *result;
161         GString *res = g_string_new ("");
162
163         if (*p++ != MONO_DECLSEC_FORMAT_20)
164                 return NULL;
165
166         g_string_append (res, "{");
167
168         /* number of encoded permission attributes */
169         num = mono_metadata_decode_value (p, &p);
170
171         for (i = 0; i < num; i++) {
172                 int len, j, pos = 0, param_len;
173                 char *param_start;
174                 char *s = declsec_20_get_classname (p, &p);
175                 g_string_sprintfa (res, "%s = {", s);
176                 g_free (s);
177
178                 /* optional parameters length */
179                 param_len = mono_metadata_decode_value (p, &p);
180                 param_start = (char *) p;
181
182                 /* number of parameters */
183                 pos = mono_metadata_decode_value (p, &p);
184                 for (j = 0; j < pos; j++) {
185                         int k, elem;
186                         int type = *p++;
187
188                         switch (type) {
189                         case MONO_DECLSEC_FIELD:
190                                 /* not sure if/how we can get this in a declarative security attribute... */
191                                 g_string_append (res, "field ");
192                                 break;
193                         case MONO_DECLSEC_PROPERTY:
194                                 g_string_append (res, "property ");
195                                 break;
196                         default:
197                                 g_warning ("TODO %d - please fill a bug report on this!", type);
198                                 break;
199                         }
200
201                         type = *p++;
202
203                         if (type == MONO_DECLSEC_ENUM) {
204                                 s = declsec_20_get_classname (p, &p);
205                                 len = mono_metadata_decode_value (p, &p);
206                                 g_string_sprintfa (res, "enum %s '%.*s' = ", s, len, p);
207                                 g_free (s);
208                                 p += len;
209                                 /* TODO: we must detect the size of the enum element (from the type ? length ?)
210                                  * note: ildasm v2 has some problem decoding them too and doesn't
211                                  * seems to rely on the type (as the other assembly isn't loaded) */
212                                 g_string_sprintfa (res, "int32(%d)", read32 (p));
213                                 p += 4;
214                         } else {
215                                 int arraytype = 0;
216                                 if (type == MONO_TYPE_SZARRAY) {
217                                         arraytype = *p++;
218                                         declsec_20_write_type (res, arraytype);
219                                 }
220                                 declsec_20_write_type (res, type);
221
222                                 len = mono_metadata_decode_value (p, &p);
223                                 g_string_sprintfa (res, " '%.*s' = ", len, p);
224                                 p += len;
225
226                                 if (type == MONO_TYPE_SZARRAY) {
227                                         type = arraytype;
228                                         declsec_20_write_type (res, type);
229                                         elem = read32 (p);
230                                         p += 4;
231                                         g_string_sprintfa (res, "[%d]", elem);
232                                 } else {
233                                         declsec_20_write_type (res, type);
234                                         elem = 1;
235                                 }
236                                 g_string_append (res, "(");
237
238                                 /* write value - or each element in the array */
239                                 for (k = 0; k < elem; k++) {
240                                         p = declsec_20_write_value (res, type, p);
241                                         /* separate array elements */
242                                         if (k < elem - 1)
243                                                 g_string_append (res, " ");
244                                 }
245                                 
246                                 if (j < pos - 1)
247                                         g_string_sprintfa (res, ")\n%s", indent);
248                                 else
249                                         g_string_append (res, ")");
250                         }
251
252                 }
253
254                 if (i < num - 1)
255                         g_string_sprintfa (res, "},\n%s", indent);
256                 else
257                         g_string_append (res, "}");
258
259                 if (param_len > 0)
260                         p = param_start + param_len;
261         }
262         g_string_append (res, "}");
263
264         result = res->str;
265         g_string_free (res, FALSE);
266         return result;
267 }