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


#include "vmpa.h"


#include <string.h>









typedef struct _py_vmpa_t
{
    PyObject_HEAD

    vmpa2t addr;

} py_vmpa_t;




/* Fournit une représentation d'une variable 'vmpa_t'. */
static PyObject *py_vmpa_to_str(PyObject *);

/* Effectue une conversion d'un objet Python en type 'vmpa_t'. */
static PyObject *py_vmpa_richcompare(PyObject *, PyObject *, int);

/* Fournit une partie du contenu de la position représentée. */
static PyObject *py_vmpa_get_value(PyObject *, void *);

/* Définit une partie du contenu de la position représentée. */
static int py_vmpa_set_value(PyObject *, PyObject *, void *);

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

/* Effectue une conversion d'un objet Python en type 'vmpa_t'. */
static bool convert_pyobj_to_vmpa(PyObject *, vmpa2t *);

/* Effectue une opération de type 'add' avec le type 'vmpa'. */
static PyObject *py_vmpa_nb_add(PyObject *, PyObject *);







/******************************************************************************
*                                                                             *
*  Paramètres  : obj = objet Python à traiter.                                *
*                                                                             *
*  Description : Fournit une représentation d'une variable 'vmpa_t'.          *
*                                                                             *
*  Retour      : Chaîne de caractère pour Python.                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_vmpa_to_str(PyObject *obj)
{
    PyObject *result;                       /* Chaîne à retourner          */
    vmpa2t *addr;                           /* Véritable adresse manipulée */
    off_t physical;                         /* Position physique           */
    uint64_t virtual;                       /* Adresse virtuelle           */

    addr = &((py_vmpa_t *)obj)->addr;

    physical = get_phy_addr(addr);
    virtual = get_virt_addr(addr);

    if (physical == VMPA_NO_PHYSICAL && virtual == VMPA_NO_VIRTUAL)
        result = PyUnicode_FromFormat("<phy=None, virt=None>");

    else if (physical != VMPA_NO_PHYSICAL && virtual == VMPA_NO_VIRTUAL)
        result = PyUnicode_FromFormat("<phy=%d, virt=None>", physical);

    else if (physical == VMPA_NO_PHYSICAL && virtual != VMPA_NO_VIRTUAL)
        result = PyUnicode_FromFormat("<phy=None, virt=0x%08x>", virtual);

    else
        result = PyUnicode_FromFormat("<phy=%d, virt=0x%08x>", physical, virtual);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj  = objet Python à tenter de convertir.                   *
