File: variant-benc.c

package info (click to toggle)
transmission 3.00-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 33,160 kB
  • sloc: ansic: 81,055; objc: 18,449; cpp: 16,303; sh: 4,470; javascript: 4,281; makefile: 1,081; xml: 139
file content (393 lines) | stat: -rw-r--r-- 8,561 bytes parent folder | download | duplicates (2)
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
/*
 * This file Copyright (C) 2008-2014 Mnemosyne LLC
 *
 * It may be used under the GNU GPL versions 2 or 3
 * or any future license endorsed by Mnemosyne LLC.
 *
 */

#include <ctype.h> /* isdigit() */
#include <errno.h>
#include <stdlib.h> /* strtoul() */
#include <string.h> /* strlen(), memchr() */

#include <event2/buffer.h>

#include "ConvertUTF.h"

#define __LIBTRANSMISSION_VARIANT_MODULE__

#include "transmission.h"
#include "ptrarray.h"
#include "utils.h" /* tr_snprintf() */
#include "variant.h"
#include "variant-common.h"

#define MAX_BENC_STR_LENGTH (128 * 1024 * 1024) /* arbitrary */

/***
****  tr_variantParse()
****  tr_variantLoad()
***/

/**
 * The initial i and trailing e are beginning and ending delimiters.
 * You can have negative numbers such as i-3e. You cannot prefix the
 * number with a zero such as i04e. However, i0e is valid.
 * Example: i3e represents the integer "3"
 * NOTE: The maximum number of bit of this integer is unspecified,
 * but to handle it as a signed 64bit integer is mandatory to handle
 * "large files" aka .torrent for more that 4Gbyte
 */
int tr_bencParseInt(uint8_t const* buf, uint8_t const* bufend, uint8_t const** setme_end, int64_t* setme_val)
{
    char* endptr;
    void const* begin;
    void const* end;
    int64_t val;

    if (buf >= bufend)
    {
        return EILSEQ;
    }

    if (*buf != 'i')
    {
        return EILSEQ;
    }

    begin = buf + 1;
    end = memchr(begin, 'e', (bufend - buf) - 1);

    if (end == NULL)
    {
        return EILSEQ;
    }

    errno = 0;
    val = evutil_strtoll(begin, &endptr, 10);

    if (errno != 0 || endptr != end) /* incomplete parse */
    {
        return EILSEQ;
    }

    if (val != 0 && *(char const*)begin == '0') /* no leading zeroes! */
    {
        return EILSEQ;
    }

    *setme_end = (uint8_t const*)end + 1;
    *setme_val = val;
    return 0;
}

/**
 * Byte strings are encoded as follows:
 * <string length encoded in base ten ASCII>:<string data>
 * Note that there is no constant beginning delimiter, and no ending delimiter.
 * Example: 4:spam represents the string "spam"
 */
int tr_bencParseStr(uint8_t const* buf, uint8_t const* bufend, uint8_t const** setme_end, uint8_t const** setme_str,
    size_t* setme_strlen)
{
    void const* end;
    size_t len;
    char* ulend;
    uint8_t const* strbegin;
    uint8_t const* strend;

    if (buf >= bufend)
    {
        goto err;
    }

    if (!isdigit(*buf))
    {
        goto err;
    }

    end = memchr(buf, ':', bufend - buf);

    if (end == NULL)
    {
        goto err;
    }

    errno = 0;
    len = strtoul((char const*)buf, &ulend, 10);

    if (errno != 0 || ulend != end || len > MAX_BENC_STR_LENGTH)
    {
        goto err;
    }

    strbegin = (uint8_t const*)end + 1;
    strend = strbegin + len;

    if (strend < strbegin || strend > bufend)
    {
        goto err;
    }

    *setme_end = (uint8_t const*)end + 1 + len;
    *setme_str = (uint8_t const*)end + 1;
    *setme_strlen = len;
    return 0;

err:
    *setme_end = NULL;
    *setme_str = NULL;
    *setme_strlen = 0;
    return EILSEQ;
}

static tr_variant* get_node(tr_ptrArray* stack, tr_quark* key, tr_variant* top, int* err)
{
    tr_variant* node = NULL;

    if (tr_ptrArrayEmpty(stack))
    {
        node = top;
    }
    else
    {
        tr_variant* parent = tr_ptrArrayBack(stack);

        if (tr_variantIsList(parent))
        {
            node = tr_variantListAdd(parent);
        }
        else if (*key != 0 && tr_variantIsDict(parent))
        {
            node = tr_variantDictAdd(parent, *key);
            *key = 0;
        }
        else
        {
            *err = EILSEQ;
        }
    }

    return node;
}

/**
 * This function's previous recursive implementation was
 * easier to read, but was vulnerable to a smash-stacking
 * attack via maliciously-crafted bencoded data. (#667)
 */
