diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Makefile.am | 35 | ||||
-rw-r--r-- | src/common/bits.c | 305 | ||||
-rw-r--r-- | src/common/bits.h | 19 | ||||
-rw-r--r-- | src/common/compiler.h | 16 | ||||
-rw-r--r-- | src/common/cpp.h | 17 | ||||
-rw-r--r-- | src/common/curl.c | 78 | ||||
-rw-r--r-- | src/common/curl.h | 14 | ||||
-rw-r--r-- | src/common/datatypes.h | 30 | ||||
-rw-r--r-- | src/common/entropy.c | 88 | ||||
-rw-r--r-- | src/common/entropy.h | 41 | ||||
-rw-r--r-- | src/common/extstr.c | 2 | ||||
-rw-r--r-- | src/common/fnv1a.c | 33 | ||||
-rw-r--r-- | src/common/fnv1a.h | 6 | ||||
-rw-r--r-- | src/common/io.h | 57 | ||||
-rw-r--r-- | src/common/json.c | 203 | ||||
-rw-r--r-- | src/common/json.h | 44 | ||||
-rw-r--r-- | src/common/leb128.c | 252 | ||||
-rw-r--r-- | src/common/leb128.h | 17 | ||||
-rw-r--r-- | src/common/pathname.c | 23 | ||||
-rw-r--r-- | src/common/pathname.h | 2 | ||||
-rw-r--r-- | src/common/sort.h | 3 | ||||
-rw-r--r-- | src/common/szbin.h | 282 | ||||
-rw-r--r-- | src/common/szstr.h | 5 | ||||
-rw-r--r-- | src/common/xdg.c | 338 | ||||
-rw-r--r-- | src/common/xdg.h | 19 |
25 files changed, 1761 insertions, 168 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 27ead1d..1056cb2 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 \ @@ -49,11 +47,14 @@ endif libcommon4_la_SOURCES = \ + array.h array.c \ asm.h asm.c \ bits.h bits.c \ compiler.h \ + cpp.h \ datatypes.h \ dllist.h dllist.c \ + entropy.h entropy.c \ environment.h environment.c \ extstr.h extstr.c \ fnv1a.h fnv1a.c \ @@ -62,10 +63,38 @@ libcommon4_la_SOURCES = \ 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 + +if BUILD_JSONGLIB_SUPPORT + +libcommon4_la_SOURCES += \ + json.h json.c + +endif + +libcommon4_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBSSL_CFLAGS) $(LIBJSONGLIB_CFLAGS) + +if BUILD_CURL_SUPPORT + +libcommon4_la_CFLAGS += $(LIBCURL_CFLAGS) + +endif + +if BUILD_JSONGLIB_SUPPORT + +libcommon4_la_CFLAGS += $(LIBJSONGLIB_CFLAGS) + +endif devdir = $(includedir)/chrysalide/$(subdir:src/%=core/%) diff --git a/src/common/bits.c b/src/common/bits.c index 37e3141..27296f2 100644 --- a/src/common/bits.c +++ b/src/common/bits.c @@ -31,6 +31,8 @@ #include "asm.h" +#include "io.h" +#include "leb128.h" @@ -50,7 +52,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 +85,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 +151,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 +194,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 +220,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 +266,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 +278,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 +370,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 +417,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 +436,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 +483,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 +496,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 +510,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 +548,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 +561,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 +646,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 +664,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 +697,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 +729,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 +772,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 +864,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); + } - start = first / (sizeof(unsigned long) * 8); - offset = first % (sizeof(unsigned long) * 8); + else + result = true; - remaining = mask->length % (sizeof(unsigned long) * 8); + start = first / BF_WORD_BIT_SIZE; + offset = first % BF_WORD_BIT_SIZE; + + remaining = mask->length % BF_WORD_BIT_SIZE; if (remaining == 0) finalcut = ~0lu; @@ -830,7 +905,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 +924,8 @@ static bool test_state_within_bit_field(const bitfield_t *field, size_t first, c } + done: + return result; } @@ -934,7 +1011,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 +1021,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 +1034,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 +1050,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 +1225,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 +1236,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 +1267,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 +1274,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' : '.'); @@ -1218,3 +1293,133 @@ void output_bit_field(const bitfield_t *field) } #endif + + +/****************************************************************************** +* * +* Paramètres : fd = flux ouvert en lecture. * +* length = éventuelle indication de la taille du champ ? * +* def_state = éventuelle indication de l'état par défaut ? * +* endian = ordre des bits dans la source. * +* * +* Description : Restaure un champ de bits depuis un flux ouvert. * +* * +* Retour : Adresse du champs de bits mis en place, NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bitfield_t *load_bit_field(int fd, const size_t *length, const bool *def_state, SourceEndian endian) +{ + bitfield_t *result; /* Structure à retourner */ + size_t final_length; /* Nombre de bits représentés */ + uleb128_t leb128_value; /* Valeur LEB128 chargée */ + bool status; /* Bilan d'une lecture */ + bool final_default_state; /* Etat d'initialisation */ + uint8_t u8_value; /* Valeur sur 8 bits chargée */ + size_t i; /* Boucle de parcours */ + + result = NULL; + + if (length != NULL) + final_length = *length; + else + { + status = load_uleb128(&leb128_value, fd); + if (!status) goto exit; + + final_length = leb128_value; + + } + + if (def_state != NULL) + final_default_state = *def_state; + + else + { + status = load_u8(fd, &u8_value); + if (!status) goto exit; + + final_default_state = !!u8_value; + + } + + result = _create_bit_field(final_length); + + result->default_state = final_default_state; + + for (i = 0; i < result->used_words; i++) + { + status = load_uleb128(&leb128_value, fd); + + if (status) + result->bits[i] = leb128_value; + + else + { + delete_bit_field(result); + result = NULL; + break; + } + + } + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : field = champ de bits à consulter. * +* fd = flux ouvert en écriture. * +* skip_len = saute la sauvegarde de la taille du champ ? * +* skip_def = saute la sauvegarde de l'état par défaut ? * +* endian = ordre des bits dans la source. * +* * +* Description : Sauvegarde un champ de bits dans un flux ouvert. * +* * +* Retour : Bilan de l'opération : true en cas de succès, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool store_bit_field(const bitfield_t *field, int fd, bool skip_len, bool skip_def, SourceEndian endian) +{ + bool result; /* Bilan à retourner */ + size_t i; /* Boucle de parcours */ + + if (skip_len) + result = true; + else + { + result = store_uleb128((const uleb128_t []) { field->length }, fd); + if (!result) goto exit; + } + + if (skip_def) + result = true; + else + { + result = store_u8(fd, field->default_state); + if (!result) goto exit; + } + + for (i = 0; i < field->used_words; i++) + { + result = store_uleb128((const uleb128_t []) { field->bits[i] }, fd); + + if (!result) + break; + + } + + exit: + + return result; + +} diff --git a/src/common/bits.h b/src/common/bits.h index a66c6f0..d9c83c8 100644 --- a/src/common/bits.h +++ b/src/common/bits.h @@ -29,6 +29,9 @@ #include <sys/types.h> +#include "datatypes.h" + + /* Champ de bits simple */ typedef struct _bitfield_t bitfield_t; @@ -65,10 +68,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, size_t); +void set_in_bit_field(bitfield_t *, size_t); + +/* Bascule à 1 une partie d'un champ de bits. */ +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 *); @@ -113,6 +122,12 @@ void output_bit_field(const bitfield_t *); #endif +/* Restaure un champ de bits depuis un flux ouvert. */ +bitfield_t *load_bit_field(int, const size_t *, const bool *, SourceEndian); + +/* Sauvegarde un champ de bits dans un flux ouvert. */ +bool store_bit_field(const bitfield_t *, int, bool, bool, SourceEndian); + #endif /* _COMMON_BITS_H */ diff --git a/src/common/compiler.h b/src/common/compiler.h index 2585e47..65df8a4 100644 --- a/src/common/compiler.h +++ b/src/common/compiler.h @@ -2,7 +2,7 @@ /* Chrysalide - Outil d'analyse de fichiers binaires * compiler.h - prototypes pour le regroupement d'astuces à destination du compilateur * - * Copyright (C) 2024 Cyrille Bagard + * Copyright (C) 2024-2025 Cyrille Bagard * * This file is part of Chrysalide. * @@ -34,5 +34,19 @@ #define __weak __attribute__((weak)) +/** + * Contournement des avertissements de la forme suivante : + * + * assignment to 'const char * const*' from incompatible pointer type 'char **' [-Wincompatible-pointer-types] + * + * Références : + * - https://www.reddit.com/r/C_Programming/comments/qa2231/const_char_const_and_char_are_incompatible/ + * - https://stackoverflow.com/questions/78125/why-cant-i-convert-char-to-a-const-char-const-in-c + * - https://c-faq.com/ansi/constmismatch.html + */ + +#define CONST_ARRAY_CAST(a, tp) (const tp **)a + + #endif /* _COMMON_COMPILER_H */ diff --git a/src/common/cpp.h b/src/common/cpp.h index 39e7676..4ebad82 100644 --- a/src/common/cpp.h +++ b/src/common/cpp.h @@ -2,7 +2,7 @@ /* Chrysalide - Outil d'analyse de fichiers binaires * cpp.h - prototypes pour avoir à disposition un langage C plus plus mieux * - * Copyright (C) 2010-2020 Cyrille Bagard + * Copyright (C) 2010-2025 Cyrille Bagard * * This file is part of Chrysalide. * @@ -31,12 +31,27 @@ /** + * Fournit la taille d'une chaîne statique. + */ +#define STATIC_STR_SIZE(s) (sizeof(s) - 1) + + +/** * Fournit la taille d'un tableau statique. */ #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) /** + * Facilite la transmission d'arguement pour des fonctions + * comme strncmp() et Cie. + */ +#define SL(str) str, strlen(str) + +#define STCSL(str) str, STATIC_STR_SIZE(str) + + +/** * Détermine la taille de la plus longue chaîne de caractères * correspondant à un type donné. */ 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/datatypes.h b/src/common/datatypes.h index 3983267..248f4a1 100644 --- a/src/common/datatypes.h +++ b/src/common/datatypes.h @@ -52,5 +52,35 @@ typedef enum _SourceEndian } SourceEndian; +/* Taille des données intégrées */ +typedef enum _MemoryDataSize +{ + MDS_UNDEFINED = 0x0, /* Taille non définie */ + + MDS_4_BITS_UNSIGNED = 0x1, /* Opérande sur 4 bits n.-s. */ + MDS_8_BITS_UNSIGNED = 0x2, /* Opérande sur 8 bits n.-s. */ + MDS_16_BITS_UNSIGNED = 0x3, /* Opérande sur 16 bits n.-s. */ + MDS_32_BITS_UNSIGNED = 0x4, /* Opérande sur 32 bits n.-s. */ + MDS_64_BITS_UNSIGNED = 0x5, /* Opérande sur 64 bits n.-s. */ + + MDS_4_BITS_SIGNED = 0x8 | 0x1, /* Opérande sur 4 bits signés */ + MDS_8_BITS_SIGNED = 0x8 | 0x2, /* Opérande sur 8 bits signés */ + MDS_16_BITS_SIGNED = 0x8 | 0x3, /* Opérande sur 16 bits signés */ + MDS_32_BITS_SIGNED = 0x8 | 0x4, /* Opérande sur 32 bits signés */ + MDS_64_BITS_SIGNED = 0x8 | 0x5 /* Opérande sur 64 bits signés */ + +} MemoryDataSize; + +#define MDS_RANGE(mds) ((mds & 0x7) - 1) +#define MDS_SIGN 0x8 +#define MDS_IS_SIGNED(mds) (mds & MDS_SIGN) + +#define MDS_4_BITS MDS_4_BITS_UNSIGNED +#define MDS_8_BITS MDS_8_BITS_UNSIGNED +#define MDS_16_BITS MDS_16_BITS_UNSIGNED +#define MDS_32_BITS MDS_32_BITS_UNSIGNED +#define MDS_64_BITS MDS_64_BITS_UNSIGNED + + #endif /* _COMMON_DATATYPES_H */ diff --git a/src/common/entropy.c b/src/common/entropy.c new file mode 100644 index 0000000..8dae698 --- /dev/null +++ b/src/common/entropy.c @@ -0,0 +1,88 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * entropy.c - calcul de l'entropie d'un contenu binaire + * + * 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 "entropy.h" + + +#include <math.h> +#include <string.h> + + + +/****************************************************************************** +* * +* Paramètres : data = séquence d'octets à traiter. * +* len = quantité de ces octets. * +* bits = calcul en concidérant les bits et non les octets ? * +* * +* Description : Détermine l'entropie d'un contenu binaire. * +* * +* Retour : Valeur d'entropie du contenu fourni. * +* * +* Remarques : - * +* * +******************************************************************************/ + +double compute_entropy(const bin_t *data, size_t len, bool bits) +{ + double result; /* Valeur calculée à renvoyer */ + unsigned long counters[256]; /* Décompte des valeurs */ + const bin_t *d_max; /* Borne de fin de parcours #1 */ + const bin_t *d_iter; /* Boucle de parcours #1 */ + double log_2; /* Valeur constante de log2 */ + unsigned long *c_max; /* Borne de fin de parcours #2 */ + unsigned long *c_iter; /* Boucle de parcours #2 */ + double freq; /* Fréquence liée à une valeur */ + + result = 0.0; + + memset(counters, 0, sizeof(counters)); + + d_max = data + len; + + for (d_iter = data; d_iter < d_max; d_iter++) + counters[*d_iter]++; + + /** + * Explication du choix de log : + * https://stackoverflow.com/questions/990477/how-to-calculate-the-entropy-of-a-file/990646#990646 + */ + + log_2 = log(bits ? 2.0 : 256.0); + + c_max = counters + 256; + + for (c_iter = counters; c_iter < c_max; c_iter++) + { + if (*c_iter == 0lu) + continue; + + freq = ((double)*c_iter) / ((double)len); + + result -= freq * (log(freq) / log_2); + + } + + return result; + +} diff --git a/src/common/entropy.h b/src/common/entropy.h new file mode 100644 index 0000000..cfd51ef --- /dev/null +++ b/src/common/entropy.h @@ -0,0 +1,41 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * entropy.h - prototypes pour le calcul de l'entropie d'un contenu binaire + * + * 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_ENTROPY_H +#define _COMMON_ENTROPY_H + + +#include <stdbool.h> +#include <stddef.h> + + +#include "../common/datatypes.h" + + + +/* Détermine l'entropie d'un contenu binaire. */ +double compute_entropy(const bin_t *, size_t, bool); + + + +#endif /* _COMMON_ENTROPY_H */ 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/fnv1a.c b/src/common/fnv1a.c index a8afd0a..057d6c9 100644 --- a/src/common/fnv1a.c +++ b/src/common/fnv1a.c @@ -2,7 +2,7 @@ /* Chrysalide - Outil d'analyse de fichiers binaires * fnv1a.c - implémentaton du calcul rapide d'empreintes de chaînes * - * Copyright (C) 2012-2018 Cyrille Bagard + * Copyright (C) 2012-2024 Cyrille Bagard * * This file is part of Chrysalide. * @@ -87,3 +87,34 @@ fnv64_t fnv_64a_hash(const char *str) return result; } + + +/****************************************************************************** +* * +* Paramètres : data = données binaires à traiter. * +* size = quantité de ces données. * +* * +* Description : Détermine l'empreinte FNV1a d'une séquence d'octets. * +* * +* Retour : Valeur calculée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +fnv64_t fnv_64a_hash_data(const uint8_t *data, size_t size) +{ + fnv64_t result; /* Valeur à retourner */ + size_t i; /* Boucle de parcours */ + + result = FNV1A_64_INIT; + + for (i = 0; i < size; i++) + { + result ^= (fnv64_t)data[i]; + result *= FNV_64_PRIME; + } + + return result; + +} diff --git a/src/common/fnv1a.h b/src/common/fnv1a.h index eec1460..4da3108 100644 --- a/src/common/fnv1a.h +++ b/src/common/fnv1a.h @@ -2,7 +2,7 @@ /* Chrysalide - Outil d'analyse de fichiers binaires * fnv1a.h - prototypes pour l'implémentaton du calcul rapide d'empreintes de chaînes * - * Copyright (C) 2012-2018 Cyrille Bagard + * Copyright (C) 2012-2024 Cyrille Bagard * * This file is part of Chrysalide. * @@ -27,6 +27,7 @@ #include <stdbool.h> #include <stdint.h> +#include <sys/types.h> /** @@ -46,6 +47,9 @@ int cmp_fnv_64a(fnv64_t, fnv64_t); /* Détermine l'empreinte FNV1a d'une chaîne de caractères. */ fnv64_t fnv_64a_hash(const char *); +/* Détermine l'empreinte FNV1a d'une séquence d'octets. */ +fnv64_t fnv_64a_hash_data(const uint8_t *, size_t); + #endif /* _COMMON_FNV1A_H */ diff --git a/src/common/io.h b/src/common/io.h index 7fe9d9d..1932b12 100644 --- a/src/common/io.h +++ b/src/common/io.h @@ -39,12 +39,69 @@ /* Lit des données depuis un flux local. */ bool safe_read(int, void *, size_t); +#define load_u8(f, v) \ + safe_read(f, v, sizeof(uint8_t)); + +#define load_u16(f, v, e) \ + ({ \ + bool __ret; \ + uint16_t __val; \ + __ret = safe_read(f, &__val, sizeof(uint16_t)); \ + __val = swap_u16(&__val, e); \ + *(v) = __val; \ + __ret; \ + }) + +#define load_u32(f, v, e) \ + ({ \ + bool __ret; \ + uint32_t __val; \ + __ret = safe_read(f, &__val, sizeof(uint32_t)); \ + __val = swap_u32(&__val, e); \ + *(v) = __val; \ + __ret; \ + }) + +#define load_u64(f, v, e) \ + ({ \ + bool __ret; \ + uint64_t __val; \ + __ret = safe_read(f, &__val, sizeof(uint64_t)); \ + __val = swap_u64(&__val, e); \ + *(v) = __val; \ + __ret; \ + }) + /* Lit des données depuis un flux local. */ ssize_t safe_read_partial(int, void *, size_t); /* Ecrit des données dans un flux local. */ bool safe_write(int, const void *, size_t); +#define store_u8(f, v) \ + safe_write(f, (const uint8_t []){ v }, sizeof(uint8_t)); + +#define store_u16(f, v, e) \ + ({ \ + uint16_t __val; \ + __val = swap_u16((const uint16_t []){ v }, e); \ + safe_write(f, &__val, sizeof(uint16_t)); \ + }) + +#define store_u32(f, v, e) \ + ({ \ + uint32_t __val; \ + __val = swap_u32((const uint32_t []){ v }, e); \ + safe_write(f, &__val, sizeof(uint32_t)); \ + }) + +#define store_u64(f, v, e) \ + ({ \ + uint64_t __val; \ + __val = swap_u64((const uint64_t []){ v }, e); \ + safe_write(f, &__val, sizeof(uint64_t)); \ + }) + /* Réceptionne des données depuis un flux réseau. */ bool safe_recv(int, void *, size_t, int); 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/leb128.c b/src/common/leb128.c index c8d2ace..7fae4d0 100644 --- a/src/common/leb128.c +++ b/src/common/leb128.c @@ -24,6 +24,20 @@ #include "leb128.h" +#include <malloc.h> + + +#include "io.h" + + +/** + * Quantité maximale d'octets de représentation. + * + * sizeof([u]leb128_t) / 7 = 9.142857142857142 + * + */ +#define MAX_LEB128_BYTES 9 + /****************************************************************************** * * @@ -113,10 +127,63 @@ bool read_leb128(leb128_t *target, const bin_t *data, phys_t *pos, phys_t len) /****************************************************************************** * * +* Paramètres : value = valeur à constituer. [OUT] * +* fd = flux ouvert en lecture à consulter. * +* * +* Description : Restaure un nombre non signé encodé au format LEB128. * +* * +* Retour : Bilan de l'opération : true en cas de succès, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool load_uleb128(uleb128_t *value, int fd) +{ + bool result; /* Bilan à retourner */ + unsigned int shift; /* Décalage à appliquer */ + uint8_t byte; /* Octet à transposer */ + + result = false; + + *value = 0; + + shift = 0; + + while (true) + { + /* Encodage sur trop d'octets ? */ + if (shift > (7 * MAX_LEB128_BYTES)) + { + result = false; + break; + } + + result = safe_read(fd, &byte, sizeof(uint8_t)); + if (!result) break; + + *value |= ((byte & 0x7f) << shift); + + result = true; + + if ((byte & 0x80) == 0x00) + break; + + shift += 7; + + } + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : value = valeur à consigner. * -* pbuf = tampon de données à constituer. [OUT] * +* fd = flux ouvert en écriture. * * * -* Description : Encode un nombre non signé encodé au format LEB128. * +* Description : Sauvegarde un nombre non signé encodé au format LEB128. * * * * Retour : Bilan de l'opération : true en cas de succès, false sinon. * * * @@ -124,7 +191,7 @@ bool read_leb128(leb128_t *target, const bin_t *data, phys_t *pos, phys_t len) * * ******************************************************************************/ -bool pack_uleb128(const uleb128_t *value, packed_buffer_t *pbuf) +bool store_uleb128(const uleb128_t *value, int fd) { bool result; /* Bilan à retourner */ uleb128_t tmp; /* Valeur modifiable */ @@ -140,7 +207,7 @@ bool pack_uleb128(const uleb128_t *value, packed_buffer_t *pbuf) if (tmp != 0) byte |= 0x80; - result = extend_packed_buffer(pbuf, &byte, sizeof(uint8_t), false); + result = safe_write(fd, &byte, sizeof(uint8_t)); } while (result && tmp != 0); @@ -153,9 +220,9 @@ bool pack_uleb128(const uleb128_t *value, packed_buffer_t *pbuf) /****************************************************************************** * * * Paramètres : value = valeur à consigner. * -* pbuf = tampon de données à constituer. [OUT] * +* len = taille du tampon de données à constitué. [OUT] * * * -* Description : Encode un nombre signé encodé au format LEB128. * +* Description : Encode un nombre non signé encodé au format LEB128. * * * * Retour : Bilan de l'opération : true en cas de succès, false sinon. * * * @@ -163,19 +230,81 @@ bool pack_uleb128(const uleb128_t *value, packed_buffer_t *pbuf) * * ******************************************************************************/ -bool pack_leb128(const leb128_t *value, packed_buffer_t *pbuf) +void *pack_uleb128(const uleb128_t *value, size_t *len) { + uint8_t *result; /* Données à retourner */ + uleb128_t tmp; /* Valeur modifiable */ + uint8_t *byte; /* Octet à transposer */ - bool result; /* Bilan à retourner */ + /* Calcul de la quantité d'octets nécessaires */ + + *len = 0; + + tmp = *value; + + do + { + tmp >>= 7; + (*len)++; + } + while (tmp != 0); + + /* Exportation */ + + result = malloc(*len * sizeof(uint8_t)); + byte = result; + + tmp = *value; + + do + { + *byte = (tmp & 0x7f); + tmp >>= 7; + + if (tmp != 0) + *byte |= 0x80; + + byte++; + + } + while (tmp != 0); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : value = valeur à consigner. * +* len = taille du tampon de données à constitué. [OUT] * +* * +* Description : Encode un nombre signé encodé au format LEB128. * +* * +* Retour : Bilan de l'opération : true en cas de succès, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +void *pack_leb128(const leb128_t *value, size_t *len) +{ + uint8_t *result; /* Données à retourner */ + bool negative; /* Nature de la valeur */ uleb128_t tmp; /* Valeur modifiable */ bool more; /* Poursuite des traitements */ - bool negative; /* Nature de la valeur */ uint8_t byte; /* Octet à transposer */ + uint8_t *iter; /* Boucle de parcours */ + + negative = (*value < 0); + + /* Calcul de la quantité d'octets nécessaires */ + + *len = 0; tmp = *value; more = true; - negative = (*value < 0); while (more) { @@ -198,10 +327,44 @@ bool pack_leb128(const leb128_t *value, packed_buffer_t *pbuf) if ((tmp == 0 && (byte & 0x40) == 0x00) || (tmp == -1 && (byte & 0x40) == 0x40)) more = false; + (*len)++; + + } + + /* Exportation */ + + result = malloc(*len * sizeof(uint8_t)); + iter = result; + + tmp = *value; + + more = true; + + while (more) + { + *iter = (tmp & 0x7f); + tmp >>= 7; + + /** + * Propagation forcée du bit de signe pour les implémentations de + * décalage basées sur une opération logique et non arithmétique. + */ + + if (negative) + tmp |= (~0llu << (LEB128_BITS_COUNT - 7)); + + /** + * Le bit de signe n'est pas le bit de poids fort ici : + * On travaille sur 7 bits, donc le masque est 0x40 ! + */ + + if ((tmp == 0 && (*iter & 0x40) == 0x00) || (tmp == -1 && (*iter & 0x40) == 0x40)) + more = false; + else - byte |= 0x80; + *iter |= 0x80; - result = extend_packed_buffer(pbuf, &byte, sizeof(uint8_t), false); + iter++; } @@ -213,7 +376,8 @@ bool pack_leb128(const leb128_t *value, packed_buffer_t *pbuf) /****************************************************************************** * * * Paramètres : value = valeur à constituer. [OUT] * -* pbuf = tampon de données à consulter. * +* pos = tête de lecture à faire évoluer. [OUT] * +* max = position maximale liée à la fin des données. * * * * Description : Décode un nombre non signé encodé au format LEB128. * * * @@ -223,38 +387,45 @@ bool pack_leb128(const leb128_t *value, packed_buffer_t *pbuf) * * ******************************************************************************/ -bool unpack_uleb128(uleb128_t *value, packed_buffer_t *pbuf) +bool unpack_uleb128(uleb128_t *value, const void **pos, const void *max) { bool result; /* Bilan à retourner */ unsigned int shift; /* Décalage à appliquer */ - uint8_t byte; /* Octet à transposer */ + uint8_t *byte; /* Octet à transposer */ - result = true; + result = false; *value = 0; shift = 0; + byte = *(uint8_t **)pos; - while (true) + do { /* Encodage sur trop d'octets ? */ - if (shift > (7 * sizeof(uleb128_t))) + if (shift > (7 * MAX_LEB128_BYTES)) { result = false; break; } - result = extract_packed_buffer(pbuf, &byte, sizeof(uint8_t), false); - if (!result) break; + /* Atteinte de la fin des données ? */ + if ((void *)byte >= max) + { + result = false; + break; + } - *value |= ((byte & 0x7f) << shift); + *value |= ((*byte & 0x7f) << shift); - if ((byte & 0x80) == 0x00) - break; + result = true; shift += 7; } + while ((*byte++ & 0x80) == 0x80); + + *pos = byte; return result; @@ -264,7 +435,8 @@ bool unpack_uleb128(uleb128_t *value, packed_buffer_t *pbuf) /****************************************************************************** * * * Paramètres : value = valeur à constituer. [OUT] * -* pbuf = tampon de données à consulter. * +* pos = tête de lecture à faire évoluer. [OUT] * +* max = position maximale liée à la fin des données. * * * * Description : Décode un nombre signé encodé au format LEB128. * * * @@ -274,44 +446,56 @@ bool unpack_uleb128(uleb128_t *value, packed_buffer_t *pbuf) * * ******************************************************************************/ -bool unpack_leb128(leb128_t *value, packed_buffer_t *pbuf) +bool unpack_leb128(leb128_t *value, const void **pos, const void *max) { bool result; /* Bilan à retourner */ unsigned int shift; /* Décalage à appliquer */ - uint8_t byte; /* Octet à transposer */ + uint8_t *byte; /* Octet à transposer */ - result = true; + result = false; *value = 0; shift = 0; + byte = *(uint8_t **)pos; do { /* Encodage sur trop d'octets ? */ - if (shift > (7 * sizeof(leb128_t))) + if (shift > (7 * MAX_LEB128_BYTES)) { result = false; break; } - result = extract_packed_buffer(pbuf, &byte, sizeof(uint8_t), false); - if (!result) break; + /* Atteinte de la fin des données ? */ + if ((void *)byte >= max) + { + result = false; + break; + } - *value |= ((byte & 0x7f) << shift); + *value |= ((*byte & 0x7f) << shift); + + result = true; shift += 7; } - while ((byte & 0x80) == 0x80); + while ((*byte++ & 0x80) == 0x80); /** * Le bit de signe n'est pas le bit de poids fort ici : * On travaille sur 7 bits, donc le masque est 0x40 ! */ - if (shift < LEB128_BITS_COUNT && (byte & 0x40) == 0x40) - *value |= (~0llu << shift); + if (result) + { + if (shift < LEB128_BITS_COUNT && (byte[-1] & 0x40) == 0x40) + *value |= (~0llu << shift); + } + + *pos = byte; return result; diff --git a/src/common/leb128.h b/src/common/leb128.h index f438068..cb712a3 100644 --- a/src/common/leb128.h +++ b/src/common/leb128.h @@ -30,7 +30,6 @@ #include "datatypes.h" -#include "packed.h" @@ -58,17 +57,23 @@ bool read_uleb128(uleb128_t *, const bin_t *, phys_t *, phys_t); /* Lit un nombre signé encodé au format LEB128. */ bool read_leb128(leb128_t *, const bin_t *, phys_t *, phys_t); +/* Charge un nombre non signé encodé au format LEB128. */ +bool load_uleb128(uleb128_t *, int); + +/* Sauvegarde un nombre non signé encodé au format LEB128. */ +bool store_uleb128(const uleb128_t *, int); + /* Encode un nombre non signé encodé au format LEB128. */ -bool pack_uleb128(const uleb128_t *, packed_buffer_t *); +void *pack_uleb128(const uleb128_t *, size_t *); /* Encode un nombre signé encodé au format LEB128. */ -bool pack_leb128(const leb128_t *, packed_buffer_t *); +void *pack_leb128(const leb128_t *, size_t *); -/* Décode un nombre non signé encodé au format LEB128. */ -bool unpack_uleb128(uleb128_t *, packed_buffer_t *); +/* Encode un nombre non signé encodé au format LEB128. */ +bool unpack_uleb128(uleb128_t *, const void **, const void *); /* Décode un nombre signé encodé au format LEB128. */ -bool unpack_leb128(leb128_t *, packed_buffer_t *); +bool unpack_leb128(leb128_t *, const void **, const void *); diff --git a/src/common/pathname.c b/src/common/pathname.c index dd3ec9e..81dc1e3 100644 --- a/src/common/pathname.c +++ b/src/common/pathname.c @@ -33,13 +33,13 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <gio/gio.h> #include <sys/stat.h> #include "extstr.h" #include "io.h" #include "../core/logs.h" -//#include "../core/params.h" // TODO : config @@ -316,16 +316,19 @@ int ensure_path_exists(const char *path) * Remarques : - * * * ******************************************************************************/ -#if 0 // TODO + int make_tmp_file(const char *prefix, const char *suffix, char **filename) { int result; /* Flux ou code à retourner */ - const char *tmpdir; /* Répertoire d'accueil */ - bool status; /* Bilan d'un consultation */ + GSettings *settings; /* Configuration sollicitée */ + gchar *tmpdir; /* Répertoire d'accueil */ size_t slen; /* Taille du suffixe */ - status = g_generic_config_get_value(get_main_configuration(), MPK_TMPDIR, &tmpdir); - if (!status) return -1; + /* Récupération d'un répertoire de travail temporaire */ + + settings = g_settings_new("re.chrysalide.framework.paths"); + + tmpdir = g_settings_get_string(settings, "tmp-work-dir"); slen = strlen(suffix); @@ -334,6 +337,12 @@ int make_tmp_file(const char *prefix, const char *suffix, char **filename) else asprintf(filename, "%s" G_DIR_SEPARATOR_S "%s-%d.XXXXXX", tmpdir, prefix, getpid()); + g_free(tmpdir); + + g_clear_object(&settings); + + /* Mise en place d'un fichier temporaire */ + result = ensure_path_exists(*filename); if (result == 0) @@ -356,7 +365,7 @@ int make_tmp_file(const char *prefix, const char *suffix, char **filename) return result; } -#endif + /****************************************************************************** * * diff --git a/src/common/pathname.h b/src/common/pathname.h index 1b6624c..104833b 100644 --- a/src/common/pathname.h +++ b/src/common/pathname.h @@ -42,9 +42,7 @@ bool mkpath(const char *); int ensure_path_exists(const char *); /* Met en place un fichier temporaire. */ -#if 0 // TODO int make_tmp_file(const char *, const char *, char **); -#endif /* Copie un fichier. */ bool copy_file(const char *, const char *); diff --git a/src/common/sort.h b/src/common/sort.h index 39a6f33..27e3739 100644 --- a/src/common/sort.h +++ b/src/common/sort.h @@ -37,6 +37,9 @@ int sort_boolean(bool, bool); /* Compare une valeur avec une autre. */ int sort_unsigned_long(unsigned long, unsigned long); +#define sort_size(v1, v2) \ + sort_unsigned_long(v1, v2) + /* Compare une valeur avec une autre. */ int sort_signed_long_long(signed long long, signed long long); diff --git a/src/common/szbin.h b/src/common/szbin.h new file mode 100644 index 0000000..8524ae3 --- /dev/null +++ b/src/common/szbin.h @@ -0,0 +1,282 @@ + +/* 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 <assert.h> +#include <malloc.h> +#include <string.h> + + +#include "datatypes.h" +#include "io.h" +#include "leb128.h" +#include "sort.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 */ + + const bin_t *static_bin_data; /* Données brutes non modifiées*/ + bin_t *bin_data; /* Données brutes */ + + }; + + 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 setup_sized_binary_from_static_string(sb, s) \ + do \ + { \ + (sb)->static_data = s; \ + (sb)->size = strlen(s) + 1; \ + } \ + while (0) + + +#define dup_into_sized_binary(sb, d, s) \ + do \ + { \ + size_t __size_once; \ + __size_once = s; \ + setup_sized_binary(sb, __size_once);\ + memcpy((sb)->data, d, __size_once); \ + } \ + while (0) + + +#define dup_sized_binary(dst, src) \ + dup_into_sized_binary((dst), (src)->static_data, (src)->size) + + +#define exit_sized_binary(sb) \ + do \ + { \ + if ((sb)->data != NULL) \ + { \ + free((sb)->data); \ + init_sized_binary(sb); \ + } \ + } \ + while (0) + + +#define resize_sized_binary(sb, s) \ + do \ + { \ + (sb)->size = s; \ + (sb)->data = realloc((sb)->data, \ + (sb)->size); \ + } \ + while (0) + + +#define add_to_sized_binary(sb, d, s) \ + do \ + { \ + size_t __old_size; \ + __old_size = (sb)->size; \ + (sb)->size += s; \ + (sb)->data = realloc((sb)->data, \ + (sb)->size); \ + memcpy((sb)->data + __old_size, \ + d, s); \ + } \ + while (0) + + +#define add_static_to_sized_binary(sb, d) \ + do \ + { \ + size_t __len; \ + __len = sizeof(d) - 1; \ + add_to_sized_binary(sb, d, __len); \ + } \ + while (0) + + +#define memcmp_sized_binary(s1, s2) \ + ({ \ + int __ret; \ + __ret = sort_size((s1)->size, (s2)->size); \ + if (__ret == 0) \ + __ret = memcmp((s1)->data, (s2)->data, (s1)->size); \ + __ret; \ + }) + + + +/** + * Conservations et rechargements. + */ + +#define load_sized_binary(sb, f) \ + ({ \ + uleb128_t __sz; \ + bool __ret; \ + __ret = load_uleb128(&__sz, f); \ + if (__ret) \ + { \ + setup_sized_binary(sb, __sz); \ + __ret = safe_read(f, (sb)->data, (sb)->size); \ + if (!__ret) \ + exit_sized_binary(sb); \ + } \ + __ret; \ + }) + + +#define load_sized_binary_as_string(sb, f) \ + ({ \ + uleb128_t __sz; \ + bool __ret; \ + __ret = load_uleb128(&__sz, f); \ + if (__ret) \ + { \ + setup_sized_binary(sb, __sz + 1); \ + __ret = safe_read(f, (sb)->data, __sz); \ + if (!__ret) \ + exit_sized_binary(sb); \ + (sb)->data[__sz] = '\0'; \ + } \ + __ret; \ + }) + + +#define store_sized_binary(sb, f) \ + ({ \ + bool __ret; \ + __ret = store_uleb128((const uleb128_t []){ (sb)->size }, f); \ + if (__ret) \ + __ret = safe_write(f, (sb)->static_data, (sb)->size); \ + __ret; \ + }) + + +#define store_sized_binary_as_string(sb, f) \ + ({ \ + bool __ret; \ + size_t __length; \ + assert((sb)->size >= 1); \ + __length = (sb)->size - 1; \ + assert((sb)->static_data[__length] == '\0'); \ + __ret = store_uleb128((const uleb128_t []){ __length }, f); \ + if (__ret) \ + __ret = safe_write(f, (sb)->static_data, __length); \ + __ret; \ + }) + + +#define unpack_sized_binary(sb, p, m) \ + ({ \ + uleb128_t __sz; \ + bool __ret; \ + __ret = unpack_uleb128(&__sz, p, m); \ + if (__ret) \ + { \ + setup_sized_binary(sb, __sz); \ + memcpy((sb)->data, *p, (sb)->size); \ + *((uint8_t **)p) += __sz; \ + } \ + __ret; \ + }) + + +#define unpack_sized_binary_as_string(sb, p, m) \ + ({ \ + uleb128_t __sz; \ + bool __ret; \ + __ret = unpack_uleb128(&__sz, p, m); \ + if (__ret) \ + { \ + setup_sized_binary(sb, __sz + 1); \ + memcpy((sb)->data, *p, __sz); \ + (sb)->data[__sz] = '\0'; \ + *((uint8_t **)p) += __sz; \ + } \ + __ret; \ + }) + + +#define pack_sized_binary(sb, l) \ + ({ \ + uint8_t *__result; \ + size_t __pos; \ + __result = pack_uleb128((const uleb128_t []){ (sb)->size }, l); \ + __pos = *(l); \ + *(l) += (sb)->size; \ + __result = realloc(__result, *(l) * sizeof(uint8_t)); \ + memcpy(&__result[__pos], (sb)->static_data, \ + ((sb)->size * sizeof(uint8_t))); \ + __result; \ + }) + + +#define pack_sized_binary_as_string(sb, l) \ + ({ \ + uint8_t *__result; \ + size_t __length; \ + size_t __pos; \ + assert((sb)->size >= 1); \ + __length = (sb)->size - 1; \ + assert((sb)->static_data[__length] == '\0'); \ + __result = pack_uleb128((const uleb128_t []){ __length }, l); \ + __pos = *(l); \ + *(l) += __length; \ + __result = realloc(__result, *(l) * sizeof(uint8_t)); \ + memcpy(&__result[__pos], (sb)->static_data, \ + (__length * sizeof(uint8_t))); \ + __result; \ + }) + + + +#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 *); |