diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2024-01-21 22:36:47 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2024-01-21 22:36:47 (GMT) |
commit | 0ff1e52622828663d01f98c97f2cd8eccb8facf8 (patch) | |
tree | 88b5fcf2412f863276876d0b8ad8db91903f3758 /src/analysis/scan/matches/bytes.c | |
parent | 0fac40d5a5752e8d7b92f57ea3cfa089f13a2d1f (diff) |
Refactor the scan match storage.
Diffstat (limited to 'src/analysis/scan/matches/bytes.c')
-rw-r--r-- | src/analysis/scan/matches/bytes.c | 595 |
1 files changed, 370 insertions, 225 deletions
diff --git a/src/analysis/scan/matches/bytes.c b/src/analysis/scan/matches/bytes.c index f0b97fe..ca8c5b6 100644 --- a/src/analysis/scan/matches/bytes.c +++ b/src/analysis/scan/matches/bytes.c @@ -36,20 +36,20 @@ -/* --------------------- CORRESPONDANCE AVEC UNE SUITE D'OCTETS --------------------- */ +/* -------------------- CONSERVATION DE CORRESPONDANCES ETABLIES -------------------- */ -/* Initialise la classe des correspondances de chaînes. */ -static void g_scan_bytes_match_class_init(GScanBytesMatchClass *); +/* Initialise la classe des séries de correspondances d'octets. */ +static void g_scan_bytes_matches_class_init(GScanBytesMatchesClass *); -/* Initialise une instance de correspondance de chaîne trouvée. */ -static void g_scan_bytes_match_init(GScanBytesMatch *); +/* Initialise une instance de série de correspondances trouvées. */ +static void g_scan_bytes_matches_init(GScanBytesMatches *); /* Supprime toutes les références externes. */ -static void g_scan_bytes_match_dispose(GScanBytesMatch *); +static void g_scan_bytes_matches_dispose(GScanBytesMatches *); /* Procède à la libération totale de la mémoire. */ -static void g_scan_bytes_match_finalize(GScanBytesMatch *); +static void g_scan_bytes_matches_finalize(GScanBytesMatches *); @@ -57,27 +57,27 @@ static void g_scan_bytes_match_finalize(GScanBytesMatch *); /* Affiche une correspondance au format texte. */ -static void g_scan_bytes_match_output_to_text(const GScanBytesMatch *, int); +static void g_scan_bytes_matches_output_to_text(const GScanBytesMatches *, int); /* Affiche une correspondance au format JSON. */ -static void g_scan_bytes_match_output_to_json(const GScanBytesMatch *, const sized_string_t *, unsigned int, int); +static void g_scan_bytes_matches_output_to_json(const GScanBytesMatches *, const sized_string_t *, unsigned int, int); /* ---------------------------------------------------------------------------------- */ -/* CORRESPONDANCE AVEC UNE SUITE D'OCTETS */ +/* CONSERVATION DE CORRESPONDANCES ETABLIES */ /* ---------------------------------------------------------------------------------- */ -/* Indique le type défini pour un correspondance de chaîne identifiée. */ -G_DEFINE_TYPE(GScanBytesMatch, g_scan_bytes_match, G_TYPE_SCAN_MATCH); +/* Indique le type défini pour une série de correspondances d'octets identifiées. */ +G_DEFINE_TYPE(GScanBytesMatches, g_scan_bytes_matches, G_TYPE_SCAN_MATCHES); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * -* Description : Initialise la classe des correspondances de chaînes. * +* Description : Initialise la classe des séries de correspondances d'octets. * * * * Retour : - * * * @@ -85,29 +85,29 @@ G_DEFINE_TYPE(GScanBytesMatch, g_scan_bytes_match, G_TYPE_SCAN_MATCH); * * ******************************************************************************/ -static void g_scan_bytes_match_class_init(GScanBytesMatchClass *klass) +static void g_scan_bytes_matches_class_init(GScanBytesMatchesClass *klass) { GObjectClass *object; /* Autre version de la classe */ - GScanMatchClass *match; /* Version parente de la classe*/ + GScanMatchesClass *matches; /* Version parente de la classe*/ object = G_OBJECT_CLASS(klass); - object->dispose = (GObjectFinalizeFunc/* ! */)g_scan_bytes_match_dispose; - object->finalize = (GObjectFinalizeFunc)g_scan_bytes_match_finalize; + object->dispose = (GObjectFinalizeFunc/* ! */)g_scan_bytes_matches_dispose; + object->finalize = (GObjectFinalizeFunc)g_scan_bytes_matches_finalize; - match = G_SCAN_MATCH_CLASS(klass); + matches = G_SCAN_MATCHES_CLASS(klass); - match->to_text = (output_scan_match_to_text_fc)g_scan_bytes_match_output_to_text; - match->to_json = (output_scan_match_to_json_fc)g_scan_bytes_match_output_to_json; + matches->to_text = (output_scan_matches_to_text_fc)g_scan_bytes_matches_output_to_text; + matches->to_json = (output_scan_matches_to_json_fc)g_scan_bytes_matches_output_to_json; } /****************************************************************************** * * -* Paramètres : match = instance à initialiser. * +* Paramètres : matches = instance à initialiser. * * * -* Description : Initialise une instance de correspondance de chaîne trouvée. * +* Description : Initialise une instance de série de correspondances trouvées.* * * * Retour : - * * * @@ -115,21 +115,22 @@ static void g_scan_bytes_match_class_init(GScanBytesMatchClass *klass) * * ******************************************************************************/ -static void g_scan_bytes_match_init(GScanBytesMatch *match) +static void g_scan_bytes_matches_init(GScanBytesMatches *matches) { - match->content = NULL; + matches->context = NULL; - match->start = VMPA_NO_PHYSICAL; - match->len = VMPA_NO_PHYSICAL; + matches->content_start = VMPA_NO_PHYSICAL; + matches->content_end = VMPA_NO_PHYSICAL; - match->has_mod_path = false; + matches->areas = NULL; + matches->count = 0; } /****************************************************************************** * * -* Paramètres : match = instance d'objet GLib à traiter. * +* Paramètres : matches = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * @@ -139,18 +140,18 @@ static void g_scan_bytes_match_init(GScanBytesMatch *match) * * ******************************************************************************/ -static void g_scan_bytes_match_dispose(GScanBytesMatch *match) +static void g_scan_bytes_matches_dispose(GScanBytesMatches *matches) { - g_clear_object(&match->content); + //g_clear_object(&matches->context); - G_OBJECT_CLASS(g_scan_bytes_match_parent_class)->dispose(G_OBJECT(match)); + G_OBJECT_CLASS(g_scan_bytes_matches_parent_class)->dispose(G_OBJECT(matches)); } /****************************************************************************** * * -* Paramètres : match = instance d'objet GLib à traiter. * +* Paramètres : matches = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * @@ -160,9 +161,9 @@ static void g_scan_bytes_match_dispose(GScanBytesMatch *match) * * ******************************************************************************/ -static void g_scan_bytes_match_finalize(GScanBytesMatch *match) +static void g_scan_bytes_matches_finalize(GScanBytesMatches *matches) { - G_OBJECT_CLASS(g_scan_bytes_match_parent_class)->finalize(G_OBJECT(match)); + G_OBJECT_CLASS(g_scan_bytes_matches_parent_class)->finalize(G_OBJECT(matches)); } @@ -170,11 +171,9 @@ static void g_scan_bytes_match_finalize(GScanBytesMatch *match) /****************************************************************************** * * * Paramètres : source = lien vers le motif recherché d'origine. * -* content = contenu binaire présentant un motif reconnu. * -* start = position de départ d'un motif détecté. * -* len = taille du motif repéré. * +* context = contexte associé au scan courant. * * * -* Description : Prend note d'une correspondance trouvée avec un motif. * +* Description : Crée un suivi pour série de correspondances avec des octets. * * * * Retour : Correspondance mise en place. * * * @@ -182,13 +181,13 @@ static void g_scan_bytes_match_finalize(GScanBytesMatch *match) * * ******************************************************************************/ -GScanMatch *g_scan_bytes_match_new(GSearchPattern *source, GBinContent *content, phys_t start, phys_t len) +GScanMatches *g_scan_bytes_matches_new(GSearchPattern *source, GScanContext *context) { - GScanMatch *result; /* Structure à retourner */ + GScanMatches *result; /* Structure à retourner */ - result = g_object_new(G_TYPE_SCAN_BYTES_MATCH, NULL); + result = g_object_new(G_TYPE_SCAN_BYTES_MATCHES, NULL); - if (!g_scan_bytes_match_create(G_SCAN_BYTES_MATCH(result), source, content, start, len)) + if (!g_scan_bytes_matches_create(G_SCAN_BYTES_MATCHES(result), source, context)) g_clear_object(&result); return result; @@ -198,13 +197,11 @@ GScanMatch *g_scan_bytes_match_new(GSearchPattern *source, GBinContent *content, /****************************************************************************** * * -* Paramètres : match = instance à initialiser pleinement. * +* Paramètres : matches = instance à initialiser pleinement. * * source = lien vers le motif recherché d'origine. * -* content = contenu binaire présentant un motif reconnu. * -* start = position de départ d'un motif détecté. * -* len = taille du motif repéré. * +* context = contexte associé au scan courant. * * * -* Description : Met en place une correspondance trouvée avec un motif. * +* Description : Met en place une série de correspondances avec des octets. * * * * Retour : Bilan de l'opération. * * * @@ -212,23 +209,33 @@ GScanMatch *g_scan_bytes_match_new(GSearchPattern *source, GBinContent *content, * * ******************************************************************************/ -bool g_scan_bytes_match_create(GScanBytesMatch *match, GSearchPattern *source, GBinContent *content, phys_t start, phys_t len) +bool g_scan_bytes_matches_create(GScanBytesMatches *matches, GSearchPattern *source, GScanContext *context) { bool result; /* Bilan à retourner */ - GScanMatch *base; /* Lien vers les infos de base */ + GScanMatches *base; /* Lien vers les infos de base */ + GBinContent *content; /* Contenu à manipuler */ + vmpa2t start; /* Point de début du contenu */ + vmpa2t end; /* Point de fin du contenu */ result = true; - base = G_SCAN_MATCH(match); + base = G_SCAN_MATCHES(matches); base->source = source; g_object_ref(G_OBJECT(source)); - match->content = content; - g_object_ref(G_OBJECT(content)); + matches->context = context; + //g_object_ref(G_OBJECT(context)); - match->start = start; - match->len = len; + content = g_scan_context_get_content(context); + + g_binary_content_compute_start_pos(content, &start); + g_binary_content_compute_end_pos(content, &end); + + matches->content_start = start.physical; + matches->content_end = end.physical; + + g_object_unref(G_OBJECT(content)); return result; @@ -237,21 +244,21 @@ bool g_scan_bytes_match_create(GScanBytesMatch *match, GSearchPattern *source, G /****************************************************************************** * * -* Paramètres : match = informations de correspondance à consulter. * +* Paramètres : matches = informations de correspondances à consulter. * * * -* Description : Fournit une référence au contenu lié à la correspondance. * +* Description : Fournit le contexte du scan associé aux correspondances. * * * -* Retour : Content binaire associé au context. * +* Retour : Contexte de scan courant. * * * * Remarques : - * * * ******************************************************************************/ -GBinContent *g_scan_bytes_match_get_content(const GScanBytesMatch *match) +GScanContext *g_scan_bytes_matches_get_context(const GScanBytesMatches *matches) { - GBinContent *result; /* Instance à retourner */ + GScanContext *result; /* Instance à retourner */ - result = match->content; + result = matches->context; g_object_ref(G_OBJECT(result)); @@ -262,26 +269,44 @@ GBinContent *g_scan_bytes_match_get_content(const GScanBytesMatch *match) /****************************************************************************** * * -* Paramètres : match = informations de correspondance à consulter. * -* start = position de départ d'un motif détecté. [OUT] * -* end = position d'arrivée d'un motif détecté. [OUT] * +* Paramètres : matches = suivi de correspondances à manipuler. * +* list = correspondances établies à mémoriser. * +* count = taille de cette liste. * * * -* Description : Indique la localisation d'une correspondance établie. * +* Description : Intègre une liste de correspondances vérifiées. * * * -* Retour : Taille mesurée de la correspondance. * +* Retour : - * * * * Remarques : - * * * ******************************************************************************/ -phys_t g_scan_bytes_match_get_location(const GScanBytesMatch *match, phys_t *start, phys_t *end) +void g_scan_bytes_matches_set_list(GScanBytesMatches *matches, match_area_t *list, size_t count) { - phys_t result; /* Taille à retourner */ + matches->areas = list; - result = match->len; + matches->count = count; - *start = match->start; - *end = match->start + result; +} + + +/****************************************************************************** +* * +* Paramètres : matches = suivi de correspondances à consulter. * +* * +* Description : Indique le nombre de correspondances pour une définition. * +* * +* Retour : Quantité de correspondances établies pour un motif entier. * +* * +* Remarques : - * +* * +******************************************************************************/ + +size_t g_scan_bytes_matches_count(const GScanBytesMatches *matches) +{ + size_t result; /* Quantité à retourner */ + + result = matches->count; return result; @@ -290,28 +315,71 @@ phys_t g_scan_bytes_match_get_location(const GScanBytesMatch *match, phys_t *sta /****************************************************************************** * * -* Paramètres : match = informations de correspondance à compléter. * -* index = indice de la combinaison de modificateurs ciblée. * +* Paramètres : matches = suivi de correspondances à consulter. * +* index = indice de la correspondance recherchée. * * * -* Description : Mémorise l'origine d'une correspondance à partir d'un indice.* +* Description : Fournit les informations relatives à une correspondance. * * * -* Retour : - * +* Retour : Propritétés de la correspondance visée ou NULL pour un échec.* +* * +* Remarques : - * +* * +******************************************************************************/ + +const match_area_t *g_scan_bytes_matches_get(const GScanBytesMatches *matches, size_t index) +{ + const match_area_t *result; /* Pointeur à retourner */ + + + for_each_match_area(result, matches->areas) + { + if (index == 0) + break; + } + + assert(index == 0); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : matches = informations de correspondance à consulter. * +* index = indice de la correspondance visée. * +* start = position de départ d'un motif détecté. [OUT] * +* end = position d'arrivée d'un motif détecté. [OUT] * +* * +* Description : Indique la localisation d'une correspondance établie. * +* * +* Retour : Taille mesurée de la correspondance. * * * * Remarques : - * * * ******************************************************************************/ -void g_scan_bytes_match_remember_modifier_path(GScanBytesMatch *match, size_t index) +phys_t g_scan_bytes_matches_get_location(const GScanBytesMatches *matches, size_t index, phys_t *start, phys_t *end) { - match->mod_path_index = index; - match->has_mod_path = true; + phys_t result; /* Taille à retourner */ + + result = 0; + + /* + result = match->len; + + *start = match->start; + *end = match->start + result; + */ + + return result; } /****************************************************************************** * * -* Paramètres : match = informations de correspondance à consulter. * +* Paramètres : matches = informations de correspondance à consulter. * * * * Description : Retrouve l'origine d'une correspondance à partir d'un indice.* * * @@ -321,11 +389,15 @@ void g_scan_bytes_match_remember_modifier_path(GScanBytesMatch *match, size_t in * * ******************************************************************************/ -char *g_scan_bytes_match_get_modifier_path(const GScanBytesMatch *match) +char *g_scan_bytes_matches_get_modifier_path(const GScanBytesMatches *matches) { char *result; /* Combinaison à retourner */ GBytesToken *pattern; /* Autre version du motif */ + result = NULL; + + /* + if (match->has_mod_path) { pattern = G_BYTES_TOKEN(G_SCAN_MATCH(match)->source); @@ -334,6 +406,7 @@ char *g_scan_bytes_match_get_modifier_path(const GScanBytesMatch *match) else result = NULL; + */ return result; @@ -348,8 +421,8 @@ char *g_scan_bytes_match_get_modifier_path(const GScanBytesMatch *match) /****************************************************************************** * * -* Paramètres : match = définition de correspondance à manipuler. * -* fd = canal d'écriture. * +* Paramètres : matches = définition de correspondance à manipuler. * +* fd = canal d'écriture. * * * * Description : Affiche une correspondance au format texte. * * * @@ -359,91 +432,105 @@ char *g_scan_bytes_match_get_modifier_path(const GScanBytesMatch *match) * * ******************************************************************************/ -static void g_scan_bytes_match_output_to_text(const GScanBytesMatch *match, int fd) +static void g_scan_bytes_matches_output_to_text(const GScanBytesMatches *matches, int fd) { + GBinContent *content; /* Contenu binaire analysé */ + GScanMatches *base; /* Lien vers les infos de base */ + const char *name; /* Désignation du motif ciblé */ + match_area_t *iter; /* Boucle de parcours #1 */ char value[2 + ULLONG_MAXLEN]; /* Impression de la position */ int ret; /* Bilan d'une conversion */ - GScanMatch *base; /* Lien vers les infos de base */ - const char *name; /* Désignation du motif ciblé */ vmpa2t pos; /* Tête de lecture */ + phys_t len; /* Taille d'une correspondance */ const bin_t *data; /* Accès aux données brutes */ phys_t k; /* Boucle de parcours #2 */ - /* Position dans le binaire (hexadécimal) */ + content = g_scan_context_get_content(matches->context); - ret = snprintf(value, ULLONG_MAXLEN, "0x%llx", (unsigned long long)match->start); + base = G_SCAN_MATCHES(matches); - if (ret > 0) - write(fd, value, ret); + name = g_search_pattern_get_name(base->source); - else + for_each_match_area(iter, matches->areas) { - log_simple_message(LMT_EXT_ERROR, "Error while converting offset to hex!"); - write(fd, "\"<error>\"", 9); - } - - write(fd, ":", 1); + /* Position dans le binaire (hexadécimal) */ - /* Affichage de la désignation */ + ret = snprintf(value, ULLONG_MAXLEN, "0x%llx", (unsigned long long)iter->start); - write(fd, "$", 1); + if (ret > 0) + write(fd, value, ret); - base = G_SCAN_MATCH(match); + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting offset to hex!"); + write(fd, "\"<error>\"", 9); + } - name = g_search_pattern_get_name(base->source); + write(fd, ":", 1); - /** - * Les fonctionnalités Yara d'origine autorisent les variables anonymes '$'. - * - * Cette absence de nom est supportée ici. - */ + /* Affichage de la désignation */ - if (name != NULL) - write(fd, name, strlen(name)); + write(fd, "$", 1); - write(fd, ": ", 2); + /** + * Les fonctionnalités Yara d'origine autorisent les variables anonymes '$'. + * + * Cette absence de nom est supportée ici. + */ - /* Affichage du contenu */ + if (name != NULL) + write(fd, name, strlen(name)); - init_vmpa(&pos, match->start, VMPA_NO_VIRTUAL); + write(fd, ": ", 2); - data = g_binary_content_get_raw_access(match->content, &pos, match->len); + /* Affichage du contenu */ - for (k = 0; k < match->len; k++) - { - if (isprint(data[k])) - write(fd, &data[k], 1); + init_vmpa(&pos, iter->start, VMPA_NO_VIRTUAL); - else - { - write(fd, "\\x", 2); + len = iter->end - iter->start; - ret = snprintf(value, ULLONG_MAXLEN, "%02hhx", data[k]); + data = g_binary_content_get_raw_access(content, &pos, len); + assert(data != NULL); - if (ret > 0) - { - assert(ret == 2); - write(fd, value, ret); - } + for (k = 0; k < len; k++) + { + if (isprint(data[k])) + write(fd, &data[k], 1); else { - log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); - write(fd, "??", 2); + write(fd, "\\x", 2); + + ret = snprintf(value, ULLONG_MAXLEN, "%02hhx", data[k]); + + if (ret > 0) + { + assert(ret == 2); + write(fd, value, ret); + } + + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); + write(fd, "??", 2); + } + } } + write(fd, "\n", 1); + } - write(fd, "\n", 1); + g_object_unref(G_OBJECT(content)); } /****************************************************************************** * * -* Paramètres : match = définition de correspondance à manipuler. * +* Paramètres : matches = définition de correspondance à manipuler. * * padding = éventuel bourrage initial à placer ou NULL. * * level = profondeur actuelle. * * fd = canal d'écriture. * @@ -456,188 +543,246 @@ static void g_scan_bytes_match_output_to_text(const GScanBytesMatch *match, int * * ******************************************************************************/ -static void g_scan_bytes_match_output_to_json(const GScanBytesMatch *match, const sized_string_t *padding, unsigned int level, int fd) +static void g_scan_bytes_matches_output_to_json(const GScanBytesMatches *matches, const sized_string_t *padding, unsigned int level, int fd) { unsigned int i; /* Boucle de parcours #1 */ - vmpa2t pos; /* Tête de lecture */ char value[4 + ULLONG_MAXLEN]; /* Impression de la position */ int ret; /* Bilan d'une conversion */ + GBinContent *content; /* Contenu binaire analysé */ + match_area_t *iter; /* Boucle de parcours #1 */ + vmpa2t pos; /* Tête de lecture */ + phys_t len; /* Taille d'une correspondance */ const bin_t *data; /* Accès aux données brutes */ phys_t k; /* Boucle de parcours #2 */ - /* Position dans le binaire (décimal) */ + /* Nombre de correspondances */ for (i = 0; i < level; i++) write(fd, padding->data, padding->len); - write(fd, "\"offset\": ", 10); + write(fd, "\"match_count\": ", 15); - ret = snprintf(value, ULLONG_MAXLEN, "%llu", (unsigned long long)match->start); + ret = snprintf(value, ULLONG_MAXLEN, "%zu", matches->count); if (ret > 0) write(fd, value, ret); else { - log_simple_message(LMT_EXT_ERROR, "Error while converting offset!"); + log_simple_message(LMT_EXT_ERROR, "Error while converting value!"); write(fd, "null", 4); } write(fd, ",\n", 2); - /* Position dans le binaire (hexadécimal) */ + /* Détail des correspondances */ for (i = 0; i < level; i++) write(fd, padding->data, padding->len); - write(fd, "\"offset_hex\": ", 14); + write(fd, "\"matches\": [\n", 13); - ret = snprintf(value, ULLONG_MAXLEN, "\"0x%llx\"", (unsigned long long)match->start); + content = g_scan_context_get_content(matches->context); - if (ret > 0) - write(fd, value, ret); - - else + for_each_match_area(iter, matches->areas) { - log_simple_message(LMT_EXT_ERROR, "Error while converting offset to hex!"); - write(fd, "null", 4); - } - - write(fd, ",\n", 2); + /* Marqueur de début */ - /* Affichage du contenu brut */ + for (i = 0; i < (level + 1); i++) + write(fd, padding->data, padding->len); - for (i = 0; i < level; i++) - write(fd, padding->data, padding->len); + write(fd, "{\n", 2); - write(fd, "\"content\": \"", 12); + /* Position dans le binaire (décimal) */ - init_vmpa(&pos, match->start, VMPA_NO_VIRTUAL); + for (i = 0; i < (level + 2); i++) + write(fd, padding->data, padding->len); - data = g_binary_content_get_raw_access(match->content, &pos, match->len); - assert(data != NULL); + write(fd, "\"offset\": ", 10); - for (k = 0; k < match->len; k++) - { - if (data[k] == '\\') - write(fd, "\\\\", 2); + ret = snprintf(value, ULLONG_MAXLEN, "%llu", (unsigned long long)iter->start); - else if (isprint(data[k])) - write(fd, &data[k], 1); + if (ret > 0) + write(fd, value, ret); else { - write(fd, "\\u", 2); + log_simple_message(LMT_EXT_ERROR, "Error while converting offset!"); + write(fd, "null", 4); + } - /** - * Cf. https://datatracker.ietf.org/doc/html/rfc8259#section-7 - */ - ret = snprintf(value, ULLONG_MAXLEN, "%04hhx", data[k]); + write(fd, ",\n", 2); - if (ret > 0) - { - assert(ret == 4); - write(fd, value, ret); - } + /* Position dans le binaire (hexadécimal) */ - else - { - log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); - write(fd, "??", 2); - } + for (i = 0; i < (level + 2); i++) + write(fd, padding->data, padding->len); - } + write(fd, "\"offset_hex\": ", 14); - } + ret = snprintf(value, ULLONG_MAXLEN, "\"0x%llx\"", (unsigned long long)iter->start); - write(fd, "\",\n", 3); + if (ret > 0) + write(fd, value, ret); - /* Affichage du contenu en version humainement lisible */ + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting offset to hex!"); + write(fd, "null", 4); + } - for (i = 0; i < level; i++) - write(fd, padding->data, padding->len); + write(fd, ",\n", 2); - write(fd, "\"content_str\": \"", 16); + /* Affichage du contenu brut */ - init_vmpa(&pos, match->start, VMPA_NO_VIRTUAL); + for (i = 0; i < (level + 2); i++) + write(fd, padding->data, padding->len); - data = g_binary_content_get_raw_access(match->content, &pos, match->len); - assert(data != NULL); + write(fd, "\"content\": \"", 12); - for (k = 0; k < match->len; k++) - { - if (data[k] == '\\') - write(fd, "\\\\", 2); + init_vmpa(&pos, iter->start, VMPA_NO_VIRTUAL); - else if (isprint(data[k])) - write(fd, &data[k], 1); + len = iter->end - iter->start; - else + data = g_binary_content_get_raw_access(content, &pos, len); + assert(data != NULL); + + for (k = 0; k < len; k++) { - write(fd, "\\\\x", 3); + if (data[k] == '\\') + write(fd, "\\\\", 2); - ret = snprintf(value, ULLONG_MAXLEN, "%02hhx", data[k]); + else if (isprint(data[k])) + write(fd, &data[k], 1); - if (ret > 0) + else { - assert(ret == 2); - write(fd, value, ret); + write(fd, "\\u", 2); + + /** + * Cf. https://datatracker.ietf.org/doc/html/rfc8259#section-7 + */ + ret = snprintf(value, ULLONG_MAXLEN, "%04hhx", data[k]); + + if (ret > 0) + { + assert(ret == 4); + write(fd, value, ret); + } + + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); + write(fd, "??", 2); + } + } + } + + write(fd, "\",\n", 3); + + /* Affichage du contenu en version humainement lisible */ + + for (i = 0; i < (level + 2); i++) + write(fd, padding->data, padding->len); + + write(fd, "\"content_str\": \"", 16); + + init_vmpa(&pos, iter->start, VMPA_NO_VIRTUAL); + + data = g_binary_content_get_raw_access(content, &pos, len); + assert(data != NULL); + + for (k = 0; k < len; k++) + { + if (data[k] == '\\') + write(fd, "\\\\", 2); + + else if (isprint(data[k])) + write(fd, &data[k], 1); + else { - log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); - write(fd, "??", 2); + write(fd, "\\\\x", 3); + + ret = snprintf(value, ULLONG_MAXLEN, "%02hhx", data[k]); + + if (ret > 0) + { + assert(ret == 2); + write(fd, value, ret); + } + + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); + write(fd, "??", 2); + } + } } - } + write(fd, "\",\n", 3); - write(fd, "\",\n", 3); + /* Affichage du contenu brut */ - /* Affichage du contenu brut */ + for (i = 0; i < (level + 2); i++) + write(fd, padding->data, padding->len); - for (i = 0; i < level; i++) - write(fd, padding->data, padding->len); + write(fd, "\"length\": ", 10); - write(fd, "\"length\": ", 10); + ret = snprintf(value, ULLONG_MAXLEN, "%llu", (unsigned long long)len); - init_vmpa(&pos, match->start, VMPA_NO_VIRTUAL); + if (ret > 0) + write(fd, value, ret); - ret = snprintf(value, ULLONG_MAXLEN, "%llu", (unsigned long long)match->len); + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); + write(fd, "-1", 2); + } - if (ret > 0) - write(fd, value, ret); + write(fd, ",\n", 2); - else - { - log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); - write(fd, "-1", 2); - } + /* Affichage du contenu brut (hexadécimal) */ - write(fd, ",\n", 2); + for (i = 0; i < (level + 2); i++) + write(fd, padding->data, padding->len); - /* Affichage du contenu brut (hexadécimal) */ + write(fd, "\"length_hex\": ", 14); - for (i = 0; i < level; i++) - write(fd, padding->data, padding->len); + ret = snprintf(value, ULLONG_MAXLEN, "\"0x%llx\"", (unsigned long long)len); - write(fd, "\"length_hex\": ", 14); + if (ret > 0) + write(fd, value, ret); - init_vmpa(&pos, match->start, VMPA_NO_VIRTUAL); + else + { + log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); + write(fd, "\"0xffffffffffffffff\"", 20); + } - ret = snprintf(value, ULLONG_MAXLEN, "\"0x%llx\"", (unsigned long long)match->len); + write(fd, "\n", 1); - if (ret > 0) - write(fd, value, ret); + /* Marqueur de fin */ + + for (i = 0; i < (level + 1); i++) + write(fd, padding->data, padding->len); + + if (is_last_match_area(iter, matches->areas)) + write(fd, "}\n", 2); + else + write(fd, "},\n", 3); - else - { - log_simple_message(LMT_EXT_ERROR, "Error while converting data!"); - write(fd, "\"0xffffffffffffffff\"", 20); } - write(fd, "\n", 1); + g_object_unref(G_OBJECT(content)); + + for (i = 0; i < level; i++) + write(fd, padding->data, padding->len); + + write(fd, "]\n", 2); } |