summaryrefslogtreecommitdiff
path: root/src/analysis/db/misc/rlestr.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2014-08-18 21:55:24 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2014-08-18 21:55:24 (GMT)
commita0a7b6c1e05c78ae433f353d15e3366107b67d03 (patch)
treebca0b187778cf016c6131bfc982b08c67a38442b /src/analysis/db/misc/rlestr.c
parent161c0f8ab227af5033b1b6456607b9b9c3bc60df (diff)
Inserted storages and collections into loaded binaries (first steps).
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@389 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/analysis/db/misc/rlestr.c')
-rw-r--r--src/analysis/db/misc/rlestr.c345
1 files changed, 345 insertions, 0 deletions
diff --git a/src/analysis/db/misc/rlestr.c b/src/analysis/db/misc/rlestr.c
new file mode 100644
index 0000000..d75e7ab
--- /dev/null
+++ b/src/analysis/db/misc/rlestr.c
@@ -0,0 +1,345 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * rlestr.c - encodage par plage unique d'une chaîne de caractères
+ *
+ * Copyright (C) 2014 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA 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.
+ *
+ * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "rlestr.h"
+
+
+#include <endian.h>
+#include <malloc.h>
+#include <string.h>
+
+
+#include "../../../common/io.h"
+
+
+
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <time.h>
+#include <unistd.h>
+
+
+
+
+
+
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : str = représentation de chaîne à traiter. *
+* data = données à conserver en mémoire. *
+* *
+* Description : Définit une représentation de chaîne de caractères. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void init_rle_string(rle_string *str, const char *data)
+{
+ if (data != NULL)
+ {
+ str->data = strdup(data);
+ str->length = strlen(data);
+ }
+ else
+ {
+ str->data = NULL;
+ str->length = 0;
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : str = représentation de chaîne à traiter. *
+* data = données à conserver en mémoire. *
+* *
+* Description : Constitue une représentation de chaîne de caractères. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void set_rle_string(rle_string *str, const char *data)
+{
+ if (str->data != NULL)
+ unset_rle_string(str);
+
+ if (data != NULL)
+ {
+ str->data = strdup(data);
+ str->length = strlen(data);
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : str = représentation de chaîne à traiter. *
+* *
+* Description : Libère la mémoire associée à la représentation. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void unset_rle_string(rle_string *str)
+{
+ if (str->data != NULL)
+ {
+ free(str->data);
+ str->data = NULL;
+
+ str->length = 0;
+
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : s1 = première chaîne à comparer. *
+* s2 = seconde chaîne à comparer. *
+* *
+* Description : Effectue la comparaison entre deux chaînes de caractères. *
+* *
+* Retour : Résultat de la comparaison : -1, 0 ou 1. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+int cmp_rle_string(const rle_string *s1, const rle_string *s2)
+{
+ int result; /* Bilan à retourner */
+
+ if (s1->length < s2->length)
+ result = -1;
+
+ else if (s1->length > s2->length)
+ result = 1;
+
+ else
+ {
+ if (s1->data == NULL && s2->data == NULL)
+ result = 0;
+
+ else if (s1->data != NULL && s2->data == NULL)
+ result = 1;
+
+ else if (s1->data == NULL && s2->data != NULL)
+ result = -1;
+
+ else
+ result = strcmp(s1->data, s2->data);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : str = informations à constituer. [OUT] *
+* fd = flux ouvert en lecture pour l'importation. *
+* *
+* Description : Importe la définition d'une chaîne de caractères. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool load_rle_string(rle_string *str, int fd)
+{
+#if 0
+ uint32_t val32; /* Valeur sur 32 bits */
+ ssize_t got; /* Quantité de données reçues */
+
+ got = safe_recv(fd, &val32, sizeof(uint32_t), MSG_WAITALL);
+ if (got != sizeof(uint32_t)) return false;
+
+ str->length = le32toh(val32);
+
+ if (str->length > 0)
+ {
+ str->data = (char *)malloc(str->length + 1);
+
+ got = safe_recv(fd, str->data, str->length + 1, MSG_WAITALL);
+ if (got != (str->length + 1))
+ {
+ unset_rle_string(str);
+ return false;
+ }
+
+ str->data[str->length] = '\0';
+
+ }
+#endif
+ return true;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : str = informations à sauvegarer. *
+* fd = flux ouvert en écriture pour l'exportation. *
+* *
+* Description : Exporte la définition d'une chaîne de caractères. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool store_rle_string(const rle_string *str, int fd)
+{
+#if 0
+ ssize_t got; /* Quantité de données reçues */
+
+ got = safe_send(fd, (uint32_t []) { htole32(str->length) }, sizeof(uint32_t), MSG_WAITALL);
+ if (got != sizeof(uint32_t)) return false;
+
+ if (str->length > 0)
+ {
+ got = safe_send(fd, str->data, str->length + 1, MSG_WAITALL);
+ if (got != (str->length + 1)) return false;
+ }
+#endif
+ return true;
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : str = informations à constituer. [OUT] *
+* fd = flux ouvert en lecture pour l'importation. *
+* flags = éventuelles options de réception supplémentaires. *
+* *
+* Description : Importe la définition d'une chaîne de caractères. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool recv_rle_string(rle_string *str, int fd, int flags)
+{
+ uint32_t val32; /* Valeur sur 32 bits */
+ bool status; /* Bilan d'une opération */
+
+ str->data = NULL;
+ str->length = 0;
+
+ status = safe_recv(fd, &val32, sizeof(uint32_t), flags);
+ if (!status) return false;
+
+ str->length = be32toh(val32);
+
+ if (str->length > 0)
+ {
+ str->data = (char *)malloc(str->length + 1);
+
+ status = safe_recv(fd, str->data, str->length + 1, flags);
+ if (!status)
+ {
+ unset_rle_string(str);
+ return false;
+ }
+
+ str->data[str->length] = '\0';
+
+ }
+
+ return true;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : str = informations à sauvegarer. *
+* fd = flux ouvert en écriture pour l'exportation. *
+* flags = éventuelles options d'envoi supplémentaires. *
+* *
+* Description : Exporte la définition d'une chaîne de caractères. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool send_rle_string(const rle_string *str, int fd, int flags)
+{
+ bool status; /* Bilan d'une opération */
+
+ status = safe_send(fd, (uint32_t []) { htobe32(str->length) }, sizeof(uint32_t), flags);
+ if (!status) return false;
+
+ if (str->length > 0)
+ {
+ status = safe_send(fd, str->data, str->length + 1, flags);
+ if (!status) return false;
+ }
+
+ return true;
+
+}