From a6ca2e39dc58764b2eee673fa14d6afbb1fc5203 Mon Sep 17 00:00:00 2001 From: ilammy Date: Sun, 19 Apr 2015 15:37:47 +0300 Subject: [PATCH] chibi.crypto: move sexp_uintN_t typedefs to First we check for C99 support in Makefile.detect, looking for the header we need and verifying whether it is the right one by using a definition required by C99 standard to be present in that header. uintN_t types are optional, but implementations are required to provide corresponding limit #defines for the types they support, so we can check for this with preprocessor only. Finally, we define SEXP_UINTN_DEFINED for any sexp_uintN_t we have so that the code can use #ifs to check for exact integer support. --- Makefile.detect | 10 +++++++++- include/chibi/sexp.h | 28 ++++++++++++++++++++++++++++ lib/chibi/crypto/integers.h | 28 ---------------------------- lib/chibi/crypto/sha2.c | 4 +++- 4 files changed, 40 insertions(+), 30 deletions(-) delete mode 100644 lib/chibi/crypto/integers.h diff --git a/Makefile.detect b/Makefile.detect index 7b786442..83994c72 100644 --- a/Makefile.detect +++ b/Makefile.detect @@ -108,7 +108,7 @@ RLDFLAGS=-Wl,-R$(DESTDIR)$(LIBDIR) endif ######################################################################## -# Check for NTP (who needs autoconf?) +# Check for headers (who needs autoconf?) ifndef $(SEXP_USE_NTP_GETTIME) SEXP_USE_NTP_GETTIME := $(shell echo "main(){struct ntptimeval n; ntp_gettime(&n);}" | gcc -fsyntax-only -include sys/timex.h -xc - >/dev/null 2>/dev/null && echo 1 || echo 0) @@ -117,3 +117,11 @@ endif ifeq ($(SEXP_USE_NTP_GETTIME),1) CPPFLAGS += -DSEXP_USE_NTPGETTIME endif + +ifndef $(SEXP_USE_INTTYPES) +SEXP_USE_INTTYPES := $(shell echo "main(){int_least8_t x;}" | gcc -fsyntax-only -include inttypes.h -xc - >/dev/null 2>/dev/null && echo 1 || echo 0) +endif + +ifeq ($(SEXP_USE_INTTYPES),1) +CPPFLAGS += -DSEXP_USE_INTTYPES +endif diff --git a/include/chibi/sexp.h b/include/chibi/sexp.h index f21ac4f9..af618d0e 100755 --- a/include/chibi/sexp.h +++ b/include/chibi/sexp.h @@ -195,6 +195,34 @@ typedef int sexp_sint_t; #define sexp_heap_align(n) sexp_align(n, 4) #endif +#ifdef SEXP_USE_INTTYPES +# include +# ifdef UINT8_MAX +# define SEXP_UINT8_DEFINED 1 +typedef uint8_t sexp_uint8_t; +# endif +# ifdef UINT32_MAX +# define SEXP_UINT32_DEFINED 1 +typedef uint32_t sexp_uint32_t; +# endif +#else +# include +# if UCHAR_MAX == 255 +# define SEXP_UINT8_DEFINED 1 +typedef unsigned char sexp_uint8_t; +# endif +# if UINT_MAX == 4294967295U +# define SEXP_UINT32_DEFINED 1 +typedef unsigned int sexp_uint32_t; +# elif ULONG_MAX == 4294967295UL +# define SEXP_UINT32_DEFINED 1 +typedef unsigned long sexp_uint32_t; +# elif USHRT_MAX == 4294967295U +# define SEXP_UINT32_DEFINED 1 +typedef unsigned short sexp_uint32_t; +# endif +#endif + #if SEXP_USE_LONG_PROCEDURE_ARGS typedef int sexp_proc_num_args_t; #else diff --git a/lib/chibi/crypto/integers.h b/lib/chibi/crypto/integers.h deleted file mode 100644 index 191bd0f4..00000000 --- a/lib/chibi/crypto/integers.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef CHIBI_CRYPTO_INTEGERS_H -#define CHIBI_CRYPTO_INTEGERS_H - -#if __STDC_VERSION__ >= 199901L /* C99 */ -# include - typedef uint32_t sexp_uint32_t; - typedef uint8_t sexp_uint8_t; -#else -# include -# -# if UCHAR_MAX == 255 - typedef unsigned char sexp_uint8_t; -# else -# error Could not find 8-bit type -# endif -# -# if UINT_MAX == 4294967295U - typedef unsigned int sexp_uint32_t; -# elif ULONG_MAX == 4294967295UL - typedef unsigned long sexp_uint32_t; -# elif USHRT_MAX == 4294967295U - typedef unsigned short sexp_uint32_t; -# else -# error Could not find 32-bit type -# endif -#endif - -#endif /* CHIBI_CRYPTO_INTEGERS_H */ diff --git a/lib/chibi/crypto/sha2.c b/lib/chibi/crypto/sha2.c index c0a9c8a3..fe0d14f3 100644 --- a/lib/chibi/crypto/sha2.c +++ b/lib/chibi/crypto/sha2.c @@ -2,7 +2,9 @@ /* Copyright (c) 2015 Alexei Lozovsky. All rights reserved. */ /* BSD-style license: http://synthcode.com/license.txt */ -#include "integers.h" +#if !(SEXP_UINT8_DEFINED && SEXP_UINT32_DEFINED) +# error SHA-2 requires exact 8-bit and 32-bit integers to be available +#endif /* * SHA-2 algorithms are described in RFC 6234: