implemented decmatch (artf724241)
[deliverable/titan.core.git] / core / Bitstring.hh
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
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 * Beres, Szabolcs
12 * Delic, Adam
13 * Forstner, Matyas
14 * Horvath, Gabriella
15 * Kovacs, Ferenc
16 * Raduly, Csaba
17 * Szabados, Kristof
18 * Szabo, Bence Janos
19 * Szabo, Janos Zoltan – initial implementation
20 * Szalai, Gabor
21 * Tatarka, Gabor
22 *
23 ******************************************************************************/
24 #ifndef BITSTRING_HH
25 #define BITSTRING_HH
26
27 #include "Types.h"
28 #include "Basetype.hh"
29 #include "Template.hh"
30 #include "Optional.hh"
31 #include "RAW.hh"
32 #include "BER.hh"
33
34 class INTEGER;
35 class HEXSTRING;
36 class OCTETSTRING;
37 class CHARSTRING;
38 class BITSTRING_ELEMENT;
39
40 class Module_Param;
41
42 /** bitstring value class.
43 * Refcounted copy-on-write implementation */
44 class BITSTRING : public Base_Type {
45
46 friend class BITSTRING_ELEMENT;
47 friend class BITSTRING_template;
48
49 friend BITSTRING int2bit(const INTEGER& value, int length);
50 friend BITSTRING hex2bit(const HEXSTRING& value);
51 friend BITSTRING oct2bit(const OCTETSTRING& value);
52 friend BITSTRING str2bit(const CHARSTRING& value);
53 friend BITSTRING substr(const BITSTRING& value, int index, int returncount);
54 friend BITSTRING replace(const BITSTRING& value, int index, int len,
55 const BITSTRING& repl);
56
57 struct bitstring_struct;
58 bitstring_struct *val_ptr;
59
60 /** Allocate memory if needed.
61 * @param n_bits the number of bits needed.
62 * @pre n_bits >= 0
63 * @post val_ptr != 0
64 * If n_bits > 0, allocates n_bits/8 bytes of memory
65 * */
66 void init_struct(int n_bits);
67 /// Get the bit at the given index.
68 boolean get_bit(int bit_index) const;
69 /// Assign \p new_value to the bit at the given index
70 void set_bit(int bit_index, boolean new_value);
71 /// Copy-on-write
72 void copy_value();
73 /** Ensures that if the bitstring length is not a multiple of 8 then
74 * the unused bits in the last byte are all cleared. */
75 void clear_unused_bits() const;
76 /// Creates a BITSTRING with pre-allocated but uninitialised memory.
77 BITSTRING(int n_bits);
78
79 public:
80 BITSTRING();
81 BITSTRING(int init_n_bits, const unsigned char* init_bits);
82
83 /** Copy constructor.
84 *
85 * @pre \p other_value must be bound */
86 BITSTRING(const BITSTRING& other_value);
87
88 /** Creates a BITSTRING with a single bit.
89 * @pre \p other_value must be bound */
90 BITSTRING(const BITSTRING_ELEMENT& other_value);
91
92 ~BITSTRING();
93 /// Decrement the reference count and free the memory if it's the last reference
94 void clean_up();
95
96 BITSTRING& operator=(const BITSTRING& other_value);
97 BITSTRING& operator=(const BITSTRING_ELEMENT& other_value);
98
99 boolean operator==(const BITSTRING& other_value) const;
100 boolean operator==(const BITSTRING_ELEMENT& other_value) const;
101
102 inline boolean operator!=(const BITSTRING& other_value) const
103 { return !(*this == other_value); }
104 inline boolean operator!=(const BITSTRING_ELEMENT& other_value) const
105 { return !(*this == other_value); }
106
107 BITSTRING operator+(const BITSTRING& other_value) const;
108 BITSTRING operator+(const BITSTRING_ELEMENT& other_value) const;
109
110 BITSTRING operator~() const;
111 BITSTRING operator&(const BITSTRING& other_value) const;
112 BITSTRING operator&(const BITSTRING_ELEMENT& other_value) const;
113 BITSTRING operator|(const BITSTRING& other_value) const;
114 BITSTRING operator|(const BITSTRING_ELEMENT& other_value) const;
115 BITSTRING operator^(const BITSTRING& other_value) const;
116 BITSTRING operator^(const BITSTRING_ELEMENT& other_value) const;
117
118 BITSTRING operator<<(int shift_count) const;
119 BITSTRING operator<<(const INTEGER& shift_count) const;
120 BITSTRING operator>>(int shift_count) const;
121 BITSTRING operator>>(const INTEGER& shift_count) const;
122 BITSTRING operator<<=(int rotate_count) const;
123 BITSTRING operator<<=(const INTEGER& rotate_count) const;
124 BITSTRING operator>>=(int rotate_count) const;
125 BITSTRING operator>>=(const INTEGER& rotate_count) const;
126
127 BITSTRING_ELEMENT operator[](int index_value);
128 BITSTRING_ELEMENT operator[](const INTEGER& index_value);
129 const BITSTRING_ELEMENT operator[](int index_value) const;
130 const BITSTRING_ELEMENT operator[](const INTEGER& index_value) const;
131
132 inline boolean is_bound() const { return val_ptr != NULL; }
133 inline boolean is_value() const { return val_ptr != NULL; }
134 inline void must_bound(const char *err_msg) const
135 { if (val_ptr == NULL) TTCN_error("%s", err_msg); }
136
137 int lengthof() const;
138
139 operator const unsigned char*() const;
140
141 void log() const;
142
143 #ifdef TITAN_RUNTIME_2
144 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const BITSTRING*>(other_value)); }
145 void set_value(const Base_Type* other_value) { *this = *(static_cast<const BITSTRING*>(other_value)); }
146 Base_Type* clone() const { return new BITSTRING(*this); }
147 const TTCN_Typedescriptor_t* get_descriptor() const { return &BITSTRING_descr_; }
148 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
149 #else
150 inline boolean is_present() const { return is_bound(); }
151 #endif
152
153 void set_param(Module_Param& param);
154 Module_Param* get_param(Module_Param_Name& param_name) const;
155
156 void encode_text(Text_Buf& text_buf) const;
157 void decode_text(Text_Buf& text_buf);
158
159 private:
160 void BER_encode_putbits(unsigned char *target,
161 unsigned int bitnum_start,
162 unsigned int bit_count) const;
163
164 void BER_decode_getbits(const unsigned char *source,
165 size_t s_len, unsigned int& bitnum_start);
166
167 void BER_decode_TLV_(const ASN_BER_TLV_t& p_tlv, unsigned L_form,
168 unsigned int& bitnum_start);
169 public:
170 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
171 TTCN_EncDec::coding_t p_coding, ...) const;
172
173 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
174 TTCN_EncDec::coding_t p_coding, ...);
175
176 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
177 unsigned p_coding) const;
178
179 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
180 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
181
182 /** Encodes the value of the variable according to the
183 * TTCN_Typedescriptor_t. It must be public because called by
184 * another types during encoding. */
185 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
186 /** Decodes the value of the variable according to the
187 * TTCN_Typedescriptor_t. It must be public because called by
188 * another types during encoding. Returns the number of decoded
189 * bits. */
190 int RAW_decode(const TTCN_Typedescriptor_t& , TTCN_Buffer&, int, raw_order_t,
191 boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
192
193 int XER_encode(const XERdescriptor_t&, TTCN_Buffer&, unsigned int, int, embed_values_enc_struct_t*) const;
194 int XER_decode(const XERdescriptor_t&, XmlReaderWrap& reader, unsigned int, unsigned int, embed_values_dec_struct_t*);
195
196 /** Encodes accordingly to the JSON encoding rules.
197 * Returns the length of the encoded data. */
198 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
199
200 /** Decodes accordingly to the JSON decoding rules.
201 * Returns the length of the encoded data. */
202 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
203 };
204
205 class BITSTRING_ELEMENT {
206 boolean bound_flag;
207 BITSTRING& str_val;
208 int bit_pos;
209
210 public:
211 BITSTRING_ELEMENT(boolean par_bound_flag, BITSTRING& par_str_val,
212 int par_bit_pos);
213
214 BITSTRING_ELEMENT& operator=(const BITSTRING& other_value);
215 BITSTRING_ELEMENT& operator=(const BITSTRING_ELEMENT& other_value);
216
217 boolean operator==(const BITSTRING& other_value) const;
218 boolean operator==(const BITSTRING_ELEMENT& other_value) const;
219
220 boolean operator!=(const BITSTRING& other_value) const
221 { return !(*this == other_value); }
222 boolean operator!=(const BITSTRING_ELEMENT& other_value) const
223 { return !(*this == other_value); }
224
225 BITSTRING operator+(const BITSTRING& other_value) const;
226 BITSTRING operator+(const BITSTRING_ELEMENT& other_value) const;
227
228 BITSTRING operator~() const;
229 BITSTRING operator&(const BITSTRING& other_value) const;
230 BITSTRING operator&(const BITSTRING_ELEMENT& other_value) const;
231 BITSTRING operator|(const BITSTRING& other_value) const;
232 BITSTRING operator|(const BITSTRING_ELEMENT& other_value) const;
233 BITSTRING operator^(const BITSTRING& other_value) const;
234 BITSTRING operator^(const BITSTRING_ELEMENT& other_value) const;
235
236 inline boolean is_bound() const { return bound_flag; }
237 inline boolean is_present() const { return bound_flag; }
238 inline boolean is_value() const { return bound_flag; }
239 inline void must_bound(const char *err_msg) const
240 { if (!bound_flag) TTCN_error("%s", err_msg); }
241
242 boolean get_bit() const;
243
244 void log() const;
245 };
246
247 /// bitstring template class
248 struct decmatch_struct;
249
250 class BITSTRING_template : public Restricted_Length_Template {
251 #ifdef __SUNPRO_CC
252 public:
253 #endif
254 struct bitstring_pattern_struct;
255 private:
256 BITSTRING single_value;
257 union {
258 struct {
259 unsigned int n_values;
260 BITSTRING_template *list_value;
261 } value_list;
262 bitstring_pattern_struct *pattern_value;
263 decmatch_struct* dec_match;
264 };
265
266 void copy_template(const BITSTRING_template& other_value);
267 static boolean match_pattern(const bitstring_pattern_struct *string_pattern,
268 const BITSTRING::bitstring_struct *string_value);
269
270 public:
271 BITSTRING_template();
272 BITSTRING_template(template_sel other_value);
273 BITSTRING_template(const BITSTRING& other_value);
274 BITSTRING_template(const BITSTRING_ELEMENT& other_value);
275 BITSTRING_template(const OPTIONAL<BITSTRING>& other_value);
276 BITSTRING_template(unsigned int n_elements,
277 const unsigned char *pattern_elements);
278 BITSTRING_template(const BITSTRING_template& other_value);
279 ~BITSTRING_template();
280 void clean_up();
281
282 BITSTRING_template& operator=(template_sel other_value);
283 BITSTRING_template& operator=(const BITSTRING& other_value);
284 BITSTRING_template& operator=(const BITSTRING_ELEMENT& other_value);
285 BITSTRING_template& operator=(const OPTIONAL<BITSTRING>& other_value);
286 BITSTRING_template& operator=(const BITSTRING_template& other_value);
287
288 BITSTRING_ELEMENT operator[](int index_value);
289 BITSTRING_ELEMENT operator[](const INTEGER& index_value);
290 const BITSTRING_ELEMENT operator[](int index_value) const;
291 const BITSTRING_ELEMENT operator[](const INTEGER& index_value) const;
292
293 boolean match(const BITSTRING& other_value, boolean legacy = FALSE) const;
294 const BITSTRING& valueof() const;
295
296 int lengthof() const;
297
298 void set_type(template_sel template_type, unsigned int list_length = 0);
299 BITSTRING_template& list_item(unsigned int list_index);
300
301 void set_decmatch(Dec_Match_Interface* new_instance);
302
303 void log() const;
304 void log_match(const BITSTRING& match_value, boolean legacy = FALSE) const;
305
306 void set_param(Module_Param& param);
307 Module_Param* get_param(Module_Param_Name& param_name) const;
308
309 void encode_text(Text_Buf& text_buf) const;
310 void decode_text(Text_Buf& text_buf);
311
312 boolean is_present(boolean legacy = FALSE) const;
313 boolean match_omit(boolean legacy = FALSE) const;
314 #ifdef TITAN_RUNTIME_2
315 void valueofv(Base_Type* value) const { *(static_cast<BITSTRING*>(value)) = valueof(); }
316 void set_value(template_sel other_value) { *this = other_value; }
317 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const BITSTRING*>(other_value)); }
318 Base_Template* clone() const { return new BITSTRING_template(*this); }
319 const TTCN_Typedescriptor_t* get_descriptor() const { return &BITSTRING_descr_; }
320 boolean matchv(const Base_Type* other_value, boolean legacy) const { return match(*(static_cast<const BITSTRING*>(other_value)), legacy); }
321 void log_matchv(const Base_Type* match_value, boolean legacy) const { log_match(*(static_cast<const BITSTRING*>(match_value)), legacy); }
322 #else
323 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
324 #endif
325 };
326
327 #endif
This page took 0.039317 seconds and 6 git commands to generate.