oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2
3public enum ShareText {
4 /// Item providers can materialize plain text as BOM-tagged UTF-16.
5 /// Read at most twice the UTF-8 limit, then enforce the actual message bound.
6 public static func decode(_ data: Data) throws -> String {
7 guard data.count <= 32 * 1024 + 2 else { throw AttachmentFailure.tooLarge }
8 if data.starts(with: Data("bplist".utf8)) {
9 // Foundation item providers materialize NSString using a secure
10 // archive. Permit that one class, never arbitrary provider objects.
11 guard let text = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSString.self, from: data) as String? else { throw AttachmentFailure.type }
12 guard text.utf8.count <= 16 * 1024 else { throw AttachmentFailure.tooLarge }
13 return text
14 }
15 let encoding: String.Encoding = data.starts(with: [0xff, 0xfe]) || data.starts(with: [0xfe, 0xff]) ? .utf16 : .utf8
16 guard let text = String(data: data, encoding: encoding) else { throw AttachmentFailure.type }
17 guard text.utf8.count <= 16 * 1024 else { throw AttachmentFailure.tooLarge }
18 return text
19 }
20}
21