ios: update chat_read_file (#3037)

This commit is contained in:
Evgeny Poberezkin 2023-09-07 22:43:51 +01:00 committed by GitHub
parent e76440ee66
commit 113a57c7c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 15 deletions

View File

@ -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 { public func readCryptoFile(path: String, cryptoArgs: CryptoFileArgs) throws -> Data {
var cPath = path.cString(using: .utf8)! var cPath = path.cString(using: .utf8)!
var cKey = cryptoArgs.fileKey.cString(using: .utf8)! var cKey = cryptoArgs.fileKey.cString(using: .utf8)!
var cNonce = cryptoArgs.fileNonce.cString(using: .utf8)! var cNonce = cryptoArgs.fileNonce.cString(using: .utf8)!
let r = chat_read_file(&cPath, &cKey, &cNonce)! let ptr = chat_read_file(&cPath, &cKey, &cNonce)!
let d = String.init(cString: r).data(using: .utf8)! let status = UInt8(ptr.pointee)
switch try jsonDecoder.decode(ReadFileResult.self, from: d) { switch status {
case let .error(err): throw RuntimeError(err) case 0: // ok
case let .result(size): return Data(bytes: r.advanced(by: d.count + 1), count: size) 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)")
} }
} }

View File

@ -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_encrypt_media(char *key, char *frame, int len);
extern char *chat_decrypt_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); extern char *chat_write_file(char *path, char *data, int len);
// chat_read_file returns a buffer with: // chat_read_file returns a buffer with:
// 1. NUL-terminated C string with JSON of ReadFileResult, followed by // result status (1 byte), then if
// 2. file data, the length is defined in ReadFileResult // 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); 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); 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); extern char *chat_decrypt_file(char *fromPath, char *key, char *nonce, char *toPath);