]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/meta.cpp
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-lib / c++ / meta.cpp
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/meta.C
20 //
21 // $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c++-lib/c++/meta.cpp,v 1.1.1.1 2001/05/18 23:14:06 mb Exp $
22 // $Log: meta.cpp,v $
23 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
24 // Move from private repository to open source repository
25 //
26 // Revision 1.2 2001/05/05 00:59:17 rmurphy
27 // Adding darwin license headers
28 //
29 // Revision 1.1 2000/06/15 18:44:58 dmitch
30 // These snacc-generated source files are now checked in to allow cross-platform build.
31 //
32 // Revision 1.2 2000/06/08 20:05:36 dmitch
33 // Mods for X port. These files are actually machine generated and probably don't need to be in CVS....
34 //
35 // Revision 1.1.1.1 2000/03/09 01:00:06 rmurphy
36 // Base Fortissimo Tree
37 //
38 // Revision 1.1 1999/02/25 05:21:56 mb
39 // Added snacc c++ library
40 //
41 // Revision 1.5 1997/02/28 13:39:47 wan
42 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
43 //
44 // Revision 1.4 1995/08/17 15:23:51 rj
45 // introducing an AsnEnumTypeDesc class with its own TclGetDesc2 function that returns the value names but omits the numeric values.
46 // utility function AsnSe_TypeDesc::mandatmemberr added.
47 //
48 // Revision 1.3 1995/07/26 19:39:35 rj
49 // comment leader fixed
50 //
51 // Revision 1.2 1995/07/25 22:11:31 rj
52 // lots of new data types, and new data and function members in old ones.
53 //
54 // use memcmpeq that is defined in .../snacc.h to use either memcmp or bcmp.
55 //
56 // code extracted from AsnOcts::TclGetVal and AsnOcts::TclSetVal in asn-octs.C into ::debinify and ::binify.
57 //
58 // #if TCL ... #endif wrapped into #if META ... #endif
59 //
60 // call constructor with additional pdu and create arguments.
61 //
62 // changed `_' to `-' in file names.
63
64 #include "asn-incl.h"
65
66 #if META
67
68 AsnMemberDesc::AsnMemberDesc (const char *_name, const AsnTypeDesc *_desc):
69 name (_name),
70 desc (_desc)
71 {
72 }
73
74 AsnMemberDesc::AsnMemberDesc():
75 name (NULL), desc(NULL)
76 {
77 }
78
79 int AsnMemberDesc::TclGetDesc (Tcl_DString *desc) const
80 {
81 if (name)
82 {
83 Tcl_DStringStartSublist (desc);
84 Tcl_DStringAppendElement (desc, (char*)name);
85 this->desc->AsnTypeDesc::TclGetDesc (desc);
86 TclGetDesc2 (desc);
87 Tcl_DStringEndSublist (desc);
88 return TCL_OK;
89 }
90 else
91 return TCL_BREAK;
92 }
93
94 int AsnMemberDesc::TclGetDesc2 (Tcl_DString *desc) const
95 {
96 return TCL_OK;
97 }
98
99 AsnSe_MemberDesc::AsnSe_MemberDesc (const char *name, const AsnTypeDesc *desc, bool _optional):
100 AsnMemberDesc (name, desc),
101 optional (_optional)
102 {
103 }
104
105 AsnSe_MemberDesc::AsnSe_MemberDesc():
106 AsnMemberDesc()
107 {
108 }
109
110 int AsnSe_MemberDesc::TclGetDesc2 (Tcl_DString *desc) const
111 {
112 Tcl_DStringAppendElement (desc, optional ? "optional" : "mandatory");
113 return TCL_OK;
114 }
115
116 const char *const AsnTypeDesc::typenames[] = // NOTE: keep this array in sync with the enum Type
117 {
118 "(void)",
119 "(alias)",
120
121 "INTEGER",
122 "REAL",
123 "NULL",
124 "BOOLEAN",
125 "ENUMERATED",
126 "BIT STRING",
127 "OCTET STRING",
128 "OBJECT IDENTIFIER",
129
130 "SET",
131 "SEQUENCE",
132 "SET OF",
133 "SEQUENCE OF",
134 "CHOICE",
135 "ANY",
136 };
137
138 AsnTypeDesc::AsnTypeDesc (const AsnModuleDesc *_module, const char *_name, bool ispdu, Type _type, AsnType *(*_create)()):
139 module (_module),
140 name (_name),
141 pdu (ispdu),
142 type (_type),
143 create (_create)
144 {
145 }
146
147 const AsnModuleDesc *AsnTypeDesc::getmodule() const
148 {
149 return module;
150 }
151
152 const char *AsnTypeDesc::getname() const
153 {
154 return name;
155 }
156
157 bool AsnTypeDesc::ispdu() const
158 {
159 return pdu;
160 }
161
162 AsnTypeDesc::Type AsnTypeDesc::gettype() const
163 {
164 return type;
165 }
166
167 const AsnNameDesc *AsnTypeDesc::getnames() const
168 {
169 Asn1Error << typenames[type] << "::getnames() called" << endl;
170 abort();
171 return NULL;
172 }
173
174 //const AsnMemberDesc *AsnTypeDesc::getmembers() const
175 //{
176 //Asn1Error << typenames[type] << "::getmembers() called" << endl;
177 //abort();
178 //}
179
180 //\[banner "names types (int, enum)"]-----------------------------------------------------------------------------------------------
181 AsnNamesTypeDesc::AsnNamesTypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)(), const AsnNameDesc *_names):
182 AsnTypeDesc (module, name, ispdu, type, create),
183 names (_names)
184 {
185 }
186
187 const AsnNameDesc *AsnNamesTypeDesc::getnames() const
188 {
189 return names;
190 }
191
192 //\[banner "enum type"]-------------------------------------------------------------------------------------------------------------
193 AsnEnumTypeDesc::AsnEnumTypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)(), const AsnNameDesc *names):
194 AsnNamesTypeDesc (module, name, ispdu, type, create, names)
195 {
196 }
197
198 //\[banner "members types (choice, set, sequence)"]---------------------------------------------------------------------------------
199 AsnMembersTypeDesc::AsnMembersTypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)()):
200 AsnTypeDesc (module, name, ispdu, type, create)
201 {
202 }
203
204 //\[banner "choice type"]-----------------------------------------------------------------------------------------------------------
205 AsnChoiceTypeDesc::AsnChoiceTypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)(), const AsnChoiceMemberDesc *_members):
206 AsnMembersTypeDesc (module, name, ispdu, type, create),
207 members (_members)
208 {
209 }
210
211 int AsnChoiceTypeDesc::choicebyname (const char *name) const
212 {
213 for (int m=0; members[m].name; m++)
214 if (!strcmp (members[m].name, name))
215 return m;
216
217 return -1;
218 }
219
220 const char *AsnChoiceTypeDesc::choicebyvalue (int value) const
221 {
222 return members[value].name;
223 }
224
225 //\[banner "set/sequence type"]-----------------------------------------------------------------------------------------------------
226 AsnSe_TypeDesc::AsnSe_TypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)(), const AsnSe_MemberDesc *_members):
227 AsnMembersTypeDesc (module, name, ispdu, type, create),
228 members (_members)
229 {
230 }
231
232 //\[banner "list type"]-------------------------------------------------------------------------------------------------------------
233 AsnListTypeDesc::AsnListTypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)(), const AsnTypeDesc *_base):
234 AsnTypeDesc (module, name, ispdu, type, create),
235 base (_base)
236 {
237 }
238
239 //\[banner "alias type"]------------------------------------------------------------------------------------------------------------
240 AsnAliasTypeDesc::AsnAliasTypeDesc (const AsnModuleDesc *module, const char *name, bool ispdu, Type type, AsnType *(*create)(), const AsnTypeDesc *_alias):
241 AsnTypeDesc (module, name, ispdu, type, create),
242 alias (_alias)
243 {
244 }
245
246 const AsnModuleDesc *AsnAliasTypeDesc::getmodule() const
247 {
248 return module;
249 }
250
251 const char *AsnAliasTypeDesc::getname() const
252 {
253 return name;
254 }
255
256 bool AsnAliasTypeDesc::ispdu() const
257 {
258 return pdu;
259 }
260
261 AsnTypeDesc::Type AsnAliasTypeDesc::gettype() const
262 {
263 return alias->gettype();
264 }
265
266 const AsnNameDesc *AsnAliasTypeDesc::getnames() const
267 {
268 return alias->getnames();
269 }
270
271 //const AsnMemberDesc *AsnAliasTypeDesc::getmembers() const
272 //{
273 //return alias->getmembers();
274 //}
275
276 //\[banner "Tcl routines"]----------------------------------------------------------------------------------------------------------
277 #if TCL
278
279 int AsnTypeDesc::TclGetDesc (Tcl_DString *desc) const
280 {
281 Tcl_DStringStartSublist (desc);
282 Tcl_DStringAppendElement (desc, getmodule() ? (char*) getmodule()->name : "");
283 Tcl_DStringAppendElement (desc, getname() ? (char*) getname() : "");
284 Tcl_DStringEndSublist (desc);
285 Tcl_DStringAppendElement (desc, ispdu() ? "pdu" : "sub");
286 Tcl_DStringAppendElement (desc, (char*) typenames[gettype()]);
287
288 return TCL_OK;
289 }
290
291 int AsnTypeDesc::TclGetDesc2 (Tcl_DString *desc) const
292 {
293 return TCL_OK;
294 }
295
296 int AsnNamesTypeDesc::TclGetDesc (Tcl_DString *desc) const
297 {
298 AsnTypeDesc::TclGetDesc (desc);
299 return TclGetDesc2 (desc);
300 }
301
302 // for BIT STRING and INTEGER:
303 int AsnNamesTypeDesc::TclGetDesc2 (Tcl_DString *desc) const
304 {
305 Tcl_DStringStartSublist (desc);
306 const AsnNameDesc *n;
307 if (n = names)
308 for (; n->name; n++)
309 {
310 Tcl_DStringStartSublist (desc);
311 Tcl_DStringAppendElement (desc, (char*) n->name);
312 char buf[32];
313 sprintf (buf, "%d", n->value);
314 Tcl_DStringAppendElement (desc, buf);
315 Tcl_DStringEndSublist (desc);
316 }
317 Tcl_DStringEndSublist (desc);
318
319 return TCL_OK;
320 }
321
322 int AsnEnumTypeDesc::TclGetDesc2 (Tcl_DString *desc) const
323 {
324 Tcl_DStringStartSublist (desc);
325 const AsnNameDesc *n;
326 if (n = names)
327 for (; n->name; n++)
328 Tcl_DStringAppendElement (desc, (char*) n->name);
329 Tcl_DStringEndSublist (desc);
330
331 return TCL_OK;
332 }
333
334 int AsnMembersTypeDesc::TclGetDesc (Tcl_DString *desc) const
335 {
336 AsnTypeDesc::TclGetDesc (desc);
337 return TclGetDesc2 (desc);
338 }
339
340 int AsnChoiceTypeDesc::TclGetDesc2 (Tcl_DString *desc) const
341 {
342 Tcl_DStringStartSublist (desc);
343 const AsnChoiceMemberDesc *m;
344 if (m = members)
345 for (; m->TclGetDesc (desc) == TCL_OK; m++)
346 ;
347 Tcl_DStringEndSublist (desc);
348
349 return TCL_OK;
350 }
351
352 int AsnSe_TypeDesc::mandatmemberr (Tcl_Interp *interp, const char *membername) const
353 {
354 sprintf (interp->result, "(in type %s.%s:) member %s is mandatory and can't be deleted", getmodule()->name, getname(), membername);
355 Tcl_SetErrorCode (interp, "SNACC", "MANDMEMB", NULL);
356 return TCL_ERROR;
357 }
358
359 int AsnSe_TypeDesc::TclGetDesc2 (Tcl_DString *desc) const
360 {
361 Tcl_DStringStartSublist (desc);
362 const AsnSe_MemberDesc *m;
363 if (m = members)
364 for (; m->TclGetDesc (desc) == TCL_OK; m++)
365 ;
366 Tcl_DStringEndSublist (desc);
367
368 return TCL_OK;
369 }
370
371 int AsnListTypeDesc::TclGetDesc (Tcl_DString *desc) const
372 {
373 AsnTypeDesc::TclGetDesc (desc);
374 return base->AsnTypeDesc::TclGetDesc (desc);
375 }
376
377 int AsnAliasTypeDesc::TclGetDesc (Tcl_DString *desc) const
378 {
379 AsnTypeDesc::TclGetDesc (desc);
380 return alias->TclGetDesc2 (desc);
381 }
382
383 //\[sep]----------------------------------------------------------------------------------------------------------------------------
384 // designed to be used with Tcl_SplitList(): argument list that automagically frees itself when it goes out of scope:
385 Args::Args()
386 {
387 v = NULL;
388 }
389
390 Args::~Args()
391 {
392 if (v)
393 free (v);
394 }
395
396 //\[sep]----------------------------------------------------------------------------------------------------------------------------
397 // since Tcl cannot handle binary strings, the following hack is needed:
398
399 int debinify (Tcl_Interp *interp, const char *bin, size_t len)
400 {
401 char* str;
402 int i, o;
403
404 #ifndef _IBM_ENC_
405 str = new char[2*len+1];
406 #else
407 str = (char *) mem_mgr_ptr->Get (2*len+2);
408 #endif /* _IBM_ENC_ */
409
410
411 for (o=i=0; i<len; i++)
412 switch (bin[i])
413 {
414 case '\0':
415 str[o++] = '\\';
416 str[o++] = '0';
417 break;
418 case '\\':
419 str[o++] = '\\';
420 // fall thru
421 default:
422 str[o++] = bin[i];
423 }
424 str[o] = '\0';
425
426 Tcl_SetResult (interp, str, TCL_VOLATILE);
427
428 #ifndef _IBM_ENC_
429 delete str;
430 #else
431 mem_mgr_ptr->Put ((void*) str);
432 #endif /* _IBM_ENC_ */
433
434 return TCL_OK;
435 }
436
437 int binify (Tcl_Interp *interp, const char *str, char *buf, size_t *len)
438 {
439 for (*len=0; *str; )
440 if (*str == '\\')
441 switch (*++str)
442 {
443 case '0':
444 buf[(*len)++] = '\0';
445 str++;
446 break;
447 case '\\':
448 buf[(*len)++] = *str++;
449 break;
450 default:
451 Tcl_AppendResult (interp, "illegal use of '\\' in string value", NULL);
452 Tcl_SetErrorCode (interp, "SNACC", "ILLESC", NULL);
453 return TCL_ERROR;
454 }
455 else
456 buf[(*len)++] = *str++;
457
458 return TCL_OK;
459 }
460
461 //\[sep]----------------------------------------------------------------------------------------------------------------------------
462 #endif // TCL
463
464 #endif // META