]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/tbl-dbg.c
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-lib / src / tbl-dbg.c
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 #ifdef TTBL
20 #include "tbl-dbg.h"
21
22 TdeExceptionCode DBGMinCode = TDEINFO;
23
24 void DBGOcts PARAMS ((v),
25 AsnOcts* v)
26 {
27 int i;
28 for (i = 0; i < v->octetLen; i++)
29 fprintf (stdout, "%c", isprint(v->octs[i])?v->octs[i]:'.');
30 }
31
32 char*
33 Class2ClassStr PARAMS ((class),
34 int class)
35 {
36 switch (class)
37 {
38 case UNIV:
39 return "UNIV";
40 break;
41
42 case APPL:
43 return "APPL";
44 break;
45
46 case CNTX:
47 return "CNTX";
48 break;
49
50 case PRIV:
51 return "PRIV";
52 break;
53
54 default:
55 return "UNKNOWN";
56 break;
57 }
58 }
59
60 char*
61 Form2FormStr PARAMS ((form),
62 BER_FORM form)
63 {
64 switch (form)
65 {
66 case PRIM:
67 return "PRIM";
68 break;
69
70 case CONS:
71 return "CONS";
72 break;
73
74 default:
75 return "UNKNOWN";
76 break;
77 }
78 }
79
80 char*
81 Code2UnivCodeStr PARAMS ((code),
82 BER_UNIV_CODE code)
83 {
84 switch (code)
85 {
86 case BOOLEAN_TAG_CODE:
87 return "BOOLEAN";
88 break;
89
90 case INTEGER_TAG_CODE:
91 return "INTEGER";
92 break;
93
94 case BITSTRING_TAG_CODE:
95 return "BIT STRING";
96 break;
97
98 case OCTETSTRING_TAG_CODE:
99 return "OCTET STRING";
100 break;
101
102 case NULLTYPE_TAG_CODE:
103 return "NULL";
104 break;
105
106 case OID_TAG_CODE:
107 return "OBJECT IDENTIFIER";
108 break;
109
110 case OD_TAG_CODE:
111 return "OD";
112 break;
113
114 case EXTERNAL_TAG_CODE:
115 return "EXTERNAL";
116 break;
117
118 case REAL_TAG_CODE:
119 return "REAL";
120 break;
121
122 case ENUM_TAG_CODE:
123 return "ENUM";
124 break;
125
126 case SEQ_TAG_CODE:
127 return "SEQUENCE";
128 break;
129
130 case SET_TAG_CODE:
131 return "SET";
132 break;
133
134 case NUMERICSTRING_TAG_CODE:
135 return "NUMERICSTRING";
136 break;
137
138 case PRINTABLESTRING_TAG_CODE:
139 return "PRINTABLESTRING";
140 break;
141
142 case TELETEXSTRING_TAG_CODE:
143 return "TELETEXSTRING";
144 break;
145
146 case VIDEOTEXSTRING_TAG_CODE:
147 return "VIDEOTEXSTRING";
148 break;
149
150 case IA5STRING_TAG_CODE:
151 return "IA5STRING";
152 break;
153
154 case UTCTIME_TAG_CODE:
155 return "UTCTIME";
156 break;
157
158 case GENERALIZEDTIME_TAG_CODE:
159 return "GENERALIZEDTIME";
160 break;
161
162 case GRAPHICSTRING_TAG_CODE:
163 return "GRAPHICSTRING";
164 break;
165
166 case VISIBLESTRING_TAG_CODE:
167 return "VISIBLESTRING";
168 break;
169
170 case GENERALSTRING_TAG_CODE:
171 return "GENERALSTRING";
172 break;
173
174 default:
175 return "UNKNOWN";
176
177 }
178 } /* Form2FormStr */
179
180 #define SOT 0
181 #define EOL 1
182 #define EOLINC 2
183 #define DECSOTEOL 3
184 #define SOTSPC 4
185 #define EOLIF 5
186
187 void DBGIndent PARAMS ((mode),
188 int mode)
189 {
190 static int indent = 0;
191 static int withinline = 0;
192 int i;
193
194 /*DEC*/
195 if (mode==DECSOTEOL)
196 indent--;
197
198 /*SPC*/
199 if (mode==SOTSPC && withinline)
200 fprintf(stdout," ");
201
202 /*SOT*/
203 if ((mode==SOT || mode==DECSOTEOL || mode==SOTSPC) && !withinline)
204 {
205 for (i=0; i<indent; i++)
206 fprintf(stdout," ");
207 withinline = 1;
208 }
209
210 /*IF*/
211 if (mode==EOLIF && withinline)
212 fprintf(stdout,"\n");
213
214 /*EOL*/
215 if (mode==EOL || mode==EOLINC || mode==DECSOTEOL || mode==EOLIF)
216 withinline = 0;
217
218 /*INC*/
219 if (mode==EOLINC)
220 indent++;
221 }
222
223 int DBGSimple PARAMS ((tag, v, begin),
224 AsnTag tag _AND_
225 AsnOcts* v _AND_
226 int begin)
227 {
228 BER_CLASS tclass = TAG_ID_CLASS(tag);
229 BER_FORM form = TAG_ID_FORM(tag);
230 unsigned long int code = tag & 0x1FFFFFFF;
231 BER_UNIV_CODE bcode;
232 char* codename;
233 int i;
234 if (tclass==UNIV)
235 {
236 bcode = code>>24;
237 codename = Code2UnivCodeStr(bcode);
238 }
239 else
240 {
241 bcode = OCTETSTRING_TAG_CODE;
242 codename = "NOT_UNIV";
243 }
244 if (begin)
245 {
246 DBGIndent(SOTSPC);
247 fprintf (stdout, "%s", codename);
248 if (TAG_IS_CONS(tag))
249 {
250 if (tclass==UNIV)
251 {
252 fprintf(stdout, " {\n");
253 DBGIndent(EOLINC);
254 }
255 }
256 else
257 {
258 fprintf(stdout,": ");
259 PrintAsnOcts(stdout,v,0);
260 fprintf(stdout,"\n");
261 DBGIndent(EOL);
262 }
263 }
264 else
265 {
266 if (TAG_IS_CONS(tag) && tclass==UNIV)
267 {
268 DBGIndent(DECSOTEOL);
269 fprintf(stdout, "}\n");
270 }
271 }
272 return 0;
273 }
274
275 void DBGNamedValue PARAMS ((tnnl, val, mode),
276 TBLNamedNumberList* tnnl _AND_
277 AsnInt val _AND_
278 int mode)
279 {
280 /* mode 0: Don't print if no named value. postfix print with -- */
281 /* mode 0|1: prefix with -- */
282 /* mode >1: prefix with , */
283 TBLNamedNumber* tnn;
284 char* name = NULL;
285 FOR_EACH_LIST_ELMT (tnn, tnnl)
286 if (tnn->value == val)
287 {
288 name = tnn->name.octs;
289 break;
290 }
291 if (!mode && !name)
292 return;
293 if (mode<=1)
294 fprintf(stdout," -- ");
295 else
296 fprintf(stdout,", ");
297 if (name)
298 fprintf(stdout,"%s",name);
299 fprintf(stdout,"(%d)",val);
300 if (!mode)
301 fprintf(stdout," --");
302 }
303
304 void
305 DBGPrintType PARAMS ((type),
306 TBLType* type)
307 {
308 static char* TIN [] = { "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
309 "NULL", "OBJECT IDENTIFIER", "REAL", "ENUMERATED", "SEQUENCE", "SET",
310 "SEQUENCE OF", "SET OF", "CHOICE", NULL };
311
312 if (type->typeId == TBL_TYPEREF)
313 DBGOcts(&type->content->a.typeRef->typeDefPtr->typeName);
314 else
315 fprintf(stdout,TIN[type->typeId]);
316 if (type->fieldName.octetLen)
317 {
318 fprintf(stdout," ");
319 DBGOcts(&type->fieldName);
320 }
321 }
322
323 typedef int (*Proc) PROTO (());
324
325 int
326 DBGType PARAMS ((type, val, begin),
327 TBLType* type _AND_
328 AVal* val _AND_
329 int begin)
330 {
331 static Proc printproc [] = {PrintAsnBool, PrintAsnInt, PrintAsnBits,
332 PrintAsnOcts,
333 PrintAsnNull, PrintAsnOid, PrintAsnReal, PrintAsnInt, NULL, NULL,
334 NULL, NULL, NULL,
335 NULL};
336
337 if (begin)
338 {
339 DBGIndent(SOTSPC);
340 DBGPrintType(type);
341 if (type->typeId >= TBL_SEQUENCE && type->typeId <= TBL_CHOICE)
342 {
343 fprintf(stdout," {\n");
344 DBGIndent(EOLINC);
345 }
346 }
347 else
348 {
349 if (printproc[type->typeId])
350 {
351 DBGIndent(SOT);
352 fprintf(stdout,": ");
353 (*printproc[type->typeId])(stdout,val,0);
354 switch (type->typeId)
355 {
356 case TBL_BITSTRING:
357 {
358 AsnInt i;
359 AsnBits* b = (AsnBits*)val;
360 int mode = 1;
361 for (i=0; i<b->bitLen;i++)
362 if (GetAsnBit(b,i))
363 DBGNamedValue(type->values,i,mode++);
364 if (mode>1)
365 fprintf(stdout," --");
366 }
367 break;
368 case TBL_ENUMERATED:
369 DBGNamedValue(type->values,*(AsnInt*)val,0);
370 break;
371 default:
372 break;
373 }
374 fprintf(stdout,"\n");
375 DBGIndent(EOL);
376 }
377 if (type->typeId >= TBL_SEQUENCE && type->typeId <= TBL_CHOICE)
378 {
379 DBGIndent(DECSOTEOL);
380 fprintf(stdout,"}\n");
381 }
382 }
383 return 0;
384 }
385
386 int DBGExc PARAMS ((code, p1, p2, p3),
387 TdeExceptionCode code _AND_
388 void* p1 _AND_
389 void* p2 _AND_
390 void* p3)
391 {
392 if (code<DBGMinCode)
393 return 0;
394 switch (code)
395 {
396 case TDEEOC:
397 DBGIndent(SOTSPC);
398 fprintf(stdout,"[EOC]");
399 break;
400 case TDEPEEKTAG:
401 DBGIndent(SOTSPC);
402 fprintf(stdout,"[%08X]",*(AsnTag*)p1);
403 break;
404 case TDEPUSHTAG:
405 DBGIndent(SOTSPC);
406 fprintf(stdout,"[%08X/%d(%d)]",*(AsnTag*)p1,*(int*)p2,*(int*)p3);
407 break;
408 case TDEUNEXPECTED:
409 DBGIndent(EOLIF);
410 DBGIndent(SOT);
411 fprintf(stdout,"WARNING: Unexpected type encountered");
412 if (p2) {
413 fprintf(stdout," when expecting ");
414 DBGPrintType((TBLType*)p2);
415 }
416 fprintf(stdout," while decoding ");
417 DBGPrintType((TBLType*)p1);
418 fprintf(stdout,", decoding it in simple mode.\n");
419 DBGIndent(EOL);
420 break;
421 case TDENONOPTIONAL:
422 DBGIndent(EOLIF);
423 DBGIndent(SOT);
424 fprintf(stdout,"WARNING: Non-optional element ");
425 DBGPrintType((TBLType*)p2);
426 fprintf(stdout," missing in ");
427 DBGPrintType((TBLType*)p1);
428 fprintf(stdout,".\n");
429 DBGIndent(EOL);
430 break;
431 case TDEMANDATORY:
432 DBGIndent(EOLIF);
433 DBGIndent(SOT);
434 fprintf(stdout,"WARNING: Mandatory elements missing in ");
435 DBGPrintType((TBLType*)p1);
436 fprintf(stdout,".\n");
437 DBGIndent(EOL);
438 break;
439 case TDECONSTRAINT:
440 DBGIndent(EOLIF);
441 DBGIndent(SOT);
442 fprintf(stdout,"WARNING: Value %d not in SIZE(%d..%d) in ",
443 *(AsnInt*)p3,((TBLRange*)p2)->from,((TBLRange*)p2)->to);
444 DBGPrintType((TBLType*)p1);
445 fprintf(stdout,".\n");
446 DBGIndent(EOL);
447 break;
448 case TDENOMATCH:
449 DBGIndent(EOLIF);
450 DBGIndent(SOT);
451 fprintf(stdout,"WARNING: Tag [%08X] does not match tag [%08X] of type ",
452 *(AsnTag*)p3,*(AsnTag*)p2);
453 DBGPrintType((TBLType*)p1);
454 fprintf(stdout,".\n");
455 DBGIndent(EOL);
456 break;
457 case TDEERROR:
458 DBGIndent(EOLIF);
459 DBGIndent(SOT);
460 fprintf(stdout,"ERROR: %s.\n",(char*)p1);
461 DBGIndent(EOL);
462 break;
463 }
464 return 0;
465 }
466 #endif