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
19 * Szabo, Janos Zoltan – initial implementation
23 ******************************************************************************/
28 #include "Basetype.hh"
29 #include "Template.hh"
30 #include "Optional.hh"
38 class BITSTRING_ELEMENT;
42 /** bitstring value class.
43 * Refcounted copy-on-write implementation */
44 class BITSTRING : public Base_Type {
46 friend class BITSTRING_ELEMENT;
47 friend class BITSTRING_template;
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);
57 struct bitstring_struct;
58 bitstring_struct *val_ptr;
60 /** Allocate memory if needed.
61 * @param n_bits the number of bits needed.
64 * If n_bits > 0, allocates n_bits/8 bytes of memory
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);
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);
81 BITSTRING(int init_n_bits, const unsigned char* init_bits);
85 * @pre \p other_value must be bound */
86 BITSTRING(const BITSTRING& other_value);
88 /** Creates a BITSTRING with a single bit.
89 * @pre \p other_value must be bound */
90 BITSTRING(const BITSTRING_ELEMENT& other_value);
93 /// Decrement the reference count and free the memory if it's the last reference
96 BITSTRING& operator=(const BITSTRING& other_value);
97 BITSTRING& operator=(const BITSTRING_ELEMENT& other_value);
99 boolean operator==(const BITSTRING& other_value) const;
100 boolean operator==(const BITSTRING_ELEMENT& other_value) const;
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); }
107 BITSTRING operator+(const BITSTRING& other_value) const;
108 BITSTRING operator+(const BITSTRING_ELEMENT& other_value) const;
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;
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;
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;
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); }
137 int lengthof() const;
139 operator const unsigned char*() const;
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;
150 inline boolean is_present() const { return is_bound(); }
153 void set_param(Module_Param& param);
154 Module_Param* get_param(Module_Param_Name& param_name) const;
156 void encode_text(Text_Buf& text_buf) const;
157 void decode_text(Text_Buf& text_buf);
160 void BER_encode_putbits(unsigned char *target,
161 unsigned int bitnum_start,
162 unsigned int bit_count) const;
164 void BER_decode_getbits(const unsigned char *source,
165 size_t s_len, unsigned int& bitnum_start);
167 void BER_decode_TLV_(const ASN_BER_TLV_t& p_tlv, unsigned L_form,
168 unsigned int& bitnum_start);
170 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
171 TTCN_EncDec::coding_t p_coding, ...) const;
173 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
174 TTCN_EncDec::coding_t p_coding, ...);
176 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
177 unsigned p_coding) const;
179 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
180 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
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
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);
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*);
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;
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);
205 class BITSTRING_ELEMENT {
211 BITSTRING_ELEMENT(boolean par_bound_flag, BITSTRING& par_str_val,
214 BITSTRING_ELEMENT& operator=(const BITSTRING& other_value);
215 BITSTRING_ELEMENT& operator=(const BITSTRING_ELEMENT& other_value);
217 boolean operator==(const BITSTRING& other_value) const;
218 boolean operator==(const BITSTRING_ELEMENT& other_value) const;
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); }
225 BITSTRING operator+(const BITSTRING& other_value) const;
226 BITSTRING operator+(const BITSTRING_ELEMENT& other_value) const;
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;
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); }
242 boolean get_bit() const;
247 /// bitstring template class
248 class BITSTRING_template : public Restricted_Length_Template {
252 struct bitstring_pattern_struct;
254 BITSTRING single_value;
257 unsigned int n_values;
258 BITSTRING_template *list_value;
260 bitstring_pattern_struct *pattern_value;
263 void copy_template(const BITSTRING_template& other_value);
264 static boolean match_pattern(const bitstring_pattern_struct *string_pattern,
265 const BITSTRING::bitstring_struct *string_value);
268 BITSTRING_template();
269 BITSTRING_template(template_sel other_value);
270 BITSTRING_template(const BITSTRING& other_value);
271 BITSTRING_template(const BITSTRING_ELEMENT& other_value);
272 BITSTRING_template(const OPTIONAL<BITSTRING>& other_value);
273 BITSTRING_template(unsigned int n_elements,
274 const unsigned char *pattern_elements);
275 BITSTRING_template(const BITSTRING_template& other_value);
276 ~BITSTRING_template();
279 BITSTRING_template& operator=(template_sel other_value);
280 BITSTRING_template& operator=(const BITSTRING& other_value);
281 BITSTRING_template& operator=(const BITSTRING_ELEMENT& other_value);
282 BITSTRING_template& operator=(const OPTIONAL<BITSTRING>& other_value);
283 BITSTRING_template& operator=(const BITSTRING_template& other_value);
285 BITSTRING_ELEMENT operator[](int index_value);
286 BITSTRING_ELEMENT operator[](const INTEGER& index_value);
287 const BITSTRING_ELEMENT operator[](int index_value) const;
288 const BITSTRING_ELEMENT operator[](const INTEGER& index_value) const;
290 boolean match(const BITSTRING& other_value, boolean legacy = FALSE) const;
291 const BITSTRING& valueof() const;
293 int lengthof() const;
295 void set_type(template_sel template_type, unsigned int list_length);
296 BITSTRING_template& list_item(unsigned int list_index);
299 void log_match(const BITSTRING& match_value, boolean legacy = FALSE) const;
301 void set_param(Module_Param& param);
302 Module_Param* get_param(Module_Param_Name& param_name) const;
304 void encode_text(Text_Buf& text_buf) const;
305 void decode_text(Text_Buf& text_buf);
307 boolean is_present(boolean legacy = FALSE) const;
308 boolean match_omit(boolean legacy = FALSE) const;
309 #ifdef TITAN_RUNTIME_2
310 void valueofv(Base_Type* value) const { *(static_cast<BITSTRING*>(value)) = valueof(); }
311 void set_value(template_sel other_value) { *this = other_value; }
312 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const BITSTRING*>(other_value)); }
313 Base_Template* clone() const { return new BITSTRING_template(*this); }
314 const TTCN_Typedescriptor_t* get_descriptor() const { return &BITSTRING_descr_; }
315 boolean matchv(const Base_Type* other_value, boolean legacy) const { return match(*(static_cast<const BITSTRING*>(other_value)), legacy); }
316 void log_matchv(const Base_Type* match_value, boolean legacy) const { log_match(*(static_cast<const BITSTRING*>(match_value)), legacy); }
318 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;