/* Chrysalide - Outil d'analyse de fichiers binaires
 * bookmark.h - prototypes pour la gestion des signets au sein d'un binaire
 *
 * 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 "bookmark.h"


#include <stdio.h>
#include <sys/socket.h>


#include <i18n.h>


#include "../collection-int.h"
#include "../item-int.h"



/* --------------------- ELABORATION D'UN ELEMENT DE COLLECTION --------------------- */


/* Signet à l'intérieur d'une zone de texte (instance) */
struct _GDbBookmark
{
    GDbItem parent;                         /* A laisser en premier        */

    vmpa2t addr;                            /* Adresse du signet           */
    rle_string comment;                     /* Eventuel commentaire associé*/

    bool prev_state;                        /* Drapeau déjà présent avant ?*/

};

/* Signet à l'intérieur d'une zone de texte (classe) */
struct _GDbBookmarkClass
{
    GDbItemClass parent;                    /* A laisser en premier        */

};


/* Initialise la classe des signets dans une zone de texte. */
static void g_db_bookmark_class_init(GDbBookmarkClass *);

/* Initialise un signet dans une zone de texte. */
static void g_db_bookmark_init(GDbBookmark *);

/* Supprime toutes les références externes. */
static void g_db_bookmark_dispose(GDbBookmark *);

/* Procède à la libération totale de la mémoire. */
static void g_db_bookmark_finalize(GDbBookmark *);

/* Effectue la comparaison entre deux signets de collection. */
static gint g_db_bookmark_cmp(GDbBookmark *, GDbBookmark *, bool);

/* Importe la définition d'un signet dans un flux réseau. */
static bool g_db_bookmark_recv_from_fd(GDbBookmark *, int, int);

/* Exporte la définition d'un signet dans un flux réseau. */
static bool g_db_bookmark_send_to_fd(const GDbBookmark *, int, int);

/* Construit la description humaine d'un signet sur un tampon. */
static void g_db_bookmark_build_label(GDbBookmark *);

/* Exécute un signet sur un tampon de binaire chargé. */
static bool g_db_bookmark_run(GDbBookmark *, GLoadedBinary *, bool *, bool);

/* Applique un signet sur un tampon de binaire chargé. */
static bool g_db_bookmark_apply(GDbBookmark *, GLoadedBinary *);

/* Annule un signet sur un tampon de binaire chargé. */
static bool g_db_bookmark_cancel(GDbBookmark *, GLoadedBinary *);

/* Constitue les champs destinés à une insertion / modification. */
static bool g_db_bookmark_prepare_db_statement(const GDbBookmark *, bound_value **, size_t *);

/* Charge les valeurs utiles pour un signet. */
static bool g_db_bookmark_load(GDbBookmark *, const bound_value *, size_t);



/* ---------------------- DEFINITION DE LA COLLECTION ASSOCIEE ---------------------- */


/* Collection dédiée aux signets (instance) */
struct _GBookmarkCollection
{
    GDbCollection parent;                   /* A laisser en premier        */

};

/* Collection dédiée aux signets (classe) */
struct _GBookmarkCollectionClass
{
    GDbCollectionClass parent;              /* A laisser en premier        */

};


/* Initialise la classe des signets dans une zone de texte. */
static void g_bookmark_collection_class_init(GBookmarkCollectionClass *);

/* Initialise un signet dans une zone de texte. */
static void g_bookmark_collection_init(GBookmarkCollection *);

/* Supprime toutes les références externes. */
static void g_bookmark_collection_dispose(GBookmarkCollection *);

/* Procède à la libération totale de la mémoire. */
static void g_bookmark_collection_finalize(GBookmarkCollection *);

/* Crée la table des signets dans une base de données. */
static bool g_bookmark_collection_create_db_table(const GBookmarkCollection *, sqlite3 *);

/* Décrit les colonnes utiles à un chargement de données. */
static bool g_bookmark_collection_setup_load(GBookmarkCollection *, bound_value **, size_t *);

/* Détermine si un élément est déjà présent ou non. */
static GDbItem *g_bookmark_collection_has_key(GBookmarkCollection *, va_list *);



