From 113a57c7c759d4e9674cdea2428c8a028622c397 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 7 Sep 2023 22:43:51 +0100 Subject: [PATCH] ios: update chat_read_file (#3037) --- apps/ios/SimpleXChat/CryptoFile.swift | 25 +++++++++++++++---------- apps/ios/SimpleXChat/SimpleX.h | 11 ++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/apps/ios/SimpleXChat/CryptoFile.swift b/apps/ios/SimpleXChat/CryptoFile.swift index d641464ee..dcb2be9ae 100644 --- a/apps/ios/SimpleXChat/CryptoFile.swift +++ b/apps/ios/SimpleXChat/CryptoFile.swift @@ -25,20 +25,25 @@ public func writeCryptoFile(path: String, data: Data) throws -> CryptoFileArgs { } } -enum ReadFileResult: Decodable { - case result(fileSize: Int) - case error(readError: String) -} - public func readCryptoFile(path: String, cryptoArgs: CryptoFileArgs) throws -> Data { var cPath = path.cString(using: .utf8)! var cKey = cryptoArgs.fileKey.cString(using: .utf8)! var cNonce = cryptoArgs.fileNonce.cString(using: .utf8)! - let r = chat_read_file(&cPath, &cKey, &cNonce)! - let d = String.init(cString: r).data(using: .utf8)! - switch try jsonDecoder.decode(ReadFileResult.self, from: d) { - case let .error(err): throw RuntimeError(err) - case let .result(size): return Data(bytes: r.advanced(by: d.count + 1), count: size) + let ptr = chat_read_file(&cPath, &cKey, &cNonce)! + let status = UInt8(ptr.pointee) + switch status { + case 0: // ok + let dLen = Data(bytes: ptr.advanced(by: 1), count: 4) + let len = dLen.withUnsafeBytes { $0.load(as: UInt32.self) } + let d = Data(bytes: ptr.advanced(by: 5), count: Int(len)) + free(ptr) + return d + case 1: // error + let err = String.init(cString: ptr) + free(ptr) + throw RuntimeError(err) + default: + throw RuntimeError("unexpected chat_read_file status: \(status)") } } diff --git a/apps/ios/SimpleXChat/SimpleX.h b/apps/ios/SimpleXChat/SimpleX.h index 55b44dee3..67c2fa728 100644 --- a/apps/ios/SimpleXChat/SimpleX.h +++ b/apps/ios/SimpleXChat/SimpleX.h @@ -26,16 +26,17 @@ extern char *chat_password_hash(char *pwd, char *salt); extern char *chat_encrypt_media(char *key, char *frame, int len); extern char *chat_decrypt_media(char *key, char *frame, int len); -// chat_write_file returns NUL-terminated string with JSON of WriteFileResult +// chat_write_file returns null-terminated string with JSON of WriteFileResult extern char *chat_write_file(char *path, char *data, int len); // chat_read_file returns a buffer with: -// 1. NUL-terminated C string with JSON of ReadFileResult, followed by -// 2. file data, the length is defined in ReadFileResult +// result status (1 byte), then if +// status == 0 (success): buffer length (uint32, 4 bytes), buffer of specified length. +// status == 1 (error): null-terminated error message string. extern char *chat_read_file(char *path, char *key, char *nonce); -// chat_encrypt_file returns NUL-terminated string with JSON of WriteFileResult +// chat_encrypt_file returns null-terminated string with JSON of WriteFileResult extern char *chat_encrypt_file(char *fromPath, char *toPath); -// chat_decrypt_file returns NUL-terminated string with the error message +// chat_decrypt_file returns null-terminated string with the error message extern char *chat_decrypt_file(char *fromPath, char *key, char *nonce, char *toPath);