subbrute_main_view.c 15 KB

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