summaryrefslogtreecommitdiff
path: root/src/format/java/pool.c
blob: b825084b22ef1e6aaf55923f077276583867849d (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

/* Chrysalide - Outil d'analyse de fichiers binaires
 * pool.c - lecture du réservoir de constantes
 *
 * Copyright (C) 2009-2012 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 "pool.h"


#include <malloc.h>
#include <math.h>
#include <string.h>

#include "java-int.h"
#include "../../common/endianness.h"
#include "../../common/extstr.h"



/* Charge les propriétés d'une constante du réservoir. */
bool load_java_pool_entry(GJavaFormat *, constant_pool_entry *, off_t *);

/* Fournit une entrée donnée du réservoir de constantes. */
const constant_pool_entry *get_java_pool_entry(const GJavaFormat *, uint16_t, ConstantPoolTag);



/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                pos    = point de lecture à faire évoluer. [OUT]             *
*                                                                             *
*  Description : Charge le réservoir de constantes d'un binaire Java.         *
*                                                                             *
*  Retour      : true si l'opération s'est bien déroulée, false sinon.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool load_java_pool(GJavaFormat *format, off_t *pos)
{
    bool result;                            /* Bilan à remonter            */
    uint16_t count;                         /* Nombre d'éléments présents  */
    uint16_t i;                             /* Boucle de parcours          */

    result = false/*read_u16(&count, G_BIN_FORMAT(format)->content, pos,
                    G_BIN_FORMAT(format)->length, SRE_BIG)*/;
#if 0
    printf("Alloc %hu entries (result=%d)\n", count, result);

    format->header.pool_len = count - 1;
    format->header.pool = (constant_pool_entry *)calloc(count - 1, sizeof(constant_pool_entry));

    for (i = 1; i < count && result; i++)
    {
        result = load_java_pool_entry(format, &format->header.pool[i - 1], pos);

        if (format->header.pool[i - 1].tag == CONSTANT_LONG
            || format->header.pool[i - 1].tag == CONSTANT_DOUBLE)
        {
            i++;

            /* On n'est jamais trop prudent */
            if (i < count)
                format->header.pool[i - 1].tag = CONSTANT_EMPTY;

        }

    }
#endif
    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à vider.                *
*                                                                             *
*  Description : Décharge le réservoir de constantes d'un binaire Java.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void unload_java_pool(GJavaFormat *format)
{
    uint16_t i;                             /* Boucle de parcours          */

    for (i = 0; i < format->header.pool_len; i++)
        switch (format->header.pool[i].tag)
        {
            case CONSTANT_EMPTY:
            case CONSTANT_CLASS:
            case CONSTANT_FIELD_REF:
            case CONSTANT_METHOD_REF:
            case CONSTANT_INTERFACE_METHOD_REF:
            case CONSTANT_STRING:
            case CONSTANT_INTEGER:
            case CONSTANT_FLOAT:
            case CONSTANT_LONG:
            case CONSTANT_DOUBLE:
            case CONSTANT_NAME_AND_TYPE:
                break;

            case CONSTANT_UTF8:
                free(format->header.pool[i].info.utf8.bytes);
                break;

        }

    free(format->header.pool);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                entry  = élément à spécifier. [OUT]                          *
*                pos    = point de lecture à faire évoluer. [OUT]             *
*                                                                             *
*  Description : Charge les propriétés d'une constante du réservoir.          *
*                                                                             *
*  Retour      : true si l'opération s'est bien déroulée, false sinon.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool load_java_pool_entry(GJavaFormat *format, constant_pool_entry *entry, off_t *pos)
{
    bool result;                            /* Bilan à retourner           */
    uint8_t tag;                            /* Type de l'élément           */
    uint32_t low_bytes;                     /* Octets de poids faible      */
    uint32_t high_bytes;                    /* Octets de poids fort        */
    uint64_t bits;                          /* Nombre lu sur 64 bits       */
    int sign;                               /* Signe du nombre lu          */
	int exponent;                           /* Exposant du nombre lu       */
	uint64_t mantissa32;                    /* Mantisse du nombre lu 32b   */
	uint64_t mantissa64;                    /* Mantisse du nombre lu 64b   */
    uint16_t length;                        /* Taille d'une chaîne         */

    result = false/*read_u8(&tag, G_BIN_FORMAT(format)->content, pos,
                    G_BIN_FORMAT(format)->length, SRE_BIG)*/;
#if 0
    entry->tag = tag;

    switch (entry->tag)
    {
        case CONSTANT_CLASS:
            result = read_u16(&entry->info.class.name_index, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            break;

        case CONSTANT_FIELD_REF:
        case CONSTANT_METHOD_REF:
        case CONSTANT_INTERFACE_METHOD_REF:

            result = read_u16(&entry->info.ref.class_index, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            result &= read_u16(&entry->info.ref.name_and_type_index, G_BIN_FORMAT(format)->content,
                               pos, G_BIN_FORMAT(format)->length, SRE_BIG);

            break;

        case CONSTANT_STRING:
            result = read_u16(&entry->info.string.string_index, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            break;

        case CONSTANT_INTEGER:
            result = read_u32(&entry->info.int_val.val, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            break;

        case CONSTANT_FLOAT:

            result = read_u32(&low_bytes, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);

            if (result)
            {
                if (low_bytes == 0x7f800000)
                    entry->info.float_val.val = INFINITY;

                else if (low_bytes == 0xff800000)
                    entry->info.float_val.val = /* -1* */INFINITY;

                else if ((low_bytes >= 0x7f800001 && low_bytes <= 0x7fffffff)
                         || (low_bytes >= 0xff800001 && low_bytes <= 0xffffffff))
                    entry->info.float_val.val = NAN;

                else if (low_bytes == 0x00000000 || low_bytes == 0x80000000)
                    entry->info.float_val.val = 0;

                else
                {
                    sign = (low_bytes & 0x80000000) ? -1 : 1;
                    exponent = (low_bytes >> 23) & 0xff;
                    mantissa32 = (exponent == 0 ?
                                  (low_bytes & 0x7fffff) << 1 :
                                  (low_bytes & 0x7fffff) | 0x800000);

                    entry->info.float_val.val = pow(2, (exponent - 150));
                    entry->info.float_val.val *= mantissa32;
                    entry->info.float_val.val *= sign;

                }

            }

            break;

        case CONSTANT_LONG:

            result = read_u32(&high_bytes, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            result &= read_u32(&low_bytes, G_BIN_FORMAT(format)->content,
                               pos, G_BIN_FORMAT(format)->length, SRE_BIG);

            if (result)
            {
                entry->info.double_val.val = (uint64_t)high_bytes << 32;
                entry->info.double_val.val += low_bytes;
            }

            break;

        case CONSTANT_DOUBLE:

            result = read_u32(&high_bytes, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            result &= read_u32(&low_bytes, G_BIN_FORMAT(format)->content,
                               pos, G_BIN_FORMAT(format)->length, SRE_BIG);

            if (result)
            {
                bits = (uint64_t)high_bytes << 32 | (uint64_t)low_bytes;

                if (bits == 0x7ff0000000000000ll)
                    entry->info.double_val.val = INFINITY;

                else if (bits == 0xfff0000000000000ll)
                    entry->info.double_val.val = /* -1* */INFINITY;

                else if ((bits >= 0x7ff0000000000001ll && bits <= 0x7fffffffffffffffll)
                         || (bits >= 0xfff0000000000001ll && bits <= 0xffffffffffffffffll))
                    entry->info.double_val.val = NAN;

                else if (bits == 0x0000000000000000ll || bits == 0x8000000000000000ll)
                    entry->info.double_val.val = 0;

                else
                {
                    sign = ((bits >> 63) == 0) ? 1 : -1;
                    exponent = (bits >> 52) & 0x7ffl;
                    mantissa64 = (exponent == 0 ?
                                  (bits & 0xfffffffffffffll) << 1 :
                                  (bits & 0xfffffffffffffll) | 0x10000000000000ll);

                    entry->info.double_val.val = pow(2, (exponent - 1075));
                    entry->info.double_val.val *= mantissa64;
                    entry->info.double_val.val *= sign;

                }

            }

            break;

        case CONSTANT_NAME_AND_TYPE:

            result = read_u16(&entry->info.name_type.name_index, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);
            result &= read_u16(&entry->info.name_type.descriptor_index, G_BIN_FORMAT(format)->content,
                               pos, G_BIN_FORMAT(format)->length, SRE_BIG);

            break;

        case CONSTANT_UTF8:

            result = read_u16(&length, G_BIN_FORMAT(format)->content,
                              pos, G_BIN_FORMAT(format)->length, SRE_BIG);

            if (result)
            {
                entry->info.utf8.bytes = (char *)calloc(length + 1, sizeof(char));
                memcpy(entry->info.utf8.bytes, &G_BIN_FORMAT(format)->content[*pos], length);
                *pos += length;
            }

            break;

        default:
            result = false;
            break;

    }
#endif
    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                index  = indice de l'élément dont la valeur est à recupérer. *
*                expected = type de l'élément à trouver à l'indice donné.     *
*                                                                             *
*  Description : Fournit une entrée donnée du réservoir de constantes.        *
*                                                                             *
*  Retour      : Entrée du réservoir de constantes ou NULL en cas d'erreur.   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const constant_pool_entry *get_java_pool_entry(const GJavaFormat *format, uint16_t index, ConstantPoolTag expected)
{
    const constant_pool_entry *result;      /* Entrée à retourner          */
    constant_pool_entry *entry;             /* Entrée du réservoir visée   */

    result = NULL;

    if (/*index < 0 && FIXME */index <= format->header.pool_len);
    {
        entry = &format->header.pool[index - 1];

        if (entry->tag == expected)
            result = entry;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format   = description de l'exécutable à consulter.          *
*                index    = indice de l'élément de la table à recupérer.      *
*                expected = type de l'élément à trouver à l'indice donné.     *
*                                                                             *
*  Description : Construit une version humaine de référence.                  *
*                                                                             *
*  Retour      : Référence construite ou NULL en cas de problème.             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *build_reference_from_java_pool(const GJavaFormat *format, uint16_t index, JavaRefType expected)
{
    char *result;                           /* Chaîne humaine à retourner  */
    const constant_pool_entry *entry;       /* Entrée du réservoir visée 1 */
    const constant_pool_entry *subentry;    /* Entrée du réservoir visée 2 */
    const char *tmp;                        /* Copie de chaîne intouchable */

    result = NULL;

    switch (expected)
    {
        case JRT_FIELD:
            entry = get_java_pool_entry(format, index, CONSTANT_FIELD_REF);
            break;
        case JRT_METHOD:
            entry = get_java_pool_entry(format, index, CONSTANT_METHOD_REF);
            break;
        case JRT_INTERFACE_METHOD:
            entry = get_java_pool_entry(format, index, CONSTANT_INTERFACE_METHOD_REF);
            break;
        default:
            entry = NULL;
            break;
    }

    if (entry == NULL)
        goto brfjp_error;

    /* Lieu parent où trouver la référence */

    subentry = get_java_pool_entry(format, entry->info.ref.class_index, CONSTANT_CLASS);

    if (subentry == NULL)
        goto brfjp_error;

    if (!get_java_pool_ut8_string(format, subentry->info.class.name_index, &tmp))
        goto brfjp_error;

    result = strdup(tmp);

    /* Champ proprement dit */

    subentry = get_java_pool_entry(format, entry->info.ref.name_and_type_index, CONSTANT_NAME_AND_TYPE);

    if (subentry == NULL)
        goto brfjp_error;

    if (!get_java_pool_ut8_string(format, subentry->info.name_type.name_index, &tmp))
        goto brfjp_error;

    result = stradd(result, ".");
    result = stradd(result, tmp);

    /* Petites retouches finales */

    result = strrpl(result, "/", ".");
    result = strrpl(result, "<", "&lt;");
    result = strrpl(result, ">", "&gt;");

    return result;

 brfjp_error:

    if (result != NULL)
        free(result);

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                index  = indice de l'élément dont la valeur est à recupérer. *
*                str    = adresse où placer la chaîne de caractères trouvée.  *
*                                                                             *
*  Description : Recherche une chaîne de caractères dans le réservoir.        *
*                                                                             *
*  Retour      : true si l'opération s'est bien déroulée, false sinon.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool get_java_pool_ut8_string(const GJavaFormat *format, uint16_t index, const char **str)
{
    bool result;                            /* Bilan à renvoyer            */
    const constant_pool_entry *entry;       /* Entrée du réservoir visée   */

    entry = get_java_pool_entry(format, index, CONSTANT_UTF8);

    result = (entry != NULL);

    if (result)
        (*str) = entry->info.utf8.bytes;

    return result;

}