mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-30 16:29:12 +00:00
3857cd7d5f
* Fix TextBox word wrap behavior * Wrap width is 120 pixels, not 140. (140 is larger than the screen!) * Glyph width already includes spacing; don't add 1 additional px * When starting a new line, include wrapped glyph width in new line_width. * Call canvas_set_font before text_box_insert_endline so that glyph width is calculated using correct font. Previous approach worked somewhat well using default TextBoxFontText but this version is more robust, particularly when using TextBoxFontHex. * Add basic Mifare DESFire reading, file/app browser * Fix build with APP_ARCHIVE=0 * Add bool type to flipper_format * Add ability to save and load DESFire card data * Skip over NfcSceneDeviceInfo when viewing saved DESFire info * mf_df_clear: don't leak master key settings key versions * When opening a DESFire card from Archive, retain UID emulation behavior * rm unnecessary \r\n * show Popup instead of leaving view in bad state * Move NfcReaderRequestData out of union This makes it safe to emulate DESFire/EMV without clobbering card data. * Display saved DESFire cards via NfcSceneDeviceInfo * Display and save file metadata even when contents are missing This can happen when a file doesn't allow unauthenticated reads (see the call to mf_df_parse_read_data_response in nfc_worker.c). Co-authored-by: Kevin Wallace <git+flipperzero@kevin.wallace.seattle.wa.us> Co-authored-by: あく <alleteam@gmail.com> Co-authored-by: gornekich <n.gorbadey@gmail.com>
587 lines
No EOL
18 KiB
C
587 lines
No EOL
18 KiB
C
#include <furi/check.h>
|
|
#include <toolbox/stream/stream.h>
|
|
#include <toolbox/stream/string_stream.h>
|
|
#include <toolbox/stream/file_stream.h>
|
|
#include "flipper_format.h"
|
|
#include "flipper_format_i.h"
|
|
#include "flipper_format_stream.h"
|
|
#include "flipper_format_stream_i.h"
|
|
|
|
/********************************** Private **********************************/
|
|
struct FlipperFormat {
|
|
Stream* stream;
|
|
bool strict_mode;
|
|
};
|
|
|
|
static const char* const flipper_format_filetype_key = "Filetype";
|
|
static const char* const flipper_format_version_key = "Version";
|
|
|
|
Stream* flipper_format_get_raw_stream(FlipperFormat* flipper_format) {
|
|
return flipper_format->stream;
|
|
}
|
|
|
|
/********************************** Public **********************************/
|
|
|
|
FlipperFormat* flipper_format_string_alloc() {
|
|
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
|
|
flipper_format->stream = string_stream_alloc();
|
|
flipper_format->strict_mode = false;
|
|
return flipper_format;
|
|
}
|
|
|
|
FlipperFormat* flipper_format_file_alloc(Storage* storage) {
|
|
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
|
|
flipper_format->stream = file_stream_alloc(storage);
|
|
flipper_format->strict_mode = false;
|
|
return flipper_format;
|
|
}
|
|
|
|
bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path) {
|
|
furi_assert(flipper_format);
|
|
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
|
|
}
|
|
|
|
bool flipper_format_file_open_append(FlipperFormat* flipper_format, const char* path) {
|
|
furi_assert(flipper_format);
|
|
|
|
bool result =
|
|
file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_APPEND);
|
|
|
|
// Add EOL if it is not there
|
|
if(stream_size(flipper_format->stream) >= 1) {
|
|
do {
|
|
char last_char;
|
|
result = false;
|
|
|
|
if(!stream_seek(flipper_format->stream, -1, StreamOffsetFromEnd)) break;
|
|
|
|
uint16_t bytes_were_read =
|
|
stream_read(flipper_format->stream, (uint8_t*)&last_char, 1);
|
|
if(bytes_were_read != 1) break;
|
|
|
|
if(last_char != flipper_format_eoln) {
|
|
if(!flipper_format_stream_write_eol(flipper_format->stream)) break;
|
|
}
|
|
|
|
result = true;
|
|
} while(false);
|
|
} else {
|
|
stream_seek(flipper_format->stream, 0, StreamOffsetFromEnd);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_file_open_always(FlipperFormat* flipper_format, const char* path) {
|
|
furi_assert(flipper_format);
|
|
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
|
|
}
|
|
|
|
bool flipper_format_file_open_new(FlipperFormat* flipper_format, const char* path) {
|
|
furi_assert(flipper_format);
|
|
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_NEW);
|
|
}
|
|
|
|
bool flipper_format_file_close(FlipperFormat* flipper_format) {
|
|
furi_assert(flipper_format);
|
|
return file_stream_close(flipper_format->stream);
|
|
}
|
|
|
|
void flipper_format_free(FlipperFormat* flipper_format) {
|
|
furi_assert(flipper_format);
|
|
stream_free(flipper_format->stream);
|
|
free(flipper_format);
|
|
}
|
|
|
|
void flipper_format_set_strict_mode(FlipperFormat* flipper_format, bool strict_mode) {
|
|
flipper_format->strict_mode = strict_mode;
|
|
}
|
|
|
|
bool flipper_format_rewind(FlipperFormat* flipper_format) {
|
|
furi_assert(flipper_format);
|
|
return stream_rewind(flipper_format->stream);
|
|
}
|
|
|
|
bool flipper_format_seek_to_end(FlipperFormat* flipper_format) {
|
|
furi_assert(flipper_format);
|
|
return stream_seek(flipper_format->stream, 0, StreamOffsetFromEnd);
|
|
}
|
|
|
|
bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key) {
|
|
size_t pos = stream_tell(flipper_format->stream);
|
|
stream_seek(flipper_format->stream, 0, StreamOffsetFromStart);
|
|
bool result = flipper_format_stream_seek_to_key(flipper_format->stream, key, false);
|
|
stream_seek(flipper_format->stream, pos, StreamOffsetFromStart);
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_read_header(
|
|
FlipperFormat* flipper_format,
|
|
string_t filetype,
|
|
uint32_t* version) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_read_string(flipper_format, flipper_format_filetype_key, filetype) &&
|
|
flipper_format_read_uint32(flipper_format, flipper_format_version_key, version, 1);
|
|
}
|
|
|
|
bool flipper_format_write_header(
|
|
FlipperFormat* flipper_format,
|
|
string_t filetype,
|
|
const uint32_t version) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_write_header_cstr(flipper_format, string_get_cstr(filetype), version);
|
|
}
|
|
|
|
bool flipper_format_write_header_cstr(
|
|
FlipperFormat* flipper_format,
|
|
const char* filetype,
|
|
const uint32_t version) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_write_string_cstr(
|
|
flipper_format, flipper_format_filetype_key, filetype) &&
|
|
flipper_format_write_uint32(flipper_format, flipper_format_version_key, &version, 1);
|
|
}
|
|
|
|
bool flipper_format_get_value_count(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
uint32_t* count) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_stream_get_value_count(
|
|
flipper_format->stream, key, count, flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_stream_read_value_line(
|
|
flipper_format->stream, key, FlipperStreamValueStr, data, 1, flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueStr,
|
|
.data = string_get_cstr(data),
|
|
.data_size = 1,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_write_string_cstr(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const char* data) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueStr,
|
|
.data = data,
|
|
.data_size = 1,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_read_uint32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
uint32_t* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_stream_read_value_line(
|
|
flipper_format->stream,
|
|
key,
|
|
FlipperStreamValueUint32,
|
|
data,
|
|
data_size,
|
|
flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_write_uint32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const uint32_t* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueUint32,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_read_int32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
int32_t* data,
|
|
const uint16_t data_size) {
|
|
return flipper_format_stream_read_value_line(
|
|
flipper_format->stream,
|
|
key,
|
|
FlipperStreamValueInt32,
|
|
data,
|
|
data_size,
|
|
flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_write_int32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const int32_t* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueInt32,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_read_bool(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
bool* data,
|
|
const uint16_t data_size) {
|
|
return flipper_format_stream_read_value_line(
|
|
flipper_format->stream,
|
|
key,
|
|
FlipperStreamValueBool,
|
|
data,
|
|
data_size,
|
|
flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_write_bool(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const bool* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueBool,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_read_float(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
float* data,
|
|
const uint16_t data_size) {
|
|
return flipper_format_stream_read_value_line(
|
|
flipper_format->stream,
|
|
key,
|
|
FlipperStreamValueFloat,
|
|
data,
|
|
data_size,
|
|
flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_write_float(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const float* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueFloat,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_read_hex(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
uint8_t* data,
|
|
const uint16_t data_size) {
|
|
return flipper_format_stream_read_value_line(
|
|
flipper_format->stream,
|
|
key,
|
|
FlipperStreamValueHex,
|
|
data,
|
|
data_size,
|
|
flipper_format->strict_mode);
|
|
}
|
|
|
|
bool flipper_format_write_hex(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const uint8_t* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueHex,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_write_comment_cstr(flipper_format, string_get_cstr(data));
|
|
}
|
|
|
|
bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data) {
|
|
furi_assert(flipper_format);
|
|
return flipper_format_stream_write_comment_cstr(flipper_format->stream, data);
|
|
}
|
|
|
|
bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueIgnore,
|
|
.data = NULL,
|
|
.data_size = 0,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueStr,
|
|
.data = string_get_cstr(data),
|
|
.data_size = 1,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_string_cstr(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const char* data) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueStr,
|
|
.data = data,
|
|
.data_size = 1,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_uint32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const uint32_t* data,
|
|
const uint16_t data_size) {
|
|
furi_assert(flipper_format);
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueUint32,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_int32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const int32_t* data,
|
|
const uint16_t data_size) {
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueInt32,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_bool(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const bool* data,
|
|
const uint16_t data_size) {
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueBool,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_float(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const float* data,
|
|
const uint16_t data_size) {
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueFloat,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_update_hex(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const uint8_t* data,
|
|
const uint16_t data_size) {
|
|
FlipperStreamWriteData write_data = {
|
|
.key = key,
|
|
.type = FlipperStreamValueHex,
|
|
.data = data,
|
|
.data_size = data_size,
|
|
};
|
|
bool result = flipper_format_stream_delete_key_and_write(
|
|
flipper_format->stream, &write_data, flipper_format->strict_mode);
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_string(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
string_t data) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_string(flipper_format, key, data);
|
|
} else {
|
|
result = flipper_format_update_string(flipper_format, key, data);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_string_cstr(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const char* data) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_string_cstr(flipper_format, key, data);
|
|
} else {
|
|
result = flipper_format_update_string_cstr(flipper_format, key, data);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_uint32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const uint32_t* data,
|
|
const uint16_t data_size) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_uint32(flipper_format, key, data, data_size);
|
|
} else {
|
|
result = flipper_format_update_uint32(flipper_format, key, data, data_size);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_int32(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const int32_t* data,
|
|
const uint16_t data_size) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_int32(flipper_format, key, data, data_size);
|
|
} else {
|
|
result = flipper_format_update_int32(flipper_format, key, data, data_size);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_bool(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const bool* data,
|
|
const uint16_t data_size) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_bool(flipper_format, key, data, data_size);
|
|
} else {
|
|
result = flipper_format_update_bool(flipper_format, key, data, data_size);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_float(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const float* data,
|
|
const uint16_t data_size) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_float(flipper_format, key, data, data_size);
|
|
} else {
|
|
result = flipper_format_update_float(flipper_format, key, data, data_size);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool flipper_format_insert_or_update_hex(
|
|
FlipperFormat* flipper_format,
|
|
const char* key,
|
|
const uint8_t* data,
|
|
const uint16_t data_size) {
|
|
bool result = false;
|
|
|
|
if(!flipper_format_key_exist(flipper_format, key)) {
|
|
flipper_format_seek_to_end(flipper_format);
|
|
result = flipper_format_write_hex(flipper_format, key, data, data_size);
|
|
} else {
|
|
result = flipper_format_update_hex(flipper_format, key, data, data_size);
|
|
}
|
|
|
|
return result;
|
|
} |