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