dict_attack.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "dict_attack.h"
  2. #include <m-string.h>
  3. #include <gui/elements.h>
  4. typedef enum {
  5. DictAttackStateRead,
  6. DictAttackStateCardRemoved,
  7. } DictAttackState;
  8. struct DictAttack {
  9. View* view;
  10. DictAttackCallback callback;
  11. void* context;
  12. };
  13. typedef struct {
  14. DictAttackState state;
  15. MfClassicType type;
  16. string_t header;
  17. uint8_t sectors_total;
  18. uint8_t sectors_read;
  19. uint8_t sector_current;
  20. uint8_t keys_total;
  21. uint8_t keys_found;
  22. uint16_t dict_keys_total;
  23. uint16_t dict_keys_current;
  24. } DictAttackViewModel;
  25. static void dict_attack_draw_callback(Canvas* canvas, void* model) {
  26. DictAttackViewModel* m = model;
  27. if(m->state == DictAttackStateCardRemoved) {
  28. canvas_set_font(canvas, FontPrimary);
  29. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  30. canvas_set_font(canvas, FontSecondary);
  31. elements_multiline_text_aligned(
  32. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  33. } else if(m->state == DictAttackStateRead) {
  34. char draw_str[32] = {};
  35. canvas_set_font(canvas, FontPrimary);
  36. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, string_get_cstr(m->header));
  37. canvas_set_font(canvas, FontSecondary);
  38. float dict_progress = m->dict_keys_total == 0 ?
  39. 0 :
  40. (float)(m->dict_keys_current) / (float)(m->dict_keys_total);
  41. float progress = m->sectors_total == 0 ? 0 :
  42. ((float)(m->sector_current) + dict_progress) /
  43. (float)(m->sectors_total);
  44. if(progress > 1.0) {
  45. progress = 1.0;
  46. }
  47. elements_progress_bar(canvas, 5, 15, 120, progress);
  48. canvas_set_font(canvas, FontSecondary);
  49. snprintf(draw_str, sizeof(draw_str), "Keys found: %d/%d", m->keys_found, m->keys_total);
  50. canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
  51. snprintf(
  52. draw_str, sizeof(draw_str), "Sectors Read: %d/%d", m->sectors_read, m->sectors_total);
  53. canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_str);
  54. }
  55. elements_button_center(canvas, "Skip");
  56. }
  57. static bool dict_attack_input_callback(InputEvent* event, void* context) {
  58. DictAttack* dict_attack = context;
  59. bool consumed = false;
  60. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  61. if(dict_attack->callback) {
  62. dict_attack->callback(dict_attack->context);
  63. }
  64. consumed = true;
  65. }
  66. return consumed;
  67. }
  68. DictAttack* dict_attack_alloc() {
  69. DictAttack* dict_attack = malloc(sizeof(DictAttack));
  70. dict_attack->view = view_alloc();
  71. view_allocate_model(dict_attack->view, ViewModelTypeLocking, sizeof(DictAttackViewModel));
  72. view_set_draw_callback(dict_attack->view, dict_attack_draw_callback);
  73. view_set_input_callback(dict_attack->view, dict_attack_input_callback);
  74. view_set_context(dict_attack->view, dict_attack);
  75. with_view_model(
  76. dict_attack->view, (DictAttackViewModel * model) {
  77. string_init(model->header);
  78. return false;
  79. });
  80. return dict_attack;
  81. }
  82. void dict_attack_free(DictAttack* dict_attack) {
  83. furi_assert(dict_attack);
  84. with_view_model(
  85. dict_attack->view, (DictAttackViewModel * model) {
  86. string_clear(model->header);
  87. return false;
  88. });
  89. view_free(dict_attack->view);
  90. free(dict_attack);
  91. }
  92. void dict_attack_reset(DictAttack* dict_attack) {
  93. furi_assert(dict_attack);
  94. with_view_model(
  95. dict_attack->view, (DictAttackViewModel * model) {
  96. model->state = DictAttackStateRead;
  97. model->type = MfClassicType1k;
  98. model->sectors_total = 0;
  99. model->sectors_read = 0;
  100. model->sector_current = 0;
  101. model->keys_total = 0;
  102. model->keys_found = 0;
  103. model->dict_keys_total = 0;
  104. model->dict_keys_current = 0;
  105. string_reset(model->header);
  106. return false;
  107. });
  108. }
  109. View* dict_attack_get_view(DictAttack* dict_attack) {
  110. furi_assert(dict_attack);
  111. return dict_attack->view;
  112. }
  113. void dict_attack_set_callback(DictAttack* dict_attack, DictAttackCallback callback, void* context) {
  114. furi_assert(dict_attack);
  115. furi_assert(callback);
  116. dict_attack->callback = callback;
  117. dict_attack->context = context;
  118. }
  119. void dict_attack_set_header(DictAttack* dict_attack, const char* header) {
  120. furi_assert(dict_attack);
  121. furi_assert(header);
  122. with_view_model(
  123. dict_attack->view, (DictAttackViewModel * model) {
  124. string_set_str(model->header, header);
  125. return true;
  126. });
  127. }
  128. void dict_attack_set_card_detected(DictAttack* dict_attack, MfClassicType type) {
  129. furi_assert(dict_attack);
  130. with_view_model(
  131. dict_attack->view, (DictAttackViewModel * model) {
  132. model->state = DictAttackStateRead;
  133. model->sectors_total = mf_classic_get_total_sectors_num(type);
  134. model->keys_total = model->sectors_total * 2;
  135. return true;
  136. });
  137. }
  138. void dict_attack_set_card_removed(DictAttack* dict_attack) {
  139. furi_assert(dict_attack);
  140. with_view_model(
  141. dict_attack->view, (DictAttackViewModel * model) {
  142. model->state = DictAttackStateCardRemoved;
  143. return true;
  144. });
  145. }
  146. void dict_attack_set_sector_read(DictAttack* dict_attack, uint8_t sec_read) {
  147. furi_assert(dict_attack);
  148. with_view_model(
  149. dict_attack->view, (DictAttackViewModel * model) {
  150. model->sectors_read = sec_read;
  151. return true;
  152. });
  153. }
  154. void dict_attack_set_keys_found(DictAttack* dict_attack, uint8_t keys_found) {
  155. furi_assert(dict_attack);
  156. with_view_model(
  157. dict_attack->view, (DictAttackViewModel * model) {
  158. model->keys_found = keys_found;
  159. return true;
  160. });
  161. }
  162. void dict_attack_set_current_sector(DictAttack* dict_attack, uint8_t curr_sec) {
  163. furi_assert(dict_attack);
  164. with_view_model(
  165. dict_attack->view, (DictAttackViewModel * model) {
  166. model->sector_current = curr_sec;
  167. model->dict_keys_current = 0;
  168. return true;
  169. });
  170. }
  171. void dict_attack_inc_current_sector(DictAttack* dict_attack) {
  172. furi_assert(dict_attack);
  173. with_view_model(
  174. dict_attack->view, (DictAttackViewModel * model) {
  175. if(model->sector_current < model->sectors_total) {
  176. model->sector_current++;
  177. model->dict_keys_current = 0;
  178. }
  179. return true;
  180. });
  181. }
  182. void dict_attack_inc_keys_found(DictAttack* dict_attack) {
  183. furi_assert(dict_attack);
  184. with_view_model(
  185. dict_attack->view, (DictAttackViewModel * model) {
  186. if(model->keys_found < model->keys_total) {
  187. model->keys_found++;
  188. }
  189. return true;
  190. });
  191. }
  192. void dict_attack_set_total_dict_keys(DictAttack* dict_attack, uint16_t dict_keys_total) {
  193. furi_assert(dict_attack);
  194. with_view_model(
  195. dict_attack->view, (DictAttackViewModel * model) {
  196. model->dict_keys_total = dict_keys_total;
  197. return true;
  198. });
  199. }
  200. void dict_attack_inc_current_dict_key(DictAttack* dict_attack, uint16_t keys_tried) {
  201. furi_assert(dict_attack);
  202. with_view_model(
  203. dict_attack->view, (DictAttackViewModel * model) {
  204. if(model->dict_keys_current + keys_tried < model->dict_keys_total) {
  205. model->dict_keys_current += keys_tried;
  206. }
  207. return true;
  208. });
  209. }