Basetype.cc 41.5 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
17
18
19
20
21
22
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Feher, Csaba
 *   Forstner, Matyas
 *   Kovacs, Ferenc
 *   Ormandi, Matyas
 *   Raduly, Csaba
 *   Szabo, Bence Janos
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
23
24
25
26
27
28
29
30
31
#include "Basetype.hh"
#include "../common/memory.h"
#include "Logger.hh"
#include "Error.hh"
#include "BER.hh"
#include "RAW.hh"
#include "TEXT.hh"
#include "XER.hh"
#include "JSON.hh"
32
#include "OER.hh"
Elemer Lelik's avatar
Elemer Lelik committed
33
34
#include "XmlReader.hh"
#include "Module_list.hh"
Elemer Lelik's avatar
Elemer Lelik committed
35
#include "Universal_charstring.hh"
Elemer Lelik's avatar
Elemer Lelik committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <openssl/bn.h>

#include <stdarg.h>
#include <string.h>
#include <limits.h>
// Note: RT2-only classes (Record_Type, Record_Of_Type) and Base_Type methods
// are in core2/Basetype2.cc

boolean Base_Type::ispresent() const
{
  TTCN_error("Base_Type::ispresent(): calling ispresent() on a non-optional field.");
  return TRUE;
}

void Base_Type::log() const
{
  TTCN_Logger::log_event_str("<logging of this type is not implemented>");
}

void Base_Type::encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
57
                       int p_coding, ...) const
Elemer Lelik's avatar
Elemer Lelik committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{
  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;}
  case TTCN_EncDec::CT_RAW: {
    TTCN_EncDec_ErrorContext ec("While RAW-encoding type '%s': ", p_td.name);
    if(!p_td.raw)
      TTCN_EncDec_ErrorContext::error_internal
        ("No RAW descriptor available for type '%s'.", p_td.name);
    RAW_enc_tr_pos rp;
    rp.level=0;
    rp.pos=NULL;
    RAW_enc_tree root(TRUE, NULL, &rp, 1, p_td.raw);
    RAW_encode(p_td, root);
    root.put_to_buf(p_buf);
    break;}
  case TTCN_EncDec::CT_TEXT: {
    TTCN_EncDec_ErrorContext ec("While TEXT-encoding type '%s': ", p_td.name);
    if(!p_td.text)
      TTCN_EncDec_ErrorContext::error_internal
        ("No TEXT descriptor available for type '%s'.", p_td.name);
    TEXT_encode(p_td,p_buf);
    break;}
  case TTCN_EncDec::CT_XER: {
    TTCN_EncDec_ErrorContext ec("While XER-encoding type '%s': ", p_td.name);
    if(!p_td.xer) TTCN_EncDec_ErrorContext::error_internal(
      "No XER descriptor available for type '%s'.", p_td.name);
    unsigned XER_coding=va_arg(pvar, unsigned);
94
    XER_encode(*(p_td.xer),p_buf, XER_coding, 0, 0, 0);
Elemer Lelik's avatar
Elemer Lelik committed
95
96
97
98
99
100
101
102
    p_buf.put_c('\n');
    break;}
  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);
103
    JSON_encode(p_td, tok, FALSE);
Elemer Lelik's avatar
Elemer Lelik committed
104
105
    p_buf.put_s(tok.get_buffer_length(), (const unsigned char*)tok.get_buffer());
    break;}
106
107
108
109
110
111
  case TTCN_EncDec::CT_OER: {
    TTCN_EncDec_ErrorContext ec("While OER-encoding type '%s': ", p_td.name);
    if(!p_td.oer)  TTCN_EncDec_ErrorContext::error_internal(
      "No OER descriptor available for type '%s'.", p_td.name);
    OER_encode(p_td, p_buf);
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
112
113
114
115
116
117
118
119
  default:
    TTCN_error("Unknown coding method requested to encode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void Base_Type::decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
120
                       int p_coding, ...)
Elemer Lelik's avatar
Elemer Lelik committed
121
122
{
  va_list pvar;
123
  va_start(pvar, p_coding);
Elemer Lelik's avatar
Elemer Lelik committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  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;}
  case TTCN_EncDec::CT_RAW: {
    TTCN_EncDec_ErrorContext ec("While RAW-decoding type '%s': ", p_td.name);
    if(!p_td.raw)
      TTCN_EncDec_ErrorContext::error_internal
        ("No RAW descriptor available for type '%s'.", p_td.name);
    raw_order_t order;
    switch(p_td.raw->top_bit_order) {
    case TOP_BIT_LEFT:
      order=ORDER_LSB;
      break;
    case TOP_BIT_RIGHT:
    default:
      order=ORDER_MSB;
    }
    RAW_decode(p_td, p_buf, p_buf.get_len()*8, order);
    break;}
  case TTCN_EncDec::CT_TEXT: {
    Limit_Token_List limit;
    TTCN_EncDec_ErrorContext ec("While TEXT-decoding type '%s': ", p_td.name);
    if(!p_td.text)
      TTCN_EncDec_ErrorContext::error_internal
        ("No TEXT descriptor available for type '%s'.", p_td.name);
    const unsigned char *b=p_buf.get_data();
156
    int null_added=0;
Elemer Lelik's avatar
Elemer Lelik committed
157
    if(b[p_buf.get_len()-1]!='\0'){
158
      null_added=1;
Elemer Lelik's avatar
Elemer Lelik committed
159
160
161
162
163
164
165
166
167
      p_buf.set_pos(p_buf.get_len());
      p_buf.put_zero(8,ORDER_LSB);
      p_buf.rewind();
    }
    if(TEXT_decode(p_td,p_buf,limit)<0)
      ec.error(TTCN_EncDec::ET_INCOMPL_MSG,
               "Can not decode type '%s', because invalid or incomplete"
               " message was received"
               , p_td.name);
168
169
170
171
172
173
    if(null_added){
      size_t actpos=p_buf.get_pos();
      p_buf.set_pos(p_buf.get_len()-1);
      p_buf.cut_end();
      p_buf.set_pos(actpos);
    }
Elemer Lelik's avatar
Elemer Lelik committed
174
175
176
177
178
179
180
181
    break;}
  case TTCN_EncDec::CT_XER: {
    TTCN_EncDec_ErrorContext ec("While XER-decoding type '%s': ", p_td.name);
    unsigned XER_coding=va_arg(pvar, unsigned);
    XmlReaderWrap reader(p_buf);
    for (int success=reader.Read(); success==1; success=reader.Read()) {
      if (reader.NodeType() == XML_READER_TYPE_ELEMENT) break;
    }
Elemer Lelik's avatar
Elemer Lelik committed
182
    XER_decode(*(p_td.xer), reader, XER_coding, XER_NONE, 0);
Elemer Lelik's avatar
Elemer Lelik committed
183
184
185
186
187
188
189
190
191
    size_t bytes = reader.ByteConsumed();
    p_buf.set_pos(bytes);
    break;}
  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());
