hex_editor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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) buffered_file_stream_close(instance->model->stream);
  99. furi_string_free(instance->buffer);
  100. free(instance->model);
  101. free(instance);
  102. }
  103. static bool hex_editor_open_file(HexEditor* hex_editor, const char* file_path) {
  104. furi_assert(hex_editor);
  105. furi_assert(file_path);
  106. hex_editor->model->stream = buffered_file_stream_alloc(hex_editor->storage);
  107. if(!buffered_file_stream_open(
  108. hex_editor->model->stream, file_path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  109. FURI_LOG_E(TAG, "Unable to open stream: %s", file_path);
  110. return false;
  111. };
  112. hex_editor->model->file_size = stream_size(hex_editor->model->stream);
  113. return true;
  114. }
  115. int32_t hex_editor_app(void* p) {
  116. UNUSED(p);
  117. HexEditor* hex_editor = hex_editor_alloc();
  118. FuriString* file_path;
  119. file_path = furi_string_alloc();
  120. do {
  121. if(p && strlen(p)) {
  122. furi_string_set(file_path, (const char*)p);
  123. } else {
  124. // TODO bac to any
  125. furi_string_set(file_path, "/any/nfc");
  126. DialogsFileBrowserOptions browser_options;
  127. dialog_file_browser_set_basic_options(&browser_options, "*", &I_edit_10px);
  128. browser_options.hide_ext = false;
  129. browser_options.hide_dot_files = false;
  130. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  131. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  132. furi_record_close(RECORD_DIALOGS);
  133. if(!res) {
  134. FURI_LOG_I(TAG, "No file selected");
  135. break;
  136. }
  137. }
  138. FURI_LOG_I(TAG, "File selected: %s", furi_string_get_cstr(file_path));
  139. if(!hex_editor_open_file(hex_editor, furi_string_get_cstr(file_path))) break;
  140. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  141. FURI_LOG_T(TAG, "No keys left in dict");
  142. break;
  143. }
  144. InputEvent event;
  145. int8_t offset_modifier;
  146. while(1) {
  147. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  148. // и проверяем, что у нас получилось это сделать
  149. furi_check(
  150. furi_message_queue_get(hex_editor->input_queue, &event, FuriWaitForever) ==
  151. FuriStatusOk);
  152. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  153. if(event.type == InputTypeShort || event.type == InputTypeRepeat) {
  154. if(!hex_editor->model->mode) {
  155. offset_modifier = 1;
  156. if(event.type == InputTypeRepeat) {
  157. offset_modifier = 3;
  158. }
  159. if(event.key == InputKeyRight) {
  160. hex_editor->model->string_offset += offset_modifier;
  161. if(hex_editor->model->string_offset >=
  162. furi_string_size(hex_editor->buffer)) {
  163. // dengeros
  164. hex_editor->model->string_offset -=
  165. furi_string_size(hex_editor->buffer);
  166. }
  167. }
  168. if(event.key == InputKeyLeft) {
  169. if(hex_editor->model->string_offset - offset_modifier < 0) {
  170. // dengeros
  171. hex_editor->model->string_offset +=
  172. furi_string_size(hex_editor->buffer);
  173. }
  174. hex_editor->model->string_offset -= offset_modifier;
  175. }
  176. if(event.key == InputKeyDown) {
  177. hex_editor->model->string_offset = 0;
  178. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  179. FURI_LOG_T(TAG, "No keys left in dict");
  180. }
  181. }
  182. if(event.key == InputKeyUp) {
  183. hex_editor->model->string_offset = 0;
  184. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  185. FURI_LOG_E(TAG, "Unable to seek stream");
  186. break;
  187. }
  188. // NOT work on first line
  189. stream_seek_to_char(
  190. hex_editor->model->stream, '\n', StreamDirectionBackward);
  191. if(!stream_seek_to_char(
  192. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  193. stream_rewind(hex_editor->model->stream);
  194. } else {
  195. if(!stream_seek(
  196. hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  197. FURI_LOG_E(TAG, "Unable to seek stream");
  198. break;
  199. }
  200. }
  201. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  202. FURI_LOG_T(TAG, "No keys left in dict");
  203. break;
  204. }
  205. }
  206. if(event.key == InputKeyOk) {
  207. hex_editor->model->editable_char = furi_string_get_char(
  208. hex_editor->buffer, hex_editor->model->string_offset);
  209. hex_editor->model->mode = 1;
  210. }
  211. } else {
  212. offset_modifier = 1;
  213. if(event.type == InputTypeRepeat) {
  214. offset_modifier = 4;
  215. }
  216. if(event.key == InputKeyRight) {
  217. hex_editor->model->editable_char += offset_modifier;
  218. }
  219. if(event.key == InputKeyLeft) {
  220. hex_editor->model->editable_char -= offset_modifier;
  221. }
  222. if(event.key == InputKeyOk) {
  223. if(!stream_seek(hex_editor->model->stream, -1, StreamOffsetFromCurrent)) {
  224. FURI_LOG_E(TAG, "Unable to seek stream");
  225. break;
  226. }
  227. if(!stream_seek_to_char(
  228. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  229. FURI_LOG_E(TAG, "Unable to stream_seek_to_char n");
  230. // first line
  231. stream_seek(hex_editor->model->stream, 0, StreamOffsetFromStart);
  232. if(!stream_seek(
  233. hex_editor->model->stream,
  234. hex_editor->model->string_offset,
  235. StreamOffsetFromCurrent)) {
  236. FURI_LOG_E(TAG, "Unable to seek string_offset");
  237. }
  238. } else {
  239. if(!stream_seek(
  240. hex_editor->model->stream,
  241. hex_editor->model->string_offset + 1,
  242. StreamOffsetFromCurrent)) {
  243. FURI_LOG_E(TAG, "Unable to seek string_offset +1");
  244. }
  245. }
  246. if(!stream_write_char(
  247. hex_editor->model->stream, hex_editor->model->editable_char)) {
  248. FURI_LOG_E(TAG, "Unable to stream_write_char");
  249. break;
  250. }
  251. hex_editor->model->editable_char = ' ';
  252. hex_editor->model->mode = 0;
  253. if(!stream_seek_to_char(
  254. hex_editor->model->stream, '\n', StreamDirectionBackward)) {
  255. FURI_LOG_E(TAG, "Unable to stream_seek_to_char n 2");
  256. // first line
  257. stream_seek(hex_editor->model->stream, 0, StreamOffsetFromStart);
  258. } else {
  259. if(!stream_seek(
  260. hex_editor->model->stream, 1, StreamOffsetFromCurrent)) {
  261. FURI_LOG_E(TAG, "Unable to seek stream");
  262. break;
  263. }
  264. }
  265. if(!stream_read_line(hex_editor->model->stream, hex_editor->buffer)) {
  266. FURI_LOG_T(TAG, "No keys left in dict");
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. if(event.key == InputKeyBack) {
  273. break;
  274. }
  275. // ?
  276. view_port_update(hex_editor->view_port);
  277. }
  278. } while(false);
  279. furi_string_free(file_path);
  280. hex_editor_free(hex_editor);
  281. return 0;
  282. }