File: tag-util.c

package info (click to toggle)
snacc 1.3bbn-9.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,484 kB
  • ctags: 4,134
  • sloc: ansic: 35,869; sh: 7,050; cpp: 4,380; yacc: 2,215; lex: 517; makefile: 253; sed: 4
file content (496 lines) | stat: -rw-r--r-- 11,406 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * compiler/back_ends/c_gen/tag_util.c  - utilities for dealing with tags
 *
 * MS 92
 * Copyright (C) 1991, 1992 Michael Sample
 *            and the University of British Columbia
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * $Header: /usr/app/odstb/CVS/snacc/compiler/back-ends/tag-util.c,v 1.3 1995/07/25 18:15:28 rj Exp $
 * $Log: tag-util.c,v $
 * Revision 1.3  1995/07/25 18:15:28  rj
 * changed `_' to `-' in file names.
 *
 * Revision 1.2  1994/09/01  00:26:07  rj
 * snacc_config.h and other superfluous .h files removed.
 *
 * Revision 1.1  1994/08/28  09:48:39  rj
 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
 *
 */

#include <stdio.h>

#include "asn-incl.h"
#include "asn1module.h"
#include "mem.h"
#include "define.h"
#include "lib-types.h"
#include "c-gen/rules.h"
#include "c-gen/type-info.h"
#include "str-util.h"
#include "snacc-util.h"
#include "c-gen/util.h"
#include "tag-util.h"



/*
 * returns the tags for the given type (stops at next type definition).
 * if no tags have been grabbed yet and an untagged CHOICE is encountered,
 * all of the CHOICE's top level tags are returned and the stoleChoiceTags
 * flag is set.  If the type has no tags an empty list is returned, not
 * NULL.
 *
 * ASSUMES: tag list's and implicit flags have been adjusted according
 *          to module level IMPLICIT/EXPLICIT-TAGS and type level
 *          IMPLICIT/EXPLICIT tagging.
 *
 * EXAMPLE:
 *
 *   typeX ::= SEQUENCE                         SomeChoice ::= CHOICE
 *   {                                          {
 *         foo [0] INTEGER,                          [0] INTEGER,
 *         bar SomeChoice,                           [1] BOOLEAN,
 *         bell [1] IMPLICIT BOOLEAN,                [2] IA5String
 *         gumby [2] SomeChoice,                }
           poki SomeOtherChoice
 *   }
 *
 *  SomeOtherChoice ::= [APPLICATION 99] CHOICE { ....}
 *
 *  GetTags (foo's type) -->  CNTX 0, UNIV INTEGER_TAG_CODE   stoleChoiceTags = FALSE
 *  GetTags (bar)        -->  CNTX 0, CNTX 1, CNTX 2   (SomeChoice Elmt's first Tags)
 *                                                      stoleChoiceTags = TRUE
 *  GetTags (bell)       -->  CNTX 1    stoleChoiceTags = FALSE
 *  GetTags (gumby)      -->  CNTX 2    stoleChoiceTags = FALSE
 *  GetTags (poki)       -->  APPLICATION 99    stoleChoiceTags = FALSE
 *
 * MS 92/03/04 Added tag form information
 */
