Merge pull request #3335 from lambdageek/dev/system-reflection-assembly
[mono.git] / mcs / class / corlib / Mono / RuntimeMarshal.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Runtime.CompilerServices;
4
5 namespace Mono {
6         internal static class RuntimeMarshal {
7                 internal static string PtrToUtf8String (IntPtr ptr)
8                 {
9                         unsafe {
10                                 return new String ((sbyte*)ptr);
11                         }
12                 }
13
14                 internal static SafeStringMarshal MarshalString (string str)
15                 {
16                         return new SafeStringMarshal (str);
17                 }
18
19                 static int DecodeBlobSize (IntPtr in_ptr, out IntPtr out_ptr)
20                 {
21                         uint size;
22                         unsafe {
23                                 byte *ptr = (byte*)in_ptr;
24         
25                                 if ((*ptr & 0x80) == 0) {
26                                         size = (uint)(ptr [0] & 0x7f);
27                                         ptr++;
28                                 } else if ((*ptr & 0x40) == 0){
29                                         size = (uint)(((ptr [0] & 0x3f) << 8) + ptr [1]);
30                                         ptr += 2;
31                                 } else {
32                                         size = (uint)(((ptr [0] & 0x1f) << 24) +
33                                                 (ptr [1] << 16) +
34                                                 (ptr [2] << 8) +
35                                                 ptr [3]);
36                                         ptr += 4;
37                                 }
38                                 out_ptr = (IntPtr)ptr;
39                         }
40
41                         return (int)size;
42                 }
43
44                 internal static byte[] DecodeBlobArray (IntPtr ptr)
45                 {
46                         IntPtr out_ptr;
47                         int size = DecodeBlobSize (ptr, out out_ptr);
48                         byte[] res = new byte [size];
49                         Marshal.Copy (out_ptr, res, 0, size);
50                         return res;
51                 }
52
53                 internal static int AsciHexDigitValue (int c)
54                 {
55                         if (c >= '0' && c <= '9')
56                                 return c - '0';
57                         if (c >= 'a' && c <= 'f')
58                                 return c - 'a' + 10;
59                         return c - 'A' + 10;
60                 }
61
62                 [MethodImpl (MethodImplOptions.InternalCall)]
63                 internal static extern void FreeAssemblyName (ref MonoAssemblyName name);
64         }
65 }