1 /******************************************************************************
2 * Copyright (c) 2000-2016 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
14 * Szabo, Janos Zoltan – initial implementation
17 ******************************************************************************/
25 typedef bignum_st BIGNUM;
27 #define RAW_INT_ENC_LENGTH 4
28 #define REVERSE_BITS(b) (BitReverseTable[(b)&0xFF])
33 * \defgroup RAW RAW-related stuff.
35 * The RAW encoder/decoder can be used to handle protocols where the position
36 * of information elements must be specified with bit-level precision.
40 * The RAW encoder is a two-pass encoder. In the first pass, information about
41 * all the information elements is collected in a RAW_enc_tree object.
42 * This information is used to write the actual encoding into the buffer.
44 * This two-pass mechanism is needed because the contents of earlier fields
45 * can depend on fields which have not been encoded yet (e.g. length or
46 * CROSSTAG selector fields).
49 extern const unsigned char BitReverseTable[256];
50 extern const unsigned char BitMaskTable[9];
52 struct TTCN_Typedescriptor_t;
53 struct TTCN_TEXTdescriptor_t;
55 int min_bits(BIGNUM *a);
57 EXT_BIT_NO, /**< No extension bit */
58 EXT_BIT_YES, /**< Extension bit used 0: more octets, 1: no more octets */
59 EXT_BIT_REVERSE /**< Extension bit used 1: more octets, 0: no more octets */
67 SG_NO, /**< no sign, value coded as positive number */
68 SG_2COMPL, /**< the value coded as 2s complement */
69 SG_SG_BIT /**< the MSB used to encode the sign of the value */
71 /** position indicator of the encoding tree
73 struct RAW_enc_tr_pos{
78 struct RAW_enc_pointer{
79 RAW_enc_tr_pos target;
85 struct RAW_enc_lengthto{
87 RAW_enc_tr_pos* fields;
91 struct RAW_coding_par{
93 raw_order_t byteorder;
95 raw_order_t fieldorder;
98 /** RAW attributes for runtime */
99 struct TTCN_RAWdescriptor_t{
100 int fieldlength; /**< length of field in \a unit s */
101 raw_sign_t comp; /**< the method used for storing negative numbers */
102 raw_order_t byteorder;
103 raw_order_t endianness;
104 raw_order_t bitorderinfield;
105 raw_order_t bitorderinoctet;
106 ext_bit_t extension_bit; /**< MSB mangling */
107 raw_order_t hexorder;
108 raw_order_t fieldorder;
109 top_bit_order_t top_bit_order;
113 int unit; /**< number of bits per unit */
114 int padding_pattern_length;
115 const unsigned char* padding_pattern;
116 int length_restrition;
120 CALC_NO, /**< not a calculated field */
121 CALC_LENGTH, /**< calculated field for LENGTHTO */
122 CALC_POINTER /**< calculated field for POINTERTO */
125 /** RAW encoding tree class.
126 * Collects the result of RAW encoding each component, recursively.
130 /** indicates that the node is leaf (contains actual data) or not
131 * (contains pointers to other nodes) */
133 bool must_free; /**< data_ptr was allocated, must be freed */
134 bool data_ptr_used; /**< Whether data_ptr member is used, not data_array */
136 RAW_enc_tree *parent;
137 RAW_enc_tr_pos curr_pos;
138 int length; /**< Encoded length */
139 /** @name Encoding parameters related to the filling of the buffer @{ */
145 int padding_pattern_length;
146 const unsigned char* padding_pattern;
147 int align; /**< alignment length */
149 int ext_bit_handling; /**< 1: start, 2: stop, 3: only this */
151 top_bit_order_t top_bit_order;
152 const TTCN_Typedescriptor_t *coding_descr;
153 calc_type calc; /**< is it a calculated field or not */
154 RAW_coding_par coding_par;
155 union{ /** depends on calc */
156 RAW_enc_lengthto lengthto; /**< calc is CALC_LENGTH */
157 RAW_enc_pointer pointerto; /**< calc is CALC_POINTER */
159 union{ /** depends on isleaf */
162 RAW_enc_tree **nodes;
163 } node; /**< isleaf is FALSE */
164 /** depends on data_ptr_used */
166 unsigned char *data_ptr; /**< data_ptr_used==true */
167 unsigned char data_array[RAW_INT_ENC_LENGTH]; /**< false */
168 } leaf; /**< isleaf is TRUE */
170 RAW_enc_tree(boolean is_leaf, RAW_enc_tree *par, RAW_enc_tr_pos *par_pos,
171 int my_pos,const TTCN_RAWdescriptor_t *raw_attr);
174 /** Transfers the encoded information to the buffer.
175 * @param buf buffer to receive the encoded data.
176 * Calls calc_padding, calc_fields and fill_buf.
178 void put_to_buf(TTCN_Buffer &buf);
179 RAW_enc_tree* get_node(RAW_enc_tr_pos&);
181 void fill_buf(TTCN_Buffer &);
182 int calc_padding(int);
184 /// Copy constructor disabled
185 RAW_enc_tree(const RAW_enc_tree&);
186 /// Assignment disabled
187 RAW_enc_tree& operator=(const RAW_enc_tree&);
189 struct TTCN_Typedescriptor_t;
190 int RAW_encode_enum_type(const TTCN_Typedescriptor_t&, RAW_enc_tree&,
192 int RAW_decode_enum_type(const TTCN_Typedescriptor_t&,
193 TTCN_Buffer&, int, raw_order_t, int&,
194 int, bool no_err=false);
196 /// Allocate \p nodes_num pointers
197 RAW_enc_tree** init_nodes_of_enc_tree(int nodes_num);
198 /// Allocate \p num structures
199 RAW_enc_tr_pos* init_lengthto_fields_list(int num);
200 int* init_new_tree_pos(RAW_enc_tr_pos &old_pos,int new_levels, int* new_pos);
201 void free_tree_pos(int* ptr);
202 int min_of_ints(int num_of_int, ...);
204 extern const TTCN_RAWdescriptor_t INTEGER_raw_;
205 extern const TTCN_RAWdescriptor_t BOOLEAN_raw_;
206 extern const TTCN_RAWdescriptor_t BITSTRING_raw_;
207 extern const TTCN_RAWdescriptor_t OCTETSTRING_raw_;
208 extern const TTCN_RAWdescriptor_t CHARSTRING_raw_;
209 extern const TTCN_RAWdescriptor_t HEXSTRING_raw_;
210 extern const TTCN_RAWdescriptor_t FLOAT_raw_;
211 extern const TTCN_RAWdescriptor_t UNIVERSAL_CHARSTRING_raw_;
213 /** @} end of RAW group */
215 inline int bigger(int l, int r) {
219 inline int smaller(int l, int r) {
220 return l < r ? l : r;