]> git.saurik.com Git - apple/security.git/blame - SecuritySNACCRuntime/c++-lib/c++/asn-list.cpp
Security-54.1.9.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-lib / c++ / asn-list.cpp
CommitLineData
bac41a7b
A
1/*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19// file: .../c++-lib/src/asn-list.C
20//
21// Mike Sample
22// 92/07/02
23//
24// *** NOTE - this is not tested and not used ****
25// snacc generates a new class for each list type,
26// methods and all.
27// (gcc choked on templates)
28// Copyright (C) 1992 Michael Sample and the University of British Columbia
29//
30// This library is free software; you can redistribute it and/or
31// modify it provided that this copyright/license information is retained
32// in original form.
33//
34// If you modify this file, you must clearly indicate your changes.
35//
36// This source code is distributed in the hope that it will be
37// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
39//
a66d0d4a 40// $Header: /cvs/root/Security/SecuritySNACCRuntime/c++-lib/c++/Attic/asn-list.cpp,v 1.1.1.1 2001/05/18 23:14:06 mb Exp $
bac41a7b
A
41// $Log: asn-list.cpp,v $
42// Revision 1.1.1.1 2001/05/18 23:14:06 mb
43// Move from private repository to open source repository
44//
45// Revision 1.2 2001/05/05 00:59:17 rmurphy
46// Adding darwin license headers
47//
48// Revision 1.1 2000/06/15 18:44:57 dmitch
49// These snacc-generated source files are now checked in to allow cross-platform build.
50//
51// Revision 1.2 2000/06/08 20:05:35 dmitch
52// Mods for X port. These files are actually machine generated and probably don't need to be in CVS....
53//
54// Revision 1.1.1.1 2000/03/09 01:00:06 rmurphy
55// Base Fortissimo Tree
56//
57// Revision 1.3 1999/07/14 23:53:56 aram
58// Made const correct so things build with CW 5.0
59//
60// Revision 1.2 1999/03/21 02:07:36 mb
61// Added Copy to every AsnType.
62//
63// Revision 1.1 1999/02/25 05:21:52 mb
64// Added snacc c++ library
65//
66// Revision 1.8 1997/09/04 13:54:09 wan
67// A little more portability
68//
69// Revision 1.7 1997/01/02 08:42:39 rj
70// names of Tcl*-functions fixed (obviously they weren't needed :-)
71//
72// Revision 1.6 1995/07/24 20:18:15 rj
73// #if TCL ... #endif wrapped into #if META ... #endif
74//
75// call constructor with additional pdu and create arguments.
76//
77// changed `_' to `-' in file names.
78//
79// Revision 1.5 1995/02/18 14:06:02 rj
80// #pragma interface/implementation are GNU specific and need to be wrapped.
81//
82// Revision 1.4 1994/10/08 04:18:25 rj
83// code for meta structures added (provides information about the generated code itself).
84//
85// code for Tcl interface added (makes use of the above mentioned meta code).
86//
87// virtual inline functions (the destructor, the Clone() function, BEnc(), BDec() and Print()) moved from inc/*.h to src/*.C because g++ turns every one of them into a static non-inline function in every file where the .h file gets included.
88//
89// made Print() const (and some other, mainly comparison functions).
90//
91// several `unsigned long int' turned into `size_t'.
92//
93// Revision 1.3 1994/08/31 23:38:24 rj
94// FALSE/TRUE turned into false/true
95//
96// Revision 1.2 1994/08/28 10:01:14 rj
97// comment leader fixed.
98//
99// Revision 1.1 1994/08/28 09:21:02 rj
100// first check-in. for a list of changes to 1.1 please refer to the ChangeLog.
101
102#include "asn-config.h"
103#include "asn-len.h"
104#include "asn-tag.h"
105#include "asn-type.h"
106
107#ifdef __GNUG__
108#pragma implementation
109#endif
110
111#include "asn-list.h"
112
113template <class T>
114void AsnList<T>::SetCurrElmt (unsigned long int index)
115{
116 unsigned long int i;
117 curr = first;
118 for (i = 0; (i < (count-1)) && (i < index); i++)
119 curr = curr->next;
120}
121
122
123// print routine for lists
124template <class T>
125ostream &operator << (ostream &os, AsnList<T> &l)
126{
127 os << "SEQUENCE OF { ";
128
129 l.SetCurrToFirst();
130 for (; l.Curr() != NULL; l.GoNext())
131 {
132 os << *l.Curr();
133 if (l.Curr() != l.Last())
134 os << ", ";
135 }
136
137 os << " }";
138 return os;
139}
140
141
142
143// alloc new list elmt, put at end of list
144// and return the component type
145template <class T>
146T &AsnList<T>::Append()
147{
148 AsnListElmt *newElmt;
149
150 newElmt = new AsnListElmt;
151
152 newElmt->next = NULL;
153
154 if (last == NULL)
155 {
156 newElmt->prev = NULL;
157 first = last = newElmt;
158 }
159 else
160 {
161 newElmt->prev = last;
162 last->next = newElmt;
163 last = newElmt;
164 }
165
166 count++;
167
168 return newElmt->elmt;
169
170} /* AsnList::Append */
171
172
173// alloc new list elmt, put at beggining of list
174// and return the component type
175template <class T>
176T &AsnList<T>::Prepend()
177{
178 AsnListElmt *newElmt;
179
180 newElmt = new AsnListElmt;
181
182 newElmt->prev = NULL;
183
184 if (first == NULL)
185 {
186 newElmt->next = NULL;
187 first = last = newElmt;
188 }
189 else
190 {
191 newElmt->next = first;
192 first->prev = newElmt;
193 first = newElmt;
194 }
195
196 count++;
197
198 return newElmt->elmt;
199
200} /* AsnList::Prepend */
201
202template <class T>
203AsnList<T>& AsnList<T>::AppendAndCopy (T &elmt)
204{
205 AsnListElmt *newElmt;
206
207 newElmt = new AsnListElmt;
208
209 newElmt->elmt = elmt;
210
211 newElmt->next = NULL;
212
213 if (last == NULL)
214 {
215 newElmt->prev = NULL;
216 first = last = newElmt;
217 }
218 else
219 {
220 newElmt->prev = last;
221 last->next = newElmt;
222 last = newElmt;
223 }
224
225 count++;
226
227 return this;
228
229} /* AppendAndCopy */
230
231template <class T>
232AsnList<T>& AsnList<T>::PrependAndCopy (T &elmt)
233{
234 AsnListElmt *newElmt;
235
236 newElmt = new AsnListElmt;
237
238 newElmt->elmt = elmt;
239
240 newElmt->prev = NULL;
241
242 if (first == NULL)
243 {
244 newElmt->next = NULL;
245 first = last = newElmt;
246 }
247 else
248 {
249 newElmt->next = first;
250 first->prev = newElmt;
251 first = newElmt;
252 }
253
254 count++;
255
256 return this;
257
258} /* PrependAndCopy */
259
260template <class T>
261AsnType *AsnList<T>::Clone() const
262{
263 return new T;
264}
265
266template <class T>
267AsnType *AsnList<T>::Copy() const
268{
269 return new T (*this);
270}
271
272template <class T>
273AsnLen AsnList<T>::BEncContent (BUF_TYPE b)
274{
275 AsnListElmt *currElmt;
276 AsnLen sum = 0;
277
278 for (currElmt = last; currElmt != NULL; currElmt = currElmt->prev)
279 sum += currElmt->elmt.BEnc (b);
280
281 return sum;
282}
283
284template <class T>
285void AsnList<T>::BDecContent (BUF_TYPE b, AsnTag tagId, AsnLen elmtLen, AsnLen &bytesDecoded, ENV_TYPE env)
286{
287 T listElmt;
288 AsnTag listElmtTagId;
289 AsnLen localBytesDecoded = 0;
290 AsnLen listElmtLen = 0;
291
292
293 while ((localBytesDecoded < elmtLen) || (elmtLen == INDEFINITE_LEN))
294 {
295 listElmtTagId = BDecTag (b, bytesDecoded, env);
296
297 if ((listElmtTagId == EOC) && (elmtLen == INDEFINITE_LEN))
298 break;
299
300
301 listElmt = Append();
302 listElmtLen = BDecLen (b, bytesDecoded, env);
303 listElmt.BDecContent (b, listElmtTagId, listElmtLen, localBytesDecoded, env);
304 }
305 bytesDecoded += localBytesDecoded;
306
307} /* AsnList<T>::BDecContent */
308
309template <class T>
310AsnLen AsnList<T>::BEnc (BUF_TYPE b)
311{
312 AsnLen l;
313 l = BEncContent (b);
314 l += BEncDefLen (b, l);
315 l += BEncTag1 (b, UNIV, CONS, SEQ_TAG_CODE);
316 return l;
317}
318
319template <class T>
320void AsnList<T>::BDec (BUF_TYPE b, AsnLen &bytesDecoded, ENV_TYPE env)
321{
322 AsnLen elmtLen;
323 if (BDecTag (b, bytesDecoded, env) != MAKE_TAG_ID (UNIV, CONS, SEQ_TAG_CODE))
324 {
325 Asn1Error << "AsnList::BDec: ERROR tag on SEQUENCE OF is wrong." << endl;
326 longjmp (env,-54);
327 }
328 elmtLen = BDecLen (b, bytesDecoded, env);
329
330 BDecContent (b, MAKE_TAG_ID (UNIV, CONS, SEQ_TAG_CODE), elmtLen, bytesDecoded, env);
331}
332
333template <class T, class U>
334int ListsEquiv (AsnList<T>& l1, AsnList<U>& l2)
335{
336 if (l1.Count() != l2.Count())
337 return false;
338
339 l1.SetCurrToFirst();
340 l2.SetCurrToFirst();
341
342 for (; l1.Curr() != NULL; l1.GoNext(), l2.GoNext())
343 {
344 if (*l1.Curr() != *l2.Curr())
345 {
346 return false;
347 }
348 }
349 return true;
350}
351
352#if 0
353#if META
354
355const AsnTypeDesc AsnList::_desc (NULL, NULL, false, AsnTypeDesc::SET_or_SEQUENCE_OF, NULL);
356
357const AsnTypeDesc *AsnList::_getdesc() const
358{
359 return &_desc;
360}
361
362#if TCL
363
364int AsnList::TclGetVal (Tcl_Interp *interp) const
365{
366 return TCL_ERROR;
367}
368
369int AsnList::TclSetVal (Tcl_Interp *interp, const char *valstr)
370{
371 return TCL_ERROR;
372}
373
374#endif /* TCL */
375#endif /* META */
376#endif /* 0 */