iclass_elite_dict.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "iclass_elite_dict.h"
  2. #include <lib/toolbox/args.h>
  3. #include <lib/flipper_format/flipper_format.h>
  4. #define ICLASS_ELITE_DICT_FLIPPER_NAME APP_DATA_PATH("assets/iclass_elite_dict.txt")
  5. #define ICLASS_ELITE_DICT_USER_NAME APP_DATA_PATH("assets/iclass_elite_dict_user.txt")
  6. #define TAG "IclassEliteDict"
  7. #define ICLASS_ELITE_KEY_LINE_LEN (17)
  8. #define ICLASS_ELITE_KEY_LEN (8)
  9. struct IclassEliteDict {
  10. Stream* stream;
  11. uint32_t total_keys;
  12. };
  13. bool iclass_elite_dict_check_presence(IclassEliteDictType dict_type) {
  14. Storage* storage = furi_record_open(RECORD_STORAGE);
  15. bool dict_present = false;
  16. if(dict_type == IclassEliteDictTypeFlipper) {
  17. dict_present =
  18. (storage_common_stat(storage, ICLASS_ELITE_DICT_FLIPPER_NAME, NULL) == FSE_OK);
  19. } else if(dict_type == IclassEliteDictTypeUser) {
  20. dict_present = (storage_common_stat(storage, ICLASS_ELITE_DICT_USER_NAME, NULL) == FSE_OK);
  21. }
  22. furi_record_close(RECORD_STORAGE);
  23. return dict_present;
  24. }
  25. IclassEliteDict* iclass_elite_dict_alloc(IclassEliteDictType dict_type) {
  26. IclassEliteDict* dict = malloc(sizeof(IclassEliteDict));
  27. Storage* storage = furi_record_open(RECORD_STORAGE);
  28. dict->stream = buffered_file_stream_alloc(storage);
  29. FuriString* next_line = furi_string_alloc();
  30. bool dict_loaded = false;
  31. do {
  32. if(dict_type == IclassEliteDictTypeFlipper) {
  33. if(!buffered_file_stream_open(
  34. dict->stream, ICLASS_ELITE_DICT_FLIPPER_NAME, FSAM_READ, FSOM_OPEN_EXISTING)) {
  35. buffered_file_stream_close(dict->stream);
  36. break;
  37. }
  38. } else if(dict_type == IclassEliteDictTypeUser) {
  39. if(!buffered_file_stream_open(
  40. dict->stream, ICLASS_ELITE_DICT_USER_NAME, FSAM_READ_WRITE, FSOM_OPEN_ALWAYS)) {
  41. buffered_file_stream_close(dict->stream);
  42. break;
  43. }
  44. }
  45. // Read total amount of keys
  46. while(true) { //-V547
  47. if(!stream_read_line(dict->stream, next_line)) break;
  48. if(furi_string_get_char(next_line, 0) == '#') continue;
  49. if(furi_string_size(next_line) != ICLASS_ELITE_KEY_LINE_LEN) continue;
  50. dict->total_keys++;
  51. }
  52. furi_string_reset(next_line);
  53. stream_rewind(dict->stream);
  54. dict_loaded = true;
  55. FURI_LOG_I(TAG, "Loaded dictionary with %lu keys", dict->total_keys);
  56. } while(false);
  57. if(!dict_loaded) { //-V547
  58. buffered_file_stream_close(dict->stream);
  59. free(dict);
  60. dict = NULL;
  61. }
  62. furi_record_close(RECORD_STORAGE);
  63. furi_string_free(next_line);
  64. return dict;
  65. }
  66. void iclass_elite_dict_free(IclassEliteDict* dict) {
  67. furi_assert(dict);
  68. furi_assert(dict->stream);
  69. buffered_file_stream_close(dict->stream);
  70. stream_free(dict->stream);
  71. free(dict);
  72. }
  73. uint32_t iclass_elite_dict_get_total_keys(IclassEliteDict* dict) {
  74. furi_assert(dict);
  75. return dict->total_keys;
  76. }
  77. bool iclass_elite_dict_get_next_key(IclassEliteDict* dict, uint8_t* key) {
  78. furi_assert(dict);
  79. furi_assert(dict->stream);
  80. uint8_t key_byte_tmp = 0;
  81. FuriString* next_line = furi_string_alloc();
  82. bool key_read = false;
  83. *key = 0ULL;
  84. while(!key_read) {
  85. if(!stream_read_line(dict->stream, next_line)) break;
  86. if(furi_string_get_char(next_line, 0) == '#') continue;
  87. if(furi_string_size(next_line) != ICLASS_ELITE_KEY_LINE_LEN) continue;
  88. for(uint8_t i = 0; i < ICLASS_ELITE_KEY_LEN * 2; i += 2) {
  89. args_char_to_hex(
  90. furi_string_get_char(next_line, i),
  91. furi_string_get_char(next_line, i + 1),
  92. &key_byte_tmp);
  93. key[i / 2] = key_byte_tmp;
  94. }
  95. key_read = true;
  96. }
  97. furi_string_free(next_line);
  98. return key_read;
  99. }
  100. bool iclass_elite_dict_rewind(IclassEliteDict* dict) {
  101. furi_assert(dict);
  102. furi_assert(dict->stream);
  103. return stream_rewind(dict->stream);
  104. }
  105. bool iclass_elite_dict_add_key(IclassEliteDict* dict, uint8_t* key) {
  106. furi_assert(dict);
  107. furi_assert(dict->stream);
  108. FuriString* key_str = furi_string_alloc();
  109. for(size_t i = 0; i < 6; i++) {
  110. furi_string_cat_printf(key_str, "%02X", key[i]);
  111. }
  112. furi_string_cat_printf(key_str, "\n");
  113. bool key_added = false;
  114. do {
  115. if(!stream_seek(dict->stream, 0, StreamOffsetFromEnd)) break;
  116. if(!stream_insert_string(dict->stream, key_str)) break;
  117. key_added = true;
  118. } while(false);
  119. furi_string_free(key_str);
  120. return key_added;
  121. }