extract childs from tlv works

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

View file

@ -1183,12 +1183,24 @@ 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_is_array(elm)) {
if (json_array_append_new(elm, obj)) {
PrintAndLog("ERROR: can't append array: %s", path);
return 2;
}
} else {
if (json_path_set(elm, path, obj, 0, &error)) { if (json_path_set(elm, path, obj, 0, &error)) {
PrintAndLog("ERROR: can't set json path: ", error.text); PrintAndLog("ERROR: can't set json path: ", error.text);
return 2; return 2;
} else { }
}
}
char * name = emv_get_tag_name(tlvelm); char * name = emv_get_tag_name(tlvelm);
if (name && strlen(name) > 0 && strncmp(name, "Unknown", 7)) if (name && strlen(name) > 0 && strncmp(name, "Unknown", 7))
JsonSaveStr(obj, "name", emv_get_tag_name(tlvelm)); JsonSaveStr(obj, "name", emv_get_tag_name(tlvelm));
@ -1197,7 +1209,6 @@ int JsonSaveTLVElm(json_t *elm, char *path, struct tlv *tlvelm, bool saveValue)
if (saveValue) if (saveValue)
JsonSaveBufAsHex(obj, "value", (uint8_t *)tlvelm->value, tlvelm->len); 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) {
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); JsonSaveTLVTreeElm(elm, path, tlvp, true);
if (tlvdb_elm_get_children(tlvp)) { pelm = json_path_get(elm, path);
}
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 {
@ -1510,11 +1551,6 @@ int CmdHFEMVScan(const char *cmd) {
break; break;
} }
// DropField // DropField
DropField(); DropField();