1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Proxmark3 RDV40 AID list library
17 //-----------------------------------------------------------------------------
18 #include "aidsearch.h"
21 #include "fileutils.h"
24 static int openAIDFile(json_t
**root
, bool verbose
) {
28 int res
= searchFile(&path
, RESOURCES_SUBDIR
, "aidlist", ".json", false);
29 if (res
!= PM3_SUCCESS
) {
33 int retval
= PM3_SUCCESS
;
34 *root
= json_load_file(path
, 0, &error
);
36 PrintAndLogEx(ERR
, "json (%s) error on line %d: %s", path
, error
.line
, error
.text
);
41 if (!json_is_array(*root
)) {
42 PrintAndLogEx(ERR
, "Invalid json (%s) format. root must be an array.", path
);
47 PrintAndLogEx(DEBUG
, "Loaded file " _YELLOW_("%s") " " _GREEN_("%zu") " records ( " _GREEN_("ok") " )"
49 , json_array_size(*root
)
56 static int closeAIDFile(json_t
*root
) {
61 json_t
*AIDSearchInit(bool verbose
) {
63 int res
= openAIDFile(&root
, verbose
);
64 if (res
!= PM3_SUCCESS
)
70 json_t
*AIDSearchGetElm(json_t
*root
, size_t elmindx
) {
71 json_t
*data
= json_array_get(root
, elmindx
);
72 if (!json_is_object(data
)) {
73 PrintAndLogEx(ERR
, "data [%zu] is not an object\n", elmindx
);
79 int AIDSearchFree(json_t
*root
) {
80 return closeAIDFile(root
);
83 static const char *jsonStrGet(json_t
*data
, const char *name
) {
86 jstr
= json_object_get(data
, name
);
89 if (!json_is_string(jstr
)) {
90 PrintAndLogEx(ERR
, "`%s` is not a string", name
);
94 const char *cstr
= json_string_value(jstr
);
95 if (strlen(cstr
) == 0)
100 static bool aidCompare(const char *aidlarge
, const char *aidsmall
) {
101 if (strcmp(aidlarge
, aidsmall
) == 0)
104 if (strlen(aidlarge
) > strlen(aidsmall
))
105 if (strncmp(aidlarge
, aidsmall
, strlen(aidsmall
)) == 0)
111 bool AIDGetFromElm(json_t
*data
, uint8_t *aid
, size_t aidmaxlen
, int *aidlen
) {
113 const char *hexaid
= jsonStrGet(data
, "AID");
114 if (hexaid
== NULL
|| strlen(hexaid
) == 0)
117 int res
= param_gethex_to_eol(hexaid
, 0, aid
, (int)aidmaxlen
, aidlen
);
124 int PrintAIDDescription(json_t
*xroot
, char *aid
, bool verbose
) {
125 int retval
= PM3_SUCCESS
;
127 json_t
*root
= xroot
;
129 root
= AIDSearchInit(verbose
);
134 size_t maxaidlen
= 0;
135 for (size_t elmindx
= 0; elmindx
< json_array_size(root
); elmindx
++) {
136 json_t
*data
= AIDSearchGetElm(root
, elmindx
);
139 const char *dictaid
= jsonStrGet(data
, "AID");
140 if (aidCompare(aid
, dictaid
)) { // dictaid may be less length than requested aid
141 if (maxaidlen
< strlen(dictaid
) && strlen(dictaid
) <= strlen(aid
)) {
142 maxaidlen
= strlen(dictaid
);
152 const char *vaid
= jsonStrGet(elm
, "AID");
153 const char *vendor
= jsonStrGet(elm
, "Vendor");
154 const char *name
= jsonStrGet(elm
, "Name");
155 const char *country
= jsonStrGet(elm
, "Country");
156 const char *description
= jsonStrGet(elm
, "Description");
157 const char *type
= jsonStrGet(elm
, "Type");
159 if (verbose
== false) {
160 PrintAndLogEx(SUCCESS
, "AID : " _YELLOW_("%s") " | %s | %s", vaid
, vendor
, name
);
162 PrintAndLogEx(SUCCESS
, "Input AID..... " _YELLOW_("%s"), aid
);
164 PrintAndLogEx(SUCCESS
, "Found AID..... " _YELLOW_("%s"), vaid
);
166 PrintAndLogEx(SUCCESS
, "Vendor........ " _YELLOW_("%s"), vendor
);
168 PrintAndLogEx(SUCCESS
, "Type.......... " _YELLOW_("%s"), type
);
170 PrintAndLogEx(SUCCESS
, "Name.......... " _YELLOW_("%s"), name
);
172 PrintAndLogEx(SUCCESS
, "Country....... %s", country
);
174 PrintAndLogEx(SUCCESS
, "Description... %s", description
);
183 int PrintAIDDescriptionBuf(json_t
*root
, uint8_t *aid
, size_t aidlen
, bool verbose
) {
184 return PrintAIDDescription(root
, sprint_hex_inrow(aid
, aidlen
), verbose
);