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