mf_classic_dict.c 11 KB

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