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 "../common/memory.h"
13 #include "Basetype.hh"
16 #include <openssl/bn.h>
18 const unsigned char BitReverseTable
[256] =
20 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
21 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
22 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
23 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
24 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
25 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
26 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
27 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
28 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
29 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
30 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
31 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
32 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
33 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
34 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
35 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
36 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
37 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
38 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
39 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
40 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
41 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
42 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
43 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
44 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
45 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
46 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
47 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
48 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
49 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
50 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
51 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
54 const unsigned char BitMaskTable
[9]={
55 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
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
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
75 RAW_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
)
81 data_ptr_used
= false;
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
;
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
;
100 ext_bit
= raw_attr
->extension_bit
;
101 top_bit_order
= raw_attr
->top_bit_order
;
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
;
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
;
113 body
.leaf
.data_ptr
= NULL
;
116 body
.node
.num_of_nodes
= 0;
117 body
.node
.nodes
= NULL
;
121 RAW_enc_tree::~RAW_enc_tree()
124 if (must_free
) Free(body
.leaf
.data_ptr
);
127 for (int a
= 0; a
< body
.node
.num_of_nodes
; a
++) {
128 if (body
.node
.nodes
[a
] != NULL
) delete body
.node
.nodes
[a
];
130 Free(body
.node
.nodes
);
134 Free(calcof
.lengthto
.fields
);
144 void RAW_enc_tree::put_to_buf(TTCN_Buffer
&buf
){
145 //printf("Start put_to_buf\n\r");
147 //printf("End padding\n\r");
149 //printf("End calc\n\r");
151 //printf("End fill\n\r");
154 void RAW_enc_tree::calc_fields()
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
;
166 szumm
= (szumm
+ calcof
.lengthto
.unit
- 1) / calcof
.lengthto
.unit
;
169 atm
= get_node(calcof
.lengthto
.fields
[0]);
170 if (atm
) szumm
= atm
->body
.node
.num_of_nodes
;
173 temp
.RAW_encode(*coding_descr
, *this);
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
);
182 curr_pos
.pos
[curr_pos
.level
- 1] = base
;
183 b
= get_node(curr_pos
);
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
;
190 temp
.RAW_encode(*coding_descr
, *this);
197 for (int a
= 0; a
< body
.node
.num_of_nodes
; a
++)
198 if (body
.node
.nodes
[a
]) body
.node
.nodes
[a
]->calc_fields();
202 int RAW_enc_tree::calc_padding(int position
)
204 int current_pos
= position
;
207 int new_pos
= ((current_pos
+ prepadding
- 1) / prepadding
) * prepadding
;
208 prepadlength
= new_pos
- position
;
209 current_pos
= new_pos
;
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
);
217 length
= current_pos
- position
- prepadlength
;
219 else current_pos
+= length
;
221 int new_pos
= ((current_pos
+ padding
- 1) / padding
) * padding
;
222 padlength
= new_pos
- length
- position
- prepadlength
;
223 current_pos
= new_pos
;
228 void RAW_enc_tree::fill_buf(TTCN_Buffer
&buf
)
230 boolean old_order
= buf
.get_order();
231 if (top_bit_order
!= TOP_BIT_INHERITED
) buf
.set_order(top_bit_order
233 buf
.put_pad(prepadlength
, padding_pattern
, padding_pattern_length
,
234 coding_par
.fieldorder
);
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);
241 buf
.put_b(length
- align_length
, body
.leaf
.data_ptr
, coding_par
, align
);
243 buf
.put_b(length
- align_length
, body
.leaf
.data_array
, coding_par
, align
);
244 if (ext_bit_handling
> 1)
246 else if (ext_bit
!= EXT_BIT_NO
&& !ext_bit_handling
) buf
.stop_ext_bit();
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
);
256 if (!ext_bit_handling
) {
257 if (ext_bit
!= EXT_BIT_NO
&& !rec_of
)
259 else if (ext_bit
!= EXT_BIT_NO
&& rec_of
)
260 buf
.set_last_bit(ext_bit
== EXT_BIT_YES
);
262 else if (ext_bit_handling
> 1) buf
.stop_ext_bit();
264 buf
.put_pad(padlength
, padding_pattern
, padding_pattern_length
,
265 coding_par
.fieldorder
);
266 buf
.set_order(old_order
);
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.
275 * @param req_pos the position of the element
276 * @return the element at the given position
278 RAW_enc_tree
* RAW_enc_tree::get_node(RAW_enc_tr_pos
&req_pos
)
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
]];
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.
296 * @param a the integer in question
297 * @return the number of bits needed to represent the given integer
302 register int bits
= 0;
303 register int tmp
= a
;
315 int min_bits(BIGNUM
*a
)
318 register int bits
= BN_num_bits(a
) + BN_is_negative(a
);
322 // Called from code generated by enum.c (defEnumClass)
323 int RAW_encode_enum_type(const TTCN_Typedescriptor_t
& p_td
,
324 RAW_enc_tree
& myleaf
, int integer_value
, int min_bits_enum
)
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
,
343 NULL
, TTCN_Typedescriptor_t::DONTCARE
};
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
;
350 // Called from code generated by enum.c (defEnumClass)
351 int 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
,
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
,
372 NULL
, TTCN_Typedescriptor_t::DONTCARE
};
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
;
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
);
384 RAW_enc_tree
** init_nodes_of_enc_tree(int nodes_num
)
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
*));
391 RAW_enc_tr_pos
* init_lengthto_fields_list(int num
){
392 return (RAW_enc_tr_pos
*)Malloc(num
*sizeof(RAW_enc_tr_pos
));
395 int* 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));
402 void free_tree_pos(int* ptr
){
406 int min_of_ints(int num_of_int
,...)
409 va_start(pvar
,num_of_int
);
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
;
422 /** Default descriptors of RAW encoding for primitive types. padding
426 * | | padding_pattern_length
427 * | | padding_pattern
428 * length,comp ,byteorder,align ,ord_field,ord_octet,ext_bit ,hexorder,fieldorder,top_bit, | | length_restriction*/
429 const 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};
430 const 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};
431 const 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};
432 const 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};
433 const 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};
434 const 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};
435 const 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};