1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* Contributors:
* Balasko, Jeno
* Baranyi, Botond
*
******************************************************************************/
#include "Coders.hh"
using Custom1::c__separator;
namespace Custom2 {
BITSTRING f__enc__rec(const Custom3::Rec& x)
{
return int2bit(x.num(), 8);
}
INTEGER f__dec__rec(BITSTRING& b, Custom3::Rec& x)
{
x.num() = bit2int(b);
x.str() = "c++";
b = BITSTRING(0, NULL);
return 0;
}
BITSTRING f__enc__uni(const Custom1::Uni& x)
{
if (x.get_selection() == Custom1::Uni::ALT_i) {
return c__separator + int2bit(x.i(), 8) + c__separator;
}
else {
return c__separator + c__separator;
}
}
INTEGER f__dec__uni(BITSTRING& b, Custom1::Uni& x)
{
int b_len = b.lengthof();
int sep_len = c__separator.lengthof();
if (b_len >= 2 * sep_len &&
substr(b, 0, sep_len) == c__separator &&
substr(b, b_len - sep_len, sep_len) == c__separator) {
if (b_len > 2 * sep_len) {
x.i() = bit2int(substr(b, sep_len, b_len - 2 * sep_len));
}
b = BITSTRING(0, NULL);
return 0;
}
else {
return 1;
}
}
BITSTRING f__enc__bs(const BITSTRING& x)
{
return x;
}
INTEGER f__dec__bs(BITSTRING& b, BITSTRING& x)
{
x = b;
b = BITSTRING(0, NULL);
return 0;
}
} // namespace Custom2
namespace Custom1 {
BITSTRING f__enc__recof(const RecOf& x)
{
BITSTRING res = x[0];
for (int i = 1; i < x.size_of(); ++i) {
res = res + c__separator + x[i];
}
return res;
}
int find_bitstring(const BITSTRING& src, int start, const BITSTRING& fnd)
{
int len = fnd.lengthof();
for (int i = start; i <= src.lengthof() - len; ++i) {
if (substr(src, i, len) == fnd) {
return i;
}
}
return -1;
}
INTEGER f__dec__recof(BITSTRING& b, RecOf& x)
{
int start = 0;
int end = find_bitstring(b, start, c__separator);
int index = 0;
while(end != -1) {
x[index] = substr(b, start, end - start);
++index;
start = end + c__separator.lengthof();
end = find_bitstring(b, start, c__separator);
}
x[index] = substr(b, start, b.lengthof() - start);
b = BITSTRING(0, NULL);
return 0;
}
} // namespace Custom1
namespace AsnAndPer {
BITSTRING f__enc__seqof(const Types::SeqOf& x)
{
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);
TTCN_Buffer buf;
x.encode(Types::SeqOf_descr_, buf, TTCN_EncDec::CT_JSON, false);
OCTETSTRING tmp;
buf.get_string(tmp);
return oct2bit(tmp);
}
INTEGER f__dec__seqof(BITSTRING& x, Types::SeqOf& y)
{
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
TTCN_Buffer buf(bit2oct(x));
y.decode(Types::SeqOf_descr_, buf, TTCN_EncDec::CT_JSON);
switch (TTCN_EncDec::get_last_error_type()) {
case TTCN_EncDec::ET_NONE: {
buf.cut();
OCTETSTRING tmp;
buf.get_string(tmp);
x = oct2bit(tmp);
return 0; }
case TTCN_EncDec::ET_INCOMPL_MSG:
case TTCN_EncDec::ET_LEN_ERR:
return 2;
default:
return 1;
}
}
INTEGER f__dec__choice(BITSTRING& x, Types::Choice& y)
{
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
TTCN_Buffer buf(bit2oct(x));
y.decode(Types::Choice_descr_, buf, TTCN_EncDec::CT_JSON);
switch (TTCN_EncDec::get_last_error_type()) {
case TTCN_EncDec::ET_NONE: {
buf.cut();
OCTETSTRING tmp;
buf.get_string(tmp);
x = oct2bit(tmp);
return 0; }
case TTCN_EncDec::ET_INCOMPL_MSG:
case TTCN_EncDec::ET_LEN_ERR:
return 2;
default:
return 1;
}
}
} // namespace AsnAndPer
namespace Phantom {
BITSTRING f__enc__set(const Custom3::Set& x)
{
return int2bit(x.num(), 8);
}
INTEGER f__dec__set(BITSTRING& b, Custom3::Set& x)
{
x.num() = bit2int(b);
x.str() = "c++";
b = BITSTRING(0, NULL);
return 0;
}
BITSTRING f__enc__setof(const Types::SetOf& x)
{
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);
TTCN_Buffer buf;
x.encode(Types::SetOf_descr_, buf, TTCN_EncDec::CT_JSON, false);
OCTETSTRING tmp;
buf.get_string(tmp);
return oct2bit(tmp);
}
INTEGER f__dec__setof(BITSTRING& x, Types::SetOf& y)
{
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);
TTCN_Buffer buf(bit2oct(x));
y.decode(Types::SetOf_descr_, buf, TTCN_EncDec::CT_JSON);
switch (TTCN_EncDec::get_last_error_type()) {
case TTCN_EncDec::ET_NONE: {
buf.cut();
OCTETSTRING tmp;
buf.get_string(tmp);
x = oct2bit(tmp);
return 0; }
case TTCN_EncDec::ET_INCOMPL_MSG:
case TTCN_EncDec::ET_LEN_ERR:
return 2;
default:
return 1;
}
}
} // namespace Phantom
|