[FL-1791] Flipper file format (#740)

* Lib: new flipper file format library
* Lib: flipper file format cpp wrapper
* Storage: simple function for remove file and check error
* iButton app: remove file worker, use new flipper file format instead
* Dialogs: storage error message
* Storage: simple function for mkdir and check error
* iButton app: error messages
* Libs: update makefile
* RFID app: remove file worker, use new flipper file format instead
* Flipper File: library documentation

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
SG
2021-10-06 19:40:28 +10:00
committed by GitHub
parent e0c1928fde
commit c8b36dd406
15 changed files with 1100 additions and 158 deletions

View File

@@ -0,0 +1,72 @@
#include "flipper-file-cpp.h"
FlipperFileCpp::FlipperFileCpp(Storage* storage) {
file = flipper_file_alloc(storage);
}
FlipperFileCpp::~FlipperFileCpp() {
flipper_file_free(file);
}
bool FlipperFileCpp::open_read(const char* filename) {
return flipper_file_open_read(file, filename);
}
bool FlipperFileCpp::new_write(const char* filename) {
return flipper_file_new_write(file, filename);
}
bool FlipperFileCpp::close() {
return flipper_file_close(file);
}
bool FlipperFileCpp::read_header(string_t filetype, uint32_t* version) {
return flipper_file_read_header(file, filetype, version);
}
bool FlipperFileCpp::write_header(string_t filetype, const uint32_t version) {
return flipper_file_write_header(file, filetype, version);
}
bool FlipperFileCpp::write_header_cstr(const char* filetype, const uint32_t version) {
return flipper_file_write_header_cstr(file, filetype, version);
}
bool FlipperFileCpp::read_string(const char* key, string_t data) {
return flipper_file_read_string(file, key, data);
}
bool FlipperFileCpp::write_string(const char* key, string_t data) {
return flipper_file_write_string(file, key, data);
}
bool FlipperFileCpp::write_string_cstr(const char* key, const char* data) {
return flipper_file_write_string_cstr(file, key, data);
}
bool FlipperFileCpp::read_uint32(const char* key, uint32_t* data) {
return flipper_file_read_uint32(file, key, data);
}
bool FlipperFileCpp::write_uint32(const char* key, const uint32_t data) {
return flipper_file_write_uint32(file, key, data);
}
bool FlipperFileCpp::write_comment(string_t data) {
return flipper_file_write_comment(file, data);
}
bool FlipperFileCpp::write_comment_cstr(const char* data) {
return flipper_file_write_comment_cstr(file, data);
}
bool FlipperFileCpp::write_hex_array(
const char* key,
const uint8_t* data,
const uint16_t data_size) {
return flipper_file_write_hex_array(file, key, data, data_size);
}
bool FlipperFileCpp::read_hex_array(const char* key, uint8_t* data, const uint16_t data_size) {
return flipper_file_read_hex_array(file, key, data, data_size);
}

View File

@@ -0,0 +1,41 @@
#pragma once
#include "flipper-file.h"
class FlipperFileCpp {
private:
FlipperFile* file;
public:
FlipperFileCpp(Storage* storage);
~FlipperFileCpp();
bool open_read(const char* filename);
bool new_write(const char* filename);
bool close();
bool read_header(string_t filetype, uint32_t* version);
bool write_header(string_t filetype, const uint32_t version);
bool write_header_cstr(const char* filetype, const uint32_t version);
bool read_string(const char* key, string_t data);
bool write_string(const char* key, string_t data);
bool write_string_cstr(const char* key, const char* data);
bool read_uint32(const char* key, uint32_t* data);
bool write_uint32(const char* key, const uint32_t data);
bool write_comment(string_t data);
bool write_comment_cstr(const char* data);
bool write_hex_array(const char* key, const uint8_t* data, const uint16_t data_size);
bool read_hex_array(const char* key, uint8_t* data, const uint16_t data_size);
};

464
lib/toolbox/flipper-file.c Normal file
View File

@@ -0,0 +1,464 @@
#include <furi.h>
#include "flipper-file.h"
#include <toolbox/hex.h>
#include <inttypes.h>
struct FlipperFile {
File* file;
};
const char* flipper_file_filetype_key = "Filetype";
const char* flipper_file_version_key = "Version";
const char flipper_file_eoln = '\n';
const char flipper_file_eolr = '\r';
const char flipper_file_delimiter = ':';
const char flipper_file_comment = '#';
/**
* Writes data to a file as a hexadecimal array.
* @param file
* @param data
* @param data_size
* @return true on success write
*/
bool flipper_file_write_hex_internal(File* file, const uint8_t* data, const uint16_t data_size) {
const uint8_t byte_text_size = 3;
char byte_text[byte_text_size];
bool result = true;
uint16_t bytes_written;
for(uint8_t i = 0; i < data_size; i++) {
snprintf(byte_text, byte_text_size, "%02X", data[i]);
if(i != 0) {
// space
const char space = ' ';
bytes_written = storage_file_write(file, &space, sizeof(space));
if(bytes_written != sizeof(space)) {
result = false;
break;
}
}
bytes_written = storage_file_write(file, &byte_text, strlen(byte_text));
if(bytes_written != strlen(byte_text)) {
result = false;
break;
}
}
return result;
}
/**
* Reads a valid key from a file as a string.
* After reading, the rw pointer will be on the flipper_file_delimiter symbol.
* Optimized not to read comments and values into RAM.
* @param file
* @param key
* @return true on success read
*/
bool flipper_file_read_valid_key(File* file, string_t key) {
string_clean(key);
bool found = false;
bool error = false;
const uint8_t buffer_size = 32;
uint8_t buffer[buffer_size];
bool accumulate = true;
bool new_line = true;
while(true) {
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
if(bytes_were_read == 0) break;
for(uint16_t i = 0; i < bytes_were_read; i++) {
if(buffer[i] == flipper_file_eoln) {
// EOL found, clean data, start accumulating data and set the new_line flag
string_clean(key);
accumulate = true;
new_line = true;
} else if(buffer[i] == flipper_file_eolr) {
// Ignore
} else if(buffer[i] == flipper_file_comment && new_line) {
// if there is a comment character and we are at the beginning of a new line
// do not accumulate comment data and reset the new_line flag
accumulate = false;
new_line = false;
} else if(buffer[i] == flipper_file_delimiter) {
if(new_line) {
// we are on a "new line" and found the delimiter
// this can only be if we have previously found some kind of key, so
// clear the data, set the flag that we no longer want to accumulate data
// and reset the new_line flag
string_clean(key);
accumulate = false;
new_line = false;
} else {
// parse the delimiter only if we are accumulating data
if(accumulate) {
// we found the delimiter, move the rw pointer to the correct location
// and signal that we have found something
// TODO negative seek
uint64_t position = storage_file_tell(file);
position = position - bytes_were_read + i;
if(!storage_file_seek(file, position, true)) {
error = true;
break;
}
found = true;
break;
}
}
} else {
// just new symbol, reset the new_line flag
new_line = false;
if(accumulate) {
// and accumulate data if we want
string_push_back(key, buffer[i]);
}
}
}
if(found || error) break;
}
return found;
}
/**
* Sets rw pointer to the data after the key
* @param file
* @param key
* @return true if key was found
*/
bool flipper_file_seek_to_key(File* file, const char* key) {
bool found = false;
string_t readed_key;
string_init(readed_key);
// TODO optimize this to search from a stored rw pointer
if(storage_file_seek(file, 0, true)) {
while(!storage_file_eof(file)) {
if(flipper_file_read_valid_key(file, readed_key)) {
if(string_cmp_str(readed_key, key) == 0) {
uint64_t position = storage_file_tell(file);
if(!storage_file_seek(file, position + 2, true)) break;
found = true;
break;
}
}
}
}
string_clear(readed_key);
return found;
}
/**
* Reads data as a string from the stored rw pointer to the \r or \n symbol position
* @param file
* @param str_result
* @return true on success read
*/
bool flipper_file_read_until(File* file, string_t str_result) {
string_clean(str_result);
const uint8_t buffer_size = 32;
uint8_t buffer[buffer_size];
do {
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
if(bytes_were_read == 0) break;
bool result = false;
bool error = false;
for(uint16_t i = 0; i < bytes_were_read; i++) {
if(buffer[i] == flipper_file_eoln) {
// TODO negative seek
uint64_t position = storage_file_tell(file);
position = position - bytes_were_read + i;
if(!storage_file_seek(file, position, true)) {
error = true;
break;
}
result = true;
break;
} else if(buffer[i] == flipper_file_eolr) {
// Ignore
} else {
string_push_back(str_result, buffer[i]);
}
}
if(result || error) {
break;
}
} while(true);
return string_size(str_result) != 0;
}
/**
* Reads single hexadecimal data from a file to byte
* @param file
* @param byte
* @return bool
*/
bool flipper_file_read_hex_byte(File* file, uint8_t* byte) {
uint8_t hi_nibble_value, low_nibble_value;
uint8_t text[3];
bool result = false;
uint16_t bytes_were_read = storage_file_read(file, text, 3);
if(bytes_were_read >= 2) {
if(text[0] != ' ') {
if(hex_char_to_hex_nibble(text[0], &hi_nibble_value) &&
hex_char_to_hex_nibble(text[1], &low_nibble_value)) {
*byte = (hi_nibble_value << 4) | low_nibble_value;
result = true;
}
} else {
if(hex_char_to_hex_nibble(text[1], &hi_nibble_value) &&
hex_char_to_hex_nibble(text[2], &low_nibble_value)) {
*byte = (hi_nibble_value << 4) | low_nibble_value;
result = true;
}
}
}
return result;
}
FlipperFile* flipper_file_alloc(Storage* storage) {
FlipperFile* flipper_file = malloc(sizeof(FlipperFile));
flipper_file->file = storage_file_alloc(storage);
return flipper_file;
}
void flipper_file_free(FlipperFile* flipper_file) {
furi_assert(flipper_file);
if(storage_file_is_open(flipper_file->file)) {
storage_file_close(flipper_file->file);
}
storage_file_free(flipper_file->file);
free(flipper_file);
}
bool flipper_file_open_read(FlipperFile* flipper_file, const char* filename) {
furi_assert(flipper_file);
bool result = storage_file_open(flipper_file->file, filename, FSAM_READ, FSOM_OPEN_EXISTING);
return result;
}
bool flipper_file_new_write(FlipperFile* flipper_file, const char* filename) {
furi_assert(flipper_file);
bool result = storage_file_open(flipper_file->file, filename, FSAM_WRITE, FSOM_CREATE_ALWAYS);
return result;
}
bool flipper_file_close(FlipperFile* flipper_file) {
furi_assert(flipper_file);
if(storage_file_is_open(flipper_file->file)) {
return storage_file_close(flipper_file->file);
}
return true;
}
bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version) {
bool result = false;
do {
result = flipper_file_read_string(flipper_file, flipper_file_filetype_key, filetype);
if(!result) break;
result = flipper_file_read_uint32(flipper_file, flipper_file_version_key, version);
if(!result) break;
} while(false);
return result;
}
bool flipper_file_write_header(
FlipperFile* flipper_file,
string_t filetype,
const uint32_t version) {
bool result = false;
do {
result = flipper_file_write_string(flipper_file, flipper_file_filetype_key, filetype);
if(!result) break;
result = flipper_file_write_uint32(flipper_file, flipper_file_version_key, version);
if(!result) break;
} while(false);
return result;
}
bool flipper_file_write_header_cstr(
FlipperFile* flipper_file,
const char* filetype,
const uint32_t version) {
bool result = false;
string_t value;
string_init_set(value, filetype);
result = flipper_file_write_header(flipper_file, value, version);
string_clear(value);
return result;
}
bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data) {
furi_assert(flipper_file);
bool result = false;
if(flipper_file_seek_to_key(flipper_file->file, key)) {
if(flipper_file_read_until(flipper_file->file, data)) {
result = true;
}
}
return result;
}
bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data) {
furi_assert(flipper_file);
bool result = false;
do {
uint16_t bytes_written;
bytes_written = storage_file_write(flipper_file->file, key, strlen(key));
if(bytes_written != strlen(key)) break;
const char delimiter_buffer[2] = {flipper_file_delimiter, ' '};
bytes_written =
storage_file_write(flipper_file->file, delimiter_buffer, sizeof(delimiter_buffer));
if(bytes_written != sizeof(delimiter_buffer)) break;
bytes_written =
storage_file_write(flipper_file->file, string_get_cstr(data), string_size(data));
if(bytes_written != string_size(data)) break;
bytes_written =
storage_file_write(flipper_file->file, &flipper_file_eoln, sizeof(flipper_file_eoln));
if(bytes_written != sizeof(flipper_file_eoln)) break;
result = true;
} while(false);
return result;
}
bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data) {
bool result = false;
string_t value;
string_init_set(value, data);
result = flipper_file_write_string(flipper_file, key, value);
string_clear(value);
return result;
}
bool flipper_file_read_uint32(FlipperFile* flipper_file, const char* key, uint32_t* data) {
bool result = false;
string_t value;
string_init(value);
result = flipper_file_read_string(flipper_file, key, value);
if(result) {
int ret = sscanf(string_get_cstr(value), "%" PRIu32, data);
if(ret != 1) result = false;
}
string_clear(value);
return result;
}
bool flipper_file_write_uint32(FlipperFile* flipper_file, const char* key, const uint32_t data) {
bool result = false;
string_t value;
string_init_printf(value, "%" PRIu32, data);
result = flipper_file_write_string(flipper_file, key, value);
string_clear(value);
return result;
}
bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data) {
furi_assert(flipper_file);
bool result = false;
do {
uint16_t bytes_written;
const char comment_buffer[2] = {flipper_file_comment, ' '};
bytes_written =
storage_file_write(flipper_file->file, comment_buffer, sizeof(comment_buffer));
if(bytes_written != sizeof(comment_buffer)) break;
bytes_written =
storage_file_write(flipper_file->file, string_get_cstr(data), string_size(data));
if(bytes_written != string_size(data)) break;
bytes_written =
storage_file_write(flipper_file->file, &flipper_file_eoln, sizeof(flipper_file_eoln));
if(bytes_written != sizeof(flipper_file_eoln)) break;
result = true;
} while(false);
return result;
}
bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data) {
bool result = false;
string_t value;
string_init_set(value, data);
result = flipper_file_write_comment(flipper_file, value);
string_clear(value);
return result;
}
bool flipper_file_write_hex_array(
FlipperFile* flipper_file,
const char* key,
const uint8_t* data,
const uint16_t data_size) {
furi_assert(flipper_file);
bool result = false;
do {
uint16_t bytes_written;
bytes_written = storage_file_write(flipper_file->file, key, strlen(key));
if(bytes_written != strlen(key)) break;
const char delimiter_buffer[2] = {flipper_file_delimiter, ' '};
bytes_written =
storage_file_write(flipper_file->file, delimiter_buffer, sizeof(delimiter_buffer));
if(bytes_written != sizeof(delimiter_buffer)) break;
if(!flipper_file_write_hex_internal(flipper_file->file, data, data_size)) break;
bytes_written =
storage_file_write(flipper_file->file, &flipper_file_eoln, sizeof(flipper_file_eoln));
if(bytes_written != sizeof(flipper_file_eoln)) break;
result = true;
} while(false);
return result;
}
bool flipper_file_read_hex_array(
FlipperFile* flipper_file,
const char* key,
uint8_t* data,
const uint16_t data_size) {
furi_assert(flipper_file);
bool result = false;
if(flipper_file_seek_to_key(flipper_file->file, key)) {
result = true;
for(uint16_t i = 0; i < data_size; i++) {
if(!flipper_file_read_hex_byte(flipper_file->file, &data[i])) {
result = false;
break;
}
}
}
return result;
}

