/* Chrysalide - Outil d'analyse de fichiers binaires
 * routine.c - équivalent Python du fichier "analysis/routine.c"
 *
 * Copyright (C) 2013 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "routine.h"


#include <string.h>
#include <pygobject.h>


#include <i18n.h>


#include <analysis/routine.h>


#include "block.h"



/* Crée un nouvel objet Python de type 'BinRoutine'. */
static PyObject *py_binary_routine_new(PyTypeObject *, PyObject *, PyObject *);

/* Fournit le nom humain d'une routine. */
static PyObject *py_binary_routine_get_name(PyObject *, void *);

/* Définit le nom humain d'une routine. */
static int py_binary_routine_set_name(PyObject *, PyObject *, void *);

/* Fournit les blocs basiques de la routine. */
static PyObject *py_binary_routine_get_basic_blocks(PyObject *, void *);



/******************************************************************************
*                                                                             *
*  Paramètres  : type = type de l'objet à instancier.                         *
*                args = arguments fournis à l'appel.                          *
*                kwds = arguments de type key=val fournis.                    *
*                                                                             *
*  Description : Crée un nouvel objet Python de type 'BinaryRoutine'.         *
*                                                                             *
*  Retour      : Instance Python mise en place.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_routine_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyObject *result;                       /* Création à retourner        */

    result = pygobject_new(G_OBJECT(g_binary_routine_new()));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Fournit le nom humain d'une routine.                         *
*                                                                             *
*  Retour      : Désignation humainement lisible ou None si non définie.      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_routine_get_name(PyObject *self, void *closure)
{
    PyObject *result;                       /* Valeur à retourner          */
    GBinRoutine *routine;                   /* Elément à consulter         */
    const char *name;                       /* Désignation courante        */

    routine = G_BIN_ROUTINE(pygobject_get(self));
    name = g_binary_routine_get_name(routine);

    if (name != NULL)
        result = PyUnicode_FromString(name);
    else
    {
        result = Py_None;
        Py_INCREF(result);
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                value   = valeur fournie à intégrer ou prendre en compte.    *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Définit le nom humain d'une routine.                         *
*                                                                             *
*  Retour      : Bilan de l'opération pour Python.                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_binary_routine_set_name(PyObject *self, PyObject *value, void *closure)
{
    GBinRoutine *routine;                   /* Elément à consulter         */

    if (!PyUnicode_Check(value) && value != Py_None)
    {
        PyErr_SetString(PyExc_TypeError, _("The attribute value must be a string."));
        return -1;
    }

    routine = G_BIN_ROUTINE(pygobject_get(self));

    if (!PyUnicode_Check(value))
        g_binary_routine_set_name(routine, strdup(PyUnicode_DATA(value)));
    else
        g_binary_routine_set_name(routine, NULL);

    return 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = classe représentant une routine binaire.           *
*                closure = adresse non utilisée ici.                          *
*                                                                             *
*  Description : Fournit les blocs basiques de la routine.                    *
*                                                                             *
*  Retour      : Ensemble de blocs déterminés via les instructions.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_binary_routine_get_basic_blocks(PyObject *self, void *closure)
{
    PyObject *result;                       /* Eléments à retourner        */
    GBinRoutine *routine;                   /* Version native              */
    GInstrBlock *blocks;                    /* Blocs basiques de routine   */

    routine = G_BIN_ROUTINE(pygobject_get(self));
    blocks = g_binary_routine_get_basic_blocks(routine);

    result = pygobject_new(G_OBJECT(blocks));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                value   = valeur fournie à intégrer ou prendre en compte.    *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Définit les blocs basiques de la routine.                    *
*                                                                             *
*  Retour      : Bilan de l'opération pour Python.                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_binary_routine_set_basic_blocks(PyObject *self, PyObject *value, void *closure)
{
    GBinRoutine *routine;                   /* Elément à consulter         */
    int ret;                                /* Bilan de lecture des args.  */
    GInstrBlock *blocks;                    /* Blocs basiques à intégrer   */

    ret = PyObject_IsInstance(value, (PyObject *)get_python_instr_block_type());
    if (!ret) return -1;

    routine = G_BIN_ROUTINE(pygobject_get(self));
    blocks = G_INSTR_BLOCK(pygobject_get(value));

    g_binary_routine_set_basic_blocks(routine, blocks);

    return 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Fournit un accès à une définition de type à diffuser.        *
*                                                                             *
*  Retour      : Définition d'objet pour Python.                              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

PyTypeObject *get_python_binary_routine_type(void)
{
    static PyMethodDef py_binary_routine_methods[] = {
        { NULL }
    };

    static PyGetSetDef py_binary_routine_getseters[] = {
        {
            "name", py_binary_routine_get_name, py_binary_routine_set_name,
            "Name of the current routine.", NULL
        },
        {
            "basic_blocks", py_binary_routine_get_basic_blocks, py_binary_routine_set_basic_blocks,
            "Basic blocks of the binary routine.", NULL
        },
        { NULL }
    };

    static PyTypeObject py_binary_routine_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

        .tp_name        = "pychrysalide.analysis.BinRoutine",

        .tp_flags       = Py_TPFLAGS_DEFAULT,

        .tp_doc         = "PyChrysalide binary routine",

        .tp_methods     = py_binary_routine_methods,
        .tp_getset      = py_binary_routine_getseters,
        .tp_new         = (newfunc)py_binary_routine_new

    };

    return &py_binary_routine_type;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : module = module dont la définition est à compléter.          *
*                                                                             *
*  Description : Prend en charge l'objet 'pychrysalide.analysis.BinRoutine'.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool register_python_binary_routine(PyObject *module)
{
    PyTypeObject *py_binary_routine_type;    /* Type Python 'BinRoutine'  */
    int ret;                                /* Bilan d'un appel            */
    PyObject *dict;                         /* Dictionnaire du module      */

    py_binary_routine_type = get_python_binary_routine_type();

    py_binary_routine_type->tp_base = &PyGObject_Type;
    py_binary_routine_type->tp_basicsize = py_binary_routine_type->tp_base->tp_basicsize;

    if (PyType_Ready(py_binary_routine_type) != 0)
        return false;

    Py_INCREF(py_binary_routine_type);
    ret = PyModule_AddObject(module, "BinRoutine", (PyObject *)py_binary_routine_type);
    if (ret != 0) return false;

    dict = PyModule_GetDict(module);
    pygobject_register_class(dict, "BinRoutine", G_TYPE_BIN_ROUTINE, py_binary_routine_type,
                             Py_BuildValue("(O)", py_binary_routine_type->tp_base));

    return true;

}