*                addr = structure équivalente pour Chrysalide.                *
*                                                                             *
*  Description : Effectue une conversion d'un objet Python en type 'vmpa_t'.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_vmpa_richcompare(PyObject *a, PyObject *b, int op)
{
    PyObject *result;                       /* Chaîne à retourner          */
    vmpa2t *addr_a;                         /* Première adresse à traiter  */
    vmpa2t addr_b;                          /* Seconde adresse à traiter   */

    addr_a = &((py_vmpa_t *)a)->addr;

    if (!convert_pyobj_to_vmpa(b, &addr_b))
        return NULL;

    switch (op)
    {
        case Py_EQ:
            result = are_equal(addr_a, &addr_b) ? Py_True : Py_False;
            break;

        case Py_NE:
            result = are_equal(addr_a, &addr_b) ? Py_False : Py_True;
            break;

        default:
            result = Py_NotImplemented;
            break;

    }

    Py_INCREF(result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = définition d'adresse visée par la procédure.       *
*                closure = sélection de la valeur à traiter.                  *
*                                                                             *
*  Description : Fournit une partie du contenu de la position représentée.    *
*                                                                             *
*  Retour      : Nombre positif ou nul ou None.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_vmpa_get_value(PyObject *self, void *closure)
{
    PyObject *result;                       /* Valeur à retourner          */
    py_vmpa_t *vmpa;                        /* Véritable objet Python      */
    char *key;                              /* Contenu à cibler précisément*/

    vmpa = (py_vmpa_t *)self;

    key = (char *)closure;

    if (strcmp(key, "phy") == 0)
    {
        if (get_phy_addr(&vmpa->addr) == VMPA_NO_PHYSICAL)
        {
            result = Py_None;
            Py_INCREF(result);
        }
        else result = Py_BuildValue("K", get_phy_addr(&vmpa->addr));
    }
    else
    {
        if (get_virt_addr(&vmpa->addr) == VMPA_NO_VIRTUAL)
        {
            result = Py_None;
            Py_INCREF(result);
        }
        else result = Py_BuildValue("K", get_virt_addr(&vmpa->addr));
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = définition d'adresse visée par la procédure.       *
*                value   = valeur fournie à intégrer ou prendre en compte.    *
*                closure = sélection de la valeur à traiter.                  *
*                                                                             *
*  Description : Définit une partie du contenu de la position représentée.    *
*                                                                             *
*  Retour      : Bilan de l'opération pour Python.                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_vmpa_set_value(PyObject *self, PyObject *value, void *closure)
{
    int result;                             /* Bilan à faire remonter      */
    py_vmpa_t *vmpa;                        /* Véritable objet Python      */
    char *key;                              /* Contenu à cibler précisément*/
    PY_LONG_LONG val;                       /* Valeur traduite génériquemt */
    int overflow;                           /* Détection d'une grosse val. */

    result = 0;

    vmpa = (py_vmpa_t *)self;

    key = (char *)closure;

    if (strcmp(key, "phy") == 0)
    {
        if (value == Py_None)
            init_vmpa(&vmpa->addr, VMPA_NO_PHYSICAL, get_virt_addr(&vmpa->addr));

        else
        {
            val = PyLong_AsLongLongAndOverflow(value, &overflow);

            if (val == -1 && (overflow == 1 || PyErr_Occurred()))
            {
                result = -1;
                PyErr_Clear();
            }
            else init_vmpa(&vmpa->addr, val, get_virt_addr(&vmpa->addr));

        }

    }
    else
    {
        if (value == Py_None)
            init_vmpa(&vmpa->addr, get_phy_addr(&vmpa->addr), VMPA_NO_VIRTUAL);

        else
        {
            val = PyLong_AsLongLongAndOverflow(value, &overflow);

            if (val == -1 && (overflow == 1 || PyErr_Occurred()))
            {
                result = -1;
                PyErr_Clear();
            }
            else init_vmpa(&vmpa->addr, get_phy_addr(&vmpa->addr), val);

        }

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  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 'vmpa2t'.                *
*                                                                             *
*  Retour      : Instance Python mise en place.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_vmpa_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    py_vmpa_t *result;                       /* Instance à retourner        */
    unsigned long long phy;                 /* Position physique           */
    unsigned long long virt;                /* Adresse en mémoire virtuelle*/
    int ret;                                /* Bilan de lecture des args.  */

    phy = VMPA_NO_PHYSICAL;
    virt = VMPA_NO_VIRTUAL;

    ret = PyArg_ParseTuple(args, "|KK", &phy, &virt);
    if (!ret) Py_RETURN_NONE;

    result = (py_vmpa_t *)type->tp_alloc(type, 0);

    init_vmpa(&result->addr, phy, virt);

    return (PyObject *)result;

}






/******************************************************************************
*                                                                             *
*  Paramètres  : obj  = objet Python à tenter de convertir.                   *
*                addr = structure équivalente pour Chrysalide.                *
*                                                                             *
*  Description : Effectue une conversion d'un objet Python en type 'vmpa_t'.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool convert_pyobj_to_vmpa(PyObject *obj, vmpa2t *addr)
{
    bool result;                            /* Résulats à retourner        */
    PyTypeObject *py_vmpa_type;             /* Type Python pour 'vmpa'     */
    int ret;                                /* Bilan d'un appel            */
    PY_LONG_LONG value;                     /* Valeur de type générique    */
    int overflow;                           /* Détection d'une grosse val. */

    result = false;

    py_vmpa_type = get_python_vmpa_type();

    ret = PyObject_IsInstance(obj, (PyObject *)py_vmpa_type);

    /* S'il n'y a rien à faire... */
    if (ret == 1)
    {
        *addr = ((py_vmpa_t *)obj)->addr;
        result = true;
    }

    /* Sinon on demande à Python... */
    else
    {
        value = PyLong_AsLongLongAndOverflow(obj, &overflow);

        if (value == -1 && (overflow == 1 || PyErr_Occurred()))
            PyErr_Clear();

        else
        {
            init_vmpa(addr, value, value);
            result = true;
        }

    }

    return result;

}




/******************************************************************************
*                                                                             *
*  Paramètres  : o1 = premier élément concerné par l'opération.               *
*                o2 = second élément concerné par l'opération.                *
*                                                                             *
*  Description : Effectue une opération de type 'add' avec le type 'vmpa'.    *
*                                                                             *
*  Retour      : Résultat de l'opération.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_vmpa_nb_add(PyObject *o1, PyObject *o2)
{
    PyObject *result;                       /* Résultat à retourner        */
    vmpa2t addr1;                           /* Première adresse à traiter  */
    vmpa2t addr2;                           /* Seconde adresse à traiter   */
    PyTypeObject *py_vmpa_type;             /* Type Python pour 'vmpa'     */

    if (!convert_pyobj_to_vmpa(o1, &addr1))
        return NULL;

    if (!convert_pyobj_to_vmpa(o2, &addr2))
        return NULL;

    py_vmpa_type = get_python_vmpa_type();

    result = PyObject_CallObject((PyObject *)py_vmpa_type, NULL);

    init_vmpa(&((py_vmpa_t *)result)->addr,
              addr1.physical + addr2.physical,
              addr1.virtual + addr2.virtual);

    return result;

}




void log_simple_message(/*LogMessageType*/ int type, const char *msg)
{

}


void log_variadic_message(/*LogMessageType*/ int type, const char *fmt, ...)
{


}


void change_editor_items_current_view_content(void/*GtkViewPanel*/ *view)
{


}




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

PyTypeObject *get_python_vmpa_type(void)
{
    static PyNumberMethods py_vmpa_nb_proto = {

        .nb_add = py_vmpa_nb_add,

        /*
     binaryfunc nb_add;
     binaryfunc nb_subtract;
     binaryfunc nb_multiply;
     binaryfunc nb_remainder;
     binaryfunc nb_divmod;
     ternaryfunc nb_power;
     unaryfunc nb_negative;
     unaryfunc nb_positive;
     unaryfunc nb_absolute;
     inquiry nb_bool;
     unaryfunc nb_invert;
     binaryfunc nb_lshift;
     binaryfunc nb_rshift;
     binaryfunc nb_and;
     binaryfunc nb_xor;
     binaryfunc nb_or;
     unaryfunc nb_int;
     void *nb_reserved;
     unaryfunc nb_float;

     binaryfunc nb_inplace_add;
     binaryfunc nb_inplace_subtract;
     binaryfunc nb_inplace_multiply;
     binaryfunc nb_inplace_remainder;
     ternaryfunc nb_inplace_power;
     binaryfunc nb_inplace_lshift;
     binaryfunc nb_inplace_rshift;
     binaryfunc nb_inplace_and;
     binaryfunc nb_inplace_xor;
     binaryfunc nb_inplace_or;

     binaryfunc nb_floor_divide;
     binaryfunc nb_true_divide;
     binaryfunc nb_inplace_floor_divide;
     binaryfunc nb_inplace_true_divide;
     
     unaryfunc nb_index;

        */

    };

    static PyGetSetDef py_vmpa_getseters[] = {

        {
            "phy", py_vmpa_get_value, py_vmpa_set_value,
            "Give access to the physical offset of the location", "phy"
        },

        {
            "virt", py_vmpa_get_value, py_vmpa_set_value,
            "Give access to the virtual address of the location", "virt"
        },
        { NULL }

    };

    static PyTypeObject py_vmpa_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

        .tp_name        = "pychrysalide.arch.vmpa",
        .tp_basicsize   = sizeof(py_vmpa_t),

        .tp_as_number   = &py_vmpa_nb_proto,

        .tp_str         = py_vmpa_to_str,

        .tp_flags       = Py_TPFLAGS_DEFAULT,

        .tp_doc         = "Python object for vmpa_t",

        .tp_richcompare = py_vmpa_richcompare,

        .tp_getset      = py_vmpa_getseters,
        .tp_new         = (newfunc)py_vmpa_new

    };

    return &py_vmpa_type;

}


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

bool register_python_vmpa(PyObject *module)
{
    PyTypeObject *py_vmpa_type;             /* Type Python pour 'vmpa'     */
    int ret;                                /* Bilan d'un appel            */

    py_vmpa_type = get_python_vmpa_type();

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

    Py_INCREF(py_vmpa_type);
    ret = PyModule_AddObject(module, "vmpa", (PyObject *)py_vmpa_type);

    return (ret == 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : obj = objet Python à traiter.                                *
*                                                                             *
*  Description : Donne accès au coeur d'un objet 'pychrysalide.arch.vmpa'.    *
*                                                                             *
*  Retour      : Localistion réelle ou NULL en cas de mauvaise conversion.    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

vmpa2t *get_internal_vmpa(PyObject *obj)
{
    int ret;                                /* Bilan d'analyse             */

    ret = PyObject_IsInstance(obj, (PyObject *)get_python_vmpa_type());
    if (!ret) return NULL;

    return &((py_vmpa_t *)obj)->addr;

}