Zlib.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import CGRPCZlib
  17. import NIO
  18. import struct Foundation.Data
  19. /// Provides minimally configurable wrappers around zlib's compression and decompression
  20. /// functionality.
  21. ///
  22. /// See also: https://www.zlib.net/manual.html
  23. enum Zlib {
  24. // MARK: Deflate (compression)
  25. /// Creates a new compressor for the given compression format.
  26. ///
  27. /// This compressor is only suitable for compressing whole messages at a time. Callers
  28. /// must `reset()` the compressor between subsequent calls to `deflate`.
  29. ///
  30. /// - Parameter format:The expected compression type.
  31. class Deflate {
  32. private var stream: ZStream
  33. private let format: CompressionFormat
  34. init(format: CompressionFormat) {
  35. self.stream = ZStream()
  36. self.format = format
  37. self.initialize()
  38. }
  39. deinit {
  40. self.end()
  41. }
  42. /// Compresses the data in `input` into the `output` buffer.
  43. ///
  44. /// - Parameter input: The complete data to be compressed.
  45. /// - Parameter output: The `ByteBuffer` into which the compressed message should be written.
  46. /// - Returns: The number of bytes written into the `output` buffer.
  47. func deflate(_ input: inout ByteBuffer, into output: inout ByteBuffer) throws -> Int {
  48. // Note: This is only valid because we always use Z_FINISH to flush.
  49. //
  50. // From the documentation:
  51. // Note that it is possible for the compressed size to be larger than the value returned
  52. // by deflateBound() if flush options other than Z_FINISH or Z_NO_FLUSH are used.
  53. let upperBound = CGRPCZlib_deflateBound(&self.stream.zstream, UInt(input.readableBytes))
  54. return try input.readWithUnsafeMutableReadableBytes { inputPointer -> (Int, Int) in
  55. self.stream.nextInputBuffer = CGRPCZlib_castVoidToBytefPointer(inputPointer.baseAddress!)
  56. self.stream.availableInputBytes = inputPointer.count
  57. defer {
  58. self.stream.nextInputBuffer = nil
  59. self.stream.availableInputBytes = 0
  60. }
  61. let writtenBytes = try output.writeWithUnsafeMutableBytes(minimumWritableBytes: Int(upperBound)) { outputPointer in
  62. try self.stream.deflate(
  63. outputBuffer: CGRPCZlib_castVoidToBytefPointer(outputPointer.baseAddress!),
  64. outputBufferSize: outputPointer.count
  65. )
  66. }
  67. let bytesRead = inputPointer.count - self.stream.availableInputBytes
  68. return (bytesRead, writtenBytes)
  69. }
  70. }
  71. /// Resets compression state. This must be called after each call to `deflate` if more
  72. /// messages are to be compressed by this instance.
  73. func reset() {
  74. let rc = CGRPCZlib_deflateReset(&self.stream.zstream)
  75. // Possible return codes:
  76. // - Z_OK
  77. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  78. //
  79. // If we're in an inconsistent state we can just replace the stream and initialize it.
  80. switch rc {
  81. case Z_OK:
  82. ()
  83. case Z_STREAM_ERROR:
  84. self.end()
  85. self.stream = ZStream()
  86. self.initialize()
  87. default:
  88. preconditionFailure("deflateReset: unexpected return code rc=\(rc)")
  89. }
  90. }
  91. /// Initialize the `z_stream` used for deflate.
  92. private func initialize() {
  93. let rc = CGRPCZlib_deflateInit2(
  94. &self.stream.zstream,
  95. Z_DEFAULT_COMPRESSION, // compression level
  96. Z_DEFLATED, // compression method (this must be Z_DEFLATED)
  97. self.format.windowBits, // window size, i.e. deflate/gzip
  98. 8, // memory level (this is the default value in the docs)
  99. Z_DEFAULT_STRATEGY // compression strategy
  100. )
  101. // Possible return codes:
  102. // - Z_OK
  103. // - Z_MEM_ERROR: not enough memory
  104. // - Z_STREAM_ERROR: a parameter was invalid
  105. //
  106. // If we can't allocate memory then we can't progress anyway, and we control the parameters
  107. // so not throwing an error here is okay.
  108. assert(rc == Z_OK, "deflateInit2 error: rc=\(rc) \(self.stream.lastErrorMessage ?? "")")
  109. }
  110. /// Calls `deflateEnd` on the underlying `z_stream` to deallocate resources allocated by zlib.
  111. private func end() {
  112. let _ = CGRPCZlib_deflateEnd(&self.stream.zstream)
  113. // Possible return codes:
  114. // - Z_OK
  115. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  116. //
  117. // Since we're going away there's no reason to fail here.
  118. }
  119. }
  120. // MARK: Inflate (decompression)
  121. /// Creates a new decompressor for the given compression format.
  122. ///
  123. /// This decompressor is only suitable for decompressing whole messages at a time. Callers
  124. /// must `reset()` the decompressor between subsequent calls to `inflate`.
  125. ///
  126. /// - Parameter format:The expected compression type.
  127. class Inflate {
  128. private var stream: ZStream
  129. private let format: CompressionFormat
  130. init(format: CompressionFormat) {
  131. self.stream = ZStream()
  132. self.format = format
  133. self.initialize()
  134. }
  135. deinit {
  136. self.end()
  137. }
  138. /// Resets decompression state. This must be called after each call to `inflate` if more
  139. /// messages are to be decompressed by this instance.
  140. func reset() {
  141. let rc = CGRPCZlib_inflateReset(&self.stream.zstream)
  142. // Possible return codes:
  143. // - Z_OK
  144. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  145. //
  146. // If we're in an inconsistent state we can just replace the stream and initialize it.
  147. switch rc {
  148. case Z_OK:
  149. ()
  150. case Z_STREAM_ERROR:
  151. self.end()
  152. self.stream = ZStream()
  153. self.initialize()
  154. default:
  155. preconditionFailure("inflateReset: unexpected return code rc=\(rc)")
  156. }
  157. }
  158. /// Inflate the readable bytes from the `input` buffer into the `output` buffer.
  159. ///
  160. /// - Parameters:
  161. /// - input: The buffer read compressed bytes from.
  162. /// - output: The buffer to write the decompressed bytes into.
  163. /// - Returns: The number of bytes written into `output`.
  164. @discardableResult
  165. func inflate(_ input: inout ByteBuffer, into output: inout ByteBuffer) throws -> Int {
  166. return try input.readWithUnsafeMutableReadableBytes { inputPointer -> (Int, Int) in
  167. // Setup the input buffer.
  168. self.stream.availableInputBytes = inputPointer.count
  169. self.stream.nextInputBuffer = CGRPCZlib_castVoidToBytefPointer(inputPointer.baseAddress!)
  170. defer {
  171. self.stream.availableInputBytes = 0
  172. self.stream.nextInputBuffer = nil
  173. }
  174. // We don't know how large the output will be; we'll try 2x the input size.
  175. var outBufferSize = inputPointer.count * 2
  176. var inflated = false
  177. var bytesWritten = 0
  178. while !inflated {
  179. bytesWritten = try output.writeWithUnsafeMutableBytes(minimumWritableBytes: outBufferSize) { outputPointer in
  180. let inflateResult = try self.stream.inflate(
  181. outputBuffer: CGRPCZlib_castVoidToBytefPointer(outputPointer.baseAddress!),
  182. outputBufferSize: outputPointer.count
  183. )
  184. switch inflateResult.outcome {
  185. case .complete:
  186. inflated = true
  187. case .outputBufferTooSmall:
  188. outBufferSize *= 2
  189. }
  190. return inflateResult.bytesWritten
  191. }
  192. }
  193. assert(inflated)
  194. let bytesRead = inputPointer.count - self.stream.availableInputBytes
  195. return (bytesRead, bytesWritten)
  196. }
  197. }
  198. private func initialize() {
  199. let rc = CGRPCZlib_inflateInit2(&self.stream.zstream, self.format.windowBits)
  200. // Possible return codes:
  201. // - Z_OK
  202. // - Z_MEM_ERROR: not enough memory
  203. //
  204. // If we can't allocate memory then we can't progress anyway so not throwing an error here is
  205. // okay.
  206. precondition(rc == Z_OK, "inflateInit2 error: rc=\(rc) \(self.stream.lastErrorMessage ?? "")")
  207. }
  208. func end() {
  209. let _ = CGRPCZlib_inflateEnd(&self.stream.zstream)
  210. // Possible return codes:
  211. // - Z_OK
  212. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  213. //
  214. // Since we're going away there's no reason to fail here.
  215. }
  216. }
  217. enum InflateResult {
  218. case complete
  219. case outputBufferTooSmall
  220. }
  221. // MARK: ZStream
  222. /// This wraps a zlib `z_stream` to provide more Swift-like access to the underlying C-struct.
  223. struct ZStream {
  224. var zstream: z_stream
  225. init() {
  226. self.zstream = z_stream()
  227. self.zstream.next_in = nil
  228. self.zstream.avail_in = 0
  229. self.zstream.next_out = nil
  230. self.zstream.avail_out = 0
  231. self.zstream.zalloc = nil
  232. self.zstream.zfree = nil
  233. self.zstream.opaque = nil
  234. }
  235. /// Number of bytes available to read `self.nextInputBuffer`. See also: `z_stream.avail_in`.
  236. var availableInputBytes: Int {
  237. get {
  238. return Int(self.zstream.avail_in)
  239. }
  240. set {
  241. self.zstream.avail_in = UInt32(newValue)
  242. }
  243. }
  244. /// The next input buffer that zlib should read from. See also: `z_stream.next_in`.
  245. var nextInputBuffer: UnsafeMutablePointer<Bytef>! {
  246. get {
  247. return self.zstream.next_in
  248. }
  249. set {
  250. self.zstream.next_in = newValue
  251. }
  252. }
  253. /// The remaining writable space in `nextOutputBuffer`. See also: `z_stream.avail_out`.
  254. var availableOutputBytes: Int {
  255. get {
  256. return Int(self.zstream.avail_out)
  257. }
  258. set {
  259. return self.zstream.avail_out = UInt32(newValue)
  260. }
  261. }
  262. /// The next output buffer where zlib should write bytes to. See also: `z_stream.next_out`.
  263. var nextOutputBuffer: UnsafeMutablePointer<Bytef>! {
  264. get {
  265. return self.zstream.next_out
  266. }
  267. set {
  268. self.zstream.next_out = newValue
  269. }
  270. }
  271. /// The last error message that zlib wrote. No message is guaranteed on error, however, `nil` is
  272. /// guaranteed if there is no error. See also `z_stream.msg`.
  273. var lastErrorMessage: String? {
  274. guard let bytes = self.zstream.msg else {
  275. return nil
  276. }
  277. return String(cString: bytes)
  278. }
  279. enum InflateOutcome {
  280. /// The data was successfully inflated.
  281. case complete
  282. /// A larger output buffer is required.
  283. case outputBufferTooSmall
  284. }
  285. struct InflateResult {
  286. var bytesWritten: Int
  287. var outcome: InflateOutcome
  288. }
  289. /// Decompress the stream into the given output buffer.
  290. ///
  291. /// - Parameter outputBuffer: The buffer into which to write the decompressed data.
  292. /// - Parameter outputBufferSize: The space available in `outputBuffer`.
  293. /// - Returns: The result of the `inflate`, whether it was successful or whether a larger
  294. /// output buffer is required.
  295. mutating func inflate(
  296. outputBuffer: UnsafeMutablePointer<UInt8>,
  297. outputBufferSize: Int
  298. ) throws -> InflateResult {
  299. self.nextOutputBuffer = outputBuffer
  300. self.availableOutputBytes = outputBufferSize
  301. defer {
  302. self.nextOutputBuffer = nil
  303. self.availableOutputBytes = 0
  304. }
  305. let rc = CGRPCZlib_inflate(&self.zstream, Z_FINISH)
  306. let outcome: InflateOutcome
  307. // Possible return codes:
  308. // - Z_OK: some progress has been made
  309. // - Z_STREAM_END: the end of the compressed data has been reached and all uncompressed output
  310. // has been produced
  311. // - Z_NEED_DICT: a preset dictionary is needed at this point
  312. // - Z_DATA_ERROR: the input data was corrupted
  313. // - Z_STREAM_ERROR: the stream structure was inconsistent
  314. // - Z_MEM_ERROR there was not enough memory
  315. // - Z_BUF_ERROR if no progress was possible or if there was not enough room in the output
  316. // buffer when Z_FINISH is used.
  317. //
  318. // Note that Z_OK is not okay here since we always flush with Z_FINISH and therefore
  319. // use Z_STREAM_END as our success criteria.
  320. switch rc {
  321. case Z_STREAM_END:
  322. outcome = .complete
  323. case Z_BUF_ERROR:
  324. outcome = .outputBufferTooSmall
  325. default:
  326. throw GRPCError.ZlibCompressionFailure(code: rc, message: self.lastErrorMessage).captureContext()
  327. }
  328. return InflateResult(
  329. bytesWritten: outputBufferSize - self.availableOutputBytes,
  330. outcome: outcome
  331. )
  332. }
  333. /// Compresses the `inputBuffer` into the `outputBuffer`.
  334. ///
  335. /// `outputBuffer` must be large enough to store the compressed data, `deflateBound()` provides
  336. /// an upper bound for this value.
  337. ///
  338. /// - Parameter outputBuffer: The buffer into which to write the compressed data.
  339. /// - Parameter outputBufferSize: The space available in `outputBuffer`.
  340. /// - Returns: The number of bytes written into the `outputBuffer`.
  341. mutating func deflate(
  342. outputBuffer: UnsafeMutablePointer<UInt8>,
  343. outputBufferSize: Int
  344. ) throws -> Int {
  345. self.nextOutputBuffer = outputBuffer
  346. self.availableOutputBytes = outputBufferSize
  347. defer {
  348. self.nextOutputBuffer = nil
  349. self.availableOutputBytes = 0
  350. }
  351. let rc = CGRPCZlib_deflate(&self.zstream, Z_FINISH)
  352. // Possible return codes:
  353. // - Z_OK: some progress has been made
  354. // - Z_STREAM_END: all input has been consumed and all output has been produced (only when
  355. // flush is set to Z_FINISH)
  356. // - Z_STREAM_ERROR: the stream state was inconsistent
  357. // - Z_BUF_ERROR: no progress is possible
  358. //
  359. // The documentation notes that Z_BUF_ERROR is not fatal, and deflate() can be called again
  360. // with more input and more output space to continue compressing. However, we
  361. // call `deflateBound()` before `deflate()` which guarantees that the output size will not be
  362. // larger than the value returned by `deflateBound()` if `Z_FINISH` flush is used. As such,
  363. // the only acceptable outcome is `Z_STREAM_END`.
  364. guard rc == Z_STREAM_END else {
  365. throw GRPCError.ZlibCompressionFailure(code: rc, message: self.lastErrorMessage).captureContext()
  366. }
  367. return outputBufferSize - self.availableOutputBytes
  368. }
  369. }
  370. enum CompressionFormat {
  371. case deflate
  372. case gzip
  373. var windowBits: Int32 {
  374. switch self {
  375. case .deflate:
  376. return 15
  377. case .gzip:
  378. return 31
  379. }
  380. }
  381. }
  382. }