2 * declsec.h: Support for the new declarative security attribute
3 * metadata format (2.0)
6 * Sebastien Pouliot <sebastien@ximian.com>
8 * Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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"
23 declsec_20_get_classname (const char* p
, const char **rptr
)
29 GString
*res
= g_string_new ("");
30 int len
= mono_metadata_decode_value (p
, &p
);
33 while ((*c
++ != ',') && (cpos
++ < len
));
38 while ((*a
++ != ',') && (apos
++ < len
));
40 if (apos
- cpos
> 1) {
41 g_string_append_printf (res
, "[%.*s]%.*s", apos
- cpos
, c
, cpos
, p
);
43 /* in-assembly type aren't fully qualified (no comma) */
44 g_string_append_printf (res
, "%.*s", cpos
- 1, p
);
52 g_string_free (res
, FALSE
);
57 declsec_20_write_type (GString
*str
, char type
)
60 case MONO_TYPE_BOOLEAN
:
61 g_string_append (str
, "bool");
63 case MONO_TYPE_SZARRAY
:
64 g_string_append (str
, "[]");
66 case MONO_TYPE_SYSTEM_TYPE
:
67 g_string_append (str
, "type");
69 case MONO_TYPE_STRING
:
70 g_string_append (str
, "string");
73 g_warning ("TODO type %d - please fill a bug report on this!", type
);
80 declsec_20_write_value (GString
*str
, char type
, const char *value
)
84 g_string_append_printf (str
, "%d", (unsigned char)*value
);
87 g_string_append_printf (str
, "%d", *value
);
89 case MONO_TYPE_BOOLEAN
:
90 g_string_append_printf (str
, "%s", *value
? "true" : "false");
93 g_string_append_printf (str
, "0x%04X", read16 (value
));
96 g_string_append_printf (str
, "%d", read16 (value
));
99 g_string_append_printf (str
, "%d", (gint16
)read16 (value
));
102 g_string_append_printf (str
, "%d", read32 (value
));
105 g_string_append_printf (str
, "%d", (gint32
)read32 (value
));
108 g_string_append_printf (str
, "%lld", (long long)read64 (value
));
111 g_string_append_printf (str
, "%lld", (long long)read64 (value
));
116 readr4 (value
, &val
);
117 inf
= dis_isinf (val
);
119 g_string_append_printf (str
, "0xFF800000"); /* negative infinity */
121 g_string_append_printf (str
, "0x7F800000"); /* positive infinity */
122 else if (dis_isnan (val
))
123 g_string_append_printf (str
, "0xFFC00000"); /* NaN */
125 g_string_append_printf (str
, "%.8g", val
);
131 readr8 (value
, &val
);
132 inf
= dis_isinf (val
);
134 g_string_append_printf (str
, "0xFFF00000000000000"); /* negative infinity */
136 g_string_append_printf (str
, "0x7FFF0000000000000"); /* positive infinity */
137 else if (isnan (val
))
138 g_string_append_printf (str
, "0xFFF80000000000000"); /* NaN */
140 g_string_append_printf (str
, "%.17g", val
);
143 case MONO_TYPE_STRING
:
144 if (*value
== (char)0xff) {
145 g_string_append (str
, "nullref");
148 int len
= mono_metadata_decode_value (value
, &value
);
149 g_string_append_printf (str
, "'%.*s'", len
, value
);
152 case MONO_TYPE_SYSTEM_TYPE
: {
153 char *cname
= declsec_20_get_classname (value
, NULL
);
154 int len
= mono_metadata_decode_value (value
, &value
);
155 g_string_append (str
, cname
);
164 dump_declsec_entry20 (MonoImage
*m
, const char* p
, const char *indent
)
168 GString
*res
= g_string_new ("");
170 if (*p
++ != MONO_DECLSEC_FORMAT_20
)
173 g_string_append (res
, "{");
175 /* number of encoded permission attributes */
176 num
= mono_metadata_decode_value (p
, &p
);
178 for (i
= 0; i
< num
; i
++) {
179 int len
, j
, pos
= 0, param_len
;
181 char *s
= declsec_20_get_classname (p
, &p
);
182 g_string_append_printf (res
, "%s = {", s
);
185 /* optional parameters length */
186 param_len
= mono_metadata_decode_value (p
, &p
);
187 param_start
= (char *) p
;
189 /* number of parameters */
190 pos
= mono_metadata_decode_value (p
, &p
);
191 for (j
= 0; j
< pos
; j
++) {
196 case MONO_DECLSEC_FIELD
:
197 /* not sure if/how we can get this in a declarative security attribute... */
198 g_string_append (res
, "field ");
200 case MONO_DECLSEC_PROPERTY
:
201 g_string_append (res
, "property ");
204 g_warning ("TODO %d - please fill a bug report on this!", type
);
210 if (type
== MONO_DECLSEC_ENUM
) {
211 s
= declsec_20_get_classname (p
, &p
);
212 len
= mono_metadata_decode_value (p
, &p
);
213 g_string_append_printf (res
, "enum %s '%.*s' = ", s
, len
, p
);
216 /* TODO: we must detect the size of the enum element (from the type ? length ?)
217 * note: ildasm v2 has some problem decoding them too and doesn't
218 * seems to rely on the type (as the other assembly isn't loaded) */
219 g_string_append_printf (res
, "int32(%d)", read32 (p
));
223 if (type
== MONO_TYPE_SZARRAY
) {
225 declsec_20_write_type (res
, arraytype
);
227 declsec_20_write_type (res
, type
);
229 len
= mono_metadata_decode_value (p
, &p
);
230 g_string_append_printf (res
, " '%.*s' = ", len
, p
);
233 if (type
== MONO_TYPE_SZARRAY
) {
235 declsec_20_write_type (res
, type
);
238 g_string_append_printf (res
, "[%d]", elem
);
240 declsec_20_write_type (res
, type
);
243 g_string_append (res
, "(");
245 /* write value - or each element in the array */
246 for (k
= 0; k
< elem
; k
++) {
247 p
= declsec_20_write_value (res
, type
, p
);
248 /* separate array elements */
250 g_string_append (res
, " ");
254 g_string_append_printf (res
, ")\n%s", indent
);
256 g_string_append (res
, ")");
262 g_string_append_printf (res
, "},\n%s", indent
);
264 g_string_append (res
, "}");
267 p
= param_start
+ param_len
;
269 g_string_append (res
, "}");
272 g_string_free (res
, FALSE
);