Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
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 | #ifndef _RAW_HH | |
9 | #define _RAW_HH | |
10 | ||
11 | #include "Types.h" | |
12 | #include "Encdec.hh" | |
13 | ||
14 | struct bignum_st; | |
15 | typedef bignum_st BIGNUM; | |
16 | ||
17 | #define RAW_INT_ENC_LENGTH 4 | |
18 | #define REVERSE_BITS(b) (BitReverseTable[(b)&0xFF]) | |
19 | ||
3abe9331 | 20 | #define RAW_INTX -1 |
21 | ||
970ed795 EL |
22 | /** |
23 | * \defgroup RAW RAW-related stuff. | |
24 | * | |
25 | * The RAW encoder/decoder can be used to handle protocols where the position | |
26 | * of information elements must be specified with bit-level precision. | |
27 | * | |
28 | * @{ | |
29 | * | |
30 | * The RAW encoder is a two-pass encoder. In the first pass, information about | |
31 | * all the information elements is collected in a RAW_enc_tree object. | |
32 | * This information is used to write the actual encoding into the buffer. | |
33 | * | |
34 | * This two-pass mechanism is needed because the contents of earlier fields | |
35 | * can depend on fields which have not been encoded yet (e.g. length or | |
36 | * CROSSTAG selector fields). | |
37 | */ | |
38 | ||
39 | extern const unsigned char BitReverseTable[256]; | |
40 | extern const unsigned char BitMaskTable[9]; | |
41 | class RAW_enc_tree; | |
42 | struct TTCN_Typedescriptor_t; | |
43 | struct TTCN_TEXTdescriptor_t; | |
44 | int min_bits(int a); | |
45 | int min_bits(BIGNUM *a); | |
46 | enum ext_bit_t{ | |
47 | EXT_BIT_NO, /**< No extension bit */ | |
48 | EXT_BIT_YES, /**< Extension bit used 0: more octets, 1: no more octets */ | |
49 | EXT_BIT_REVERSE /**< Extension bit used 1: more octets, 0: no more octets */ | |
50 | }; | |
51 | enum top_bit_order_t{ | |
52 | TOP_BIT_INHERITED, | |
53 | TOP_BIT_LEFT, | |
54 | TOP_BIT_RIGHT | |
55 | }; | |
56 | enum raw_sign_t{ | |
57 | SG_NO, /**< no sign, value coded as positive number */ | |
58 | SG_2COMPL, /**< the value coded as 2s complement */ | |
59 | SG_SG_BIT /**< the MSB used to encode the sign of the value */ | |
60 | }; | |
61 | /** position indicator of the encoding tree | |
62 | */ | |
63 | struct RAW_enc_tr_pos{ | |
64 | int level; | |
65 | int *pos; | |
66 | }; | |
67 | ||
68 | struct RAW_enc_pointer{ | |
69 | RAW_enc_tr_pos target; | |
70 | int ptr_offset; | |
71 | int unit; | |
72 | int ptr_base; | |
73 | }; | |
74 | ||
75 | struct RAW_enc_lengthto{ | |
76 | int num_of_fields; | |
77 | RAW_enc_tr_pos* fields; | |
78 | int unit; | |
79 | }; | |
80 | ||
81 | struct RAW_coding_par{ | |
82 | raw_order_t bitorder; | |
83 | raw_order_t byteorder; | |
84 | raw_order_t hexorder; | |
85 | raw_order_t fieldorder; | |
86 | }; | |
87 | ||
88 | /** RAW attributes for runtime */ | |
89 | struct TTCN_RAWdescriptor_t{ | |
90 | int fieldlength; /**< length of field in \a unit s */ | |
91 | raw_sign_t comp; /**< the method used for storing negative numbers */ | |
92 | raw_order_t byteorder; | |
93 | raw_order_t endianness; | |
94 | raw_order_t bitorderinfield; | |
95 | raw_order_t bitorderinoctet; | |
96 | ext_bit_t extension_bit; /**< MSB mangling */ | |
97 | raw_order_t hexorder; | |
98 | raw_order_t fieldorder; | |
99 | top_bit_order_t top_bit_order; | |
100 | int padding; | |
101 | int prepadding; | |
102 | int ptroffset; | |
103 | int unit; /**< number of bits per unit */ | |
104 | int padding_pattern_length; | |
105 | const unsigned char* padding_pattern; | |
106 | int length_restrition; | |
107 | }; | |
108 | ||
109 | enum calc_type { | |
110 | CALC_NO, /**< not a calculated field */ | |
111 | CALC_LENGTH, /**< calculated field for LENGTHTO */ | |
112 | CALC_POINTER /**< calculated field for POINTERTO */ | |
113 | }; | |
114 | ||
115 | /** RAW encoding tree class. | |
116 | * Collects the result of RAW encoding each component, recursively. | |
117 | * */ | |
118 | class RAW_enc_tree{ | |
119 | public: | |
120 | /** indicates that the node is leaf (contains actual data) or not | |
121 | * (contains pointers to other nodes) */ | |
122 | bool isleaf; | |
123 | bool must_free; /**< data_ptr was allocated, must be freed */ | |
124 | bool data_ptr_used; /**< Whether data_ptr member is used, not data_array */ | |
125 | bool rec_of; | |
126 | RAW_enc_tree *parent; | |
127 | RAW_enc_tr_pos curr_pos; | |
128 | int length; /**< Encoded length */ | |
129 | /** @name Encoding parameters related to the filling of the buffer @{ */ | |
130 | int padding; | |
131 | int prepadding; | |
132 | int startpos; | |
133 | int padlength; | |
134 | int prepadlength; | |
135 | int padding_pattern_length; | |
136 | const unsigned char* padding_pattern; | |
137 | int align; /**< alignment length */ | |
138 | /** @} */ | |
139 | int ext_bit_handling; /**< 1: start, 2: stop, 3: only this */ | |
140 | ext_bit_t ext_bit; | |
141 | top_bit_order_t top_bit_order; | |
142 | const TTCN_Typedescriptor_t *coding_descr; | |
143 | calc_type calc; /**< is it a calculated field or not */ | |
144 | RAW_coding_par coding_par; | |
145 | union{ /** depends on calc */ | |
146 | RAW_enc_lengthto lengthto; /**< calc is CALC_LENGTH */ | |
147 | RAW_enc_pointer pointerto; /**< calc is CALC_POINTER */ | |
148 | } calcof; | |
149 | union{ /** depends on isleaf */ | |
150 | struct{ | |
151 | int num_of_nodes; | |
152 | RAW_enc_tree **nodes; | |
153 | } node; /**< isleaf is FALSE */ | |
154 | /** depends on data_ptr_used */ | |
155 | union{ | |
156 | unsigned char *data_ptr; /**< data_ptr_used==true */ | |
157 | unsigned char data_array[RAW_INT_ENC_LENGTH]; /**< false */ | |
158 | } leaf; /**< isleaf is TRUE */ | |
159 | } body; | |
160 | RAW_enc_tree(boolean is_leaf, RAW_enc_tree *par, RAW_enc_tr_pos *par_pos, | |
161 | int my_pos,const TTCN_RAWdescriptor_t *raw_attr); | |
162 | ~RAW_enc_tree(); | |
163 | ||
164 | /** Transfers the encoded information to the buffer. | |
165 | * @param buf buffer to receive the encoded data. | |
166 | * Calls calc_padding, calc_fields and fill_buf. | |
167 | */ | |
168 | void put_to_buf(TTCN_Buffer &buf); | |
169 | RAW_enc_tree* get_node(RAW_enc_tr_pos&); | |
170 | private: | |
171 | void fill_buf(TTCN_Buffer &); | |
172 | int calc_padding(int); | |
173 | void calc_fields(); | |
174 | /// Copy constructor disabled | |
175 | RAW_enc_tree(const RAW_enc_tree&); | |
176 | /// Assignment disabled | |
177 | RAW_enc_tree& operator=(const RAW_enc_tree&); | |
178 | }; | |
179 | struct TTCN_Typedescriptor_t; | |
180 | int RAW_encode_enum_type(const TTCN_Typedescriptor_t&, RAW_enc_tree&, | |
181 | int, int); | |
182 | int RAW_decode_enum_type(const TTCN_Typedescriptor_t&, | |
183 | TTCN_Buffer&, int, raw_order_t, int&, | |
184 | int, bool no_err=false); | |
185 | ||
186 | /// Allocate \p nodes_num pointers | |
187 | RAW_enc_tree** init_nodes_of_enc_tree(int nodes_num); | |
188 | /// Allocate \p num structures | |
189 | RAW_enc_tr_pos* init_lengthto_fields_list(int num); | |
190 | int* init_new_tree_pos(RAW_enc_tr_pos &old_pos,int new_levels, int* new_pos); | |
191 | void free_tree_pos(int* ptr); | |
192 | int min_of_ints(int num_of_int, ...); | |
193 | ||
194 | extern const TTCN_RAWdescriptor_t INTEGER_raw_; | |
195 | extern const TTCN_RAWdescriptor_t BOOLEAN_raw_; | |
196 | extern const TTCN_RAWdescriptor_t BITSTRING_raw_; | |
197 | extern const TTCN_RAWdescriptor_t OCTETSTRING_raw_; | |
198 | extern const TTCN_RAWdescriptor_t CHARSTRING_raw_; | |
199 | extern const TTCN_RAWdescriptor_t HEXSTRING_raw_; | |
200 | extern const TTCN_RAWdescriptor_t FLOAT_raw_; | |
201 | ||
202 | /** @} end of RAW group */ | |
203 | ||
204 | inline int bigger(int l, int r) { | |
205 | return l < r ? r: l; | |
206 | } | |
207 | ||
208 | inline int smaller(int l, int r) { | |
209 | return l < r ? l : r; | |
210 | } | |
211 | ||
212 | ||
213 | #endif // _RAW_HH |