@@ -13,30 +13,9 @@ internal import FileIO
1313@_implementationOnly import FileIO
1414#endif
1515
16- public protocol StringTable < Encoding> : Sequence < StringTableEntry > {
17- associatedtype Encoding : _UnicodeEncoding
18- }
19-
2016extension ELFFile {
17+ public typealias UnicodeStrings = BinaryParseSupport . UnicodeStrings
2118 public typealias Strings = UnicodeStrings < UTF8 >
22-
23- public struct UnicodeStrings < Encoding: _UnicodeEncoding > : StringTable {
24- typealias FileSlice = File . FileSlice
25-
26- private let fileSlice : FileSlice
27-
28- /// file offset of string table start
29- public let offset : Int
30-
31- /// size of string table
32- public let size : Int
33-
34- public let isSwapped : Bool
35-
36- public func makeIterator( ) -> Iterator {
37- . init( fileSlice: fileSlice, isSwapped: isSwapped)
38- }
39- }
4019}
4120
4221extension ELFFile . UnicodeStrings {
@@ -51,95 +30,10 @@ extension ELFFile.UnicodeStrings {
5130 length: size
5231 )
5332 self . init (
54- fileSlice : fileSlice,
33+ source : fileSlice,
5534 offset: offset,
5635 size: size,
5736 isSwapped: isSwapped
5837 )
5938 }
6039}
61-
62- extension ELFFile . UnicodeStrings {
63- public var data : Data ? {
64- try ? fileSlice. readAllData ( )
65- }
66- }
67-
68- extension ELFFile . UnicodeStrings {
69- public struct Iterator : IteratorProtocol {
70- public typealias Element = StringTableEntry
71-
72- private let fileSice : FileSlice
73- private let tableSize : Int
74- private let isSwapped : Bool
75-
76- private var nextOffset : Int
77-
78- init ( fileSlice: FileSlice , isSwapped: Bool ) {
79- self . fileSice = fileSlice
80- self . tableSize = fileSlice. size
81- self . nextOffset = 0
82- self . isSwapped = isSwapped
83- }
84-
85- public mutating func next( ) -> Element ? {
86- guard nextOffset < tableSize else { return nil }
87-
88- let ptr = UnsafeRawPointer ( fileSice. ptr)
89- . advanced ( by: nextOffset)
90- . assumingMemoryBound ( to: Encoding . CodeUnit. self)
91- var ( string, offset) = ptr. readString ( as: Encoding . self)
92-
93- defer {
94- nextOffset += offset
95- }
96-
97- if isSwapped || shouldSwap ( ptr) {
98- let data = Data ( bytes: ptr, count: offset)
99- . byteSwapped ( Encoding . CodeUnit. self)
100- string = data. withUnsafeBytes {
101- let baseAddress = $0. baseAddress!
102- . assumingMemoryBound ( to: Encoding . CodeUnit. self)
103- return . init(
104- decodingCString: baseAddress,
105- as: Encoding . self
106- )
107- }
108- }
109-
110- return . init(
111- string: string,
112- offset: nextOffset
113- )
114- }
115- }
116- }
117-
118- extension ELFFile . Strings {
119- func string( at offset: Int ) -> Element ? {
120- guard fileSlice. size >= offset else { return nil }
121- let string = String (
122- cString: fileSlice. ptr
123- . advanced ( by: offset)
124- . assumingMemoryBound ( to: CChar . self)
125- )
126- return . init( string: string, offset: offset)
127- }
128- }
129-
130- extension ELFFile . UnicodeStrings . Iterator {
131- // https://github.com/swiftlang/swift-corelibs-foundation/blob/4a9694d396b34fb198f4c6dd865702f7dc0b0dcf/Sources/Foundation/NSString.swift#L1390
132- func shouldSwap( _ ptr: UnsafePointer < Encoding . CodeUnit > ) -> Bool {
133- let size = MemoryLayout< Encoding . CodeUnit> . size
134- switch size {
135- case 1 :
136- return false
137- case 2 :
138- return ptr. pointee == 0xFFFE /* ZERO WIDTH NO-BREAK SPACE (swapped) */
139- case 4 :
140- return ptr. pointee == UInt32 ( 0xFFFE0000 ) // avoid overflows in 32bit env
141- default :
142- return false
143- }
144- }
145- }
0 commit comments