/* 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 . */ #include "rlestr.h" #include #include #include #include "../../../common/io.h" #include #include #include #include #include /****************************************************************************** * * * 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), MSG_MORE | 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; }