1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2019 merlokk
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Proxmark3 RDV40 AID list library
9 //-----------------------------------------------------------------------------
10 #include "aidsearch.h"
13 #include "fileutils.h"
16 static int openAIDFile(json_t
**root
, bool verbose
) {
20 int res
= searchFile(&path
, RESOURCES_SUBDIR
, "aidlist", ".json", false);
21 if (res
!= PM3_SUCCESS
) {
25 int retval
= PM3_SUCCESS
;
26 *root
= json_load_file(path
, 0, &error
);
28 PrintAndLogEx(ERR
, "json (%s) error on line %d: %s", path
, error
.line
, error
.text
);
33 if (!json_is_array(*root
)) {
34 PrintAndLogEx(ERR
, "Invalid json (%s) format. root must be an array.", path
);
39 PrintAndLogEx(DEBUG
, "Loaded file " _YELLOW_("%s") " " _GREEN_("%zu") " records ( " _GREEN_("ok") " )", path
, json_array_size(*root
));
45 static int closeAIDFile(json_t
*root
) {
50 json_t
*AIDSearchInit(bool verbose
) {
52 int res
= openAIDFile(&root
, verbose
);
53 if (res
!= PM3_SUCCESS
)
59 json_t
*AIDSearchGetElm(json_t
*root
, size_t elmindx
) {
60 json_t
*data
= json_array_get(root
, elmindx
);
61 if (!json_is_object(data
)) {
62 PrintAndLogEx(ERR
, "data [%zu] is not an object\n", elmindx
);
68 int AIDSearchFree(json_t
*root
) {
69 return closeAIDFile(root
);
72 static const char *jsonStrGet(json_t
*data
, const char *name
) {
75 jstr
= json_object_get(data
, name
);
78 if (!json_is_string(jstr
)) {
79 PrintAndLogEx(ERR
, "`%s` is not a string", name
);
83 const char *cstr
= json_string_value(jstr
);
84 if (strlen(cstr
) == 0)
89 static bool aidCompare(const char *aidlarge
, const char *aidsmall
) {
90 if (strcmp(aidlarge
, aidsmall
) == 0)
93 if (strlen(aidlarge
) > strlen(aidsmall
))
94 if (strncmp(aidlarge
, aidsmall
, strlen(aidsmall
)) == 0)
100 bool AIDGetFromElm(json_t
*data
, uint8_t *aid
, size_t aidmaxlen
, int *aidlen
) {
102 const char *hexaid
= jsonStrGet(data
, "AID");
103 if (hexaid
== NULL
|| strlen(hexaid
) == 0)
106 int res
= param_gethex_to_eol(hexaid
, 0, aid
, (int)aidmaxlen
, aidlen
);
113 int PrintAIDDescription(json_t
*xroot
, char *aid
, bool verbose
) {
114 int retval
= PM3_SUCCESS
;
116 json_t
*root
= xroot
;
118 root
= AIDSearchInit(verbose
);
123 size_t maxaidlen
= 0;
124 for (size_t elmindx
= 0; elmindx
< json_array_size(root
); elmindx
++) {
125 json_t
*data
= AIDSearchGetElm(root
, elmindx
);
128 const char *dictaid
= jsonStrGet(data
, "AID");
129 if (aidCompare(aid
, dictaid
)) { // dictaid may be less length than requested aid
130 if (maxaidlen
< strlen(dictaid
) && strlen(dictaid
) <= strlen(aid
)) {
131 maxaidlen
= strlen(dictaid
);
141 const char *vaid
= jsonStrGet(elm
, "AID");
142 const char *vendor
= jsonStrGet(elm
, "Vendor");
143 const char *name
= jsonStrGet(elm
, "Name");
144 const char *country
= jsonStrGet(elm
, "Country");
145 const char *description
= jsonStrGet(elm
, "Description");
146 const char *type
= jsonStrGet(elm
, "Type");
148 if (verbose
== false) {
149 PrintAndLogEx(SUCCESS
, "AID : " _YELLOW_("%s") " | %s | %s", vaid
, vendor
, name
);
151 PrintAndLogEx(SUCCESS
, "Input AID..... " _YELLOW_("%s"), aid
);
153 PrintAndLogEx(SUCCESS
, "Found AID..... " _YELLOW_("%s"), vaid
);
155 PrintAndLogEx(SUCCESS
, "Vendor........ " _YELLOW_("%s"), vendor
);
157 PrintAndLogEx(SUCCESS
, "Type.......... " _YELLOW_("%s"), type
);
159 PrintAndLogEx(SUCCESS
, "Name.......... " _YELLOW_("%s"), name
);
161 PrintAndLogEx(SUCCESS
, "Country....... %s", country
);
163 PrintAndLogEx(SUCCESS
, "Description... %s", description
);
172 int PrintAIDDescriptionBuf(json_t
*root
, uint8_t *aid
, size_t aidlen
, bool verbose
) {
173 return PrintAIDDescription(root
, sprint_hex_inrow(aid
, aidlen
), verbose
);