2 using System
.Runtime
.InteropServices
;
4 namespace XamMac
.CoreFoundation
6 internal static class CFHelpers
8 internal const string CoreFoundationLibrary
= "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
9 internal const string SecurityLibrary
= "/System/Library/Frameworks/Security.framework/Security";
11 [DllImport (CoreFoundationLibrary
)]
12 internal extern static void CFRelease (IntPtr obj
);
14 [DllImport (CoreFoundationLibrary
)]
15 internal extern static IntPtr
CFRetain (IntPtr obj
);
17 [StructLayout (LayoutKind
.Sequential
)]
22 public CFRange (int loc
, int len
)
23 : this ((long) loc
, (long) len
)
27 public CFRange (long l
, long len
)
29 this.loc
= (IntPtr
) l
;
30 this.len
= (IntPtr
) len
;
34 [DllImport (CoreFoundationLibrary
, CharSet
=CharSet
.Unicode
)]
35 extern static IntPtr
CFStringCreateWithCharacters (IntPtr allocator
, string str
, IntPtr count
);
37 [DllImport (CoreFoundationLibrary
, CharSet
=CharSet
.Unicode
)]
38 extern static IntPtr
CFStringGetLength (IntPtr handle
);
40 [DllImport (CoreFoundationLibrary
, CharSet
=CharSet
.Unicode
)]
41 extern static IntPtr
CFStringGetCharactersPtr (IntPtr handle
);
43 [DllImport (CoreFoundationLibrary
, CharSet
=CharSet
.Unicode
)]
44 extern static IntPtr
CFStringGetCharacters (IntPtr handle
, CFRange range
, IntPtr buffer
);
46 internal static string FetchString (IntPtr handle
)
48 if (handle
== IntPtr
.Zero
)
53 int l
= (int)CFStringGetLength (handle
);
54 IntPtr u
= CFStringGetCharactersPtr (handle
);
55 IntPtr buffer
= IntPtr
.Zero
;
56 if (u
== IntPtr
.Zero
){
57 CFRange r
= new CFRange (0, l
);
58 buffer
= Marshal
.AllocCoTaskMem (l
* 2);
59 CFStringGetCharacters (handle
, r
, buffer
);
63 str
= new string ((char *) u
, 0, l
);
66 if (buffer
!= IntPtr
.Zero
)
67 Marshal
.FreeCoTaskMem (buffer
);
72 [DllImport (CoreFoundationLibrary
)]
73 extern static IntPtr
CFDataGetLength (IntPtr handle
);
75 [DllImport (CoreFoundationLibrary
)]
76 extern static IntPtr
CFDataGetBytePtr (IntPtr handle
);
78 internal static byte[] FetchDataBuffer (IntPtr handle
)
80 var length
= (int)CFDataGetLength (handle
);
81 var buffer
= new byte [length
];
82 var ptr
= CFDataGetBytePtr (handle
);
83 Marshal
.Copy (ptr
, buffer
, 0, buffer
.Length
);
87 [DllImport (CoreFoundationLibrary
)]
88 extern static IntPtr
CFDataCreateWithBytesNoCopy (IntPtr allocator
, IntPtr bytes
, IntPtr length
, IntPtr bytesDeallocator
);
90 [DllImport (CoreFoundationLibrary
)]
91 extern static IntPtr
CFDataCreate (IntPtr allocator
, IntPtr bytes
, IntPtr length
);
93 [DllImport (SecurityLibrary
)]
94 extern static IntPtr
SecCertificateCreateWithData (IntPtr allocator
, IntPtr cfData
);
96 unsafe internal static IntPtr
CreateCertificateFromData (byte[] data
)
98 fixed (void *ptr
= data
) {
99 var cfdata
= CFDataCreate (IntPtr
.Zero
, (IntPtr
)ptr
, new IntPtr (data
.Length
));
100 if (cfdata
== IntPtr
.Zero
)
103 var certificate
= SecCertificateCreateWithData (IntPtr
.Zero
, cfdata
);
104 if (cfdata
!= IntPtr
.Zero
)