-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_editor.c
351 lines (285 loc) · 12.8 KB
/
hex_editor.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
#include <stdio.h>
#include <furi.h>
#include <gui/gui.h>
#include <gui/elements.h>
#include <dialogs/dialogs.h>
#include <input/input.h>
#include <notification/notification_messages.h>
#include <storage/storage.h>
#include <stream/stream.h>
#include <stream/buffered_file_stream.h>
#include <toolbox/stream/file_stream.h>
#include <hex_editor_icons.h>
// #include <assets_icons.h>
#define TAG "HexEditor"
#define STEP 6u
typedef struct {
uint32_t file_offset;
uint32_t file_read_bytes;
uint32_t file_size;
uint8_t string_offset;
char editable_char;
Stream* stream;
bool mode;
} HexEditorModel;
typedef struct {
HexEditorModel* model;
FuriMessageQueue* input_queue;
ViewPort* view_port;
Gui* gui;
Storage* storage;
FuriString* buffer;
} HexEditor;
static void draw_callback(Canvas* canvas, void* ctx) {
HexEditor* hex_editor = ctx;
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 10, "Line and mode:");
canvas_set_font(canvas, FontSecondary);
uint8_t com_str_offset = 0;
uint8_t local_offset = MAX(hex_editor->model->string_offset - 5, 0);
// TODO UTF ?
for(uint8_t i = 0; i < MIN((uint8_t)128 / STEP, furi_string_size(hex_editor->buffer)); i++) {
if(i + local_offset >= furi_string_size(hex_editor->buffer)) {
break;
}
char a = furi_string_get_char(hex_editor->buffer, i + local_offset);
canvas_draw_glyph(
canvas, 0 + com_str_offset + (STEP - canvas_glyph_width(canvas, a)) / 2, 20, a);
com_str_offset += STEP;
}
canvas_draw_icon(
canvas,
0 + MIN(hex_editor->model->string_offset, 5) * STEP - 4 + STEP / 2,
21,
&I_Pin_arrow_up_7x9);
if(hex_editor->model->mode) {
elements_button_left(canvas, "ASCII -");
elements_button_right(canvas, "ASCII +");
elements_button_center(canvas, "");
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 45, "edit");
} else {
elements_button_left(canvas, "");
elements_button_right(canvas, "");
elements_button_center(canvas, "chmod");
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 45, "seek");
}
canvas_draw_glyph(canvas, 30, 45, hex_editor->model->editable_char);
}
static void input_callback(InputEvent* input_event, void* ctx) {
// Проверяем, что контекст не нулевой
furi_assert(ctx);
HexEditor* hex_editor = ctx;
furi_message_queue_put(hex_editor->input_queue, input_event, 100);
}
static HexEditor* hex_editor_alloc() {
HexEditor* instance = malloc(sizeof(HexEditor));
instance->model = malloc(sizeof(HexEditorModel));
memset(instance->model, 0x0, sizeof(HexEditorModel));
instance->model->editable_char = ' ';
instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
instance->view_port = view_port_alloc();
view_port_draw_callback_set(instance->view_port, draw_callback, instance);
view_port_input_callback_set(instance->view_port, input_callback, instance);
instance->gui = furi_record_open(RECORD_GUI);
gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
instance->storage = furi_record_open(RECORD_STORAGE);
instance->buffer = furi_string_alloc();
return instance;
}
static void hex_editor_free(HexEditor* instance) {
furi_record_close(RECORD_STORAGE);
gui_remove_view_port(instance->gui, instance->view_port);
view_port_free(instance->view_port);
furi_record_close(RECORD_GUI);
furi_message_queue_free(instance->input_queue);
if(instance->model->stream) {
buffered_file_stream_close(instance->model->stream);
stream_free(instance->model->stream);
}
furi_string_free(instance->buffer);
free(instance->model);
free(instance);
}
static bool hex_editor_open_file(HexEditor* hex_editor, const char* file_path) {
furi_assert(hex_editor);
furi_assert(file_path);
hex_editor->model->stream = buffered_file_stream_alloc(hex_editor->storage);
if(!buffered_file_stream_open(
hex_editor->model->stream, file_path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Unable to open stream: %s", file_path);
return false;
};
hex_editor->model->file_size = stream_size(hex_editor->model->stream);
return true;
}
int32_t hex_editor_app(void* p) {
UNUSED(p);
HexEditor* hex_editor = hex_editor_alloc();
FuriString* file_path;
file_path = furi_string_alloc();
do {
if(p && strlen(p)) {
furi_string_set(file_path, (const char*)p);
} else {
// TODO bac to any
furi_string_set(file_path, "/any/nfc");
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, "*", &I_edit_10px);
browser_options.hide_ext = false;
browser_options.hide_dot_files = false;
DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
furi_record_close(RECORD_DIALOGS);
if(!res) {
FURI_LOG_I(TAG, "No file selected");
break;
}
}
FURI_LOG_I(TAG, "File selected: %s", furi_string_get_cstr(file_path));
if(!hex_editor_open_file(hex_editor, furi_string_get_cstr(file_path))) break;
if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
FURI_LOG_T(TAG, "No keys left in dict");
break;
}
InputEvent event;
int8_t offset_modifier;
while(1) {
// Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
// и проверяем, что у нас получилось это сделать
furi_check(
furi_message_queue_get(hex_editor->input_queue, &event, FuriWaitForever) ==
FuriStatusOk);
// Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
if(event.type == InputTypeShort || event.type == InputTypeRepeat) {
if(!hex_editor->model->mode) {
offset_modifier = 1;
if(event.type == InputTypeRepeat) {
offset_modifier = 3;
}
if(event.key == InputKeyRight) {
hex_editor->model->string_offset += offset_modifier;
if(hex_editor->model->string_offset >=
furi_string_size(hex_editor->buffer)) {
// dengeros
hex_editor->model->string_offset -=
furi_string_size(hex_editor->buffer);
}
}
if(event.key == InputKeyLeft) {
if(hex_editor->model->string_offset - offset_modifier < 0) {
// dengeros
hex_editor->model->string_offset +=
furi_string_size(hex_editor->buffer);
}
hex_editor->model->string_offset -= offset_modifier;
}
if(event.key == InputKeyDown) {
hex_editor->model->string_offset = 0;
if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
FURI_LOG_T(TAG, "No keys left in dict");
}
}
if(event.key == InputKeyUp) {
hex_editor->model->string_offset = 0;
if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
FURI_LOG_E(TAG, "Unable to seek stream");
break;
}
// NOT work on first line
stream_seek_to_char(
hex_editor->model->stream, '\n', StreamDirectionBackward);
if(!stream_seek_to_char(
hex_editor->model->stream, '\n', StreamDirectionBackward)) {
stream_rewind(hex_editor->model->stream);
} else {
if(!stream_seek(
hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
FURI_LOG_E(TAG, "Unable to seek stream");
break;
}
}
if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
FURI_LOG_T(TAG, "No keys left in dict");
break;
}
}
if(event.key == InputKeyOk) {
hex_editor->model->editable_char = furi_string_get_char(
hex_editor->buffer, hex_editor->model->string_offset);
hex_editor->model->mode = 1;
}
} else {
offset_modifier = 1;
if(event.type == InputTypeRepeat) {
offset_modifier = 4;
}
if(event.key == InputKeyRight) {
hex_editor->model->editable_char += offset_modifier;
}
if(event.key == InputKeyLeft) {
hex_editor->model->editable_char -= offset_modifier;
}
if(event.key == InputKeyOk) {
if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
FURI_LOG_E(TAG, "Unable to seek stream");
break;
}
if(!stream_seek_to_char(
hex_editor->model->stream, '\n', StreamDirectionBackward)) {
FURI_LOG_E(TAG, "Unable to stream_seek_to_char n");
// first line
stream_seek(hex_editor->model->stream, 0, StreamOffsetFromStart);
if(!stream_seek(
hex_editor->model->stream,
hex_editor->model->string_offset,
StreamOffsetFromCurrent)) {
FURI_LOG_E(TAG, "Unable to seek string_offset");
}
} else {
if(!stream_seek(
hex_editor->model->stream,
hex_editor->model->string_offset + 1,
StreamOffsetFromCurrent)) {
FURI_LOG_E(TAG, "Unable to seek string_offset +1");
}
}
if(!stream_write_char(
hex_editor->model->stream, hex_editor->model->editable_char)) {
FURI_LOG_E(TAG, "Unable to stream_write_char");
break;
}
hex_editor->model->editable_char = ' ';
hex_editor->model->mode = 0;
if(!stream_seek_to_char(
hex_editor->model->stream, '\n', StreamDirectionBackward)) {
FURI_LOG_E(TAG, "Unable to stream_seek_to_char n 2");
// first line
stream_seek(hex_editor->model->stream, 0, StreamOffsetFromStart);
} else {
if(!stream_seek(
hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
FURI_LOG_E(TAG, "Unable to seek stream");
break;
}
}
if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
FURI_LOG_T(TAG, "No keys left in dict");
break;
}
}
}
}
if(event.key == InputKeyBack) {
break;
}
// ?
view_port_update(hex_editor->view_port);
}
} while(false);
furi_string_free(file_path);
hex_editor_free(hex_editor);
return 0;
}