192
    if(JSON_decode(p_td, tok, FALSE, FALSE)<0)
Elemer Lelik's avatar
Elemer Lelik committed
193
194
195
196
197
198
      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;}
199
200
201
202
  case TTCN_EncDec::CT_OER: {
    TTCN_EncDec_ErrorContext ec("While OER-decoding type '%s': ", p_td.name);
    if(!p_td.oer)  TTCN_EncDec_ErrorContext::error_internal(
      "No OER descriptor available for type '%s'.", p_td.name);
203
204
    OER_struct p_oer;
    OER_decode(p_td, p_buf, p_oer);
205
    break;}
Elemer Lelik's avatar
Elemer Lelik committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  default:
    TTCN_error("Unknown coding method requested to decode type '%s'",
               p_td.name);
  }
  va_end(pvar);
}

void Base_Type::BER_chk_descr(const TTCN_Typedescriptor_t& p_td)
{
  if(!p_td.ber)
    TTCN_EncDec_ErrorContext::error_internal
      ("No BER descriptor available for type '%s'.", p_td.name);
}

void Base_Type::BER_encode_chk_coding(unsigned& p_coding)
{
  switch(p_coding) {
  case BER_ENCODE_CER:
  case BER_ENCODE_DER:
    break;
  default:
    TTCN_warning("Unknown BER encoding requested; using DER.");
    p_coding=BER_ENCODE_DER;
    break;
  }
}

void Base_Type::XER_encode_chk_coding(unsigned& p_coding,
  const TTCN_Typedescriptor_t& p_td)
{
  if (!p_td.xer) {
    TTCN_EncDec_ErrorContext::error_internal
      ("No XER descriptor available for type '%s'.", p_td.name);
  }
  switch (p_coding) {
  case XER_BASIC:
242
  case XER_BASIC | XER_CANONICAL:
Elemer Lelik's avatar
Elemer Lelik committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  case XER_CANONICAL:
  case XER_EXTENDED:
  case XER_EXTENDED | XER_CANONICAL:
    break;
  default:
    TTCN_warning("Unknown XER encoding requested; using Basic XER.");
    p_coding = XER_BASIC;
    break;
  }
}

static const cbyte empty_tag_end[4] = "/>\n";

int Base_Type::begin_xml(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
257
  unsigned int& flavor, int indent, boolean empty,
258
  collector_fn collector, const char *type_atr, unsigned int flavor2) const
Elemer Lelik's avatar
Elemer Lelik committed
259
260
{
  const int exer = is_exer(flavor);
261
262
263
  int omit_tag =
    // can never omit the tag at the toplevel, except when the type is union
    (indent != 0 || (flavor2 & THIS_UNION))
Elemer Lelik's avatar
Elemer Lelik committed
264
265
266
267
268
269
270
271
272
273
    && ( ((flavor & XER_RECOF) // can remove the tag even if not EXER
      && !(exer && (flavor & BXER_EMPTY_ELEM))) // except 26.6, 26.7
      || (exer /*&& */
        && ( (p_td.xer_bits & (UNTAGGED|ANY_ATTRIBUTES|ANY_ELEMENT))
          || (flavor & (EMBED_VALUES|XER_LIST|ANY_ATTRIBUTES|USE_NIL|USE_TYPE_ATTR)))));

  if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
    begin_attribute(p_td, p_buf);
  }
  else if (!omit_tag) { // full tag
274
    const int indenting = !is_canonical(flavor);
Elemer Lelik's avatar
Elemer Lelik committed
275
276
277
278
    if (indenting) do_indent(p_buf, indent);
    p_buf.put_c('<');
    if (exer) write_ns_prefix(p_td, p_buf);

279
    boolean namespaces_needed = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
280
    if (exer) {
281
      const namespace_t *ns_info = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
282
      if (p_td.my_module != NULL && p_td.ns_index != -1) {
283
        ns_info = p_td.my_module->get_ns((size_t)p_td.ns_index);
Elemer Lelik's avatar
Elemer Lelik committed
284
285
286
287
288
289
290
291
292
293
294
      }

      namespaces_needed = exer && //!(p_td.xer_bits & FORM_UNQUALIFIED) &&
        (indent==0 // top-level type
          || (ns_info && *ns_info->px == '\0' // own ns is prefixless
            && (flavor & DEF_NS_SQUASHED))
          );
    }

    size_t num_collected = 0;
    char **collected_ns = NULL;
295
    boolean def_ns = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
296
    if (namespaces_needed) {
297
      collected_ns = (this->*collector)(p_td, num_collected, def_ns, flavor2);
Elemer Lelik's avatar
Elemer Lelik committed
298
299
300
301
302
303
304
305
306
307
308
309
    }

    p_buf.put_s((size_t)p_td.namelens[exer] - 2, (cbyte*)p_td.names[exer]);

    if (namespaces_needed) {
      for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {
        p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);
        Free(collected_ns[cur_coll]); // job done
      }
      Free(collected_ns);
    }

310
311
312
313
314
315
316
    // If a default namespace is in effect (uri but no prefix) and the type
    // is unqualified, the default namespace must be canceled; otherwise
    // an XML tag without a ns prefix looks like it belongs to the def.namespace
    const boolean empty_ns_hack = exer && !omit_tag && (indent > 0)
      && (p_td.xer_bits & FORM_UNQUALIFIED)
      && (flavor & DEF_NS_PRESENT);

Elemer Lelik's avatar
Elemer Lelik committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    if (empty_ns_hack) {
      p_buf.put_s(9, (cbyte*)" xmlns=''");
      flavor &= ~DEF_NS_PRESENT;
      flavor |=  DEF_NS_SQUASHED;
    }
    else if (def_ns) {
      flavor &= ~DEF_NS_SQUASHED;
      flavor |=  DEF_NS_PRESENT;
    }

    if (type_atr) {
      p_buf.put_s(mstrlen(const_cast<char*>(type_atr)), (cbyte*)type_atr);
    }
    // now close the tag
    if (empty) {
      p_buf.put_s(2 + indenting, empty_tag_end);
    }
    else {
      p_buf.put_s(
        1 + (indenting
          && !(flavor & SIMPLE_TYPE)
          && !(exer && (p_td.xer_bits & (XER_LIST|USE_TYPE_ATTR)))),
          empty_tag_end+1);
    }
  }
  else { // tag is omitted
    // If the outer XML element optimistically wrote a newline after its start tag,
    // back up over it.
    size_t buf_used = p_buf.get_len();
    if (exer && (flavor & USE_NIL) && (buf_used-- > 0) && // sequence point!
      (p_buf.get_data()[buf_used] == '\n')) {
      p_buf.increase_length((size_t)-1); // back up over the newline
      omit_tag = -1; // to help fix HO85831
350
351
352
353
    } else if (exer && (p_td.xer_bits & USE_TYPE_ATTR) && type_atr && (flavor2 & FROM_UNION_USETYPE)) {
      p_buf.increase_length((size_t)-1); // back up over the endtag
      p_buf.put_s(mstrlen(const_cast<char*>(type_atr)), (cbyte*)type_atr);
      p_buf.put_c('>');
Elemer Lelik's avatar
Elemer Lelik committed
354
355
356
357
358
359
360
361
362
    }
  }

  Free(const_cast<char*>(type_atr));

  return omit_tag;
}

