Sync with 5.3.0
[deliverable/titan.core.git] / core / RAW.cc
CommitLineData
970ed795
EL
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 <string.h>
9#include "../common/memory.h"
10#include "Types.h"
11#include "Encdec.hh"
12#include "RAW.hh"
13#include "Basetype.hh"
14#include "Integer.hh"
15
16#include <openssl/bn.h>
17
18const unsigned char BitReverseTable[256] =
19{
200x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
210x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
220x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
230x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
240x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
250x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
260x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
270x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
280x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
290x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
300x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
310x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
320x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
330x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
340x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
350x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
360x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
370x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
380x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
390x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
400x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
410x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
420x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
430x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
440x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
450x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
460x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
470x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
480x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
490x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
500x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
510x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
52};
53
54const unsigned char BitMaskTable[9]={
550x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
56};
57
58/**
59 * Initialize the RAW encoding tree. The tree representations makes it easier
60 * to encode/decode structured types with various attributes. Each node in the
61 * tree stores information about the node's parent, position, attributes,
62 * child nodes etc. The root of the tree is on the first level and its ``par''
63 * is always NULL. That's why there's a ``par_pos'' parameter, but it could be
64 * omitted. The first part of the position route in ``curr_pos.pos'' is
65 * inherited from ``par''. The last element is the number of the current
66 * element. Only the leaves carry data. Other nodes are just for construction
67 * purposes.
68 *
69 * @param is_leaf true if it's a node with no children
70 * @param par the parent of the current node
71 * @param par_pos the parent's position
72 * @param my_pos the child node's number of ``par''
73 * @param raw_attr encoding attributes
74 */
75RAW_enc_tree::RAW_enc_tree(boolean is_leaf, RAW_enc_tree *par,
76 RAW_enc_tr_pos *par_pos, int my_pos, const TTCN_RAWdescriptor_t *raw_attr)
77{
78 bool orders = false;
79 isleaf = is_leaf;
80 must_free = false;
81 data_ptr_used = false;
82 rec_of = false;
83 parent = par;
84 curr_pos.pos = (int*) Malloc((par_pos->level+1)*sizeof(int));
85 if (par_pos->level) memcpy((void*) curr_pos.pos, (void*) par_pos->pos,
86 par_pos->level * sizeof(int));
87 curr_pos.level = par_pos->level + 1;
88 curr_pos.pos[curr_pos.level - 1] = my_pos;
89 length = 0;
90 padding = raw_attr->padding;
91 prepadding = raw_attr->prepadding;
92 padding_pattern_length = raw_attr->padding_pattern_length;
93 padding_pattern = raw_attr->padding_pattern;
94 startpos = 0;
95 padlength = 0;
96 prepadlength = 0;
97 align = 0;
98 ext_bit_handling = 0;
99 coding_descr = NULL;
100 ext_bit = raw_attr->extension_bit;
101 top_bit_order = raw_attr->top_bit_order;
102 calc = CALC_NO;
103 if (raw_attr->byteorder == ORDER_MSB) orders = true;
104 if (raw_attr->bitorderinfield == ORDER_MSB) orders = !orders;
105 coding_par.byteorder = orders ? ORDER_MSB : ORDER_LSB;
106 orders = false;
107 if (raw_attr->bitorderinoctet == ORDER_MSB) orders = true;
108 if (raw_attr->bitorderinfield == ORDER_MSB) orders = !orders;
109 coding_par.bitorder = orders ? ORDER_MSB : ORDER_LSB;
110 coding_par.hexorder = raw_attr->hexorder;
111 coding_par.fieldorder = raw_attr->fieldorder;
112 if (isleaf) {
113 body.leaf.data_ptr = NULL;
114 }
115 else {
116 body.node.num_of_nodes = 0;
117 body.node.nodes = NULL;
118 }
119}
120
121RAW_enc_tree::~RAW_enc_tree()
122{
123 if (isleaf) {
124 if (must_free) Free(body.leaf.data_ptr);
125 }
126 else {
127 for (int a = 0; a < body.node.num_of_nodes; a++) {
128 if (body.node.nodes[a] != NULL) delete body.node.nodes[a];
129 }
130 Free(body.node.nodes);
131 }
132 switch (calc) {
133 case CALC_LENGTH:
134 Free(calcof.lengthto.fields);
135 break;
136 case CALC_POINTER:
137 break;
138 default:
139 break;
140 }
141 Free(curr_pos.pos);
142}
143
144void RAW_enc_tree::put_to_buf(TTCN_Buffer &buf){
145//printf("Start put_to_buf\n\r");
146 calc_padding(0);
147//printf("End padding\n\r");
148 calc_fields();
149//printf("End calc\n\r");
150 fill_buf(buf);
151//printf("End fill\n\r");
152}
153
154void RAW_enc_tree::calc_fields()
155{
156 if (isleaf) {
157 int szumm = 0;
158 RAW_enc_tree *atm;
159 switch (calc) {
160 case CALC_LENGTH: {
161 if (calcof.lengthto.unit != -1) {
162 for (int a = 0; a < calcof.lengthto.num_of_fields; a++) {
163 atm = get_node(calcof.lengthto.fields[a]);
164 if (atm) szumm += atm->length + atm->padlength + atm->prepadlength;
165 }
166 szumm = (szumm + calcof.lengthto.unit - 1) / calcof.lengthto.unit;
167 }
168 else {
169 atm = get_node(calcof.lengthto.fields[0]);
170 if (atm) szumm = atm->body.node.num_of_nodes;
171 }
172 INTEGER temp(szumm);
173 temp.RAW_encode(*coding_descr, *this);
174 break; }
175 case CALC_POINTER: {
176 int cl = curr_pos.pos[curr_pos.level - 1];
177 curr_pos.pos[curr_pos.level - 1] = calcof.pointerto.ptr_base;
178 int base = calcof.pointerto.ptr_base;
179 RAW_enc_tree *b = get_node(curr_pos);
180 while (b == NULL) {
181 base++;
182 curr_pos.pos[curr_pos.level - 1] = base;
183 b = get_node(curr_pos);
184 }
185 curr_pos.pos[curr_pos.level - 1] = cl;
186 atm = get_node(calcof.pointerto.target);
187 if (atm) szumm = (atm->startpos - b->startpos + calcof.pointerto.unit - 1
188 - calcof.pointerto.ptr_offset) / calcof.pointerto.unit;
189 INTEGER temp(szumm);
190 temp.RAW_encode(*coding_descr, *this);
191 break; }
192 default:
193 break;
194 }
195 }
196 else {
197 for (int a = 0; a < body.node.num_of_nodes; a++)
198 if (body.node.nodes[a]) body.node.nodes[a]->calc_fields();
199 }
200}
201
202int RAW_enc_tree::calc_padding(int position)
203{
204 int current_pos = position;
205 startpos = position;
206 if (prepadding) {
207 int new_pos = ((current_pos + prepadding - 1) / prepadding) * prepadding;
208 prepadlength = new_pos - position;
209 current_pos = new_pos;
210 }
211 if (!isleaf) {
212 for (int a = 0; a < body.node.num_of_nodes; a++) {
213 if (body.node.nodes[a]) {
214 current_pos = body.node.nodes[a]->calc_padding(current_pos);
215 }
216 }
217 length = current_pos - position - prepadlength;
218 }
219 else current_pos += length;
220 if (padding) {
221 int new_pos = ((current_pos + padding - 1) / padding) * padding;
222 padlength = new_pos - length - position - prepadlength;
223 current_pos = new_pos;
224 }
225 return current_pos;
226}
227
228void RAW_enc_tree::fill_buf(TTCN_Buffer &buf)
229{
230 boolean old_order = buf.get_order();
231 if (top_bit_order != TOP_BIT_INHERITED) buf.set_order(top_bit_order
232 != TOP_BIT_RIGHT);
233 buf.put_pad(prepadlength, padding_pattern, padding_pattern_length,
234 coding_par.fieldorder);
235 if (isleaf) {
236//printf("align: %d, length: %d\n\r",align,length);
237 int align_length = align < 0 ? -align : align;
238 if (ext_bit != EXT_BIT_NO) buf.start_ext_bit(ext_bit == EXT_BIT_REVERSE);
239// if(ext_bit_handling%2) buf.start_ext_bit(ext_bit==EXT_BIT_REVERSE);
240 if (data_ptr_used)
241 buf.put_b(length - align_length, body.leaf.data_ptr, coding_par, align);
242 else
243 buf.put_b(length - align_length, body.leaf.data_array, coding_par, align);
244 if (ext_bit_handling > 1)
245 buf.stop_ext_bit();
246 else if (ext_bit != EXT_BIT_NO && !ext_bit_handling) buf.stop_ext_bit();
247 }
248 else {
249 if (ext_bit != EXT_BIT_NO && (!rec_of || ext_bit_handling % 2))
250 buf.start_ext_bit(ext_bit == EXT_BIT_REVERSE);
251 for (int a = 0; a < body.node.num_of_nodes; a++) {
252 if (body.node.nodes[a]) body.node.nodes[a]->fill_buf(buf);
253 if (ext_bit != EXT_BIT_NO && rec_of && !ext_bit_handling)
254 buf.set_last_bit(ext_bit != EXT_BIT_YES);
255 }
256 if (!ext_bit_handling) {
257 if (ext_bit != EXT_BIT_NO && !rec_of)
258 buf.stop_ext_bit();
259 else if (ext_bit != EXT_BIT_NO && rec_of)
260 buf.set_last_bit(ext_bit == EXT_BIT_YES);
261 }
262 else if (ext_bit_handling > 1) buf.stop_ext_bit();
263 }
264 buf.put_pad(padlength, padding_pattern, padding_pattern_length,
265 coding_par.fieldorder);
266 buf.set_order(old_order);
267}
268
269/**
270 * Return the element at ``req_pos'' from the RAW encoding tree. At first get
271 * the root of the whole tree at the first level. Then go down in the tree
272 * following the route in the ``req_pos.pos'' array. If the element was not
273 * found NULL is returned.
274 *
275 * @param req_pos the position of the element
276 * @return the element at the given position
277 */
278RAW_enc_tree* RAW_enc_tree::get_node(RAW_enc_tr_pos &req_pos)
279{
280 if (req_pos.level == 0) return NULL;
281 RAW_enc_tree* t = this;
282 int cur_level = curr_pos.level;
283 for (int b = 1; b < cur_level; b++) t = t->parent;
284 for (cur_level = 1; cur_level < req_pos.level; cur_level++) {
285 if (!t || t->isleaf || t->body.node.num_of_nodes <= req_pos.pos[cur_level]) return NULL;
286 t = t->body.node.nodes[req_pos.pos[cur_level]];
287 }
288 return t;
289}
290
291/**
292 * Return the number of bits needed to represent an integer value `a'. The
293 * sign bit is added for negative values. It has a different implementation
294 * for `BIGNUM' values.
295 *
296 * @param a the integer in question
297 * @return the number of bits needed to represent the given integer
298 * in sign+magnitude
299 */
300int min_bits(int a)
301{
302 register int bits = 0;
303 register int tmp = a;
304 if (a < 0) {
305 bits = 1;
306 tmp = -a;
307 }
308 while (tmp != 0) {
309 bits++;
310 tmp /= 2;
311 }
312 return bits;
313}
314
315int min_bits(BIGNUM *a)
316{
317 if (!a) return 0;
318 register int bits = BN_num_bits(a) + BN_is_negative(a);
319 return bits;
320}
321
322// Called from code generated by enum.c (defEnumClass)
323int RAW_encode_enum_type(const TTCN_Typedescriptor_t& p_td,
324 RAW_enc_tree& myleaf, int integer_value, int min_bits_enum)
325{
326 int fl = p_td.raw->fieldlength ? p_td.raw->fieldlength : min_bits_enum;
327 TTCN_RAWdescriptor_t my_raw;
328 my_raw.fieldlength = fl;
329 my_raw.comp = p_td.raw->comp;
330 my_raw.byteorder = p_td.raw->byteorder;
331 my_raw.endianness = p_td.raw->endianness;
332 my_raw.bitorderinfield = p_td.raw->bitorderinfield;
333 my_raw.bitorderinoctet = p_td.raw->bitorderinoctet;
334 my_raw.extension_bit = p_td.raw->extension_bit;
335 my_raw.hexorder = p_td.raw->hexorder;
336 my_raw.fieldorder = p_td.raw->fieldorder;
337 my_raw.top_bit_order = p_td.raw->top_bit_order;
338 my_raw.padding = p_td.raw->padding;
339 my_raw.prepadding = p_td.raw->prepadding;
340 my_raw.ptroffset = p_td.raw->ptroffset;
341 my_raw.unit = p_td.raw->unit;
342 TTCN_Typedescriptor_t my_descr = { p_td.name, 0, &my_raw, NULL, NULL, NULL,
a38c6d4c 343 NULL, TTCN_Typedescriptor_t::DONTCARE };
970ed795
EL
344 INTEGER i(integer_value);
345 i.RAW_encode(my_descr, myleaf);
346 // myleaf.align=0;//p_td.raw->endianness==ORDER_MSB?min_bits_enum-fl:fl-min_bits_enum;
347 return myleaf.length = fl;
348}
349
350// Called from code generated by enum.c (defEnumClass)
351int RAW_decode_enum_type(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
352 int limit, raw_order_t top_bit_ord, int& value, int min_bits_enum,
353 bool no_err)
354{
355 int fl = p_td.raw->fieldlength ? p_td.raw->fieldlength : min_bits_enum;
356 TTCN_RAWdescriptor_t my_raw;
357 my_raw.fieldlength = fl;
358 my_raw.comp = p_td.raw->comp;
359 my_raw.byteorder = p_td.raw->byteorder;
360 my_raw.endianness = p_td.raw->endianness;
361 my_raw.bitorderinfield = p_td.raw->bitorderinfield;
362 my_raw.bitorderinoctet = p_td.raw->bitorderinoctet;
363 my_raw.extension_bit = p_td.raw->extension_bit;
364 my_raw.hexorder = p_td.raw->hexorder;
365 my_raw.fieldorder = p_td.raw->fieldorder;
366 my_raw.top_bit_order = p_td.raw->top_bit_order;
367 my_raw.padding = p_td.raw->padding;
368 my_raw.prepadding = p_td.raw->prepadding;
369 my_raw.ptroffset = p_td.raw->ptroffset;
370 my_raw.unit = p_td.raw->unit;
371 TTCN_Typedescriptor_t my_descr = { p_td.name, 0, &my_raw, NULL, NULL, NULL,
a38c6d4c 372 NULL, TTCN_Typedescriptor_t::DONTCARE };
970ed795
EL
373 INTEGER i;
374 /* if(p_td.raw->endianness==ORDER_MSB)
375 buff.increase_pos_bit(fl-min_bits_enum);*/
376 fl = i.RAW_decode(my_descr, buff, limit, top_bit_ord, no_err);
377 if (fl < 0) return fl;
378 value = (int) i;
379 /* if(p_td.raw->endianness==ORDER_LSB)
380 buff.increase_pos_bit(fl-min_bits_enum);*/
381 return fl + buff.increase_pos_padd(p_td.raw->padding);
382}
383
384RAW_enc_tree** init_nodes_of_enc_tree(int nodes_num)
385{
386 RAW_enc_tree ** ret_val=(RAW_enc_tree **) Malloc(nodes_num*sizeof(RAW_enc_tree*));
387 memset(ret_val,0,nodes_num*sizeof(RAW_enc_tree*));
388 return ret_val;
389}
390
391RAW_enc_tr_pos* init_lengthto_fields_list(int num){
392 return (RAW_enc_tr_pos *)Malloc(num*sizeof(RAW_enc_tr_pos));
393}
394
395int* init_new_tree_pos(RAW_enc_tr_pos &old_pos,int new_levels, int* new_pos){
396 int *new_position=(int*)Malloc((old_pos.level+new_levels)*sizeof(int));
397 memcpy(new_position,old_pos.pos,old_pos.level*sizeof(int));
398 memcpy(new_position+old_pos.level,new_pos,new_levels*sizeof(int));
399 return new_position;
400}
401
402void free_tree_pos(int* ptr){
403 Free(ptr);
404}
405
406int min_of_ints(int num_of_int,...)
407{
408 va_list pvar;
409 va_start(pvar,num_of_int);
410 int min_val = 0;
411 if (num_of_int > 0) {
412 min_val = va_arg(pvar, int);
413 for (int a = 1; a < num_of_int; a++) {
414 int b = va_arg(pvar, int);
415 if (b < min_val) min_val = b;
416 }
417 }
418 va_end(pvar);
419 return min_val;
420}
421
422/** Default descriptors of RAW encoding for primitive types. padding
423 * | prepadding
424 * | ptroffset
425 * | unit
426 * | | padding_pattern_length
427 * | | padding_pattern
428 * length,comp ,byteorder,align ,ord_field,ord_octet,ext_bit ,hexorder,fieldorder,top_bit, | | length_restriction*/
429const TTCN_RAWdescriptor_t INTEGER_raw_= {8,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
430const TTCN_RAWdescriptor_t BOOLEAN_raw_= {1,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
431const TTCN_RAWdescriptor_t BITSTRING_raw_= {0,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
432const TTCN_RAWdescriptor_t OCTETSTRING_raw_={0,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
433const TTCN_RAWdescriptor_t HEXSTRING_raw_= {0,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
434const TTCN_RAWdescriptor_t CHARSTRING_raw_= {0,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
435const TTCN_RAWdescriptor_t FLOAT_raw_= {64,SG_NO,ORDER_LSB,ORDER_LSB,ORDER_LSB,ORDER_LSB,EXT_BIT_NO,ORDER_LSB,ORDER_LSB,TOP_BIT_INHERITED,0,0,0,8,0,NULL,-1};
This page took 0.037942 seconds and 5 git commands to generate.