Annotation of src/usr.bin/asn1_compile/gen_length.c, Revision 1.2
1.1 hin 1: /*
2: * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3: * (Royal Institute of Technology, Stockholm, Sweden).
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: *
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * 3. Neither the name of the Institute nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: */
33:
34: #include "gen_locl.h"
35:
36: /*
1.2 ! biorn 37: RCSID("$KTH: gen_length.c,v 1.14 2004/01/19 17:54:33 lha Exp $");
1.1 hin 38: */
39:
40: static void
41: length_primitive (const char *typename,
42: const char *name,
43: const char *variable)
44: {
45: fprintf (codefile, "%s += length_%s(%s);\n", variable, typename, name);
46: }
47:
48: static void
49: length_type (const char *name, const Type *t, const char *variable)
50: {
51: switch (t->type) {
52: case TType:
53: #if 0
54: length_type (name, t->symbol->type);
55: #endif
56: fprintf (codefile, "%s += length_%s(%s);\n",
57: variable, t->symbol->gen_name, name);
58: break;
59: case TInteger:
60: if(t->members == NULL)
61: length_primitive ("integer", name, variable);
62: else {
63: char *s;
64: asprintf(&s, "(const int*)%s", name);
65: if(s == NULL)
66: errx (1, "out of memory");
67: length_primitive ("integer", s, variable);
68: free(s);
69: }
70: break;
71: case TUInteger:
72: length_primitive ("unsigned", name, variable);
73: break;
74: case TEnumerated :
75: length_primitive ("enumerated", name, variable);
76: break;
77: case TOctetString:
78: length_primitive ("octet_string", name, variable);
79: break;
80: case TOID :
81: length_primitive ("oid", name, variable);
82: break;
83: case TBitString: {
84: /*
85: * XXX - Hope this is correct
86: * look at TBitString case in `encode_type'
87: */
88: fprintf (codefile, "%s += 7;\n", variable);
89: break;
90: }
91: case TSequence: {
92: Member *m;
93: int tag = -1;
94:
95: if (t->members == NULL)
96: break;
97:
98: for (m = t->members; m && tag != m->val; m = m->next) {
99: char *s;
100:
101: asprintf (&s, "%s(%s)->%s",
102: m->optional ? "" : "&", name, m->gen_name);
103: if (m->optional)
104: fprintf (codefile, "if(%s)", s);
105: fprintf (codefile, "{\n"
106: "int oldret = %s;\n"
107: "%s = 0;\n", variable, variable);
108: length_type (s, m->type, "ret");
109: fprintf (codefile, "%s += 1 + length_len(%s) + oldret;\n",
110: variable, variable);
111: fprintf (codefile, "}\n");
112: if (tag == -1)
113: tag = m->val;
114: free (s);
115: }
116: fprintf (codefile,
117: "%s += 1 + length_len(%s);\n", variable, variable);
118: break;
119: }
120: case TSequenceOf: {
121: char *n;
122:
123: fprintf (codefile,
124: "{\n"
125: "int oldret = %s;\n"
126: "int i;\n"
127: "%s = 0;\n",
128: variable, variable);
129:
130: fprintf (codefile, "for(i = (%s)->len - 1; i >= 0; --i){\n", name);
1.2 ! biorn 131: fprintf (codefile, "int oldret = %s;\n"
! 132: "%s = 0;\n", variable, variable);
1.1 hin 133: asprintf (&n, "&(%s)->val[i]", name);
134: length_type(n, t->subtype, variable);
1.2 ! biorn 135: fprintf (codefile, "%s += oldret;\n",
! 136: variable);
1.1 hin 137: fprintf (codefile, "}\n");
138:
139: fprintf (codefile,
140: "%s += 1 + length_len(%s) + oldret;\n"
141: "}\n", variable, variable);
142: free(n);
143: break;
144: }
145: case TGeneralizedTime:
146: length_primitive ("generalized_time", name, variable);
147: break;
148: case TGeneralString:
149: length_primitive ("general_string", name, variable);
150: break;
1.2 ! biorn 151: case TUTF8String:
! 152: length_primitive ("utf8string", name, variable);
! 153: break;
! 154: case TNull:
! 155: fprintf (codefile, "%s += length_nulltype();\n", variable);
! 156: break;
1.1 hin 157: case TApplication:
158: length_type (name, t->subtype, variable);
159: fprintf (codefile, "ret += 1 + length_len (ret);\n");
1.2 ! biorn 160: break;
! 161: case TBoolean:
! 162: length_primitive ("boolean", name, variable);
1.1 hin 163: break;
164: default :
165: abort ();
166: }
167: }
168:
169: void
170: generate_type_length (const Symbol *s)
171: {
172: fprintf (headerfile,
173: "size_t length_%s(const %s *);\n",
174: s->gen_name, s->gen_name);
175:
176: fprintf (codefile,
177: "size_t\n"
178: "length_%s(const %s *data)\n"
179: "{\n"
180: "size_t ret = 0;\n",
181: s->gen_name, s->gen_name);
182:
183: length_type ("data", s->type, "ret");
184: fprintf (codefile, "return ret;\n}\n\n");
185: }
186: