-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHexEditorView.swift
176 lines (163 loc) · 6.22 KB
/
HexEditorView.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
168
169
170
171
172
173
174
175
176
//
// HexEditorView.swift
//
// Created by Speedyfriend67 on 27.06.24
//
//
// HexEditorView.swift
//
// Created by Speedyfriend67 on 27.06.24
//
import SwiftUI
struct HexEditorView: View {
@State private var fileContent: [UInt8] = []
@State private var isLoading: Bool = true
@State private var showError: Bool = false
@State private var searchText: String = ""
@State private var jumpToAddress: String = ""
@State private var searchResults: [Int] = []
let fileURL: URL
var body: some View {
VStack {
if isLoading {
ProgressView("Loading...")
.padding()
} else if showError {
Text("Failed to load file.")
.foregroundColor(.red)
} else {
HeaderView(searchText: $searchText, jumpToAddress: $jumpToAddress, onSearch: search)
ScrollViewReader { scrollView in
ScrollView(.vertical) {
LazyVStack(alignment: .leading, spacing: 1) {
ForEach(0..<fileContent.count / 8, id: \.self) { row in
let address = row * 8
RowView(index: address, fileContent: $fileContent, searchResults: searchResults)
.id(address)
}
}
.padding(.horizontal, 5)
}
.background(Color.black)
.onChange(of: jumpToAddress) { newValue in
if let address = Int(newValue, radix: 16) {
withAnimation {
scrollView.scrollTo(address, anchor: .top)
}
}
}
}
}
}
.onAppear(perform: loadFile)
.navigationTitle(fileURL.lastPathComponent)
.navigationBarTitleDisplayMode(.inline)
}
private func loadFile() {
DispatchQueue.global(qos: .userInitiated).async {
do {
let data = try Data(contentsOf: fileURL)
DispatchQueue.main.async {
self.fileContent = Array(data)
self.isLoading = false
}
} catch {
DispatchQueue.main.async {
self.isLoading = false
self.showError = true
}
}
}
}
private func search() {
guard !searchText.isEmpty else {
searchResults = []
return
}
let searchBytes = Array(searchText.utf8)
searchResults = []
for index in 0...(fileContent.count - searchBytes.count) {
if Array(fileContent[index..<(index + searchBytes.count)]) == searchBytes {
searchResults.append(contentsOf: index..<(index + searchBytes.count))
}
}
}
}
struct HeaderView: View {
@Binding var searchText: String
@Binding var jumpToAddress: String
var onSearch: () -> Void
var body: some View {
HStack {
TextField("Start search here", text: $searchText, onCommit: onSearch)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
TextField("Go to address", text: $jumpToAddress)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
Button(action: {
if Int(jumpToAddress, radix: 16) != nil {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) // Dismiss the keyboard
}
onSearch()
}) {
Image(systemName: "magnifyingglass")
}
.padding(.horizontal)
}
}
}
struct RowView: View {
let index: Int
@Binding var fileContent: [UInt8]
let searchResults: [Int]
var body: some View {
HStack(spacing: 1) {
Text(String(format: "%08X", index))
.foregroundColor(.blue)
.frame(width: 60, alignment: .leading)
ForEach(0..<8, id: \.self) { offset in
let currentIndex = index + offset
if currentIndex < fileContent.count {
TextField("", text: Binding(
get: {
String(format: "%02X", fileContent[currentIndex])
},
set: { newValue in
if let byte = UInt8(newValue, radix: 16) {
fileContent[currentIndex] = byte
}
}
))
.foregroundColor(searchResults.contains(currentIndex) ? .yellow : .white)
.frame(width: 24, alignment: .leading)
.textFieldStyle(PlainTextFieldStyle())
.font(.system(size: 12, weight: .regular, design: .monospaced))
} else {
Text(" ")
.frame(width: 24, alignment: .leading)
}
}
Spacer()
ForEach(0..<8, id: \.self) { offset in
let currentIndex = index + offset
if currentIndex < fileContent.count {
let char = fileContent[currentIndex] >= 32 && fileContent[currentIndex] < 127 ? Character(UnicodeScalar(fileContent[currentIndex])) : "."
Text(String(char))
.foregroundColor(searchResults.contains(currentIndex) ? .yellow : .blue)
.frame(width: 12, alignment: .leading)
.font(.system(size: 12, weight: .regular, design: .monospaced))
} else {
Text(" ")
.frame(width: 12, alignment: .leading)
}
}
}
.font(.system(size: 12, weight: .regular, design: .monospaced))
}
}
struct HexEditorView_Previews: PreviewProvider {
static var previews: some View {
HexEditorView(fileURL: URL(fileURLWithPath: "/path/to/file"))
}
}