private static string LoadCFStringSymbol(IntPtr lib, string name)
 {
     using (SafeCFStringHandle cfString = new SafeCFStringHandle(LoadSymbol(lib, name), false))
     {
         Debug.Assert(!cfString.IsInvalid);
         return(Interop.CoreFoundation.CFStringToString(cfString));
     }
 }
        internal static string GetSecErrorString(int osStatus)
        {
            using (SafeCFStringHandle cfString = AppleCryptoNative_SecCopyErrorMessageString(osStatus))
            {
                if (cfString.IsInvalid)
                {
                    return(null);
                }

                return(CoreFoundation.CFStringToString(cfString));
            }
        }
            private string?GetString(IntPtr key)
            {
                IntPtr dictValue = CFDictionaryGetValue(_dictionary, key);

                if (dictValue != IntPtr.Zero)
                {
                    using (SafeCFStringHandle handle = new SafeCFStringHandle(dictValue, false))
                    {
                        return(CFStringToString(handle));
                    }
                }
                return(null);
            }
        internal static string CFStringToString(SafeCFStringHandle cfString)
        {
            Debug.Assert(cfString != null);
            Debug.Assert(!cfString.IsInvalid);
            Debug.Assert(!cfString.IsClosed);

            // If the string is already stored internally as UTF-8 we can (usually)
            // get the raw pointer to the data blob, then we can Marshal in the string
            // via pointer semantics, avoiding a copy.
            IntPtr interiorPointer = CFStringGetCStringPtr(
                cfString,
                CFStringBuiltInEncodings.kCFStringEncodingUTF8);

            if (interiorPointer != IntPtr.Zero)
            {
                return(Marshal.PtrToStringUTF8(interiorPointer) !);
            }

            SafeCFDataHandle cfData = CFStringCreateExternalRepresentation(
                IntPtr.Zero,
                cfString,
                CFStringBuiltInEncodings.kCFStringEncodingUTF8,
                0);

            using (cfData)
            {
                bool addedRef = false;

                try
                {
                    cfData.DangerousAddRef(ref addedRef);

                    unsafe
                    {
                        // Note that CFDataGetLength(cfData).ToInt32() will throw on
                        // too large of an input. Since a >2GB string is pretty unlikely,
                        // that's considered a good thing here.
                        return(Encoding.UTF8.GetString(
                                   CFDataGetBytePtr(cfData),
                                   CFDataGetLength(cfData).ToInt32()));
                    }
                }
                finally
                {
                    if (addedRef)
                    {
                        cfData.DangerousRelease();
                    }
                }
            }
        }
        internal static string CFStringToString(SafeCFStringHandle cfString)
        {
            Debug.Assert(cfString != null);
            Debug.Assert(!cfString.IsInvalid);
            Debug.Assert(!cfString.IsClosed);

#if HAVE_PTRTOSTRINGUTF8
            IntPtr interiorPointer = CFStringGetCStringPtr(
                cfString,
                CFStringBuiltInEncodings.kCFStringEncodingUTF8);

            if (interiorPointer != IntPtr.Zero)
            {
                return(Marshal.PtrToStringUTF8(interiorPointer));
            }
#endif

            SafeCFDataHandle cfData = CFStringCreateExternalRepresentation(
                IntPtr.Zero,
                cfString,
                CFStringBuiltInEncodings.kCFStringEncodingUTF8,
                0);

            using (cfData)
            {
                bool addedRef = false;

                try
                {
                    cfData.DangerousAddRef(ref addedRef);

                    unsafe
                    {
                        // Note that CFDataGetLength(cfData).ToInt32() will throw on
                        // too large of an input. Since a >2GB string is pretty unlikely,
                        // that's considered a good thing here.
                        return(Encoding.UTF8.GetString(
                                   CFDataGetBytePtr(cfData),
                                   CFDataGetLength(cfData).ToInt32()));
                    }
                }
                finally
                {
                    if (addedRef)
                    {
                        cfData.DangerousRelease();
                    }
                }
            }
        }
Example #6
0
        internal static string?GetErrorDescription(SafeCFErrorHandle cfError)
        {
            Debug.Assert(cfError != null);

            if (cfError.IsInvalid)
            {
                return(null);
            }

            Debug.Assert(!cfError.IsClosed);

            using (SafeCFStringHandle cfString = CFErrorCopyDescription(cfError))
            {
                return(CFStringToString(cfString));
            }
        }
 private static partial SafeCFDataHandle CFStringCreateExternalRepresentation(
     IntPtr alloc,
     SafeCFStringHandle theString,
     CFStringBuiltInEncodings encoding,
     byte lossByte);
 private static partial IntPtr CFStringGetCStringPtr(
     SafeCFStringHandle cfString,
     CFStringBuiltInEncodings encoding);
Example #9
0
 private static partial int AppleCryptoNative_X509GetSubjectSummary(
     SafeSecCertificateHandle cert,
     out SafeCFStringHandle cfSubjectSummaryOut);