Skip to content

Commit

Permalink
Dadoum/mainからプルリクエスト#97をマージする
Browse files Browse the repository at this point in the history
Initial Linux support
  • Loading branch information
p-x9 authored Oct 1, 2024
2 parents 4632684 + d1e515e commit c3ef0ac
Show file tree
Hide file tree
Showing 34 changed files with 4,496 additions and 62 deletions.
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "bc1c29221f6dfeb0ebbfbc98eb95cd3d4967868e",
"version" : "3.4.0"
}
}
],
"version" : 2
}
12 changes: 11 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import PackageDescription

let package = Package(
name: "MachOKit",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13),
],
products: [
.library(
name: "MachOKit",
Expand All @@ -14,11 +20,15 @@ let package = Package(
targets: ["MachOKitC"]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "4.0.0")
],
targets: [
.target(
name: "MachOKit",
dependencies: [
"MachOKitC"
"MachOKitC",
.product(name: "Crypto", package: "swift-crypto")
],
swiftSettings: SwiftSetting.allCases
),
Expand Down
7 changes: 3 additions & 4 deletions Sources/MachOKit/DyldCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public class DyldCache {
)

// check magic of header
guard let cpuType = header._cpuType,
let cpuSubType = header._cpuSubType else {
throw NSError() // FIXME: error
}
// FIXME: error instead of unwrap
let cpuType = header._cpuType!
let cpuSubType = header._cpuSubType!
self.cpu = .init(
typeRawValue: cpuType.rawValue,
subtypeRawValue: cpuSubType.rawValue
Expand Down
4 changes: 4 additions & 0 deletions Sources/MachOKit/Header/CPUSubType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ extension CPUARM64_32SubType: CustomStringConvertible {
extension CPUSubType {
/// CPU subtype of host pc
static var current: CPUSubType? {
#if canImport(Darwin)
guard let cpuType: CPUType = .current else {
return nil
}
Expand All @@ -1062,6 +1063,9 @@ extension CPUSubType {
let ret = sysctlbyname("hw.cpusubtype", &subtype, &size, nil, 0)
guard ret != -1 else { return nil }
return .init(rawValue: subtype, of: cpuType)
#else
return nil
#endif
}
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/MachOKit/Header/CPUType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension CPUType: RawRepresentable {

public init?(rawValue: cpu_type_t) {
switch rawValue {
case CPU_TYPE_ANY: self = .any
case RawValue(CPU_TYPE_ANY): self = .any
case CPU_TYPE_VAX: self = .vax
case CPU_TYPE_MC680x0: self = .mc680x0
case CPU_TYPE_X86: self = .x86
Expand All @@ -71,7 +71,7 @@ extension CPUType: RawRepresentable {

public var rawValue: cpu_type_t {
switch self {
case .any: CPU_TYPE_ANY
case .any: RawValue(CPU_TYPE_ANY)
case .vax: CPU_TYPE_VAX
case .mc680x0: CPU_TYPE_MC680x0
case .x86: CPU_TYPE_X86
Expand Down Expand Up @@ -127,6 +127,7 @@ extension CPUType {
extension CPUType {
/// CPU type of host pc
static var current: CPUType? {
#if canImport(Darwin)
var type: cpu_type_t = 0
var size = MemoryLayout<cpu_type_t>.size
let ret = sysctlbyname("hw.cputype", &type, &size, nil, 0)
Expand All @@ -140,5 +141,8 @@ extension CPUType {
}

return .init(rawValue: type)
#else
return nil
#endif
}
}
1 change: 0 additions & 1 deletion Sources/MachOKit/Header/MachHeader/MachHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import Darwin

public struct MachHeader: LayoutWrapper {
public var layout: mach_header
Expand Down
1 change: 0 additions & 1 deletion Sources/MachOKit/LoadCommand/BuildVersionCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import MachO

public struct BuildVersionCommand: LoadCommandWrapper {
public typealias Layout = build_version_command
Expand Down
1 change: 0 additions & 1 deletion Sources/MachOKit/LoadCommand/EncryptionInfoCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import MachO

public protocol EncryptionInfoCommandProtocol: LoadCommandWrapper {
var cryptId: Int { get }
Expand Down
1 change: 0 additions & 1 deletion Sources/MachOKit/LoadCommand/LoadCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import MachO

public enum LoadCommand {
/// LC_SEGMENT
Expand Down
1 change: 0 additions & 1 deletion Sources/MachOKit/LoadCommand/LoadCommandType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import MachO

public enum LoadCommandType {
/// LC_SEGMENT
Expand Down
1 change: 0 additions & 1 deletion Sources/MachOKit/LoadCommand/Model/Dylib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import MachO

public struct Dylib {
/// library's path name
Expand Down
1 change: 0 additions & 1 deletion Sources/MachOKit/LoadCommand/RpathCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import MachO

public struct RpathCommand: LoadCommandWrapper {
public typealias Layout = rpath_command
Expand Down
1 change: 1 addition & 0 deletions Sources/MachOKit/MachOFile+CodeSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//

import CoreFoundation
import Foundation
import MachOKitC

Expand Down
1 change: 1 addition & 0 deletions Sources/MachOKit/MachOImage+CodeSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//

import CoreFoundation
import Foundation

extension MachOImage {
Expand Down
6 changes: 5 additions & 1 deletion Sources/MachOKit/MachOImage+static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
//

import Foundation
import MachO


extension MachOImage {
/// Sequence of loaded machO images.
public static var images: AnySequence<MachOImage> {
#if canImport(Darwin)
AnySequence(
(0..<_dyld_image_count())
.lazy
Expand All @@ -20,6 +21,9 @@ extension MachOImage {
MachOImage(ptr: $0)
}
)
#else
AnySequence([])
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/MachOKit/MachOImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extension MachOImage {
/// - /usr/lib/swift/libswiftFoundation.dylib
/// → libswiftFoundation
public init?(name: String) {
#if canImport(Darwin)
let indices = 0..<_dyld_image_count()
let index = indices.first { index in
guard let pathC = _dyld_get_image_name(index) else {
Expand All @@ -99,6 +100,9 @@ extension MachOImage {
} else {
return nil
}
#else
return nil
#endif
}
}

Expand All @@ -107,9 +111,13 @@ extension MachOImage {
///
/// It is the same value that can be obtained by `Dl_info.dli_fname` or `_dyld_get_image_name`.
public var path: String? {
#if canImport(Darwin)
var info = Dl_info()
dladdr(ptr, &info)
return String(cString: info.dli_fname)
#else
return nil
#endif
}

/// virtual memory address slide of machO image.
Expand Down
7 changes: 3 additions & 4 deletions Sources/MachOKit/MachOKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ public enum File {
public func loadFromFile(url: URL) throws -> File {
let fileHandle = try FileHandle(forReadingFrom: url)
let magicRaw: UInt32 = fileHandle.read(offset: 0)

guard let magic = Magic(rawValue: magicRaw) else {
throw NSError() // FIXME: error
}

// FIXME: error instead of unwrap
let magic = Magic(rawValue: magicRaw)!

if magic.isFat {
return .fat(try FatFile(url: url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation
import MachOKitC
import CommonCrypto
import Crypto

public struct CodeSignCodeDirectory: LayoutWrapper {
public typealias Layout = CS_CodeDirectory
Expand Down Expand Up @@ -58,28 +58,16 @@ extension CodeSignCodeDirectory {
in signature: MachOFile.CodeSign
) -> Data? {
let data = signature.data[offset ..< offset + numericCast(layout.length)]
let length: CC_LONG = numericCast(layout.length)

return data.withUnsafeBytes {
guard let baseAddress = $0.baseAddress else {
return nil
}
switch hashType {
switch hashType {
case .sha1:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CC_SHA1(baseAddress, length, &digest)
return Data(digest)
return Data(Insecure.SHA1.hash(data: data))
case .sha256, .sha256_truncated:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(baseAddress, length, &digest)
return Data(digest)
return Data(SHA256.hash(data: data))
case .sha384:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA384_DIGEST_LENGTH))
CC_SHA384(baseAddress, length, &digest)
return Data(digest)
return Data(SHA384.hash(data: data))
case .none:
return nil
}
}
}

Expand All @@ -90,28 +78,16 @@ extension CodeSignCodeDirectory {
bytes: signature.basePointer.advanced(by: offset),
count: numericCast(layout.length)
)
let length: CC_LONG = numericCast(layout.length)

return data.withUnsafeBytes {
guard let baseAddress = $0.baseAddress else {
return nil
}
switch hashType {
switch hashType {
case .sha1:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CC_SHA1(baseAddress, length, &digest)
return Data(digest)
return Data(Insecure.SHA1.hash(data: data))
case .sha256, .sha256_truncated:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(baseAddress, length, &digest)
return Data(digest)
return Data(SHA256.hash(data: data))
case .sha384:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA384_DIGEST_LENGTH))
CC_SHA384(baseAddress, length, &digest)
return Data(digest)
return Data(SHA384.hash(data: data))
case .none:
return nil
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/MachOKit/Util/exported.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//
//

#if canImport(MachO)
@_documentation(visibility: internal)
@_exported
import MachO
#endif

@_exported
import MachOKitC
8 changes: 0 additions & 8 deletions Sources/MachOKitC/dummy.c

This file was deleted.

Loading

0 comments on commit c3ef0ac

Please sign in to comment.