2022-07-26 15:30:49 +00:00
|
|
|
#include "mf_classic_dict.h"
|
|
|
|
|
|
|
|
#include <lib/toolbox/args.h>
|
|
|
|
#include <lib/flipper_format/flipper_format.h>
|
|
|
|
|
|
|
|
#define MF_CLASSIC_DICT_FLIPPER_PATH EXT_PATH("nfc/assets/mf_classic_dict.nfc")
|
|
|
|
#define MF_CLASSIC_DICT_USER_PATH EXT_PATH("nfc/assets/mf_classic_dict_user.nfc")
|
2022-09-22 17:35:28 +00:00
|
|
|
#define MF_CLASSIC_DICT_UNIT_TEST_PATH EXT_PATH("unit_tests/mf_classic_dict.nfc")
|
2022-07-26 15:30:49 +00:00
|
|
|
|
|
|
|
#define TAG "MfClassicDict"
|
|
|
|
|
|
|
|
#define NFC_MF_CLASSIC_KEY_LEN (13)
|
|
|
|
|
|
|
|
struct MfClassicDict {
|
|
|
|
Stream* stream;
|
|
|
|
uint32_t total_keys;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool mf_classic_dict_check_presence(MfClassicDictType dict_type) {
|
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
|
|
|
|
bool dict_present = false;
|
|
|
|
if(dict_type == MfClassicDictTypeFlipper) {
|
|
|
|
dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_FLIPPER_PATH, NULL) == FSE_OK;
|
|
|
|
} else if(dict_type == MfClassicDictTypeUser) {
|
|
|
|
dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_USER_PATH, NULL) == FSE_OK;
|
2022-09-22 17:35:28 +00:00
|
|
|
} else if(dict_type == MfClassicDictTypeUnitTest) {
|
|
|
|
dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_UNIT_TEST_PATH, NULL) ==
|
|
|
|
FSE_OK;
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
|
|
|
|
return dict_present;
|
|
|
|
}
|
|
|
|
|
|
|
|
MfClassicDict* mf_classic_dict_alloc(MfClassicDictType dict_type) {
|
|
|
|
MfClassicDict* dict = malloc(sizeof(MfClassicDict));
|
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
dict->stream = buffered_file_stream_alloc(storage);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
|
|
|
|
bool dict_loaded = false;
|
|
|
|
do {
|
|
|
|
if(dict_type == MfClassicDictTypeFlipper) {
|
|
|
|
if(!buffered_file_stream_open(
|
|
|
|
dict->stream, MF_CLASSIC_DICT_FLIPPER_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
|
|
|
buffered_file_stream_close(dict->stream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if(dict_type == MfClassicDictTypeUser) {
|
|
|
|
if(!buffered_file_stream_open(
|
|
|
|
dict->stream, MF_CLASSIC_DICT_USER_PATH, FSAM_READ_WRITE, FSOM_OPEN_ALWAYS)) {
|
|
|
|
buffered_file_stream_close(dict->stream);
|
|
|
|
break;
|
|
|
|
}
|
2022-09-22 17:35:28 +00:00
|
|
|
} else if(dict_type == MfClassicDictTypeUnitTest) {
|
|
|
|
if(!buffered_file_stream_open(
|
|
|
|
dict->stream,
|
|
|
|
MF_CLASSIC_DICT_UNIT_TEST_PATH,
|
|
|
|
FSAM_READ_WRITE,
|
|
|
|
FSOM_CREATE_ALWAYS)) {
|
|
|
|
buffered_file_stream_close(dict->stream);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read total amount of keys
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* next_line;
|
|
|
|
next_line = furi_string_alloc();
|
2022-07-26 15:30:49 +00:00
|
|
|
while(true) {
|
2022-10-06 17:07:56 +00:00
|
|
|
if(!stream_read_line(dict->stream, next_line)) {
|
|
|
|
FURI_LOG_T(TAG, "No keys left in dict");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
furi_string_trim(next_line);
|
|
|
|
FURI_LOG_T(
|
|
|
|
TAG,
|
|
|
|
"Read line: %s, len: %d",
|
|
|
|
furi_string_get_cstr(next_line),
|
|
|
|
furi_string_size(next_line));
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(next_line, 0) == '#') continue;
|
2022-10-06 17:07:56 +00:00
|
|
|
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN - 1) continue;
|
2022-07-26 15:30:49 +00:00
|
|
|
dict->total_keys++;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(next_line);
|
2022-07-26 15:30:49 +00:00
|
|
|
stream_rewind(dict->stream);
|
|
|
|
|
|
|
|
dict_loaded = true;
|
|
|
|
FURI_LOG_I(TAG, "Loaded dictionary with %d keys", dict->total_keys);
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
if(!dict_loaded) {
|
|
|
|
buffered_file_stream_close(dict->stream);
|
|
|
|
free(dict);
|
|
|
|
dict = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_classic_dict_free(MfClassicDict* dict) {
|
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
|
|
|
buffered_file_stream_close(dict->stream);
|
|
|
|
stream_free(dict->stream);
|
|
|
|
free(dict);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static void mf_classic_dict_int_to_str(uint8_t* key_int, FuriString* key_str) {
|
|
|
|
furi_string_reset(key_str);
|
2022-09-19 16:43:53 +00:00
|
|
|
for(size_t i = 0; i < 6; i++) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_cat_printf(key_str, "%02X", key_int[i]);
|
2022-09-19 16:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static void mf_classic_dict_str_to_int(FuriString* key_str, uint64_t* key_int) {
|
2022-09-19 16:43:53 +00:00
|
|
|
uint8_t key_byte_tmp;
|
|
|
|
|
|
|
|
*key_int = 0ULL;
|
|
|
|
for(uint8_t i = 0; i < 12; i += 2) {
|
|
|
|
args_char_to_hex(
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_char(key_str, i), furi_string_get_char(key_str, i + 1), &key_byte_tmp);
|
2022-09-22 17:35:28 +00:00
|
|
|
*key_int |= (uint64_t)key_byte_tmp << 8 * (5 - i / 2);
|
2022-09-19 16:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
uint32_t mf_classic_dict_get_total_keys(MfClassicDict* dict) {
|
|
|
|
furi_assert(dict);
|
|
|
|
|
|
|
|
return dict->total_keys;
|
|
|
|
}
|
|
|
|
|
2022-09-19 16:43:53 +00:00
|
|
|
bool mf_classic_dict_rewind(MfClassicDict* dict) {
|
2022-07-26 15:30:49 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-09-19 16:43:53 +00:00
|
|
|
return stream_rewind(dict->stream);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool mf_classic_dict_get_next_key_str(MfClassicDict* dict, FuriString* key) {
|
2022-09-19 16:43:53 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
2022-07-26 15:30:49 +00:00
|
|
|
|
|
|
|
bool key_read = false;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(key);
|
2022-07-26 15:30:49 +00:00
|
|
|
while(!key_read) {
|
2022-09-19 16:43:53 +00:00
|
|
|
if(!stream_read_line(dict->stream, key)) break;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(key, 0) == '#') continue;
|
|
|
|
if(furi_string_size(key) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
|
|
|
furi_string_left(key, 12);
|
2022-07-26 15:30:49 +00:00
|
|
|
key_read = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key_read;
|
|
|
|
}
|
|
|
|
|
2022-09-19 16:43:53 +00:00
|
|
|
bool mf_classic_dict_get_next_key(MfClassicDict* dict, uint64_t* key) {
|
2022-07-26 15:30:49 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* temp_key;
|
|
|
|
temp_key = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
bool key_read = mf_classic_dict_get_next_key_str(dict, temp_key);
|
|
|
|
if(key_read) {
|
|
|
|
mf_classic_dict_str_to_int(temp_key, key);
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(temp_key);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_read;
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool mf_classic_dict_is_key_present_str(MfClassicDict* dict, FuriString* key) {
|
2022-07-26 15:30:49 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* next_line;
|
|
|
|
next_line = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
|
|
|
|
bool key_found = false;
|
|
|
|
stream_rewind(dict->stream);
|
|
|
|
while(!key_found) {
|
|
|
|
if(!stream_read_line(dict->stream, next_line)) break;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(next_line, 0) == '#') continue;
|
|
|
|
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
|
|
|
furi_string_left(next_line, 12);
|
|
|
|
if(!furi_string_equal(key, next_line)) continue;
|
2022-09-19 16:43:53 +00:00
|
|
|
key_found = true;
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
2022-09-19 16:43:53 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(next_line);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mf_classic_dict_is_key_present(MfClassicDict* dict, uint8_t* key) {
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* temp_key;
|
2022-09-19 16:43:53 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
temp_key = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
mf_classic_dict_int_to_str(key, temp_key);
|
|
|
|
bool key_found = mf_classic_dict_is_key_present_str(dict, temp_key);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(temp_key);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_found;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool mf_classic_dict_add_key_str(MfClassicDict* dict, FuriString* key) {
|
2022-09-19 16:43:53 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_cat_printf(key, "\n");
|
2022-07-26 15:30:49 +00:00
|
|
|
|
|
|
|
bool key_added = false;
|
|
|
|
do {
|
|
|
|
if(!stream_seek(dict->stream, 0, StreamOffsetFromEnd)) break;
|
2022-09-19 16:43:53 +00:00
|
|
|
if(!stream_insert_string(dict->stream, key)) break;
|
|
|
|
dict->total_keys++;
|
2022-07-26 15:30:49 +00:00
|
|
|
key_added = true;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_left(key, 12);
|
2022-07-26 15:30:49 +00:00
|
|
|
return key_added;
|
|
|
|
}
|
2022-09-19 16:43:53 +00:00
|
|
|
|
|
|
|
bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key) {
|
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* temp_key;
|
|
|
|
temp_key = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
mf_classic_dict_int_to_str(key, temp_key);
|
|
|
|
bool key_added = mf_classic_dict_add_key_str(dict, temp_key);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(temp_key);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_added;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool mf_classic_dict_get_key_at_index_str(MfClassicDict* dict, FuriString* key, uint32_t target) {
|
2022-09-19 16:43:53 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* next_line;
|
2022-09-19 16:43:53 +00:00
|
|
|
uint32_t index = 0;
|
2022-10-05 15:15:23 +00:00
|
|
|
next_line = furi_string_alloc();
|
|
|
|
furi_string_reset(key);
|
2022-09-19 16:43:53 +00:00
|
|
|
|
|
|
|
bool key_found = false;
|
|
|
|
while(!key_found) {
|
|
|
|
if(!stream_read_line(dict->stream, next_line)) break;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(next_line, 0) == '#') continue;
|
|
|
|
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
2022-09-19 16:43:53 +00:00
|
|
|
if(index++ != target) continue;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set_n(key, next_line, 0, 12);
|
2022-09-19 16:43:53 +00:00
|
|
|
key_found = true;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(next_line);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mf_classic_dict_get_key_at_index(MfClassicDict* dict, uint64_t* key, uint32_t target) {
|
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* temp_key;
|
|
|
|
temp_key = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
bool key_found = mf_classic_dict_get_key_at_index_str(dict, temp_key, target);
|
|
|
|
if(key_found) {
|
|
|
|
mf_classic_dict_str_to_int(temp_key, key);
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(temp_key);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_found;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool mf_classic_dict_find_index_str(MfClassicDict* dict, FuriString* key, uint32_t* target) {
|
2022-09-19 16:43:53 +00:00
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* next_line;
|
|
|
|
next_line = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
|
|
|
|
bool key_found = false;
|
|
|
|
uint32_t index = 0;
|
|
|
|
stream_rewind(dict->stream);
|
|
|
|
while(!key_found) {
|
|
|
|
if(!stream_read_line(dict->stream, next_line)) break;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(next_line, 0) == '#') continue;
|
|
|
|
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
|
|
|
furi_string_left(next_line, 12);
|
|
|
|
if(!furi_string_equal(key, next_line)) continue;
|
2022-09-19 16:43:53 +00:00
|
|
|
key_found = true;
|
|
|
|
*target = index;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(next_line);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mf_classic_dict_find_index(MfClassicDict* dict, uint8_t* key, uint32_t* target) {
|
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* temp_key;
|
|
|
|
temp_key = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
mf_classic_dict_int_to_str(key, temp_key);
|
|
|
|
bool key_found = mf_classic_dict_find_index_str(dict, temp_key, target);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(temp_key);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mf_classic_dict_delete_index(MfClassicDict* dict, uint32_t target) {
|
|
|
|
furi_assert(dict);
|
|
|
|
furi_assert(dict->stream);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* next_line;
|
|
|
|
next_line = furi_string_alloc();
|
2022-09-19 16:43:53 +00:00
|
|
|
uint32_t index = 0;
|
|
|
|
|
|
|
|
bool key_removed = false;
|
|
|
|
while(!key_removed) {
|
|
|
|
if(!stream_read_line(dict->stream, next_line)) break;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(next_line, 0) == '#') continue;
|
|
|
|
if(furi_string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
2022-09-19 16:43:53 +00:00
|
|
|
if(index++ != target) continue;
|
|
|
|
stream_seek(dict->stream, -NFC_MF_CLASSIC_KEY_LEN, StreamOffsetFromCurrent);
|
|
|
|
if(!stream_delete(dict->stream, NFC_MF_CLASSIC_KEY_LEN)) break;
|
|
|
|
dict->total_keys--;
|
|
|
|
key_removed = true;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(next_line);
|
2022-09-19 16:43:53 +00:00
|
|
|
return key_removed;
|
|
|
|
}
|