Zlib.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 NIOCore
  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 =
  62. try output
  63. .writeWithUnsafeMutableBytes(minimumWritableBytes: Int(upperBound)) { outputPointer in
  64. try self.stream.deflate(
  65. outputBuffer: CGRPCZlib_castVoidToBytefPointer(outputPointer.baseAddress!),
  66. outputBufferSize: outputPointer.count
  67. )
  68. }
  69. let bytesRead = inputPointer.count - self.stream.availableInputBytes
  70. return (bytesRead, writtenBytes)
  71. }
  72. }
  73. /// Resets compression state. This must be called after each call to `deflate` if more
  74. /// messages are to be compressed by this instance.
  75. func reset() {
  76. let rc = CGRPCZlib_deflateReset(&self.stream.zstream)
  77. // Possible return codes:
  78. // - Z_OK
  79. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  80. //
  81. // If we're in an inconsistent state we can just replace the stream and initialize it.
  82. switch rc {
  83. case Z_OK:
  84. ()
  85. case Z_STREAM_ERROR:
  86. self.end()
  87. self.stream = ZStream()
  88. self.initialize()
  89. default:
  90. preconditionFailure("deflateReset: unexpected return code rc=\(rc)")
  91. }
  92. }
  93. /// Initialize the `z_stream` used for deflate.
  94. private func initialize() {
  95. let rc = CGRPCZlib_deflateInit2(
  96. &self.stream.zstream,
  97. Z_DEFAULT_COMPRESSION, // compression level
  98. Z_DEFLATED, // compression method (this must be Z_DEFLATED)
  99. self.format.windowBits, // window size, i.e. deflate/gzip
  100. 8, // memory level (this is the default value in the docs)
  101. Z_DEFAULT_STRATEGY // compression strategy
  102. )
  103. // Possible return codes:
  104. // - Z_OK
  105. // - Z_MEM_ERROR: not enough memory
  106. // - Z_STREAM_ERROR: a parameter was invalid
  107. //
  108. // If we can't allocate memory then we can't progress anyway, and we control the parameters
  109. // so not throwing an error here is okay.
  110. assert(rc == Z_OK, "deflateInit2 error: rc=\(rc) \(self.stream.lastErrorMessage ?? "")")
  111. }
  112. /// Calls `deflateEnd` on the underlying `z_stream` to deallocate resources allocated by zlib.
  113. private func end() {
  114. _ = CGRPCZlib_deflateEnd(&self.stream.zstream)
  115. // Possible return codes:
  116. // - Z_OK
  117. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  118. //
  119. // Since we're going away there's no reason to fail here.
  120. }
  121. }
  122. // MARK: Inflate (decompression)
  123. /// Creates a new decompressor for the given compression format.
  124. ///
  125. /// This decompressor is only suitable for decompressing whole messages at a time. Callers
  126. /// must `reset()` the decompressor between subsequent calls to `inflate`.
  127. ///
  128. /// - Parameter format:The expected compression type.
  129. class Inflate {
  130. enum InflationState {
  131. /// Inflation is in progress.
  132. case inflating(InflatingState)
  133. /// Inflation completed successfully.
  134. case inflated
  135. init(compressedSize: Int, limit: DecompressionLimit) {
  136. self = .inflating(InflatingState(compressedSize: compressedSize, limit: limit))
  137. }
  138. /// Update the state with the result of `Zlib.ZStream.inflate(outputBuffer:outputBufferSize:)`.
  139. mutating func update(with result: Zlib.ZStream.InflateResult) throws {
  140. switch (result.outcome, self) {
  141. case var (.outputBufferTooSmall, .inflating(state)):
  142. guard state.outputBufferSize < state.maxDecompressedSize else {
  143. // We hit the decompression limit and last time we clamped our output buffer size; we
  144. // can't use a larger buffer without exceeding the limit.
  145. throw GRPCError.DecompressionLimitExceeded(compressedSize: state.compressedSize)
  146. .captureContext()
  147. }
  148. state.increaseOutputBufferSize()
  149. self = .inflating(state)
  150. case let (.complete, .inflating(state)):
  151. // Since we request a _minimum_ output buffer size from `ByteBuffer` it's possible that
  152. // the decompressed size exceeded the decompression limit.
  153. guard result.totalBytesWritten <= state.maxDecompressedSize else {
  154. throw GRPCError.DecompressionLimitExceeded(compressedSize: state.compressedSize)
  155. .captureContext()
  156. }
  157. self = .inflated
  158. case (.complete, .inflated),
  159. (.outputBufferTooSmall, .inflated):
  160. preconditionFailure("invalid outcome '\(result.outcome)'; inflation is already complete")
  161. }
  162. }
  163. }
  164. struct InflatingState {
  165. /// The compressed size of the data to inflate.
  166. let compressedSize: Int
  167. /// The maximum size the decompressed data may be, according to the user-defined
  168. /// decompression limit.
  169. let maxDecompressedSize: Int
  170. /// The minimum size requested for the output buffer.
  171. private(set) var outputBufferSize: Int
  172. init(compressedSize: Int, limit: DecompressionLimit) {
  173. self.compressedSize = compressedSize
  174. self.maxDecompressedSize = limit.maximumDecompressedSize(compressedSize: compressedSize)
  175. self.outputBufferSize = compressedSize
  176. self.increaseOutputBufferSize()
  177. }
  178. /// Increase the output buffer size without exceeding `maxDecompressedSize`.
  179. mutating func increaseOutputBufferSize() {
  180. let nextOutputBufferSize = 2 * self.outputBufferSize
  181. if nextOutputBufferSize > self.maxDecompressedSize {
  182. self.outputBufferSize = self.maxDecompressedSize
  183. } else {
  184. self.outputBufferSize = nextOutputBufferSize
  185. }
  186. }
  187. }
  188. private var stream: ZStream
  189. private let format: CompressionFormat
  190. private let limit: DecompressionLimit
  191. init(format: CompressionFormat, limit: DecompressionLimit) {
  192. self.stream = ZStream()
  193. self.format = format
  194. self.limit = limit
  195. self.initialize()
  196. }
  197. deinit {
  198. self.end()
  199. }
  200. /// Resets decompression state. This must be called after each call to `inflate` if more
  201. /// messages are to be decompressed by this instance.
  202. func reset() {
  203. let rc = CGRPCZlib_inflateReset(&self.stream.zstream)
  204. // Possible return codes:
  205. // - Z_OK
  206. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  207. //
  208. // If we're in an inconsistent state we can just replace the stream and initialize it.
  209. switch rc {
  210. case Z_OK:
  211. ()
  212. case Z_STREAM_ERROR:
  213. self.end()
  214. self.stream = ZStream()
  215. self.initialize()
  216. default:
  217. preconditionFailure("inflateReset: unexpected return code rc=\(rc)")
  218. }
  219. }
  220. /// Inflate the readable bytes from the `input` buffer into the `output` buffer.
  221. ///
  222. /// - Parameters:
  223. /// - input: The buffer read compressed bytes from.
  224. /// - output: The buffer to write the decompressed bytes into.
  225. /// - Returns: The number of bytes written into `output`.
  226. @discardableResult
  227. func inflate(_ input: inout ByteBuffer, into output: inout ByteBuffer) throws -> Int {
  228. if input.readableBytes == 0 {
  229. // Zero length compressed messages are always empty messages. Skip the inflate step
  230. // below and just return the number of bytes we wrote.
  231. return 0
  232. }
  233. return try input.readWithUnsafeMutableReadableBytes { inputPointer -> (Int, Int) in
  234. // Setup the input buffer.
  235. self.stream.availableInputBytes = inputPointer.count
  236. self.stream.nextInputBuffer = CGRPCZlib_castVoidToBytefPointer(inputPointer.baseAddress!)
  237. defer {
  238. self.stream.availableInputBytes = 0
  239. self.stream.nextInputBuffer = nil
  240. }
  241. var bytesWritten = 0
  242. var state = InflationState(compressedSize: inputPointer.count, limit: self.limit)
  243. while case let .inflating(inflationState) = state {
  244. // Each call to inflate writes into the buffer, so we need to take the writer index into
  245. // account here.
  246. let writerIndex = output.writerIndex
  247. let minimumWritableBytes = inflationState.outputBufferSize - writerIndex
  248. bytesWritten =
  249. try output
  250. .writeWithUnsafeMutableBytes(minimumWritableBytes: minimumWritableBytes) {
  251. outputPointer in
  252. let inflateResult = try self.stream.inflate(
  253. outputBuffer: CGRPCZlib_castVoidToBytefPointer(outputPointer.baseAddress!),
  254. outputBufferSize: outputPointer.count
  255. )
  256. try state.update(with: inflateResult)
  257. return inflateResult.bytesWritten
  258. }
  259. }
  260. let bytesRead = inputPointer.count - self.stream.availableInputBytes
  261. return (bytesRead, bytesWritten)
  262. }
  263. }
  264. private func initialize() {
  265. let rc = CGRPCZlib_inflateInit2(&self.stream.zstream, self.format.windowBits)
  266. // Possible return codes:
  267. // - Z_OK
  268. // - Z_MEM_ERROR: not enough memory
  269. //
  270. // If we can't allocate memory then we can't progress anyway so not throwing an error here is
  271. // okay.
  272. precondition(rc == Z_OK, "inflateInit2 error: rc=\(rc) \(self.stream.lastErrorMessage ?? "")")
  273. }
  274. func end() {
  275. _ = CGRPCZlib_inflateEnd(&self.stream.zstream)
  276. // Possible return codes:
  277. // - Z_OK
  278. // - Z_STREAM_ERROR: the source stream state was inconsistent.
  279. //
  280. // Since we're going away there's no reason to fail here.
  281. }
  282. }
  283. // MARK: ZStream
  284. /// This wraps a zlib `z_stream` to provide more Swift-like access to the underlying C-struct.
  285. struct ZStream {
  286. var zstream: z_stream
  287. init() {
  288. self.zstream = z_stream()
  289. self.zstream.next_in = nil
  290. self.zstream.avail_in = 0
  291. self.zstream.next_out = nil
  292. self.zstream.avail_out = 0
  293. self.zstream.zalloc = nil
  294. self.zstream.zfree = nil
  295. self.zstream.opaque = nil
  296. }
  297. /// Number of bytes available to read `self.nextInputBuffer`. See also: `z_stream.avail_in`.
  298. var availableInputBytes: Int {
  299. get {
  300. return Int(self.zstream.avail_in)
  301. }
  302. set {
  303. self.zstream.avail_in = UInt32(newValue)
  304. }
  305. }
  306. /// The next input buffer that zlib should read from. See also: `z_stream.next_in`.
  307. var nextInputBuffer: UnsafeMutablePointer<Bytef>! {
  308. get {
  309. return self.zstream.next_in
  310. }
  311. set {
  312. self.zstream.next_in = newValue
  313. }
  314. }
  315. /// The remaining writable space in `nextOutputBuffer`. See also: `z_stream.avail_out`.
  316. var availableOutputBytes: Int {
  317. get {
  318. return Int(self.zstream.avail_out)
  319. }
  320. set {
  321. self.zstream.avail_out = UInt32(newValue)
  322. return
  323. }
  324. }
  325. /// The next output buffer where zlib should write bytes to. See also: `z_stream.next_out`.
  326. var nextOutputBuffer: UnsafeMutablePointer<Bytef>! {
  327. get {
  328. return self.zstream.next_out
  329. }
  330. set {
  331. self.zstream.next_out = newValue
  332. }
  333. }
  334. /// The total number of bytes written to the output buffer. See also: `z_stream.total_out`.
  335. var totalOutputBytes: Int {
  336. return Int(self.zstream.total_out)
  337. }
  338. /// The last error message that zlib wrote. No message is guaranteed on error, however, `nil` is
  339. /// guaranteed if there is no error. See also `z_stream.msg`.
  340. var lastErrorMessage: String? {
  341. guard let bytes = self.zstream.msg else {
  342. return nil
  343. }
  344. return String(cString: bytes)
  345. }
  346. enum InflateOutcome {
  347. /// The data was successfully inflated.
  348. case complete
  349. /// A larger output buffer is required.
  350. case outputBufferTooSmall
  351. }
  352. struct InflateResult {
  353. var bytesWritten: Int
  354. var totalBytesWritten: Int
  355. var outcome: InflateOutcome
  356. }
  357. /// Decompress the stream into the given output buffer.
  358. ///
  359. /// - Parameter outputBuffer: The buffer into which to write the decompressed data.
  360. /// - Parameter outputBufferSize: The space available in `outputBuffer`.
  361. /// - Returns: The result of the `inflate`, whether it was successful or whether a larger
  362. /// output buffer is required.
  363. mutating func inflate(
  364. outputBuffer: UnsafeMutablePointer<UInt8>,
  365. outputBufferSize: Int
  366. ) throws -> InflateResult {
  367. self.nextOutputBuffer = outputBuffer
  368. self.availableOutputBytes = outputBufferSize
  369. defer {
  370. self.nextOutputBuffer = nil
  371. self.availableOutputBytes = 0
  372. }
  373. let rc = CGRPCZlib_inflate(&self.zstream, Z_FINISH)
  374. let outcome: InflateOutcome
  375. // Possible return codes:
  376. // - Z_OK: some progress has been made
  377. // - Z_STREAM_END: the end of the compressed data has been reached and all uncompressed output
  378. // has been produced
  379. // - Z_NEED_DICT: a preset dictionary is needed at this point
  380. // - Z_DATA_ERROR: the input data was corrupted
  381. // - Z_STREAM_ERROR: the stream structure was inconsistent
  382. // - Z_MEM_ERROR there was not enough memory
  383. // - Z_BUF_ERROR if no progress was possible or if there was not enough room in the output
  384. // buffer when Z_FINISH is used.
  385. //
  386. // Note that Z_OK is not okay here since we always flush with Z_FINISH and therefore
  387. // use Z_STREAM_END as our success criteria.
  388. switch rc {
  389. case Z_STREAM_END:
  390. outcome = .complete
  391. case Z_BUF_ERROR:
  392. outcome = .outputBufferTooSmall
  393. default:
  394. throw GRPCError.ZlibCompressionFailure(code: rc, message: self.lastErrorMessage)
  395. .captureContext()
  396. }
  397. return InflateResult(
  398. bytesWritten: outputBufferSize - self.availableOutputBytes,
  399. totalBytesWritten: self.totalOutputBytes,
  400. outcome: outcome
  401. )
  402. }
  403. /// Compresses the `inputBuffer` into the `outputBuffer`.
  404. ///
  405. /// `outputBuffer` must be large enough to store the compressed data, `deflateBound()` provides
  406. /// an upper bound for this value.
  407. ///
  408. /// - Parameter outputBuffer: The buffer into which to write the compressed data.
  409. /// - Parameter outputBufferSize: The space available in `outputBuffer`.
  410. /// - Returns: The number of bytes written into the `outputBuffer`.
  411. mutating func deflate(
  412. outputBuffer: UnsafeMutablePointer<UInt8>,
  413. outputBufferSize: Int
  414. ) throws -> Int {
  415. self.nextOutputBuffer = outputBuffer
  416. self.availableOutputBytes = outputBufferSize
  417. defer {
  418. self.nextOutputBuffer = nil
  419. self.availableOutputBytes = 0
  420. }
  421. let rc = CGRPCZlib_deflate(&self.zstream, Z_FINISH)
  422. // Possible return codes:
  423. // - Z_OK: some progress has been made
  424. // - Z_STREAM_END: all input has been consumed and all output has been produced (only when
  425. // flush is set to Z_FINISH)
  426. // - Z_STREAM_ERROR: the stream state was inconsistent
  427. // - Z_BUF_ERROR: no progress is possible
  428. //
  429. // The documentation notes that Z_BUF_ERROR is not fatal, and deflate() can be called again
  430. // with more input and more output space to continue compressing. However, we
  431. // call `deflateBound()` before `deflate()` which guarantees that the output size will not be
  432. // larger than the value returned by `deflateBound()` if `Z_FINISH` flush is used. As such,
  433. // the only acceptable outcome is `Z_STREAM_END`.
  434. guard rc == Z_STREAM_END else {
  435. throw GRPCError.ZlibCompressionFailure(code: rc, message: self.lastErrorMessage)
  436. .captureContext()
  437. }
  438. return outputBufferSize - self.availableOutputBytes
  439. }
  440. }
  441. enum CompressionFormat {
  442. case deflate
  443. case gzip
  444. var windowBits: Int32 {
  445. switch self {
  446. case .deflate:
  447. return 15
  448. case .gzip:
  449. return 31
  450. }
  451. }
  452. }
  453. }