Skip to content

Commit

Permalink
1.2.2 - Migrate to file_stream, fix more memory leaks (this time not …
Browse files Browse the repository at this point in the history
…mine)
  • Loading branch information
AloneLiberty committed Apr 25, 2023
1 parent 30a3796 commit af49313
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mifare_nested_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <lib/nfc/nfc_device.h>
#include "mifare_nested_icons.h"

#define NESTED_VERSION_APP "1.2.1"
#define NESTED_VERSION_APP "1.2.2"
#define NESTED_GITHUB_LINK "https://github.com/AloneLiberty/FlipperNested"
#define NESTED_RECOVER_KEYS_GITHUB_LINK "https://github.com/AloneLiberty/FlipperNestedRecovery"
#define NESTED_NONCE_FORMAT_VERSION "3"
Expand Down
42 changes: 20 additions & 22 deletions mifare_nested_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <storage/storage.h>
#include <stream/stream.h>
#include <stream/buffered_file_stream.h>
#include <stream/file_stream.h>
#include "string.h"
#include <furi.h>
#include <furi_hal.h>
Expand Down Expand Up @@ -471,11 +471,10 @@ void mifare_nested_worker_write_nonces(
uint32_t delay,
uint32_t distance) {
FuriString* path = furi_string_alloc();
Stream* file_stream = buffered_file_stream_alloc(storage);
Stream* file_stream = file_stream_alloc(storage);
mifare_nested_worker_get_nonces_file_path(data, path);

buffered_file_stream_open(
file_stream, furi_string_get_cstr(path), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);

FuriString* header = furi_string_alloc_printf(
"Filetype: Flipper Nested Nonce Manifest File\nVersion: %s\nNote: you will need desktop app to recover keys: %s\n",
Expand Down Expand Up @@ -544,7 +543,7 @@ void mifare_nested_worker_write_nonces(

free(nonces);
furi_string_free(path);
buffered_file_stream_close(file_stream);
file_stream_close(file_stream);
free(file_stream);
furi_record_close(RECORD_STORAGE);
}
Expand Down Expand Up @@ -867,12 +866,12 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
continue;
}

Stream* file_stream = buffered_file_stream_alloc(storage);
Stream* file_stream = file_stream_alloc(storage);
FuriString* hardnested_file = furi_string_alloc();
mifare_nested_worker_get_hardnested_file_path(
&data, hardnested_file, sector, key_type);

buffered_file_stream_open(
file_stream_open(
file_stream,
furi_string_get_cstr(hardnested_file),
FSAM_READ_WRITE,
Expand Down Expand Up @@ -909,7 +908,7 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_

if(result.static_encrypted) {
// TODO: Delete file?
buffered_file_stream_close(file_stream);
file_stream_close(file_stream);
free(found);
free(mf_data);
nfc_deactivate();
Expand Down Expand Up @@ -958,7 +957,7 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
free(found);
}

buffered_file_stream_close(file_stream);
file_stream_close(file_stream);
}
}

Expand Down Expand Up @@ -1269,8 +1268,8 @@ bool* mifare_nested_worker_check_keys_exists(
uint32_t key_count,
MifareNestedWorker* mifare_nested_worker) {
bool* old_keys = malloc(sizeof(bool) * key_count);
Stream* file_stream = buffered_file_stream_alloc(storage);
buffered_file_stream_open(file_stream, path, FSAM_READ, FSOM_OPEN_ALWAYS);
Stream* file_stream = file_stream_alloc(storage);
file_stream_open(file_stream, path, FSAM_READ, FSOM_OPEN_ALWAYS);
FuriString* key_strings[key_count];

for(uint32_t i = 0; i < key_count; i++) {
Expand Down Expand Up @@ -1300,29 +1299,29 @@ bool* mifare_nested_worker_check_keys_exists(
furi_string_free(key_strings[i]);
}

buffered_file_stream_close(file_stream);
file_stream_close(file_stream);
free(file_stream);

return old_keys;
}

void mifare_nested_worker_write_key(Storage* storage, FuriString* key) {
Stream* file_stream = buffered_file_stream_alloc(storage);
buffered_file_stream_open(
Stream* file_stream = file_stream_alloc(storage);
file_stream_open(
file_stream,
EXT_PATH("nfc/assets/mf_classic_dict_user.nfc"),
FSAM_READ_WRITE,
FSOM_OPEN_APPEND);

stream_write_string(file_stream, key);

buffered_file_stream_close(file_stream);
file_stream_close(file_stream);
}

void mifare_nested_worker_check_keys(MifareNestedWorker* mifare_nested_worker) {
KeyInfo_t* key_info = mifare_nested_worker->context->keys;
Storage* storage = furi_record_open(RECORD_STORAGE);
Stream* file_stream = buffered_file_stream_alloc(storage);
Stream* file_stream = file_stream_alloc(storage);
FuriString* next_line = furi_string_alloc();
FuriString* path = furi_string_alloc();
FuriHalNfcDevData data = {};
Expand Down Expand Up @@ -1363,15 +1362,14 @@ void mifare_nested_worker_check_keys(MifareNestedWorker* mifare_nested_worker) {

mifare_nested_worker_get_found_keys_file_path(&data, path);

if(!buffered_file_stream_open(
file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
if(!file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Can't open %s", furi_string_get_cstr(path));

buffered_file_stream_close(file_stream);
file_stream_close(file_stream);

mifare_nested_worker_get_nonces_file_path(&data, path);

if(!buffered_file_stream_open(
if(!file_stream_open(
file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
mifare_nested_worker->callback(
MifareNestedWorkerEventNeedCollection, mifare_nested_worker->context);
Expand All @@ -1380,7 +1378,7 @@ void mifare_nested_worker_check_keys(MifareNestedWorker* mifare_nested_worker) {
MifareNestedWorkerEventNeedKeyRecovery, mifare_nested_worker->context);
}

buffered_file_stream_close(file_stream);
file_stream_close(file_stream);

free(file_stream);
furi_string_free(path);
Expand Down Expand Up @@ -1488,7 +1486,7 @@ void mifare_nested_worker_check_keys(MifareNestedWorker* mifare_nested_worker) {
}

furi_string_free(next_line);
buffered_file_stream_close(file_stream);
file_stream_close(file_stream);
free(file_stream);

mifare_nested_worker->callback(
Expand Down

0 comments on commit af49313

Please sign in to comment.