2
* Off-the-Record Messaging library
3
* Copyright (C) 2004-2008 Ian Goldberg, Chris Alexander, Nikita Borisov
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of version 2.1 of the GNU Lesser General
8
* Public License as published by the Free Software Foundation.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
/* Make a single TLV, copying the supplied data */
27
OtrlTLV *otrl_tlv_new(unsigned short type, unsigned short len,
28
const unsigned char *data)
30
OtrlTLV *tlv = malloc(sizeof(OtrlTLV));
34
tlv->data = malloc(len + 1);
35
assert(tlv->data != NULL);
36
memmove(tlv->data, data, len);
37
tlv->data[tlv->len] = '\0';
42
/* Construct a chain of TLVs from the given data */
43
OtrlTLV *otrl_tlv_parse(const unsigned char *serialized, size_t seriallen)
46
OtrlTLV **tlvp = &tlv;
47
while (seriallen >= 4) {
48
unsigned short type = (serialized[0] << 8) + serialized[1];
49
unsigned short len = (serialized[2] << 8) + serialized[3];
50
serialized += 4; seriallen -=4;
51
if (seriallen < len) break;
52
*tlvp = otrl_tlv_new(type, len, serialized);
55
tlvp = &((*tlvp)->next);
60
/* Deallocate a chain of TLVs */
61
void otrl_tlv_free(OtrlTLV *tlv)
64
OtrlTLV *next = tlv->next;
71
/* Find the serialized length of a chain of TLVs */
72
size_t otrl_tlv_seriallen(const OtrlTLV *tlv)
76
totlen += tlv->len + 4;
82
/* Serialize a chain of TLVs. The supplied buffer must already be large
84
void otrl_tlv_serialize(unsigned char *buf, const OtrlTLV *tlv)
87
buf[0] = (tlv->type >> 8) & 0xff;
88
buf[1] = tlv->type & 0xff;
89
buf[2] = (tlv->len >> 8) & 0xff;
90
buf[3] = tlv->len & 0xff;
92
memmove(buf, tlv->data, tlv->len);
98
/* Return the first TLV with the given type in the chain, or NULL if one
99
* isn't found. (The tlvs argument isn't const because the return type
100
* needs to be non-const.) */
101
OtrlTLV *otrl_tlv_find(OtrlTLV *tlvs, unsigned short type)
104
if (tlvs->type == type) return tlvs;