1/* creates the md5global.h file.
2 *  Derived from KTH kerberos library bits.c program
3 * Tim Martin
4 * $Id: makemd5.c,v 1.3 2004/07/07 22:48:35 snsimon Exp $
5 */
6/*
7 * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. The name "Carnegie Mellon University" must not be used to
22 *    endorse or promote products derived from this software without
23 *    prior written permission. For permission or any other legal
24 *    details, please contact
25 *      Office of Technology Transfer
26 *      Carnegie Mellon University
27 *      5000 Forbes Avenue
28 *      Pittsburgh, PA  15213-3890
29 *      (412) 268-4387, fax: (412) 268-7395
30 *      tech-transfer@andrew.cmu.edu
31 *
32 * 4. Redistributions of any form whatsoever must retain the following
33 *    acknowledgment:
34 *    "This product includes software developed by Computing Services
35 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 *
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 */
45
46/*
47 * Copyright (c) 1997, 1998 Kungliga Tekniska H�gskolan
48 * (Royal Institute of Technology, Stockholm, Sweden).
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 *
55 * 1. Redistributions of source code must retain the above copyright
56 *    notice, this list of conditions and the following disclaimer.
57 *
58 * 2. Redistributions in binary form must reproduce the above copyright
59 *    notice, this list of conditions and the following disclaimer in the
60 *    documentation and/or other materials provided with the distribution.
61 *
62 * 3. All advertising materials mentioning features or use of this software
63 *    must display the following acknowledgement:
64 *      This product includes software developed by Kungliga Tekniska
65 *      H�gskolan and its contributors.
66 *
67 * 4. Neither the name of the Institute nor the names of its contributors
68 *    may be used to endorse or promote products derived from this software
69 *    without specific prior written permission.
70 *
71 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81 * SUCH DAMAGE.
82 */
83
84
85
86#include <stdio.h>
87#include <string.h>
88#include <stdlib.h>
89#include <ctype.h>
90
91
92static void
93my_strupr(char *s)
94{
95    char *p = s;
96    while(*p){
97	if(islower((int) *p))
98	    *p = toupper((int) *p);
99	p++;
100    }
101}
102
103
104#define BITSIZE(TYPE)						\
105{								\
106    int b = 0; TYPE x = 1, zero = 0; char *pre = "U";		\
107    char tmp[128], tmp2[128];					\
108    while(x){ x <<= 1; b++; if(x < zero) pre=""; }		\
109    if(b >= len){						\
110        int tabs;						\
111	sprintf(tmp, "%sINT%d" , pre, len/8);			\
112	sprintf(tmp2, "typedef %s %s;", #TYPE, tmp);		\
113	my_strupr(tmp);						\
114	tabs = 5 - strlen(tmp2) / 8;				\
115        fprintf(f, "%s", tmp2);					\
116	while(tabs-- > 0) fprintf(f, "\t");			\
117	fprintf(f, "/* %2d bits */\n", b);			\
118        return;                                                 \
119    }								\
120}
121
122static void
123try_signed(FILE *f, int len)
124{
125    BITSIZE(signed char);
126    BITSIZE(short);
127    BITSIZE(int);
128    BITSIZE(long);
129#ifdef HAVE_LONG_LONG
130    BITSIZE(long long);
131#endif
132    fprintf(f, "/* There is no %d bit type */\n", len);
133}
134
135static void
136try_unsigned(FILE *f, int len)
137{
138    BITSIZE(unsigned char);
139    BITSIZE(unsigned short);
140    BITSIZE(unsigned int);
141    BITSIZE(unsigned long);
142#ifdef HAVE_LONG_LONG
143    BITSIZE(unsigned long long);
144#endif
145    fprintf(f, "/* There is no %d bit type */\n", len);
146}
147
148static int print_pre(FILE *f)
149{
150  fprintf(f,
151	  "/* GLOBAL.H - RSAREF types and constants\n"
152	  " */\n"
153          "#ifndef MD5GLOBAL_H\n"
154          "#define MD5GLOBAL_H\n"
155	  "\n"
156	  "/* PROTOTYPES should be set to one if and only if the compiler supports\n"
157	  "  function argument prototyping.\n"
158	  "The following makes PROTOTYPES default to 0 if it has not already\n"
159	  "  been defined with C compiler flags.\n"
160	  " */\n"
161	  "#ifndef PROTOTYPES\n"
162	  "#define PROTOTYPES 0\n"
163	  "#endif\n"
164	  "\n"
165	  "/* POINTER defines a generic pointer type */\n"
166	  "typedef unsigned char *POINTER;\n"
167	  "\n"
168	  );
169  return 1;
170}
171
172static int print_post(FILE *f)
173{
174  fprintf(f, "\n"
175	  "/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.\n"
176	  "If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it\n"
177	  "returns an empty list.\n"
178	  "*/\n"
179	  "#if PROTOTYPES\n"
180	  "#define PROTO_LIST(list) list\n"
181	  "#else\n"
182	  "#define PROTO_LIST(list) ()\n"
183	  "#endif\n"
184	  "\n"
185	  "#endif /* MD5GLOBAL_H */\n\n"
186	  );
187
188  return 1;
189}
190
191
192int main(int argc, char **argv)
193{
194  FILE *f;
195  char *fn, *hb;
196
197  if(argc < 2){
198    fn = "bits.h";
199    hb = "__BITS_H__";
200    f = stdout;
201  } else {
202    char *p;
203    fn = argv[1];
204    hb = malloc(strlen(fn) + 5);
205    sprintf(hb, "__%s__", fn);
206    for(p = hb; *p; p++){
207      if(!isalnum((int) *p))
208	*p = '_';
209    }
210    f = fopen(argv[1], "w");
211  }
212
213  print_pre(f);
214
215#ifndef HAVE_INT8_T
216    try_signed (f, 8);
217#endif /* HAVE_INT8_T */
218#ifndef HAVE_INT16_T
219    try_signed (f, 16);
220#endif /* HAVE_INT16_T */
221#ifndef HAVE_INT32_T
222    try_signed (f, 32);
223#endif /* HAVE_INT32_T */
224#ifndef HAVE_INT64_T
225    try_signed (f, 64);
226#endif /* HAVE_INT64_T */
227
228#ifndef HAVE_U_INT8_T
229    try_unsigned (f, 8);
230#endif /* HAVE_INT8_T */
231#ifndef HAVE_U_INT16_T
232    try_unsigned (f, 16);
233#endif /* HAVE_U_INT16_T */
234#ifndef HAVE_U_INT32_T
235    try_unsigned (f, 32);
236#endif /* HAVE_U_INT32_T */
237#ifndef HAVE_U_INT64_T
238    try_unsigned (f, 64);
239#endif /* HAVE_U_INT64_T */
240
241    print_post(f);
242
243    fclose(f);
244
245    return 0;
246}
247