subbrute_main_view.c 16 KB

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