subbrute_main_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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,
  212. instance->context);
  213. updated = true;
  214. }
  215. return updated;
  216. }
  217. bool subbrute_main_view_input_ordinary_protocol(
  218. InputEvent* event,
  219. SubBruteMainView* instance,
  220. bool is_short) {
  221. const uint8_t min_value = 0;
  222. const uint8_t correct_total = SubBruteAttackTotalCount - 1;
  223. uint8_t index = instance->index;
  224. uint8_t min_repeats = subbrute_protocol_repeats_count(index);
  225. uint8_t max_repeats = min_repeats * 3;
  226. uint8_t current_repeats = instance->repeat_values[index];
  227. bool updated = false;
  228. if(event->key == InputKeyUp && is_short) {
  229. if(index == min_value) {
  230. instance->index = correct_total;
  231. } else {
  232. instance->index = CLAMP(index - 1, correct_total, min_value);
  233. }
  234. updated = true;
  235. } else if(event->key == InputKeyDown && is_short) {
  236. if(index == correct_total) {
  237. instance->index = min_value;
  238. } else {
  239. instance->index = CLAMP(index + 1, correct_total, min_value);
  240. }
  241. updated = true;
  242. } else if(event->key == InputKeyLeft && is_short) {
  243. instance->repeat_values[index] = CLAMP(current_repeats - 1, max_repeats, min_repeats);
  244. updated = true;
  245. } else if(event->key == InputKeyRight && is_short) {
  246. instance->repeat_values[index] = CLAMP(current_repeats + 1, max_repeats, min_repeats);
  247. updated = true;
  248. } else if(event->key == InputKeyOk && is_short) {
  249. if(index == SubBruteAttackLoadFile) {
  250. instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
  251. } else {
  252. instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
  253. }
  254. updated = true;
  255. }
  256. if(updated) {
  257. instance->window_position = instance->index;
  258. if(instance->window_position > 0) {
  259. instance->window_position -= 1;
  260. }
  261. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  262. instance->window_position = 0;
  263. } else {
  264. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  265. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  266. }
  267. }
  268. }
  269. return updated;
  270. }
  271. bool subbrute_main_view_input(InputEvent* event, void* context) {
  272. furi_assert(event);
  273. furi_assert(context);
  274. SubBruteMainView* instance = (SubBruteMainView*)context;
  275. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  276. #ifdef FURI_DEBUG
  277. FURI_LOG_I(TAG, "InputKey: BACK");
  278. #endif
  279. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  280. return false;
  281. }
  282. #ifdef FURI_DEBUG
  283. FURI_LOG_D(
  284. TAG,
  285. "InputKey: %d, extra_repeats: %d",
  286. event->key,
  287. instance->repeat_values[instance->index]);
  288. #endif
  289. bool updated = false;
  290. bool is_short = (event->type == InputTypeShort) || (event->type == InputTypeRepeat);
  291. if(instance->is_select_byte) {
  292. if(is_short) {
  293. updated = subbrute_main_view_input_file_protocol(event, instance);
  294. }
  295. } else {
  296. updated = subbrute_main_view_input_ordinary_protocol(event, instance, is_short);
  297. }
  298. if(updated) {
  299. with_view_model(
  300. instance->view,
  301. SubBruteMainViewModel * model,
  302. {
  303. model->index = instance->index;
  304. model->window_position = instance->window_position;
  305. model->key_from_file = instance->key_from_file;
  306. model->is_select_byte = instance->is_select_byte;
  307. model->two_bytes = instance->two_bytes;
  308. model->repeat_values[model->index] = instance->repeat_values[instance->index];
  309. },
  310. true);
  311. }
  312. return updated;
  313. }
  314. void subbrute_main_view_enter(void* context) {
  315. furi_assert(context);
  316. }
  317. void subbrute_main_view_exit(void* context) {
  318. furi_assert(context);
  319. }
  320. SubBruteMainView* subbrute_main_view_alloc() {
  321. SubBruteMainView* instance = malloc(sizeof(SubBruteMainView));
  322. instance->view = view_alloc();
  323. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteMainViewModel));
  324. view_set_context(instance->view, instance);
  325. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_main_view_draw);
  326. view_set_input_callback(instance->view, subbrute_main_view_input);
  327. view_set_enter_callback(instance->view, subbrute_main_view_enter);
  328. view_set_exit_callback(instance->view, subbrute_main_view_exit);
  329. instance->index = 0;
  330. instance->window_position = 0;
  331. instance->key_from_file = 0;
  332. instance->is_select_byte = false;
  333. instance->two_bytes = false;
  334. with_view_model(
  335. instance->view,
  336. SubBruteMainViewModel * model,
  337. {
  338. model->index = instance->index;
  339. model->window_position = instance->window_position;
  340. model->key_from_file = instance->key_from_file;
  341. model->is_select_byte = instance->is_select_byte;
  342. model->two_bytes = instance->two_bytes;
  343. },
  344. true);
  345. return instance;
  346. }
  347. void subbrute_main_view_free(SubBruteMainView* instance) {
  348. furi_assert(instance);
  349. view_free(instance->view);
  350. free(instance);
  351. }
  352. View* subbrute_main_view_get_view(SubBruteMainView* instance) {
  353. furi_assert(instance);
  354. return instance->view;
  355. }
  356. void subbrute_main_view_set_index(
  357. SubBruteMainView* instance,
  358. uint8_t idx,
  359. const uint8_t* repeats,
  360. bool is_select_byte,
  361. bool two_bytes,
  362. uint64_t key_from_file) {
  363. furi_assert(instance);
  364. furi_assert(idx < SubBruteAttackTotalCount);
  365. #ifdef FURI_DEBUG
  366. FURI_LOG_I(TAG, "Set index: %d, is_select_byte: %d", idx, is_select_byte);
  367. #endif
  368. for(size_t i = 0; i < SubBruteAttackTotalCount; i++) {
  369. instance->repeat_values[i] = repeats[i];
  370. }
  371. instance->is_select_byte = is_select_byte;
  372. instance->two_bytes = two_bytes;
  373. instance->key_from_file = key_from_file;
  374. instance->index = idx;
  375. instance->window_position = idx;
  376. if(!is_select_byte) {
  377. if(instance->window_position > 0) {
  378. instance->window_position -= 1;
  379. }
  380. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  381. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  382. }
  383. }
  384. with_view_model(
  385. instance->view,
  386. SubBruteMainViewModel * model,
  387. {
  388. model->index = instance->index;
  389. model->window_position = instance->window_position;
  390. model->key_from_file = instance->key_from_file;
  391. model->is_select_byte = instance->is_select_byte;
  392. model->two_bytes = instance->two_bytes;
  393. for(size_t i = 0; i < SubBruteAttackTotalCount; i++) {
  394. model->repeat_values[i] = repeats[i];
  395. }
  396. },
  397. true);
  398. }
  399. SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
  400. furi_assert(instance);
  401. return instance->index;
  402. }
  403. const uint8_t* subbrute_main_view_get_repeats(SubBruteMainView* instance) {
  404. furi_assert(instance);
  405. return instance->repeat_values;
  406. }
  407. bool subbrute_main_view_get_two_bytes(SubBruteMainView* instance) {
  408. furi_assert(instance);
  409. return instance->two_bytes;
  410. }