void Base_Type::end_xml  (const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
363
  unsigned int flavor, int indent, boolean empty, unsigned int flavor2) const
Elemer Lelik's avatar
Elemer Lelik committed
364
365
{
  int exer = is_exer(flavor);
366
367
368
  boolean omit_tag =
    // can never omit the tag at the toplevel, except when the type is union
    (indent != 0 || (flavor2 & THIS_UNION))
Elemer Lelik's avatar
Elemer Lelik committed
369
370
371
372
373
374
375
376
377
378
379
    && ( ((flavor & XER_RECOF) // can remove the tag even if not EXER
      && !(exer && (flavor & BXER_EMPTY_ELEM))) // except 26.6, 26.7
      || (exer /*&& */
        && ( (p_td.xer_bits & (UNTAGGED|ANY_ATTRIBUTES|ANY_ELEMENT))
          || (flavor & (EMBED_VALUES|XER_LIST|ANY_ATTRIBUTES|USE_NIL|USE_TYPE_ATTR)))));

  if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
    p_buf.put_c('\'');
  }
  else if (!empty && !omit_tag) {
    // now close the tag
380
    int indenting = !is_canonical(flavor);
Elemer Lelik's avatar
Elemer Lelik committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
    if (indenting && !(flavor & SIMPLE_TYPE)) do_indent(p_buf, indent);
    p_buf.put_s(2, (cbyte*)"</");
    if (exer) write_ns_prefix(p_td, p_buf);
    p_buf.put_s((size_t)p_td.namelens[exer]-1+indenting, (cbyte*)p_td.names[exer]);
  }

}


ASN_BER_TLV_t* Base_Type::BER_encode_chk_bound(boolean p_isbound)
{
  if(!p_isbound) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
    ASN_BER_TLV_t *new_tlv=ASN_BER_TLV_t::construct(0, NULL);
    new_tlv->Tlen=0;
    new_tlv->Tstr=NULL;
    new_tlv->Llen=0;
    new_tlv->Lstr=NULL;
    return new_tlv;
  }
  else return NULL;
}

// Called only from the two places in BER_encode_TLV_OCTETSTRING, below.
void Base_Type::BER_encode_putoctets_OCTETSTRING
(unsigned char *target,
 unsigned int octetnum_start, unsigned int octet_count,
 int p_nof_octets, const unsigned char *p_octets_ptr)
{
  if(   octetnum_start             > static_cast<unsigned int>(p_nof_octets)
     || octetnum_start+octet_count > static_cast<unsigned int>(p_nof_octets))
    TTCN_EncDec_ErrorContext::error_internal
      ("In Base_Type::BER_encode_putoctets_OCTETSTRING(): Index overflow.");
  memcpy(target, &p_octets_ptr[octetnum_start], octet_count);
}

ASN_BER_TLV_t* Base_Type::BER_encode_TLV_OCTETSTRING
(unsigned p_coding,
 int p_nof_octets, const unsigned char *p_octets_ptr)
{
  unsigned char *V_ptr;
  size_t V_len;
  unsigned int nof_fragments=0;

  if(p_coding==BER_ENCODE_CER) {
    nof_fragments=(p_nof_octets+999)/1000;
    if(!nof_fragments) nof_fragments=1;
  }
  else /*if(coding==BER_ENCODE_DER)*/ {
    nof_fragments=1;
  }

  ASN_BER_TLV_t *new_tlv=NULL;
  boolean is_constructed=nof_fragments>1;
  if(!is_constructed) {
    V_len=p_nof_octets;
    V_ptr=(unsigned char*)Malloc(V_len);
    BER_encode_putoctets_OCTETSTRING(V_ptr, 0, p_nof_octets,
                                     p_nof_octets, p_octets_ptr);
    new_tlv=ASN_BER_TLV_t::construct(V_len, V_ptr);
  }
  else { // is constructed
    ASN_BER_TLV_t *tmp_tlv=NULL;
    new_tlv=ASN_BER_TLV_t::construct(NULL);
    unsigned int rest_octets=p_nof_octets-(nof_fragments-1)*1000;
    V_len=1000;
    for(unsigned int i=0; i<nof_fragments; i++) {
      if(i==nof_fragments-1) V_len=rest_octets;
      V_ptr=(unsigned char*)Malloc(V_len);
      BER_encode_putoctets_OCTETSTRING(V_ptr, i*1000, V_len,
                                       p_nof_octets, p_octets_ptr);
      tmp_tlv=ASN_BER_TLV_t::construct(V_len, V_ptr);
      tmp_tlv=ASN_BER_V2TLV(tmp_tlv, OCTETSTRING_descr_, p_coding);
      new_tlv->add_TLV(tmp_tlv);
    }
  }
  return new_tlv;
}

/*
// for debugging purposes
static std::string bignum_as_bin(const BIGNUM* const BN) {
  int bytes = BN_num_bytes(BN);
  unsigned char* bn_as_bin = (unsigned char*) Malloc(bytes);
  BN_bn2bin(BN, bn_as_bin);

  std::string result;
  for (int i = 0; i < bytes; ++i) {
    result.append(char_as_bin(bn_as_bin[i]));
    result.append(" ");
  }
  Free(bn_as_bin);
  return result;
}

static std::string int_as_bin(int myInt) {
  std::string result;
  for (int i = sizeof(myInt) - 1; i >= 0; --i) {
    char current = (myInt >> (i * 8)) & 0xFF;
    result.append(char_as_bin(current));
  }
  return result;
}

static std::string char_as_bin(unsigned char ch) {
  std::string result;
  for (int i = 0; i < 8; ++i) {
    result.append((ch & 0x80) == 0x80 ? "1" : "0");
    ch <<= 1;
  }
  return result;
}

static std::string chars_as_bin(const unsigned char* const chars, int len) {
  std::string result;
  for (int i = 0; i < len; ++i) {
    result.append(char_as_bin(chars[i]));
    if (i != len - 1) {
      result.append(" ");
    }
  }
  return result;
}
*/