267
lib/toolbox/flipper-file.h Normal file
View File

@@ -0,0 +1,267 @@
/**
* @file flipper-file.h
* Flipper File Format helper library.
*
* Flipper File Format is a fairly simple format for storing data in a file.
*
* Flipper file structure:
*
* ~~~~~~~~~~~~~~~~~~~~~
* # Commentary
* Field name: field value
* ~~~~~~~~~~~~~~~~~~~~~
*
* Lines starting with the # character are ignored (considered as comments). The separator between the name of the value and the value itself is the string ": ".
*
* Currently supported types:
*
* ~~~~~~~~~~~~~~~~~~~~~
* String: text
* Uint32: 1
* Hex Array: A4 B3 C2 D1 12 FF
* ~~~~~~~~~~~~~~~~~~~~~
*
* End of line is LF when writing, but CR is supported when reading.
*
* The library is designed in such a way that comments and field values are completely ignored when searching for keys, that is, they do not consume memory.
*
* File example:
*
* ~~~~~~~~~~~~~~~~~~~~~
* Filetype: Flipper Test File
* Version: 1
* # Just test file
* String: String value
* UINT: 1234
* Hex Array: 00 01 FF A3
* ~~~~~~~~~~~~~~~~~~~~~
*
* Writing:
*
* ~~~~~~~~~~~~~~~~~~~~~
* FlipperFile file = flipper_file_alloc(storage);
*
* do {
* const uint32_t version = 1;
* const char* string_value = "String value";
* const uint32_t uint32_value = 1234;
* const uint16_t array_size = 4;
* const uint8_t* array[array_size] = {0x00, 0x01, 0xFF, 0xA3};
*
* if(!flipper_file_new_write(file, "/ext/flipper_file_test")) break;
* if(!flipper_file_write_header_cstr(file, "Flipper Test File", version)) break;
* if(!flipper_file_write_comment_cstr(file, "Just test file")) break;
* if(!flipper_file_write_string_cstr(file, "String", string_value)) break;
* if(!flipper_file_flipper_file_write_uint32(file, "UINT", uint32_value)) break;
* if(!flipper_file_write_hex_array(file, "Hex Array", array, array_size)) break;
*
* // signal that the file was written successfully
* } while(0);
*
* flipper_file_close(file);
* flipper_file_free(file);
* ~~~~~~~~~~~~~~~~~~~~~
*
* Reading:
*
* ~~~~~~~~~~~~~~~~~~~~~
* FlipperFile file = flipper_file_alloc(storage);
*
* do {
* uint32_t version = 1;
* string_t file_type;
* string_t string_value;
* uint32_t uint32_value = 1;
* uint16_t array_size = 4;
* uint8_t* array[array_size] = {0};
* string_init(file_type);
* string_init(string_value);
*
* if(!flipper_file_open_read(file, "/ext/flipper_file_test")) break;
* if(!flipper_file_read_header(file, file_type, &version)) break;
* if(!flipper_file_read_string(file, "String", string_value)) break;
* if(!flipper_file_read_uint32(file, "UINT", &uint32_value)) break;
* if(!flipper_file_read_hex_array(file, "Hex Array", array, array_size)) break;
*
* // signal that the file was read successfully
* } while(0);
*
* flipper_file_close(file);
* flipper_file_free(file);
* ~~~~~~~~~~~~~~~~~~~~~
*
*/
#pragma once
#include <stdint.h>
#include <mlib/m-string.h>
#include <storage/storage.h>
#ifdef __cplusplus
extern "C" {
#endif
/** FlipperFile type anonymous structure. */
typedef struct FlipperFile FlipperFile;
/**
* Allocate FlipperFile.
* @param storage storage api
* @return FlipperFile* Pointer to a FlipperFile instance
*/
FlipperFile* flipper_file_alloc(Storage* storage);
/**
* Free FlipperFile.
* @param flipper_file Pointer to a FlipperFile instance
*/
void flipper_file_free(FlipperFile* flipper_file);
/**
* Open file for reading.
* @param flipper_file Pointer to a FlipperFile instance
* @param filename File name and path
* @return True on success
*/
bool flipper_file_open_read(FlipperFile* flipper_file, const char* filename);
/**
* Open file for writing. Creates a new file, or deletes the contents of the file if it already exists.
* @param flipper_file Pointer to a FlipperFile instance
* @param filename File name and path
* @return True on success
*/
bool flipper_file_new_write(FlipperFile* flipper_file, const char* filename);
/**
* Close the file.
* @param flipper_file Pointer to a FlipperFile instance
* @return True on success
*/
bool flipper_file_close(FlipperFile* flipper_file);
/**
* Read the header (file type and version) from the file.
* @param flipper_file Pointer to a FlipperFile instance
* @param filetype File type string
* @param version Version Value
* @return True on success
*/
bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version);
/**
* Write the header (file type and version) to the file.
* @param flipper_file Pointer to a FlipperFile instance
* @param filetype File type string
* @param version Version Value
* @return True on success
*/
bool flipper_file_write_header(
FlipperFile* flipper_file,
string_t filetype,
const uint32_t version);
/**
* Write the header (file type and version) to the file. Plain C string version.
* @param flipper_file Pointer to a FlipperFile instance
* @param filetype File type string
* @param version Version Value
* @return True on success
*/
bool flipper_file_write_header_cstr(
FlipperFile* flipper_file,
const char* filetype,
const uint32_t version);
/**
* Read a string from a file by Key
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data);
/**
* Write key and string to file.
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data);
/**
* Write key and string to file. Plain C string version.
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
/**
* Read uint32 from a file by Key
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_file_read_uint32(FlipperFile* flipper_file, const char* key, uint32_t* data);
/**
* Write key and uint32 to file.
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_file_write_uint32(FlipperFile* flipper_file, const char* key, const uint32_t data);
/**
* Write comment to file.
* @param flipper_file Pointer to a FlipperFile instance
* @param data Comment text
* @return True on success
*/
bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data);
/**
* Write comment to file. Plain C string version.
* @param flipper_file Pointer to a FlipperFile instance
* @param data Comment text
* @return True on success
*/
bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data);
/**
* Read hex array from a file by Key
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @param data_size Value size
* @return True on success
*/
bool flipper_file_read_hex_array(
FlipperFile* flipper_file,
const char* key,
uint8_t* data,
const uint16_t data_size);
/**
* Write key and hex array to file.
* @param flipper_file Pointer to a FlipperFile instance
* @param key Key
* @param data Value
* @param data_size Value size
* @return True on success
*/
bool flipper_file_write_hex_array(
FlipperFile* flipper_file,
const char* key,
const uint8_t* data,
const uint16_t data_size);
#ifdef __cplusplus
}
#endif