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