ASN_BER_TLV_t *Base_Type::BER_encode_TLV_INTEGER(unsigned,
  const int_val_t& p_int_val)
{
  if(p_int_val.is_native()){
    RInt p_int_val_int=p_int_val.get_val();
    // Determine the number of octets to be used in the encoding.
    unsigned long ulong_val=p_int_val_int >= 0
      ?static_cast<unsigned long>(p_int_val_int):
      ~static_cast<unsigned long>(p_int_val_int);
    size_t V_len=1;
    ulong_val>>=7;
    while(ulong_val!=0){
      V_len++;
      ulong_val>>=8;
    }
    ASN_BER_TLV_t *new_tlv=ASN_BER_TLV_t::construct(V_len,NULL);
    // Already in 2's complement encoding.
    ulong_val=static_cast<unsigned long>(p_int_val_int);
    for(size_t i=V_len;i>0;i--){
      new_tlv->V.str.Vstr[i-1]=ulong_val&0xFF;
      ulong_val>>=8;
    }
    return new_tlv;
  }

  // bignum
  
  const BIGNUM* const D = p_int_val.get_val_openssl();
  if (BN_is_zero(D)) {
    ASN_BER_TLV_t *new_tlv=ASN_BER_TLV_t::construct(1,NULL);
    new_tlv->V.str.Vstr[0] = 0;
    return new_tlv;
  }

  size_t num_bytes = BN_num_bytes(D);
  unsigned char* bn_as_bin = (unsigned char*) Malloc(num_bytes);
  BN_bn2bin(D, bn_as_bin);

545
  boolean pad = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
546
547
548
549
550
551
  if (BN_is_negative(D)) {
    for(size_t i = 0; i < num_bytes; ++i){
      bn_as_bin[i] = ~bn_as_bin[i];
    }

    // add one
552
    boolean stop = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
553
554
555
556
557
    for (int i = num_bytes - 1; i >= 0 && !stop; --i) {
      for (int j = 0; j < 8 && !stop; ++j) {
        unsigned char mask = (0x1 << j);
        if (!(bn_as_bin[i] & mask)) {
          bn_as_bin[i] |= mask;
558
          stop = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
        } else {
          bn_as_bin[i] ^= mask;
        }
      }
    }
    pad = !(bn_as_bin[0] & 0x80);
  } else {
    pad = bn_as_bin[0] & 0x80;
  }

  ASN_BER_TLV_t* new_tlv = ASN_BER_TLV_t::construct(num_bytes + pad, NULL);
  if (pad) {
    new_tlv->V.str.Vstr[0] = BN_is_negative(D) ? 0xFF : 0x0;
  } 
  
  memcpy(new_tlv->V.str.Vstr + pad, bn_as_bin, num_bytes);

  Free(bn_as_bin);
  return new_tlv;
}

ASN_BER_TLV_t *Base_Type::BER_encode_TLV_INTEGER(unsigned p_coding,
  const int& p_int_val)
{
  int_val_t p_int_val_val(p_int_val);
  return BER_encode_TLV_INTEGER(p_coding, p_int_val_val);
}

void Base_Type::BER_encode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
                                          boolean p_isvalid, int p_value)
{
  if(!p_isvalid)
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_ENC_ENUM,
       "Encoding unknown value '%d' for enumerated type '%s'.",
       p_value, p_td.name);

}

void Base_Type::BER_decode_str2TLV(TTCN_Buffer& p_buf, ASN_BER_TLV_t& p_tlv,
                                   unsigned L_form)
{
  if(!ASN_BER_str2TLV(p_buf.get_read_len(), p_buf.get_read_data(),
                      p_tlv, L_form))
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_INCOMPL_MSG, "TLV is not complete.");
}

boolean Base_Type::BER_decode_constdTLV_next(const ASN_BER_TLV_t& p_tlv,
                                             size_t& V_pos,
                                             unsigned L_form,
                                             ASN_BER_TLV_t& p_target_tlv)
{
  if(p_tlv.V.str.Vlen<=V_pos) {
    if(!p_tlv.isLenDefinite)
      TTCN_EncDec_ErrorContext::error
        (TTCN_EncDec::ET_INCOMPL_MSG,
         "Missing end-of-contents octet in the indefinite length"
         " constructed TLV.");
    return FALSE;
  }
  if(!ASN_BER_str2TLV(p_tlv.V.str.Vlen-V_pos,
                      p_tlv.V.str.Vstr+V_pos, p_target_tlv, L_form)) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_INCOMPL_MSG,
       "Incomplete TLV in the constructed TLV.");
  }
  if(!p_tlv.isLenDefinite && p_target_tlv.tagnumber==0
     && p_target_tlv.tagclass==ASN_TAG_UNIV)
    return FALSE;
  V_pos+=p_target_tlv.get_len();
  return TRUE;
}

void Base_Type::BER_decode_constdTLV_end(const ASN_BER_TLV_t& p_tlv,
                                         size_t& V_pos,
                                         unsigned L_form,
                                         ASN_BER_TLV_t& p_target_tlv,
                                         boolean tlv_present)
{
  if(tlv_present
     || BER_decode_constdTLV_next(p_tlv, V_pos, L_form, p_target_tlv)) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_SUPERFL,
       "Superfluous TLV(s) at the end of constructed TLV.");
  }
}

static void BER_decode_chk_tag(const ASN_Tag_t& tag,
                               const ASN_BER_TLV_t& tlv)
{
  if (tlv.isTagComplete &&
      (tag.tagclass != tlv.tagclass || tag.tagnumber != tlv.tagnumber)) {
    ASN_Tag_t rcvdtag;
    rcvdtag.tagclass=tlv.tagclass;
    rcvdtag.tagnumber=tlv.tagnumber;
    char *rcvdstr=rcvdtag.print();
    try {
      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TAG,
	"Tag mismatch: Received: %s.", rcvdstr);
    } catch (...) {
      Free(rcvdstr);
      throw;
    }
    Free(rcvdstr);
  }
}

