hex_editor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <gui/elements.h>
  5. #include <dialogs/dialogs.h>
  6. #include <input/input.h>
  7. #include <notification/notification_messages.h>
  8. #include <storage/storage.h>
  9. #include <stream/stream.h>
  10. #include <stream/buffered_file_stream.h>
  11. #include <toolbox/stream/file_stream.h>
  12. #include <hex_editor_icons.h>
  13. // #include <assets_icons.h>
  14. #define TAG "HexEditor"
  15. #define STEP 6u
  16. typedef struct {
  17. uint32_t file_offset;
  18. uint32_t file_read_bytes;
  19. uint32_t file_size;
  20. uint8_t string_offset;
  21. char editable_char;
  22. Stream* stream;
  23. bool mode;
  24. } HexEditorModel;
  25. typedef struct {
  26. HexEditorModel* model;
  27. FuriMessageQueue* input_queue;
  28. ViewPort* view_port;
  29. Gui* gui;
  30. Storage* storage;
  31. FuriString* buffer;
  32. } HexEditor;
  33. static void draw_callback(Canvas* canvas, void* ctx) {
  34. HexEditor* hex_editor = ctx;
  35. canvas_clear(canvas);
  36. canvas_set_font(canvas, FontPrimary);
  37. canvas_draw_str(canvas, 0, 10, "Line and mode:");
  38. canvas_set_font(canvas, FontSecondary);
  39. uint8_t com_str_offset = 0;
  40. uint8_t local_offset = MAX(hex_editor->model->string_offset - 5, 0);
  41. // TODO UTF ?
  42. for(uint8_t i = 0; i < MIN((uint8_t)128 / STEP, furi_string_size(hex_editor->buffer)); i++) {
  43. if(i + local_offset >= furi_string_size(hex_editor->buffer)) {
  44. break;
  45. }
  46. char a = furi_string_get_char(hex_editor->buffer, i + local_offset);
  47. canvas_draw_glyph(
  48. canvas, 0 + com_str_offset + (STEP - canvas_glyph_width(canvas, a)) / 2, 20, a);
  49. com_str_offset += STEP;
  50. }
  51. canvas_draw_icon(
  52. canvas,
  53. 0 + MIN(hex_editor->model->string_offset, 5) * STEP - 4 + STEP / 2,
  54. 21,
  55. &I_Pin_arrow_up_7x9);
  56. if(hex_editor->model->mode) {
  57. elements_button_left(canvas, "ASCII -");
  58. elements_button_right(canvas, "ASCII +");
  59. elements_button_center(canvas, "");
  60. canvas_set_font(canvas, FontPrimary);
  61. canvas_draw_str(canvas, 0, 45, "edit");
  62. } else {
  63. elements_button_left(canvas, "");
  64. elements_button_right(canvas, "");
  65. elements_button_center(canvas, "chmod");
  66. canvas_set_font(canvas, FontPrimary);
  67. canvas_draw_str(canvas, 0, 45, "seek");
  68. }
  69. canvas_draw_glyph(canvas, 30, 45, hex_editor->model->editable_char);
  70. }
  71. static void input_callback(InputEvent* input_event, void* ctx) {
  72. // Проверяем, что контекст не нулевой
  73. furi_assert(ctx);
  74. HexEditor* hex_editor = ctx;
  75. furi_message_queue_put(hex_editor->input_queue, input_event, 100);
  76. }
  77. static HexEditor* hex_editor_alloc() {
  78. HexEditor* instance = malloc(sizeof(HexEditor));
  79. instance->model = malloc(sizeof(HexEditorModel));
  80. memset(instance->model, 0x0, sizeof(HexEditorModel));
  81. instance->model->editable_char = ' ';
  82. instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  83. instance->view_port = view_port_alloc();
  84. view_port_draw_callback_set(instance->view_port, draw_callback, instance);
  85. view_port_input_callback_set(instance->view_port, input_callback, instance);
  86. instance->gui = furi_record_open(RECORD_GUI);
  87. gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
  88. instance->storage = furi_record_open(RECORD_STORAGE);
  89. instance->buffer = furi_string_alloc();
  90. return instance;
  91. }
  92. static void hex_editor_free(HexEditor* instance) {
  93. furi_record_close(RECORD_STORAGE);
  94. gui_remove_view_port(instance->gui, instance->view_port);
  95. view_port_free(instance->view_port);
  96. furi_record_close(RECORD_GUI);
  97. furi_message_queue_free(instance->input_queue);
  98. if(instance->model->stream) {
  99. buffered_file_stream_close(instance->model->stream);
  100. stream_free(instance->model->stream);
  101. }
  102. furi_string_free(instance->buffer);
  103. free(instance->model);
  104. free(instance);
  105. }
  106. static bool hex_editor_open_file(HexEditor* hex_editor, const char* file_path) {
  107. furi_assert(hex_editor);
  108. furi_assert(file_path);
  109. hex_editor->model->stream = buffered_file_stream_alloc(hex_editor->storage);
  110. if(!buffered_file_stream_open(
  111. hex_editor->model->stream, file_path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  112. FURI_LOG_E(TAG, "Unable to open stream: %s", file_path);
  113. return false;
  114. };
  115. hex_editor->model->file_size = stream_size(hex_editor->model->stream);
  116. return true;
  117. }
  118. int32_t hex_editor_app(void* p) {
  119. UNUSED(p);
  120. HexEditor* hex_editor = hex_editor_alloc();
  121. FuriString* file_path;
  122. file_path = furi_string_alloc();
  123. do {
  124. if(p && strlen(p)) {
  125. furi_string_set(file_path, (const char*)p);
  126. } else {
  127. // TODO bac to any
  128. furi_string_set(file_path, "/any/nfc");
  129. DialogsFileBrowserOptions browser_options;
  130. dialog_file_browser_set_basic_options(&browser_options, "*", &I_edit_10px);
  131. browser_options.hide_ext = false;
  132. browser_options.hide_dot_files = false;
  133. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  134. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  135. furi_record_close(RECORD_DIALOGS);
  136. if(!res) {
  137. FURI_LOG_I(TAG, "No file selected");
  138. break;
  139. }
  140. }
  141. FURI_LOG_I(TAG, "File selected: %s", furi_string_get_cstr(file_path));
  142. if(!hex_editor_open_file(hex_editor, furi_string_get_cstr(file_path))) break;
  143. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  144. FURI_LOG_T(TAG, "No keys left in dict");
  145. break;
  146. }
  147. InputEvent event;
  148. int8_t offset_modifier;
  149. while(1) {
  150. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  151. // и проверяем, что у нас получилось это сделать
  152. furi_check(
  153. furi_message_queue_get(hex_editor->input_queue, &event, FuriWaitForever) ==
  154. FuriStatusOk);
  155. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  156. if(event.type == InputTypeShort || event.type == InputTypeRepeat) {
  157. if(!hex_editor->model->mode) {
  158. offset_modifier = 1;
  159. if(event.type == InputTypeRepeat) {
  160. offset_modifier = 3;
  161. }
  162. if(event.key == InputKeyRight) {
  163. hex_editor->model->string_offset += offset_modifier;
  164. if(hex_editor->model->string_offset >=
  165. furi_string_size(hex_editor->buffer)) {
  166. // dengeros
  167. hex_editor->model->string_offset -=
  168. furi_string_size(hex_editor->buffer);
  169. }
  170. }
  171. if(event.key == InputKeyLeft) {
  172. if(hex_editor->model->string_offset - offset_modifier < 0) {
  173. // dengeros
  174. hex_editor->model->string_offset +=
  175. furi_string_size(hex_editor->buffer);
  176. }
  177. hex_editor->model->string_offset -= offset_modifier;
  178. }
  179. if(event.key == InputKeyDown) {
  180. hex_editor->model->string_offset = 0;
  181. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  182. FURI_LOG_T(TAG, "No keys left in dict");
  183. }
  184. }
  185. if(event.key == InputKeyUp) {
  186. hex_editor->model->string_offset = 0;
  187. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  188. FURI_LOG_E(TAG, "Unable to seek stream");
  189. break;
  190. }
  191. // NOT work on first line
  192. stream_seek_to_char(
  193. hex_editor->model->stream, '\n', StreamDirectionBackward);
  194. if(!stream_seek_to_char(
  195. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  196. stream_rewind(hex_editor->model->stream);
  197. } else {
  198. if(!stream_seek(
  199. hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  200. FURI_LOG_E(TAG, "Unable to seek stream");
  201. break;
  202. }
  203. }
  204. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  205. FURI_LOG_T(TAG, "No keys left in dict");
  206. break;
  207. }
  208. }
  209. if(event.key == InputKeyOk) {
  210. hex_editor->model->editable_char = furi_string_get_char(
  211. hex_editor->buffer, hex_editor->model->string_offset);
  212. hex_editor->model->mode = 1;
  213. }
  214. } else {
  215. offset_modifier = 1;
  216. if(event.type == InputTypeRepeat) {
  217. offset_modifier = 4;
  218. }
  219. if(event.key == InputKeyRight) {
  220. hex_editor->model->editable_char += offset_modifier;
  221. }
  222. if(event.key == InputKeyLeft) {
  223. hex_editor->model->editable_char -= offset_modifier;
  224. }
  225. if(event.key == InputKeyOk) {
  226. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  227. FURI_LOG_E(TAG, "Unable to seek stream");
  228. break;
  229. }
  230. if(!stream_seek_to_char(
  231. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  232. FURI_LOG_E(TAG, "Unable to stream_seek_to_char n");
  233. // first line
  234. stream_seek(hex_editor->model->stream, 0, StreamOffsetFromStart);
  235. if(!stream_seek(
  236. hex_editor->model->stream,
  237. hex_editor->model->string_offset,
  238. StreamOffsetFromCurrent)) {
  239. FURI_LOG_E(TAG, "Unable to seek string_offset");
  240. }
  241. } else {
  242. if(!stream_seek(
  243. hex_editor->model->stream,
  244. hex_editor->model->string_offset + 1,
  245. StreamOffsetFromCurrent)) {
  246. FURI_LOG_E(TAG, "Unable to seek string_offset +1");
  247. }
  248. }
  249. if(!stream_write_char(
  250. hex_editor->model->stream, hex_editor->model->editable_char)) {
  251. FURI_LOG_E(TAG, "Unable to stream_write_char");
  252. break;
  253. }
  254. hex_editor->model->editable_char = ' ';
  255. hex_editor->model->mode = 0;
  256. if(!stream_seek_to_char(
  257. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  258. FURI_LOG_E(TAG, "Unable to stream_seek_to_char n 2");
  259. // first line
  260. stream_seek(hex_editor->model->stream, 0, StreamOffsetFromStart);
  261. } else {
  262. if(!stream_seek(
  263. hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  264. FURI_LOG_E(TAG, "Unable to seek stream");
  265. break;
  266. }
  267. }
  268. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  269. FURI_LOG_T(TAG, "No keys left in dict");
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. if(event.key == InputKeyBack) {
  276. break;
  277. }
  278. // ?
  279. view_port_update(hex_editor->view_port);
  280. }
  281. } while(false);
  282. furi_string_free(file_path);
  283. hex_editor_free(hex_editor);
  284. return 0;
  285. }