TagList*
GetTags PARAMS ((t, stoleChoiceTags),
    Type *t _AND_
    int *stoleChoiceTags)
{
    Tag *tag;
    TagList *tl;
    TagList *retVal;
    Tag *last;
    Tag  *tagCopy;
    Tag **tagHndl;
    int implicitRef;
    int stoleChoicesAgain;
    NamedType *e;

    tl = t->tags;
    if (tl != NULL)
        AsnListFirst (tl);

    retVal = (TagList*) AsnListNew (sizeof (void*));
    implicitRef = FALSE;
    *stoleChoiceTags = FALSE;

    for (;;)
    {
         /*
          * go through tag list local to this type if any
          */

        FOR_REST_LIST_ELMT (tag, tl)
        {
            tagCopy = (Tag*)Malloc (sizeof (Tag));
            memcpy (tagCopy, tag, sizeof (Tag));
            tagHndl = (Tag**)AsnListAppend (retVal);
            *tagHndl = tagCopy;

        }

        /*
         * follow tags of referenced types
         */

        if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
             (t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
        {
            if (!implicitRef)
                implicitRef = t->implicit;


            if (t->basicType->a.localTypeRef->link == NULL)
            {
                fprintf (stderr,"ERROR - unresolved type ref, cannot get tags for decoding>\n");
                break;
            }
            t = t->basicType->a.localTypeRef->link->type;
            tl = t->tags;

            if (tl != NULL)
            {
                AsnListFirst (tl); /* set curr ptr to first node */
                if ((!LIST_EMPTY (tl)) && implicitRef)
                {
                    AsnListNext (tl);
                    implicitRef = FALSE;
                }
            }

        }

        /*
         * if untagged choice and no tags found yet
         */
        else if ((t->basicType->choiceId == BASICTYPE_CHOICE) && (LIST_EMPTY (retVal)))
        {
            /*
             * Return list of top level tags from this choice
             * and set "stoleChoiceTags" bool param
             */
            if (implicitRef)
                fprintf (stderr,"ERROR - IMPLICITLY Tagged CHOICE\n");

            *stoleChoiceTags = TRUE;

            FOR_EACH_LIST_ELMT (e, t->basicType->a.choice)
            {
                stoleChoicesAgain = FALSE;
                tl = GetTags (e->type, &stoleChoicesAgain);

                if (tl == NULL)
                    break;

		AsnListFirst (tl);
                if (stoleChoicesAgain)
                {
                    FOR_EACH_LIST_ELMT (tag, tl)
                    {
                        tagCopy = (Tag*)Malloc (sizeof (Tag));
                        memcpy (tagCopy, tag, sizeof (Tag));
			tagHndl = (Tag**)AsnListAppend (retVal);
			*tagHndl = tagCopy;

                    }
                }
                else
                {
		  tag = (Tag*)FIRST_LIST_ELMT (tl);
		  tagCopy = (Tag*)Malloc (sizeof (Tag));
		  memcpy (tagCopy, tag, sizeof (Tag));
		  tagHndl = (Tag**)AsnListAppend (retVal);
		  *tagHndl = tagCopy;
                }
                FreeTags (tl);
            }

            break;  /* exit for loop */
        }

        else
            break; /* exit for loop */
    }


    if (!*stoleChoiceTags && (retVal != NULL) && !LIST_EMPTY (retVal))
    {
        last = (Tag*)LAST_LIST_ELMT (retVal);
        FOR_EACH_LIST_ELMT (tag, retVal)
        {
            tag->form = CONS;
        }
        last->form = LIBTYPE_GET_TAG_FORM (GetBuiltinType (t));
    }

    AsnListFirst (retVal);
    return retVal;

}  /* GetTags */


void
FreeTags PARAMS ((tl),
    TagList *tl)
{
    Tag *tag;
    AsnListNode *listNode;
    AsnListNode *ln;

    /* free tags */
    FOR_EACH_LIST_ELMT (tag, tl)
    {
        Free (tag);
    }

    /* free list nodes */
    for (ln = FIRST_LIST_NODE (tl); ln != NULL; )
    {
        listNode = ln;
	ln = ln->next;
        Free (listNode);
    }

    /* free list head */
    Free (tl);

}  /* FreeTags */

/*
 * Returns the number of tags that GetTags would return for
 * the same type.
 */
int
CountTags PARAMS ((t),
    Type *t)
{
    int tagCount;
    Tag *tag;
    TagList *tl;
    int implicitRef;
    int stoleChoicesAgain;
    NamedType *e;

    tl = t->tags;
    if (tl != NULL)
        AsnListFirst (tl);

    tagCount = 0;
    implicitRef = FALSE;

    for (;;)
    {
         /*
          * go through tag list local to this type if any
          */

        FOR_REST_LIST_ELMT (tag, tl)
        {
	    tagCount++;
        }

        /*
         * follow tags of referenced types
         */

        if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
             (t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
        {
            if (!implicitRef)
                implicitRef = t->implicit;


            if (t->basicType->a.localTypeRef->link == NULL)
            {
                fprintf (stderr,"ERROR - unresolved type ref, cannot get tags for decoding>\n");
                break;
            }
            t = t->basicType->a.localTypeRef->link->type;
            tl = t->tags;

            if (tl != NULL)
            {
                AsnListFirst (tl); /* set curr ptr to first node */
                if ((!LIST_EMPTY (tl)) && implicitRef)
                {
                    AsnListNext (tl);
                    implicitRef = FALSE;
                }
            }

        }
        else
            break;
    }

    return tagCount;

}  /* CountTags */


unsigned long int
TagByteLen PARAMS ((tagCode),
    unsigned long int tagCode)
{
    unsigned long int tagLen;

    if (tagCode < 31)
        tagLen = 1;
    else if (tagCode < 128)
        tagLen = 2;
    else if (tagCode < 16384)
        tagLen = 3;
    else if (tagCode < 2097152)
        tagLen = 4;
    else
        tagLen = 5;

    return tagLen;
} /* TagByteLen */



char*
Class2ClassStr PARAMS ((class),
    int class)
{
    switch (class)
    {
        case UNIV:
            return "UNIV";
            break;

        case APPL:
            return "APPL";
            break;

        case CNTX:
            return "CNTX";
            break;

        case PRIV:
            return "PRIV";
            break;

        default:
            return "UNKNOWN";
            break;
    }
} /* Class2ClassStr */



char*
Form2FormStr PARAMS ((form),
    BER_FORM form)
{
    switch (form)
    {
        case PRIM:
            return "PRIM";
            break;

        case CONS:
            return "CONS";
            break;

        default:
            return "UNKNOWN";
            break;
    }
} /* Form2FormStr */



char*
Code2UnivCodeStr PARAMS ((code),
    BER_UNIV_CODE code)
{
    switch (code)
    {
      case BOOLEAN_TAG_CODE:
        return "BOOLEAN_TAG_CODE";
        break;

      case INTEGER_TAG_CODE:
        return "INTEGER_TAG_CODE";
        break;

      case BITSTRING_TAG_CODE:
        return "BITSTRING_TAG_CODE";
        break;

      case OCTETSTRING_TAG_CODE:
        return "OCTETSTRING_TAG_CODE";
        break;

      case NULLTYPE_TAG_CODE:
        return "NULLTYPE_TAG_CODE";
        break;

      case OID_TAG_CODE:
        return "OID_TAG_CODE";
        break;

      case OD_TAG_CODE:
        return "OD_TAG_CODE";
        break;

      case EXTERNAL_TAG_CODE:
        return "EXTERNAL_TAG_CODE";
        break;

      case REAL_TAG_CODE:
        return "REAL_TAG_CODE";
        break;

      case ENUM_TAG_CODE:
        return "ENUM_TAG_CODE";
        break;

      case UTF8STRING_TAG_CODE:
	return "UTF8STRING_TAG_CODE";
	break;

      case SEQ_TAG_CODE:
        return "SEQ_TAG_CODE";
        break;

      case SET_TAG_CODE:
        return "SET_TAG_CODE";
        break;

      case NUMERICSTRING_TAG_CODE:
        return "NUMERICSTRING_TAG_CODE";
        break;

      case PRINTABLESTRING_TAG_CODE:
        return "PRINTABLESTRING_TAG_CODE";
        break;

      case TELETEXSTRING_TAG_CODE:
        return "TELETEXSTRING_TAG_CODE";
        break;

      case VIDEOTEXSTRING_TAG_CODE:
        return "VIDEOTEXSTRING_TAG_CODE";
        break;

      case IA5STRING_TAG_CODE:
        return "IA5STRING_TAG_CODE";
        break;

      case UTCTIME_TAG_CODE:
        return "UTCTIME_TAG_CODE";
        break;

      case GENERALIZEDTIME_TAG_CODE:
        return "GENERALIZEDTIME_TAG_CODE";
        break;

      case GRAPHICSTRING_TAG_CODE:
        return "GRAPHICSTRING_TAG_CODE";
        break;

      case VISIBLESTRING_TAG_CODE:
        return "VISIBLESTRING_TAG_CODE";
        break;

      case GENERALSTRING_TAG_CODE:
        return "GENERALSTRING_TAG_CODE";
        break;

      case UNIVERSALSTRING_TAG_CODE:
	return "UNIVERSALSTRING_TAG_CODE";
	break;

      case BMPSTRING_TAG_CODE:
	return "BMPSTRING_TAG_CODE";
	break;

      default:
        return "UNKNOWN";

    }
} /* TagId2FormStr */