void Base_Type::BER_decode_strip_tags(const ASN_BERdescriptor_t& p_ber,
                                      const ASN_BER_TLV_t& p_tlv,
                                      unsigned L_form,
                                      ASN_BER_TLV_t& stripped_tlv)
{
  size_t i=p_ber.n_tags;
  if(i==0) {
    stripped_tlv=p_tlv;
    return;
  }
  char *expectedstr=p_ber.print_tags();
  TTCN_EncDec_ErrorContext ec("While checking tags (expecting %s): ",
                              expectedstr);
  Free(expectedstr);
  if(i==1) {
    BER_decode_chk_tag(p_ber.tags[0], p_tlv);
    stripped_tlv=p_tlv;
    return;
  }
  ASN_BER_TLV_t curr_tlv=p_tlv;
  boolean doit=TRUE;
  i--;
  while(doit) {
    TTCN_EncDec_ErrorContext ec2("At pos #%lu: ",
      (unsigned long) (p_ber.n_tags - i));
    BER_decode_chk_tag(p_ber.tags[i], curr_tlv);
    if(i!=0) { // not the innermost tag
      if(!curr_tlv.isConstructed) {
        ec2.error(TTCN_EncDec::ET_TAG,
          "The other (innermost %lu) tag(s) are missing.", (unsigned long) i);
        doit=FALSE;
        stripped_tlv=curr_tlv;
      }
      else { // O.K., is constructed
        size_t V_pos=0;
        BER_decode_constdTLV_next(curr_tlv, V_pos, L_form, stripped_tlv);
        // if superfluous...?
        ASN_BER_TLV_t endchecker_tlv;
        BER_decode_constdTLV_end(curr_tlv, V_pos, L_form, endchecker_tlv,
                                 FALSE);
        curr_tlv=stripped_tlv;
        i--;
      } // is constructed
    } // not the innermost
    else { // innermost tag
      doit=FALSE;
    }
  } // while doit
}

void Base_Type::BER_decode_getoctets_OCTETSTRING
(const unsigned char *source, size_t s_len,
 unsigned int& octetnum_start,
 int& p_nof_octets, unsigned char *p_octets_ptr)
{
  p_nof_octets=octetnum_start+s_len;
  memcpy(&p_octets_ptr[octetnum_start], source, s_len);
  octetnum_start+=s_len;
}

void Base_Type::BER_decode_TLV_OCTETSTRING
(const ASN_BER_TLV_t& p_tlv, unsigned L_form,
 unsigned int& octetnum_start,
 int& p_nof_octets, unsigned char *p_octets_ptr)
{
  if(!p_tlv.isConstructed) {
    BER_decode_getoctets_OCTETSTRING
      (p_tlv.V.str.Vstr, p_tlv.V.str.Vlen, octetnum_start,
       p_nof_octets, p_octets_ptr);
  }
  else { // is constructed
    ASN_BER_TLV_t tlv2;
    size_t V_pos=0;
    boolean doit=TRUE;
    while(doit) {
      if(!ASN_BER_str2TLV(p_tlv.V.str.Vlen-V_pos, p_tlv.V.str.Vstr+V_pos,
                          tlv2, L_form)) {
        TTCN_EncDec_ErrorContext::error
          (TTCN_EncDec::ET_INCOMPL_MSG,
           "Incomplete TLV in a constructed OCTETSTRING TLV.");
        return;
      }
      if(!p_tlv.isLenDefinite && tlv2.tagnumber==0
         && tlv2.tagclass==ASN_TAG_UNIV)
        doit=FALSE; // End-of-contents
      if(doit) {
        ASN_BER_TLV_t stripped_tlv;
        BER_decode_strip_tags(OCTETSTRING_ber_, tlv2, L_form, stripped_tlv);
	BER_decode_TLV_OCTETSTRING(tlv2, L_form, octetnum_start,
                                   p_nof_octets, p_octets_ptr);
	V_pos+=tlv2.get_len();
	if(V_pos>=p_tlv.V.str.Vlen) doit=FALSE;
      }
    } // while(doit)
  } // else / is constructed
}



boolean Base_Type::BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
  unsigned, int_val_t& p_int_val)
{
  p_tlv.chk_constructed_flag(FALSE);
  if (!p_tlv.isComplete) return FALSE;
  if (!p_tlv.V_tlvs_selected && p_tlv.V.str.Vlen == 0) {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG,
      "Length of V-part is 0.");
    return FALSE;
  }

  const size_t Vlen = p_tlv.V.str.Vlen;

  if (Vlen > sizeof(RInt)) { // Bignum

781
    const boolean negative = p_tlv.V.str.Vstr[0] & 0x80;
Elemer Lelik's avatar
Elemer Lelik committed
782
783
784
785
786
787
    BIGNUM *D = BN_new();

    if (negative) {
      unsigned char* const Vstr = (unsigned char*) Malloc(Vlen);
      memcpy(Vstr, p_tlv.V.str.Vstr, Vlen);
      // -1 
788
      boolean stop = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
789
790
791
792
793
      for (int i = Vlen - 1; i >= 0 && !stop; --i) {
        for(int j = 0; j < 8 && !stop; ++j) {
          unsigned char mask = (0x1 << j);
          if (Vstr[i] & mask) {
            Vstr[i] ^= mask;
794
            stop = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
          } else {
            Vstr[i] |= mask;
          }
        }
      }

      for (size_t i = 0; i < Vlen; ++i) {
        Vstr[i] = ~Vstr[i];
      }

      BN_bin2bn(Vstr, Vlen, D);
      Free(Vstr);
    } else { // positive case
      BN_bin2bn(p_tlv.V.str.Vstr, Vlen, D);
    }

    BN_set_negative(D, negative);
    p_int_val = int_val_t(D);
    return TRUE;
  } // bignum 
  
  // Native int Vlen <= sizeof(RInt)
  const unsigned char* const Vstr = p_tlv.V.str.Vstr;
  RInt int_val = 0; 
  if (Vstr[0] & 0x80) { // negative 
      // the first bytes should be 1-s
      // e.g. -1 encoded on 32 bits: 11111111111111111111111111111111
      // e.g. -1 encoded on 8  bits:                         11111111 
      //                             [(sizeof(RInt)-Vlen)*8 ][Vlen*8]
      //                             [         sizeof(RInt)*8       ]

    for (size_t i = 0; i < sizeof(RInt) - Vlen; ++i) {
      int_val |= 0xFF;
      int_val <<= 8;
    }
  }

  int_val |= p_tlv.V.str.Vstr[0];
  for (size_t i = 1; i < Vlen; ++i) {
    int_val <<= 8;
    int_val |= p_tlv.V.str.Vstr[i];
  }

  p_int_val = int_val_t(int_val);
  return TRUE;
}

boolean Base_Type::BER_decode_TLV_INTEGER(const ASN_BER_TLV_t& p_tlv,
  unsigned L_form, int& p_int_val)
{
  int_val_t p_int_val_val(p_int_val);
  boolean ret_val = BER_decode_TLV_INTEGER(p_tlv, L_form, p_int_val_val);
  if (p_int_val_val.is_native()) {
    p_int_val = p_int_val_val.get_val();
  } else {
    TTCN_warning("Large integer value was decoded and it can't be returned "
    "as a native `int'");
  }
  return ret_val;
}

