summaryrefslogtreecommitdiff
path: root/src/debug/remgdb/packet.c
blob: aa606c7e83cae29757b5e0cab2468370bbeb0730 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * packet.c - manipulation des paquets de données GDB.
 *
 * 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 "packet.h"


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


#include "../../common/dllist.h"



/* Répresentation d'un paquet GDB (instance) */
struct _GGdbPacket
{
    GObject parent;                         /* A laisser en premier        */

    DL_LIST_ITEM(link);                     /* Lien vers les autres        */

    char *buffer;                           /* Données à traiter           */
    size_t len;                             /* Quantité de ces données     */
    size_t allocated;                       /* Taille du tampon            */

    uint8_t checksum;                       /* Empreinte de contrôle       */

};


/* Répresentation d'un paquet GDB (classe) */
struct _GGdbPacketClass
{
    GObjectClass parent;                    /* A laisser en premier        */

};


/* Initialise la classe des représentations des paquets GDB. */
static void g_gdb_packet_class_init(GGdbPacketClass *);

/* Initialise une instance de représentation de paquet GDB. */
static void g_gdb_packet_init(GGdbPacket *);



/* Indique le type défini pour une répresentation de paquet GDB. */
G_DEFINE_TYPE(GGdbPacket, g_gdb_packet, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des représentations des paquets GDB.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_gdb_packet_class_init(GGdbPacketClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une instance de représentation de paquet GDB.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_gdb_packet_init(GGdbPacket *packet)
{
    DL_LIST_ITEM_INIT(&packet->link);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée une représentation de paquet GDB.                       *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GGdbPacket *g_gdb_packet_new(void)
{
    GGdbPacket *result;                     /* Structure à retourner       */

    result = g_object_new(G_TYPE_GDB_PACKET, NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = paquet à préparer pour une émission.                *
*                                                                             *
*  Description : Prépare un paquet pour un envoi prochain.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_gdb_packet_start_new_command(GGdbPacket *packet)
{
    if (packet->allocated == 0)
    {
        packet->allocated = 1;
        packet->buffer = (char *)calloc(packet->allocated, sizeof(char));
    }

    packet->buffer[0] = '\0';
    packet->len = 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = paquet à préparer pour une émission.                *
*                string = chaîne à inclure dans le paquet.                    *
*                                                                             *
*  Description : Complète un paquet pour un envoi prochain.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_gdb_packet_append(GGdbPacket *packet, const char *string)
{
    size_t len;                             /* Taille de la chaîne donnée  */

    len = strlen(string);

    /* Si la place n'est pas assez grande */
    if ((packet->len + len + 1) >= packet->allocated)
    {
        packet->buffer = (char *)realloc(packet->buffer, (packet->len + len + 1) * sizeof(char));
        packet->allocated = packet->len + len + 1;
    }

    strcat(packet->buffer, string);

    packet->len += len;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = paquet à analyser.                                  *
*                                                                             *
*  Description : Détermine l'empreinte des données d'un paquet GDB.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_gdb_packet_compute_checksum(GGdbPacket *packet)
{
    int sum;                                /* Valeur cumulée des données  */
    size_t i;                               /* Boucle de parcours          */

    sum = 0;

    for (i = 0; i < packet->len; i++)
        sum += packet->buffer[i];

    packet->checksum = sum % 256;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet   = paquet à analyser.                                *
*                checksum = contrôle d'intégrité à retrouver.                 *
*                                                                             *
*  Description : Contrôle l'intégrité des données d'un paquet GDB.            *
*                                                                             *
*  Retour      : Bilan de la vérification.                                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_gdb_packet_verify_checksum(GGdbPacket *packet, uint8_t checksum)
{
    g_gdb_packet_compute_checksum(packet);

    return checksum == packet->checksum;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = paquet à décoder et/ou décompresser.                *
*                                                                             *
*  Description : Décode et/ou décompresse un paquet GDB.                      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_gdb_packet_decode(GGdbPacket *packet)
{
    bool result;                            /* Bilan à retourner           */
    char *buffer;                           /* Données transcrites         */
    size_t allocated;                       /* Quantité de données gérées  */
    size_t i;                               /* Boucle de parcours          */
    size_t k;                               /* Point d'insertion           */
    size_t repeat;                          /* Nombre de répétitions       */

    result = true;

    allocated = packet->len + 1;
    buffer = (char *)calloc(allocated, sizeof(char));

    for (i = 0, k = 0; i < packet->len && result; i++)
        switch (packet->buffer[i])
        {
            case '#':
            case '$':
                result = false;
                break;

            case '*':

                if (++i == packet->len || k == 0)
                {
                    result = false;
                    break;
                }

                repeat = packet->buffer[i] - ' ' + 3;

                allocated += repeat;
                buffer = (char *)realloc(buffer, allocated * sizeof(char));

                memset(&buffer[k], buffer[k - 1], repeat);
                k += repeat;

                break;

            case '}':

                if (++i == packet->len)
                {
                    result = false;
                    break;
                }

                buffer[k++] = packet->buffer[i] ^ 0x20;

                break;

            default:
                buffer[k++] = packet->buffer[i];
                break;

        }

    if (packet->buffer != NULL)
        free(packet->buffer);

    packet->buffer = buffer;
    packet->len = k;
    packet->allocated = allocated;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet   = paquet à analyser.                                *
*                data     = données contenues dans le paquet. [OUT]           *
*                len      = quantité de ces données. [OUT]                    *
*                checksum = contrôle d'intégrité des données ou NULL. [OUT]   *
*                                                                             *
*  Description : Fournit le contenu du paquet.                                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_gdb_packet_get_data(const GGdbPacket *packet, const char **data, size_t *len, uint8_t *checksum)
{
    *data = packet->buffer;
    *len = packet->len;

    if (checksum != NULL)
        *checksum = packet->checksum;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : list = liste de paquets à compléter.                         *
*                item = paquet à ajouter à la liste.                          *
*                                                                             *
*  Description : Ajoute un paquet à une liste de paquets.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_gdb_packet_push(GGdbPacket **list, GGdbPacket *item)
{
    dl_list_push(item, list, GGdbPacket, link);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : list = liste de paquets à consulter.                         *
*                                                                             *
*  Description : Retire et fournit le premier élément d'une liste de paquets. *
*                                                                             *
*  Retour      : Elément dépilé de la liste de paquets.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GGdbPacket *g_gdb_packet_pop(GGdbPacket **list)
{
    return dl_list_pop(list, GGdbPacket, link);

}