extract childs from tlv works

This commit is contained in:
merlokk 2018-10-05 20:04:18 +03:00
commit 7edb677c99

View file

@ -1183,20 +1183,31 @@ int JsonSaveTLVElm(json_t *elm, char *path, struct tlv *tlvelm, bool saveValue)
return 1; return 1;
if (path[0] == '$') { if (path[0] == '$') {
json_t *obj = json_object();
json_t *obj = json_path_get(elm, path);
if (!obj) {
obj = json_object();
if (json_path_set(elm, path, obj, 0, &error)) { if (json_is_array(elm)) {
PrintAndLog("ERROR: can't set json path: ", error.text); if (json_array_append_new(elm, obj)) {
return 2; PrintAndLog("ERROR: can't append array: %s", path);
} else { return 2;
char * name = emv_get_tag_name(tlvelm); }
if (name && strlen(name) > 0 && strncmp(name, "Unknown", 7)) } else {
JsonSaveStr(obj, "name", emv_get_tag_name(tlvelm)); if (json_path_set(elm, path, obj, 0, &error)) {
JsonSaveHex(obj, "tag", tlvelm->tag, 0); PrintAndLog("ERROR: can't set json path: ", error.text);
JsonSaveHex(obj, "length", tlvelm->len, 0); return 2;
if (saveValue) }
JsonSaveBufAsHex(obj, "value", (uint8_t *)tlvelm->value, tlvelm->len); }
} }
char * name = emv_get_tag_name(tlvelm);
if (name && strlen(name) > 0 && strncmp(name, "Unknown", 7))
JsonSaveStr(obj, "name", emv_get_tag_name(tlvelm));
JsonSaveHex(obj, "tag", tlvelm->tag, 0);
JsonSaveHex(obj, "length", tlvelm->len, 0);
if (saveValue)
JsonSaveBufAsHex(obj, "value", (uint8_t *)tlvelm->value, tlvelm->len);
} }
return 0; return 0;
@ -1205,12 +1216,42 @@ int JsonSaveTLVElm(json_t *elm, char *path, struct tlv *tlvelm, bool saveValue)
int JsonSaveTLVTreeElm(json_t *elm, char *path, struct tlvdb *tlvdbelm, bool saveValue) { int JsonSaveTLVTreeElm(json_t *elm, char *path, struct tlvdb *tlvdbelm, bool saveValue) {
return JsonSaveTLVElm(elm, path, (struct tlv *)tlvdb_get_tlv(tlvdbelm), saveValue); return JsonSaveTLVElm(elm, path, (struct tlv *)tlvdb_get_tlv(tlvdbelm), saveValue);
} }
int JsonSaveTLVTree(json_t *elm, char *path, struct tlvdb *tlvdbelm) { int JsonSaveTLVTree(json_t *elm, char *path, struct tlvdb *tlvdbelm) {
struct tlvdb *tlvp = tlvdbelm; struct tlvdb *tlvp = tlvdbelm;
while (tlvp) { while (tlvp) {
JsonSaveTLVTreeElm(elm, path, tlvp, true); json_t *pelm = json_path_get(elm, path);
if (pelm && json_is_array(pelm)) {
json_t *appendelm = json_object();
json_array_append_new(pelm, appendelm);
JsonSaveTLVTreeElm(appendelm, "$", tlvp, true);
pelm = appendelm;
} else {
JsonSaveTLVTreeElm(elm, path, tlvp, true);
pelm = json_path_get(elm, path);
}
if (tlvdb_elm_get_children(tlvp)) { if (tlvdb_elm_get_children(tlvp)) {
// get path element
if(!pelm)
return 1;
// check childs element and add it if not found
json_t *chjson = json_path_get(pelm, "$.Childs");
if (!chjson) {
json_object_set_new(pelm, "Childs", json_array());
chjson = json_path_get(pelm, "$.Childs");
}
// check
if (!json_is_array(chjson)) {
PrintAndLog("E->Internal logic error. `$.Childs` is not an array.");
break;
}
// Recursion
JsonSaveTLVTree(chjson, "$", tlvdb_elm_get_children(tlvp));
} }
tlvp = tlvdb_elm_get_next(tlvp); tlvp = tlvdb_elm_get_next(tlvp);
@ -1282,7 +1323,7 @@ int CmdHFEMVScan(const char *cmd) {
} }
if (!json_is_object(root)) { if (!json_is_object(root)) {
PrintAndLog("ERROR: Invalid json format. root must be object."); PrintAndLog("ERROR: Invalid json format. root must be an object.");
return 1; return 1;
} }
} else { } else {
@ -1509,11 +1550,6 @@ int CmdHFEMVScan(const char *cmd) {
break; break;
} }
// DropField // DropField
DropField(); DropField();