Skip to content

Commit

Permalink
Add ZSTD Decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
asdfugil committed Jun 5, 2024
1 parent 036eca2 commit 425d205
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
25 changes: 25 additions & 0 deletions PurePKG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
01AFD24B2C1037B500C66591 /* libzstd in Frameworks */ = {isa = PBXBuildFile; productRef = 01AFD24A2C1037B500C66591 /* libzstd */; };
652BCB492BD3678E00DA4AA0 /* Down in Frameworks */ = {isa = PBXBuildFile; productRef = 652BCB482BD3678E00DA4AA0 /* Down */; };
653AB88D2BCF416500E3A215 /* TweakDepitionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653AB88C2BCF416500E3A215 /* TweakDepitionView.swift */; };
653AB8952BCF433B00E3A215 /* InstalledView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653AB8942BCF433B00E3A215 /* InstalledView.swift */; };
Expand Down Expand Up @@ -101,6 +102,7 @@
files = (
65DC66E12BCCF24F00EBFE82 /* Nuke in Frameworks */,
659F63942C0E296900A36D5A /* SWCompression in Frameworks */,
01AFD24B2C1037B500C66591 /* libzstd in Frameworks */,
65DC66E32BCCF24F00EBFE82 /* NukeUI in Frameworks */,
652BCB492BD3678E00DA4AA0 /* Down in Frameworks */,
);
Expand Down Expand Up @@ -260,6 +262,7 @@
65DC66E22BCCF24F00EBFE82 /* NukeUI */,
652BCB482BD3678E00DA4AA0 /* Down */,
659F63932C0E296900A36D5A /* SWCompression */,
01AFD24A2C1037B500C66591 /* libzstd */,
);
productName = PureKFD;
productReference = 65D0169E2BA949C9003F11E4 /* PurePKG.app */;
Expand Down Expand Up @@ -293,6 +296,7 @@
65DC66DF2BCCF24F00EBFE82 /* XCRemoteSwiftPackageReference "Nuke" */,
652BCB472BD3678E00DA4AA0 /* XCRemoteSwiftPackageReference "Down" */,
659F63922C0E296900A36D5A /* XCRemoteSwiftPackageReference "SWCompression" */,
01AFD2492C1037B500C66591 /* XCRemoteSwiftPackageReference "zstd" */,
);
productRefGroup = 65D0169F2BA949C9003F11E4 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -533,6 +537,10 @@
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
/opt/procursus/lib,
/usr/lib,
/var/jb/usr/lib,
"@loader_path/.jbroot/usr/lib",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6;
Expand Down Expand Up @@ -579,6 +587,10 @@
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
/opt/procursus/lib,
/usr/lib,
/var/jb/usr/lib,
"@loader_path/.jbroot/usr/lib",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6;
Expand Down Expand Up @@ -622,6 +634,14 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
01AFD2492C1037B500C66591 /* XCRemoteSwiftPackageReference "zstd" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/facebook/zstd.git";
requirement = {
kind = revision;
revision = 794ea1b0afca0f020f4e57b6732332231fb23c70;
};
};
652BCB472BD3678E00DA4AA0 /* XCRemoteSwiftPackageReference "Down" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/johnxnguyen/Down.git";
Expand Down Expand Up @@ -649,6 +669,11 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
01AFD24A2C1037B500C66591 /* libzstd */ = {
isa = XCSwiftPackageProductDependency;
package = 01AFD2492C1037B500C66591 /* XCRemoteSwiftPackageReference "zstd" */;
productName = libzstd;
};
652BCB482BD3678E00DA4AA0 /* Down */ = {
isa = XCSwiftPackageProductDependency;
package = 652BCB472BD3678E00DA4AA0 /* XCRemoteSwiftPackageReference "Down" */;
Expand Down
32 changes: 30 additions & 2 deletions Shared/Decompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,38 @@

import Foundation
import SWCompression
import libzstd

func DecompressZSTD(data: Data) throws -> Data {
let stream = ZSTD_createDStream();
if (stream == nil) {
throw "ZSTD_createDStream() failed!"
}
ZSTD_initDStream(stream);
var outData = Data();

var inBuffer = ZSTD_inBuffer(src: (data as NSData).bytes, size: data.count, pos: 0);
let tempBuf = UnsafeMutableRawPointer.allocate(byteCount: 1048576, alignment: 8)
while (inBuffer.pos < inBuffer.size) {
var outBuffer = ZSTD_outBuffer(dst: tempBuf, size: 1048576, pos: 0);
let retval = ZSTD_decompressStream(stream, &outBuffer, &inBuffer);
if ((ZSTD_isError(retval)) != 0) {
tempBuf.deallocate();
ZSTD_freeDStream(stream);
throw String(cString: ZSTD_getErrorName(retval));
}
outData.append(tempBuf.bindMemory(to: UInt8.self, capacity: 1048576), count: outBuffer.pos);
}
ZSTD_freeDStream(stream)
tempBuf.deallocate();
return outData;
}

enum ArchiveType {
case XZ
case BZip
case GZip
case Zstd
}

extension URL {
Expand All @@ -23,8 +50,7 @@ extension URL {
} else if self.lastPathComponent.contains(".bz2") {
return .BZip
} else if self.lastPathComponent.contains(".zst") {
log("idk how to add ZST support but if someone wants to, contribute to purepkg at https://github.com/lrdsnow/purepkg")
return nil
return .Zstd
} else {
return nil
}
Expand All @@ -42,6 +68,8 @@ extension Data {
decompressedData = try BZip2.decompress(data: self)
case .GZip:
decompressedData = try GzipArchive.unarchive(archive: self)
case .Zstd:
decompressedData = try DecompressZSTD(data: self)
}
return decompressedData
} catch {
Expand Down
10 changes: 5 additions & 5 deletions Shared/RepoHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import UIKit

public class RepoHandler {
public static func get_dict_compressed(_ url: URL, completion: @escaping ([String: String]?, Error?) -> Void) {
let suffixes = ["", "gz", "lzma", "xz", "bz2"]
let suffixes = ["zst", "xz", "lzma", "gz", "bz2", ""]
var attempt = 0

func attemptFetch(url: URL) {
Expand All @@ -34,11 +34,11 @@ public class RepoHandler {
}
}

attemptFetch(url: url)
attemptFetch(url: url.deletingPathExtension().appendingPathExtension(suffixes[attempt]))
}

public static func get_compressed(_ url: URL, completion: @escaping ([[String: String]]?, Error?) -> Void) {
let suffixes = ["", "gz", "lzma", "xz", "bz2"]
let suffixes = ["zst", "xz", "lzma", "gz", "bz2", ""]
var attempt = 0

func attemptFetch(url: URL) {
Expand All @@ -57,8 +57,8 @@ public class RepoHandler {
}
}
}

attemptFetch(url: url)
attemptFetch(url: url.deletingPathExtension().appendingPathExtension(suffixes[attempt]))

}

public static func get_dict(_ url: URL, completion: @escaping ([String: String]?, Error?) -> Void) {
Expand Down

0 comments on commit 425d205

Please sign in to comment.