/*

// Actually, this is not used, but when we ever need a
// type-independent integer decoding, then we can move this to
// Basetype.hh. It is so beautiful and wonderful, I did not wanted
// this to get lost. :)

template<typename int_type>
void Base_Type::BER_decode_TLV_INTEGER
(const ASN_BER_TLV_t& p_tlv, unsigned L_form, int_type& p_intval)
{
  boolean overflow_flag=FALSE;
  p_tlv.chk_constructed_flag(FALSE);
  if(!p_tlv.V_tlvs_selected && p_tlv.V.str.Vlen==0) {
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_INVAL_MSG, "Length of V-part is 0.");
    p_intval=static_cast<int_type>(0);
    return;
  }
  int_type tmp_int=static_cast<int_type>(p_tlv.V.str.Vstr[0] & 0x7F);
  for(size_t i=1; i<p_tlv.V.str.Vlen; i++) {
    p_intval=tmp_int;
    tmp_int=static_cast<int_type>(256*tmp_int);
    if(tmp_int<p_intval) overflow_flag=TRUE;
    tmp_int=static_cast<int_type>(tmp_int+p_tlv.V.str.Vstr[i]);
  }
  if(p_tlv.V.str.Vstr[0] & 0x80) { // negative
    int_type highbit=static_cast<int_type>(1);
    for(size_t i=0; i<p_tlv.V.str.Vlen*8-1; i++) {
      int_type backup=highbit;
      highbit=static_cast<int_type>(2*highbit);
      if(highbit/2!=backup) overflow_flag=TRUE;
    }
    p_intval=static_cast<int_type>(tmp_int-highbit);
  }
  else { // positive
    p_intval=tmp_int;
  }
  if(overflow_flag)
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_REPR,
       "Value is too big (%lu octets).", p_tlv.V.str.Vlen);
}

*/

boolean Base_Type::BER_decode_TLV_CHOICE(const ASN_BERdescriptor_t& p_ber,
                                         const ASN_BER_TLV_t& p_tlv,
                                         unsigned L_form,
                                         ASN_BER_TLV_t& p_target_tlv)
{
  if(p_ber.n_tags>0) {
    size_t V_pos=0;
    p_tlv.chk_constructed_flag(TRUE);
    if(!BER_decode_constdTLV_next(p_tlv, V_pos, L_form, p_target_tlv))
      return FALSE;
  }
  else p_target_tlv=p_tlv;
  return TRUE;
}

boolean Base_Type::BER_decode_CHOICE_selection(boolean select_result,
                                               const ASN_BER_TLV_t& p_tlv)
{
  if(select_result) return TRUE;
  ASN_Tag_t rcvd_tag;
  rcvd_tag.tagclass=p_tlv.tagclass;
  rcvd_tag.tagnumber=p_tlv.tagnumber;
  char *rcvd_str=rcvd_tag.print();
  try {
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TAG,
      "Invalid tag or unknown alternative: %s.", rcvd_str);
  } catch (...) {
    Free(rcvd_str);
    throw;
  }
  Free(rcvd_str);
  return FALSE;
}

void Base_Type::BER_decode_chk_enum_valid(const TTCN_Typedescriptor_t& p_td,
                                          boolean p_isvalid, int p_value)
{
  /** \todo If extensible ENUMERATED is supported (for example, in the
    * typedescriptor there is something...), then give different error
    * message depending on that flag. */
  if(!p_isvalid)
    TTCN_EncDec_ErrorContext::error
      (TTCN_EncDec::ET_DEC_ENUM,
       "Unknown value '%d' received for enumerated type '%s'.",
       p_value, p_td.name);
}

ASN_BER_TLV_t*
Base_Type::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
                          unsigned) const
{
  BER_chk_descr(p_td);
  ASN_BER_TLV_t *tlv=BER_encode_chk_bound(TRUE);
  if(tlv) return tlv;
  TTCN_EncDec_ErrorContext::error_internal
    ("BER_encode_V() not implemented for type '%s'.", p_td.name);
  return NULL;
}

boolean Base_Type::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
                                      const ASN_BER_TLV_t& p_tlv)
{
  if(p_td.ber->n_tags==0 || !p_tlv.isTagComplete) return TRUE;
  const ASN_Tag_t& tag=p_td.ber->tags[p_td.ber->n_tags-1];
  return (tag.tagclass==p_tlv.tagclass && tag.tagnumber==p_tlv.tagnumber);
}

boolean Base_Type::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
                                  const ASN_BER_TLV_t& p_tlv,
                                  unsigned L_form)
{
  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;
  ec.error_internal
    ("BER_decode_V() not implemented for type '%s'.", p_td.name);
  return FALSE;
}

int Base_Type::RAW_encode(const TTCN_Typedescriptor_t& p_td,
                           RAW_enc_tree&) const
{
  TTCN_error("RAW encoding requested for type '%s'"
             " which has no RAW encoding method.", p_td.name);
  return 0;
}

int Base_Type::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
                           TTCN_Buffer&) const
{
  TTCN_error("TEXT encoding requested for type '%s'"
             " which has no TEXT encoding method.", p_td.name);
  return 0;
}

int Base_Type::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
                 TTCN_Buffer&,  Limit_Token_List&, boolean, boolean)
{
  TTCN_error("TEXT decoding requested for type '%s'"
             " which has no TEXT decoding method.", p_td.name);
  return 0;
}

int Base_Type::RAW_decode(const TTCN_Typedescriptor_t& p_td,
  TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
1008
1009
  boolean /* no_error */, int /* sel_field */, boolean /* first_call */,
  const RAW_Force_Omit* /* force_omit */ )
Elemer Lelik's avatar
Elemer Lelik committed
1010
1011
1012
1013
1014
1015
1016
{
  TTCN_error("RAW decoding requested for type '%s'"
             " which has no RAW decoding method.",p_td.name);
  return 0;
}

int Base_Type::XER_encode(const XERdescriptor_t& p_td,
1017
                          TTCN_Buffer&, unsigned int, unsigned int, int, embed_values_enc_struct_t*) const
Elemer Lelik's avatar
Elemer Lelik committed
1018
1019
1020
1021
1022
1023
{
  TTCN_error("XER encoding requested for type '%-.*s' which has no"
             " XER encoding method.", p_td.namelens[0]-2, p_td.names[0]);
  return 0;
}

