/* Chrysalide - Outil d'analyse de fichiers binaires
 * class.c - annotation des définitions de classes
 *
 * Copyright (C) 2016 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 "class.h"


#include <assert.h>
#include <malloc.h>
#include <stdio.h>


#include <i18n.h>
#include <arch/raw.h>
#include <format/symbol.h>
#include <format/dex/class.h>
#include <format/dex/dex_def.h>


#include "code.h"



/* Commente les définitions des classes pour la VM Dalvik. */
static bool annotate_dex_class_data(const GDexFormat *, const GDexClass *, uint32_t );

/* Commente les définitions des champs encodés. */
static bool annotate_dex_encoded_field(const GDexFormat *, vmpa2t *);

/* Commente les définitions des méthodes encodées. */
static bool annotate_dex_encoded_method(const GDexFormat *, const encoded_method *, vmpa2t *);



/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Commente les définitions des classes pour la VM Dalvik.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_class_defs(const GDexFormat *format, GtkStatusStack *status)
{
    bool result;                            /* Bilan à retourner           */
    GBinContent *content;                   /* Contenu binaire à lire      */
    const dex_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    activity_id_t msg;                      /* Message de progression      */
    uint32_t i;                             /* Boucle de parcours          */
    vmpa2t start;                           /* Localisation des symboles   */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */
    GDexClass *class;                       /* Classe chargée à manipuler  */
    const class_def_item *def;              /* Définition brute à lire     */

    result = true;

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    header = g_dex_format_get_header(format);
    endian = SRE_LITTLE;//g_dex_format_get_endianness(format);

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), header->class_defs_off, &pos))
        return false;

    msg = gtk_status_stack_add_activity(status, _("Writing annotations for all Dex classes..."),
                                        header->class_defs_size);

    for (i = 0; i < header->class_defs_size && result; i++)
    {
        /* class_idx */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index into the type_ids list for this class"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        if (i == 0)
            g_binary_symbol_define_as_block_start(symbol, true);

        /* access_flags */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

        asprintf(&text, _("Access flags for the class"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* superclass_idx */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the superclass or NO_INDEX if this class has no superclass"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* interfaces_off */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

        asprintf(&text, _("Offset to the list of interfaces"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* source_file_idx */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        asprintf(&text, _("Index for the name of the file containing the original source or NO_INDEX"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* annotations_off */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

        asprintf(&text, _("Offset to the annotations structure for this class"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* class_data_off */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

        asprintf(&text, _("Offset to the associated class data for this item"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* static_values_off */

        copy_vmpa(&start, &pos);
        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, &pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

        asprintf(&text, _("Offset to the list of initial values for static fields"));

        ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

        free(text);

        /* Annotations supplémentaires */

        class = g_dex_format_get_class(format, i);

        def = g_dex_class_get_definition(class);

        if (def->class_data_off > 0)
            result = annotate_dex_class_data(format, class, def->class_data_off);

        /* TODO : g_object_unref(G_OBJECT(class));*/

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    g_object_unref(G_OBJECT(content));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                class  = classe Dex dont les données sont à commenter.       *
*                offset = tête de lecture physique des symboles.              *
*                                                                             *
*  Description : Commente les définitions des classes pour la VM Dalvik.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_class_data(const GDexFormat *format, const GDexClass *class, uint32_t offset)
{
    bool result;                            /* Bilan à retourner           */
    GBinContent *content;                   /* Contenu binaire à lire      */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    vmpa2t start;                           /* Localisation des symboles   */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */
    const class_data_item *data;            /* Données chargées à lire     */
    uleb128_t i;                            /* Boucle de parcours          */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), offset, &pos))
        return false;

    /* static_fields_size */

    copy_vmpa(&start, &pos);
    instr = g_raw_instruction_new_uleb128(content, &pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    asprintf(&text, _("Number of static fields defined in this item"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    g_binary_symbol_define_as_block_start(symbol, true);

    /* instance_fields_size */

    copy_vmpa(&start, &pos);
    instr = g_raw_instruction_new_uleb128(content, &pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    asprintf(&text, _("Number of instance fields defined in this item"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    /* direct_methods_size */

    copy_vmpa(&start, &pos);
    instr = g_raw_instruction_new_uleb128(content, &pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    asprintf(&text, _("Number of direct methods defined in this item"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    /* virtual_methods_size */

    copy_vmpa(&start, &pos);
    instr = g_raw_instruction_new_uleb128(content, &pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    asprintf(&text, _("Number of virtual methods defined in this item"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    /* Chargements complémentaires */

    result = true;

    data = g_dex_class_get_data(class);

    if (data != NULL)
    {
        for (i = 0; i < data->static_fields_size && result; i++)
            result = annotate_dex_encoded_field(format, &pos);

        for (i = 0; i < data->instance_fields_size && result; i++)
            result = annotate_dex_encoded_field(format, &pos);

        for (i = 0; i < data->direct_methods_size && result; i++)
            result = annotate_dex_encoded_method(format, &data->direct_methods[i], &pos);

        for (i = 0; i < data->virtual_methods_size && result; i++)
            result = annotate_dex_encoded_method(format, &data->virtual_methods[i], &pos);

    }

    /* Nettoyage final */

    g_object_unref(G_OBJECT(content));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                pos    = tête de lecture à faire progresser. [OUT]           *
*                                                                             *
*  Description : Commente les définitions des champs encodés.                 *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_field(const GDexFormat *format, vmpa2t *pos)
{
    GBinContent *content;                   /* Contenu binaire à lire      */
    vmpa2t start;                           /* Localisation des symboles   */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    /* field_idx_diff */

    copy_vmpa(&start, pos);
    instr = g_raw_instruction_new_uleb128(content, pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    asprintf(&text, _("Index into the field_ids list for the identity of this field"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    g_binary_symbol_define_as_block_start(symbol, true);

    /* access_flags */

    copy_vmpa(&start, pos);
    instr = g_raw_instruction_new_uleb128(content, pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

    asprintf(&text, _("Access flags for the field"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    /* Nettoyage final */

    g_object_unref(G_OBJECT(content));

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                method = méthode à décrire.                                  *
*                pos    = tête de lecture à faire progresser. [OUT]           *
*                                                                             *
*  Description : Commente les définitions des méthodes encodées.              *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_method(const GDexFormat *format, const encoded_method *method, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    GBinContent *content;                   /* Contenu binaire à lire      */
    vmpa2t start;                           /* Localisation des symboles   */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *text;                             /* Texte constant à insérer    */

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    /* method_idx_diff */

    copy_vmpa(&start, pos);
    instr = g_raw_instruction_new_uleb128(content, pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    asprintf(&text, _("Index into the method_ids list for the identity of this method"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    g_binary_symbol_define_as_block_start(symbol, true);

    /* access_flags */

    copy_vmpa(&start, pos);
    instr = g_raw_instruction_new_uleb128(content, pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

    asprintf(&text, _("Access flags for the method"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    /* code_off */

    copy_vmpa(&start, pos);
    instr = g_raw_instruction_new_uleb128(content, pos);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_HEX);

    asprintf(&text, _("Offset to the code structure for this method"));

    ADD_RAW_AS_SYM(format, symbol, &start, instr, comment, text);

    free(text);

    /* Chargements complémentaires, si non abstraite ni native */

    if (method->code_off > 0)
        result = annotate_dex_code_item(format, method->code_off);
    else
        result = true;

    /* Nettoyage final */

    g_object_unref(G_OBJECT(content));

    return result;

}