1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 ///////////////////////////////////////////////////////////////////////////////
11 #include "Basetype.hh"
12 #include "Template.hh"
13 #include "Optional.hh"
20 class OCTETSTRING_ELEMENT;
23 // octetstring value class
25 class OCTETSTRING : public Base_Type {
27 friend class OCTETSTRING_ELEMENT;
28 friend class OCTETSTRING_template;
29 friend class TTCN_Buffer;
31 friend OCTETSTRING int2oct(int value, int length);
32 friend OCTETSTRING int2oct(const INTEGER& value, int length);
33 friend OCTETSTRING str2oct(const CHARSTRING& value);
34 friend OCTETSTRING bit2oct(const BITSTRING& value);
35 friend OCTETSTRING hex2oct(const HEXSTRING& value);
36 friend OCTETSTRING unichar2oct(const UNIVERSAL_CHARSTRING& invalue);
37 friend OCTETSTRING unichar2oct(const UNIVERSAL_CHARSTRING& invalue, const CHARSTRING& string_encoding);
38 friend OCTETSTRING replace(const OCTETSTRING& value, int index, int len,
39 const OCTETSTRING& repl);
41 protected: // for ASN_ANY which is derived from OCTETSTRING
42 struct octetstring_struct;
43 octetstring_struct *val_ptr;
45 void init_struct(int n_octets);
47 OCTETSTRING(int n_octets);
51 OCTETSTRING(int n_octets, const unsigned char* octets_ptr);
52 OCTETSTRING(const OCTETSTRING_ELEMENT& other_value);
53 OCTETSTRING(const OCTETSTRING& other_value);
56 OCTETSTRING& operator=(const OCTETSTRING& other_value);
57 OCTETSTRING& operator=(const OCTETSTRING_ELEMENT& other_value);
59 boolean operator==(const OCTETSTRING& other_value) const;
60 boolean operator==(const OCTETSTRING_ELEMENT& other_value) const;
62 inline boolean operator!=(const OCTETSTRING& other_value) const
63 { return !(*this == other_value); }
64 inline boolean operator!=(const OCTETSTRING_ELEMENT& other_value) const
65 { return !(*this == other_value); }
67 OCTETSTRING operator+(const OCTETSTRING& other_value) const;
68 OCTETSTRING operator+(const OCTETSTRING_ELEMENT& other_value) const;
70 OCTETSTRING& operator+=(const OCTETSTRING& other_value);
71 OCTETSTRING& operator+=(const OCTETSTRING_ELEMENT& other_value);
73 OCTETSTRING operator~() const;
74 OCTETSTRING operator&(const OCTETSTRING& other_value) const;
75 OCTETSTRING operator&(const OCTETSTRING_ELEMENT& other_value) const;
76 OCTETSTRING operator|(const OCTETSTRING& other_value) const;
77 OCTETSTRING operator|(const OCTETSTRING_ELEMENT& other_value) const;
78 OCTETSTRING operator^(const OCTETSTRING& other_value) const;
79 OCTETSTRING operator^(const OCTETSTRING_ELEMENT& other_value) const;
81 OCTETSTRING operator<<(int shift_count) const;
82 OCTETSTRING operator<<(const INTEGER& shift_count) const;
83 OCTETSTRING operator>>(int shift_count) const;
84 OCTETSTRING operator>>(const INTEGER& shift_count) const;
85 OCTETSTRING operator<<=(int rotate_count) const;
86 OCTETSTRING operator<<=(const INTEGER& rotate_count) const;
87 OCTETSTRING operator>>=(int rotate_count) const;
88 OCTETSTRING operator>>=(const INTEGER& rotate_count) const;
90 OCTETSTRING_ELEMENT operator[](int index_value);
91 OCTETSTRING_ELEMENT operator[](const INTEGER& index_value);
92 const OCTETSTRING_ELEMENT operator[](int index_value) const;
93 const OCTETSTRING_ELEMENT operator[](const INTEGER& index_value) const;
95 inline boolean is_bound() const { return val_ptr != NULL; }
96 inline boolean is_value() const { return val_ptr != NULL; }
97 inline void must_bound(const char *err_msg) const
98 { if (val_ptr == NULL) TTCN_error("%s", err_msg); }
101 int lengthof() const;
102 operator const unsigned char*() const;
105 #ifdef TITAN_RUNTIME_2
106 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const OCTETSTRING*>(other_value)); }
107 void set_value(const Base_Type* other_value) { *this = *(static_cast<const OCTETSTRING*>(other_value)); }
108 Base_Type* clone() const { return new OCTETSTRING(*this); }
109 const TTCN_Typedescriptor_t* get_descriptor() const { return &OCTETSTRING_descr_; }
111 inline boolean is_present() const { return is_bound(); }
115 void set_param(Module_Param& param);
116 Module_Param* get_param(Module_Param_Name& param_name) const;
118 void encode_text(Text_Buf& text_buf) const;
119 void decode_text(Text_Buf& text_buf);
122 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
123 TTCN_EncDec::coding_t p_coding, ...) const;
125 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
126 TTCN_EncDec::coding_t p_coding, ...);
128 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
129 unsigned p_coding) const;
130 #ifdef TITAN_RUNTIME_2
131 ASN_BER_TLV_t* BER_encode_negtest_raw() const;
132 virtual int encode_raw(TTCN_Buffer& p_buf) const;
133 virtual int RAW_encode_negtest_raw(RAW_enc_tree& p_myleaf) const;
134 /** Adds this octetstring to the end of a JSON buffer as raw data.
135 * Used during the negative testing of the JSON encoder.
136 * @return The number of bytes added. */
137 int JSON_encode_negtest_raw(JSON_Tokenizer&) const;
139 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
140 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
142 /** Encodes the value of the variable according to the
143 * TTCN_Typedescriptor_t. It must be public because called by
144 * another types during encoding. Returns the length of encoded data*/
145 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
146 /** Decodes the value of the variable according to the
147 * TTCN_Typedescriptor_t. It must be public because called by
148 * another types during encoding. Returns the number of decoded
150 int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer& buff, int limit,
151 raw_order_t top_bit_ord, boolean no_err=FALSE,
152 int sel_field=-1, boolean first_call=TRUE);
153 int TEXT_encode(const TTCN_Typedescriptor_t&,
155 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
156 boolean no_err=FALSE, boolean first_call=TRUE);
157 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
158 unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
159 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
160 unsigned int flavor, embed_values_dec_struct_t*);
162 /** Encodes accordingly to the JSON encoding rules.
163 * Returns the length of the encoded data. */
164 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
166 /** Decodes accordingly to the JSON encoding rules.
167 * Returns the length of the decoded data. */
168 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
172 class OCTETSTRING_ELEMENT {
174 OCTETSTRING& str_val;
178 OCTETSTRING_ELEMENT(boolean par_bound_flag, OCTETSTRING& par_str_val,
181 OCTETSTRING_ELEMENT& operator=(const OCTETSTRING& other_value);
182 OCTETSTRING_ELEMENT& operator=(const OCTETSTRING_ELEMENT& other_value);
184 boolean operator==(const OCTETSTRING& other_value) const;
185 boolean operator==(const OCTETSTRING_ELEMENT& other_value) const;
186 inline boolean operator!=(const OCTETSTRING& other_value) const
187 { return !(*this == other_value); }
188 inline boolean operator!=(const OCTETSTRING_ELEMENT& other_value) const
189 { return !(*this == other_value); }
191 OCTETSTRING operator+(const OCTETSTRING& other_value) const;
192 OCTETSTRING operator+(const OCTETSTRING_ELEMENT& other_value) const;
194 OCTETSTRING operator~() const;
195 OCTETSTRING operator&(const OCTETSTRING& other_value) const;
196 OCTETSTRING operator&(const OCTETSTRING_ELEMENT& other_value) const;
197 OCTETSTRING operator|(const OCTETSTRING& other_value) const;
198 OCTETSTRING operator|(const OCTETSTRING_ELEMENT& other_value) const;
199 OCTETSTRING operator^(const OCTETSTRING& other_value) const;
200 OCTETSTRING operator^(const OCTETSTRING_ELEMENT& other_value) const;
202 inline boolean is_bound() const { return bound_flag; }
203 inline boolean is_present() const { return bound_flag; }
204 inline boolean is_value() const { return bound_flag; }
205 inline void must_bound(const char *err_msg) const
206 { if (!bound_flag) TTCN_error("%s", err_msg); }
208 unsigned char get_octet() const;
213 // octetstring template class
215 class OCTETSTRING_template : public Restricted_Length_Template {
219 struct octetstring_pattern_struct;
221 OCTETSTRING single_value;
224 unsigned int n_values;
225 OCTETSTRING_template *list_value;
227 octetstring_pattern_struct *pattern_value;
230 void copy_template(const OCTETSTRING_template& other_value);
231 static boolean match_pattern(const octetstring_pattern_struct *string_pattern,
232 const OCTETSTRING::octetstring_struct *string_value);
235 OCTETSTRING_template();
236 OCTETSTRING_template(template_sel other_value);
237 OCTETSTRING_template(const OCTETSTRING& other_value);
238 OCTETSTRING_template(const OCTETSTRING_ELEMENT& other_value);
239 OCTETSTRING_template(const OPTIONAL<OCTETSTRING>& other_value);
240 OCTETSTRING_template(unsigned int n_elements,
241 const unsigned short *pattern_elements);
242 OCTETSTRING_template(const OCTETSTRING_template& other_value);
243 ~OCTETSTRING_template();
246 OCTETSTRING_template& operator=(template_sel other_value);
247 OCTETSTRING_template& operator=(const OCTETSTRING& other_value);
248 OCTETSTRING_template& operator=(const OCTETSTRING_ELEMENT& other_value);
249 OCTETSTRING_template& operator=(const OPTIONAL<OCTETSTRING>& other_value);
250 OCTETSTRING_template& operator=(const OCTETSTRING_template& other_value);
252 OCTETSTRING_ELEMENT operator[](int index_value);
253 OCTETSTRING_ELEMENT operator[](const INTEGER& index_value);
254 const OCTETSTRING_ELEMENT operator[](int index_value) const;
255 const OCTETSTRING_ELEMENT operator[](const INTEGER& index_value) const;
257 boolean match(const OCTETSTRING& other_value, boolean legacy = FALSE) const;
258 const OCTETSTRING& valueof() const;
260 int lengthof() const;
262 void set_type(template_sel template_type, unsigned int list_length);
263 OCTETSTRING_template& list_item(unsigned int list_index);
266 void log_match(const OCTETSTRING& match_value, boolean legacy = FALSE) const;
268 void set_param(Module_Param& param);
269 Module_Param* get_param(Module_Param_Name& param_name) const;
271 void encode_text(Text_Buf& text_buf) const;
272 void decode_text(Text_Buf& text_buf);
274 boolean is_present(boolean legacy = FALSE) const;
275 boolean match_omit(boolean legacy = FALSE) const;
276 #ifdef TITAN_RUNTIME_2
277 void valueofv(Base_Type* value) const { *(static_cast<OCTETSTRING*>(value)) = valueof(); }
278 void set_value(template_sel other_value) { *this = other_value; }
279 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const OCTETSTRING*>(other_value)); }
280 Base_Template* clone() const { return new OCTETSTRING_template(*this); }
281 const TTCN_Typedescriptor_t* get_descriptor() const { return &OCTETSTRING_descr_; }
282 boolean matchv(const Base_Type* other_value, boolean legacy) const { return match(*(static_cast<const OCTETSTRING*>(other_value)), legacy); }
283 void log_matchv(const Base_Type* match_value, boolean legacy) const { log_match(*(static_cast<const OCTETSTRING*>(match_value)), legacy); }
285 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;