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 ///////////////////////////////////////////////////////////////////////////////
9 #include "String_struct.hh"
11 #include "../common/memory.h"
13 void ASN_ANY::encode(const TTCN_Typedescriptor_t
& p_td
,
15 TTCN_EncDec::coding_t p_coding
, ...) const
18 va_start(pvar
, 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
);
28 case TTCN_EncDec::CT_RAW
:
30 TTCN_error("Unknown coding method requested to encode type '%s'",
36 void ASN_ANY::decode(const TTCN_Typedescriptor_t
& p_td
,
38 TTCN_EncDec::coding_t p_coding
, ...)
41 va_start(pvar
, 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);
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());
51 case TTCN_EncDec::CT_RAW
:
53 TTCN_error("Unknown coding method requested to decode type '%s'",
60 ASN_ANY::BER_encode_TLV(const TTCN_Typedescriptor_t
& p_td
,
61 unsigned p_coding
) const
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
);
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.");
76 new_tlv
=ASN_BER_TLV_t::construct(0, NULL
);
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
);
85 new_tlv
=ASN_BER_V2TLV(new_tlv
, p_td
, p_coding
);
89 boolean
ASN_ANY::BER_decode_TLV(const TTCN_Typedescriptor_t
& p_td
,
90 const ASN_BER_TLV_t
& p_tlv
,
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().");
102 if(p_td
.ber
->n_tags
>0) {
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
);