2002-07-16 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mono / metadata / metadata.c
index 9e07c9c642d0ddf90b198ef3e6c8553e816dbef1..10003353c3524384771049121a8507f4c5e819d4 100644 (file)
@@ -1,10 +1,11 @@
 /*
  * metadata.c: Routines for accessing the metadata
  *
- * Author:
+ * Authors:
  *   Miguel de Icaza (miguel@ximian.com)
+ *   Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2001 Ximian, Inc.
+ * (C) 2001-2002 Ximian, Inc.
  */
 
 #include <config.h>
@@ -19,7 +20,7 @@
 #include "private.h"
 #include "class.h"
 
-static void do_mono_metadata_parse_type (MonoType *type, MonoMetadata *m, const char *ptr, const char **rptr);
+static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr);
 
 /*
  * Encoding of the "description" argument:
@@ -366,8 +367,20 @@ inverse of this mapping.
 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
 
 /* Reference: Partition II - 23.2.6 */
+/*
+ * mono_metadata_compute_size:
+ * @meta: metadata context
+ * @tableindex: metadata table number
+ * @result_bitfield: pointer to guint32 where to store additional info
+ * 
+ * mono_metadata_compute_size() computes the lenght in bytes of a single
+ * row in a metadata table. The size of each column is encoded in the
+ * @result_bitfield return value along with the number of columns in the table.
+ * the resulting bitfield should be handed to the mono_metadata_table_size()
+ * and mono_metadata_table_count() macros.
+ */
 int
