|
| 1 | +// Copyright (c) 2021 Jeff Lebrun |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the Software), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +/// `ArArchiveReader` reads `ar` files. |
| 22 | +public struct ArArchiveReader { |
| 23 | + private var data: [UInt8] |
| 24 | + private var currentIndex: Int = 0 |
| 25 | + |
| 26 | + /// Used primarily when in a for loop: |
| 27 | + /// |
| 28 | + /// ```swift |
| 29 | + /// let bytes = Array<UInt8>(try Data(contentsOf: myURL)) |
| 30 | + /// let reader = try ArArchiveReader(archive: bytes) |
| 31 | + /// for index = in 0..<reader.count { |
| 32 | + /// let header = reader.headers[index] |
| 33 | + /// let data = reader[header] |
| 34 | + /// // Use data and header. |
| 35 | + /// } |
| 36 | + /// ``` |
| 37 | + /// |
| 38 | + |
| 39 | + /// The headers that describe the files in this archive. |
| 40 | + /// |
| 41 | + /// Use this to find a file in the archive, then use the provided subscript to get the bytes of the file. |
| 42 | + /// |
| 43 | + /// ```swift |
| 44 | + /// let bytes = Array<UInt8>(try Data(contentsOf: myURL)) |
| 45 | + /// let reader = try ArArchiveReader(archive: bytes) |
| 46 | + /// let bytes = reader[reader.headers[0]] |
| 47 | + /// // Use bytes... |
| 48 | + /// ``` |
| 49 | + /// |
| 50 | + public var headers: [Header] = [] |
| 51 | + |
| 52 | + /// The initializer reads all the `ar` headers in preparation for random access to the header's file contents later. |
| 53 | + public init(archive: [UInt8]) throws { |
| 54 | + if archive.isEmpty { |
| 55 | + throw ArArchiveError.emptyArchive |
| 56 | + } else if archive.count < 8 { |
| 57 | + // The global header is missing. |
| 58 | + throw ArArchiveError.invalidArchive |
| 59 | + } else if Array(archive[0...7]) != globalHeader.asciiArray { |
| 60 | + // The global header is invalid. |
| 61 | + throw ArArchiveError.invalidArchive |
| 62 | + } |
| 63 | + |
| 64 | + // Drop the global header from the byte array. |
| 65 | + self.data = Array(archive[8...]) |
| 66 | + |
| 67 | + var index = 0 |
| 68 | + |
| 69 | + // Read all the headers so we can provide random access to the data later. |
| 70 | + while index < (self.data.count - 1), (index + (headerSize - 1)) < self.data.count - 1 { |
| 71 | + var h = try self.parseHeader(bytes: Array(self.data[index...(index + headerSize - 1)])) |
| 72 | + |
| 73 | + index += headerSize + 1 |
| 74 | + h.contentLocation = index - 1 |
| 75 | + index += h.size |
| 76 | + |
| 77 | + self.headers.append(h) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// Retrieves the bytes of the item at `index`, where index is the index of the `header` stored in the `headers` property of the reader. |
| 82 | + /// |
| 83 | + /// Internally, the `Header` stored at `index` is used to find the file. |
| 84 | + public subscript(index: Int) -> [UInt8] { |
| 85 | + Array(self.data[self.headers[index].contentLocation..<self.headers[index].contentLocation + self.headers[index].size]) |
| 86 | + } |
| 87 | + |
| 88 | + /// Retrieves the bytes of the file described in `header`. |
| 89 | + /// |
| 90 | + /// - Parameter header: The `Header` that describes the file you wish to retrieves. |
| 91 | + /// |
| 92 | + /// `header` MUST be a `Header` contained in the `headers` property of this `ArArchiveReader` or else you will get a "index out of range" error. |
| 93 | + public subscript(header header: Header) -> [UInt8] { |
| 94 | + Array(self.data[header.contentLocation..<header.contentLocation + header.size]) |
| 95 | + } |
| 96 | + |
| 97 | + private func parseHeader(bytes: [UInt8]) throws -> Header { |
| 98 | + var start = 0 |
| 99 | + let name = self.readString(from: Array(bytes[start...15])) |
| 100 | + |
| 101 | + start = 16 |
| 102 | + |
| 103 | + let modificationTime = self.readInt(from: Array(bytes[start...(start + 11)])) |
| 104 | + |
| 105 | + start += 12 |
| 106 | + let userID = self.readInt(from: Array(bytes[start...(start + 5)])) |
| 107 | + |
| 108 | + start += 6 |
| 109 | + |
| 110 | + let groupID = self.readInt(from: Array(bytes[start...(start + 5)])) |
| 111 | + |
| 112 | + start += 6 |
| 113 | + |
| 114 | + let mode = UInt32(String(readString(from: Array(bytes[start...(start + 5)])).dropFirst(3)), radix: 8) |
| 115 | + |
| 116 | + start += 8 |
| 117 | + |
| 118 | + let size = self.readInt(from: Array(bytes[start...(start + 7)])) |
| 119 | + |
| 120 | + guard |
| 121 | + let mT = modificationTime, |
| 122 | + let u = userID, |
| 123 | + let g = groupID, |
| 124 | + let m = mode, |
| 125 | + let s = size |
| 126 | + else { throw ArArchiveError.invalidHeader } |
| 127 | + |
| 128 | + var h = Header(name: name, userID: u, groupID: g, mode: m, modificationTime: mT) |
| 129 | + h.size = s |
| 130 | + return h |
| 131 | + } |
| 132 | + |
| 133 | + /// From [blakesmith/r/reader.go: line 62](https://github.com/blakesmith/ar/blob/809d4375e1fb5bb262c159fc3ec2e7a86a8bfd28/reader.go#L62) . |
| 134 | + private func readString(from bytes: [UInt8]) -> String { |
| 135 | + var i = bytes.count - 1 |
| 136 | + |
| 137 | + while i > 0, bytes[i] == 32 /* ASCII space character */ { |
| 138 | + i -= 1 |
| 139 | + } |
| 140 | + |
| 141 | + return String(bytes[0...i].map({ Character(Unicode.Scalar($0)) })) |
| 142 | + } |
| 143 | + |
| 144 | + private func readInt(from bytes: [UInt8], radix: Int? = nil) -> Int? { |
| 145 | + if let r = radix { |
| 146 | + return Int(self.readString(from: bytes), radix: r) |
| 147 | + } else { |
| 148 | + return Int(self.readString(from: bytes)) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +extension ArArchiveReader: IteratorProtocol, Sequence { |
| 154 | + public typealias Element = (Header, [UInt8]) |
| 155 | + |
| 156 | + public mutating func next() -> Element? { |
| 157 | + if self.currentIndex > self.headers.count - 1 { |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + let bytes = self[currentIndex] |
| 162 | + let h = self.headers[self.currentIndex] |
| 163 | + self.currentIndex += 1 |
| 164 | + |
| 165 | + return (h, bytes) |
| 166 | + } |
| 167 | +} |
0 commit comments