SPB Git

Repository Public License

ByteSource.swift 6.7 KB 170 lines swift

1// ORIX Convert — Packages/ORIXConvertCore/Sources/ConvertCore/Detection/ByteSource.swift2// Description: Bounded, bounds-checked random access to file bytes for the detector, plus small decoding helpers3//              (integers, ASCII, bounded inflate). Nothing here trusts a length declared by the file.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Compression6import Foundation78/// Random access to the bytes being inspected. Reads never exceed the real size and are capped per call.9protocol ByteSource {10    var size: Int64 { get }11    /// Returns at most `count` bytes at `offset` (fewer at the end of the data, none when out of range).12    func read(at offset: Int64, count: Int) -> [UInt8]13}1415/// Largest single read the detector performs.16let byteSourceReadCap = 4 << 201718final class FileByteSource: ByteSource {19    private let fd: Int3220    let size: Int642122    /// Opens a regular file (symlinks are followed). Returns `nil` for directories, devices, FIFOs and unreadable files.23    init?(url: URL) {24        var st = stat()25        guard stat(url.path, &st) == 0, (st.st_mode & S_IFMT) == S_IFREG else { return nil }26        let descriptor = open(url.path, O_RDONLY | O_NONBLOCK)27        guard descriptor >= 0 else { return nil }28        fd = descriptor29        size = Int64(st.st_size)30    }3132    deinit { close(fd) }3334    func read(at offset: Int64, count: Int) -> [UInt8] {35        guard offset >= 0, offset < size, count > 0 else { return [] }36        let n = Int(min(Int64(min(count, byteSourceReadCap)), size - offset))37        var buffer = [UInt8](repeating: 0, count: n)38        let got = buffer.withUnsafeMutableBytes { pread(fd, $0.baseAddress, n, offset) }39        if got <= 0 { return [] }40        if got < n { buffer.removeLast(n - got) }41        return buffer42    }43}4445struct DataByteSource: ByteSource {46    let bytes: [UInt8]47    var size: Int64 { Int64(bytes.count) }48    func read(at offset: Int64, count: Int) -> [UInt8] {49        guard offset >= 0, offset < size, count > 0 else { return [] }50        let start = Int(offset)51        let end = start + min(min(count, byteSourceReadCap), bytes.count - start)52        return Array(bytes[start..<end])53    }54}5556extension Array where Element == UInt8 {57    func has(_ signature: [UInt8], at offset: Int = 0) -> Bool {58        guard offset >= 0, signature.count <= count - offset else { return false }59        for (i, b) in signature.enumerated() where self[offset + i] != b { return false }60        return true61    }6263    func hasASCII(_ text: String, at offset: Int = 0) -> Bool { has(Array(text.utf8), at: offset) }6465    func u8(_ o: Int) -> Int? { o >= 0 && o < count ? Int(self[o]) : nil }6667    func u16le(_ o: Int) -> Int? {68        guard o >= 0, o + 2 <= count else { return nil }69        return Int(self[o]) | Int(self[o + 1]) << 870    }7172    func u16be(_ o: Int) -> Int? {73        guard o >= 0, o + 2 <= count else { return nil }74        return Int(self[o]) << 8 | Int(self[o + 1])75    }7677    func u32le(_ o: Int) -> Int? {78        guard o >= 0, o + 4 <= count else { return nil }79        return Int(self[o]) | Int(self[o + 1]) << 8 | Int(self[o + 2]) << 16 | Int(self[o + 3]) << 2480    }8182    func u32be(_ o: Int) -> Int? {83        guard o >= 0, o + 4 <= count else { return nil }84        return Int(self[o]) << 24 | Int(self[o + 1]) << 16 | Int(self[o + 2]) << 8 | Int(self[o + 3])85    }8687    func u64le(_ o: Int) -> UInt64? {88        guard o >= 0, o + 8 <= count else { return nil }89        var v: UInt64 = 090        for i in 0..<8 { v |= UInt64(self[o + i]) << UInt64(8 * i) }91        return v92    }9394    func u64be(_ o: Int) -> UInt64? {95        guard o >= 0, o + 8 <= count else { return nil }96        var v: UInt64 = 097        for i in 0..<8 { v = v << 8 | UInt64(self[o + i]) }98        return v99    }100101    /// Latin-1 view of a byte range (clamped), for tags and names.102    func latin1(_ o: Int, _ n: Int) -> String {103        guard o >= 0, n > 0, o < count else { return "" }104        return String(self[o..<Swift.min(count, o + n)].map { Character(UnicodeScalar($0)) })105    }106107    /// First position of `needle` in `from..<limit`.108    func find(_ needle: [UInt8], from: Int = 0, limit: Int? = nil) -> Int? {109        let end = Swift.min(limit ?? count, count)110        guard !needle.isEmpty, from >= 0, end - from >= needle.count else { return nil }111        let first = needle[0]112        var i = from113        let last = end - needle.count114        while i <= last {115            if self[i] == first, has(needle, at: i) { return i }116            i += 1117        }118        return nil119    }120}121122enum BoundedDecoder {123    /// Decodes as much of `input` as possible into at most `maxOutput` bytes. Truncated or corrupt input yields the124    /// bytes produced before the problem (possibly none). `COMPRESSION_ZLIB` is raw DEFLATE; `COMPRESSION_LZMA`125    /// reads the XZ container.126    static func decode(_ input: [UInt8], algorithm: compression_algorithm, maxOutput: Int) -> [UInt8] {127        guard !input.isEmpty, maxOutput > 0 else { return [] }128        let stream = UnsafeMutablePointer<compression_stream>.allocate(capacity: 1)129        defer { stream.deallocate() }130        guard compression_stream_init(stream, COMPRESSION_STREAM_DECODE, algorithm) == COMPRESSION_STATUS_OK else { return [] }131        defer { compression_stream_destroy(stream) }132        var output = [UInt8](repeating: 0, count: maxOutput)133        var produced = 0134        input.withUnsafeBufferPointer { src in135            output.withUnsafeMutableBufferPointer { dst in136                guard let s = src.baseAddress, let d = dst.baseAddress else { return }137                stream.pointee.src_ptr = s138                stream.pointee.src_size = src.count139                stream.pointee.dst_ptr = d140                stream.pointee.dst_size = maxOutput141                _ = compression_stream_process(stream, 0)142                produced = maxOutput - stream.pointee.dst_size143            }144        }145        guard produced > 0, produced <= maxOutput else { return [] }146        return Array(output[0..<produced])147    }148149    /// Offset of the DEFLATE stream inside a gzip member, or `nil` when the header is not valid/complete.150    static func gzipPayloadOffset(_ p: [UInt8]) -> Int? {151        guard p.count >= 10, p[0] == 0x1F, p[1] == 0x8B, p[2] == 8 else { return nil }152        let flags = p[3]153        var o = 10154        if flags & 0x04 != 0 {155            guard let xlen = p.u16le(o) else { return nil }156            o += 2 + xlen157        }158        if flags & 0x08 != 0 {159            guard o < p.count, let end = p[o...].firstIndex(of: 0) else { return nil }160            o = end + 1161        }162        if flags & 0x10 != 0 {163            guard o < p.count, let end = p[o...].firstIndex(of: 0) else { return nil }164            o = end + 1165        }166        if flags & 0x02 != 0 { o += 2 }167        return o < p.count ? o : nil168    }169}170
Files main
Menu

Theme