OctetString.c 1.03 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "TLVEncoder.h"
#include "TLVDecoder.h"
#include "OctetString.h"

int encode_octet_string(OctetString *octetstring, uint8_t *buffer, uint32_t buflen)
{
11
12
13
    CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, octetstring->length, buflen);
    memcpy((void*)buffer, (void*)octetstring->value, octetstring->length);
    return octetstring->length;
Cedric Roux's avatar
 
Cedric Roux committed
14
15
16
17
}

int decode_octet_string(OctetString *octetstring, uint16_t pdulen, uint8_t *buffer, uint32_t buflen)
{
18
19
20
21
22
23
24
    if (buflen < pdulen)
        return -1;
    octetstring->length = pdulen;
    octetstring->value = malloc(sizeof(uint8_t) * (pdulen+1));
    memcpy((void*)octetstring->value, (void*)buffer, pdulen);
    octetstring->value[pdulen] = '\0';
    return octetstring->length;
Cedric Roux's avatar
 
Cedric Roux committed
25
26
27
28
}

void dump_octet_string_xml(OctetString *octetstring)
{
29
30
31
32
33
    int i;
    printf("    <Length>%u</Length>\n    <values>", octetstring->length);
    for (i = 0; i < octetstring->length; i++)
        printf("0x%x ", octetstring->value[i]);
    printf("</values>\n");
Cedric Roux's avatar
 
Cedric Roux committed
34
35
}