forked from TheOfficialFloW/VitaShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.c
369 lines (291 loc) · 9.26 KB
/
hex.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
VitaShell
Copyright (C) 2015-2018, TheFloW
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "archive.h"
#include "file.h"
#include "text.h"
#include "hex.h"
#include "message_dialog.h"
#include "theme.h"
#include "language.h"
#include "utils.h"
static void hexListAddEntry(HexList *list, HexListEntry *entry) {
entry->next = NULL;
entry->previous = NULL;
if (list->head == NULL) {
list->head = entry;
list->tail = entry;
} else {
HexListEntry *tail = list->tail;
tail->next = entry;
entry->previous = tail;
list->tail = entry;
}
list->length++;
}
static void hexListEmpty(HexList *list) {
HexListEntry *entry = list->head;
while (entry) {
HexListEntry *next = entry->next;
free(entry);
entry = next;
}
list->head = NULL;
list->tail = NULL;
list->length = 0;
}
static HexListEntry *hexListGetNthEntry(HexList *list, int n) {
HexListEntry *entry = list->head;
while (n > 0 && entry) {
n--;
entry = entry->next;
}
if (n != 0)
return NULL;
return entry;
}
int hexViewer(const char *file) {
int text_viewer = 0;
uint8_t *buffer = memalign(4096, BIG_BUFFER_SIZE);
if (!buffer)
return -1;
int size = 0;
if (isInArchive()) {
size = ReadArchiveFile(file, buffer, BIG_BUFFER_SIZE);
} else {
size = ReadFile(file, buffer, BIG_BUFFER_SIZE);
}
if (size <= 0) {
free(buffer);
return size;
}
int modify_allowed = 1;
if (isInArchive()) {
modify_allowed = 0;
}
int changed = 0;
int base_pos = 0, rel_pos = 0;
uint8_t nibble_pos = 0;
HexList list;
memset(&list, 0, sizeof(HexList));
int i;
for (i = 0; i < 0x10; i++) {
HexListEntry *entry = malloc(sizeof(HexListEntry));
memcpy(entry->data, buffer + i * 0x10, 0x10);
hexListAddEntry(&list, entry);
}
while (1) {
readPad();
if (!isMessageDialogRunning()) {
if (hold_pad[PAD_UP] || hold2_pad[PAD_LEFT_ANALOG_UP]) {
if (rel_pos > 0) {
rel_pos -= 0x10;
} else if (base_pos > 0) {
base_pos -= 0x10;
// Tail to head
list.tail->next = list.head;
list.head->previous = list.tail;
list.head = list.tail;
// Second last to tail
list.tail = list.tail->previous;
list.tail->next = NULL;
// No previous
list.head->previous = NULL;
// Read
memcpy(list.head->data, buffer+base_pos, 0x10);
}
} else if (hold_pad[PAD_DOWN] || hold2_pad[PAD_LEFT_ANALOG_DOWN]) {
if ((rel_pos+0x10) < size) {
if ((rel_pos+0x10) < ((MAX_POSITION - 1) * 0x10)) {
rel_pos += 0x10;
} else if ((base_pos + rel_pos+0x10) < size) {
base_pos += 0x10;
// Head to tail
list.head->previous = list.tail;
list.tail->next = list.head;
list.tail = list.head;
// Second first to head
list.head = list.head->next;
list.head->previous = NULL;
// No next
list.tail->next = NULL;
// Read
memcpy(list.tail->data, buffer+base_pos + (0x10 - 1) * 0x10, 0x10);
}
}
}
// Page skip
if (hold_pad[PAD_LTRIGGER]) {
if ((base_pos + rel_pos) != 0) {
if ((base_pos-0x10*0x10) >= 0) {
base_pos -= 0x10*0x10;
} else {
base_pos = 0;
rel_pos = 0;
}
HexListEntry *entry = list.head;
int i;
for (i = 0; i < 0x10; i++) {
memcpy(entry->data, buffer + base_pos + i*0x10, 0x10);
entry = entry->next;
}
}
}
if (hold_pad[PAD_RTRIGGER]) {
if (size >= 0xF0) {
if ((base_pos + rel_pos+0x1F0) < size) {
base_pos += 0x10*0x10;
} else {
base_pos = ALIGN(size, 0x10) - 0xF0;
rel_pos = 0xE0;
}
HexListEntry *entry = list.head;
int i;
for (i = 0; i < 0x10; i++) {
memcpy(entry->data, buffer + base_pos + i*0x10, 0x10);
entry = entry->next;
}
}
}
uint8_t max_nibble = (2*0x10) - 1;
// Last line
if ((base_pos + rel_pos+0x10) >= size) {
uint8_t rest = size % 0x10;
if (rest == 0)
rest = 0x10;
max_nibble = 2*rest - 1;
}
if (nibble_pos > max_nibble) {
nibble_pos = max_nibble;
}
if (hold_pad[PAD_LEFT] || hold2_pad[PAD_LEFT_ANALOG_LEFT]) {
if (nibble_pos > 0)
nibble_pos--;
} else if (hold_pad[PAD_RIGHT] || hold2_pad[PAD_LEFT_ANALOG_RIGHT]) {
if (nibble_pos < max_nibble)
nibble_pos++;
}
// Cancel or switch to text viewer
if (pressed_pad[PAD_CANCEL] || pressed_pad[PAD_SQUARE]) {
if (pressed_pad[PAD_CANCEL]) {
text_viewer = 0;
} else {
text_viewer = 1;
}
if (changed) {
initMessageDialog(SCE_MSG_DIALOG_BUTTON_TYPE_YESNO, language_container[SAVE_MODIFICATIONS]);
} else {
break;
}
}
// Increase nibble
if (modify_allowed && hold_pad[PAD_ENTER]) {
changed = 1;
int cur_pos = rel_pos + base_pos + nibble_pos / 2;
uint8_t ch = buffer[cur_pos];
uint8_t high_nibble = (ch >> 4) & 0xF;
uint8_t low_nibble = ch & 0xF;
int low = (nibble_pos % 2);
uint8_t nibble = low ? (ch & 0xF) : ((ch >> 4) & 0xF);
if (nibble < 0xF) {
nibble++;
} else {
nibble = 0;
}
HexListEntry *entry = hexListGetNthEntry(&list, rel_pos / 0x10);
if (low) {
uint8_t byte = (high_nibble << 4) | nibble;
buffer[cur_pos] = byte;
entry->data[nibble_pos/2] = byte;
} else {
uint8_t byte = (nibble << 4) | low_nibble;
buffer[cur_pos] = byte;
entry->data[nibble_pos/2] = byte;
}
}
} else {
int msg_result = updateMessageDialog();
if (msg_result == MESSAGE_DIALOG_RESULT_YES) {
SceUID fd = sceIoOpen(file, SCE_O_WRONLY, 0777);
if (fd >= 0) {
sceIoWrite(fd, buffer, size);
sceIoClose(fd);
}
break;
} else if (msg_result == MESSAGE_DIALOG_RESULT_NO) {
break;
}
}
// Start drawing
startDrawing(bg_hex_image);
// Draw shell info
drawShellInfo(file);
// Draw scroll bar
int pos = (base_pos - (base_pos % 0x10)) / 0x10 + ((base_pos % 0x10) ? 1 : 0);
int n_lines = (size - (size % 0x10)) / 0x10 + ((size % 0x10) ? 1 : 0);
drawScrollBar(pos, n_lines);
// Offset/size
pgf_draw_textf(HEX_CHAR_X, START_Y, HEX_OFFSET_COLOR, "%08X/%08X", rel_pos+base_pos, size);
// Offset x
pgf_draw_text(SHELL_MARGIN_X, START_Y, HEX_OFFSET_COLOR, language_container[OFFSET]);
int x;
for (x = 0; x < 0x10; x++) {
pgf_draw_textf(HEX_OFFSET_X + (x * HEX_OFFSET_SPACE), START_Y, HEX_OFFSET_COLOR, "%02X", x);
}
HexListEntry *entry = list.head;
int y;
for (y = 0; y < 0x10; y++) {
int x;
for (x = 0; x < 0x10; x++) {
uint8_t nibble_x = x * 2;
uint8_t ch = entry->data[x];
uint32_t offset = base_pos + x + y*0x10;
if (offset >= size)
break;
uint32_t color = HEX_COLOR;
int on_line = 0;
if (rel_pos == (y * 0x10)) {
color = FOCUS_COLOR;
on_line = 1;
}
// Character hex
uint8_t high_nibble = (ch >> 4) & 0xF;
uint8_t low_nibble = ch & 0xF;
int w = pgf_draw_textf(HEX_OFFSET_X + (x * HEX_OFFSET_SPACE), START_Y + ((y + 1) * FONT_Y_SPACE), (on_line && nibble_x == nibble_pos) ? HEX_NIBBLE_COLOR : color, "%01X", high_nibble);
pgf_draw_textf(HEX_OFFSET_X + (x * HEX_OFFSET_SPACE) + w, START_Y + ((y + 1) * FONT_Y_SPACE), (on_line && (nibble_x + 1) == nibble_pos) ? HEX_NIBBLE_COLOR : color, "%01X", low_nibble);
// Character
ch = (ch >= 0x20) ? ch : '.';
int width = font_size_cache[(int)ch];
uint8_t byte_nibble_pos = nibble_pos - (nibble_pos % 2);
pgf_draw_textf(HEX_CHAR_X + (x * FONT_X_SPACE) + (FONT_X_SPACE - width) / 2.0f, START_Y + ((y + 1) * FONT_Y_SPACE), (on_line && nibble_x == byte_nibble_pos) ? HEX_NIBBLE_COLOR : color, "%c", ch);
}
// Offset y
if (x > 0)
pgf_draw_textf(SHELL_MARGIN_X, START_Y + ((y + 1) * FONT_Y_SPACE), HEX_OFFSET_COLOR, "%08X", base_pos + (y * 0x10));
// It's the end, break
if (x < 0x10)
break;
// Next
entry = entry->next;
}
// End drawing
endDrawing();
}
hexListEmpty(&list);
free(buffer);
if (text_viewer)
textViewer(file);
return 0;
}