summaryrefslogtreecommitdiff
path: root/tools/d2c/qckcall.c
blob: c35bd9dbc950cad289da885b7869d9394bd3c511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

/* Chrysalide - Outil d'analyse de fichiers binaires
 * qckcall.c - appel rapide et facilité à une fonction C de Chrysalide
 *
 * Copyright (C) 2015-2017 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "qckcall.h"


#include <malloc.h>
#include <regex.h>
#include <string.h>
#include <sys/param.h>


#include "helpers.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : callee = fonction appelée à nommer.                          *
*                                                                             *
*  Description : Prépare au besoin la définition d'une macro de transtypage.  *
*                                                                             *
*  Retour      : Macro de transtypage à libérer après usage ou NULL.          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *build_cast_if_needed(const char *callee)
{
    char *result;                           /* Macro à retourner           */
    regex_t preg;                           /* Expression régulière        */
    int ret;                                /* Bilan d'une manipulation    */
    regmatch_t pmatch[3];                   /* Correspondances de chaînes  */
    size_t cmplen;                          /* Taille de comparaison       */

    result = NULL;

    ret = regcomp(&preg, "(g_([a-z0-9]*)_instruction)", REG_EXTENDED);
    if (ret != 0)
    {
        fprintf(stderr, "Internal error: bad regular expression.\n");
        return NULL;
    }

    ret = regexec(&preg, callee, sizeof(pmatch) / sizeof(regmatch_t), pmatch, 0);
    if (ret == REG_NOMATCH)
    {
        fprintf(stderr, "Internal error: bad function for dealing wih instruction: '%s'.\n", callee);
        goto cif_exit;
    }

    /**
     * La variable de résultat est de type 'GArchInstruction',
     * donc toute fonction différente de g_arch_instruction_*() attend un transtypage...
     */

    cmplen = MAX(strlen("arch"), pmatch[2].rm_eo - pmatch[2].rm_so);

    if (strncmp("arch", &callee[pmatch[2].rm_so], cmplen) != 0)
    {
        result = strndup(&callee[pmatch[1].rm_so], pmatch[1].rm_eo - pmatch[1].rm_so);

        result = make_string_upper(result);

    }

 cif_exit:

    regfree(&preg);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : callee = fonction appelée à nommer.                          *
*                args   = précise si la conversion est la dernière.           *
*                fd     = descripteur d'un flux ouvert en écriture.           *
*                bits   = gestionnaire des bits d'encodage.                   *
*                list   = liste de l'ensemble des fonctions de conversion.    *
*                                                                             *
*  Description : Réalise un appel à une fonction liée à une instruction.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool call_instr_func(const char *callee, const arg_list_t *args, int fd, const coding_bits *bits, const conv_list *list)
{
    bool result;                            /* Bilan à remonter            */
    char *cast;                             /* Macro de transtypage        */

    cast = build_cast_if_needed(callee);

    if (cast == NULL)
        dprintf(fd, "\t%s(result, ", callee);

    else
    {
        dprintf(fd, "\t%s(%s(result), ", callee, cast);
        free(cast);
    }

    result = define_arg_list(args, fd, bits, list);

    dprintf(fd, ");\n");

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : callee = fonction appelée à nommer.                          *
*                args   = précise si la conversion est la dernière.           *
*                fd     = descripteur d'un flux ouvert en écriture.           *
*                bits   = gestionnaire des bits d'encodage.                   *
*                list   = liste de l'ensemble des fonctions de conversion.    *
*                                                                             *
*  Description : Réalise un appel à une fonction liée à une instruction.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool checked_call_instr_func(const char *callee, const arg_list_t *args, int fd, const coding_bits *bits, const conv_list *list)
{
    bool result;                            /* Bilan à remonter            */
    char *cast;                             /* Macro de transtypage        */

    cast = build_cast_if_needed(callee);

    if (cast == NULL)
        dprintf(fd, "\tif (!%s(result, ", callee);

    else
    {
        dprintf(fd, "\tif (!%s(%s(result), ", callee, cast);
        free(cast);
    }

    result = define_arg_list(args, fd, bits, list);

    dprintf(fd, "))\n");

    return result;

}