Sync with 5.2.0
[deliverable/titan.core.git] / core / Charstring.hh
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 CHARSTRING_HH
9 #define CHARSTRING_HH
10
11 #include <sys/types.h>
12 #include <regex.h>
13
14 #include "Types.h"
15 #include "Basetype.hh"
16 #include "Template.hh"
17 #include "Optional.hh"
18
19 class INTEGER;
20 class BITSTRING;
21 class HEXSTRING;
22 class OCTETSTRING;
23 class CHARSTRING_ELEMENT;
24 class UNIVERSAL_CHARSTRING;
25 class UNIVERSAL_CHARSTRING_ELEMENT;
26
27 class Module_Param;
28
29 /** Runtime class for character strings.
30 *
31 * Memory is reserved for the terminating null character
32 * (not counted in the length).
33 *
34 * This class implements the following string types:
35 *
36 * - TTCN-3 \c charstring
37 * - ASN.1 restricted character strings:
38 * - \c NumericString
39 * - \c PrintableString
40 * - \c IA5String
41 * - \c VisibleString.
42 * - ASN.1 time types:
43 * - \c UTCTime
44 * - \c GeneralizedTime
45 *
46 */
47 class CHARSTRING : public Base_Type {
48
49 friend class CHARSTRING_ELEMENT;
50 friend class UNIVERSAL_CHARSTRING;
51 friend class UNIVERSAL_CHARSTRING_ELEMENT;
52 friend class TTCN_Buffer;
53
54 friend boolean operator==(const char* string_value,
55 const CHARSTRING& other_value);
56 friend CHARSTRING operator+(const char* string_value,
57 const CHARSTRING& other_value);
58 friend CHARSTRING operator+(const char* string_value,
59 const CHARSTRING_ELEMENT& other_value);
60
61 friend CHARSTRING bit2str(const BITSTRING& value);
62 friend CHARSTRING hex2str(const HEXSTRING& value);
63 friend CHARSTRING oct2str(const OCTETSTRING& value);
64 friend CHARSTRING unichar2char(const UNIVERSAL_CHARSTRING& value);
65 friend CHARSTRING replace(const CHARSTRING& value, int index, int len,
66 const CHARSTRING& repl);
67 friend UNIVERSAL_CHARSTRING operator+(const universal_char& uchar_value,
68 const UNIVERSAL_CHARSTRING_ELEMENT& other_value);
69 friend UNIVERSAL_CHARSTRING operator+(const universal_char& uchar_value,
70 const UNIVERSAL_CHARSTRING& other_value);
71 friend boolean operator==(const universal_char& uchar_value,
72 const UNIVERSAL_CHARSTRING& other_value);
73 friend boolean operator==(const char *string_value,
74 const UNIVERSAL_CHARSTRING_ELEMENT& other_value);
75 friend UNIVERSAL_CHARSTRING operator+(const char *string_value,
76 const UNIVERSAL_CHARSTRING& other_value);
77 friend UNIVERSAL_CHARSTRING operator+(const char *string_value,
78 const UNIVERSAL_CHARSTRING_ELEMENT& other_value);
79
80 struct charstring_struct;
81 charstring_struct *val_ptr;
82
83 void init_struct(int n_chars);
84 void copy_value();
85 CHARSTRING(int n_chars);
86
87 public:
88 /** Construct an unbound CHARSTRING object */
89 CHARSTRING();
90 /** Construct a CHARSTRING of length 1 */
91 CHARSTRING(char other_value);
92 /** Construct a CHARSTRING from the given C string.
93 * @param chars_ptr may be NULL, resulting in an empty CHARSTRING */
94 CHARSTRING(const char* chars_ptr);
95 /** Construct a CHARSTRING of a given length.
96 *
97 * CHARSTRING(0,NULL) is the most efficient way to a bound, empty string.
98 *
99 * @param n_chars number of characters
100 * @param chars_ptr pointer to string (does not need to be 0-terminated)
101 * @pre at least \p n_chars are readable from \p chars_ptr */
102 CHARSTRING(int n_chars, const char* chars_ptr);
103 /** Copy constructor */
104 CHARSTRING(const CHARSTRING& other_value);
105 /** Construct a CHARSTRING of length one */
106 CHARSTRING(const CHARSTRING_ELEMENT& other_value);
107
108 /** Destructor. Simply calls clean_up() */
109 ~CHARSTRING();
110 void clean_up();
111
112 CHARSTRING& operator=(const char* other_value);
113 CHARSTRING& operator=(const CHARSTRING& other_value);
114 CHARSTRING& operator=(const CHARSTRING_ELEMENT& other_value);
115 CHARSTRING& operator=(const UNIVERSAL_CHARSTRING& other_value);
116
117 boolean operator==(const char* other_value) const;
118 boolean operator==(const CHARSTRING& other_value) const;
119 boolean operator==(const CHARSTRING_ELEMENT& other_value) const;
120 boolean operator==(const UNIVERSAL_CHARSTRING& other_value) const;
121 boolean operator==(const UNIVERSAL_CHARSTRING_ELEMENT& other_value) const;
122
123 inline boolean operator!=(const char* other_value) const
124 { return !(*this == other_value); }
125 inline boolean operator!=(const CHARSTRING& other_value) const
126 { return !(*this == other_value); }
127 inline boolean operator!=(const CHARSTRING_ELEMENT& other_value) const
128 { return !(*this == other_value); }
129
130 CHARSTRING operator+(const char* other_value) const;
131 CHARSTRING operator+(const CHARSTRING& other_value) const;
132 CHARSTRING operator+(const CHARSTRING_ELEMENT& other_value) const;
133 UNIVERSAL_CHARSTRING operator+(const UNIVERSAL_CHARSTRING& other_value) const;
134 UNIVERSAL_CHARSTRING operator+
135 (const UNIVERSAL_CHARSTRING_ELEMENT& other_value) const;
136
137 CHARSTRING& operator+=(char other_value);
138 CHARSTRING& operator+=(const char *other_value);
139 CHARSTRING& operator+=(const CHARSTRING& other_value);
140 CHARSTRING& operator+=(const CHARSTRING_ELEMENT& other_value);
141
142 CHARSTRING operator<<=(int rotate_count) const;
143 CHARSTRING operator<<=(const INTEGER& rotate_count) const;
144 CHARSTRING operator>>=(int rotate_count) const;
145 CHARSTRING operator>>=(const INTEGER& rotate_count) const;
146
147 CHARSTRING_ELEMENT operator[](int index_value);
148 CHARSTRING_ELEMENT operator[](const INTEGER& index_value);
149 const CHARSTRING_ELEMENT operator[](int index_value) const;
150 const CHARSTRING_ELEMENT operator[](const INTEGER& index_value) const;
151
152 operator const char*() const;
153
154 inline boolean is_bound() const { return val_ptr != NULL; }
155 inline boolean is_value() const { return val_ptr != NULL; }
156 inline void must_bound(const char *err_msg) const
157 { if (val_ptr == NULL) TTCN_error("%s", err_msg); }
158
159 int lengthof() const;
160
161 void log() const;
162
163 #ifdef TITAN_RUNTIME_2
164 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const CHARSTRING*>(other_value)); }
165 void set_value(const Base_Type* other_value) { *this = *(static_cast<const CHARSTRING*>(other_value)); }
166 Base_Type* clone() const { return new CHARSTRING(*this); }
167 const TTCN_Typedescriptor_t* get_descriptor() const { return &CHARSTRING_descr_; }
168 #else
169 inline boolean is_present() const { return is_bound(); }
170 #endif
171
172 /** Initializes this object with a module parameter value or concatenates a module
173 * parameter value to the end of this string.
174 * @note UFT-8 strings will be decoded. If the decoding results in multi-octet
175 * characters an error will be thrown. */
176 void set_param(Module_Param& param);
177
178 void encode_text(Text_Buf& text_buf) const;
179 void decode_text(Text_Buf& text_buf);
180
181 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
182 TTCN_EncDec::coding_t p_coding, ...) const;
183
184 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
185 TTCN_EncDec::coding_t p_coding, ...);
186
187 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
188 unsigned p_coding) const;
189
190 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
191 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
192
193 /** Encodes the value of the variable according to the
194 * TTCN_Typedescriptor_t. It must be public because called by
195 * another types during encoding. Returns the length of encoded
196 * data */
197 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
198 /** Decodes the value of the variable according to the
199 * TTCN_Typedescriptor_t. It must be public because called by
200 * another types during encoding. Returns the number of decoded
201 * bits */
202 int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t,
203 boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
204 int TEXT_encode(const TTCN_Typedescriptor_t&,
205 TTCN_Buffer&) const;
206 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
207 boolean no_err=FALSE, boolean first_call=TRUE);
208 int XER_encode(const XERdescriptor_t&, TTCN_Buffer&, unsigned int, int, embed_values_enc_struct_t*) const;
209 int XER_decode(const XERdescriptor_t&, XmlReaderWrap& reader, unsigned int, embed_values_dec_struct_t*);
210
211 /** Returns the charstring in the format a string would appear in C or TTCN-3 code.
212 * Inserts double quotation marks to the beginning and end of the string and
213 * doubles the backlsashes in escaped characters.
214 * Used for JSON encoding.
215 *
216 * Example: "He said\nhis name was \"Al\"." -> "\"He said\\nhis name was \\\"Al\\\".\""
217 * @note The returned character buffer needs to be freed after use. */
218 char* to_JSON_string() const;
219
220 /** Initializes the charstring with a JSON string value.
221 * The double quotation marks from the beginning and end of the JSON string are
222 * removed and double-escaped characters are changed to simple-escaped ones.
223 * JSON characters stored as \u + 4 hex digits are converted to the characters
224 * they represent.
225 * Returns false if any non-charstring characters were found, otherwise true.
226 * Used for JSON decoding.
227 *
228 * Example: "\"He said\\nhis name was \\\"Al\\\".\"" -> "He said\nhis name was \"Al\"."
229 * Example2: "\u0061" -> "a"
230 *
231 * @param check_quotes turn the checking of double quotes (mentioned above) on or off */
232 boolean from_JSON_string(const char* p_value, size_t p_value_len, boolean check_quotes);
233
234 /** Encodes accordingly to the JSON encoding rules.
235 * Returns the length of the encoded data.
236 * @note Since JSON has its own set of escaped characters, the ones in the
237 * charstring need to be double-escaped. */
238 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
239
240 /** Decodes accordingly to the JSON encoding rules.
241 * Returns the length of the decoded data. */
242 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
243
244 #ifdef TITAN_RUNTIME_2
245 virtual int encode_raw(TTCN_Buffer& p_buf) const;
246 #endif
247 };
248
249 class CHARSTRING_ELEMENT {
250 boolean bound_flag;
251 CHARSTRING& str_val;
252 int char_pos;
253
254 public:
255 CHARSTRING_ELEMENT(boolean par_bound_flag, CHARSTRING& par_str_val,
256 int par_char_pos);
257
258 CHARSTRING_ELEMENT& operator=(const char* other_value);
259 CHARSTRING_ELEMENT& operator=(const CHARSTRING& other_value);
260 CHARSTRING_ELEMENT& operator=(const CHARSTRING_ELEMENT& other_value);
261
262 boolean operator==(const char *other_value) const;
263 boolean operator==(const CHARSTRING& other_value) const;
264 boolean operator==(const CHARSTRING_ELEMENT& other_value) const;
265 boolean operator==(const UNIVERSAL_CHARSTRING& other_value) const;
266 boolean operator==(const UNIVERSAL_CHARSTRING_ELEMENT& other_value) const;
267
268 inline boolean operator!=(const char *other_value) const
269 { return !(*this == other_value); }
270 inline boolean operator!=(const CHARSTRING& other_value) const
271 { return !(*this == other_value); }
272 inline boolean operator!=(const CHARSTRING_ELEMENT& other_value) const
273 { return !(*this == other_value); }
274
275 CHARSTRING operator+(const char *other_value) const;
276 CHARSTRING operator+(const CHARSTRING& other_value) const;
277 CHARSTRING operator+(const CHARSTRING_ELEMENT& other_value) const;
278 UNIVERSAL_CHARSTRING operator+(const UNIVERSAL_CHARSTRING& other_value) const;
279 UNIVERSAL_CHARSTRING operator+
280 (const UNIVERSAL_CHARSTRING_ELEMENT& other_value) const;
281
282 inline boolean is_bound() const { return bound_flag; }
283 inline boolean is_present() const { return bound_flag; }
284 inline boolean is_value() const { return bound_flag; }
285 inline void must_bound(const char *err_msg) const
286 { if (!bound_flag) TTCN_error("%s", err_msg); }
287
288 char get_char() const;
289
290 void log() const;
291 };
292
293 extern boolean operator==(const char* string_value,
294 const CHARSTRING& other_value);
295 extern boolean operator==(const char* string_value,
296 const CHARSTRING_ELEMENT& other_value);
297
298 inline boolean operator!=(const char* string_value,
299 const CHARSTRING& other_value)
300 {
301 return !(string_value == other_value);
302 }
303
304 inline boolean operator!=(const char* string_value,
305 const CHARSTRING_ELEMENT& other_value)
306 {
307 return !(string_value == other_value);
308 }
309
310 extern CHARSTRING operator+(const char* string_value,
311 const CHARSTRING& other_value);
312 extern CHARSTRING operator+(const char* string_value,
313 const CHARSTRING_ELEMENT& other_value);
314
315 extern CHARSTRING operator<<=(const char *string_value,
316 const INTEGER& rotate_count);
317 extern CHARSTRING operator>>=(const char *string_value,
318 const INTEGER& rotate_count);
319
320 // charstring template class
321
322 class CHARSTRING_template : public Restricted_Length_Template {
323
324 friend class UNIVERSAL_CHARSTRING_template;
325
326 private:
327 CHARSTRING single_value;
328 union {
329 struct {
330 unsigned int n_values;
331 CHARSTRING_template *list_value;
332 } value_list;
333 struct {
334 boolean min_is_set, max_is_set;
335 char min_value, max_value;
336 } value_range;
337 mutable struct {
338 boolean regexp_init;
339 regex_t posix_regexp;
340 } pattern_value;
341 };
342
343 void copy_template(const CHARSTRING_template& other_value);
344 static void log_pattern(int n_chars, const char *chars_ptr);
345
346 public:
347 CHARSTRING_template();
348 /** Constructor for any/omit/any-or-omit
349 * @param other_value must be ANY_VALUE, OMIT_VALUE or ANY_OR_OMIT
350 */
351 CHARSTRING_template(template_sel other_value);
352 /** Construct from a string
353 * @post template_selection is SPECIFIC_VALUE
354 * @param other_value
355 */
356 CHARSTRING_template(const CHARSTRING& other_value);
357 /** Construct from a one-character string (result of CHARSTRING::op[])
358 * @post template_selection is SPECIFIC_VALUE
359 * @param other_value
360 */
361 CHARSTRING_template(const CHARSTRING_ELEMENT& other_value);
362 /** Construct from an optional string
363 * @post template_selection is SPECIFIC_VALUE or OMIT_VALUE
364 * @param other_value
365 */
366 CHARSTRING_template(const OPTIONAL<CHARSTRING>& other_value);
367 /** Constructor for STRING_PATTERN
368 * @param p_sel must be STRING_PATTERN or else Dynamic Testcase Error
369 * @param p_str pattern string
370 */
371 CHARSTRING_template(template_sel p_sel, const CHARSTRING& p_str);
372 /// Copy constructor
373 CHARSTRING_template(const CHARSTRING_template& other_value);
374
375 ~CHARSTRING_template();
376 void clean_up();
377
378 CHARSTRING_template& operator=(template_sel other_value);
379 CHARSTRING_template& operator=(const CHARSTRING& other_value);
380 CHARSTRING_template& operator=(const CHARSTRING_ELEMENT& other_value);
381 CHARSTRING_template& operator=(const OPTIONAL<CHARSTRING>& other_value);
382 CHARSTRING_template& operator=(const CHARSTRING_template& other_value);
383
384 CHARSTRING_ELEMENT operator[](int index_value);
385 CHARSTRING_ELEMENT operator[](const INTEGER& index_value);
386 const CHARSTRING_ELEMENT operator[](int index_value) const;
387 const CHARSTRING_ELEMENT operator[](const INTEGER& index_value) const;
388
389 boolean match(const CHARSTRING& other_value) const;
390 const CHARSTRING& valueof() const;
391
392 int lengthof() const;
393
394 void set_type(template_sel template_type, unsigned int list_length = 0);
395 CHARSTRING_template& list_item(unsigned int list_index);
396
397 void set_min(const CHARSTRING& min_value);
398 void set_max(const CHARSTRING& max_value);
399
400 void log() const;
401 void log_match(const CHARSTRING& match_value) const;
402
403 void set_param(Module_Param& param);
404
405 void encode_text(Text_Buf& text_buf) const;
406 void decode_text(Text_Buf& text_buf);
407
408 boolean is_present() const;
409 boolean match_omit() const;
410 #ifdef TITAN_RUNTIME_2
411 void valueofv(Base_Type* value) const { *(static_cast<CHARSTRING*>(value)) = valueof(); }
412 void set_value(template_sel other_value) { *this = other_value; }
413 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const CHARSTRING*>(other_value)); }
414 Base_Template* clone() const { return new CHARSTRING_template(*this); }
415 const TTCN_Typedescriptor_t* get_descriptor() const { return &CHARSTRING_descr_; }
416 boolean matchv(const Base_Type* other_value) const { return match(*(static_cast<const CHARSTRING*>(other_value))); }
417 void log_matchv(const Base_Type* match_value) const { log_match(*(static_cast<const CHARSTRING*>(match_value))); }
418 #else
419 void check_restriction(template_res t_res, const char* t_name=NULL) const;
420 #endif
421
422 /** Returns the single_value member
423 * @pre template_selection is SPECIFIC_VALUE or STRING_PATTERN */
424 const CHARSTRING& get_single_value() const;
425 };
426
427 typedef CHARSTRING NumericString;
428 typedef CHARSTRING PrintableString;
429 typedef CHARSTRING IA5String;
430 typedef CHARSTRING VisibleString;
431 typedef VisibleString ISO646String;
432
433 typedef CHARSTRING_template NumericString_template;
434 typedef CHARSTRING_template PrintableString_template;
435 typedef CHARSTRING_template IA5String_template;
436 typedef CHARSTRING_template VisibleString_template;
437 typedef VisibleString_template ISO646String_template;
438
439 typedef VisibleString ASN_GeneralizedTime;
440 typedef VisibleString ASN_UTCTime;
441
442 typedef VisibleString_template ASN_GeneralizedTime_template;
443 typedef VisibleString_template ASN_UTCTime_template;
444
445 // Only for backward compatibility
446 typedef CHARSTRING CHAR;
447 typedef CHARSTRING_template CHAR_template;
448
449 // TTCN_TYPE is a class that represents a ttcn-3 value or template
450 template<typename TTCN_TYPE>
451 CHARSTRING ttcn_to_string(const TTCN_TYPE& ttcn_data) {
452 Logger_Format_Scope logger_format(TTCN_Logger::LF_TTCN);
453 TTCN_Logger::begin_event_log2str();
454 ttcn_data.log();
455 return TTCN_Logger::end_event_log2str();
456 }
457
458 template<typename TTCN_TYPE>
459 void string_to_ttcn(const CHARSTRING& ttcn_string, TTCN_TYPE& ttcn_value) {
460 Module_Param* parsed_mp = process_config_string2ttcn((const char*)ttcn_string, ttcn_value.is_component());
461 try {
462 Ttcn_String_Parsing ttcn_string_parsing;
463 ttcn_value.set_param(*parsed_mp);
464 delete parsed_mp;
465 }
466 catch (...) {
467 delete parsed_mp;
468 throw;
469 }
470 }
471
472 #endif
This page took 0.042094 seconds and 5 git commands to generate.