subbrute_main_view.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #include "subbrute_main_view.h"
  2. #include "../subbrute_i.h"
  3. #include "../helpers/gui_top_buttons.h"
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #define STATUS_BAR_Y_SHIFT 14
  7. #define TAG "SubBruteMainView"
  8. #define ITEMS_ON_SCREEN 3
  9. #define ITEMS_INTERVAL 1
  10. #define ITEM_WIDTH 14
  11. #define ITEM_Y 27
  12. #define ITEM_HEIGHT 13
  13. #define TEXT_X 6
  14. #define TEXT_Y 37
  15. #define TEXT_INTERVAL 3
  16. #define TEXT_WIDTH 12
  17. #define ITEM_FRAME_RADIUS 2
  18. struct SubBruteMainView {
  19. View* view;
  20. SubBruteMainViewCallback callback;
  21. void* context;
  22. uint8_t index;
  23. bool is_select_byte;
  24. bool two_bytes;
  25. uint64_t key_from_file;
  26. uint8_t repeat_values[SubBruteAttackTotalCount];
  27. uint8_t window_position;
  28. };
  29. typedef struct {
  30. uint8_t index;
  31. uint8_t repeat_values[SubBruteAttackTotalCount];
  32. uint8_t window_position;
  33. bool is_select_byte;
  34. bool two_bytes;
  35. uint64_t key_from_file;
  36. } SubBruteMainViewModel;
  37. void subbrute_main_view_set_callback(
  38. SubBruteMainView* instance,
  39. SubBruteMainViewCallback callback,
  40. void* context) {
  41. furi_assert(instance);
  42. furi_assert(callback);
  43. instance->callback = callback;
  44. instance->context = context;
  45. }
  46. void subbrute_main_view_center_displayed_key(
  47. Canvas* canvas,
  48. uint64_t key,
  49. uint8_t index,
  50. bool two_bytes) {
  51. uint8_t text_x = TEXT_X;
  52. uint8_t item_x = TEXT_X - ITEMS_INTERVAL;
  53. canvas_set_font(canvas, FontSecondary);
  54. for(int i = 0; i < 8; i++) {
  55. char current_value[3] = {0};
  56. uint8_t byte_value = (uint8_t)(key >> 8 * (7 - i)) & 0xFF;
  57. snprintf(current_value, sizeof(current_value), "%02X", byte_value);
  58. // For two bytes we need to select prev location
  59. if(!two_bytes && i == index) {
  60. canvas_set_color(canvas, ColorBlack);
  61. canvas_draw_rbox(
  62. canvas, item_x - 1, ITEM_Y, ITEM_WIDTH + 1, ITEM_HEIGHT, ITEM_FRAME_RADIUS);
  63. canvas_set_color(canvas, ColorWhite);
  64. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  65. } else if(two_bytes && (i == index || i == index - 1)) {
  66. if(i == index) {
  67. canvas_set_color(canvas, ColorBlack);
  68. canvas_draw_rbox(
  69. canvas,
  70. item_x - ITEMS_INTERVAL - ITEM_WIDTH - 1,
  71. ITEM_Y,
  72. ITEM_WIDTH * 2 + ITEMS_INTERVAL * 2 + 1,
  73. ITEM_HEIGHT,
  74. ITEM_FRAME_RADIUS);
  75. canvas_set_color(canvas, ColorWhite);
  76. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  77. // Redraw prev element with white
  78. memset(current_value, 0, sizeof(current_value));
  79. byte_value = (uint8_t)(key >> 8 * (7 - i + 1)) & 0xFF;
  80. snprintf(current_value, sizeof(current_value), "%02X", byte_value);
  81. canvas_draw_str(
  82. canvas, text_x - (TEXT_WIDTH + TEXT_INTERVAL), TEXT_Y, current_value);
  83. } else {
  84. canvas_set_color(canvas, ColorWhite);
  85. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  86. }
  87. } else {
  88. canvas_set_color(canvas, ColorBlack);
  89. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  90. }
  91. text_x = text_x + TEXT_WIDTH + TEXT_INTERVAL;
  92. item_x = item_x + ITEM_WIDTH + ITEMS_INTERVAL;
  93. }
  94. // Return normal color
  95. canvas_set_color(canvas, ColorBlack);
  96. }
  97. void subbrute_main_view_draw_is_byte_selected(Canvas* canvas, SubBruteMainViewModel* model) {
  98. #ifdef FURI_DEBUG
  99. //FURI_LOG_D(TAG, "key_from_file: %s", model->key_from_file);
  100. #endif
  101. //char msg_index[18];
  102. //snprintf(msg_index, sizeof(msg_index), "Field index: %d", model->index);
  103. canvas_set_font(canvas, FontSecondary);
  104. canvas_draw_str_aligned(
  105. canvas, 64, 17, AlignCenter, AlignTop, "Please select values to calc:");
  106. subbrute_main_view_center_displayed_key(
  107. canvas, model->key_from_file, model->index, model->two_bytes);
  108. //const char* line = furi_string_get_cstr(menu_items);
  109. //canvas_set_font(canvas, FontSecondary);
  110. //canvas_draw_str_aligned(
  111. // canvas, 64, 37, AlignCenter, AlignTop, furi_string_get_cstr(menu_items));
  112. elements_button_center(canvas, "Select");
  113. if(model->index > 0) {
  114. elements_button_left(canvas, " ");
  115. }
  116. if(model->index < 7) {
  117. elements_button_right(canvas, " ");
  118. }
  119. // Switch to another mode
  120. if(model->two_bytes) {
  121. elements_button_top_left(canvas, "One byte");
  122. } else {
  123. elements_button_top_left(canvas, "Two bytes");
  124. }
  125. }
  126. void subbrute_main_view_draw_is_ordinary_selected(Canvas* canvas, SubBruteMainViewModel* model) {
  127. uint16_t screen_width = canvas_width(canvas);
  128. uint16_t screen_height = canvas_height(canvas);
  129. // Title
  130. canvas_set_font(canvas, FontPrimary);
  131. canvas_draw_box(canvas, 0, 0, canvas_width(canvas), STATUS_BAR_Y_SHIFT);
  132. canvas_invert_color(canvas);
  133. canvas_draw_str_aligned(canvas, 64, 3, AlignCenter, AlignTop, SUBBRUTEFORCER_VER);
  134. canvas_invert_color(canvas);
  135. // Menu
  136. canvas_set_color(canvas, ColorBlack);
  137. canvas_set_font(canvas, FontSecondary);
  138. const uint8_t item_height = 16;
  139. const uint8_t string_height_offset = 9;
  140. #ifdef FURI_DEBUG
  141. //FURI_LOG_D(TAG, "window_position: %d, index: %d", model->window_position, model->index);
  142. #endif
  143. for(size_t position = 0; position < SubBruteAttackTotalCount; ++position) {
  144. uint8_t item_position = position - model->window_position;
  145. if(item_position < ITEMS_ON_SCREEN) {
  146. if(model->index == position) {
  147. canvas_draw_str_aligned(
  148. canvas,
  149. 3,
  150. string_height_offset + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  151. AlignLeft,
  152. AlignCenter,
  153. subbrute_protocol_name(position));
  154. elements_frame(
  155. canvas, 1, 1 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, 124, 15);
  156. } else {
  157. canvas_draw_str_aligned(
  158. canvas,
  159. 4,
  160. string_height_offset + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  161. AlignLeft,
  162. AlignCenter,
  163. subbrute_protocol_name(position));
  164. }
  165. uint8_t current_repeat_count = model->repeat_values[position];
  166. uint8_t min_repeat_count = subbrute_protocol_repeats_count(position);
  167. if(current_repeat_count > min_repeat_count) {
  168. #ifdef FW_ORIGIN_Official
  169. canvas_set_font(canvas, FontSecondary);
  170. #else
  171. canvas_set_font(canvas, FontBatteryPercent);
  172. #endif
  173. char buffer[10];
  174. snprintf(buffer, sizeof(buffer), "x%d", current_repeat_count);
  175. uint8_t temp_x_offset_repeats =
  176. current_repeat_count <= SUBBRUTE_PROTOCOL_MAX_REPEATS ? 15 : 18;
  177. canvas_draw_str_aligned(
  178. canvas,
  179. screen_width - temp_x_offset_repeats,
  180. string_height_offset + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  181. AlignLeft,
  182. AlignCenter,
  183. buffer);
  184. canvas_set_font(canvas, FontSecondary);
  185. }
  186. }
  187. }
  188. elements_scrollbar_pos(
  189. canvas,
  190. screen_width,
  191. STATUS_BAR_Y_SHIFT + 2,
  192. screen_height - STATUS_BAR_Y_SHIFT,
  193. model->index,
  194. SubBruteAttackTotalCount);
  195. }
  196. void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
  197. if(model->is_select_byte) {
  198. subbrute_main_view_draw_is_byte_selected(canvas, model);
  199. } else {
  200. subbrute_main_view_draw_is_ordinary_selected(canvas, model);
  201. }
  202. }
  203. bool subbrute_main_view_input_file_protocol(InputEvent* event, SubBruteMainView* instance) {
  204. bool updated = false;
  205. if(event->key == InputKeyLeft) {
  206. if((instance->index > 0 && !instance->two_bytes) ||
  207. (instance->two_bytes && instance->index > 1)) {
  208. instance->index--;
  209. }
  210. updated = true;
  211. } else if(event->key == InputKeyRight) {
  212. if(instance->index < 7) {
  213. instance->index++;
  214. }
  215. updated = true;
  216. } else if(event->key == InputKeyUp) {
  217. instance->two_bytes = !instance->two_bytes;
  218. // Because index is changing
  219. if(instance->two_bytes && instance->index < 7) {
  220. instance->index++;
  221. }
  222. // instance->callback(
  223. // instance->two_bytes ? SubBruteCustomEventTypeChangeStepUp :
  224. // SubBruteCustomEventTypeChangeStepDown,
  225. // instance->context);
  226. updated = true;
  227. } else if(event->key == InputKeyOk) {
  228. instance->callback(SubBruteCustomEventTypeIndexSelected, instance->context);
  229. updated = true;
  230. }
  231. return updated;
  232. }
  233. bool subbrute_main_view_input_ordinary_protocol(
  234. InputEvent* event,
  235. SubBruteMainView* instance,
  236. bool is_short) {
  237. const uint8_t min_value = 0;
  238. const uint8_t correct_total = SubBruteAttackTotalCount - 1;
  239. uint8_t index = instance->index;
  240. uint8_t min_repeats = subbrute_protocol_repeats_count(index);
  241. uint8_t max_repeats = min_repeats * 3;
  242. uint8_t current_repeats = instance->repeat_values[index];
  243. bool updated = false;
  244. if(event->key == InputKeyUp && is_short) {
  245. if(index == min_value) {
  246. instance->index = correct_total;
  247. } else {
  248. instance->index = CLAMP(index - 1, correct_total, min_value);
  249. }
  250. //instance->repeat_values = 0;
  251. updated = true;
  252. } else if(event->key == InputKeyDown && is_short) {
  253. if(index == correct_total) {
  254. instance->index = min_value;
  255. } else {
  256. instance->index = CLAMP(index + 1, correct_total, min_value);
  257. }
  258. //instance->repeat_values = 0;
  259. updated = true;
  260. } else if(event->key == InputKeyLeft && is_short) {
  261. instance->repeat_values[index] = CLAMP(current_repeats - 1, max_repeats, min_repeats);
  262. updated = true;
  263. } else if(event->key == InputKeyRight && is_short) {
  264. instance->repeat_values[index] = CLAMP(current_repeats + 1, max_repeats, min_repeats);
  265. updated = true;
  266. } else if(event->key == InputKeyOk && is_short) {
  267. if(index == SubBruteAttackLoadFile) {
  268. instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
  269. } else {
  270. instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
  271. }
  272. updated = true;
  273. }
  274. if(updated) {
  275. instance->window_position = instance->index;
  276. if(instance->window_position > 0) {
  277. instance->window_position -= 1;
  278. }
  279. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  280. instance->window_position = 0;
  281. } else {
  282. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  283. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  284. }
  285. }
  286. }
  287. return updated;
  288. }
  289. bool subbrute_main_view_input(InputEvent* event, void* context) {
  290. furi_assert(event);
  291. furi_assert(context);
  292. SubBruteMainView* instance = context;
  293. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  294. #ifdef FURI_DEBUG
  295. FURI_LOG_I(TAG, "InputKey: BACK");
  296. #endif
  297. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  298. return false;
  299. }
  300. #ifdef FURI_DEBUG
  301. FURI_LOG_D(
  302. TAG,
  303. "InputKey: %d, extra_repeats: %d",
  304. event->key,
  305. instance->repeat_values[instance->index]);
  306. #endif
  307. bool updated = false;
  308. bool is_short = (event->type == InputTypeShort) || (event->type == InputTypeRepeat);
  309. if(instance->is_select_byte) {
  310. if(is_short) {
  311. updated = subbrute_main_view_input_file_protocol(event, instance);
  312. }
  313. } else {
  314. updated = subbrute_main_view_input_ordinary_protocol(event, instance, is_short);
  315. }
  316. if(updated) {
  317. with_view_model(
  318. instance->view,
  319. SubBruteMainViewModel * model,
  320. {
  321. model->index = instance->index;
  322. model->window_position = instance->window_position;
  323. model->key_from_file = instance->key_from_file;
  324. model->is_select_byte = instance->is_select_byte;
  325. model->two_bytes = instance->two_bytes;
  326. model->repeat_values[model->index] = instance->repeat_values[instance->index];
  327. },
  328. true);
  329. }
  330. return updated;
  331. }
  332. void subbrute_main_view_enter(void* context) {
  333. furi_assert(context);
  334. }
  335. void subbrute_main_view_exit(void* context) {
  336. furi_assert(context);
  337. }
  338. SubBruteMainView* subbrute_main_view_alloc() {
  339. SubBruteMainView* instance = malloc(sizeof(SubBruteMainView));
  340. instance->view = view_alloc();
  341. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteMainViewModel));
  342. view_set_context(instance->view, instance);
  343. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_main_view_draw);
  344. view_set_input_callback(instance->view, subbrute_main_view_input);
  345. view_set_enter_callback(instance->view, subbrute_main_view_enter);
  346. view_set_exit_callback(instance->view, subbrute_main_view_exit);
  347. instance->index = 0;
  348. instance->window_position = 0;
  349. instance->key_from_file = 0;
  350. instance->is_select_byte = false;
  351. instance->two_bytes = false;
  352. with_view_model(
  353. instance->view,
  354. SubBruteMainViewModel * model,
  355. {
  356. model->index = instance->index;
  357. model->window_position = instance->window_position;
  358. model->key_from_file = instance->key_from_file;
  359. model->is_select_byte = instance->is_select_byte;
  360. model->two_bytes = instance->two_bytes;
  361. },
  362. true);
  363. return instance;
  364. }
  365. void subbrute_main_view_free(SubBruteMainView* instance) {
  366. furi_assert(instance);
  367. view_free(instance->view);
  368. free(instance);
  369. }
  370. View* subbrute_main_view_get_view(SubBruteMainView* instance) {
  371. furi_assert(instance);
  372. return instance->view;
  373. }
  374. void subbrute_main_view_set_index(
  375. SubBruteMainView* instance,
  376. uint8_t idx,
  377. const uint8_t* repeats,
  378. bool is_select_byte,
  379. bool two_bytes,
  380. uint64_t key_from_file) {
  381. furi_assert(instance);
  382. furi_assert(idx < SubBruteAttackTotalCount);
  383. #ifdef FURI_DEBUG
  384. FURI_LOG_I(TAG, "Set index: %d, is_select_byte: %d", idx, is_select_byte);
  385. #endif
  386. for(size_t i = 0; i < SubBruteAttackTotalCount; i++) {
  387. instance->repeat_values[i] = repeats[i];
  388. }
  389. instance->is_select_byte = is_select_byte;
  390. instance->two_bytes = two_bytes;
  391. instance->key_from_file = key_from_file;
  392. instance->index = idx;
  393. instance->window_position = idx;
  394. if(!is_select_byte) {
  395. if(instance->window_position > 0) {
  396. instance->window_position -= 1;
  397. }
  398. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  399. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  400. }
  401. }
  402. with_view_model(
  403. instance->view,
  404. SubBruteMainViewModel * model,
  405. {
  406. model->index = instance->index;
  407. model->window_position = instance->window_position;
  408. model->key_from_file = instance->key_from_file;
  409. model->is_select_byte = instance->is_select_byte;
  410. model->two_bytes = instance->two_bytes;
  411. for(size_t i = 0; i < SubBruteAttackTotalCount; i++) {
  412. model->repeat_values[i] = repeats[i];
  413. }
  414. },
  415. true);
  416. }
  417. SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
  418. furi_assert(instance);
  419. return instance->index;
  420. }
  421. const uint8_t* subbrute_main_view_get_repeats(SubBruteMainView* instance) {
  422. furi_assert(instance);
  423. return instance->repeat_values;
  424. }
  425. bool subbrute_main_view_get_two_bytes(SubBruteMainView* instance) {
  426. furi_assert(instance);
  427. return instance->two_bytes;
  428. }