mf_classic_dict.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "mf_classic_dict.h"
  2. #include <lib/toolbox/args.h>
  3. #include <lib/flipper_format/flipper_format.h>
  4. #define MF_CLASSIC_DICT_FLIPPER_PATH EXT_PATH("nfc/assets/mf_classic_dict.nfc")
  5. #define MF_CLASSIC_DICT_USER_PATH EXT_PATH("nfc/assets/mf_classic_dict_user.nfc")
  6. #define MF_CLASSIC_DICT_UNIT_TEST_PATH EXT_PATH("unit_tests/mf_classic_dict.nfc")
  7. #define TAG "MfClassicDict"
  8. #define NFC_MF_CLASSIC_KEY_LEN (13)
  9. struct MfClassicDict {
  10. Stream* stream;
  11. uint32_t total_keys;
  12. };
  13. bool mf_classic_dict_check_presence(MfClassicDictType dict_type) {
  14. Storage* storage = furi_record_open(RECORD_STORAGE);
  15. bool dict_present = false;
  16. if(dict_type == MfClassicDictTypeFlipper) {
  17. dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_FLIPPER_PATH, NULL) == FSE_OK;
  18. } else if(dict_type == MfClassicDictTypeUser) {
  19. dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_USER_PATH, NULL) == FSE_OK;
  20. } else if(dict_type == MfClassicDictTypeUnitTest) {
  21. dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_UNIT_TEST_PATH, NULL) ==
  22. FSE_OK;
  23. }
  24. furi_record_close(RECORD_STORAGE);
  25. return dict_present;
  26. }
  27. MfClassicDict* mf_classic_dict_alloc(MfClassicDictType dict_type) {
  28. MfClassicDict* dict = malloc(sizeof(MfClassicDict));
  29. Storage* storage = furi_record_open(RECORD_STORAGE);
  30. dict->stream = buffered_file_stream_alloc(storage);
  31. furi_record_close(RECORD_STORAGE);
  32. bool dict_loaded = false;
  33. do {
  34. if(dict_type == MfClassicDictTypeFlipper) {
  35. if(!buffered_file_stream_open(
  36. dict->stream, MF_CLASSIC_DICT_FLIPPER_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  37. buffered_file_stream_close(dict->stream);
  38. break;
  39. }
  40. } else if(dict_type == MfClassicDictTypeUser) {
  41. if(!buffered_file_stream_open(
  42. dict->stream, MF_CLASSIC_DICT_USER_PATH, FSAM_READ_WRITE, FSOM_OPEN_ALWAYS)) {
  43. buffered_file_stream_close(dict->stream);
  44. break;
  45. }
  46. } else if(dict_type == MfClassicDictTypeUnitTest) {
  47. if(!buffered_file_stream_open(
  48. dict->stream,
  49. MF_CLASSIC_DICT_UNIT_TEST_PATH,
  50. FSAM_READ_WRITE,
  51. FSOM_CREATE_ALWAYS)) {
  52. buffered_file_stream_close(dict->stream);
  53. break;
  54. }
  55. }
  56. // Read total amount of keys
  57. FuriString* next_line;
  58. next_line = furi_string_alloc();
  59. while(true) {
  60. if(!stream_read_line(dict->stream, next_line)) {
  61. FURI_LOG_T(TAG, "No keys left in dict");
  62. break;
  63. }
  64. furi_string_trim(next_line);
  65. FURI_LOG_T(
  66. TAG,
  67. "Read line: %s, len: %d",
  68. furi_string_get_cstr(next_line),
  69. furi_string_size(next_line));
  70. if(furi_string_get_char(next_line, 0) == '#') continue;
  71. if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN - 1) continue;
  72. dict->total_keys++;
  73. }
  74. furi_string_free(next_line);
  75. stream_rewind(dict->stream);
  76. dict_loaded = true;
  77. FURI_LOG_I(TAG, "Loaded dictionary with %ld keys", dict->total_keys);
  78. } while(false);
  79. if(!dict_loaded) {
  80. buffered_file_stream_close(dict->stream);
  81. free(dict);
  82. dict = NULL;
  83. }
  84. return dict;
  85. }
  86. void mf_classic_dict_free(MfClassicDict* dict) {
  87. furi_assert(dict);
  88. furi_assert(dict->stream);
  89. buffered_file_stream_close(dict->stream);
  90. stream_free(dict->stream);
  91. free(dict);
  92. }
  93. static void mf_classic_dict_int_to_str(uint8_t* key_int, FuriString* key_str) {
  94. furi_string_reset(key_str);
  95. for(size_t i = 0; i < 6; i++) {
  96. furi_string_cat_printf(key_str, "%02X", key_int[i]);
  97. }
  98. }
  99. static void mf_classic_dict_str_to_int(FuriString* key_str, uint64_t* key_int) {
  100. uint8_t key_byte_tmp;
  101. *key_int = 0ULL;
  102. for(uint8_t i = 0; i < 12; i += 2) {
  103. args_char_to_hex(
  104. furi_string_get_char(key_str, i), furi_string_get_char(key_str, i + 1), &key_byte_tmp);
  105. *key_int |= (uint64_t)key_byte_tmp << 8 * (5 - i / 2);
  106. }
  107. }
  108. uint32_t mf_classic_dict_get_total_keys(MfClassicDict* dict) {
  109. furi_assert(dict);
  110. return dict->total_keys;
  111. }
  112. bool mf_classic_dict_rewind(MfClassicDict* dict) {
  113. furi_assert(dict);
  114. furi_assert(dict->stream);
  115. return stream_rewind(dict->stream);
  116. }
  117. bool mf_classic_dict_get_next_key_str(MfClassicDict* dict, FuriString* key) {
  118. furi_assert(dict);
  119. furi_assert(dict->stream);
  120. bool key_read = false;
  121. furi_string_reset(key);
  122. while(!key_read) {
  123. if(!stream_read_line(dict->stream, key)) break;
  124. if(furi_string_get_char(key, 0) == '#') continue;
  125. if(furi_string_size(key) != NFC_MF_CLASSIC_KEY_LEN) continue;
  126. furi_string_left(key, 12);
  127. key_read = true;
  128. }
  129. return key_read;
  130. }
  131. bool mf_classic_dict_get_next_key(MfClassicDict* dict, uint64_t* key) {
  132. furi_assert(dict);
  133. furi_assert(dict->stream);
  134. FuriString* temp_key;
  135. temp_key = furi_string_alloc();
  136. bool key_read = mf_classic_dict_get_next_key_str(dict, temp_key);
  137. if(key_read) {
  138. mf_classic_dict_str_to_int(temp_key, key);
  139. }
  140. furi_string_free(temp_key);
  141. return key_read;
  142. }
  143. bool mf_classic_dict_is_key_present_str(MfClassicDict* dict, FuriString* key) {
  144. furi_assert(dict);
  145. furi_assert(dict->stream);
  146. FuriString* next_line;
  147. next_line = furi_string_alloc();
  148. bool key_found = false;
  149. stream_rewind(dict->stream);
  150. while(!key_found) {
  151. if(!stream_read_line(dict->stream, next_line)) break;
  152. if(furi_string_get_char(next_line, 0) == '#') continue;
  153. if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
  154. furi_string_left(next_line, 12);
  155. if(!furi_string_equal(key, next_line)) continue;
  156. key_found = true;
  157. }
  158. furi_string_free(next_line);
  159. return key_found;
  160. }
  161. bool mf_classic_dict_is_key_present(MfClassicDict* dict, uint8_t* key) {
  162. FuriString* temp_key;
  163. temp_key = furi_string_alloc();
  164. mf_classic_dict_int_to_str(key, temp_key);
  165. bool key_found = mf_classic_dict_is_key_present_str(dict, temp_key);
  166. furi_string_free(temp_key);
  167. return key_found;
  168. }
  169. bool mf_classic_dict_add_key_str(MfClassicDict* dict, FuriString* key) {
  170. furi_assert(dict);
  171. furi_assert(dict->stream);
  172. furi_string_cat_printf(key, "\n");
  173. bool key_added = false;
  174. do {
  175. if(!stream_seek(dict->stream, 0, StreamOffsetFromEnd)) break;
  176. if(!stream_insert_string(dict->stream, key)) break;
  177. dict->total_keys++;
  178. key_added = true;
  179. } while(false);
  180. furi_string_left(key, 12);
  181. return key_added;
  182. }
  183. bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key) {
  184. furi_assert(dict);
  185. furi_assert(dict->stream);
  186. FuriString* temp_key;
  187. temp_key = furi_string_alloc();
  188. mf_classic_dict_int_to_str(key, temp_key);
  189. bool key_added = mf_classic_dict_add_key_str(dict, temp_key);
  190. furi_string_free(temp_key);
  191. return key_added;
  192. }
  193. bool mf_classic_dict_get_key_at_index_str(MfClassicDict* dict, FuriString* key, uint32_t target) {
  194. furi_assert(dict);
  195. furi_assert(dict->stream);
  196. FuriString* next_line;
  197. uint32_t index = 0;
  198. next_line = furi_string_alloc();
  199. furi_string_reset(key);
  200. bool key_found = false;
  201. while(!key_found) {
  202. if(!stream_read_line(dict->stream, next_line)) break;
  203. if(furi_string_get_char(next_line, 0) == '#') continue;
  204. if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
  205. if(index++ != target) continue;
  206. furi_string_set_n(key, next_line, 0, 12);
  207. key_found = true;
  208. }
  209. furi_string_free(next_line);
  210. return key_found;
  211. }
  212. bool mf_classic_dict_get_key_at_index(MfClassicDict* dict, uint64_t* key, uint32_t target) {
  213. furi_assert(dict);
  214. furi_assert(dict->stream);
  215. FuriString* temp_key;
  216. temp_key = furi_string_alloc();
  217. bool key_found = mf_classic_dict_get_key_at_index_str(dict, temp_key, target);
  218. if(key_found) {
  219. mf_classic_dict_str_to_int(temp_key, key);
  220. }
  221. furi_string_free(temp_key);
  222. return key_found;
  223. }
  224. bool mf_classic_dict_find_index_str(MfClassicDict* dict, FuriString* key, uint32_t* target) {
  225. furi_assert(dict);
  226. furi_assert(dict->stream);
  227. FuriString* next_line;
  228. next_line = furi_string_alloc();
  229. bool key_found = false;
  230. uint32_t index = 0;
  231. stream_rewind(dict->stream);
  232. while(!key_found) {
  233. if(!stream_read_line(dict->stream, next_line)) break;
  234. if(furi_string_get_char(next_line, 0) == '#') continue;
  235. if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
  236. furi_string_left(next_line, 12);
  237. if(!furi_string_equal(key, next_line)) continue;
  238. key_found = true;
  239. *target = index;
  240. }
  241. furi_string_free(next_line);
  242. return key_found;
  243. }
  244. bool mf_classic_dict_find_index(MfClassicDict* dict, uint8_t* key, uint32_t* target) {
  245. furi_assert(dict);
  246. furi_assert(dict->stream);
  247. FuriString* temp_key;
  248. temp_key = furi_string_alloc();
  249. mf_classic_dict_int_to_str(key, temp_key);
  250. bool key_found = mf_classic_dict_find_index_str(dict, temp_key, target);
  251. furi_string_free(temp_key);
  252. return key_found;
  253. }
  254. bool mf_classic_dict_delete_index(MfClassicDict* dict, uint32_t target) {
  255. furi_assert(dict);
  256. furi_assert(dict->stream);
  257. FuriString* next_line;
  258. next_line = furi_string_alloc();
  259. uint32_t index = 0;
  260. bool key_removed = false;
  261. while(!key_removed) {
  262. if(!stream_read_line(dict->stream, next_line)) break;
  263. if(furi_string_get_char(next_line, 0) == '#') continue;
  264. if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
  265. if(index++ != target) continue;
  266. stream_seek(dict->stream, -NFC_MF_CLASSIC_KEY_LEN, StreamOffsetFromCurrent);
  267. if(!stream_delete(dict->stream, NFC_MF_CLASSIC_KEY_LEN)) break;
  268. dict->total_keys--;
  269. key_removed = true;
  270. }
  271. furi_string_free(next_line);
  272. return key_removed;
  273. }