/* Chrysalide - Outil d'analyse de fichiers binaires
* snapshot.c - encodage des informations utiles aux instantanés
*
* Copyright (C) 2019 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 .
*/
#include "snapshot.h"
#include
#include
#include "../../../core/logs.h"
/* ---------------------------------------------------------------------------------- */
/* IDENTIFIANTS DES INSTANTANES */
/* ---------------------------------------------------------------------------------- */
/******************************************************************************
* *
* Paramètres : id = identifiant d'instantané à initialiser. [OUT] *
* *
* Description : Construit un identifiant pour instantané de base de données. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool init_snapshot_id(snapshot_id_t *id)
{
bool result; /* Bilan à retourner */
int ret; /* Bilan d'une génération */
unsigned char rand[SNAP_ID_RAND_SZ]; /* Tirage aléatoire */
size_t i; /* Boucle de parcours */
static char *alphabet = "0123456789abcdef";
ret = RAND_bytes(rand, SNAP_ID_RAND_SZ);
if (ret != 1)
{
LOG_ERROR_OPENSSL;
result = false;
}
else
{
for (i = 0; i < SNAP_ID_RAND_SZ; i++)
{
id->name[i * 2 + 0] = alphabet[rand[i] & 0xf];
id->name[i * 2 + 1] = alphabet[(rand[i] >> 4) & 0xf];
}
id->name[i * 2] = '\0';
result = true;
}
return result;
}
/******************************************************************************
* *
* Paramètres : id = identifiant d'instantané à initialiser. [OUT] *
* text = source de données pour la constitution. *
* *
* Description : Construit un identifiant pour instantané de base de données. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool init_snapshot_id_from_text(snapshot_id_t *id, const char *text)
{
bool result; /* Bilan à retourner */
result = (strlen(text) == (SNAP_ID_HEX_SZ - 1));
if (result)
strcpy(id->name, text);
return result;
}
/******************************************************************************
* *
* Paramètres : dest = destination de la copie d'indentifiant. [OUT] *
* src = source de l'identifiant à copier. *
* *
* Description : Effectue une copie d'identifiant d'instantané. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
void copy_snapshot_id(snapshot_id_t *dest, const snapshot_id_t *src)
{
memcpy(dest->name, src->name, SNAP_ID_HEX_SZ);
}
/******************************************************************************
* *
* Paramètres : id1 = premier identifiant à comparer. *
* id2 = second identifiant à comparer. *
* *
* Description : Effectue la comparaison entre deux identifiants. *
* *
* Retour : Résultat de la comparaison : -1, 0 ou 1. *
* *
* Remarques : - *
* *
******************************************************************************/
int cmp_snapshot_id(const snapshot_id_t *id1, const snapshot_id_t *id2)
{
int result; /* Bilan à retourner */
result = memcmp(id1->name, id2->name, SNAP_ID_HEX_SZ);
return result;
}
/******************************************************************************
* *
* Paramètres : id = informations à constituer. [OUT] *
* pbuf = paquet de données où venir puiser les infos. *
* *
* Description : Importe la définition d'un identifiant d'instantané. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool unpack_snapshot_id(snapshot_id_t *id, packed_buffer *pbuf)
{
bool result; /* Bilan à retourner */
result = extract_packed_buffer(pbuf, id->name, SNAP_ID_HEX_SZ, false);
return result;
}
/******************************************************************************
* *
* Paramètres : id = informations à sauvegarer. *
* pbuf = paquet de données où venir inscrire les infos. *
* *
* Description : Exporte la définition d'un identifiant d'instantané. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool pack_snapshot_id(const snapshot_id_t *id, packed_buffer *pbuf)
{
bool result; /* Bilan à retourner */
result = extend_packed_buffer(pbuf, id->name, SNAP_ID_HEX_SZ, false);
return result;
}
/* ---------------------------------------------------------------------------------- */
/* PROPRIETES DES INSTANTANES */
/* ---------------------------------------------------------------------------------- */