summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Makefile.am20
-rw-r--r--src/common/bits.c173
-rw-r--r--src/common/bits.h10
-rw-r--r--src/common/curl.c78
-rw-r--r--src/common/curl.h14
-rw-r--r--src/common/extstr.c2
-rw-r--r--src/common/json.c203
-rw-r--r--src/common/json.h44
-rw-r--r--src/common/szbin.h88
-rw-r--r--src/common/szstr.h5
-rw-r--r--src/common/xdg.c338
-rw-r--r--src/common/xdg.h19
12 files changed, 879 insertions, 115 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 27ead1d..7f0fe4e 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -6,7 +6,6 @@ libcommon_la_SOURCES = \
array.h array.c \
asm.h asm.c \
bconst.h \
- bits.h bits.c \
compression.h compression.c \
cpp.h \
cpu.h cpu.c \
@@ -21,7 +20,6 @@ libcommon_la_SOURCES = \
packed.h packed.c \
pathname.h pathname.c \
pearson.h pearson.c \
- shuffle.h shuffle.c \
sort.h sort.c \
sqlite.h sqlite.c \
szstr.h \
@@ -58,14 +56,30 @@ libcommon4_la_SOURCES = \
extstr.h extstr.c \
fnv1a.h fnv1a.c \
io.h io.c \
+ json.h json.c \
leb128.h leb128.c \
macros.h \
packed.h packed.c \
pathname.h pathname.c \
+ shuffle.h shuffle.c \
sort.h sort.c \
+ szbin.h \
xdg.h xdg.c
-libcommon4_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBSSL_CFLAGS)
+if BUILD_CURL_SUPPORT
+
+libcommon4_la_SOURCES += \
+ curl.h curl.c
+
+endif
+
+libcommon4_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBSSL_CFLAGS) $(LIBJSONGLIB_CFLAGS)
+
+if BUILD_CURL_SUPPORT
+
+libcommon4_la_CFLAGS += $(LIBCURL_CFLAGS)
+
+endif
devdir = $(includedir)/chrysalide/$(subdir:src/%=core/%)
diff --git a/src/common/bits.c b/src/common/bits.c
index 37e3141..f730c66 100644
--- a/src/common/bits.c
+++ b/src/common/bits.c
@@ -50,7 +50,8 @@ struct _bitfield_t
/* Taille des mots intégrés */
-#define BF_WORD_SIZE (sizeof(unsigned long) * 8)
+#define BF_WORD_BIT_SIZE (sizeof(unsigned long) * 8)
+#define BF_WORD_BYTE_SIZE sizeof(unsigned long)
/* Crée un champ de bits initialisé à zéro. */
@@ -82,10 +83,10 @@ static bitfield_t *_create_bit_field(size_t length)
size_t needed; /* Nombre de mots à allouer */
size_t base; /* Allocation de base en octets*/
- needed = length / (sizeof(unsigned long) * 8);
- if (length % (sizeof(unsigned long) * 8) != 0) needed++;
+ needed = length / BF_WORD_BIT_SIZE;
+ if (length % BF_WORD_BIT_SIZE != 0) needed++;
- base = sizeof(bitfield_t) + needed * sizeof(unsigned long);
+ base = sizeof(bitfield_t) + needed * BF_WORD_BYTE_SIZE;
result = malloc(base);
@@ -148,7 +149,7 @@ bitfield_t *dup_bit_field(const bitfield_t *field)
result = _create_bit_field(field->length);
- memcpy(result->bits, field->bits, result->used_words * sizeof(unsigned long));
+ memcpy(result->bits, field->bits, result->used_words * BF_WORD_BYTE_SIZE);
return result;
@@ -191,7 +192,7 @@ void copy_bit_field(bitfield_t *dest, const bitfield_t *src)
{
assert(dest->length == src->length);
- memcpy(dest->bits, src->bits, dest->used_words * sizeof(unsigned long));
+ memcpy(dest->bits, src->bits, dest->used_words * BF_WORD_BYTE_SIZE);
}
@@ -217,8 +218,8 @@ void truncate_bit_field(bitfield_t **field, size_t length)
_field = *field;
- needed = length / (sizeof(unsigned long) * 8);
- if (length % (sizeof(unsigned long) * 8) != 0) needed++;
+ needed = length / BF_WORD_BIT_SIZE;
+ if (length % BF_WORD_BIT_SIZE != 0) needed++;
if (needed <= _field->allocated_words)
{
@@ -263,10 +264,10 @@ void resize_bit_field(bitfield_t **field, size_t length)
{
/* Redimensionnement */
- needed = length / (sizeof(unsigned long) * 8);
- if (length % (sizeof(unsigned long) * 8) != 0) needed++;
+ needed = length / BF_WORD_BIT_SIZE;
+ if (length % BF_WORD_BIT_SIZE != 0) needed++;
- base = sizeof(bitfield_t) + needed * sizeof(unsigned long);
+ base = sizeof(bitfield_t) + needed * BF_WORD_BYTE_SIZE;
/* Initialisation, si nécessaire */
@@ -275,8 +276,8 @@ void resize_bit_field(bitfield_t **field, size_t length)
*field = realloc(_field, base);
_field = *field;
- last = _field->length / (sizeof(unsigned long) * 8);
- remaining = _field->length % (sizeof(unsigned long) * 8);
+ last = _field->length / BF_WORD_BIT_SIZE;
+ remaining = _field->length % BF_WORD_BIT_SIZE;
if (remaining != 0)
{
@@ -367,7 +368,7 @@ int compare_bit_fields(const bitfield_t *a, const bitfield_t *b)
else
{
- final = a->length % (8 * sizeof(unsigned long));
+ final = a->length % BF_WORD_BIT_SIZE;
if (final == 0)
final = ~0lu;
@@ -414,7 +415,7 @@ int compare_bit_fields(const bitfield_t *a, const bitfield_t *b)
void reset_all_in_bit_field(bitfield_t *field)
{
- memset(field->bits, 0u, field->used_words * sizeof(unsigned long));
+ memset(field->bits, 0u, field->used_words * BF_WORD_BYTE_SIZE);
}
@@ -433,7 +434,35 @@ void reset_all_in_bit_field(bitfield_t *field)
void set_all_in_bit_field(bitfield_t *field)
{
- memset(field->bits, ~0u, field->used_words * sizeof(unsigned long));
+ memset(field->bits, ~0u, field->used_words * BF_WORD_BYTE_SIZE);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : field = champ de bits à modifier. *
+* first = indice du premier bit à traiter. *
+* *
+* Description : Bascule à 0 une partie d'un champ de bits. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void reset_in_bit_field(bitfield_t *field, size_t first)
+{
+ size_t index; /* Cellule de tableau visée */
+ size_t remaining; /* Nombre de bits restants */
+
+ assert(first < field->length);
+
+ index = first / BF_WORD_BIT_SIZE;
+ remaining = first % BF_WORD_BIT_SIZE;
+
+ field->bits[index] &= ~(1ul << remaining);
}
@@ -452,7 +481,7 @@ void set_all_in_bit_field(bitfield_t *field)
* *
******************************************************************************/
-void reset_in_bit_field(bitfield_t *field, size_t first, size_t count)
+void reset_multi_in_bit_field(bitfield_t *field, size_t first, size_t count)
{
size_t last; /* Point d'arrêt de la boucle */
size_t i; /* Boucle de parcours */
@@ -465,8 +494,8 @@ void reset_in_bit_field(bitfield_t *field, size_t first, size_t count)
for (i = first; i < last; i++)
{
- index = i / (sizeof(unsigned long) * 8);
- remaining = i % (sizeof(unsigned long) * 8);
+ index = i / BF_WORD_BIT_SIZE;
+ remaining = i % BF_WORD_BIT_SIZE;
field->bits[index] &= ~(1ul << remaining);
@@ -479,6 +508,34 @@ void reset_in_bit_field(bitfield_t *field, size_t first, size_t count)
* *
* Paramètres : field = champ de bits à modifier. *
* first = indice du premier bit à traiter. *
+* *
+* Description : Bascule à 1 une partie d'un champ de bits. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void set_in_bit_field(bitfield_t *field, size_t first)
+{
+ size_t index; /* Cellule de tableau visée */
+ size_t remaining; /* Nombre de bits restants */
+
+ assert(first < field->length);
+
+ index = first / BF_WORD_BIT_SIZE;
+ remaining = first % BF_WORD_BIT_SIZE;
+
+ field->bits[index] |= (1ul << remaining);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : field = champ de bits à modifier. *
+* first = indice du premier bit à traiter. *
* count = nombre de bits à marquer. *
* *
* Description : Bascule à 1 une partie d'un champ de bits. *
@@ -489,7 +546,7 @@ void reset_in_bit_field(bitfield_t *field, size_t first, size_t count)
* *
******************************************************************************/
-void set_in_bit_field(bitfield_t *field, size_t first, size_t count)
+void set_multi_in_bit_field(bitfield_t *field, size_t first, size_t count)
{
size_t last; /* Point d'arrêt de la boucle */
size_t i; /* Boucle de parcours */
@@ -502,8 +559,8 @@ void set_in_bit_field(bitfield_t *field, size_t first, size_t count)
for (i = first; i < last; i++)
{
- index = i / (sizeof(unsigned long) * 8);
- remaining = i % (sizeof(unsigned long) * 8);
+ index = i / BF_WORD_BIT_SIZE;
+ remaining = i % BF_WORD_BIT_SIZE;
field->bits[index] |= (1ul << remaining);
@@ -587,12 +644,12 @@ void or_bit_field_at(bitfield_t *dest, const bitfield_t *src, size_t first)
assert((first + src->length) <= dest->length);
- start = first / (sizeof(unsigned long) * 8);
- offset = first % (sizeof(unsigned long) * 8);
+ start = first / BF_WORD_BIT_SIZE;
+ offset = first % BF_WORD_BIT_SIZE;
- remaining = (first + src->length) % (sizeof(unsigned long) * 8);
+ remaining = (first + src->length) % BF_WORD_BIT_SIZE;
- if ((first + src->length) % (sizeof(unsigned long) * 8) > 0)
+ if ((first + src->length) % BF_WORD_BIT_SIZE > 0)
last_iter = src->used_words;
else
last_iter = src->used_words - 1;
@@ -605,7 +662,7 @@ void or_bit_field_at(bitfield_t *dest, const bitfield_t *src, size_t first)
word = 0;
if (i > 0 && offset > 0)
- word |= src->bits[i - 1] >> (sizeof(unsigned long) * 8 - offset);
+ word |= src->bits[i - 1] >> (BF_WORD_BIT_SIZE - offset);
if (i == last_iter && remaining > 0)
word &= (1ul << remaining) - 1;
@@ -638,8 +695,8 @@ bool test_in_bit_field(const bitfield_t *field, size_t n)
assert(n < field->length);
- index = n / (sizeof(unsigned long) * 8);
- remaining = n % (sizeof(unsigned long) * 8);
+ index = n / BF_WORD_BIT_SIZE;
+ remaining = n % BF_WORD_BIT_SIZE;
result = field->bits[index] & (1ul << remaining);
@@ -670,8 +727,8 @@ bool test_and_set_in_bit_field(bitfield_t *field, size_t n)
assert(n < field->length);
- index = n / (sizeof(unsigned long) * 8);
- remaining = n % (sizeof(unsigned long) * 8);
+ index = n / BF_WORD_BIT_SIZE;
+ remaining = n % BF_WORD_BIT_SIZE;
bits = field->bits + index;
@@ -713,8 +770,8 @@ static bool test_state_in_bit_field(const bitfield_t *field, size_t first, size_
for (i = first; i < last; i++)
{
- index = i / (sizeof(unsigned long) * 8);
- remaining = i % (sizeof(unsigned long) * 8);
+ index = i / BF_WORD_BIT_SIZE;
+ remaining = i % BF_WORD_BIT_SIZE;
current = field->bits[index] & (1ul << remaining);
@@ -805,14 +862,30 @@ static bool test_state_within_bit_field(const bitfield_t *field, size_t first, c
unsigned long bitmask; /* Masque à appliquer */
unsigned long test; /* Valeur résultante du test */
- result = true;
+ /**
+ * Si un masque est à appliquer avec débordement, les bits débordés sont considérés
+ * comme initialisés à la valeur par défaut.
+ */
+
+ if ((first + mask->length) > field->length)
+ {
+ assert(mask->length > 0);
+
+ result = (state == field->default_state);
+ if (!result) goto done;
+
+ if (first >= field->length)
+ goto done;
+
+ }
- assert((first + mask->length) <= field->length);
+ else
+ result = true;
- start = first / (sizeof(unsigned long) * 8);
- offset = first % (sizeof(unsigned long) * 8);
+ start = first / BF_WORD_BIT_SIZE;
+ offset = first % BF_WORD_BIT_SIZE;
- remaining = mask->length % (sizeof(unsigned long) * 8);
+ remaining = mask->length % BF_WORD_BIT_SIZE;
if (remaining == 0)
finalcut = ~0lu;
@@ -830,7 +903,7 @@ static bool test_state_within_bit_field(const bitfield_t *field, size_t first, c
{
word = field->bits[windex] >> offset;
if ((windex + 1) < field->used_words)
- word |= field->bits[windex + 1] << (sizeof(unsigned long) * 8 - offset);
+ word |= field->bits[windex + 1] << (BF_WORD_BIT_SIZE - offset);
}
bitmask = mask->bits[i];
@@ -849,6 +922,8 @@ static bool test_state_within_bit_field(const bitfield_t *field, size_t first, c
}
+ done:
+
return result;
}
@@ -934,7 +1009,7 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
}
else
{
- i = *prev / BF_WORD_SIZE;
+ i = *prev / BF_WORD_BIT_SIZE;
if (i >= field->used_words)
{
@@ -944,9 +1019,9 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
word = field->bits[i];
- last_pos = *prev % BF_WORD_SIZE;
+ last_pos = *prev % BF_WORD_BIT_SIZE;
- if ((last_pos + 1) == BF_WORD_SIZE)
+ if ((last_pos + 1) == BF_WORD_BIT_SIZE)
goto next_word;
word &= ~((1lu << (last_pos + 1)) - 1);
@@ -957,7 +1032,7 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
if (found > 0)
{
- result = i * BF_WORD_SIZE + found - 1;
+ result = i * BF_WORD_BIT_SIZE + found - 1;
goto done;
}
@@ -973,7 +1048,7 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
if (found > 0)
{
- result = i * BF_WORD_SIZE + found - 1;
+ result = i * BF_WORD_BIT_SIZE + found - 1;
/**
* Validation des bornes finales, pour le dernier mot.
@@ -1148,7 +1223,7 @@ size_t popcount_for_bit_field(const bitfield_t *field)
{
value = field->bits[i];
- if (remaining < (8 * sizeof(unsigned long)))
+ if (remaining < BF_WORD_BIT_SIZE)
value &= (1lu << remaining) - 1;
#if __WORDSIZE == 64
@@ -1159,7 +1234,7 @@ size_t popcount_for_bit_field(const bitfield_t *field)
# error "Unkown word size"
#endif
- remaining -= 8 * sizeof(unsigned long);
+ remaining -= BF_WORD_BIT_SIZE;
}
@@ -1190,8 +1265,6 @@ void output_bit_field(const bitfield_t *field)
printf("[len=%zu] \n", field->length);
-#define MAX_BITS (sizeof(unsigned long) * 8)
-
for (i = 0; i < field->used_words; i++)
{
value = field->bits[i];
@@ -1199,9 +1272,9 @@ void output_bit_field(const bitfield_t *field)
if (i > 0)
printf("| ");
- for (k = 0; k < MAX_BITS; k++)
+ for (k = 0; k < BF_WORD_BIT_SIZE; k++)
{
- if ((i * MAX_BITS + k) >= field->length)
+ if ((i * BF_WORD_BIT_SIZE + k) >= field->length)
break;
printf("%c", value & (1lu << k) ? '1' : '.');
diff --git a/src/common/bits.h b/src/common/bits.h
index a66c6f0..3898c73 100644
--- a/src/common/bits.h
+++ b/src/common/bits.h
@@ -65,10 +65,16 @@ void reset_all_in_bit_field(bitfield_t *);
void set_all_in_bit_field(bitfield_t *);
/* Bascule à 0 une partie d'un champ de bits. */
-void reset_in_bit_field(bitfield_t *, size_t, size_t);
+void reset_in_bit_field(bitfield_t *, size_t);
+
+/* Bascule à 0 une partie d'un champ de bits. */
+void reset_multi_in_bit_field(bitfield_t *, size_t, size_t);
+
+/* Bascule à 1 une partie d'un champ de bits. */
+void set_in_bit_field(bitfield_t *, size_t);
/* Bascule à 1 une partie d'un champ de bits. */
-void set_in_bit_field(bitfield_t *, size_t, size_t);
+void set_multi_in_bit_field(bitfield_t *, size_t, size_t);
/* Réalise une opération ET logique entre deux champs de bits. */
void and_bit_field(bitfield_t *, const bitfield_t *);
diff --git a/src/common/curl.c b/src/common/curl.c
index 573180f..e9fb92f 100644
--- a/src/common/curl.c
+++ b/src/common/curl.c
@@ -2,7 +2,7 @@
/* Chrysalide - Outil d'analyse de fichiers binaires
* curl.c - encapsulation des fonctionnalités de cURL
*
- * Copyright (C) 2022 Cyrille Bagard
+ * Copyright (C) 2022-2024 Cyrille Bagard
*
* This file is part of Chrysalide.
*
@@ -28,14 +28,62 @@
#include <string.h>
+#include "../core/global.h"
+
+
+
+/* Mémorise un volume de données transférées. */
+static int track_curl_data_transfers(CURL *, curl_infotype, char *, size_t, void *);
/* Mémorise les données reçues en réponse à une requête. */
-static size_t receive_data_from_internet(void *, size_t, size_t, curl_net_data_t *);
+static size_t receive_data_from_internet(void *, size_t, size_t, sized_binary_t *);
/******************************************************************************
* *
+* Paramètres : handle = gestionnaire de la requête cURL concernée. *
+* type = type de transfert réalisé. *
+* data = pointeur vers les données manipulées. *
+* size = quantité de données transférées. *
+* unused = adresse non utilisée ici. *
+* *
+* Description : Mémorise un volume de données transférées. *
+* *
+* Retour : CURLE_OK. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static int track_curl_data_transfers(CURL *handle, curl_infotype type, char *data, size_t size, void *unused)
+{
+ switch (type)
+ {
+ case CURLINFO_HEADER_IN:
+ case CURLINFO_DATA_IN:
+ /*case CURLINFO_SSL_DATA_IN:*/
+ update_network_stats(size, 0);
+ break;
+
+ case CURLINFO_HEADER_OUT:
+ case CURLINFO_DATA_OUT:
+ /*case CURLINFO_SSL_DATA_OUT:*/
+ update_network_stats(0, size);
+ break;
+
+ default:
+ break;
+
+ }
+
+ return 0;
+
+}
+
+
+/******************************************************************************
+* *
* Paramètres : contents = contenu nouveau en arrivance d'Internet. *
* size = taille d'un élément reçu. *
* nmemb = quantité de ces éléments. *
@@ -49,19 +97,19 @@ static size_t receive_data_from_internet(void *, size_t, size_t, curl_net_data_t
* *
******************************************************************************/
-static size_t receive_data_from_internet(void *contents, size_t size, size_t nmemb, curl_net_data_t *data)
+static size_t receive_data_from_internet(void *contents, size_t size, size_t nmemb, sized_binary_t *data)
{
size_t realsize; /* Taille brute en octets */
realsize = size * nmemb;
- data->memory = realloc(data->memory, data->size + realsize + 1);
+ data->data = realloc(data->data, data->size + realsize + 1);
- memcpy(&(data->memory[data->size]), contents, realsize);
+ memcpy(&(data->data[data->size]), contents, realsize);
data->size += realsize;
- data->memory[data->size] = 0;
+ data->data[data->size] = 0;
return realsize;
@@ -85,7 +133,7 @@ static size_t receive_data_from_internet(void *contents, size_t size, size_t nme
* *
******************************************************************************/
-bool send_http_get_request(const char *url, char * const headers[], size_t hcount, const char *cookies, setup_extra_curl_cb ecb, curl_net_data_t *resp)
+bool send_http_get_request(const char *url, char * const headers[], size_t hcount, const char *cookies, setup_extra_curl_cb ecb, sized_binary_t *resp)
{
bool result; /* Bilan d'opération à renvoyer*/
CURL *req; /* Requête HTTP */
@@ -95,12 +143,14 @@ bool send_http_get_request(const char *url, char * const headers[], size_t hcoun
result = false;
- resp->memory = NULL;
- resp->size = 0;
+ init_sized_binary(resp);
req = curl_easy_init();
if (req == NULL) goto exit;
+ curl_easy_setopt(req, CURLOPT_DEBUGFUNCTION, track_curl_data_transfers);
+ curl_easy_setopt(req, CURLOPT_VERBOSE, 1L);
+
curl_easy_setopt(req, CURLOPT_URL, url);
/* Entêtes à transmettre */
@@ -165,7 +215,7 @@ bool send_http_get_request(const char *url, char * const headers[], size_t hcoun
* *
******************************************************************************/
-bool send_http_post_request(const char *url, char * const headers[], size_t hcount, const char *cookies, const curl_net_data_t *payload, setup_extra_curl_cb ecb, curl_net_data_t *resp)
+bool send_http_post_request(const char *url, char * const headers[], size_t hcount, const char *cookies, const sized_binary_t *payload, setup_extra_curl_cb ecb, sized_binary_t *resp)
{
bool result; /* Bilan d'opération à renvoyer*/
CURL *req; /* Requête HTTP */
@@ -175,12 +225,14 @@ bool send_http_post_request(const char *url, char * const headers[], size_t hcou
result = false;
- resp->memory = NULL;
- resp->size = 0;
+ init_sized_binary(resp);
req = curl_easy_init();
if (req == NULL) goto exit;
+ curl_easy_setopt(req, CURLOPT_DEBUGFUNCTION, track_curl_data_transfers);
+ curl_easy_setopt(req, CURLOPT_VERBOSE, 1L);
+
curl_easy_setopt(req, CURLOPT_URL, url);
/* Entêtes à transmettre */
@@ -205,7 +257,7 @@ bool send_http_post_request(const char *url, char * const headers[], size_t hcou
curl_easy_setopt(req, CURLOPT_POST, 1);
- curl_easy_setopt(req, CURLOPT_POSTFIELDS, payload->memory);
+ curl_easy_setopt(req, CURLOPT_POSTFIELDS, payload->data);
curl_easy_setopt(req, CURLOPT_POSTFIELDSIZE, payload->size);
/* Emission de la requête */
diff --git a/src/common/curl.h b/src/common/curl.h
index 02d9e91..1fc8f54 100644
--- a/src/common/curl.h
+++ b/src/common/curl.h
@@ -2,7 +2,7 @@
/* Chrysalide - Outil d'analyse de fichiers binaires
* curl.h - prototypes pour l'encapsulation des fonctionnalités de cURL
*
- * Copyright (C) 2022 Cyrille Bagard
+ * Copyright (C) 2022-2024 Cyrille Bagard
*
* This file is part of Chrysalide.
*
@@ -29,14 +29,8 @@
#include <curl/curl.h>
+#include "szbin.h"
-/* Données échangées avec Internet */
-typedef struct _curl_net_data_t
-{
- char *memory; /* Zone de mémoire allouée */
- size_t size; /* Quantité de données */
-
-} curl_net_data_t;
/* Prototype pour une intervention complémentaire dans la préparation des requêtes */
@@ -44,10 +38,10 @@ typedef CURLcode (* setup_extra_curl_cb) (CURL *);
/* Mémorise les données reçues en réponse à une requête. */
-bool send_http_get_request(const char *, char * const [], size_t, const char *, setup_extra_curl_cb, curl_net_data_t *);
+bool send_http_get_request(const char *, char * const [], size_t, const char *, setup_extra_curl_cb, sized_binary_t *);
/* Mémorise les données reçues en réponse à une requête. */
-bool send_http_post_request(const char *, char * const [], size_t, const char *, const curl_net_data_t *, setup_extra_curl_cb, curl_net_data_t *);
+bool send_http_post_request(const char *, char * const [], size_t, const char *, const sized_binary_t *, setup_extra_curl_cb, sized_binary_t *);
diff --git a/src/common/extstr.c b/src/common/extstr.c
index bd3491f..fe0bab4 100644
--- a/src/common/extstr.c
+++ b/src/common/extstr.c
@@ -636,7 +636,7 @@ const void *memcasemem(const void *haystack, size_t haystacklen, const void *nee
* *
* Description : Compare sans casse deux série d'octets entre elles. *
* *
-* Retour : Status de la comparaison des séries d'octets. *
+* Retour : Statut de la comparaison des séries d'octets. *
* *
* Remarques : - *
* *
diff --git a/src/common/json.c b/src/common/json.c
new file mode 100644
index 0000000..cb64822
--- /dev/null
+++ b/src/common/json.c
@@ -0,0 +1,203 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * json.c - manipulations génériques de données au format JSON
+ *
+ * Copyright (C) 2024 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Chrysalide. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "json.h"
+
+
+#include <assert.h>
+
+
+
+/******************************************************************************
+* *
+* Paramètres : root = racine d'une arborescence JSON chargée. *
+* query = chemin XPath d'une requête à mener. *
+* error = description d'une éventuelle erreur rencontrée. [OUT]*
+* *
+* Description : Détermine la présence d'un élément avec une valeur textuelle.*
+* *
+* Retour : true si le noeud existe et porte une chaîne comme valeur. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool has_json_string_value(JsonNode *root, const char *xpath, GError **error)
+{
+ bool result; /* Bilan à retourner */
+ JsonNode *found; /* Accès direct aux trouvailles*/
+ JsonArray *array; /* Liste des résultats */
+ guint count; /* Taille de la liste */
+ JsonNode *node; /* Noeud portant la cible */
+ GValue value; /* Valeur correspondante */
+
+ result = false;
+
+ *error = NULL;
+ found = json_path_query(xpath, root, error);
+
+ if (found != NULL)
+ {
+ assert(*error == NULL);
+
+ array = json_node_get_array(found);
+
+ count = json_array_get_length(array);
+
+ if (count == 1)
+ {
+ node = json_array_get_element(array, 0);
+
+ memset(&value, 0, sizeof(GValue));
+ json_node_get_value(node, &value);
+
+ if (G_VALUE_HOLDS(&value, G_TYPE_STRING))
+ result = true;
+
+ }
+
+ json_node_unref(found);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : root = racine d'une arborescence JSON chargée. *
+* query = chemin XPath d'une requête à mener. *
+* error = description d'une éventuelle erreur rencontrée. [OUT]*
+* *
+* Description : Fournit le contenu d'un élément avec une valeur textuelle. *
+* *
+* Retour : Valeur trouvée ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+char *get_json_string_value(JsonNode *root, const char *xpath, GError **error)
+{
+ char *result; /* Valeur à retourner */
+ JsonNode *found; /* Accès direct aux trouvailles*/
+ JsonArray *array; /* Liste des résultats */
+ guint count; /* Taille de la liste */
+ JsonNode *node; /* Noeud portant la cible */
+ GValue value; /* Valeur correspondante */
+ const gchar *raw; /* Valeur brute présente */
+
+ result = NULL;
+
+ *error = NULL;
+ found = json_path_query(xpath, root, error);
+
+ if (found != NULL)
+ {
+ assert(*error == NULL);
+
+ array = json_node_get_array(found);
+
+ count = json_array_get_length(array);
+
+ if (count == 1)
+ {
+ node = json_array_get_element(array, 0);
+
+ memset(&value, 0, sizeof(GValue));
+ json_node_get_value(node, &value);
+
+ if (G_VALUE_HOLDS(&value, G_TYPE_STRING))
+ {
+ raw = json_array_get_string_element(array, 0);
+ result = strdup(raw);
+ }
+
+ }
+
+ json_node_unref(found);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : root = racine d'une arborescence JSON chargée. *
+* query = chemin XPath d'une requête à mener. *
+* error = description d'une éventuelle erreur rencontrée. [OUT]*
+* *
+* Description : Fournit le contenu d'un élément avec une valeur entière. *
+* *
+* Retour : Valeur trouvée ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+long long get_json_integer_value(JsonNode *root, const char *xpath, GError **error)
+{
+ long long result; /* Valeur à retourner */
+ JsonNode *found; /* Accès direct aux trouvailles*/
+ JsonArray *array; /* Liste des résultats */
+ guint count; /* Taille de la liste */
+ JsonNode *node; /* Noeud portant la cible */
+ GValue value; /* Valeur correspondante */
+
+ result = 0xffffffffffffffffll;
+
+ *error = NULL;
+ found = json_path_query(xpath, root, error);
+
+ if (found != NULL)
+ {
+ assert(*error == NULL);
+
+ array = json_node_get_array(found);
+
+ count = json_array_get_length(array);
+
+ if (count == 1)
+ {
+ node = json_array_get_element(array, 0);
+
+ memset(&value, 0, sizeof(GValue));
+ json_node_get_value(node, &value);
+
+ if (G_VALUE_HOLDS(&value, G_TYPE_INT64))
+ result = json_array_get_int_element(array, 0);
+
+ }
+
+ json_node_unref(found);
+
+ }
+
+ return result;
+
+}
diff --git a/src/common/json.h b/src/common/json.h
new file mode 100644
index 0000000..8794bf6
--- /dev/null
+++ b/src/common/json.h
@@ -0,0 +1,44 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * json.h - prototypes pour des manipulations génériques de données au format JSON
+ *
+ * Copyright (C) 2024 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Chrysalide. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _COMMON_JSON_H
+#define _COMMON_JSON_H
+
+
+#include <stdbool.h>
+#include <json-glib/json-glib.h>
+
+
+
+/* Détermine la présence d'un élément avec une valeur textuelle. */
+bool has_json_string_value(JsonNode *, const char *, GError **);
+
+/* Fournit le contenu d'un élément avec une valeur textuelle. */
+char *get_json_string_value(JsonNode *, const char *, GError **);
+
+/* Fournit le contenu d'un élément avec une valeur entière. */
+long long get_json_integer_value(JsonNode *, const char *, GError **);
+
+
+
+#endif /* _COMMON_JSON_H */
diff --git a/src/common/szbin.h b/src/common/szbin.h
new file mode 100644
index 0000000..ac938ab
--- /dev/null
+++ b/src/common/szbin.h
@@ -0,0 +1,88 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * szbin.h - prototypes pour une manipulation de données accompagnées d'une taille
+ *
+ * Copyright (C) 2024 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Chrysalide. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _COMMON_SZBIN_H
+#define _COMMON_SZBIN_H
+
+
+#include <malloc.h>
+#include <string.h>
+
+
+
+/* Structure associant données et taille */
+typedef struct _sized_binary_t
+{
+ union {
+
+ const char *static_data; /* Données non modifiées */
+ char *data; /* Chaîne de caractères */
+
+ };
+
+ size_t size; /* Taille correspondante */
+
+} sized_binary_t;
+
+
+#define init_sized_binary(sb) \
+ do \
+ { \
+ (sb)->data = NULL; \
+ (sb)->size = 0; \
+ } \
+ while (0)
+
+
+#define setup_sized_binary(sb, s) \
+ do \
+ { \
+ (sb)->data = malloc(s); \
+ (sb)->size = s; \
+ } \
+ while (0)
+
+
+#define dup_into_sized_binary(sb, d, s) \
+ do \
+ { \
+ setup_sized_binary(sb, s); \
+ memcpy((sb)->data, d, s); \
+ } \
+ while (0)
+
+
+#define exit_sized_binary(sb) \
+ do \
+ { \
+ if ((sb)->data != NULL) \
+ { \
+ free((sb)->data); \
+ init_sized_binary(sb); \
+ } \
+ } \
+ while (0)
+
+
+
+#endif /* _COMMON_SZBIN_H */
diff --git a/src/common/szstr.h b/src/common/szstr.h
index 406a9f1..c48d81f 100644
--- a/src/common/szstr.h
+++ b/src/common/szstr.h
@@ -54,7 +54,7 @@ typedef struct _sized_string_t
typedef sized_string_t sized_binary_t;
-
+/*
#define init_szstr(s) \
do \
{ \
@@ -62,6 +62,7 @@ typedef sized_string_t sized_binary_t;
(s)->len = 0; \
} \
while (0)
+*/
#define szstrdup(dst, src) \
do \
@@ -74,6 +75,7 @@ typedef sized_string_t sized_binary_t;
#define copy_szstr(d, s) (d) = (s);
+/*
#define exit_szstr(s) \
do \
{ \
@@ -84,6 +86,7 @@ typedef sized_string_t sized_binary_t;
} \
} \
while (0)
+*/
#define szstrcmp(s1, s2) \
({ \
diff --git a/src/common/xdg.c b/src/common/xdg.c
index cabff75..eb64dad 100644
--- a/src/common/xdg.c
+++ b/src/common/xdg.c
@@ -24,20 +24,41 @@
#include "xdg.h"
-#include <dirent.h>
-#include <errno.h>
+#include <assert.h>
#include <glib.h>
#include <malloc.h>
-#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
+
+
+#include "pathname.h"
+
+
+
+/**
+ * Cf. https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
+ */
+
+/* $HOME/.cache */
+#define CACHE_HOME_SUFFIX ".cache" G_DIR_SEPARATOR_S
+
+/* $HOME/.config */
+#define CONFIG_HOME_SUFFIX ".config" G_DIR_SEPARATOR_S
+
+/* $HOME/.local/share */
+#define DATA_HOME_SUFFIX ".local" G_DIR_SEPARATOR_S "share" G_DIR_SEPARATOR_S
+
+/* $HOME/.local/state */
+#define STATE_HOME_SUFFIX ".local" G_DIR_SEPARATOR_S "state" G_DIR_SEPARATOR_S
/******************************************************************************
* *
* Paramètres : suffix = élément visé dans le répertoire de configuration. *
+* create = assure la mise en place du répertoire final. *
* *
-* Description : Détermine le chemin d'un répertoire selon les specs. XDG. *
+* Description : Détermine le chemin d'un répertoire de données XDG. *
* *
* Retour : Chemin d'accès aux configurations personnelles ou NULL. *
* *
@@ -45,70 +66,321 @@
* *
******************************************************************************/
-char *get_xdg_config_dir(const char *suffix)
+char *get_xdg_cache_dir(const char *suffix, bool create)
{
char *result; /* Chemin d'accès à renvoyer */
const char *env; /* Valeur de l'environnement */
- DIR *directory; /* Répertoire avec contenu ? */
- struct dirent *entry; /* Elément de répertoire */
+ int ret; /* Bilan d'une assurance */
+
+ assert(suffix[0] != G_DIR_SEPARATOR);
result = NULL;
- env = getenv("XDG_CONFIG_HOME");
+ env = getenv("XDG_CACHE_HOME");
if (env != NULL && env[0] != '\0')
{
- directory = opendir(env);
- if (directory == NULL) goto default_cfg_dir;
+ result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, suffix);
+
+ }
+
+ else
+ {
+ env = getenv("HOME");
+ if (env == NULL || env[0] == '\0') goto no_env;
+
+ result = calloc(strlen(env) + 1 + strlen(CACHE_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, CACHE_HOME_SUFFIX);
+ strcat(result, suffix);
+
+ }
+
+ if (create)
+ {
+ ret = ensure_path_exists(result);
- while (1)
+ if (ret != 0)
{
- errno = 0;
+ free(result);
+ result = NULL;
+ }
- entry = readdir(directory);
+ }
- if (entry == NULL)
- {
- if (errno != 0)
- perror("readdir");
+ no_env:
- break;
+ return result;
+
+}
- }
- if (strcmp(entry->d_name, ".") == 0) continue;
- if (strcmp(entry->d_name, "..") == 0) continue;
+/******************************************************************************
+* *
+* Paramètres : suffix = élément visé dans le répertoire de configuration. *
+* create = assure la mise en place du répertoire final. *
+* *
+* Description : Détermine le chemin d'un répertoire de données XDG. *
+* *
+* Retour : Chemin d'accès aux configurations personnelles ou NULL. *
+* *
+* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. *
+* *
+******************************************************************************/
- result = calloc(strlen(env) + 2 + strlen(suffix) + 1, sizeof(char));
- strcpy(result, env);
+char *get_xdg_config_dir(const char *suffix, bool create)
+{
+ char *result; /* Chemin d'accès à renvoyer */
+ const char *env; /* Valeur de l'environnement */
+ int ret; /* Bilan d'une assurance */
- if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
- strcat(result, G_DIR_SEPARATOR_S);
+ assert(suffix[0] != G_DIR_SEPARATOR);
- strcat(result, ".");
- strcat(result, suffix);
+ result = NULL;
+ env = getenv("XDG_CONFIG_HOME");
+
+ if (env != NULL && env[0] != '\0')
+ {
+ result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, suffix);
+
+ }
+
+ else
+ {
+ env = getenv("HOME");
+ if (env == NULL || env[0] == '\0') goto no_env;
+
+ result = calloc(strlen(env) + 1 + strlen(CONFIG_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, CONFIG_HOME_SUFFIX);
+ strcat(result, suffix);
+
+ }
+
+ if (create)
+ {
+ ret = ensure_path_exists(result);
+
+ if (ret != 0)
+ {
+ free(result);
+ result = NULL;
}
- closedir(directory);
+ }
+
+ no_env:
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : suffix = élément visé dans le répertoire de configuration. *
+* create = assure la mise en place du répertoire final. *
+* *
+* Description : Détermine le chemin d'un répertoire de données XDG. *
+* *
+* Retour : Chemin d'accès aux configurations personnelles ou NULL. *
+* *
+* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. *
+* *
+******************************************************************************/
+
+char *get_xdg_data_dir(const char *suffix, bool create)
+{
+ char *result; /* Chemin d'accès à renvoyer */
+ const char *env; /* Valeur de l'environnement */
+ int ret; /* Bilan d'une assurance */
+
+ assert(suffix[0] != G_DIR_SEPARATOR);
+
+ result = NULL;
+
+ env = getenv("XDG_DATA_HOME");
+
+ if (env != NULL && env[0] != '\0')
+ {
+ result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, suffix);
+
+ }
+
+ else
+ {
+ env = getenv("HOME");
+ if (env == NULL || env[0] == '\0') goto no_env;
+
+ result = calloc(strlen(env) + 1 + strlen(DATA_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, DATA_HOME_SUFFIX);
+ strcat(result, suffix);
}
- default_cfg_dir:
+ if (create)
+ {
+ ret = ensure_path_exists(result);
+
+ if (ret != 0)
+ {
+ free(result);
+ result = NULL;
+ }
+
+ }
+
+ no_env:
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : suffix = élément visé dans le répertoire de configuration. *
+* create = assure la mise en place du répertoire final. *
+* *
+* Description : Détermine le chemin d'un répertoire de données XDG. *
+* *
+* Retour : Chemin d'accès aux configurations personnelles ou NULL. *
+* *
+* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. *
+* *
+******************************************************************************/
+
+char *get_xdg_state_dir(const char *suffix, bool create)
+{
+ char *result; /* Chemin d'accès à renvoyer */
+ const char *env; /* Valeur de l'environnement */
+ int ret; /* Bilan d'une assurance */
+
+ assert(suffix[0] != G_DIR_SEPARATOR);
+
+ result = NULL;
+
+ env = getenv("XDG_STATE_HOME");
+
+ if (env != NULL && env[0] != '\0')
+ {
+ result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
- if (result == NULL)
+ strcat(result, suffix);
+
+ }
+
+ else
{
env = getenv("HOME");
- if (env == NULL || env[0] == '\0') return NULL;
+ if (env == NULL || env[0] == '\0') goto no_env;
+
+ result = calloc(strlen(env) + 1 + strlen(STATE_HOME_SUFFIX) + strlen(suffix) + 1, sizeof(char));
+
+ strcpy(result, env);
+
+ if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
+ strcat(result, G_DIR_SEPARATOR_S);
+
+ strcat(result, STATE_HOME_SUFFIX);
+ strcat(result, suffix);
+
+ }
+
+ if (create)
+ {
+ ret = ensure_path_exists(result);
+
+ if (ret != 0)
+ {
+ free(result);
+ result = NULL;
+ }
+
+ }
- result = calloc(strlen(env) + 1 + strlen(".config" G_DIR_SEPARATOR_S) + strlen(suffix) + 1, sizeof(char));
+ no_env:
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : suffix = élément visé dans le répertoire de configuration. *
+* *
+* Description : Détermine le chemin d'un répertoire éphémère XDG. *
+* *
+* Retour : Chemin d'accès aux configurations personnelles ou NULL. *
+* *
+* Remarques : cf. http://standards.freedesktop.org/basedir-spec/. *
+* *
+******************************************************************************/
+
+char *get_xdg_runtime_dir(const char *suffix)
+{
+ char *result; /* Chemin d'accès à renvoyer */
+ const char *env; /* Valeur de l'environnement */
+
+ assert(suffix[0] != G_DIR_SEPARATOR);
+
+ result = NULL;
+
+ env = getenv("XDG_RUNTIME_DIR");
+
+ if (env != NULL && env[0] != '\0')
+ {
+ result = calloc(strlen(env) + 1 + strlen(suffix) + 1, sizeof(char));
strcpy(result, env);
if (env[strlen(env) - 1] != G_DIR_SEPARATOR)
strcat(result, G_DIR_SEPARATOR_S);
- strcat(result, ".config" G_DIR_SEPARATOR_S);
strcat(result, suffix);
}
diff --git a/src/common/xdg.h b/src/common/xdg.h
index c9c2327..a6cd91d 100644
--- a/src/common/xdg.h
+++ b/src/common/xdg.h
@@ -25,9 +25,24 @@
#define _COMMON_XDG_H
+#include <stdbool.h>
-/* Détermine le chemin d'un répertoire selon les specs. XDG. */
-char *get_xdg_config_dir(const char *);
+
+
+/* Détermine le chemin d'un répertoire de données XDG. */
+char *get_xdg_cache_dir(const char *, bool);
+
+/* Détermine le chemin d'un répertoire de données XDG. */
+char *get_xdg_config_dir(const char *, bool);
+
+/* Détermine le chemin d'un répertoire de données XDG. */
+char *get_xdg_data_dir(const char *, bool);
+
+/* Détermine le chemin d'un répertoire de données XDG. */
+char *get_xdg_state_dir(const char *, bool);
+
+/* Détermine le chemin d'un répertoire éphémère XDG. */
+char *get_xdg_runtime_dir(const char *);