Skip to content
Snippets Groups Projects

Parallel decompress

Closed Jim Wallace requested to merge parallel-decompress into main
7 files
+ 23
338
Compare changes
  • Side-by-side
  • Inline
Files
7
//// Copyright (c) 2024 Jim Wallace
////
//// Permission is hereby granted, free of charge, to any person
//// obtaining a copy of this software and associated documentation
//// files (the "Software"), to deal in the Software without
//// restriction, including without limitation the rights to use,
//// copy, modify, merge, publish, distribute, sublicense, and/or sell
//// copies of the Software, and to permit persons to whom the
//// Software is furnished to do so, subject to the following
//// conditions:
////
//// The above copyright notice and this permission notice shall be
//// included in all copies or substantial portions of the Software.
////
//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//// OTHER DEALINGS IN THE SOFTWARE.
//
//import Foundation
//import libzstd
//
///**
// * Types of exceptions thrown by the wrapper.
// */
//public enum ZSTDError : Error {
// case libraryError(errMsg : String)
// case decompressedSizeUnknown
// case invalidCompressionLevel(cl: Int32)
// case unknownError
//}
//
//struct ZSTDDecompressedData {
//
// let data: Data
//
// init(_ compressed: Data) {
//
// var result: Data = Data()
// do {
// debugPrint("Prepping ZSTD")
// //let decompCtx : OpaquePointer = ZSTD_createDCtx()
// result = try ZSTDDecompressedData.decompressFrame(compressed, nil)
// //ZSTD_freeDCtx(decompCtx)
// debugPrint("Finished ZSTD")
// } catch {
//
// }
// data = result
// }
//
//}
//
//
//extension ZSTDDecompressedData {
// /**
// * Decompress a frame that resulted from a previous compression of a buffer by ZSTD.
// * The exact frame size must be known, which is available via the
// * ZSTD_getDecompressedSize() API call.
// *
// * - parameter dataIn: frame to be decompressed
// * - parameter delegateFunction: closure/function to perform specific decompression work
// * - returns: a Data instance wrapping the decompressed buffer
// */
// static func decompressFrameCommon(_ dataIn : Data,
// _ decompCtx: OpaquePointer? = nil,
// _ delegateFunction : (UnsafeMutableRawPointer,
// Int,
// UnsafeRawPointer,
// Int,
// OpaquePointer? ) -> Int ) throws -> Data
// {
//
// let storedDSize = dataIn.withUnsafeBytes { (p : UnsafeRawBufferPointer) -> UInt64 in
// let storedSize = ZSTD_getFrameContentSize(p.baseAddress, dataIn.count)
// debugPrint("Stored size: \(storedSize)")
// return storedSize
// }
//
// guard storedDSize != 0 else {
// debugPrint("ERROR: Decompressed Size Unknown")
// throw ZSTDError.decompressedSizeUnknown
// }
//
// var retVal = Data(count: Int(storedDSize))
//
// try dataIn.withUnsafeBytes{ (pIn : UnsafeRawBufferPointer) -> Void in
// try retVal.withUnsafeMutableBytes{ (pOut :UnsafeMutableRawBufferPointer) -> Void in
// guard let pInBaseAdd = pIn.baseAddress, let pOutBaseAdd = pOut.baseAddress else {
// throw ZSTDError.unknownError
// }
// let rc = delegateFunction(pOutBaseAdd, Int(storedDSize), pInBaseAdd, dataIn.count, decompCtx)
// if ZSTD_isError(rc) != 0 {
// //if let errStr = getProcessorErrorString(rc) {
// // throw ZSTDError.libraryError(errMsg: errStr)
// //} else {
// throw ZSTDError.unknownError
// //}
// }
// }
// }
// debugPrint(String(data:retVal, encoding: .utf8)!)
// return retVal
// }
//
// /**
// * A helper function to get the error string corresponding to an error code.
// *
// * - parameter ec: error code
// * - returns: optional String matching the error code
// */
// static func getProcessorErrorString(_ ec : Int) -> String?
// {
// if (ZSTD_isError(ec) != 0) {
// if let err = ZSTD_getErrorName(ec) {
// return err.debugDescription
// //return String(bytesNoCopy: UnsafeMutableRawPointer(mutating: err), length: Int(strlen(err)), encoding: String.Encoding.ascii, freeWhenDone: false)
// }
// }
// return nil
// }
//
// /**
// * A helper to validate compression level. A valid compression level is positive and
// * does not exceed the max value provided by the ZSTD C library.
// *
// * - parameter compressionLevel : compression level to validate
// * - returns: true if compression level is valid
// */
// static func isValidCompressionLevel(_ compressionLevel : Int32) -> Bool {
// return compressionLevel >= 1 && compressionLevel <= ZSTD_maxCLevel()
// }
//
// /**
// * Decompress a frame that resulted from a previous compression of a buffer by a call
// * to compressBuffer().
// *
// * - parameter dataIn: frame to be decompressed
// * - returns: a Data instance wrapping the decompressed buffer
// */
// public static func decompressFrame(_ dataIn : Data, _ decompCtx: OpaquePointer? = nil) throws -> Data
// {
// return try ZSTDDecompressedData.decompressFrameCommon(dataIn, decompCtx, decompressFrameHelper)
// }
//
// /**
// * A private helper passed to commonProcessor.decompressFrameCommon().
// */
// private static func decompressFrameHelper(dst : UnsafeMutableRawPointer,
// dstCapacity : Int,
// src : UnsafeRawPointer,
// srcSize : Int,
// decompCtx: OpaquePointer? = nil ) -> Int {
// if decompCtx != nil {
// return ZSTD_decompressDCtx(decompCtx, dst, dstCapacity, src, srcSize);
// } else {
// return ZSTD_decompress(dst, dstCapacity, src, srcSize)
// }
// }
//}
//
Loading