1
// ---------------------------------------------------------------------------
3
// - afnix:itu module - IA5 asn string node class implementation -
4
// ---------------------------------------------------------------------------
5
// - This program is free software; you can redistribute it and/or modify -
6
// - it provided that this copyright notice is kept intact. -
8
// - This program is distributed in the hope that it will be useful, but -
9
// - without any warranty; without even the implied warranty of -
10
// - merchantability or fitness for a particular purpose. In no event shall -
11
// - the copyright holder be liable for any direct, indirect, incidental or -
12
// - special damages arising in any way out of the use of this software. -
13
// ---------------------------------------------------------------------------
14
// - copyright (c) 1999-2011 amaury darsch -
15
// ---------------------------------------------------------------------------
19
#include "AsnNode.hxx"
20
#include "AsnUtils.hpp"
21
#include "Runnable.hpp"
22
#include "AsnBuffer.hpp"
23
#include "QuarkZone.hpp"
24
#include "Exception.hpp"
28
// -------------------------------------------------------------------------
29
// - private section -
30
// -------------------------------------------------------------------------
32
// this procedure converts an octet string vector into a buffer
33
static Buffer asn_ias_cvtn (const Vector& vnod) {
34
// get the node length
35
long vlen = vnod.length ();
38
// loop in the bit string vector
39
for (long i = 0; i < vlen; i++) {
40
Object* obj = vnod.get (i);
41
// check for an asn bit string
42
AsnIas* node = dynamic_cast <AsnIas*> (obj);
44
throw Exception ("asn-error",
45
"invalid object in asn ia string vector",
48
// add into the final buffer
49
result.add (node->tobuffer ());
54
// -------------------------------------------------------------------------
56
// -------------------------------------------------------------------------
58
// create a default asn string
60
AsnIas::AsnIas (void) : AsnOctets (ASN_UNIV_IA5S) {
64
// create an asn string by value
66
AsnIas::AsnIas (const String& sval) : AsnOctets (ASN_UNIV_IA5S) {
69
// check the string argument
70
if (AsnUtils::isias (sval) == false) {
71
throw Exception ("asn-error", "invalid asn ia string", sval);
73
// simply add it into the base buffer - the default mode is BYTE
77
// create a ias string node by node and buffer
79
AsnIas::AsnIas (const AsnNode& node,
80
const Buffer& cbuf) : AsnOctets (ASN_UNIV_IA5S) {
82
AsnNode::operator = (node);
83
// check the node validity
84
if ((d_tagn != ASN_UNIV_IA5S) || (d_iclf != false)) {
85
throw Exception ("asn-error", "invalid asn string node state");
87
// process in primitive mode
88
if (d_cstf == false) {
91
// parse the buffer as a node vector
92
Vector vnod = AsnBuffer::tovnod (cbuf);
93
// set the base buffer from the node vector
94
d_octs = asn_ias_cvtn (vnod);
98
// create an asn string node by node and content node vector
100
AsnIas::AsnIas (const AsnNode& node,
101
const Vector& vnod) : AsnOctets (ASN_UNIV_IA5S) {
102
// check the node validity
103
if ((d_tagn != ASN_UNIV_IA5S) || (d_cstf != true) || (d_iclf != true)) {
104
throw Exception ("asn-error", "invalid asn string node state");
106
// copy the base node
107
AsnNode::operator = (node);
108
// set the base buffer from the node vector
109
d_octs = asn_ias_cvtn (vnod);
112
// return the node class name
114
String AsnIas::repr (void) const {
118
// return a clone of this object
120
Object* AsnIas::clone (void) const {
121
return new AsnIas (*this);
124
// reset this string node
126
void AsnIas::reset (void) {
129
// reset the base node
131
// adjust the tag and value
132
d_tagn = ASN_UNIV_IA5S;
133
// copy the string value
141
// get a string representation
143
String AsnIas::tostring (void) const {
146
String result = d_octs.tostring ();
155
// -------------------------------------------------------------------------
156
// - object section -
157
// -------------------------------------------------------------------------
160
static const long QUARK_ZONE_LENGTH = 1;
161
static QuarkZone zone (QUARK_ZONE_LENGTH);
163
// the object supported quarks
164
static const long QUARK_TOSTRING = zone.intern ("to-string");
166
// create a new object in a generic way
168
Object* AsnIas::mknew (Vector* argv) {
169
long argc = (argv == nilp) ? 0 : argv->length ();
170
// check for 0 argument
171
if (argc == 0) return new AsnIas;
172
// check for 1 argument
174
String sval = argv->getstring (0);
175
return new AsnIas (sval);
177
// too many arguments
178
throw Exception ("argument-error",
179
"too many argument with asn string constructor");
182
// return true if the given quark is defined
184
bool AsnIas::isquark (const long quark, const bool hflg) const {
186
if (zone.exists (quark) == true) {
190
bool result = hflg ? AsnOctets::isquark (quark, hflg) : false;
195
// apply this object with a set of arguments and a quark
197
Object* AsnIas::apply (Runnable* robj, Nameset* nset, const long quark,
199
// get the number of arguments
200
long argc = (argv == nilp) ? 0 : argv->length ();
202
// check for 0 argument
204
if (quark == QUARK_TOSTRING) return new String (tostring ());
206
// call the asn octets method
207
return AsnOctets::apply (robj, nset, quark, argv);