public static IntPtr ATHostConnectionCreateWithLibrary(string strPrefsValue, string strUUID) { IntPtr hPrefsValue = CoreFoundation.StringToCFString(strPrefsValue); IntPtr hUUID = CoreFoundation.StringToCFString(strUUID); IntPtr ptr3 = ATHostConnectionCreateWithLibrary_IntPtr(hPrefsValue, hUUID, 0); CoreFoundation.CFRelease(hUUID); CoreFoundation.CFRelease(hPrefsValue); return(ptr3); }
public static int ATHostConnectionSendFileError(IntPtr hATHost, string strPid, string strMediaType, int intType) { IntPtr hPid = CoreFoundation.CFStringMakeConstantString(strPid); IntPtr hMediaType = CoreFoundation.CFStringMakeConstantString(strMediaType); IntPtr ptr3 = (IntPtr)ATHostConnectionSendFileError_IntPtr(hATHost, hPid, hMediaType, intType); CoreFoundation.CFRelease(hPid); CoreFoundation.CFRelease(hMediaType); return(ptr3.ToInt32()); }
public static IntPtr ATCFMessageCreate(int sesssion, string strMessageType, Dictionary <object, object> dictParams) { IntPtr hMessageType = CoreFoundation.StringToCFString(strMessageType); IntPtr hParams = CoreFoundation.CFTypeFromManagedType(dictParams); IntPtr ptr3 = ATCFMessageCreate_IntPtr(sesssion, hMessageType, hParams); CoreFoundation.CFRelease(hMessageType); CoreFoundation.CFRelease(hParams); return(ptr3); }
public static int ATHostConnectionSendAssetCompleted(IntPtr hATHost, string strPid, string strMediaType, string strFilePath) { IntPtr hPid = CoreFoundation.CFStringMakeConstantString(strPid); IntPtr hMediaType = CoreFoundation.CFStringMakeConstantString(strMediaType); IntPtr hFilePath = CoreFoundation.CFStringMakeConstantString(strFilePath); IntPtr ptr4 = ATHostConnectionSendAssetCompleted_IntPtr(hATHost, hPid, hMediaType, hFilePath); CoreFoundation.CFRelease(hPid); CoreFoundation.CFRelease(hMediaType); CoreFoundation.CFRelease(hFilePath); return(ptr4.ToInt32()); }
public void Dispose() { if (PasswordData != IntPtr.Zero) { Security.SecKeychainItemFreeContent(IntPtr.Zero, PasswordData); } if (ItemRef != IntPtr.Zero) { CoreFoundation.CFRelease(ItemRef); } }
public void AddOrUpdate(string key, ICredential credential) { byte[] passwordBytes = Encoding.UTF8.GetBytes(credential.Password); IntPtr passwordData = IntPtr.Zero; IntPtr itemRef = IntPtr.Zero; try { // Check if an entry already exists in the keychain int findResult = SecKeychainFindGenericPassword( IntPtr.Zero, (uint)key.Length, key, (uint)credential.UserName.Length, credential.UserName, out uint _, out passwordData, out itemRef); switch (findResult) { // Create new entry case OK: ThrowIfError( SecKeychainItemModifyAttributesAndData(itemRef, IntPtr.Zero, (uint)passwordBytes.Length, passwordBytes), "Could not update existing item" ); break; // Update existing entry case ErrorSecItemNotFound: ThrowIfError( SecKeychainAddGenericPassword(IntPtr.Zero, (uint)key.Length, key, (uint)credential.UserName.Length, credential.UserName, (uint)passwordBytes.Length, passwordBytes, out itemRef), "Could not create new item" ); break; default: ThrowIfError(findResult); break; } } finally { if (passwordData != IntPtr.Zero) { SecKeychainItemFreeContent(IntPtr.Zero, passwordData); } if (itemRef != IntPtr.Zero) { CoreFoundation.CFRelease(itemRef); } } }
public static int ATHostConnectionSendFileProgress(IntPtr hATHost, string strPid, string strMediaType, double fileProgress, double totalProgress) { IntPtr hPid = CoreFoundation.CFStringMakeConstantString(strPid); IntPtr hMediaType = CoreFoundation.CFStringMakeConstantString(strMediaType); byte[] bytes = BitConverter.GetBytes(fileProgress); byte[] buffer2 = BitConverter.GetBytes(totalProgress); int num = BitConverter.ToInt32(bytes, 0); int num2 = BitConverter.ToInt32(bytes, 4); int num3 = BitConverter.ToInt32(buffer2, 0); int num4 = BitConverter.ToInt32(buffer2, 4); IntPtr ptr3 = (IntPtr)ATHostConnectionSendFileProgress_IntPtr(hATHost, hPid, hMediaType, num, num2, num3, num4); CoreFoundation.CFRelease(hPid); CoreFoundation.CFRelease(hMediaType); return(ptr3.ToInt32()); }
public ICredential Get(string key) { IntPtr passwordData = IntPtr.Zero; IntPtr itemRef = IntPtr.Zero; try { // Find the item (itemRef) and password (passwordData) in the keychain int findResult = SecKeychainFindGenericPassword( IntPtr.Zero, (uint)key.Length, key, 0, null, out uint passwordLength, out passwordData, out itemRef); switch (findResult) { case OK: // Get and decode the user name from the 'account name' attribute byte[] userNameBytes = GetAccountNameAttributeData(itemRef); string userName = Encoding.UTF8.GetString(userNameBytes); // Decode the password from the raw data byte[] passwordBytes = InteropUtils.ToByteArray(passwordData, passwordLength); string password = Encoding.UTF8.GetString(passwordBytes); return(new GitCredential(userName, password)); case ErrorSecItemNotFound: return(null); default: ThrowIfError(findResult); return(null); } } finally { if (passwordData != IntPtr.Zero) { SecKeychainItemFreeContent(IntPtr.Zero, passwordData); } if (itemRef != IntPtr.Zero) { CoreFoundation.CFRelease(itemRef); } } }
/// <summary> /// 发送消息通过Socket,大部分用途为发送plist文件(指令)给设备 /// </summary> /// <param name="sock"></param> /// <param name="message"></param> /// <returns></returns> public bool SendMessageToSocket(int sock, IntPtr message) { if ((sock < 1) || (message == IntPtr.Zero)) { return(false); } var flag = false; var stream = CoreFoundation.CFWriteStreamCreateWithAllocatedBuffers(IntPtr.Zero, IntPtr.Zero); if (stream != IntPtr.Zero) { if (!CoreFoundation.CFWriteStreamOpen(stream)) { return(false); } var zero = IntPtr.Zero; if (CoreFoundation.CFPropertyListWriteToStream(message, stream, CFPropertyListFormat.kCFPropertyListBinaryFormat_v1_0, ref zero) > 0) { var propertyName = CoreFoundation.kCFStreamPropertyDataWritten; var srcRef = CoreFoundation.CFWriteStreamCopyProperty(stream, propertyName); var buffer = CoreFoundation.CFDataGetBytePtr(srcRef); var bufferlen = CoreFoundation.CFDataGetLength(srcRef); var structure = MobileDevice.htonl((uint)bufferlen); var num3 = Marshal.SizeOf(structure); if (MobileDevice.send_UInt32(sock, ref structure, num3, 0) != num3) { Console.WriteLine("could not send message size"); } else if (MobileDevice.send(sock, buffer, bufferlen, 0) != bufferlen) { Console.WriteLine("Could not send message."); } else { flag = true; } CoreFoundation.CFRelease(srcRef); } CoreFoundation.CFWriteStreamClose(stream); } return(flag); }
public bool Remove(string key) { IntPtr passwordData = IntPtr.Zero; IntPtr itemRef = IntPtr.Zero; try { int findResult = SecKeychainFindGenericPassword( IntPtr.Zero, (uint)key.Length, key, 0, null, out _, out passwordData, out itemRef); switch (findResult) { case OK: ThrowIfError( SecKeychainItemDelete(itemRef) ); return(true); case ErrorSecItemNotFound: return(false); default: ThrowIfError(findResult); return(false); } } finally { if (passwordData != IntPtr.Zero) { SecKeychainItemFreeContent(IntPtr.Zero, passwordData); } if (itemRef != IntPtr.Zero) { CoreFoundation.CFRelease(itemRef); } } }
public static object AMDeviceCopyValue(IntPtr device, string domain, string name) { object obj2 = string.Empty; IntPtr ptr = CoreFoundation.StringToCFString(domain); IntPtr cfstring = CoreFoundation.StringToCFString(name); IntPtr srcRef = AMDeviceCopyValue_Int(device, ptr, cfstring); if (ptr != IntPtr.Zero) { CoreFoundation.CFRelease(ptr); } if (cfstring != IntPtr.Zero) { CoreFoundation.CFRelease(cfstring); } if (srcRef != IntPtr.Zero) { obj2 = RuntimeHelpers.GetObjectValue(CoreFoundation.ManagedTypeFromCFType(ref srcRef)); CoreFoundation.CFRelease(srcRef); } return(obj2); }
/// <summary> /// 接收Socket消息,主要为接收设备返回的指令结果,大部分为Plist,所以函数内将会自行作转换 /// </summary> /// <param name="sock"></param> /// <returns></returns> public object ReceiveMessageFromSocket(int sock) { if (sock < 0) { return(null); } int recvCount; uint buffer = 0; uint reviceSize = 0; uint dataSize = 0; IntPtr zero = IntPtr.Zero; if (DeviceSecureIOContext != IntPtr.Zero && SocketContext != IntPtr.Zero) { if (MobileDevice.AMDServiceConnectionReceive(SocketContext, ref buffer, 4) == 4) { dataSize = MobileDevice.ntohl(buffer); //获取数据总长度 if (dataSize <= 0) { Console.WriteLine("receive size error, dataSize:" + dataSize); return(null); } zero = Marshal.AllocCoTaskMem((int)dataSize); if (zero == IntPtr.Zero) { Console.WriteLine("Could not allocate message buffer."); return(null); } var tempPtr = zero; while (reviceSize < dataSize) { recvCount = MobileDevice.AMDServiceConnectionReceive_1(SocketContext, tempPtr, (int)(dataSize - reviceSize)); if (recvCount <= -1) { Console.WriteLine("Could not receive secure message: " + recvCount); reviceSize = dataSize + 1; } else if (recvCount == 0) { Console.WriteLine("receive size is zero. "); break; } tempPtr = new IntPtr(tempPtr.ToInt64() + recvCount); reviceSize += (uint)recvCount; } } } else if (MobileDevice.recv_UInt(sock, ref buffer, 4, 0) == 4) { dataSize = MobileDevice.ntohl(buffer); //获取数据总长度 if (dataSize <= 0) { Console.WriteLine("receive size error, dataSize:" + dataSize); return(null); } zero = Marshal.AllocCoTaskMem((int)dataSize); if (zero == IntPtr.Zero) { Console.WriteLine("Could not allocate message buffer."); return(null); } var tempPtr = zero; while (reviceSize < dataSize) { recvCount = MobileDevice.recv(sock, tempPtr, (int)(dataSize - reviceSize), 0); if (recvCount <= -1) { Console.WriteLine("Could not receive secure message: " + recvCount); reviceSize = dataSize + 1; } else if (recvCount == 0) { Console.WriteLine("receive size is zero. "); break; } tempPtr = new IntPtr(tempPtr.ToInt64() + recvCount); reviceSize += (uint)recvCount; } } var datas = IntPtr.Zero; var srcRef = IntPtr.Zero; if (reviceSize == dataSize) { datas = CoreFoundation.CFDataCreate(CoreFoundation.kCFAllocatorDefault, zero, (int)dataSize); if (datas == IntPtr.Zero) { Console.WriteLine("Could not create CFData for message"); } else { var errorString = IntPtr.Zero; srcRef = CoreFoundation.CFPropertyListCreateFromXMLData(CoreFoundation.kCFAllocatorDefault, datas, CFPropertyListMutabilityOptions.kCFPropertyListImmutable, ref errorString); if (srcRef == IntPtr.Zero) { Console.WriteLine("Could not convert raw xml into a dictionary: " + Convert.ToString(CoreFoundation.ManagedTypeFromCFType(ref errorString))); return(null); } } } if (datas != IntPtr.Zero) { try { CoreFoundation.CFRelease(datas); } catch { } } if (zero != IntPtr.Zero) { Marshal.FreeCoTaskMem(zero); } var result = CoreFoundation.ManagedTypeFromCFType(ref srcRef); if (srcRef != IntPtr.Zero) { CoreFoundation.CFRelease(srcRef); } return(result); }
/// <summary> /// 发送消息通过Socket,大部分用途为发送plist文件(指令)给设备 /// </summary> /// <param name="sock"></param> /// <param name="message"></param> /// <returns></returns> public bool SendMessageToSocket(int sock, IntPtr message) { if (sock < 1 || message == IntPtr.Zero) { return(false); } var flag = false; var stream = CoreFoundation.CFWriteStreamCreateWithAllocatedBuffers(IntPtr.Zero, IntPtr.Zero); if (stream != IntPtr.Zero) { if (!CoreFoundation.CFWriteStreamOpen(stream)) { return(false); } var zero = IntPtr.Zero; if (CoreFoundation.CFPropertyListWriteToStream(message, stream, CFPropertyListFormat.kCFPropertyListBinaryFormat_v1_0, ref zero) > 0) { IntPtr srcRef = CoreFoundation.CFWriteStreamCopyProperty(stream, CoreFoundation.kCFStreamPropertyDataWritten); IntPtr buffer = CoreFoundation.CFDataGetBytePtr(srcRef); var bufferlen = CoreFoundation.CFDataGetLength(srcRef); var structure = MobileDevice.htonl((uint)bufferlen); var structureSize = Marshal.SizeOf(structure); if (DeviceSecureIOContext == IntPtr.Zero || SocketContext == IntPtr.Zero) { if (MobileDevice.send_UInt32(sock, ref structure, structureSize, 0) != structureSize) { Console.WriteLine("could not send message size"); } else if (MobileDevice.send(sock, buffer, bufferlen, 0) != bufferlen) { Console.WriteLine("Could not send message."); } else { flag = true; } } else if (MobileDevice.AMDServiceConnectionSend_UInt32(SocketContext, ref structure, structureSize) != structureSize) { Console.WriteLine("could not send message size with socket"); } else if (MobileDevice.AMDServiceConnectionSend(SocketContext, buffer, bufferlen) != bufferlen) { Console.WriteLine("could not send message size"); } else { flag = true; } CoreFoundation.CFRelease(srcRef); } CoreFoundation.CFWriteStreamClose(stream); } return(flag); }