~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/mod/itu/shl/AsnIas.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - AsnIas.cpp                                                              -
 
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.                  -
 
7
// -                                                                         -
 
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
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Vector.hpp"
 
18
#include "AsnIas.hpp"
 
19
#include "AsnNode.hxx"
 
20
#include "AsnUtils.hpp"
 
21
#include "Runnable.hpp"
 
22
#include "AsnBuffer.hpp"
 
23
#include "QuarkZone.hpp"
 
24
#include "Exception.hpp"
 
25
 
 
26
namespace afnix {
 
27
 
 
28
  // -------------------------------------------------------------------------
 
29
  // - private section                                                       -
 
30
  // -------------------------------------------------------------------------
 
31
 
 
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 ();
 
36
    // the result buffer
 
37
    Buffer result;
 
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);
 
43
      if (node == nilp) {
 
44
        throw Exception ("asn-error",
 
45
                         "invalid object in asn ia string vector",
 
46
                         Object::repr (obj));
 
47
      }
 
48
      // add into the final buffer
 
49
      result.add (node->tobuffer ());
 
50
    }
 
51
    return result;
 
52
  }
 
53
 
 
54
  // -------------------------------------------------------------------------
 
55
  // - class section                                                         -
 
56
  // -------------------------------------------------------------------------
 
57
 
 
58
  // create a default asn string
 
59
 
 
60
  AsnIas::AsnIas (void) : AsnOctets (ASN_UNIV_IA5S) {
 
61
    reset ();
 
62
  }
 
63
 
 
64
  // create an asn string by value
 
65
 
 
66
  AsnIas::AsnIas (const String& sval) : AsnOctets (ASN_UNIV_IA5S) {
 
67
    // reset the buffer
 
68
    reset ();
 
69
    // check the string argument
 
70
    if (AsnUtils::isias (sval) == false) {
 
71
      throw Exception ("asn-error", "invalid asn ia string", sval);
 
72
    }
 
73
    // simply add it into the base buffer - the default mode is BYTE
 
74
    d_octs.add (sval);
 
75
  }
 
76
  
 
77
  // create a ias string node by node and buffer
 
78
 
 
79
  AsnIas::AsnIas (const AsnNode& node, 
 
80
                  const Buffer&  cbuf) : AsnOctets (ASN_UNIV_IA5S) {
 
81
    // copy the base node
 
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");
 
86
    }
 
87
    // process in primitive mode
 
88
    if (d_cstf == false) {
 
89
      d_octs = cbuf;
 
90
    } else {
 
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);
 
95
    }
 
96
  }
 
97
 
 
98
  // create an asn string node by node and content node vector
 
99
 
 
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");
 
105
    }
 
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);
 
110
  }
 
111
 
 
112
  // return the node class name
 
113
 
 
114
  String AsnIas::repr (void) const {
 
115
    return "AsnIas";
 
116
  }
 
117
 
 
118
  // return a clone of this object
 
119
 
 
120
  Object* AsnIas::clone (void) const {
 
121
    return new AsnIas (*this);
 
122
  }
 
123
 
 
124
  // reset this string node
 
125
 
 
126
  void AsnIas::reset (void) {
 
127
    wrlock ();
 
128
    try {
 
129
      // reset the base node
 
130
      AsnOctets::reset ();
 
131
      // adjust the tag and value
 
132
      d_tagn = ASN_UNIV_IA5S;
 
133
      // copy the string value
 
134
      unlock ();
 
135
    } catch (...) {
 
136
      unlock ();
 
137
      throw;
 
138
    }
 
139
  }
 
140
 
 
141
  // get a string representation
 
142
 
 
143
  String AsnIas::tostring (void) const {
 
144
    rdlock ();
 
145
    try {
 
146
      String result = d_octs.tostring ();
 
147
      unlock ();
 
148
      return result;
 
149
    } catch (...) {
 
150
      unlock ();
 
151
      throw;
 
152
    }
 
153
  }
 
154
  
 
155
  // -------------------------------------------------------------------------
 
156
  // - object section                                                        -
 
157
  // -------------------------------------------------------------------------
 
158
 
 
159
 // the quark zone
 
160
  static const long QUARK_ZONE_LENGTH = 1;
 
161
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
162
 
 
163
  // the object supported quarks
 
164
  static const long QUARK_TOSTRING = zone.intern ("to-string");
 
165
 
 
166
  // create a new object in a generic way
 
167
 
 
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
 
173
    if (argc == 1) {
 
174
      String sval = argv->getstring (0);
 
175
      return new AsnIas (sval);
 
176
    }
 
177
    // too many arguments
 
178
    throw Exception ("argument-error",
 
179
                     "too many argument with asn string constructor");
 
180
  }
 
181
 
 
182
  // return true if the given quark is defined
 
183
 
 
184
  bool AsnIas::isquark (const long quark, const bool hflg) const {
 
185
    rdlock ();
 
186
    if (zone.exists (quark) == true) {
 
187
      unlock ();
 
188
      return true;
 
189
    }
 
190
    bool result = hflg ? AsnOctets::isquark (quark, hflg) : false;
 
191
    unlock ();
 
192
    return result;
 
193
  }
 
194
 
 
195
  // apply this object with a set of arguments and a quark
 
196
 
 
197
  Object* AsnIas::apply (Runnable* robj, Nameset* nset, const long quark,
 
198
                         Vector* argv) {
 
199
    // get the number of arguments
 
200
    long argc = (argv == nilp) ? 0 : argv->length ();
 
201
 
 
202
    // check for 0 argument
 
203
    if (argc == 0) {
 
204
      if (quark == QUARK_TOSTRING) return new String (tostring ());
 
205
    }
 
206
    // call the asn octets method
 
207
    return AsnOctets::apply (robj, nset, quark, argv);
 
208
  }
 
209
}