Elemer Lelik's avatar
Elemer Lelik committed
1024
int Base_Type::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap&,
Elemer Lelik's avatar
Elemer Lelik committed
1025
                          unsigned int, unsigned int, embed_values_dec_struct_t*) {
Elemer Lelik's avatar
Elemer Lelik committed
1026
1027
1028
1029
1030
  TTCN_error("XER decoding requested for type '%-.*s' which has no"
             " XER decoding method.", p_td.namelens[0]-2, p_td.names[0]);
  return 0;
}

1031
int Base_Type::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean) const
Elemer Lelik's avatar
Elemer Lelik committed
1032
1033
1034
1035
1036
1037
{
  TTCN_error("JSON encoding requested for type '%s' which has no"
             " JSON encoding method.", p_td.name);
  return 0;
}

1038
int Base_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer&, boolean, boolean, int) 
Elemer Lelik's avatar
Elemer Lelik committed
1039
1040
1041
1042
1043
1044
{
  TTCN_error("JSON decoding requested for type '%s' which has no"
             " JSON decoding method.", p_td.name);
  return 0;
}

1045
1046
1047
1048
1049
1050
1051
int Base_Type::OER_encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer&) const
{
  TTCN_error("OER encoding requested for type '%s' which has no"
             " OER encoding method.", p_td.name);
  return 0;
}

1052
int Base_Type::OER_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer&, OER_struct&) 
1053
1054
1055
1056
1057
1058
{
  TTCN_error("OER decoding requested for type '%s' which has no"
             " OER decoding method.", p_td.name);
  return 0;
}

Elemer Lelik's avatar
Elemer Lelik committed
1059
boolean Base_Type::can_start(const char *name, const char *uri,
1060
  XERdescriptor_t const& xd, unsigned int flavor, unsigned int /*flavor2*/)
Elemer Lelik's avatar
Elemer Lelik committed
1061
1062
1063
1064
1065
1066
{
  boolean e_xer = is_exer(flavor);
  // Check the name. If EXER, check the namespace too.
  return check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));
}

1067
char ** Base_Type::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns, unsigned int /* flavor */) const
Elemer Lelik's avatar
Elemer Lelik committed
1068
{
1069
  def_ns = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
1070
1071
1072
  char *tmp = NULL;
  if (p_td.my_module != 0 && p_td.ns_index != -1
    && !(p_td.xer_bits & FORM_UNQUALIFIED)) {
1073
    const namespace_t *my_ns = p_td.my_module->get_ns((size_t)p_td.ns_index);
1074
    if (!*my_ns->px) def_ns = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
    tmp = mprintf(" xmlns%s%s='%s'",
      ((*my_ns->px) ? ":" : ""), my_ns->px,
      my_ns->ns
    );
  }
  // The above may throw, but then nothing was allocated.
  if (tmp != NULL) {
    num = 1;
    char **retval = (char**)Malloc(sizeof(char*));
    *retval = tmp;
    return retval;
  }
  else {
    num = 0;
    return NULL;
  }
}

void Base_Type::merge_ns(char **&collected_ns, size_t& num_collected,
  char **new_namespaces, size_t num_new)
{

  for (size_t cur_ns = 0; cur_ns < num_new; ++cur_ns) {
    for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {
      if (!strcmp(new_namespaces[cur_ns], collected_ns[cur_coll])) {
        // same, drop it
        Free(new_namespaces[cur_ns]);
        new_namespaces[cur_ns] = NULL;
        break;
      }
    }

    if (new_namespaces[cur_ns]) { // still there
      collected_ns = (char**)Realloc(collected_ns, sizeof(char*) * ++num_collected);
      collected_ns[num_collected-1] = new_namespaces[cur_ns];
    }
  }
  Free(new_namespaces);
  new_namespaces = 0;
}

/////////////////////////////////////////////////////////////

TTCN_Type_list::~TTCN_Type_list()
{
  Free(types);
}

void TTCN_Type_list::push(const Base_Type *p_type)
{
  types=(const Base_Type**)Realloc(types, ++n_types*sizeof(*types));
  types[n_types-1]=p_type;
}

const Base_Type* TTCN_Type_list::pop()
{
  if(!n_types)
    TTCN_EncDec_ErrorContext::error_internal
      ("TTCN_Type_list::pop(): List is empty.");
  const Base_Type *t;
  t=types[--n_types];
  types=(const Base_Type**)Realloc(types, n_types*sizeof(*types));
  return t;
}

const Base_Type* TTCN_Type_list::get_nth(size_t pos) const
{
  if(pos==0) return types[0];
  if(pos>n_types)
    TTCN_EncDec_ErrorContext::error_internal
      ("TTCN_Type_list::get_nth(%lu): Out of range.", (unsigned long) pos);
  return types[n_types-pos];
}

