nfc_emv_parser.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "nfc_emv_parser.h"
  2. #include <flipper_format/flipper_format.h>
  3. static const char* nfc_resources_header = "Flipper EMV resources";
  4. static const uint32_t nfc_resources_file_version = 1;
  5. static bool nfc_emv_parser_search_data(
  6. Storage* storage,
  7. const char* file_name,
  8. FuriString* key,
  9. FuriString* data) {
  10. bool parsed = false;
  11. FlipperFormat* file = flipper_format_file_alloc(storage);
  12. FuriString* temp_str;
  13. temp_str = furi_string_alloc();
  14. do {
  15. // Open file
  16. if(!flipper_format_file_open_existing(file, file_name)) break;
  17. // Read file header and version
  18. uint32_t version = 0;
  19. if(!flipper_format_read_header(file, temp_str, &version)) break;
  20. if(furi_string_cmp_str(temp_str, nfc_resources_header) ||
  21. (version != nfc_resources_file_version))
  22. break;
  23. if(!flipper_format_read_string(file, furi_string_get_cstr(key), data)) break;
  24. parsed = true;
  25. } while(false);
  26. furi_string_free(temp_str);
  27. flipper_format_free(file);
  28. return parsed;
  29. }
  30. bool nfc_emv_parser_get_aid_name(
  31. Storage* storage,
  32. uint8_t* aid,
  33. uint8_t aid_len,
  34. FuriString* aid_name) {
  35. furi_assert(storage);
  36. bool parsed = false;
  37. FuriString* key;
  38. key = furi_string_alloc();
  39. for(uint8_t i = 0; i < aid_len; i++) {
  40. furi_string_cat_printf(key, "%02X", aid[i]);
  41. }
  42. if(nfc_emv_parser_search_data(storage, EXT_PATH("nfc/assets/aid.nfc"), key, aid_name)) {
  43. parsed = true;
  44. }
  45. furi_string_free(key);
  46. return parsed;
  47. }
  48. bool nfc_emv_parser_get_country_name(
  49. Storage* storage,
  50. uint16_t country_code,
  51. FuriString* country_name) {
  52. bool parsed = false;
  53. FuriString* key;
  54. key = furi_string_alloc_printf("%04X", country_code);
  55. if(nfc_emv_parser_search_data(
  56. storage, EXT_PATH("nfc/assets/country_code.nfc"), key, country_name)) {
  57. parsed = true;
  58. }
  59. furi_string_free(key);
  60. return parsed;
  61. }
  62. bool nfc_emv_parser_get_currency_name(
  63. Storage* storage,
  64. uint16_t currency_code,
  65. FuriString* currency_name) {
  66. bool parsed = false;
  67. FuriString* key;
  68. key = furi_string_alloc_printf("%04X", currency_code);
  69. if(nfc_emv_parser_search_data(
  70. storage, EXT_PATH("nfc/assets/currency_code.nfc"), key, currency_name)) {
  71. parsed = true;
  72. }
  73. furi_string_free(key);
  74. return parsed;
  75. }