nfc_util.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "nfc_util.h"
  2. #include <furi.h>
  3. static const uint8_t nfc_util_odd_byte_parity[256] = {
  4. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,
  5. 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1,
  6. 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
  7. 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0,
  8. 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1,
  9. 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0,
  10. 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1,
  11. 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  12. 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1};
  13. void nfc_util_num2bytes(uint64_t src, uint8_t len, uint8_t* dest) {
  14. furi_assert(dest);
  15. furi_assert(len <= 8);
  16. while(len--) {
  17. dest[len] = (uint8_t)src;
  18. src >>= 8;
  19. }
  20. }
  21. uint64_t nfc_util_bytes2num(const uint8_t* src, uint8_t len) {
  22. furi_assert(src);
  23. furi_assert(len <= 8);
  24. uint64_t res = 0;
  25. while(len--) {
  26. res = (res << 8) | (*src);
  27. src++;
  28. }
  29. return res;
  30. }
  31. uint8_t nfc_util_even_parity32(uint32_t data) {
  32. // data ^= data >> 16;
  33. // data ^= data >> 8;
  34. // return !nfc_util_odd_byte_parity[data];
  35. return (__builtin_parity(data) & 0xFF);
  36. }
  37. uint8_t nfc_util_odd_parity8(uint8_t data) {
  38. return nfc_util_odd_byte_parity[data];
  39. }
  40. void nfc_util_odd_parity(const uint8_t* src, uint8_t* dst, uint8_t len) {
  41. furi_assert(src);
  42. furi_assert(dst);
  43. uint8_t parity = 0;
  44. uint8_t bit = 0;
  45. while(len--) {
  46. parity |= nfc_util_odd_parity8(*src) << (7 - bit); // parity is MSB first
  47. bit++;
  48. if(bit == 8) {
  49. *dst = parity;
  50. dst++;
  51. parity = 0;
  52. bit = 0;
  53. }
  54. src++;
  55. }
  56. if(bit) {
  57. *dst = parity;
  58. }
  59. }