daf4620534ee7b5b2dc9b5b1e30892b20b2b8ef3
[deliverable/titan.core.git] / core / ASN_Any.cc
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
8 #include "ASN_Any.hh"
9 #include "String_struct.hh"
10 #include <string.h>
11 #include "../common/memory.h"
12
13 void ASN_ANY::encode(const TTCN_Typedescriptor_t& p_td,
14 TTCN_Buffer& p_buf,
15 TTCN_EncDec::coding_t p_coding, ...) const
16 {
17 va_list pvar;
18 va_start(pvar, p_coding);
19 switch(p_coding) {
20 case TTCN_EncDec::CT_BER: {
21 TTCN_EncDec_ErrorContext ec("While BER-encoding type '%s': ", p_td.name);
22 unsigned BER_coding=va_arg(pvar, unsigned);
23 BER_encode_chk_coding(BER_coding);
24 ASN_BER_TLV_t *tlv=BER_encode_TLV(p_td, BER_coding);
25 tlv->put_in_buffer(p_buf);
26 ASN_BER_TLV_t::destruct(tlv);
27 break;}
28 case TTCN_EncDec::CT_RAW:
29 default:
30 TTCN_error("Unknown coding method requested to encode type '%s'",
31 p_td.name);
32 }
33 va_end(pvar);
34 }
35
36 void ASN_ANY::decode(const TTCN_Typedescriptor_t& p_td,
37 TTCN_Buffer& p_buf,
38 TTCN_EncDec::coding_t p_coding, ...)
39 {
40 va_list pvar;
41 va_start(pvar, p_coding);
42 switch(p_coding) {
43 case TTCN_EncDec::CT_BER: {
44 TTCN_EncDec_ErrorContext ec("While BER-decoding type '%s': ", p_td.name);
45 unsigned L_form=va_arg(pvar, unsigned);
46 ASN_BER_TLV_t tlv;
47 BER_decode_str2TLV(p_buf, tlv, L_form);
48 BER_decode_TLV(p_td, tlv, L_form);
49 if(tlv.isComplete) p_buf.increase_pos(tlv.get_len());
50 break;}
51 case TTCN_EncDec::CT_RAW:
52 default:
53 TTCN_error("Unknown coding method requested to decode type '%s'",
54 p_td.name);
55 }
56 va_end(pvar);
57 }
58
59 ASN_BER_TLV_t*
60 ASN_ANY::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
61 unsigned p_coding) const
62 {
63 BER_chk_descr(p_td);
64 ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());
65 if(new_tlv) return new_tlv;
66 ASN_BER_TLV_t *tmp_tlv=ASN_BER_TLV_t::construct(0, NULL);
67 {
68 TTCN_EncDec_ErrorContext ec("While checking ANY value: ");
69 if(!ASN_BER_str2TLV(val_ptr->n_octets, val_ptr->octets_ptr,
70 *tmp_tlv, BER_ACCEPT_ALL)
71 || tmp_tlv->get_len()!=static_cast<size_t>(val_ptr->n_octets))
72 TTCN_EncDec_ErrorContext::error
73 (TTCN_EncDec::ET_INCOMPL_ANY,
74 "The content of an ASN ANY value must be a valid, complete TLV.");
75 }
76 new_tlv=ASN_BER_TLV_t::construct(0, NULL);
77 *new_tlv=*tmp_tlv;
78 new_tlv->Tstr=(unsigned char*)Malloc(new_tlv->Tlen);
79 new_tlv->Lstr=(unsigned char*)Malloc(new_tlv->Llen);
80 new_tlv->V.str.Vstr=(unsigned char*)Malloc(new_tlv->V.str.Vlen);
81 memcpy(new_tlv->Tstr, tmp_tlv->Tstr, new_tlv->Tlen);
82 memcpy(new_tlv->Lstr, tmp_tlv->Lstr, new_tlv->Llen);
83 memcpy(new_tlv->V.str.Vstr, tmp_tlv->V.str.Vstr, new_tlv->V.str.Vlen);
84 Free(tmp_tlv);
85 new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);
86 return new_tlv;
87 }
88
89 boolean ASN_ANY::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
90 const ASN_BER_TLV_t& p_tlv,
91 unsigned L_form)
92 {
93 clean_up();
94 BER_chk_descr(p_td);
95 ASN_BER_TLV_t stripped_tlv;
96 BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);
97 TTCN_EncDec_ErrorContext ec("While decoding ASN ANY type: ");
98 if(stripped_tlv.V_tlvs_selected)
99 TTCN_EncDec_ErrorContext::error_internal
100 ("In ASN_ANY::BER_decode_TLV().");
101 size_t pos=0;
102 if(p_td.ber->n_tags>0) {
103 stripped_tlv.Tlen=0;
104 stripped_tlv.Llen=0;
105 }
106 init_struct(stripped_tlv.get_len());
107 memcpy(val_ptr->octets_ptr+pos, stripped_tlv.Tstr, stripped_tlv.Tlen);
108 pos+=stripped_tlv.Tlen;
109 memcpy(val_ptr->octets_ptr+pos, stripped_tlv.Lstr, stripped_tlv.Llen);
110 pos+=stripped_tlv.Llen;
111 memcpy(val_ptr->octets_ptr+pos,
112 stripped_tlv.V.str.Vstr, stripped_tlv.V.str.Vlen);
113 return TRUE;
114 }
This page took 0.050412 seconds and 4 git commands to generate.