forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.swift
167 lines (144 loc) · 5.49 KB
/
Image.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//===--- Image.swift - Binary image protocol for Swift --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Defines a protocol for binary image files that allows us to fetch what
// we need without knowing all of the gory details.
//
//===----------------------------------------------------------------------===//
import Swift
struct ImageSymbol {
var name: String
var offset: Int
}
internal protocol Image {
associatedtype Source: ImageSource
typealias UUID = [UInt8]
typealias Address = Source.Address
init(source: Source, baseAddress: Address, endAddress: Address) throws
var baseAddress: Address { get set }
var endAddress: Address { get set }
var source: Source { get }
var uuid: UUID? { get }
var shouldByteSwap: Bool { get }
func swapIfRequired<T: FixedWidthInteger>(_ x: T) -> T
func swapIfRequired<T: ByteSwappable>(_ x: T) -> T
func swapIfRequired<T>(_ x: T) -> T
func swapIfRequired<T: FixedWidthInteger>(array: inout [T])
func swapIfRequired<T: ByteSwappable>(array: inout [T])
func swapIfRequired<T>(array: inout [T])
func swapIfRequired<T: FixedWidthInteger>(buffer: UnsafeMutableBufferPointer<T>)
func swapIfRequired<T: ByteSwappable>(buffer: UnsafeMutableBufferPointer<T>)
func swapIfRequired<T>(buffer: UnsafeMutableBufferPointer<T>)
func swapIfRequired<T: FixedWidthInteger>(pointer: UnsafeMutablePointer<T>)
func swapIfRequired<T: ByteSwappable>(pointer: UnsafeMutablePointer<T>)
func swapIfRequired<T>(pointer: UnsafeMutablePointer<T>)
func fetch<T>(from addr: Address,
into buffer: UnsafeMutableBufferPointer<T>) throws
func fetch<T>(from addr: Address, into pointer: UnsafeMutablePointer<T>) throws
func fetch<T>(from addr: Address, count: Int, as: T.Type) throws -> [T]
func fetch<T>(from addr: Address, as type: T.Type) throws -> T
func fetchUnswapped<T>(from addr: Address,
into buffer: UnsafeMutableBufferPointer<T>) throws
func fetchUnswapped<T>(from addr: Address,
into pointer: UnsafeMutablePointer<T>) throws
func fetchUnswapped<T>(from addr: Address, count: Int, as: T.Type) throws -> [T]
func fetchUnswapped<T>(from addr: Address, as type: T.Type) throws -> T
func lookupSymbol(address: Address) -> ImageSymbol?
}
extension Image {
public func swapIfRequired<T: FixedWidthInteger>(_ x: T) -> T {
if shouldByteSwap {
return x.byteSwapped
}
return x
}
public func swapIfRequired<T: ByteSwappable>(_ x: T) -> T {
if shouldByteSwap {
return x.byteSwapped
}
return x
}
public func swapIfRequired<T>(_ x: T) -> T {
return x
}
public func swapIfRequired<T: ByteSwappable>(array: inout [T]) {
if shouldByteSwap {
array.swapBytes()
}
}
public func swapIfRequired<T: FixedWidthInteger>(array: inout [T]) {
if shouldByteSwap {
array.swapBytes()
}
}
public func swapIfRequired<T>(array: inout [T]) {
// Nothing to do
}
public func swapIfRequired<T: ByteSwappable>(buffer: UnsafeMutableBufferPointer<T>) {
if shouldByteSwap {
buffer.swapBytes()
}
}
public func swapIfRequired<T: FixedWidthInteger>(buffer: UnsafeMutableBufferPointer<T>) {
if shouldByteSwap {
buffer.swapBytes()
}
}
public func swapIfRequired<T>(buffer: UnsafeMutableBufferPointer<T>) {
// Nothing to do
}
public func swapIfRequired<T: ByteSwappable>(pointer: UnsafeMutablePointer<T>) {
if shouldByteSwap {
pointer.pointee = pointer.pointee.byteSwapped
}
}
public func swapIfRequired<T: FixedWidthInteger>(pointer: UnsafeMutablePointer<T>) {
if shouldByteSwap {
pointer.pointee = pointer.pointee.byteSwapped
}
}
public func swapIfRequired<T>(pointer: UnsafeMutablePointer<T>) {
// Nothing to do
}
public func fetchUnswapped<T>(from addr: Address,
into buffer: UnsafeMutableBufferPointer<T>) throws {
return try source.fetch(from: addr, into: buffer)
}
public func fetchUnswapped<T>(from addr: Address,
into pointer: UnsafeMutablePointer<T>) throws {
return try source.fetch(from: addr, into: pointer)
}
public func fetchUnswapped<T>(from addr: Address, count: Int, as type: T.Type) throws -> [T] {
return try source.fetch(from: addr, count: count, as: type)
}
public func fetchUnswapped<T>(from addr: Address, as type: T.Type) throws -> T {
return try source.fetch(from: addr, as: type)
}
public func fetch<T>(from addr: Address,
into buffer: UnsafeMutableBufferPointer<T>) throws {
try fetchUnswapped(from: addr, into: buffer)
swapIfRequired(buffer: buffer)
}
public func fetch<T>(from addr: Address,
into pointer: UnsafeMutablePointer<T>) throws {
try fetchUnswapped(from: addr, into: pointer)
swapIfRequired(pointer: pointer)
}
public func fetch<T>(from addr: Address, count: Int, as type: T.Type) throws -> [T]{
var result = try fetchUnswapped(from: addr, count: count, as: type)
swapIfRequired(array: &result)
return result
}
public func fetch<T>(from addr: Address, as type: T.Type) throws -> T {
return swapIfRequired(try fetchUnswapped(from: addr, as: type))
}
}