fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / aidsearch.c
blob377bc2f9ef786e87783d235e360999f4c0734b9f
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2019 merlokk
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Proxmark3 RDV40 AID list library
9 //-----------------------------------------------------------------------------
10 #include "aidsearch.h"
11 #include <ctype.h>
12 #include <string.h>
13 #include "fileutils.h"
14 #include "pm3_cmd.h"
16 static int openAIDFile(json_t **root, bool verbose) {
17 json_error_t error;
19 char *path;
20 int res = searchFile(&path, RESOURCES_SUBDIR, "aidlist", ".json", false);
21 if (res != PM3_SUCCESS) {
22 return PM3_EFILE;
25 int retval = PM3_SUCCESS;
26 *root = json_load_file(path, 0, &error);
27 if (!*root) {
28 PrintAndLogEx(ERR, "json (%s) error on line %d: %s", path, error.line, error.text);
29 retval = PM3_ESOFT;
30 goto out;
33 if (!json_is_array(*root)) {
34 PrintAndLogEx(ERR, "Invalid json (%s) format. root must be an array.", path);
35 retval = PM3_ESOFT;
36 goto out;
39 PrintAndLogEx(DEBUG, "Loaded file " _YELLOW_("%s") " " _GREEN_("%zu") " records ( " _GREEN_("ok") " )", path, json_array_size(*root));
40 out:
41 free(path);
42 return retval;
45 static int closeAIDFile(json_t *root) {
46 json_decref(root);
47 return PM3_SUCCESS;
50 json_t *AIDSearchInit(bool verbose) {
51 json_t *root = NULL;
52 int res = openAIDFile(&root, verbose);
53 if (res != PM3_SUCCESS)
54 return NULL;
56 return root;
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);
63 return NULL;
65 return data;
68 int AIDSearchFree(json_t *root) {
69 return closeAIDFile(root);
72 static const char *jsonStrGet(json_t *data, const char *name) {
73 json_t *jstr;
75 jstr = json_object_get(data, name);
76 if (jstr == NULL)
77 return NULL;
78 if (!json_is_string(jstr)) {
79 PrintAndLogEx(ERR, "`%s` is not a string", name);
80 return NULL;
83 const char *cstr = json_string_value(jstr);
84 if (strlen(cstr) == 0)
85 return NULL;
86 return cstr;
89 static bool aidCompare(const char *aidlarge, const char *aidsmall) {
90 if (strcmp(aidlarge, aidsmall) == 0)
91 return true;
93 if (strlen(aidlarge) > strlen(aidsmall))
94 if (strncmp(aidlarge, aidsmall, strlen(aidsmall)) == 0)
95 return true;
97 return false;
100 bool AIDGetFromElm(json_t *data, uint8_t *aid, size_t aidmaxlen, int *aidlen) {
101 *aidlen = 0;
102 const char *hexaid = jsonStrGet(data, "AID");
103 if (hexaid == NULL || strlen(hexaid) == 0)
104 return false;
106 int res = param_gethex_to_eol(hexaid, 0, aid, (int)aidmaxlen, aidlen);
107 if (res)
108 return false;
110 return true;
113 int PrintAIDDescription(json_t *xroot, char *aid, bool verbose) {
114 int retval = PM3_SUCCESS;
116 json_t *root = xroot;
117 if (root == NULL)
118 root = AIDSearchInit(verbose);
119 if (root == NULL)
120 goto out;
122 json_t *elm = NULL;
123 size_t maxaidlen = 0;
124 for (size_t elmindx = 0; elmindx < json_array_size(root); elmindx++) {
125 json_t *data = AIDSearchGetElm(root, elmindx);
126 if (data == NULL)
127 continue;
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);
132 elm = data;
137 if (elm == NULL)
138 goto out;
140 // print here
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);
150 } else {
151 PrintAndLogEx(SUCCESS, "Input AID..... " _YELLOW_("%s"), aid);
152 if (aid)
153 PrintAndLogEx(SUCCESS, "Found AID..... " _YELLOW_("%s"), vaid);
154 if (vendor)
155 PrintAndLogEx(SUCCESS, "Vendor........ " _YELLOW_("%s"), vendor);
156 if (type)
157 PrintAndLogEx(SUCCESS, "Type.......... " _YELLOW_("%s"), type);
158 if (name)
159 PrintAndLogEx(SUCCESS, "Name.......... " _YELLOW_("%s"), name);
160 if (country)
161 PrintAndLogEx(SUCCESS, "Country....... %s", country);
162 if (description)
163 PrintAndLogEx(SUCCESS, "Description... %s", description);
166 out:
167 if (xroot == NULL)
168 AIDSearchFree(root);
169 return retval;
172 int PrintAIDDescriptionBuf(json_t *root, uint8_t *aid, size_t aidlen, bool verbose) {
173 return PrintAIDDescription(root, sprint_hex_inrow(aid, aidlen), verbose);