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