subbrute_main_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. canvas_set_font(canvas, FontSecondary);
  99. canvas_draw_str_aligned(
  100. canvas, 64, 17, AlignCenter, AlignTop, "Please select values to calc:");
  101. subbrute_main_view_center_displayed_key(
  102. canvas, model->key_from_file, model->index, model->two_bytes);
  103. elements_button_center(canvas, "Select");
  104. if(model->index > 0) {
  105. elements_button_left(canvas, " ");
  106. }
  107. if(model->index < 7) {
  108. elements_button_right(canvas, " ");
  109. }
  110. // Switch to another mode
  111. if(model->two_bytes) {
  112. elements_button_top_left(canvas, "One byte");
  113. } else {
  114. elements_button_top_left(canvas, "Two bytes");
  115. }
  116. }
  117. void subbrute_main_view_draw_is_ordinary_selected(Canvas* canvas, SubBruteMainViewModel* model) {
  118. uint16_t screen_width = canvas_width(canvas);
  119. uint16_t screen_height = canvas_height(canvas);
  120. // Title
  121. canvas_set_font(canvas, FontPrimary);
  122. canvas_draw_box(canvas, 0, 0, canvas_width(canvas), STATUS_BAR_Y_SHIFT);
  123. canvas_invert_color(canvas);
  124. canvas_draw_str_aligned(canvas, 64, 3, AlignCenter, AlignTop, SUB_BRUTE_FORCER_VERSION);
  125. canvas_invert_color(canvas);
  126. // Menu
  127. canvas_set_color(canvas, ColorBlack);
  128. canvas_set_font(canvas, FontSecondary);
  129. const uint8_t item_height = 16;
  130. const uint8_t string_height_offset = 9;
  131. for(size_t position = 0; position < SubBruteAttackTotalCount; ++position) {
  132. uint8_t item_position = position - model->window_position;
  133. if(item_position < ITEMS_ON_SCREEN) {
  134. if(model->index == position) {
  135. canvas_draw_str_aligned(
  136. canvas,
  137. 3,
  138. string_height_offset + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  139. AlignLeft,
  140. AlignCenter,
  141. subbrute_protocol_name(position));
  142. elements_frame(
  143. canvas, 1, 1 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, 124, 15);
  144. } else {
  145. canvas_draw_str_aligned(
  146. canvas,
  147. 4,
  148. string_height_offset + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  149. AlignLeft,
  150. AlignCenter,
  151. subbrute_protocol_name(position));
  152. }
  153. uint8_t current_repeat_count = model->repeat_values[position];
  154. uint8_t min_repeat_count = subbrute_protocol_repeats_count(position);
  155. if(current_repeat_count > min_repeat_count) {
  156. #ifdef FW_ORIGIN_Official
  157. canvas_set_font(canvas, FontSecondary);
  158. #else
  159. canvas_set_font(canvas, FontBatteryPercent);
  160. #endif
  161. char buffer[10] = {0};
  162. snprintf(buffer, sizeof(buffer), "x%d", current_repeat_count);
  163. uint8_t temp_x_offset_repeats =
  164. current_repeat_count <= SUBBRUTE_PROTOCOL_MAX_REPEATS ? 15 : 18;
  165. canvas_draw_str_aligned(
  166. canvas,
  167. screen_width - temp_x_offset_repeats,
  168. string_height_offset + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  169. AlignLeft,
  170. AlignCenter,
  171. buffer);
  172. canvas_set_font(canvas, FontSecondary);
  173. }
  174. }
  175. }
  176. elements_scrollbar_pos(
  177. canvas,
  178. screen_width,
  179. STATUS_BAR_Y_SHIFT + 2,
  180. screen_height - STATUS_BAR_Y_SHIFT,
  181. model->index,
  182. SubBruteAttackTotalCount);
  183. }
  184. void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
  185. if(model->is_select_byte) {
  186. subbrute_main_view_draw_is_byte_selected(canvas, model);
  187. } else {
  188. subbrute_main_view_draw_is_ordinary_selected(canvas, model);
  189. }
  190. }
  191. bool subbrute_main_view_input_file_protocol(InputEvent* event, SubBruteMainView* instance) {
  192. bool updated = false;
  193. if(event->key == InputKeyLeft) {
  194. if((instance->index > 0 && !instance->two_bytes) ||
  195. (instance->two_bytes && instance->index > 1)) {
  196. instance->index--;
  197. }
  198. updated = true;
  199. } else if(event->key == InputKeyRight) {
  200. if(instance->index < 7) {
  201. instance->index++;
  202. }
  203. updated = true;
  204. } else if(event->key == InputKeyUp) {
  205. instance->two_bytes = !instance->two_bytes;
  206. // Because index is changing
  207. if(instance->two_bytes && instance->index < 7) {
  208. instance->index++;
  209. }
  210. updated = true;
  211. } else if(event->key == InputKeyOk) {
  212. instance->callback(SubBruteCustomEventTypeIndexSelected,
  213. instance->context);
  214. updated = true;
  215. }
  216. return updated;
  217. }
  218. bool subbrute_main_view_input_ordinary_protocol(
  219. InputEvent* event,
  220. SubBruteMainView* instance,
  221. bool is_short) {
  222. const uint8_t min_value = 0;
  223. const uint8_t correct_total = SubBruteAttackTotalCount - 1;
  224. uint8_t index = instance->index;
  225. uint8_t min_repeats = subbrute_protocol_repeats_count(index);
  226. uint8_t max_repeats = min_repeats * 3;
  227. uint8_t current_repeats = instance->repeat_values[index];
  228. bool updated = false;
  229. if(event->key == InputKeyUp && is_short) {
  230. if(index == min_value) {
  231. instance->index = correct_total;
  232. } else {
  233. instance->index = CLAMP(index - 1, correct_total, min_value);
  234. }
  235. updated = true;
  236. } else if(event->key == InputKeyDown && is_short) {
  237. if(index == correct_total) {
  238. instance->index = min_value;
  239. } else {
  240. instance->index = CLAMP(index + 1, correct_total, min_value);
  241. }
  242. updated = true;
  243. } else if(event->key == InputKeyLeft && is_short) {
  244. instance->repeat_values[index] = CLAMP(current_repeats - 1, max_repeats, min_repeats);
  245. updated = true;
  246. } else if(event->key == InputKeyRight && is_short) {
  247. instance->repeat_values[index] = CLAMP(current_repeats + 1, max_repeats, min_repeats);
  248. updated = true;
  249. } else if(event->key == InputKeyOk && is_short) {
  250. if(index == SubBruteAttackLoadFile) {
  251. instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
  252. } else {
  253. instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
  254. }
  255. updated = true;
  256. }
  257. if(updated) {
  258. instance->window_position = instance->index;
  259. if(instance->window_position > 0) {
  260. instance->window_position -= 1;
  261. }
  262. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  263. instance->window_position = 0;
  264. } else {
  265. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  266. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  267. }
  268. }
  269. }
  270. return updated;
  271. }
  272. bool subbrute_main_view_input(InputEvent* event, void* context) {
  273. furi_assert(event);
  274. furi_assert(context);
  275. SubBruteMainView* instance = (SubBruteMainView*)context;
  276. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  277. #ifdef FURI_DEBUG
  278. FURI_LOG_I(TAG, "InputKey: BACK");
  279. #endif
  280. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  281. return false;
  282. }
  283. #ifdef FURI_DEBUG
  284. FURI_LOG_D(
  285. TAG,
  286. "InputKey: %d, extra_repeats: %d",
  287. event->key,
  288. instance->repeat_values[instance->index]);
  289. #endif
  290. bool updated = false;
  291. bool is_short = (event->type == InputTypeShort) || (event->type == InputTypeRepeat);
  292. if(instance->is_select_byte) {
  293. if(is_short) {
  294. updated = subbrute_main_view_input_file_protocol(event, instance);
  295. }
  296. } else {
  297. updated = subbrute_main_view_input_ordinary_protocol(event, instance, is_short);
  298. }
  299. if(updated) {
  300. with_view_model(
  301. instance->view,
  302. SubBruteMainViewModel * model,
  303. {
  304. model->index = instance->index;
  305. model->window_position = instance->window_position;
  306. model->key_from_file = instance->key_from_file;
  307. model->is_select_byte = instance->is_select_byte;
  308. model->two_bytes = instance->two_bytes;
  309. model->repeat_values[model->index] = instance->repeat_values[instance->index];
  310. },
  311. true);
  312. }
  313. return updated;
  314. }
  315. void subbrute_main_view_enter(void* context) {
  316. furi_assert(context);
  317. }
  318. void subbrute_main_view_exit(void* context) {
  319. furi_assert(context);
  320. }
  321. SubBruteMainView* subbrute_main_view_alloc() {
  322. SubBruteMainView* instance = malloc(sizeof(SubBruteMainView));
  323. instance->view = view_alloc();
  324. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteMainViewModel));
  325. view_set_context(instance->view, instance);
  326. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_main_view_draw);
  327. view_set_input_callback(instance->view, subbrute_main_view_input);
  328. view_set_enter_callback(instance->view, subbrute_main_view_enter);
  329. view_set_exit_callback(instance->view, subbrute_main_view_exit);
  330. instance->index = 0;
  331. instance->window_position = 0;
  332. instance->key_from_file = 0;
  333. instance->is_select_byte = false;
  334. instance->two_bytes = false;
  335. with_view_model(
  336. instance->view,
  337. SubBruteMainViewModel * model,
  338. {
  339. model->index = instance->index;
  340. model->window_position = instance->window_position;
  341. model->key_from_file = instance->key_from_file;
  342. model->is_select_byte = instance->is_select_byte;
  343. model->two_bytes = instance->two_bytes;
  344. },
  345. true);
  346. return instance;
  347. }
  348. void subbrute_main_view_free(SubBruteMainView* instance) {
  349. furi_assert(instance);
  350. view_free(instance->view);
  351. free(instance);
  352. }
  353. View* subbrute_main_view_get_view(SubBruteMainView* instance) {
  354. furi_assert(instance);
  355. return instance->view;
  356. }
  357. void subbrute_main_view_set_index(
  358. SubBruteMainView* instance,
  359. uint8_t idx,
  360. const uint8_t* repeats,
  361. bool is_select_byte,
  362. bool two_bytes,
  363. uint64_t key_from_file) {
  364. furi_assert(instance);
  365. furi_assert(idx < SubBruteAttackTotalCount);
  366. #ifdef FURI_DEBUG
  367. FURI_LOG_I(TAG, "Set index: %d, is_select_byte: %d", idx, is_select_byte);
  368. #endif
  369. for(size_t i = 0; i < SubBruteAttackTotalCount; i++) {
  370. instance->repeat_values[i] = repeats[i];
  371. }
  372. instance->is_select_byte = is_select_byte;
  373. instance->two_bytes = two_bytes;
  374. instance->key_from_file = key_from_file;
  375. instance->index = idx;
  376. instance->window_position = idx;
  377. if(!is_select_byte) {
  378. if(instance->window_position > 0) {
  379. instance->window_position -= 1;
  380. }
  381. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  382. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  383. }
  384. }
  385. with_view_model(
  386. instance->view,
  387. SubBruteMainViewModel * model,
  388. {
  389. model->index = instance->index;
  390. model->window_position = instance->window_position;
  391. model->key_from_file = instance->key_from_file;
  392. model->is_select_byte = instance->is_select_byte;
  393. model->two_bytes = instance->two_bytes;
  394. for(size_t i = 0; i < SubBruteAttackTotalCount; i++) {
  395. model->repeat_values[i] = repeats[i];
  396. }
  397. },
  398. true);
  399. }
  400. SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
  401. furi_assert(instance);
  402. return instance->index;
  403. }
  404. const uint8_t* subbrute_main_view_get_repeats(SubBruteMainView* instance) {
  405. furi_assert(instance);
  406. return instance->repeat_values;
  407. }
  408. bool subbrute_main_view_get_two_bytes(SubBruteMainView* instance) {
  409. furi_assert(instance);
  410. return instance->two_bytes;
  411. }