-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFCTag.cpp
executable file
·116 lines (105 loc) · 3.09 KB
/
NFCTag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* This file is part of the Capibara zero project(https://capibarazero.github.io/).
* Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "NFCTag.hpp"
#define GET_TAG_SIZE(uid_length) uid_length > 4 ? MIFARE_ULTRALIGHT_SIZE : MIFARE_CLASSIC_SIZE
NFCTag::NFCTag(uint8_t *new_data, size_t uid_length)
{
data = (uint8_t *)malloc(GET_TAG_SIZE(uid_length));
memcpy(data, new_data, GET_TAG_SIZE(uid_length));
uid = (uint8_t *)malloc(uid_length * sizeof(uint8_t));
memcpy(uid, data, uid_length);
if (uid_length > 4)
ultralight = true;
}
NFCTag::NFCTag(uint8_t *new_data, size_t uid_length, size_t pages)
{
data = (uint8_t *)malloc(GET_TAG_SIZE(uid_length));
memcpy(data, new_data, GET_TAG_SIZE(uid_length));
uid = (uint8_t *)malloc(uid_length * sizeof(uint8_t));
memcpy(uid, data, uid_length);
ntag = true;
pages_num = pages;
}
NFCTag::NFCTag(uint8_t *idm, uint8_t *_pmm, uint16_t _sys_code)
{
uid = (uint8_t *)malloc(8 * sizeof(uint8_t));
memcpy(uid, idm, 8);
memcpy(pmm, _pmm, 8);
sys_code = _sys_code;
felica = true;
}
NFCTag::NFCTag(uint8_t *idm, uint8_t *_pmm, uint16_t _sys_code, uint8_t data[14][16])
{
uid = (uint8_t *)malloc(8 * sizeof(uint8_t));
pmm = (uint8_t *)malloc(8 * sizeof(uint8_t));
memcpy(uid, idm, 8);
memcpy(pmm, _pmm, 8);
sys_code = _sys_code;
memcpy(felica_data, data, 14*16);
// felica_blocks.insert(blocks->begin(), blocks->end());
felica = true;
}
void NFCTag::get_block(int index, uint8_t *block)
{
memcpy(block, &data[index * get_block_size()], sizeof(uint8_t) * get_block_size());
}
void NFCTag::get_atqa(uint8_t *atqa)
{
memcpy(atqa, ultralight ? &data[9] : &data[7], sizeof(uint8_t) * 2);
}
uint8_t *NFCTag::get_data() {
if(felica) {
/* Flat FeliCa matrix data */
uint8_t *flat_data = (uint8_t *)malloc(14*16);
for(int i = 0; i < 14; i++) {
memcpy(&flat_data[i], &felica_data[i][0], 16);
}
return flat_data;
}
return data;
};
FelicaSystemCodes NFCTag::get_sys_code()
{
if (!felica)
return INVALID;
switch (sys_code)
{
case NDEF:
return NDEF;
break;
case NFC_F:
return NFC_F;
break;
case LITE_S:
return LITE_S;
break;
case SECURE_ID:
return SECURE_ID;
break;
case COMMON_AREA:
return COMMON_AREA;
break;
case PLUG:
return PLUG;
break;
default:
return INVALID;
break;
}
}