int tr_variantParseBenc(void const* buf_in, void const* bufend_in, tr_variant* top, char const** setme_end)
{
    int err = 0;
    uint8_t const* buf = buf_in;
    uint8_t const* bufend = bufend_in;
    tr_ptrArray stack = TR_PTR_ARRAY_INIT;
    tr_quark key = 0;

    tr_variantInit(top, 0);

    while (buf != bufend)
    {
        if (buf > bufend) /* no more text to parse... */
        {
            err = EILSEQ;
        }

        if (err != 0)
        {
            break;
        }

        if (*buf == 'i') /* int */
        {
            int64_t val;
            uint8_t const* end;
            tr_variant* v;

            if ((err = tr_bencParseInt(buf, bufend, &end, &val)) != 0)
            {
                break;
            }

            buf = end;

            if ((v = get_node(&stack, &key, top, &err)) != NULL)
            {
                tr_variantInitInt(v, val);
            }
        }
        else if (*buf == 'l') /* list */
        {
            tr_variant* v;

            ++buf;

            if ((v = get_node(&stack, &key, top, &err)) != NULL)
            {
                tr_variantInitList(v, 0);
                tr_ptrArrayAppend(&stack, v);
            }
        }
        else if (*buf == 'd') /* dict */
        {
            tr_variant* v;

            ++buf;

            if ((v = get_node(&stack, &key, top, &err)) != NULL)
            {
                tr_variantInitDict(v, 0);
                tr_ptrArrayAppend(&stack, v);
            }
        }
        else if (*buf == 'e') /* end of list or dict */
        {
            ++buf;

            if (tr_ptrArrayEmpty(&stack) || key != 0)
            {
                err = EILSEQ;
                break;
            }
            else
            {
                tr_ptrArrayPop(&stack);

                if (tr_ptrArrayEmpty(&stack))
                {
                    break;
                }
            }
        }
        else if (isdigit(*buf)) /* string? */
        {
            tr_variant* v;
            uint8_t const* end;
            uint8_t const* str;
            size_t str_len;

            if ((err = tr_bencParseStr(buf, bufend, &end, &str, &str_len)) != 0)
            {
                break;
            }

            buf = end;

            if (key == 0 && !tr_ptrArrayEmpty(&stack) && tr_variantIsDict(tr_ptrArrayBack(&stack)))
            {
                key = tr_quark_new(str, str_len);
            }
            else if ((v = get_node(&stack, &key, top, &err)) != NULL)
            {
                tr_variantInitStr(v, str, str_len);
            }
        }
        else /* invalid bencoded text... march past it */
        {
            ++buf;
        }

        if (tr_ptrArrayEmpty(&stack))
        {
            break;
        }
    }

    if (err == 0 && (top->type == 0 || !tr_ptrArrayEmpty(&stack)))
    {
        err = EILSEQ;
    }

    if (err == 0)
    {
        if (setme_end != NULL)
        {
            *setme_end = (char const*)buf;
        }
    }
    else if (top->type != 0)
    {
        tr_variantFree(top);
        tr_variantInit(top, 0);
    }

    tr_ptrArrayDestruct(&stack, NULL);
    return err;
}

/****
*****
****/

static void saveIntFunc(tr_variant const* val, void* evbuf)
{
    evbuffer_add_printf(evbuf, "i%" PRId64 "e", val->val.i);
}

static void saveBoolFunc(tr_variant const* val, void* evbuf)
{
    if (val->val.b)
    {
        evbuffer_add(evbuf, "i1e", 3);
    }
    else
    {
        evbuffer_add(evbuf, "i0e", 3);
    }
}

static void saveRealFunc(tr_variant const* val, void* evbuf)
{
    int len;
    char buf[128];

    len = tr_snprintf(buf, sizeof(buf), "%f", val->val.d);
    evbuffer_add_printf(evbuf, "%d:", len);
    evbuffer_add(evbuf, buf, len);
}

static void saveStringFunc(tr_variant const* v, void* evbuf)
{
    size_t len;
    char const* str;
    if (!tr_variantGetStr(v, &str, &len))
    {
        len = 0;
        str = NULL;
    }

    evbuffer_add_printf(evbuf, "%zu:", len);
    evbuffer_add(evbuf, str, len);
}

static void saveDictBeginFunc(tr_variant const* val UNUSED, void* evbuf)
{
    evbuffer_add(evbuf, "d", 1);
}

static void saveListBeginFunc(tr_variant const* val UNUSED, void* evbuf)
{
    evbuffer_add(evbuf, "l", 1);
}

static void saveContainerEndFunc(tr_variant const* val UNUSED, void* evbuf)
{
    evbuffer_add(evbuf, "e", 1);
}

static struct VariantWalkFuncs const walk_funcs =
{
    saveIntFunc,
    saveBoolFunc,
    saveRealFunc,
    saveStringFunc,
    saveDictBeginFunc,
    saveListBeginFunc,
    saveContainerEndFunc
};

void tr_variantToBufBenc(tr_variant const* top, struct evbuffer* buf)
{
    tr_variantWalk(top, &walk_funcs, buf, true);
}