/* Chrysalide - Outil d'analyse de fichiers binaires
 * modpath.c - récupération des combinaisons de modification de motifs
 *
 * Copyright (C) 2023 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "modpath.h"


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


#include "../exprs/handler.h"
#include "../exprs/literal.h"
#include "../exprs/set.h"
#include "../matches/bytes.h"



/* ---------------------- INTRODUCTION D'UNE NOUVELLE FONCTION ---------------------- */


/* Initialise la classe des énumérations de formules de motifs. */
static void g_scan_modpath_function_class_init(GScanModpathFunctionClass *);

/* Initialise une instance d'énumération de formules de motifs. */
static void g_scan_modpath_function_init(GScanModpathFunction *);

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

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



/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */


/* Indique le nom associé à une expression d'évaluation. */
static char *g_scan_modpath_function_get_name(const GScanModpathFunction *);

/* Réduit une expression à une forme plus simple. */
static bool g_scan_modpath_function_run_call(GScanModpathFunction *, GScanExpression **, size_t, GScanContext *, GScanScope *, GObject **);



/* ---------------------------------------------------------------------------------- */
/*                        INTRODUCTION D'UNE NOUVELLE FONCTION                        */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour une énumération de formules créant des motifs. */
G_DEFINE_TYPE(GScanModpathFunction, g_scan_modpath_function, G_TYPE_SCAN_REGISTERED_ITEM);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des énumérations de formules de motifs. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_scan_modpath_function_class_init(GScanModpathFunctionClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GScanRegisteredItemClass *registered;   /* Version de classe parente   */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_scan_modpath_function_dispose;
    object->finalize = (GObjectFinalizeFunc)g_scan_modpath_function_finalize;

    registered = G_SCAN_REGISTERED_ITEM_CLASS(klass);

    registered->get_name = (get_registered_item_name_fc)g_scan_modpath_function_get_name;
    registered->run_call = (run_registered_item_call_fc)g_scan_modpath_function_run_call;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : func = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise une instance d'énumération de formules de motifs. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_scan_modpath_function_init(GScanModpathFunction *func)
{

}


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

static void g_scan_modpath_function_dispose(GScanModpathFunction *func)
{
    G_OBJECT_CLASS(g_scan_modpath_function_parent_class)->dispose(G_OBJECT(func));

}


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

static void g_scan_modpath_function_finalize(GScanModpathFunction *func)
{
    G_OBJECT_CLASS(g_scan_modpath_function_parent_class)->finalize(G_OBJECT(func));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Constitue une fonction d'énumération des formules de motifs. *
*                                                                             *
*  Retour      : Fonction mise en place.                                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GScanRegisteredItem *g_scan_modpath_function_new(void)
{
    GScanRegisteredItem *result;            /* Structure à retourner       */

    result = g_object_new(G_TYPE_SCAN_MODPATH_FUNCTION, NULL);

    return result;

}



/* ---------------------------------------------------------------------------------- */
/*                       IMPLEMENTATION DES FONCTIONS DE CLASSE                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : item = élément d'appel à consulter.                          *
*                                                                             *
*  Description : Indique le nom associé à une expression d'évaluation.        *
*                                                                             *
*  Retour      : Désignation humaine de l'expression d'évaluation.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static char *g_scan_modpath_function_get_name(const GScanModpathFunction *item)
{
    char *result;                           /* Désignation à retourner     */

    result = strdup("modpath");

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item  = élément d'appel à consulter.                         *
*                args  = liste d'éventuels arguments fournis.                 *
*                count = taille de cette liste.                               *
*                ctx   = contexte de suivi de l'analyse courante.             *
*                scope = portée courante des variables locales.               *
*                out   = zone d'enregistrement de la résolution opérée. [OUT] *
*                                                                             *
*  Description : Réduit une expression à une forme plus simple.               *
*                                                                             *
*  Retour      : Réduction correspondante, expression déjà réduite, ou NULL.  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_scan_modpath_function_run_call(GScanModpathFunction *item, GScanExpression **args, size_t count, GScanContext *ctx, GScanScope *scope, GObject **out)
{

    return false;

#if 0   /* FIXME */

    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours #1       */
    size_t mcount;                          /* Quantité de correspondances */
    GScanMatch **matches;                   /* Correspondances établies    */
    size_t k;                               /* Boucle de parcours #2       */
    sized_string_t path;                    /* Combinaison à conserver     */
    GScanExpression *subitem;               /* Nouvel élément à transmettre*/

    /* Validation préalable du type des arguments */

    result = true;

    for (i = 0; i < count && result; i++)
        result = G_IS_SCAN_PATTERN_HANDLER(args[i]);

    if (!result)
        goto exit;

    /* Construction des chemins attendus */

    *out = G_OBJECT(g_scan_generic_set_new());

    for (i = 0; i < count; i++)
    {
        matches = g_scan_pattern_handler_get_all_matches(G_SCAN_PATTERN_HANDLER(args[i]), ctx, &mcount);
        if (mcount == 0) continue;

        /**
         * La série est à priori constituée d'éléments de même type, donc
         * un test unique suffit.
         */
        if (!G_IS_SCAN_BYTES_MATCH(matches[0]))
        {
            for (k = 0; k < mcount; k++)
                g_object_unref(G_OBJECT(matches[k]));
        }

        else
        {
            for (k = 0; k < mcount; k++)
            {
                path.data = g_scan_bytes_match_get_modifier_path(G_SCAN_BYTES_MATCH(matches[k]));
                if (path.data == NULL) continue;

                path.len = strlen(path.data);

                subitem = g_scan_literal_expression_new(LVT_STRING, &path);

                g_scan_generic_set_add_item(G_SCAN_GENERIC_SET(*out), subitem);

                g_object_unref(G_OBJECT(subitem));

                exit_szstr(&path);

                g_object_unref(G_OBJECT(matches[k]));

            }

        }

        free(matches);

    }

 exit:

    return result;

#endif

}