perf: decode Zoko configuration in place

This commit is contained in:
2026-10-25 15:59:24 +01:00
parent 6463d47ed8
commit e6a2759497
2 changed files with 11 additions and 6 deletions
+8 -5
View File
@@ -16,6 +16,7 @@ enum ZokoError: String, LocalizedError {
enum ZokoResolver {
static let bodyLimit = 2 * 1024 * 1024
static let referer = "Referer: https://zokoanime.video/\r\n"
private static let codecKey = Array("otaku-embed-v1".utf8)
static func validResource(_ url: URL, playlist: Bool = false) -> Bool {
guard ResolvedSource.validURL(url.absoluteString), url.scheme == "https",
@@ -47,6 +48,7 @@ enum ZokoResolver {
if response.statusCode == 403 || response.statusCode == 429 || response.statusCode == 503 { throw ZokoError.protection }
guard response.statusCode == 200 else { throw ZokoError.transport }
var data = Data()
if response.expectedContentLength > 0 { data.reserveCapacity(Int(response.expectedContentLength)) }
for try await byte in bytes {
try Task.checkCancellation()
guard data.count < bodyLimit else { throw ZokoError.malformed }
@@ -88,17 +90,18 @@ enum ZokoResolver {
lower.contains("captcha") || lower.contains("sorry, you have been blocked") { throw ZokoError.protection }
throw ZokoError.malformed
}
guard let encoded = Data(base64Encoded: String(page[range])), !encoded.isEmpty else { throw ZokoError.malformed }
let key = Array("otaku-embed-v1".utf8)
var decoded = Array(encoded)
for i in decoded.indices { decoded[i] ^= key[i % key.count] }
guard var decoded = Data(base64Encoded: String(page[range])), !decoded.isEmpty else { throw ZokoError.malformed }
decoded.withUnsafeMutableBytes { (buffer: UnsafeMutableRawBufferPointer) in
let bytes = buffer.bindMemory(to: UInt8.self)
for i in bytes.indices { bytes[i] ^= codecKey[i % codecKey.count] }
}
struct Config: Decodable {
struct Subtitle: Decodable { let src: String; let lang: String; let label: String; let `default`: Bool? }
let src: String
let subtitles: [Subtitle]
}
let config: Config
do { config = try JSONDecoder().decode(Config.self, from: Data(decoded)) }
do { config = try JSONDecoder().decode(Config.self, from: decoded) }
catch { throw ZokoError.malformed }
guard let url = URL(string: config.src), validResource(url, playlist: true), url.path.hasSuffix(".m3u8"), config.subtitles.count <= 32 else { throw ZokoError.malformed }
let subtitles = try config.subtitles.map { row -> ResolvedSubtitle in