Skip to content

Commit

Permalink
Style code
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Apr 29, 2023
1 parent f32efd3 commit 3b0f6f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
40 changes: 19 additions & 21 deletions Sources/Gzip/Data+Gzip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
The MIT License (MIT)

© 2014-2022 1024jp <wolfrosch.com>
© 2014-2023 1024jp <wolfrosch.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -64,7 +64,6 @@ public struct CompressionLevel: RawRepresentable {

self.rawValue = rawValue
}

}


Expand Down Expand Up @@ -139,7 +138,6 @@ public struct GzipError: Swift.Error {

return self.message
}

}


Expand Down Expand Up @@ -242,55 +240,56 @@ extension Data {
guard !self.isEmpty else {
return Data()
}

var data = Data(capacity: self.count * 2)
var totalIn: uLong = 0
var totalOut: uLong = 0

repeat {
var stream = z_stream()
var status: Int32

status = inflateInit2_(&stream, wBits, ZLIB_VERSION, Int32(DataSize.stream))

guard status == Z_OK else {
// inflateInit2 returns:
// Z_VERSION_ERROR The zlib library version is incompatible with the version assumed by the caller.
// Z_MEM_ERROR There was not enough memory.
// Z_STREAM_ERROR A parameters are invalid.

throw GzipError(code: status, msg: stream.msg)
}

repeat {
if Int(totalOut + stream.total_out) >= data.count {
data.count += self.count / 2
}

let inputCount = self.count
let outputCount = data.count

self.withUnsafeBytes { (inputPointer: UnsafeRawBufferPointer) in
let inputStartPosition = totalIn + stream.total_in
stream.next_in = UnsafeMutablePointer<Bytef>(mutating: inputPointer.bindMemory(to: Bytef.self).baseAddress!).advanced(by: Int(inputStartPosition))
stream.avail_in = uint(inputCount) - uInt(inputStartPosition)

data.withUnsafeMutableBytes { (outputPointer: UnsafeMutableRawBufferPointer) in
let outputStartPosition = totalOut + stream.total_out
stream.next_out = outputPointer.bindMemory(to: Bytef.self).baseAddress!.advanced(by: Int(outputStartPosition))
stream.avail_out = uInt(outputCount) - uInt(outputStartPosition)

status = inflate(&stream, Z_SYNC_FLUSH)

stream.next_out = nil
}

stream.next_in = nil
}

} while (status == Z_OK)

totalIn += stream.total_in

guard inflateEnd(&stream) == Z_OK, status == Z_STREAM_END else {
// inflate returns:
// Z_DATA_ERROR The input data was corrupted (input stream not conforming to the zlib format or incorrect check value).
Expand All @@ -299,16 +298,15 @@ extension Data {
// Z_BUF_ERROR No progress is possible or there was not enough room in the output buffer when Z_FINISH is used.
throw GzipError(code: status, msg: stream.msg)
}



totalOut += stream.total_out

} while (totalIn < self.count)

data.count = Int(totalOut)

return data
}


}


Expand Down
5 changes: 1 addition & 4 deletions Tests/GzipTests/GzipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/*
The MIT License (MIT)

© 2015-2022 1024jp
© 2015-2023 1024jp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -127,7 +127,6 @@ final class GzipTests: XCTestCase {
XCTAssertTrue(data.isGzipped)
XCTAssertEqual(String(data: uncompressed, encoding: .utf8), "teststring")
}

}


Expand All @@ -145,7 +144,6 @@ private extension XCTestCase {
return Bundle(for: type(of: self)).url(forResource: name, withExtension: nil)!
#endif
}

}


Expand All @@ -161,5 +159,4 @@ private extension String {
string.append(letters.randomElement()!)
}
}

}

0 comments on commit 3b0f6f7

Please sign in to comment.