nfc_emv_parser.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. string_t key,
  9. string_t data) {
  10. bool parsed = false;
  11. FlipperFormat* file = flipper_format_file_alloc(storage);
  12. string_t temp_str;
  13. string_init(temp_str);
  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(string_cmp_str(temp_str, nfc_resources_header) ||
  21. (version != nfc_resources_file_version))
  22. break;
  23. if(!flipper_format_read_string(file, string_get_cstr(key), data)) break;
  24. parsed = true;
  25. } while(false);
  26. string_clear(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. string_t aid_name) {
  35. furi_assert(storage);
  36. bool parsed = false;
  37. string_t key;
  38. string_init(key);
  39. for(uint8_t i = 0; i < aid_len; i++) {
  40. string_cat_printf(key, "%02X", aid[i]);
  41. }
  42. if(nfc_emv_parser_search_data(storage, "/ext/nfc/assets/aid.nfc", key, aid_name)) {
  43. parsed = true;
  44. }
  45. string_clear(key);
  46. return parsed;
  47. }
  48. bool nfc_emv_parser_get_country_name(
  49. Storage* storage,
  50. uint16_t country_code,
  51. string_t country_name) {
  52. bool parsed = false;
  53. string_t key;
  54. string_init_printf(key, "%04X", country_code);
  55. if(nfc_emv_parser_search_data(storage, "/ext/nfc/assets/country_code.nfc", key, country_name)) {
  56. parsed = true;
  57. }
  58. string_clear(key);
  59. return parsed;
  60. }
  61. bool nfc_emv_parser_get_currency_name(
  62. Storage* storage,
  63. uint16_t currency_code,
  64. string_t currency_name) {
  65. bool parsed = false;
  66. string_t key;
  67. string_init_printf(key, "%04X", currency_code);
  68. if(nfc_emv_parser_search_data(
  69. storage, "/ext/nfc/assets/currency_code.nfc", key, currency_name)) {
  70. parsed = true;
  71. }
  72. string_clear(key);
  73. return parsed;
  74. }