ASN_Any.cc 5.19 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Forstner, Matyas
 *   Szabados, Kristof
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
17
18
19
20
21
22
23
#include "ASN_Any.hh"
#include "String_struct.hh"
#include <string.h>
#include "../common/memory.h"

void ASN_ANY::encode(const TTCN_Typedescriptor_t& p_td,
                     TTCN_Buffer& p_buf,
24
                     int p_coding, ...) const
Elemer Lelik's avatar
Elemer Lelik committed
25
26
27
28
29
30
31
32
33
34
35
36
{
  va_list pvar;
  va_start(pvar, p_coding);
  switch(p_coding) {
  case TTCN_EncDec::CT_BER: {
    TTCN_EncDec_ErrorContext ec("While BER-encoding type '%s': ", p_td.name);
    unsigned BER_coding=va_arg(pvar, unsigned);
    BER_encode_chk_coding(BER_coding);
    ASN_BER_TLV_t *tlv=BER_encode_TLV(p_td, BER_coding);
    tlv->put_in_buffer(p_buf);
    ASN_BER_TLV_t::destruct(tlv);
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
37
38
39
40
41
42
  case TTCN_EncDec::CT_JSON: {
    TTCN_EncDec_ErrorContext ec("While JSON-encoding type '%s': ", p_td.name);
    if(!p_td.json)
      TTCN_EncDec_ErrorContext::error_internal
        ("No JSON descriptor available for type '%s'.", p_td.name);
    JSON_Tokenizer tok(va_arg(pvar, int) != 0);
43
    JSON_encode(p_td, tok, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
44
45
    p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
46
47
48
49
50
51
52
53
54
55
  case TTCN_EncDec::CT_RAW:
  default:
    TTCN_error("Unknown coding method requested to encode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void ASN_ANY::decode(const TTCN_Typedescriptor_t& p_td,
                     TTCN_Buffer& p_buf,
56
                     int p_coding, ...)
Elemer Lelik's avatar
Elemer Lelik committed
57
58
{
  va_list pvar;
59
  va_start(pvar, p_coding);
Elemer Lelik's avatar
Elemer Lelik committed
60
61
62
63
64
65
66
67
68
  switch(p_coding) {
  case TTCN_EncDec::CT_BER: {
    TTCN_EncDec_ErrorContext ec("While BER-decoding type '%s': ", p_td.name);
    unsigned L_form=va_arg(pvar, unsigned);
    ASN_BER_TLV_t tlv;
    BER_decode_str2TLV(p_buf, tlv, L_form);
    BER_decode_TLV(p_td, tlv, L_form);
    if(tlv.isComplete) p_buf.increase_pos(tlv.get_len());
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
69
70
71
72
73
74
  case TTCN_EncDec::CT_JSON: {
    TTCN_EncDec_ErrorContext ec("While JSON-decoding type '%s': ", p_td.name);
    if(!p_td.json)
      TTCN_EncDec_ErrorContext::error_internal
        ("No JSON descriptor available for type '%s'.", p_td.name);
    JSON_Tokenizer tok((const char*)p_buf.get_data(), p_buf.get_len());
75
    if(JSON_decode(p_td, tok, FALSE, FALSE)<0)
Elemer Lelik's avatar
Elemer Lelik committed
76
77
78
79
80
81
      ec.error(TTCN_EncDec::ET_INCOMPL_MSG,
               "Can not decode type '%s', because invalid or incomplete"
               " message was received"
               , p_td.name);
    p_buf.set_pos(tok.get_buf_pos());
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  case TTCN_EncDec::CT_RAW:
  default:
    TTCN_error("Unknown coding method requested to decode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

ASN_BER_TLV_t*
ASN_ANY::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
                        unsigned p_coding) const
{
  BER_chk_descr(p_td);
  ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());
  if(new_tlv) return new_tlv;
  ASN_BER_TLV_t *tmp_tlv=ASN_BER_TLV_t::construct(0, NULL);
  {
    TTCN_EncDec_ErrorContext ec("While checking ANY value: ");
    if(!ASN_BER_str2TLV(val_ptr->n_octets, val_ptr->octets_ptr,
                        *tmp_tlv, BER_ACCEPT_ALL)
       || tmp_tlv->get_len()!=static_cast<size_t>(val_ptr->n_octets))
      TTCN_EncDec_ErrorContext::error
        (TTCN_EncDec::ET_INCOMPL_ANY,
         "The content of an ASN ANY value must be a valid, complete TLV.");
  }
  new_tlv=ASN_BER_TLV_t::construct(0, NULL);
  *new_tlv=*tmp_tlv;
  new_tlv->Tstr=(unsigned char*)Malloc(new_tlv->Tlen);
  new_tlv->Lstr=(unsigned char*)Malloc(new_tlv->Llen);
  new_tlv->V.str.Vstr=(unsigned char*)Malloc(new_tlv->V.str.Vlen);
  memcpy(new_tlv->Tstr, tmp_tlv->Tstr, new_tlv->Tlen);
  memcpy(new_tlv->Lstr, tmp_tlv->Lstr, new_tlv->Llen);
  memcpy(new_tlv->V.str.Vstr, tmp_tlv->V.str.Vstr, new_tlv->V.str.Vlen);
  Free(tmp_tlv);
  new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);
  return new_tlv;
}

boolean ASN_ANY::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
                                const ASN_BER_TLV_t& p_tlv,
                                unsigned L_form)
{
  clean_up();
  BER_chk_descr(p_td);
  ASN_BER_TLV_t stripped_tlv;
  BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);
  TTCN_EncDec_ErrorContext ec("While decoding ASN ANY type: ");
  if(stripped_tlv.V_tlvs_selected)
    TTCN_EncDec_ErrorContext::error_internal
      ("In ASN_ANY::BER_decode_TLV().");
  size_t pos=0;
  if(p_td.ber->n_tags>0) {
    stripped_tlv.Tlen=0;
    stripped_tlv.Llen=0;
  }
  init_struct(stripped_tlv.get_len());
  memcpy(val_ptr->octets_ptr+pos, stripped_tlv.Tstr, stripped_tlv.Tlen);
  pos+=stripped_tlv.Tlen;
  memcpy(val_ptr->octets_ptr+pos, stripped_tlv.Lstr, stripped_tlv.Llen);
  pos+=stripped_tlv.Llen;
  memcpy(val_ptr->octets_ptr+pos,
         stripped_tlv.V.str.Vstr, stripped_tlv.V.str.Vlen);
  return TRUE;
}