File: util.c

package info (click to toggle)
esnacc 1.8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,452 kB
  • sloc: ansic: 42,340; cpp: 13,880; yacc: 2,682; tcl: 1,587; lex: 688; sh: 573; makefile: 111
file content (223 lines) | stat: -rw-r--r-- 6,504 bytes parent folder | download | duplicates (4)
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
/*
 * compiler/back-ends/c-gen/util.c  - utilities for generating C encoders and decoders
 *
 *  MS 91/11/04
 * 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: /baseline/SNACC/compiler/back-ends/c-gen/util.c,v 1.5 2003/07/29 21:06:06 colestor Exp $
 * $Log: util.c,v $
 * Revision 1.5  2003/07/29 21:06:06  colestor
 * Added "env" reference  back to "C" generated code.
 *
 * Revision 1.4  2003/04/29 20:59:55  leonberp
 * integerated Deepak's changes for IOB support
 *
 * Revision 1.3  2002/09/16 17:35:09  mcphersc
 * Fixed warnings
 *
 * Revision 1.2  2000/10/24 14:54:48  rwc
 * Updated to remove high-level warnings (level 4 on MSVC++) for an easier build.
 * SOME warnings persist due to difficulty in modifying the SNACC compiler to
 * properly build clean source; also some files are built by Lex/Yacc.
 *
 * Revision 1.1.1.1  2000/08/21 20:36:05  leonberp
 * First CVS Version of SNACC.
 *
 * Revision 1.4  1997/08/28 07:26:10  povey
 * Changes to support DER encoding/decoding
 *
 * Revision 1.3.1.1  1997/08/20 23:14:41  povey
 *
 * Revision 1.3  1995/07/25 18:48:38  rj
 * changed `_' to `-' in file names.
 *
 * Revision 1.2  1994/09/01  00:26:52  rj
 * snacc_config.h removed.
 *
 * Revision 1.1  1994/08/28  09:48:44  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 "rules.h"
#include "snacc-util.h"
#include "enc-rules.h"
#include "util.h"
#include <string.h>


void
MakeVarPtrRef PARAMS ((r, td, parent, fieldType, parentVarName, newVarName),
    CRules *r _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    Type *fieldType _AND_
    char *parentVarName _AND_
    char *newVarName)
{
    CTRI *ctri;

    ctri = fieldType->cTypeRefInfo;

    /* always put in brackets to save future referencing hassles */
    strcpy (newVarName, "(");

    /* make ref'd field into a ptr by taking it's addr if nec */
    if (!ctri->isPtr)
        strcat (newVarName, "&");

    /* start with ref to parent */
    strcat (newVarName, parentVarName);

    /* ref this field */
    if ((td->type == parent) || (parent->cTypeRefInfo->isPtr))
        strcat (newVarName, "->");
    else
        strcat (newVarName, ".");

    /* ref choice union field if nec */
    if (parent->basicType->choiceId == BASICTYPE_CHOICE)
    {
        strcat (newVarName, r->choiceUnionFieldName);
        strcat (newVarName, ".");
    }

    strcat (newVarName, ctri->cFieldName);
    strcat (newVarName, ")");

}  /* MakeVarPtrRef */




void
MakeVarValueRef PARAMS ((r, td, parent, fieldType, parentVarName, newVarName),
    CRules *r _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    Type *fieldType _AND_
    char *parentVarName _AND_
    char *newVarName)
{
    CTRI *ctri;

    ctri = fieldType->cTypeRefInfo;

    /* always put in brackets to save future referencing hassles */
    strcpy (newVarName, "(");

    /* make ref'd field into a value by de-referencing if nec */
    if (ctri->isPtr)
        strcat (newVarName, "*");

    /* start with ref to parent */
    strcat (newVarName, parentVarName);

    /* ref this field */
    if ((td->type == parent) || (parent->cTypeRefInfo->isPtr))
        strcat (newVarName, "->");
    else
        strcat (newVarName, ".");

    /* ref choice union field if nec */
    if (parent->basicType->choiceId == BASICTYPE_CHOICE)
    {
        strcat (newVarName, r->choiceUnionFieldName);
        strcat (newVarName, ".");
    }

    strcat (newVarName, ctri->cFieldName);
    strcat (newVarName, ")");

}  /* MakeVarValueRef */

void
MakeChoiceIdValueRef PARAMS ((r, td, parent, fieldType, parentVarName, newVarName),
    CRules *r _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    Type *fieldType _AND_
    char *parentVarName _AND_
    char *newVarName)
{
    /* always put in brackets to save future referencing hassles */
    strcpy (newVarName, "(");

    /* start with ref to parent */
    strcat (newVarName, parentVarName);

    /* ref this field */
    if ((td->type == parent) || (parent->cTypeRefInfo->isPtr))
        strcat (newVarName, "->");
    else
        strcat (newVarName, ".");

    strcat (newVarName, parent->cTypeRefInfo->choiceIdEnumFieldName);
    strcat (newVarName, ")");

    r = r;  /*AVOIDS warning.*/
}  /* MakeChoiceIdValueRef */


void
PrintElmtAllocCode PARAMS ((src, type, varRefPtrName),
    FILE *src _AND_
    Type *type _AND_
    char *varRefPtrName)
{
    CTRI *ctri1;
    CTRI *ctri2;
    Type *t;

    t = GetType (type);
    ctri1 =  type->cTypeRefInfo;
    ctri2 =  t->cTypeRefInfo;
    if (ctri1->isPtr)
    {
        if (ctri2->cTypeId == C_LIST)
           fprintf (src, "\t%s = AsnListNew(sizeof(char*));\n", varRefPtrName);
        else
           fprintf (src, "\t%s = (%s*)Asn1Alloc(sizeof(%s));\n", varRefPtrName, ctri1->cTypeName, ctri1->cTypeName);
         //fprintf (src,"    CheckAsn1Alloc (%s, env);\n", varRefPtrName);
		//fprintf (src,"    CheckAsn1Alloc (%s);\n", varRefPtrName);	// Deepak: 18/Apr/2003 Not Required
    }

} /* PrintElmtAllocCode */


/*
 * prints code to decode EOCs for the lengths that go with extra tagging
 * maxLenLevel - the highest used length variable (ie 2 for elmtLen2)
 * minLenLevel - the lowest valid length variable (ie 0 for elmtLen0)
 * lenBaseVarName - len var name sans number (ie elmtLen for elmtLen2)
 * totalLevel - current level for the running total
 * totalBaseName - total var name sans number
 *                        (ie totalElmtLen for totalElmtLen1)
 */
void
PrintEocDecoders PARAMS ((f, maxLenLevel, minLenLevel, lenBaseVarName, totalLevel, totalBaseVarName),
    FILE *f _AND_
    int maxLenLevel _AND_
    int minLenLevel _AND_
    char *lenBaseVarName _AND_
    int totalLevel _AND_
    char *totalBaseVarName)
{
    int i;
    for (i = maxLenLevel; i > minLenLevel; i--)
    {
        fprintf (f,"\tif (%s%d == INDEFINITE_LEN)\n", lenBaseVarName, i);
        fprintf (f,"        %sDecEoc (b, &%s%d, env);\n", GetEncRulePrefix(), totalBaseVarName, totalLevel);
		//RWC;fprintf (f,"\t\t%sDecEoc(b,&%s%d);\n", GetEncRulePrefix(), totalBaseVarName, totalLevel);
    }
} /* PrintEocDeocoders */