const TTCN_Typedescriptor_t BOOLEAN_descr_={"BOOLEAN", &BOOLEAN_ber_,
1150
  &BOOLEAN_raw_, &BOOLEAN_text_, &BOOLEAN_xer_, &BOOLEAN_json_, &BOOLEAN_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1151
1152

const TTCN_Typedescriptor_t INTEGER_descr_={"INTEGER", &INTEGER_ber_,
1153
  &INTEGER_raw_, &INTEGER_text_, &INTEGER_xer_, &INTEGER_json_, &INTEGER_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1154
1155

const TTCN_Typedescriptor_t FLOAT_descr_={"REAL", &FLOAT_ber_, &FLOAT_raw_,
1156
  NULL, &FLOAT_xer_, &FLOAT_json_, &FLOAT_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1157
1158

const TTCN_Typedescriptor_t VERDICTTYPE_descr_={"verdicttype", NULL, NULL,
1159
  NULL, &VERDICTTYPE_xer_, &VERDICTTYPE_json_, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1160
1161

const TTCN_Typedescriptor_t OBJID_descr_={"OBJECT IDENTIFIER", &OBJID_ber_,
1162
  NULL, NULL, &OBJID_xer_, &OBJID_json_, &OBJID_oer_, NULL, TTCN_Typedescriptor_t::OBJID};
Elemer Lelik's avatar
Elemer Lelik committed
1163
1164

const TTCN_Typedescriptor_t BITSTRING_descr_={"BIT STRING", &BITSTRING_ber_,
1165
  &BITSTRING_raw_, NULL, &BITSTRING_xer_, &BITSTRING_json_, &BITSTRING_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1166
1167

const TTCN_Typedescriptor_t HEXSTRING_descr_={"hexstring", NULL,
1168
  &HEXSTRING_raw_, NULL, &HEXSTRING_xer_, &HEXSTRING_json_, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1169
1170

const TTCN_Typedescriptor_t OCTETSTRING_descr_={"OCTET STRING",
1171
  &OCTETSTRING_ber_, &OCTETSTRING_raw_, &OCTETSTRING_text_, &OCTETSTRING_xer_, &OCTETSTRING_json_, &OCTETSTRING_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1172
1173

const TTCN_Typedescriptor_t CHARSTRING_descr_={"charstring", NULL,
1174
  &CHARSTRING_raw_, &CHARSTRING_text_, &CHARSTRING_xer_, &CHARSTRING_json_, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1175
1176

const TTCN_Typedescriptor_t UNIVERSAL_CHARSTRING_descr_={"universal charstring",
1177
  NULL, &UNIVERSAL_CHARSTRING_raw_, &UNIVERSAL_CHARSTRING_text_, &UNIVERSAL_CHARSTRING_xer_, &UNIVERSAL_CHARSTRING_json_, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1178
1179

const TTCN_Typedescriptor_t COMPONENT_descr_={"component", NULL, NULL, NULL,
1180
  NULL, NULL, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1181
1182

const TTCN_Typedescriptor_t DEFAULT_descr_={"default", NULL, NULL, NULL,
1183
  NULL, NULL, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1184
1185

const TTCN_Typedescriptor_t ASN_NULL_descr_={"NULL", &ASN_NULL_ber_, NULL,
1186
  NULL, &ASN_NULL_xer_, &ASN_NULL_json_, &ASN_NULL_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1187
1188

const TTCN_Typedescriptor_t ASN_ANY_descr_={"ANY", &ASN_ANY_ber_, NULL,
1189
  NULL, NULL, &ASN_ANY_json_, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1190
1191

const TTCN_Typedescriptor_t EXTERNAL_descr_={"EXTERNAL", &EXTERNAL_ber_, NULL,
1192
  NULL, &EXTERNAL_xer_, NULL, &EXTERNAL_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1193
1194

const TTCN_Typedescriptor_t EMBEDDED_PDV_descr_={"EMBEDDED PDV",
1195
  &EMBEDDED_PDV_ber_, NULL, NULL, &EMBEDDED_PDV_xer_, NULL, &EMBEDDED_PDV_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1196
1197

const TTCN_Typedescriptor_t CHARACTER_STRING_descr_={"CHARACTER STRING",
1198
  &CHARACTER_STRING_ber_, NULL, NULL, &CHARACTER_STRING_xer_, NULL, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1199
1200

const TTCN_Typedescriptor_t ObjectDescriptor_descr_={"ObjectDescriptor",
1201
  &ObjectDescriptor_ber_, NULL, NULL, NULL, NULL, &ObjectDescriptor_oer_, NULL, TTCN_Typedescriptor_t::GRAPHICSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1202
1203

const TTCN_Typedescriptor_t UTF8String_descr_={"UTF8String", &UTF8String_ber_,
1204
  NULL, NULL, &UTF8String_xer_, &UTF8String_json_, &UTF8String_oer_, NULL, TTCN_Typedescriptor_t::UTF8STRING};
Elemer Lelik's avatar
Elemer Lelik committed
1205
1206

const TTCN_Typedescriptor_t ASN_ROID_descr_={"RELATIVE-OID", &ASN_ROID_ber_,
1207
  NULL, NULL, &ASN_ROID_xer_, &ASN_ROID_json_, &ASN_ROID_oer_, NULL, TTCN_Typedescriptor_t::ROID};
Elemer Lelik's avatar
Elemer Lelik committed
1208
1209

const TTCN_Typedescriptor_t NumericString_descr_={"NumericString",
1210
  &NumericString_ber_, NULL, NULL, &NumericString_xer_, &NumericString_json_, &NumericString_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1211
1212

const TTCN_Typedescriptor_t PrintableString_descr_={"PrintableString",
1213
  &PrintableString_ber_, NULL, NULL, &PrintableString_xer_, &PrintableString_json_, &PrintableString_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1214
1215

const TTCN_Typedescriptor_t TeletexString_descr_={"TeletexString",
1216
  &TeletexString_ber_, NULL, NULL, &TeletexString_xer_, &TeletexString_json_, &TeletexString_oer_, NULL, TTCN_Typedescriptor_t::TELETEXSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1217
1218
1219
const TTCN_Typedescriptor_t& T61String_descr_=TeletexString_descr_;

const TTCN_Typedescriptor_t VideotexString_descr_={"VideotexString",
1220
  &VideotexString_ber_, NULL, NULL, &VideotexString_xer_, &VideotexString_json_, &VideotexString_oer_, NULL, TTCN_Typedescriptor_t::VIDEOTEXSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1221
1222

const TTCN_Typedescriptor_t IA5String_descr_={"IA5String", &IA5String_ber_,
1223
  NULL, NULL, &IA5String_xer_, &IA5String_json_, &IA5String_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1224
1225

const TTCN_Typedescriptor_t ASN_GeneralizedTime_descr_={"GeneralizedTime",
1226
  &ASN_GeneralizedTime_ber_, NULL, NULL, NULL, NULL, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1227
1228

const TTCN_Typedescriptor_t ASN_UTCTime_descr_={"UTCTime", &ASN_UTCTime_ber_,
1229
  NULL, NULL, NULL, NULL, NULL, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1230
1231

const TTCN_Typedescriptor_t GraphicString_descr_={"GraphicString",
1232
  &GraphicString_ber_, NULL, NULL, &GraphicString_xer_, &GraphicString_json_, &GraphicString_oer_, NULL, TTCN_Typedescriptor_t::GRAPHICSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1233
1234

const TTCN_Typedescriptor_t VisibleString_descr_={"VisibleString",
1235
  &VisibleString_ber_, NULL, NULL, &VisibleString_xer_, &VisibleString_json_, &VisibleString_oer_, NULL, TTCN_Typedescriptor_t::DONTCARE};
Elemer Lelik's avatar
Elemer Lelik committed
1236
1237
1238
const TTCN_Typedescriptor_t& ISO646String_descr_=VisibleString_descr_;

const TTCN_Typedescriptor_t GeneralString_descr_={"GeneralString",
1239
  &GeneralString_ber_, NULL, NULL, &GeneralString_xer_, &GeneralString_json_, &GeneralString_oer_, NULL, TTCN_Typedescriptor_t::GENERALSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1240
1241

const TTCN_Typedescriptor_t UniversalString_descr_={"UniversalString",
1242
  &UniversalString_ber_, NULL, NULL, &UniversalString_xer_, &UniversalString_json_, &UniversalString_oer_, NULL, TTCN_Typedescriptor_t::UNIVERSALSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1243
1244

const TTCN_Typedescriptor_t BMPString_descr_={"BMPString", &BMPString_ber_,
1245
  NULL, NULL, &BMPString_xer_, &BMPString_json_, &BMPString_oer_, NULL, TTCN_Typedescriptor_t::BMPSTRING};
Elemer Lelik's avatar
Elemer Lelik committed
1246
1247