summaryrefslogtreecommitdiff
path: root/libotr-3.2.0/src/tlv.c
blob: 0cea7b5fa278aa6356d5d0c55a842e018830638b (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
/*
 *  Off-the-Record Messaging library
 *  Copyright (C) 2004-2008  Ian Goldberg, Chris Alexander, Nikita Borisov
 *                           <otr@cypherpunks.ca>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of version 2.1 of the GNU Lesser General
 *  Public License as published by the Free Software Foundation.
 *
 *  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 <stdlib.h>
#include <string.h>
#include <assert.h>

#include "tlv.h"

/* Make a single TLV, copying the supplied data */
OtrlTLV *otrl_tlv_new(unsigned short type, unsigned short len,
	const unsigned char *data)
{
    OtrlTLV *tlv = malloc(sizeof(OtrlTLV));
    assert(tlv != NULL);
    tlv->type = type;
    tlv->len = len;
    tlv->data = malloc(len + 1);
    assert(tlv->data != NULL);
    memmove(tlv->data, data, len);
    tlv->data[tlv->len] = '\0';
    tlv->next = NULL;
    return tlv;
}

/* Construct a chain of TLVs from the given data */
OtrlTLV *otrl_tlv_parse(const unsigned char *serialized, size_t seriallen)
{
    OtrlTLV *tlv = NULL;
    OtrlTLV **tlvp = &tlv;
    while (seriallen >= 4) {
	unsigned short type = (serialized[0] << 8) + serialized[1];
	unsigned short len = (serialized[2] << 8) + serialized[3];
	serialized += 4; seriallen -=4;
	if (seriallen < len) break;
	*tlvp = otrl_tlv_new(type, len, serialized);
	serialized += len;
	seriallen -= len;
	tlvp = &((*tlvp)->next);
    }
    return tlv;
}

/* Deallocate a chain of TLVs */
void otrl_tlv_free(OtrlTLV *tlv)
{
    while (tlv) {
	OtrlTLV *next = tlv->next;
	free(tlv->data);
	free(tlv);
	tlv = next;
    }
}

/* Find the serialized length of a chain of TLVs */
size_t otrl_tlv_seriallen(const OtrlTLV *tlv)
{
    size_t totlen = 0;
    while (tlv) {
	totlen += tlv->len + 4;
	tlv = tlv->next;
    }
    return totlen;
}

/* Serialize a chain of TLVs.  The supplied buffer must already be large
 * enough. */
void otrl_tlv_serialize(unsigned char *buf, const OtrlTLV *tlv)
{
    while (tlv) {
	buf[0] = (tlv->type >> 8) & 0xff;
	buf[1] = tlv->type & 0xff;
	buf[2] = (tlv->len >> 8) & 0xff;
	buf[3] = tlv->len & 0xff;
	buf += 4;
	memmove(buf, tlv->data, tlv->len);
	buf += tlv->len;
	tlv = tlv->next;
    }
}

/* Return the first TLV with the given type in the chain, or NULL if one
 * isn't found.  (The tlvs argument isn't const because the return type
 * needs to be non-const.) */
OtrlTLV *otrl_tlv_find(OtrlTLV *tlvs, unsigned short type)
{
    while (tlvs) {
	if (tlvs->type == type) return tlvs;
	tlvs = tlvs->next;
    }
    return NULL;
}