-mono_metadata_compute_size (MonoMetadata *meta, int tableindex, guint32 *result_bitfield)
+mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
 {
        guint32 bitfield = 0;
        int size = 0, field_size;
@@ -501,10 +514,10 @@ mono_metadata_compute_size (MonoMetadata *meta, int tableindex, guint32 *result_
                        n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
-                       /* Permission seems to be a blob heap pointer */
+                       n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
-                       /* Signature seems to be a blob heap pointer */
+                       n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
                        n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
@@ -666,10 +679,10 @@ mono_metadata_compute_size (MonoMetadata *meta, int tableindex, guint32 *result_
  * This is an internal function used by the image loader code.
  */
 void
-mono_metadata_compute_table_bases (MonoMetadata *meta)
+mono_metadata_compute_table_bases (MonoImage *meta)
 {
        int i;
-       char *base = meta->tables_base;
+       const char *base = meta->tables_base;
        
        for (i = 0; i < 64; i++){
                if (meta->tables [i].rows == 0)
@@ -691,8 +704,8 @@ mono_metadata_compute_table_bases (MonoMetadata *meta)
  * Returns a pointer to the @idx element in the metadata table
  * whose code is @table.
  */
-char *
-mono_metadata_locate (MonoMetadata *meta, int table, int idx)
+const char *
+mono_metadata_locate (MonoImage *meta, int table, int idx)
 {
        /* idx == 0 refers always to NULL */
        g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
@@ -700,8 +713,16 @@ mono_metadata_locate (MonoMetadata *meta, int table, int idx)
        return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
 }
 
-char *
-mono_metadata_locate_token (MonoMetadata *meta, guint32 token)
+/**
+ * mono_metadata_locate_token:
+ * @meta: metadata context
+ * @token: metadata token
+ *
+ * Returns a pointer to the data in the metadata represented by the
+ * token #token.
+ */
+const char *
+mono_metadata_locate_token (MonoImage *meta, guint32 token)
 {
        return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
 }
@@ -730,17 +751,24 @@ mono_metadata_get_table (MonoMetaTableEnum table)
  * Returns: an in-memory pointer to the @index in the string heap.
  */
 const char *
-mono_metadata_string_heap (MonoMetadata *meta, guint32 index)
+mono_metadata_string_heap (MonoImage *meta, guint32 index)
 {
        g_return_val_if_fail (index < meta->heap_strings.size, "");
-       return meta->raw_metadata + meta->heap_strings.offset + index;
+       return meta->heap_strings.data + index;
 }
 
+/**
+ * mono_metadata_user_string:
+ * @meta: metadata context
+ * @index: index into the user string heap.
+ *
+ * Returns: an in-memory pointer to the @index in the user string heap ("#US").
+ */
 const char *
-mono_metadata_user_string (MonoMetadata *meta, guint32 index)
+mono_metadata_user_string (MonoImage *meta, guint32 index)
 {
        g_return_val_if_fail (index < meta->heap_us.size, "");
-       return meta->raw_metadata + meta->heap_us.offset + index;
+       return meta->heap_us.data + index;
 }
 
 /**
@@ -751,10 +779,26 @@ mono_metadata_user_string (MonoMetadata *meta, guint32 index)
  * Returns: an in-memory pointer to the @index in the Blob heap.
  */
 const char *
-mono_metadata_blob_heap (MonoMetadata *meta, guint32 index)
+mono_metadata_blob_heap (MonoImage *meta, guint32 index)
 {
        g_return_val_if_fail (index < meta->heap_blob.size, "");
-       return meta->raw_metadata + meta->heap_blob.offset + index;
+       return meta->heap_blob.data + index;
+}
+
+/**
+ * mono_metadata_guid_heap:
+ * @meta: metadata context
+ * @index: index into the guid heap.
+ *
+ * Returns: an in-memory pointer to the @index in the guid heap.
+ */
+const char *
+mono_metadata_guid_heap (MonoImage *meta, guint32 index)
+{
+       --index;
+       index *= 16; /* adjust for guid size and 1-based index */
+       g_return_val_if_fail (index < meta->heap_guid.size, "");
+       return meta->heap_guid.data + index;
 }
 
 static const char *
@@ -777,7 +821,7 @@ mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
 {
        guint32 bitfield = t->size_bitfield;
        int i, count = mono_metadata_table_count (bitfield);
-       char *data = t->base + idx * t->row_size;
+       const char *data = t->base + idx * t->row_size;
        
        g_assert (res_size == count);
        
@@ -814,7 +858,7 @@ mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
 {
        guint32 bitfield = t->size_bitfield;
        int i;
-       register char *data = t->base + idx * t->row_size;
+       register const char *data = t->base + idx * t->row_size;
        register int n;
        
        g_assert (col < mono_metadata_table_count (bitfield));
@@ -883,7 +927,7 @@ mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
 guint32
 mono_metadata_decode_value (const char *_ptr, const char **rptr)
 {
-       const unsigned char *ptr = (unsigned char *) _ptr;
+       const unsigned char *ptr = (const unsigned char *) _ptr;
        unsigned char b = *ptr;
        guint32 len;
        
@@ -906,8 +950,17 @@ mono_metadata_decode_value (const char *_ptr, const char **rptr)
        return len;
 }
 
+/*
+ * mono_metadata_parse_typedef_or_ref:
+ * @m: a metadata context.
+ * @ptr: a pointer to an encoded TypedefOrRef in @m
+ * @rptr: pointer updated to match the end of the decoded stream
+ *
+ * Returns: a token valid in the @m metadata decoded from
+ * the compressed representation.
+ */
 guint32
-mono_metadata_parse_typedef_or_ref (MonoMetadata *m, const char *ptr, const char **rptr)
+mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
 {
        guint32 token;
        token = mono_metadata_decode_value (ptr, &ptr);
@@ -916,8 +969,19 @@ mono_metadata_parse_typedef_or_ref (MonoMetadata *m, const char *ptr, const char
        return mono_metadata_token_from_dor (token);
 }
 
+/*
+ * mono_metadata_parse_custom_mod:
+ * @m: a metadata context.
+ * @dest: storage where the info about the custom modifier is stored (may be NULL)
+ * @ptr: a pointer to (possibly) the start of a custom modifier list
+ * @rptr: pointer updated to match the end of the decoded stream
+ *
+ * Checks if @ptr points to a type custom modifier compressed representation.
+ *
+ * Returns: #TRUE if a custom modifier was found, #FALSE if not.
+ */
 int
-mono_metadata_parse_custom_mod (MonoMetadata *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
+mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
 {
        MonoCustomMod local;
        if ((*ptr == MONO_TYPE_CMOD_OPT) ||
@@ -931,8 +995,19 @@ mono_metadata_parse_custom_mod (MonoMetadata *m, MonoCustomMod *dest, const char
        return FALSE;
 }
 
+/*
+ * mono_metadata_parse_array:
+ * @m: a metadata context.
+ * @ptr: a pointer to an encoded array description.
+ * @rptr: pointer updated to match the end of the decoded stream
+ *
+ * Decodes the compressed array description found in the metadata @m at @ptr.
+ *
+ * Returns: a #MonoArrayType structure describing the array type
+ * and dimensions.
+ */
 MonoArrayType *
-mono_metadata_parse_array (MonoMetadata *m, const char *ptr, const char **rptr)
+mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
 {
        int i;
        MonoArrayType *array = g_new0 (MonoArrayType, 1);
@@ -957,6 +1032,12 @@ mono_metadata_parse_array (MonoMetadata *m, const char *ptr, const char **rptr)
        return array;
 }
 
+/*
+ * mono_metadata_free_array:
+ * @array: array description
+ *
+ * Frees the array description returned from mono_metadata_parse_array().
+ */
 void
 mono_metadata_free_array (MonoArrayType *array)
 {
@@ -1021,30 +1102,51 @@ static GHashTable *type_cache = NULL;
 /*
  * MonoTypes with modifies are never cached, so we never check or use that field.
  */
-static int
-mono_type_hash (MonoType *type)
+static guint
+mono_type_hash (gconstpointer data)
 {
+       const MonoType *type = (const MonoType *) data;
        return type->type | (type->byref << 8) | (type->attrs << 9);
 }
 
-static gboolean
-mono_type_equal (MonoType *a, MonoType *b)
+static gint
+mono_type_equal (gconstpointer ka, gconstpointer kb)
 {
+       const MonoType *a = (const MonoType *) ka;
+       const MonoType *b = (const MonoType *) kb;
+       
        if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
                return 0;
        /* need other checks */
        return 1;
 }
 
+/*
+ * mono_metadata_parse_type:
+ * @m: metadata context
+ * @mode: king of type that may be found at @ptr
+ * @opt_attrs: optional attributes to store in the returned type
+ * @ptr: pointer to the type representation
+ * @rptr: pointer updated to match the end of the decoded stream
+ * 
+ * Decode a compressed type description found at @ptr in @m.
+ * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
+ * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
+ * This function can be used to decode type descriptions in method signatures,
+ * field signatures, locals signatures etc.
+ *
+ * Returns: a #MonoType structure representing the decoded type.
+ */
 MonoType*
-mono_metadata_parse_type (MonoMetadata *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
+mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
 {
        MonoType *type, *cached;
 
        if (!type_cache) {
                int i;
                type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
-               for (i=0; i < NBUILTIN_TYPES (); ++i)
+
+               for (i = 0; i < NBUILTIN_TYPES (); ++i)
                        g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
        }
 
@@ -1115,11 +1217,49 @@ mono_metadata_parse_type (MonoMetadata *m, MonoParseTypeMode mode, short opt_att
        }
 }
 
+/*
+ * mono_metadata_parse_signature:
+ * @image: metadata context
+ * @toke: metadata token
+ *
+ * Decode a method signature stored in the STANDALONESIG table
+ *
+ * Returns: a MonoMethodSignature describing the signature.
+ */
+MonoMethodSignature *
+mono_metadata_parse_signature (MonoImage *image, guint32 token)
+{
+       MonoTableInfo *tables = image->tables;
+       guint32 idx = mono_metadata_token_index (token);
+       guint32 sig;
+       const char *ptr;
+
+       g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
+               
+       sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
+
+       ptr = mono_metadata_blob_heap (image, sig);
+       mono_metadata_decode_blob_size (ptr, &ptr);
+
+       return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL); 
+}
+
+/*
+ * mono_metadata_parse_method_signature:
+ * @m: metadata context
+ * @def: use #TRUE when parsing MethodDef, #FALSE with MethodRef signatures.
+ * @ptr: pointer to the signature metadata representation
+ * @rptr: pointer updated to match the end of the decoded stream
+ *
+ * Decode a method signature stored at @ptr.
+ *
+ * Returns: a MonoMethodSignature describing the signature.
+ */
 MonoMethodSignature *
-mono_metadata_parse_method_signature (MonoMetadata *m, int def, const char *ptr, const char **rptr)
+mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
 {
        MonoMethodSignature *method;
-       int i, align, offset = 0;
+       int i;
        guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
 
        if (*ptr & 0x20)
@@ -1137,11 +1277,7 @@ mono_metadata_parse_method_signature (MonoMetadata *m, int def, const char *ptr,
        method->call_convention = call_convention;
        method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, 0, ptr, &ptr);
 
-       if (method->hasthis)
-               offset += sizeof(gpointer);
        if (method->param_count) {
-               int size;
-               
                method->sentinelpos = -1;
                
                for (i = 0; i < method->param_count; ++i) {
@@ -1152,13 +1288,8 @@ mono_metadata_parse_method_signature (MonoMetadata *m, int def, const char *ptr,
                                ptr++;
                        }
                        method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, &ptr);
-                       size = mono_type_size (method->params [i], &align);
-                       offset += align - 1;
-                       offset &= ~(align - 1);
-                       offset += size;
                }
        }
-       method->params_size = offset;
 
        if (rptr)
                *rptr = ptr;
@@ -1168,15 +1299,21 @@ mono_metadata_parse_method_signature (MonoMetadata *m, int def, const char *ptr,
        return method;
 }
 
+/*
+ * mono_metadata_free_method_signature:
+ * @sig: signature to destroy
+ *
+ * Free the memory allocated in the signature @sig.
+ */
 void
-mono_metadata_free_method_signature (MonoMethodSignature *method)
+mono_metadata_free_method_signature (MonoMethodSignature *sig)
 {
        int i;
-       mono_metadata_free_type (method->ret);
-       for (i = 0; i < method->param_count; ++i)
-               mono_metadata_free_type (method->params [i]);
+       mono_metadata_free_type (sig->ret);
+       for (i = 0; i < sig->param_count; ++i)
+               mono_metadata_free_type (sig->params [i]);
 
-       g_free (method);
+       g_free (sig);
 }
 
 /* 
@@ -1194,7 +1331,7 @@ mono_metadata_free_method_signature (MonoMethodSignature *method)
  * This extracts a Type as specified in Partition II (22.2.12) 
  */
 static void
-do_mono_metadata_parse_type (MonoType *type, MonoMetadata *m, const char *ptr, const char **rptr)
+do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
 {
        type->type = mono_metadata_decode_value (ptr, &ptr);
        
@@ -1243,28 +1380,12 @@ do_mono_metadata_parse_type (MonoType *type, MonoMetadata *m, const char *ptr, c
                *rptr = ptr;
 }
 
-#if 0
-/**
- * mono_metadata_parse_type:
- * @m: metadata context to scan
- * @ptr: pointer to encoded Type stream.
- * @rptr: the new position in the stream after parsing the type
+/*
+ * mono_metadata_free_type:
+ * @type: type to free
  *
- * Returns: A MonoType structure that has the parsed information
- * from the type stored at @ptr in the metadata table @m.
+ * Free the memory allocated for type @type.
  */
-MonoType *
-mono_metadata_parse_type (MonoMetadata *m, const char *ptr, const char **rptr)
-{
-       /* should probably be allocated in a memchunk */
-       MonoType *type = g_new0(MonoType, 1);
-
-       do_mono_metadata_parse_type (type, m, ptr, rptr);
-
-       return type;
-}
-#endif
-
 void
 mono_metadata_free_type (MonoType *type)
 {
@@ -1285,6 +1406,7 @@ mono_metadata_free_type (MonoType *type)
        g_free (type);
 }
 
+#if 0
 static void
 hex_dump (const char *buffer, int base, int count)
 {
@@ -1305,6 +1427,7 @@ hex_dump (const char *buffer, int base, int count)
        }
        fflush (stdout);
 }
+#endif
 
 /** 
  * @mh: The Method header
@@ -1349,6 +1472,7 @@ parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
                        mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
                        for (i = 0; i < mh->num_clauses; ++i) {
                                MonoExceptionClause *ec = &mh->clauses [i];
+
                                if (is_fat) {
                                        /* we could memcpy and byteswap */
                                        ec->flags = read32 (p);
@@ -1379,6 +1503,7 @@ parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
                                }
                                /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
                        }
+
                }
                if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
                        ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
@@ -1387,11 +1512,21 @@ parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
        }
 }
 
+/*
+ * mono_metadata_parse_mh:
+ * @m: metadata context
+ * @ptr: pointer to the method header.
+ *
+ * Decode the method header at @ptr, including pointer to the IL code,
+ * info about local variables and optional exception tables.
+ *
+ * Returns: a MonoMethodHeader.
+ */
 MonoMethodHeader *
-mono_metadata_parse_mh (MonoMetadata *m, const char *ptr)
+mono_metadata_parse_mh (MonoImage *m, const char *ptr)
 {
        MonoMethodHeader *mh;
-       unsigned char flags = *(unsigned char *) ptr;
+       unsigned char flags = *(const unsigned char *) ptr;
        unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
        guint16 fat_flags;
        guint32 local_var_sig_tok, max_stack, code_size, init_locals;
@@ -1456,31 +1591,21 @@ mono_metadata_parse_mh (MonoMetadata *m, const char *ptr)
                       
        if (local_var_sig_tok) {
                MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
-               const char *ptr;
+               const char *locals_ptr;
                guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
                int len=0, i, bsize;
-               guint offset = 0;
 
                mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
-               ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
-               bsize = mono_metadata_decode_blob_size (ptr, &ptr);
-               if (*ptr != 0x07)
+               locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
+               bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
+               if (*locals_ptr != 0x07)
                        g_warning ("wrong signature for locals blob");
-               ptr++;
-               len = mono_metadata_decode_value (ptr, &ptr);
+               locals_ptr++;
+               len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
                mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
                mh->num_locals = len;
-               for (i = 0; i < len; ++i) {
-                       int val;
-                       int align;
-                       mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, ptr, &ptr);
-
-                       val = mono_type_size (mh->locals [i], &align);
-                       offset += align - 1;
-                       offset &= ~(align - 1);
-                       offset += val;
-               }
-               mh->locals_size = offset;
+               for (i = 0; i < len; ++i)
+                       mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
        } else {
                mh = g_new0 (MonoMethodHeader, 1);
        }
@@ -1493,6 +1618,12 @@ mono_metadata_parse_mh (MonoMetadata *m, const char *ptr)
        return mh;
 }
 
+/*
+ * mono_metadata_free_mh:
+ * @mh: a method header
+ *
+ * Free the memory allocated for the method header.
+ */
 void
 mono_metadata_free_mh (MonoMethodHeader *mh)
 {
@@ -1507,19 +1638,30 @@ mono_metadata_free_mh (MonoMethodHeader *mh)
  * mono_metadata_parse_field_type:
  * @m: metadata context to extract information from
  * @ptr: pointer to the field signature
+ * @rptr: pointer updated to match the end of the decoded stream
  *
  * Parses the field signature, and returns the type information for it. 
  *
  * Returns: The MonoType that was extracted from @ptr.
  */
 MonoType *
-mono_metadata_parse_field_type (MonoMetadata *m, short field_flags, const char *ptr, const char **rptr)
+mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
 {
        return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
 }
 
+/**
+ * mono_metadata_parse_param:
+ * @m: metadata context to extract information from
+ * @ptr: pointer to the param signature
+ * @rptr: pointer updated to match the end of the decoded stream
+ *
+ * Parses the param signature, and returns the type information for it. 
+ *
+ * Returns: The MonoType that was extracted from @ptr.
+ */
 MonoType *
-mono_metadata_parse_param (MonoMetadata *m, const char *ptr, const char **rptr)
+mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
 {
        return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
 }
@@ -1607,7 +1749,7 @@ static int
 typedef_locator (const void *a, const void *b)
 {
        locator_t *loc = (locator_t *) a;
-       char *bb = (char *) b;
+       const char *bb = (const char *) b;
        int typedef_index = (bb - loc->t->base) / loc->t->row_size;
        guint32 col, col_next;
 
@@ -1637,7 +1779,7 @@ static int
 table_locator (const void *a, const void *b)
 {
        locator_t *loc = (locator_t *) a;
-       char *bb = (char *) b;
+       const char *bb = (const char *) b;
        guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
        guint32 col;
        
@@ -1653,8 +1795,17 @@ table_locator (const void *a, const void *b)
                return 1;
 }
 
+/*
+ * mono_metadata_typedef_from_field:
+ * @meta: metadata context
+ * @index: FieldDef token
+ *
+ * Returns the 1-based index into the TypeDef table of the type that
+ * declared the field described by @index.
+ * Returns 0 if not found.
+ */
 guint32
-mono_metadata_typedef_from_field (MonoMetadata *meta, guint32 index)
+mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
 {
        MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
        locator_t loc;
@@ -1673,8 +1824,17 @@ mono_metadata_typedef_from_field (MonoMetadata *meta, guint32 index)
        return loc.result + 1;
 }
 
+/*
+ * mono_metadata_typedef_from_method:
+ * @meta: metadata context
+ * @index: MethodDef token
+ *
+ * Returns the 1-based index into the TypeDef table of the type that
+ * declared the method described by @index.
+ * Returns 0 if not found.
+ */
 guint32
-mono_metadata_typedef_from_method (MonoMetadata *meta, guint32 index)
+mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
 {
        MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
        locator_t loc;
@@ -1693,8 +1853,16 @@ mono_metadata_typedef_from_method (MonoMetadata *meta, guint32 index)
        return loc.result + 1;
 }
 
+/*
+ * mono_metadata_interfaces_from_typedef:
+ * @meta: metadata context
+ * @index: typedef token
+ * 
+ * Returns and array of interfaces that the @index typedef token implements.
+ * The number of elemnts in the array is returned in @count.
+ */
 MonoClass**
-mono_metadata_interfaces_from_typedef (MonoMetadata *meta, guint32 index, guint *count)
+mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
 {
        MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
        locator_t loc;
@@ -1738,6 +1906,110 @@ mono_metadata_interfaces_from_typedef (MonoMetadata *meta, guint32 index, guint
        return result;
 }
 
+/*
+ * mono_metadata_nested_in_typedef:
+ * @meta: metadata context
+ * @index: typedef token
+ * 
+ * Returns: the 1-based index into the TypeDef table of the type
+ * where the type described by @index is nested.
+ * Retruns 0 if @index describes a non-nested type.
+ */
+guint32
+mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
+{
+       MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
+       locator_t loc;
+       
+       if (!tdef->base)
+               return 0;
+
+       loc.idx = mono_metadata_token_index (index);
+       loc.col_idx = MONO_NESTED_CLASS_NESTED;
+       loc.t = tdef;
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+
+       /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
+       return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
+}
+
+/*
+ * mono_metadata_nesting_typedef:
+ * @meta: metadata context
+ * @index: typedef token
+ * 
+ * Returns: the 1-based index into the TypeDef table of the first type
+ * that is nested inside the type described by @index.
+ * Retruns 0 if @index doesn't have nested types.
+ */
+guint32
+mono_metadata_nesting_typedef (MonoImage *meta, guint32 index)
+{
+       MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
+       locator_t loc;
+       guint32 start;
+       
+       if (!tdef->base)
+               return 0;
+
+       loc.idx = mono_metadata_token_index (index);
+       loc.col_idx = MONO_NESTED_CLASS_ENCLOSING;
+       loc.t = tdef;
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+
+       start = loc.result;
+
+       while (start > 0) {
+               if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
+                       start--;
+               else
+                       break;
+       }
+
+       /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
+       return start + 1;
+}
+
+/*
+ * mono_metadata_packing_from_typedef:
+ * @meta: metadata context
+ * @index: token representing a type
+ * 
+ * Returns the info stored in the ClassLAyout table for the given typedef token
+ * into the @packing and @size pointers.
+ * Returns 0 if the info is not found.
+ */
+guint32
+mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
+{
+       MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
+       locator_t loc;
+       guint32 cols [MONO_CLASS_LAYOUT_SIZE];
+       
+       if (!tdef->base)
+               return 0;
+
+       loc.idx = mono_metadata_token_index (index);
+       loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
+       loc.t = tdef;
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+
+       mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
+       if (packing)
+               *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
+       if (size)
+               *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
+
+       /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
+       return loc.result + 1;
+}
+
 #ifndef __GNUC__
 #define __alignof__(a) sizeof(a)
 #endif
@@ -1814,8 +2086,15 @@ mono_type_size (MonoType *t, gint *align)
        case MONO_TYPE_VALUETYPE: {
                guint32 size;
 
-               size = mono_class_value_size (t->data.klass, align);
-               return size;
+               if (!t->data.klass->size_inited)
+                       mono_class_init (t->data.klass);
+
+               if (t->data.klass->enumtype) {
+                       return mono_type_size (t->data.klass->enum_basetype, align);
+               } else {
+                       size = mono_class_value_size (t->data.klass, align);
+                       return size;
+               }
        }
        case MONO_TYPE_CLASS:
        case MONO_TYPE_SZARRAY:
@@ -1831,6 +2110,131 @@ mono_type_size (MonoType *t, gint *align)
        return 0;
 }
 
+/*
+ * mono_type_stack_size:
+ * @t: the type to return the size it uses on the stack
+ *
+ * Returns: the number of bytes required to hold an instance of this
+ * type on the runtime stack
+ */
+int
+mono_type_stack_size (MonoType *t, gint *align)
+{
+       g_assert (t != NULL);
+
+       if (t->byref) {
+               *align = __alignof__(gpointer);
+               return sizeof (gpointer);
+       }
+
+       switch (t->type){
+       case MONO_TYPE_BOOLEAN:
+       case MONO_TYPE_CHAR:
+       case MONO_TYPE_I1:
+       case MONO_TYPE_U1:
+       case MONO_TYPE_I2:
+       case MONO_TYPE_U2:
+       case MONO_TYPE_I4:
+       case MONO_TYPE_U4:
+       case MONO_TYPE_I:
+       case MONO_TYPE_U:
+       case MONO_TYPE_STRING:
+       case MONO_TYPE_OBJECT:
+       case MONO_TYPE_CLASS:
+       case MONO_TYPE_SZARRAY:
+       case MONO_TYPE_PTR:
+       case MONO_TYPE_FNPTR:
+       case MONO_TYPE_ARRAY:
+       case MONO_TYPE_TYPEDBYREF:
+               *align = __alignof__(gpointer);
+               return sizeof (gpointer);
+       case MONO_TYPE_R4:
+               *align = __alignof__(float);
+               return 4;
+               
+       case MONO_TYPE_I8:
+       case MONO_TYPE_U8:
+               *align = __alignof__(gint64);
+       case MONO_TYPE_R8:
+               *align = __alignof__(double);
+               return 8;
+               
+       case MONO_TYPE_VALUETYPE: {
+               guint32 size;
+
+               if (t->data.klass->enumtype)
+                       return mono_type_stack_size (t->data.klass->enum_basetype, align);
+               else {
+                       size = mono_class_value_size (t->data.klass, align);
+                       *align = __alignof__(gpointer);
+                       return size;
+               }
+       }
+       default:
+               g_error ("type 0x%02x unknown", t->type);
+       }
+       return 0;
+}
+
+MonoType *
+mono_get_param_info (MonoMethodSignature *sig, int param_num, int *size, int *align)
+{
+       MonoType *type;
+       int s, a;
+
+       if (!size)
+               size = &s;
+
+       if (!align)
+               align = &a;
+
+       if (param_num == -1)
+               type = sig->ret;
+       else
+               type = sig->params [param_num];
+       
+       if (sig->pinvoke && !type->byref && type->type == MONO_TYPE_VALUETYPE && 
+           !type->data.klass->enumtype) {
+               *size = mono_class_native_size (type->data.klass);
+       } else {
+               *size = mono_type_stack_size (type, align);
+       }
+
+       return type;
+}
+
+/*
+ * mono_metadata_type_hash:
+ * @t1: a type
+ *
+ * Computes an hash value for @t1 to be used in GHashTable.
+ */
+guint
+mono_metadata_type_hash (MonoType *t1)
+{
+       guint hash = t1->type;
+
+       hash |= t1->byref << 6; /* do not collide with t1->type values */
+       switch (t1->type) {
+       case MONO_TYPE_VALUETYPE:
+       case MONO_TYPE_CLASS:
+               /* check if the distribution is good enough */
+               return hash << 7 | g_str_hash (t1->data.klass->name);
+       case MONO_TYPE_PTR:
+       case MONO_TYPE_SZARRAY:
+               return hash << 7 | mono_metadata_type_hash (t1->data.type);
+       }
+       return hash;
+}
+
+/*
+ * mono_metadata_type_equal:
+ * @t1: a type
+ * @t2: another type
+ *
+ * Determine if @t1 and @t2 represent the same type.
+ * Returns: #TRUE if @t1 and @t2 are equal.
+ */
 gboolean
 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
 {
@@ -1860,8 +2264,13 @@ mono_metadata_type_equal (MonoType *t1, MonoType *t2)
        case MONO_TYPE_VALUETYPE:
        case MONO_TYPE_CLASS:
                return t1->data.klass == t2->data.klass;
+       case MONO_TYPE_PTR:
        case MONO_TYPE_SZARRAY:
                return mono_metadata_type_equal (t1->data.type, t2->data.type);
+       case MONO_TYPE_ARRAY:
+               if (t1->data.array->rank != t2->data.array->rank)
+                       return FALSE;
+               return mono_metadata_type_equal (t1->data.array->type, t2->data.array->type);
        default:
                g_error ("implement type compare for %0x!", t1->type);
                return FALSE;
@@ -1870,6 +2279,15 @@ mono_metadata_type_equal (MonoType *t1, MonoType *t2)
        return FALSE;
 }
 
+/*
+ * mono_metadata_signature_equal:
+ * @sig1: a signature
+ * @sig2: another signature
+ *
+ * Determine if @sig1 and @sig2 represent the same signature, with the
+ * same number of arguments and the same types.
+ * Returns: #TRUE if @sig1 and @sig2 are equal.
+ */
 gboolean
 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
 {
@@ -1890,9 +2308,32 @@ mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *s
                        return FALSE;
        }
 
+       if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
+               return FALSE;
        return TRUE;
 }
 
+guint
+mono_signature_hash (MonoMethodSignature *sig)
+{
+       guint i, res = sig->ret->type;
+
+       for (i = 0; i < sig->param_count; i++)
+               res = (res << 5) - res + sig->params[i]->type;
+
+       return res;
+}
+
+/*
+ * mono_metadata_encode_value:
+ * @value: value to encode
+ * @buf: buffer where to write the compressed representation
+ * @endbuf: pointer updated to point at the end of the encoded output
+ *
+ * Encodes the value @value in the compressed representation used
+ * in metadata and stores the result in @buf. @buf needs to be big
+ * enough to hold the data (4 bytes).
+ */
 void
 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
 {
@@ -1915,3 +2356,612 @@ mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
                *endbuf = p;
 }
 
+/*
+ * mono_metadata_field_info:
+ * @meta: the Image the field is defined in
+ * @index: the index in the field table representing the field
+ * @offset: a pointer to an integer where to store the offset that 
+ * may have been specified for the field in a FieldLayout table
+ * @rva: a pointer to the RVA of the field data in the image that
+ * may have been defined in a FieldRVA table
+ * @marshal_spec: a pointer to the marshal spec that may have been 
+ * defined for the field in a FieldMarshal table.
+ *
+ * Gather info for field @index that may have been defined in the FieldLayout, 
+ * FieldRVA and FieldMarshal tables.
+ * Either of offset, rva and marshal_spec can be NULL if you're not interested 
+ * in the data.
+ */
+void
+mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, 
+                         MonoMarshalSpec **marshal_spec)
+{
+       MonoTableInfo *tdef;
+       locator_t loc;
+
+       loc.idx = index + 1;
+       if (offset) {
+               tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
+
+               loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
+               loc.t = tdef;
+
+               if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+                       *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
+               } else {
+                       *offset = (guint32)-1;
+               }
+       }
+       if (rva) {
+               tdef = &meta->tables [MONO_TABLE_FIELDRVA];
+
+               loc.col_idx = MONO_FIELD_RVA_FIELD;
+               loc.t = tdef;
+               
+               if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+                       /*
+                        * LAMESPEC: There is no signature, no nothing, just the raw data.
+                        */
+                       *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
+               } else {
+                       *rva = 0;
+               }
+       }
+       if (marshal_spec) {
+               const char *p;
+               
+               tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
+
+               loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
+               loc.t = tdef;
+               loc.idx = (loc.idx << HAS_FIELD_MARSHAL_BITS) | HAS_FIELD_MARSHAL_FIELDSREF;
+
+               if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+                       p = mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
+                       *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
+               } else {
+                       *marshal_spec = NULL;
+               }
+       }
+
+}
+
+/*
+ * mono_metadata_get_constant_index:
+ * @meta: the Image the field is defined in
+ * @index: the token that may have a row defined in the constants table
+ *
+ * @token must be a FieldDef, ParamDef or PropertyDef token.
+ *
+ * Returns: the index into the Constsnts table or 0 if not found.
+ */
+guint32
+mono_metadata_get_constant_index (MonoImage *meta, guint32 token)
+{
+       MonoTableInfo *tdef;
+       locator_t loc;
+       guint32 index = mono_metadata_token_index (token);
+
+       tdef = &meta->tables [MONO_TABLE_CONSTANT];
+       index <<= HASCONSTANT_BITS;
+       switch (mono_metadata_token_table (token)) {
+       case MONO_TABLE_FIELD:
+               index |= HASCONSTANT_FIEDDEF;
+               break;
+       case MONO_TABLE_PARAM:
+               index |= HASCONSTANT_PARAM;
+               break;
+       case MONO_TABLE_PROPERTY:
+               index |= HASCONSTANT_PROPERTY;
+               break;
+       default:
+               g_warning ("Not a valid token for the constant table: 0x%08x", token);
+               return 0;
+       }
+       loc.idx = index;
+       loc.col_idx = MONO_CONSTANT_PARENT;
+       loc.t = tdef;
+
+       if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+               return loc.result + 1;
+       }
+       return 0;
+}
+
+/*
+ * mono_metadata_events_from_typedef:
+ * @meta: metadata context
+ * @index: 0-based index (in the TypeDef table) describing a type
+ *
+ * Returns: the 0-based index in the Event table for the events in the
+ * type. The last event that belongs to the type (plus 1) is stored
+ * in the @end_idx pointer.
+ */
+guint32
+mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
+{
+       locator_t loc;
+       guint32 start, end;
+       MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_EVENTMAP];
+
+       *end_idx = 0;
+       
+       if (!tdef->base)
+               return 0;
+
+       loc.t = tdef;
+       loc.col_idx = MONO_EVENT_MAP_PARENT;
+       loc.idx = index + 1;
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+       
+       start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
+       if (loc.result + 1 < tdef->rows) {
+               end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
+       } else {
+               end = meta->tables [MONO_TABLE_EVENT].rows;
+       }
+
+       *end_idx = end;
+       return start - 1;
+}
+
+/*
+ * mono_metadata_methods_from_event:
+ * @meta: metadata context
+ * @index: 0-based index (in the Event table) describing a event
+ *
+ * Returns: the 0-based index in the MethodDef table for the methods in the
+ * event. The last method that belongs to the event (plus 1) is stored
+ * in the @end_idx pointer.
+ */
+guint32
+mono_metadata_methods_from_event   (MonoImage *meta, guint32 index, guint *end_idx)
+{
+       locator_t loc;
+       guint start, end;
+       guint32 cols [MONO_METHOD_SEMA_SIZE];
+       MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
+
+       *end_idx = 0;
+       if (!msemt->base)
+               return 0;
+
+       loc.t = msemt;
+       loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
+       loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_EVENT; /* Method association coded index */
+
+       if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
+               return 0;
+
+       start = loc.result;
+       /*
+        * We may end up in the middle of the rows... 
+        */
+       while (start > 0) {
+               if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
+                       start--;
+               else
+                       break;
+       }
+       end = start + 1;
+       while (end < msemt->rows) {
+               mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
+               if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
+                       break;
+               ++end;
+       }
+       *end_idx = end;
+       return start;
+}
+
+/*
+ * mono_metadata_properties_from_typedef:
+ * @meta: metadata context
+ * @index: 0-based index (in the TypeDef table) describing a type
+ *
+ * Returns: the 0-based index in the Property table for the properties in the
+ * type. The last property that belongs to the type (plus 1) is stored
+ * in the @end_idx pointer.
+ */
+guint32
+mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
+{
+       locator_t loc;
+       guint32 start, end;
+       MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_PROPERTYMAP];
+
+       *end_idx = 0;
+       
+       if (!tdef->base)
+               return 0;
+
+       loc.t = tdef;
+       loc.col_idx = MONO_PROPERTY_MAP_PARENT;
+       loc.idx = index + 1;
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+       
+       start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
+       if (loc.result + 1 < tdef->rows) {
+               end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
+       } else {
+               end = meta->tables [MONO_TABLE_PROPERTY].rows;
+       }
+
+       *end_idx = end;
+       return start - 1;
+}
+
+/*
+ * mono_metadata_methods_from_property:
+ * @meta: metadata context
+ * @index: 0-based index (in the PropertyDef table) describing a property
+ *
+ * Returns: the 0-based index in the MethodDef table for the methods in the
+ * property. The last method that belongs to the property (plus 1) is stored
+ * in the @end_idx pointer.
+ */
+guint32
+mono_metadata_methods_from_property   (MonoImage *meta, guint32 index, guint *end_idx)
+{
+       locator_t loc;
+       guint start, end;
+       guint32 cols [MONO_METHOD_SEMA_SIZE];
+       MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
+
+       *end_idx = 0;
+       if (!msemt->base)
+               return 0;
+
+       loc.t = msemt;
+       loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
+       loc.idx = ((index + 1) << HAS_SEMANTICS_BITS) | HAS_SEMANTICS_PROPERTY; /* Method association coded index */
+
+       if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
+               return 0;
+
+       start = loc.result;
+       /*
+        * We may end up in the middle of the rows... 
+        */
+       while (start > 0) {
+               if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
+                       start--;
+               else
+                       break;
+       }
+       end = start + 1;
+       while (end < msemt->rows) {
+               mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
+               if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
+                       break;
+               ++end;
+       }
+       *end_idx = end;
+       return start;
+}
+
+guint32
+mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
+{
+       locator_t loc;
+       MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_IMPLMAP];
+
+       if (!tdef->base)
+               return 0;
+
+       loc.t = tdef;
+       loc.col_idx = MONO_IMPLMAP_MEMBER;
+       loc.idx = ((method_idx + 1) << MEMBERFORWD_BITS) | MEMBERFORWD_METHODDEF;
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return 0;
+
+       return loc.result + 1;
+}
+
+/**
+ * @image: context where the image is created
+ * @type_spec:  typespec token
+ *
+ * Creates a MonoType representing the TypeSpec indexed by the @type_spec
+ * token.
+ */
+MonoType *
+mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
+{
+       guint32 idx = mono_metadata_token_index (type_spec);
+       MonoTableInfo *t;
+       guint32 cols [MONO_TYPESPEC_SIZE];       
+       const char *ptr;
+       guint32 len;
+       MonoType *type;
+
+       t = &image->tables [MONO_TABLE_TYPESPEC];
+       
+       mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
+       ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
+       len = mono_metadata_decode_value (ptr, &ptr);
+       type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
+
+       return type;
+}
+
+MonoMarshalSpec *
+mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
+{
+       MonoMarshalSpec *res;
+       int len;
+       const char *start = ptr;
+
+       /* fixme: this is incomplete, but I cant find more infos in the specs */
+
+       res = g_new0 (MonoMarshalSpec, 1);
+       
+       len = mono_metadata_decode_value (ptr, &ptr);
+       res->native = *ptr++;
+
+       if (res->native == MONO_NATIVE_LPARRAY) {
+               if (ptr - start <= len)
+                       res->elem_type = *ptr++;
+               if (ptr - start <= len)
+                       res->param_num = mono_metadata_decode_value (ptr, &ptr);
+               if (ptr - start <= len)
+                       res->num_elem = mono_metadata_decode_value (ptr, &ptr);
+       } 
+
+       if (res->native == MONO_NATIVE_BYVALTSTR) {
+               if (ptr - start <= len)
+                       res->num_elem = mono_metadata_decode_value (ptr, &ptr);
+       }
+
+       if (res->native == MONO_NATIVE_BYVALARRAY) {
+               if (ptr - start <= len)
+                       res->num_elem = mono_metadata_decode_value (ptr, &ptr);
+       }
+
+       return res;
+}
+
+guint32
+mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
+                       gboolean unicode, MonoMarshalConv *conv) 
+{
+       MonoMarshalConv dummy_conv;
+       int t = type->type;
+
+       if (!conv)
+               conv = &dummy_conv;
+
+       *conv = MONO_MARSHAL_CONV_NONE;
+
+       if (type->byref)
+               return MONO_NATIVE_UINT;
+
+handle_enum:
+       switch (t) {
+       case MONO_TYPE_BOOLEAN: 
+               if (mspec) {
+                       switch (mspec->native) {
+                       case MONO_NATIVE_VARIANTBOOL:
+                               *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
+                               return MONO_NATIVE_VARIANTBOOL;
+                       case MONO_NATIVE_BOOLEAN:
+                               *conv = MONO_MARSHAL_CONV_BOOL_I4;
+                               return MONO_NATIVE_BOOLEAN;
+                       case MONO_NATIVE_U1:
+                               return MONO_NATIVE_U1;
+                       default:
+                               g_error ("cant marshal bool to native type %02x", mspec->native);
+                       }
+               }
+               *conv = MONO_MARSHAL_CONV_BOOL_I4;
+               return MONO_NATIVE_BOOLEAN;
+       case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
+       case MONO_TYPE_I1: return MONO_NATIVE_I1;
+       case MONO_TYPE_U1: return MONO_NATIVE_U1;
+       case MONO_TYPE_I2: return MONO_NATIVE_I2;
+       case MONO_TYPE_U2: return MONO_NATIVE_U2;
+       case MONO_TYPE_I4: return MONO_NATIVE_I4;
+       case MONO_TYPE_U4: return MONO_NATIVE_U4;
+       case MONO_TYPE_I8: return MONO_NATIVE_I8;
+       case MONO_TYPE_U8: return MONO_NATIVE_U8;
+       case MONO_TYPE_R4: return MONO_NATIVE_R4;
+       case MONO_TYPE_R8: return MONO_NATIVE_R8;
+       case MONO_TYPE_STRING:
+               if (mspec) {
+                       switch (mspec->native) {
+                       case MONO_NATIVE_BSTR:
+                               *conv = MONO_MARSHAL_CONV_STR_BSTR;
+                               return MONO_NATIVE_BSTR;
+                       case MONO_NATIVE_LPSTR:
+                               *conv = MONO_MARSHAL_CONV_STR_LPSTR;
+                               return MONO_NATIVE_LPSTR;
+                       case MONO_NATIVE_LPWSTR:
+                               *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
+                               return MONO_NATIVE_LPWSTR;
+                       case MONO_NATIVE_LPTSTR:
+                               *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
+                               return MONO_NATIVE_LPTSTR;
+                       case MONO_NATIVE_ANSIBSTR:
+                               *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
+                               return MONO_NATIVE_ANSIBSTR;
+                       case MONO_NATIVE_TBSTR:
+                               *conv = MONO_MARSHAL_CONV_STR_TBSTR;
+                               return MONO_NATIVE_TBSTR;
+                       case MONO_NATIVE_BYVALTSTR:
+                               if (unicode)
+                                       *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
+                               else
+                                       *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
+                               return MONO_NATIVE_BYVALTSTR;
+                       default:
+                               g_error ("cant marshal string to native type %02x", mspec->native);
+                       }
+               }       
+               *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
+               return MONO_NATIVE_LPTSTR; 
+       case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
+       case MONO_TYPE_VALUETYPE: /*FIXME*/
+               if (type->data.klass->enumtype) {
+                       t = type->data.klass->enum_basetype->type;
+                       goto handle_enum;
+               }
+               return MONO_NATIVE_STRUCT;
+       case MONO_TYPE_SZARRAY: 
+       case MONO_TYPE_ARRAY: 
+               if (mspec) {
+                       switch (mspec->native) {
+                       case MONO_NATIVE_BYVALARRAY:
+                               *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
+                               return MONO_NATIVE_BYVALARRAY;
+                       case MONO_NATIVE_SAFEARRAY:
+                               *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
+                               return MONO_NATIVE_SAFEARRAY;
+                       case MONO_NATIVE_LPARRAY:                               
+                               *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
+                               return MONO_NATIVE_LPARRAY;
+                       default:
+                               g_error ("cant marshal array as native type %02x", mspec->native);
+                       }
+               }       
+
+               *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
+               return MONO_NATIVE_LPARRAY;
+       case MONO_TYPE_I: return MONO_NATIVE_INT;
+       case MONO_TYPE_U: return MONO_NATIVE_UINT;
+       case MONO_TYPE_OBJECT: {
+               if (mspec) {
+                       switch (mspec->native) {
+                       case MONO_NATIVE_STRUCT:
+                               return MONO_NATIVE_STRUCT;
+                       case MONO_NATIVE_INTERFACE:
+                               *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
+                               return MONO_NATIVE_INTERFACE;
+                       case MONO_NATIVE_IDISPATCH:
+                               *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
+                               return MONO_NATIVE_IDISPATCH;
+                       case MONO_NATIVE_IUNKNOWN:
+                               *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
+                               return MONO_NATIVE_IUNKNOWN;
+                       default:
+                               g_error ("cant marshal object as native type %02x", mspec->native);
+                       }
+               }
+               return MONO_NATIVE_LPSTRUCT; /* ?? */
+       }
+       case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
+       case MONO_TYPE_CLASS: 
+               /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
+               return MONO_NATIVE_INTERFACE; /* ?? */
+       case MONO_TYPE_TYPEDBYREF:
+       default:
+               g_error ("type 0x%02x not handled in marshal", t);
+       }
+       return MONO_NATIVE_MAX;
+}
+
+gint32
+mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, gint32 *align, 
+                       gboolean as_field, gboolean unicode)
+{
+       MonoMarshalNative native_type = mono_type_to_unmanaged (type, mspec, as_field, unicode, NULL);
+       MonoClass *klass;
+
+       switch (native_type) {
+       case MONO_NATIVE_BOOLEAN:
+               *align = 4;
+               return 4;
+       case MONO_NATIVE_I1:
+       case MONO_NATIVE_U1:
+               *align = 1;
+               return 1;
+       case MONO_NATIVE_I2:
+       case MONO_NATIVE_U2:
+               *align = 2;
+               return 2;
+       case MONO_NATIVE_I4:
+       case MONO_NATIVE_U4:
+       case MONO_NATIVE_ERROR:
+               *align = 4;
+               return 4;
+       case MONO_NATIVE_I8:
+       case MONO_NATIVE_U8:
+               *align = 8;
+               return 8;
+       case MONO_NATIVE_R4:
+               *align = 4;
+               return 4;
+       case MONO_NATIVE_R8:
+               *align = 8;
+               return 8;
+       case MONO_NATIVE_INT:
+       case MONO_NATIVE_UINT:
+       case MONO_NATIVE_LPSTR:
+       case MONO_NATIVE_LPWSTR:
+       case MONO_NATIVE_LPTSTR:
+       case MONO_NATIVE_BSTR:
+       case MONO_NATIVE_ANSIBSTR:
+       case MONO_NATIVE_TBSTR:
+       case MONO_NATIVE_LPARRAY:
+       case MONO_NATIVE_SAFEARRAY:
+       case MONO_NATIVE_IUNKNOWN:
+       case MONO_NATIVE_IDISPATCH:
+       case MONO_NATIVE_INTERFACE:
+       case MONO_NATIVE_ASANY:
+       case MONO_NATIVE_VARIANTBOOL:
+       case MONO_NATIVE_FUNC:
+       case MONO_NATIVE_LPSTRUCT:
+               *align =  __alignof__(gpointer);
+               return sizeof (gpointer);
+       case MONO_NATIVE_STRUCT: 
+               klass = mono_class_from_mono_type (type);
+               *align = mono_class_min_align (klass);
+               return mono_class_native_size (klass);
+       case MONO_NATIVE_BYVALTSTR: {
+               int esize = unicode ? 2: 1;
+               g_assert (mspec);
+               *align = esize;
+               return mspec->num_elem * esize;
+       }
+       case MONO_NATIVE_BYVALARRAY: {
+               int esize;
+               klass = mono_class_from_mono_type (type);
+               *align = mono_class_min_align (klass->element_class);
+               esize = mono_array_element_size (klass);
+               g_assert (mspec);
+               return mspec->num_elem * esize;
+       }
+       case MONO_NATIVE_CURRENCY:
+       case MONO_NATIVE_VBBYREFSTR:
+       case MONO_NATIVE_CUSTOM:
+       default:
+               g_error ("native type %02x not implemented", native_type); 
+               break;
+       }
+       g_assert_not_reached ();
+       return 0;
+}
+
+const char*
+mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
+{
+       locator_t loc;
+       MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_FIELDMARSHAL];
+
+       if (!tdef->base)
+               return NULL;
+
+       loc.t = tdef;
+       loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
+       loc.idx = ((idx + 1) << HAS_FIELD_MARSHAL_BITS) | (is_field? HAS_FIELD_MARSHAL_FIELDSREF: HAS_FIELD_MARSHAL_PARAMDEF);
+
+       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+               return NULL;
+
+       return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
+}
+