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)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include "mono/metadata/blob.h"
16 #include "mono/metadata/metadata.h"
17 #include "mono/metadata/mono-endian.h"
18 #include "mono/utils/mono-compiler.h"
24 declsec_20_get_classname (const char* p, const char **rptr)
30 GString *res = g_string_new ("");
31 int len = mono_metadata_decode_value (p, &p);
34 while ((*c++ != ',') && (cpos++ < len));
39 while ((*a++ != ',') && (apos++ < len));
41 if (apos - cpos > 1) {
42 g_string_append_printf (res, "[%.*s]%.*s", apos - cpos, c, cpos, p);
44 /* in-assembly type aren't fully qualified (no comma) */
45 g_string_append_printf (res, "%.*s", cpos - 1, p);
53 g_string_free (res, FALSE);
58 declsec_20_write_type (GString *str, char type)
61 case MONO_TYPE_BOOLEAN:
62 g_string_append (str, "bool");
64 case MONO_TYPE_SZARRAY:
65 g_string_append (str, "[]");
67 case MONO_TYPE_SYSTEM_TYPE:
68 g_string_append (str, "type");
70 case MONO_TYPE_STRING:
71 g_string_append (str, "string");
74 g_warning ("TODO type %d - please fill a bug report on this!", type);
81 declsec_20_write_value (GString *str, char type, const char *value)
85 g_string_append_printf (str, "%d", (unsigned char)*value);
88 g_string_append_printf (str, "%d", *value);
90 case MONO_TYPE_BOOLEAN:
91 g_string_append_printf (str, "%s", *value ? "true" : "false");
94 g_string_append_printf (str, "0x%04X", read16 (value));
97 g_string_append_printf (str, "%d", read16 (value));
100 g_string_append_printf (str, "%d", (gint16)read16 (value));
103 g_string_append_printf (str, "%d", read32 (value));
106 g_string_append_printf (str, "%d", (gint32)read32 (value));
109 g_string_append_printf (str, "%lld", (long long)read64 (value));
112 g_string_append_printf (str, "%lld", (long long)read64 (value));
117 readr4 (value, &val);
118 inf = dis_isinf (val);
120 g_string_append_printf (str, "0xFF800000"); /* negative infinity */
122 g_string_append_printf (str, "0x7F800000"); /* positive infinity */
123 else if (dis_isnan (val))
124 g_string_append_printf (str, "0xFFC00000"); /* NaN */
126 g_string_append_printf (str, "%.8g", val);
132 readr8 (value, &val);
133 inf = dis_isinf (val);
135 g_string_append_printf (str, "0xFFF00000000000000"); /* negative infinity */
137 g_string_append_printf (str, "0x7FFF0000000000000"); /* positive infinity */
138 else if (isnan (val))
139 g_string_append_printf (str, "0xFFF80000000000000"); /* NaN */
141 g_string_append_printf (str, "%.17g", val);
144 case MONO_TYPE_STRING:
145 if (*value == (char)0xff) {
146 g_string_append (str, "nullref");
149 int len = mono_metadata_decode_value (value, &value);
150 g_string_append_printf (str, "'%.*s'", len, value);
153 case MONO_TYPE_SYSTEM_TYPE: {
154 char *cname = declsec_20_get_classname (value, NULL);
155 int len = mono_metadata_decode_value (value, &value);
156 g_string_append (str, cname);
165 dump_declsec_entry20 (MonoImage *m, const char* p, const char *indent)
169 GString *res = g_string_new ("");
171 if (*p++ != MONO_DECLSEC_FORMAT_20)
174 g_string_append (res, "{");
176 /* number of encoded permission attributes */
177 num = mono_metadata_decode_value (p, &p);
179 for (i = 0; i < num; i++) {
180 int len, j, pos = 0, param_len;
182 char *s = declsec_20_get_classname (p, &p);
183 g_string_append_printf (res, "%s = {", s);
186 /* optional parameters length */
187 param_len = mono_metadata_decode_value (p, &p);
188 param_start = (char *) p;
190 /* number of parameters */
191 pos = mono_metadata_decode_value (p, &p);
192 for (j = 0; j < pos; j++) {
197 case MONO_DECLSEC_FIELD:
198 /* not sure if/how we can get this in a declarative security attribute... */
199 g_string_append (res, "field ");
201 case MONO_DECLSEC_PROPERTY:
202 g_string_append (res, "property ");
205 g_warning ("TODO %d - please fill a bug report on this!", type);
211 if (type == MONO_DECLSEC_ENUM) {
212 s = declsec_20_get_classname (p, &p);
213 len = mono_metadata_decode_value (p, &p);
214 g_string_append_printf (res, "enum %s '%.*s' = ", s, len, p);
217 /* TODO: we must detect the size of the enum element (from the type ? length ?)
218 * note: ildasm v2 has some problem decoding them too and doesn't
219 * seems to rely on the type (as the other assembly isn't loaded) */
220 g_string_append_printf (res, "int32(%d)", read32 (p));
224 if (type == MONO_TYPE_SZARRAY) {
226 declsec_20_write_type (res, arraytype);
228 declsec_20_write_type (res, type);
230 len = mono_metadata_decode_value (p, &p);
231 g_string_append_printf (res, " '%.*s' = ", len, p);
234 if (type == MONO_TYPE_SZARRAY) {
236 declsec_20_write_type (res, type);
239 g_string_append_printf (res, "[%d]", elem);
241 declsec_20_write_type (res, type);
244 g_string_append (res, "(");
246 /* write value - or each element in the array */
247 for (k = 0; k < elem; k++) {
248 p = declsec_20_write_value (res, type, p);
249 /* separate array elements */
251 g_string_append (res, " ");
255 g_string_append_printf (res, ")\n%s", indent);
257 g_string_append (res, ")");
263 g_string_append_printf (res, "},\n%s", indent);
265 g_string_append (res, "}");
268 p = param_start + param_len;
270 g_string_append (res, "}");
273 g_string_free (res, FALSE);