~ubuntu-branches/ubuntu/trusty/libotr2/trusty

« back to all changes in this revision

Viewing changes to src/tlv.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2012-11-05 01:06:26 UTC
  • Revision ID: package-import@ubuntu.com-20121105010626-4m3hldpaz3jfr3wb
Tags: upstream-3.2.1
ImportĀ upstreamĀ versionĀ 3.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Off-the-Record Messaging library
 
3
 *  Copyright (C) 2004-2008  Ian Goldberg, Chris Alexander, Nikita Borisov
 
4
 *                           <otr@cypherpunks.ca>
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <assert.h>
 
23
 
 
24
#include "tlv.h"
 
25
 
 
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)
 
29
{
 
30
    OtrlTLV *tlv = malloc(sizeof(OtrlTLV));
 
31
    assert(tlv != NULL);
 
32
    tlv->type = type;
 
33
    tlv->len = len;
 
34
    tlv->data = malloc(len + 1);
 
35
    assert(tlv->data != NULL);
 
36
    memmove(tlv->data, data, len);
 
37
    tlv->data[tlv->len] = '\0';
 
38
    tlv->next = NULL;
 
39
    return tlv;
 
40
}
 
41
 
 
42
/* Construct a chain of TLVs from the given data */
 
43
OtrlTLV *otrl_tlv_parse(const unsigned char *serialized, size_t seriallen)
 
44
{
 
45
    OtrlTLV *tlv = NULL;
 
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);
 
53
        serialized += len;
 
54
        seriallen -= len;
 
55
        tlvp = &((*tlvp)->next);
 
56
    }
 
57
    return tlv;
 
58
}
 
59
 
 
60
/* Deallocate a chain of TLVs */
 
61
void otrl_tlv_free(OtrlTLV *tlv)
 
62
{
 
63
    while (tlv) {
 
64
        OtrlTLV *next = tlv->next;
 
65
        free(tlv->data);
 
66
        free(tlv);
 
67
        tlv = next;
 
68
    }
 
69
}
 
70
 
 
71
/* Find the serialized length of a chain of TLVs */
 
72
size_t otrl_tlv_seriallen(const OtrlTLV *tlv)
 
73
{
 
74
    size_t totlen = 0;
 
75
    while (tlv) {
 
76
        totlen += tlv->len + 4;
 
77
        tlv = tlv->next;
 
78
    }
 
79
    return totlen;
 
80
}
 
81
 
 
82
/* Serialize a chain of TLVs.  The supplied buffer must already be large
 
83
 * enough. */
 
84
void otrl_tlv_serialize(unsigned char *buf, const OtrlTLV *tlv)
 
85
{
 
86
    while (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;
 
91
        buf += 4;
 
92
        memmove(buf, tlv->data, tlv->len);
 
93
        buf += tlv->len;
 
94
        tlv = tlv->next;
 
95
    }
 
96
}
 
97
 
 
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)
 
102
{
 
103
    while (tlvs) {
 
104
        if (tlvs->type == type) return tlvs;
 
105
        tlvs = tlvs->next;
 
106
    }
 
107
    return NULL;
 
108
}