bits.c revision 178825
155682Smarkm/* 2102644Snectar * Copyright (c) 1997-2002 Kungliga Tekniska H�gskolan 355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden). 455682Smarkm * All rights reserved. 555682Smarkm * 655682Smarkm * Redistribution and use in source and binary forms, with or without 755682Smarkm * modification, are permitted provided that the following conditions 855682Smarkm * are met: 955682Smarkm * 1055682Smarkm * 1. Redistributions of source code must retain the above copyright 1155682Smarkm * notice, this list of conditions and the following disclaimer. 1255682Smarkm * 1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1455682Smarkm * notice, this list of conditions and the following disclaimer in the 1555682Smarkm * documentation and/or other materials provided with the distribution. 1655682Smarkm * 1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors 1855682Smarkm * may be used to endorse or promote products derived from this software 1955682Smarkm * without specific prior written permission. 2055682Smarkm * 2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2455682Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3155682Smarkm * SUCH DAMAGE. 3255682Smarkm */ 3355682Smarkm 3455682Smarkm#ifdef HAVE_CONFIG_H 3555682Smarkm#include <config.h> 36178825SdfrRCSID("$Id: bits.c 18703 2006-10-20 20:33:58Z lha $"); 3755682Smarkm#endif 3855682Smarkm#include <stdio.h> 3955682Smarkm#include <string.h> 4055682Smarkm#include <stdlib.h> 4155682Smarkm#include <ctype.h> 4255682Smarkm 4355682Smarkm#define BITSIZE(TYPE) \ 4455682Smarkm{ \ 45102644Snectar int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \ 4655682Smarkm char tmp[128], tmp2[128]; \ 4755682Smarkm while(x){ x <<= 1; b++; if(x < zero) pre=""; } \ 4855682Smarkm if(b >= len){ \ 4955682Smarkm int tabs; \ 5055682Smarkm sprintf(tmp, "%sint%d_t" , pre, len); \ 5155682Smarkm sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \ 5255682Smarkm tabs = 5 - strlen(tmp2) / 8; \ 5355682Smarkm fprintf(f, "%s", tmp2); \ 5455682Smarkm while(tabs-- > 0) fprintf(f, "\t"); \ 5555682Smarkm fprintf(f, "/* %2d bits */\n", b); \ 5655682Smarkm return; \ 5755682Smarkm } \ 5855682Smarkm} 5955682Smarkm 6072445Sassar#ifndef HAVE___ATTRIBUTE__ 6172445Sassar#define __attribute__(x) 6272445Sassar#endif 6372445Sassar 6455682Smarkmstatic void 6572445Sassartry_signed(FILE *f, int len) __attribute__ ((unused)); 6672445Sassar 6772445Sassarstatic void 6872445Sassartry_unsigned(FILE *f, int len) __attribute__ ((unused)); 6972445Sassar 7072445Sassarstatic int 7172445Sassarprint_bt(FILE *f, int flag) __attribute__ ((unused)); 7272445Sassar 7372445Sassarstatic void 7455682Smarkmtry_signed(FILE *f, int len) 7555682Smarkm{ 7655682Smarkm BITSIZE(signed char); 7755682Smarkm BITSIZE(short); 7855682Smarkm BITSIZE(int); 7955682Smarkm BITSIZE(long); 8055682Smarkm#ifdef HAVE_LONG_LONG 8155682Smarkm BITSIZE(long long); 8255682Smarkm#endif 8355682Smarkm fprintf(f, "/* There is no %d bit type */\n", len); 8455682Smarkm} 8555682Smarkm 8655682Smarkmstatic void 8755682Smarkmtry_unsigned(FILE *f, int len) 8855682Smarkm{ 8955682Smarkm BITSIZE(unsigned char); 9055682Smarkm BITSIZE(unsigned short); 9155682Smarkm BITSIZE(unsigned int); 9255682Smarkm BITSIZE(unsigned long); 9355682Smarkm#ifdef HAVE_LONG_LONG 9455682Smarkm BITSIZE(unsigned long long); 9555682Smarkm#endif 9655682Smarkm fprintf(f, "/* There is no %d bit type */\n", len); 9755682Smarkm} 9855682Smarkm 9955682Smarkmstatic int 10055682Smarkmprint_bt(FILE *f, int flag) 10155682Smarkm{ 10255682Smarkm if(flag == 0){ 10355682Smarkm fprintf(f, "/* For compatibility with various type definitions */\n"); 10455682Smarkm fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n"); 10555682Smarkm fprintf(f, "#define __BIT_TYPES_DEFINED__\n"); 10655682Smarkm fprintf(f, "\n"); 10755682Smarkm } 10855682Smarkm return 1; 10955682Smarkm} 11055682Smarkm 11155682Smarkmint main(int argc, char **argv) 11255682Smarkm{ 11355682Smarkm FILE *f; 11455682Smarkm int flag; 115178825Sdfr const char *fn, *hb; 11655682Smarkm 117178825Sdfr if (argc > 1 && strcmp(argv[1], "--version") == 0) { 118178825Sdfr printf("some version"); 119178825Sdfr return 0; 120178825Sdfr } 121178825Sdfr 12255682Smarkm if(argc < 2){ 12355682Smarkm fn = "bits.h"; 12455682Smarkm hb = "__BITS_H__"; 12555682Smarkm f = stdout; 12655682Smarkm } else { 12755682Smarkm char *p; 12855682Smarkm fn = argv[1]; 129178825Sdfr p = malloc(strlen(fn) + 5); 130178825Sdfr sprintf(p, "__%s__", fn); 131178825Sdfr hb = p; 132178825Sdfr for(; *p; p++){ 13355682Smarkm if(!isalnum((unsigned char)*p)) 13455682Smarkm *p = '_'; 13555682Smarkm } 13655682Smarkm f = fopen(argv[1], "w"); 13755682Smarkm } 13855682Smarkm fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST); 13955682Smarkm fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "", 140178825Sdfr "$Id: bits.c 18703 2006-10-20 20:33:58Z lha $"); 14155682Smarkm fprintf(f, "#ifndef %s\n", hb); 14255682Smarkm fprintf(f, "#define %s\n", hb); 14355682Smarkm fprintf(f, "\n"); 144102644Snectar#ifdef HAVE_INTTYPES_H 145102644Snectar fprintf(f, "#include <inttypes.h>\n"); 146102644Snectar#endif 14755682Smarkm#ifdef HAVE_SYS_TYPES_H 14855682Smarkm fprintf(f, "#include <sys/types.h>\n"); 14955682Smarkm#endif 15055682Smarkm#ifdef HAVE_SYS_BITYPES_H 15155682Smarkm fprintf(f, "#include <sys/bitypes.h>\n"); 15255682Smarkm#endif 15355682Smarkm#ifdef HAVE_BIND_BITYPES_H 15455682Smarkm fprintf(f, "#include <bind/bitypes.h>\n"); 15555682Smarkm#endif 15655682Smarkm#ifdef HAVE_NETINET_IN6_MACHTYPES_H 15755682Smarkm fprintf(f, "#include <netinet/in6_machtypes.h>\n"); 15855682Smarkm#endif 159102644Snectar#ifdef HAVE_SOCKLEN_T 160102644Snectar fprintf(f, "#include <sys/socket.h>\n"); 161102644Snectar#endif 16255682Smarkm fprintf(f, "\n"); 16355682Smarkm 16455682Smarkm flag = 0; 16555682Smarkm#ifndef HAVE_INT8_T 16655682Smarkm flag = print_bt(f, flag); 16755682Smarkm try_signed (f, 8); 16855682Smarkm#endif /* HAVE_INT8_T */ 16955682Smarkm#ifndef HAVE_INT16_T 17055682Smarkm flag = print_bt(f, flag); 17155682Smarkm try_signed (f, 16); 17255682Smarkm#endif /* HAVE_INT16_T */ 17355682Smarkm#ifndef HAVE_INT32_T 17455682Smarkm flag = print_bt(f, flag); 17555682Smarkm try_signed (f, 32); 17655682Smarkm#endif /* HAVE_INT32_T */ 17755682Smarkm#ifndef HAVE_INT64_T 17855682Smarkm flag = print_bt(f, flag); 17955682Smarkm try_signed (f, 64); 18055682Smarkm#endif /* HAVE_INT64_T */ 18155682Smarkm 18272445Sassar#ifndef HAVE_UINT8_T 18372445Sassar flag = print_bt(f, flag); 18472445Sassar try_unsigned (f, 8); 18572445Sassar#endif /* HAVE_UINT8_T */ 18672445Sassar#ifndef HAVE_UINT16_T 18772445Sassar flag = print_bt(f, flag); 18872445Sassar try_unsigned (f, 16); 18972445Sassar#endif /* HAVE_UINT16_T */ 19072445Sassar#ifndef HAVE_UINT32_T 19172445Sassar flag = print_bt(f, flag); 19272445Sassar try_unsigned (f, 32); 19372445Sassar#endif /* HAVE_UINT32_T */ 19472445Sassar#ifndef HAVE_UINT64_T 19572445Sassar flag = print_bt(f, flag); 19672445Sassar try_unsigned (f, 64); 19772445Sassar#endif /* HAVE_UINT64_T */ 19872445Sassar 19972445Sassar#define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n") 20055682Smarkm#ifndef HAVE_U_INT8_T 20155682Smarkm flag = print_bt(f, flag); 20272445Sassar X(8); 20372445Sassar#endif /* HAVE_U_INT8_T */ 20455682Smarkm#ifndef HAVE_U_INT16_T 20555682Smarkm flag = print_bt(f, flag); 20672445Sassar X(16); 20755682Smarkm#endif /* HAVE_U_INT16_T */ 20855682Smarkm#ifndef HAVE_U_INT32_T 20955682Smarkm flag = print_bt(f, flag); 21072445Sassar X(32); 21155682Smarkm#endif /* HAVE_U_INT32_T */ 21255682Smarkm#ifndef HAVE_U_INT64_T 21355682Smarkm flag = print_bt(f, flag); 21472445Sassar X(64); 21555682Smarkm#endif /* HAVE_U_INT64_T */ 21655682Smarkm 21755682Smarkm if(flag){ 21855682Smarkm fprintf(f, "\n"); 21955682Smarkm fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n"); 22055682Smarkm } 221102644Snectar#ifdef KRB5 222102644Snectar fprintf(f, "\n"); 223102644Snectar#if defined(HAVE_SOCKLEN_T) 224102644Snectar fprintf(f, "typedef socklen_t krb5_socklen_t;\n"); 225102644Snectar#else 226102644Snectar fprintf(f, "typedef int krb5_socklen_t;\n"); 227102644Snectar#endif 228102644Snectar#if defined(HAVE_SSIZE_T) 229102644Snectar#ifdef HAVE_UNISTD_H 230102644Snectar fprintf(f, "#include <unistd.h>\n"); 231102644Snectar#endif 232102644Snectar fprintf(f, "typedef ssize_t krb5_ssize_t;\n"); 233102644Snectar#else 234102644Snectar fprintf(f, "typedef int krb5_ssize_t;\n"); 235102644Snectar#endif 236102644Snectar fprintf(f, "\n"); 237102644Snectar#endif /* KRB5 */ 23855682Smarkm fprintf(f, "#endif /* %s */\n", hb); 23955682Smarkm return 0; 24055682Smarkm} 241