From 41dcf8a45d61108fef1f545ecdee5d79d8135089 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 25 Aug 2024 18:55:24 +0200
Subject: Restore an improved cURL support.

---
 src/Makefile.am        |  6 ++++
 src/common/Makefile.am | 14 ++++++++
 src/common/curl.c      | 24 +++++++-------
 src/common/curl.h      | 14 +++-----
 src/common/szbin.h     | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/common/szstr.h     |  5 ++-
 6 files changed, 127 insertions(+), 24 deletions(-)
 create mode 100644 src/common/szbin.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 9355d4c..4e8c07c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -87,6 +87,12 @@ libchrysacore4_la_LIBADD =					\
 libchrysacore4_la_LDFLAGS =					\
 	$(TOOLKIT_LIBS) $(LIBSSL_LIBS)
 
+if BUILD_CURL_SUPPORT
+
+libchrysacore4_la_LDFLAGS += $(LIBCURL_LIBS)
+
+endif
+
 
 #--- libchrysacoreui
 
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 83c754c..24929e7 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -62,10 +62,24 @@ libcommon4_la_SOURCES =						\
 	pathname.h pathname.c					\
 	shuffle.h shuffle.c						\
 	sort.h sort.c							\
+	szbin.h									\
 	xdg.h xdg.c
 
+if BUILD_CURL_SUPPORT
+
+libcommon4_la_SOURCES +=					\
+	curl.h curl.c
+
+endif
+
 libcommon4_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBSSL_CFLAGS)
 
+if BUILD_CURL_SUPPORT
+
+libcommon4_la_CFLAGS += $(LIBCURL_CFLAGS)
+
+endif
+
 
 devdir = $(includedir)/chrysalide/$(subdir:src/%=core/%)
 
diff --git a/src/common/curl.c b/src/common/curl.c
index 573180f..2cf7f3a 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.
  *
@@ -30,7 +30,7 @@
 
 
 /* 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 *);
 
 
 
@@ -49,19 +49,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 +85,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,8 +95,7 @@ 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;
@@ -165,7 +164,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,8 +174,7 @@ 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;
@@ -205,7 +203,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/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)                                            \
     ({                                                              \
-- 
cgit v0.11.2-87-g4458