2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 RCSID("$Id: gen_length.c,v 1.11.6.1 2004/01/26 09:26:10 lha Exp $");
39 length_primitive (const char *typename
,
43 fprintf (codefile
, "%s += length_%s(%s);\n", variable
, typename
, name
);
47 length_type (const char *name
, const Type
*t
, const char *variable
)
52 length_type (name
, t
->symbol
->type
);
54 fprintf (codefile
, "%s += length_%s(%s);\n",
55 variable
, t
->symbol
->gen_name
, name
);
58 if(t
->members
== NULL
)
59 length_primitive ("integer", name
, variable
);
62 asprintf(&s
, "(const int*)%s", name
);
64 errx (1, "out of memory");
65 length_primitive ("integer", s
, variable
);
70 length_primitive ("unsigned", name
, variable
);
73 length_primitive ("enumerated", name
, variable
);
76 length_primitive ("octet_string", name
, variable
);
79 length_primitive ("oid", name
, variable
);
83 * XXX - Hope this is correct
84 * look at TBitString case in `encode_type'
86 fprintf (codefile
, "%s += 7;\n", variable
);
93 if (t
->members
== NULL
)
96 for (m
= t
->members
; m
&& tag
!= m
->val
; m
= m
->next
) {
99 asprintf (&s
, "%s(%s)->%s",
100 m
->optional
? "" : "&", name
, m
->gen_name
);
102 fprintf (codefile
, "if(%s)", s
);
103 fprintf (codefile
, "{\n"
105 "%s = 0;\n", variable
, variable
);
106 length_type (s
, m
->type
, "ret");
107 fprintf (codefile
, "%s += 1 + length_len(%s) + oldret;\n",
109 fprintf (codefile
, "}\n");
115 "%s += 1 + length_len(%s);\n", variable
, variable
);
128 fprintf (codefile
, "for(i = (%s)->len - 1; i >= 0; --i){\n", name
);
129 fprintf (codefile
, "int oldret = %s;\n"
130 "%s = 0;\n", variable
, variable
);
131 asprintf (&n
, "&(%s)->val[i]", name
);
132 length_type(n
, t
->subtype
, variable
);
133 fprintf (codefile
, "%s += oldret;\n",
135 fprintf (codefile
, "}\n");
138 "%s += 1 + length_len(%s) + oldret;\n"
139 "}\n", variable
, variable
);
143 case TGeneralizedTime
:
144 length_primitive ("generalized_time", name
, variable
);
147 length_primitive ("general_string", name
, variable
);
150 length_type (name
, t
->subtype
, variable
);
151 fprintf (codefile
, "ret += 1 + length_len (ret);\n");
159 generate_type_length (const Symbol
*s
)
162 "size_t length_%s(const %s *);\n",
163 s
->gen_name
, s
->gen_name
);
167 "length_%s(const %s *data)\n"
170 s
->gen_name
, s
->gen_name
);
172 length_type ("data", s
->type
, "ret");
173 fprintf (codefile
, "return ret;\n}\n\n");