summaryrefslogtreecommitdiff
path: root/src/simpletlv.c
blob: bc2b91d740777bff0000090c866c94e0e2b610b5 (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
/*
 * simpletlv.c: Simple TLV encoding and decoding functions
 *
 * Copyright (C) 2016 - 2018 Red Hat, Inc.
 *
 * Authors: Robert Relyea <rrelyea@redhat.com>
 *          Jakub Jelen <jjelen@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include "simpletlv.h"
#include "common.h"

int
simpletlv_get_length(struct simpletlv_member *tlv, size_t tlv_len,
                     enum simpletlv_buffer_type buffer_type)
{
    size_t i, len = 0;
    int child_length;

    for (i = 0; i < tlv_len; i++) {
        /* This TLV is skipped */
        if (tlv[i].type == SIMPLETLV_TYPE_NONE)
            continue;

        /* We can not unambiguously split the buffers
         * for recursive structures
         */
        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND
            && buffer_type != SIMPLETLV_BOTH)
            return -1;

        child_length = tlv[i].length;
        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
            child_length = simpletlv_get_length(tlv[i].value.child,
                tlv[i].length, SIMPLETLV_BOTH);
        }
        if (buffer_type & SIMPLETLV_TL) {
            len += 1/*TAG*/;
            if (child_length < 255)
                len += 1;
            else
                len += 3;
        }
        if (buffer_type & SIMPLETLV_VALUE) {
            len += child_length;
        }
    }
    return len;
}

static int
simpletlv_encode_internal(struct simpletlv_member *tlv, size_t tlv_len,
                          unsigned char **out, size_t outlen,
                          unsigned char **newptr, int buffer_type)
{
    unsigned char *tmp = NULL, *a = NULL, *p = NULL, *newp = NULL;
    size_t tmp_len = 0, p_len, i;
    int expect_len = 0, rv;

    expect_len = simpletlv_get_length(tlv, tlv_len, buffer_type);
    if (expect_len == 0 && newptr != NULL && out != NULL)
        *newptr = *out; /* Corner case for zero-length values */
    if (expect_len <= 0)
        return expect_len;

    if (outlen == 0 && out != NULL) {
        /* allocate a new buffer */
        a = g_malloc(expect_len);
        tmp = a;
        tmp_len = expect_len;
    } else if ((int)outlen >= expect_len && out != NULL) {
        tmp = *out;
        tmp_len = outlen;
    } else {
        /* we can not fit the data */
        return -1;
    }
    p = tmp;
    p_len = tmp_len;
    for (i = 0; i < tlv_len; i++) {
        size_t child_length = tlv[i].length;

        /* This TLV is skipped */
        if (tlv[i].type == SIMPLETLV_TYPE_NONE)
            continue;

        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
            child_length = simpletlv_get_length(tlv[i].value.child,
                tlv[i].length, SIMPLETLV_BOTH);
        }
        if (buffer_type & SIMPLETLV_TL) {
            rv = simpletlv_put_tag(tlv[i].tag, child_length,
                p, p_len, &newp);
            if (rv < 0)
                goto failure;
            p = newp;
        }
        if (buffer_type & SIMPLETLV_VALUE) {
            if (tlv[i].type == SIMPLETLV_TYPE_LEAF) {
                memcpy(p, tlv[i].value.value, tlv[i].length);
                p += tlv[i].length;
            } else {
                /* recurse */
                rv = simpletlv_encode_internal(tlv[i].value.child,
                    tlv[i].length, &p, p_len, &newp, buffer_type);
                if (rv < 0)
                    goto failure;
                p = newp;
            }
        }
        p_len = tmp_len - (p - tmp);
    }
    if (newptr)
        *newptr = p;
    if (out)
        *out = tmp;
    return tmp_len - p_len;

failure:
    g_free(a);
    return -1;
}

int
simpletlv_encode(struct simpletlv_member *tlv, size_t tlv_len,
                 unsigned char **out, size_t outlen, unsigned char **newptr)
{
    return simpletlv_encode_internal(tlv, tlv_len, out, outlen, newptr,
        SIMPLETLV_BOTH);
}

int
simpletlv_encode_tl(struct simpletlv_member *tlv, size_t tlv_len,
                    unsigned char **out, size_t outlen, unsigned char **newptr)
{
    return simpletlv_encode_internal(tlv, tlv_len, out, outlen, newptr,
        SIMPLETLV_TL);
}

int
simpletlv_encode_val(struct simpletlv_member *tlv, size_t tlv_len,
                     unsigned char **out, size_t outlen, unsigned char **newptr)
{
    return simpletlv_encode_internal(tlv, tlv_len, out, outlen, newptr,
        SIMPLETLV_VALUE);
}


/*
 * Put a tag/length record to a file in Simple TLV based on the  datalen
 * content length.
 */
int
simpletlv_put_tag(unsigned char tag, size_t datalen, unsigned char *out,
                  size_t outlen, unsigned char **ptr)
{
    unsigned char *p = out;

    if (outlen < 2 || (outlen < 4 && datalen >= 0xff))
        return -1;

    /* tag is just number between 0x01 and 0xFE */
    if (tag == 0x00 || tag == 0xff)
        return -1;

    *p++ = tag; /* tag is single byte */
    if (datalen < 0xff) {
        /* short value up to 255 */
        *p++ = (unsigned char)datalen; /* is in the second byte */
    } else if (datalen < 0xffff) {
        /* longer values up to 65535 */
        *p++ = (unsigned char)0xff; /* first byte is 0xff */
        *p++ = (unsigned char)datalen & 0xff;
        *p++ = (unsigned char)(datalen >> 8) & 0xff; /* LE */
    } else {
        /* we can't store more than two bytes in Simple TLV */
        return -1;
    }
    if (ptr != NULL)
        *ptr = p;
    return 0;
}

/* Read the TL file and return appropriate tag and the length of associated
 * content.
 */
int
simpletlv_read_tag(unsigned char **buf, size_t buflen, unsigned char *tag_out,
                   size_t *taglen)
{
    size_t len;
    unsigned char *p = *buf;

    if (buflen < 2) {
        *buf = p+buflen;
        return -1;
    }

    *tag_out = *p++;
    len = *p++;
    if (len == 0xff) {
        /* don't crash on bad data */
        if (buflen < 4) {
            *taglen = 0;
            return -1;
        }
        /* skip two bytes (the size) */
        len = lebytes2ushort(p);
        p+=2;
    }
    *taglen = len;
    *buf = p;
    return 0;
}

/*
 * Merges two structures into one, creating a new shallow copy of both
 * of the structures.
 * Resulting length is the sum of  a_len  and  b_len  arguments.
 */
struct simpletlv_member *
simpletlv_merge(const struct simpletlv_member *a, size_t a_len,
                const struct simpletlv_member *b, size_t b_len)
{
    int offset;
    struct simpletlv_member *r;
    size_t r_len = a_len + b_len;

    r = g_new(struct simpletlv_member, r_len);

    /* the ugly way */
    offset = a_len * sizeof(struct simpletlv_member);
    memcpy(r, a, offset);
    memcpy(&r[a_len], b, b_len * sizeof(struct simpletlv_member));
    return r;
}

void
simpletlv_free(struct simpletlv_member *tlv, size_t tlvlen)
{
    size_t i;
    if (tlv == NULL)
        return;

    for (i = 0; i < tlvlen; i++) {
        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
            simpletlv_free(tlv[i].value.child, tlv[i].length);
        } else {
            g_free(tlv[i].value.value);
        }
    }
    g_free(tlv);
}

struct simpletlv_member *
simpletlv_clone(struct simpletlv_member *tlv, size_t tlvlen)
{
    size_t i = 0, j;
    struct simpletlv_member *new = NULL;

    new = g_new(struct simpletlv_member, tlvlen);

    for (i = 0; i < tlvlen; i++) {
        new[i].type = tlv[i].type;
        new[i].tag = tlv[i].tag;
        new[i].length = tlv[i].length;
        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
            new[i].value.child = simpletlv_clone(
                tlv[i].value.child, tlv[i].length);
            if (new[i].value.child == NULL)
                goto failure;
        } else {
            new[i].value.value = g_memdup2(tlv[i].value.value, tlv[i].length);
        }
    }
    return new;

failure:
    for (j = 0; j < i; j++) {
        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
            simpletlv_free(new[j].value.child, new[j].length);
        } else {
            g_free(new[j].value.value);
        }
    }
    g_free(new);
    return NULL;
}

struct simpletlv_member *
simpletlv_parse(unsigned char *data, size_t data_len, size_t *outtlv_len)
{
    unsigned char *p, *p_end;
    unsigned char tag;
    size_t vlen;
    GArray *tlv = g_array_new(FALSE, FALSE, sizeof(struct simpletlv_member));

    p = data;
    p_end = p + data_len;
    while (p < p_end) {
        struct simpletlv_member tlvp;

        /* we can return what was parsed successfully */
        if (simpletlv_read_tag(&p, p_end - p, &tag, &vlen) < 0) {
            break;
        }
        if (vlen > (size_t) (p_end - p)) {
            break;
        }

        tlvp.tag = tag;
        tlvp.length = vlen;
        tlvp.value.value = g_memdup2(p, vlen);
        tlvp.type = SIMPLETLV_TYPE_LEAF;
        g_array_append_val(tlv, tlvp);

        p += vlen;
    }

    *outtlv_len = tlv->len;
    return (struct simpletlv_member *)(void *)g_array_free(tlv, FALSE);
}

/* vim: set ts=4 sw=4 tw=0 noet expandtab: */