/* ---------------------------------------------------------------------------------- */
/*                       ELABORATION D'UN ELEMENT DE COLLECTION                       */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour un signet à l'intérieur d'une zone de texte. */
G_DEFINE_TYPE(GDbBookmark, g_db_bookmark, G_TYPE_DB_ITEM);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des signets dans une zone de texte.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_bookmark_class_init(GDbBookmarkClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GDbItemClass *item;                     /* Encore une autre vision...  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_db_bookmark_dispose;
    object->finalize = (GObjectFinalizeFunc)g_db_bookmark_finalize;

    item = G_DB_ITEM_CLASS(klass);

    item->cmp = (cmp_db_item_fc)g_db_bookmark_cmp;

    item->recv = (recv_db_item_fc)g_db_bookmark_recv_from_fd;
    item->send = (send_db_item_fc)g_db_bookmark_send_to_fd;

    item->build_label = (build_item_label_fc)g_db_bookmark_build_label;
    item->apply = (run_item_fc)g_db_bookmark_apply;
    item->cancel = (run_item_fc)g_db_bookmark_cancel;

    item->prepare_stmt = (prepare_db_statement)g_db_bookmark_prepare_db_statement;
    item->load = (load_db_item_fc)g_db_bookmark_load;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = instance à initialiser.                           *
*                                                                             *
*  Description : Initialise un signet dans une zone de texte.                 *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_bookmark_init(GDbBookmark *bookmark)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = instance d'objet GLib à traiter.                  *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_bookmark_dispose(GDbBookmark *bookmark)
{
    G_OBJECT_CLASS(g_db_bookmark_parent_class)->dispose(G_OBJECT(bookmark));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = instance d'objet GLib à traiter.                  *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_bookmark_finalize(GDbBookmark *bookmark)
{
    exit_rle_string(&bookmark->comment);

    G_OBJECT_CLASS(g_db_bookmark_parent_class)->finalize(G_OBJECT(bookmark));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : addr    = adresse inamovible localisant une position donnée. *
*                comment = commentaire construit ou NULL.                     *
*                                                                             *
*  Description : Crée une définition d'un signet dans une zone de texte.      *
*                                                                             *
*  Retour      : Signet mis en place ou NULL en cas d'erreur.                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDbBookmark *g_db_bookmark_new(const vmpa2t *addr, const char *comment)
{
    GDbBookmark *result;                    /* Instance à retourner        */

    result = g_object_new(G_TYPE_DB_BOOKMARK, NULL);

    copy_vmpa(&result->addr, addr);

    g_db_bookmark_set_comment(result, comment);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : a    = premier élément à analyser.                           *
*                b    = second élément à analyser.                            *
*                with = précise les horodatages à prendre en compte.          *
*                                                                             *
*  Description : Effectue la comparaison entre deux signets de collection.    *
*                                                                             *
*  Retour      : Bilan de la comparaison : -1, 0 ou 1.                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static gint g_db_bookmark_cmp(GDbBookmark *a, GDbBookmark *b, bool with)
{
    gint result;                            /* Bilan de la comparaison     */

    result = cmp_vmpa(&a->addr, &b->addr);

    if (result == 0)
        result = cmp_rle_string(&a->comment, &b->comment);

    if (result == 0)
        result = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->cmp(G_DB_ITEM(a), G_DB_ITEM(b), with);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = signet dont les informations sont à charger. [OUT]*
*                fd       = flux ouvert en lecture pour l'importation.        *
*                flags    = éventuelles options d'envoi supplémentaires.      *
*                                                                             *
*  Description : Importe la définition d'un signet dans un flux réseau.       *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_recv_from_fd(GDbBookmark *bookmark, int fd, int flags)
{
    bool status;                            /* Bilan d'opération initiale  */

    status = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->recv(G_DB_ITEM(bookmark), fd, flags);
    if (!status) return false;

    if (!recv_vmpa(&bookmark->addr, fd, flags))
        return false;

    if (!recv_rle_string(&bookmark->comment, fd, flags))
        return false;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = informations à sauvegarder.                       *
*                fd       = flux ouvert en écriture pour l'exportation.       *
*                flags    = éventuelles options d'envoi supplémentaires.      *
*                                                                             *
*  Description : Exporte la définition d'un signet dans un flux réseau.       *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_send_to_fd(const GDbBookmark *bookmark, int fd, int flags)
{
    bool status;                            /* Bilan d'opération initiale  */

    status = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->send(G_DB_ITEM(bookmark), fd, MSG_MORE | flags);
    if (!status) return false;

    if (!send_vmpa(&bookmark->addr, fd, MSG_MORE | flags))
        return false;

    if (!send_rle_string(&bookmark->comment, fd, flags))
        return false;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = signet à manipuler.                               *
*                                                                             *
*  Description : Construit la description humaine d'un signet sur un tampon.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_bookmark_build_label(GDbBookmark *bookmark)
{
    asprintf(&G_DB_ITEM(bookmark)->label, _("Bookmark \"%s\""), get_rle_string(&bookmark->comment));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = signet à manipuler.                               *
*                binary   = binaire chargé en mémoire à modifier.             *
*                prev     = état précédent de la présence du drapeau. [OUT]   *
*                set      = précision quant au nouvel état du drapeau.        *
*                                                                             *
*  Description : Exécute un signet sur un tampon de binaire chargé.           *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_run(GDbBookmark *bookmark, GLoadedBinary *binary, bool *prev, bool set)
{
    bool result;                            /* Bilan à faire remonter      */
    GCodeBuffer *buffer;                    /* Tampon de lignes à traiter  */
    GBufferLine *line;                      /* Ligne de tampon à marquer   */

    result = true;

    buffer = g_loaded_binary_get_disassembled_buffer(binary);

    line = g_code_buffer_find_line_by_addr(buffer, &bookmark->addr, BLF_HAS_CODE, NULL);
    if (line == NULL)
    {
        result = false;
        goto exit;
    }

    *prev = g_buffer_line_get_flags(line) & BLF_BOOKMARK;

    if (set)
        g_buffer_line_add_flag(line, BLF_BOOKMARK);

    else
        g_buffer_line_remove_flag(line, BLF_BOOKMARK);

    g_object_unref(G_OBJECT(line));

 exit:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = signet à manipuler.                               *
*                binary   = binaire chargé en mémoire à modifier.             *
*                                                                             *
*  Description : Applique un signet sur un tampon de binaire chargé.          *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_apply(GDbBookmark *bookmark, GLoadedBinary *binary)
{
    bool result;                            /* Bilan à faire remonter      */

    result = g_db_bookmark_run(bookmark, binary, &bookmark->prev_state, true);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = signet à manipuler.                               *
*                binary   = binaire chargé en mémoire à modifier.             *
*                                                                             *
*  Description : Annule un signet sur un tampon de binaire chargé.            *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_cancel(GDbBookmark *bookmark, GLoadedBinary *binary)
{
    bool result;                            /* Bilan à faire remonter      */

    if (!bookmark->prev_state)
        result = g_db_bookmark_run(bookmark, binary, (bool []) { 0 }, false);
    else
        result = true;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = base d'éléments sur laquelle s'appuyer.           *
*                values   = couples de champs et de valeurs à lier. [OUT]     *
*                count    = nombre de ces couples. [OUT]                      *
*                                                                             *
*  Description : Constitue les champs destinés à une insertion / modification.*
*                                                                             *
*  Retour      : Etat du besoin en sauvegarde.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_prepare_db_statement(const GDbBookmark *bookmark, bound_value **values, size_t *count)
{
    bool status;                            /* Bilan d'opération initiale  */

    status = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->prepare_stmt(G_DB_ITEM(bookmark), values, count);
    if (!status) return false;

    status = prepare_vmpa_db_statement(&bookmark->addr, values, count);
    if (!status) return false;

    status &= prepare_db_statement_for_rle_string(&bookmark->comment, "comment", values, count);
    if (!status) return false;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = bascule d'affichage à charger depuis les réponses.*
*                values   = tableau d'éléments à consulter.                   *
*                count    = nombre de descriptions renseignées.               *
*                                                                             *
*  Description : Charge les valeurs utiles pour un signet.                    *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_db_bookmark_load(GDbBookmark *bookmark, const bound_value *values, size_t count)
{
    bool result;                            /* Bilan à faire remonter      */

    result = G_DB_ITEM_CLASS(g_db_bookmark_parent_class)->load(G_DB_ITEM(bookmark), values, count);

    result &= load_vmpa(&bookmark->addr, values, count);

    result &= load_rle_string(&bookmark->comment, "comment", values, count);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = informations à consulter.                         *
*                                                                             *
*  Description : Fournit l'adresse associée à un signet.                      *
*                                                                             *
*  Retour      : Adresse mémoire.                                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const vmpa2t *g_db_bookmark_get_address(GDbBookmark *bookmark)
{
    return &bookmark->addr;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = informations à consulter.                         *
*                                                                             *
*  Description : Fournit le commentaire associé à un signet.                  *
*                                                                             *
*  Retour      : Commentaire existant ou NULL.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *g_db_bookmark_get_comment(const GDbBookmark *bookmark)
{
    return get_rle_string(&bookmark->comment);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bookmark = informations à consulter.                         *
*                comment  = commentaire construit ou NULL.                    *
*                                                                             *
*  Description : Définit le commentaire associé à un signet.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_db_bookmark_set_comment(GDbBookmark *bookmark, const char *comment)
{
    set_rle_string(&bookmark->comment, comment);

}



/* ---------------------------------------------------------------------------------- */
/*                        DEFINITION DE LA COLLECTION ASSOCIEE                        */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour une collection de signets. */
G_DEFINE_TYPE(GBookmarkCollection, g_bookmark_collection, G_TYPE_DB_COLLECTION);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des signets dans une zone de texte.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_bookmark_collection_class_init(GBookmarkCollectionClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GDbCollectionClass *collec;             /* Encore une autre vision...  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_bookmark_collection_dispose;
    object->finalize = (GObjectFinalizeFunc)g_bookmark_collection_finalize;

    collec = G_DB_COLLECTION_CLASS(klass);

    collec->create_table = (collec_create_db_table_fc)g_bookmark_collection_create_db_table;
    collec->setup_load = (collec_setup_load_fc)g_bookmark_collection_setup_load;
    collec->has_key = (collec_has_key_fc)g_bookmark_collection_has_key;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : collec = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise un signet dans une zone de texte.                 *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_bookmark_collection_init(GBookmarkCollection *collec)
{
    G_DB_COLLECTION(collec)->featuring = DBF_BOOKMARKS;
    G_DB_COLLECTION(collec)->type = G_TYPE_DB_BOOKMARK;
    G_DB_COLLECTION(collec)->name = "Bookmarks";

}


/******************************************************************************
*                                                                             *
*  Paramètres  : collec = instance d'objet GLib à traiter.                    *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_bookmark_collection_dispose(GBookmarkCollection *collec)
{
    G_OBJECT_CLASS(g_bookmark_collection_parent_class)->dispose(G_OBJECT(collec));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : collec = instance d'objet GLib à traiter.                    *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_bookmark_collection_finalize(GBookmarkCollection *collec)
{
    G_OBJECT_CLASS(g_bookmark_collection_parent_class)->finalize(G_OBJECT(collec));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée une collection dédiée aux signets.                      *
*                                                                             *
*  Retour      : Collection mise en place.                                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBookmarkCollection *g_bookmark_collection_new(void)
{
    GBookmarkCollection *result;            /* Instance à retourner        */

    result = g_object_new(G_TYPE_BM_COLLECTION, NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : collec = ensemble d'éléments spectateur des opérations.      *
*                db     = accès à la base de données.                         *
*                                                                             *
*  Description : Crée la table des signets dans une base de données.          *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_bookmark_collection_create_db_table(const GBookmarkCollection *collec, sqlite3 *db)
{
    char *sql;                              /* Requête à exécuter          */
    int ret;                                /* Bilan de la création        */
    char *msg;                              /* Message d'erreur            */

    sql = "CREATE TABLE Bookmarks ("            \
             SQLITE_DB_ITEM_CREATE ", "         \
             SQLITE_VMPA_CREATE ", "            \
             SQLITE_RLESTR_CREATE("comment")    \
          ");";

    ret = sqlite3_exec(db, sql, NULL, NULL, &msg);
    if (ret != SQLITE_OK)
    {
        fprintf(stderr, "sqlite3_exec(): %s\n", msg);
        sqlite3_free(msg);
    }

    return (ret == SQLITE_OK);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : collec = ensemble d'éléments à consulter.                    *
*                values = tableau d'éléments à compléter. [OUT]               *
*                count  = nombre de descriptions renseignées. [OUT]           *
*                                                                             *
*  Description : Décrit les colonnes utiles à un chargement de données.       *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_bookmark_collection_setup_load(GBookmarkCollection *collec, bound_value **values, size_t *count)
{
    bool status;                            /* Bilan d'une préparation     */

    status = G_DB_COLLECTION_CLASS(g_bookmark_collection_parent_class)->setup_load(G_DB_COLLECTION(collec), \
                                                                                   values, count);
    if (!status) return false;

    if (!setup_load_for_vmpa(NULL, values, count))
        return false;

    if (!setup_load_of_rle_string(NULL, "comment", values, count))
        return false;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : collec = ensemble d'éléments à consulter.                    *
*                ap     = clef identifiant de manière unique un élément.      *
*                                                                             *
*  Description : Détermine si un élément est déjà présent ou non.             *
*                                                                             *
*  Retour      : Elément trouvé ou NULL si aucun.                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GDbItem *g_bookmark_collection_has_key(GBookmarkCollection *collec, va_list *ap)
{
    GDbItem *result;                        /* Bilan à retourner           */
    vmpa2t *ref;                            /* Adresse de référence        */
    GList *items;                           /* Eléments déjà en place      */
    GList *iter;                            /* Boucle de parcours          */
    GDbBookmark *bm;                        /* Signet à consulter          */

    result = NULL;

    ref = va_arg(ap, vmpa2t *);

    items = g_db_collection_list_items(G_DB_COLLECTION(collec));

    for (iter = g_list_first(items); iter != NULL && result == NULL; iter = g_list_next(iter))
    {
        bm = G_DB_BOOKMARK(iter->data);

        if (cmp_vmpa(&bm->addr, ref) != 0)

            /**
             * Un verrou est sensé être posé, donc il n'y a pas besoin
             * de toucher au décompte des références car l'élément trouvé
             * ne peut pas être supprimé.
             */
            result = G_DB_ITEM(bm);

    }

    return result;

}