From 0386a3c50fa8242e3c9c5fa077dffe483e2028a2 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 1 Apr 2016 14:40:22 -0700 Subject: [PATCH] move shared inline functions to misc file. add configure option to disable inline functions. --- .gitignore | 1 + configure.ac | 14 ++++ examples/echoserver/echoserver.c | 8 +-- examples/server/server.c | 8 +-- src/include.am | 4 ++ src/internal.c | 37 ++--------- src/io.c | 4 +- src/misc.c | 110 +++++++++++++++++++++++++++++++ src/ssh.c | 22 ++----- wolfssh/misc.h | 58 ++++++++++++++++ wolfssh/port.h | 16 ++--- 11 files changed, 217 insertions(+), 65 deletions(-) create mode 100644 src/misc.c create mode 100644 wolfssh/misc.h diff --git a/.gitignore b/.gitignore index d498c9b8..915f982a 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ aminclude.am # vim *.swp +*.swo tags diff .vimrc diff --git a/configure.ac b/configure.ac index 1ad1447f..6f1f5126 100644 --- a/configure.ac +++ b/configure.ac @@ -84,6 +84,20 @@ AS_IF([test "x$ax_enable_debug" = "xyes"], AX_PTHREAD([AM_CFLAGS="$AM_CFLAGS $PTHREAD_CFLAGS"]) + +# Inline Build +AC_ARG_ENABLE([inline], + [AS_HELP_STRING([--enable-inline],[Enable inline functions (default: enabled)])], + [ENABLED_INLINE=$enableval],[ENABLED_INLINE=yes]) + +if test "$ENABLED_INLINE" = "no" +then + AM_CFLAGS="$AM_CFLAGS -DNO_INLINE" +fi + +AM_CONDITIONAL([BUILD_INLINE], [test "x$ENABLED_INLINE" = "xyes"]) + + # Checks for typedefs, structures, and compiler characteristics. if test "$ac_cv_sizeof_long" = "8"; then AM_CPPFLAGS="$AM_CPPFLAGS -DSIZEOF_LONG=8" diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index b2e0dce3..8958eaf2 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -92,7 +92,7 @@ typedef struct { } thread_ctx_t; -static WINLINE void err_sys(const char* msg) +static INLINE void err_sys(const char* msg) { printf("server error: %s\n", msg); if (msg) @@ -100,7 +100,7 @@ static WINLINE void err_sys(const char* msg) } -static WINLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, +static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, uint16_t port) { int useLookup = 0; @@ -177,7 +177,7 @@ static WINLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, } -static WINLINE void tcp_socket(SOCKET_T* sockFd) +static INLINE void tcp_socket(SOCKET_T* sockFd) { *sockFd = socket(AF_INET_V, SOCK_STREAM, 0); @@ -217,7 +217,7 @@ static WINLINE void tcp_socket(SOCKET_T* sockFd) } -static WINLINE void tcp_bind(SOCKET_T* sockFd, uint16_t port, int useAnyAddr) +static INLINE void tcp_bind(SOCKET_T* sockFd, uint16_t port, int useAnyAddr) { SOCKADDR_IN_T addr; diff --git a/examples/server/server.c b/examples/server/server.c index acb10c50..6b5246f9 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -92,7 +92,7 @@ typedef struct { } thread_ctx_t; -static WINLINE void err_sys(const char* msg) +static INLINE void err_sys(const char* msg) { printf("server error: %s\n", msg); if (msg) @@ -100,7 +100,7 @@ static WINLINE void err_sys(const char* msg) } -static WINLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, +static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, uint16_t port) { int useLookup = 0; @@ -177,7 +177,7 @@ static WINLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, } -static WINLINE void tcp_socket(SOCKET_T* sockFd) +static INLINE void tcp_socket(SOCKET_T* sockFd) { *sockFd = socket(AF_INET_V, SOCK_STREAM, 0); @@ -217,7 +217,7 @@ static WINLINE void tcp_socket(SOCKET_T* sockFd) } -static WINLINE void tcp_bind(SOCKET_T* sockFd, uint16_t port, int useAnyAddr) +static INLINE void tcp_bind(SOCKET_T* sockFd, uint16_t port, int useAnyAddr) { SOCKADDR_IN_T addr; diff --git a/src/include.am b/src/include.am index 8157f944..289765f1 100644 --- a/src/include.am +++ b/src/include.am @@ -15,3 +15,7 @@ src_libwolfssh_la_CPPFLAGS = -DBUILDING_WOLFSSH $(AM_CPPFLAGS) src_libwolfssh_la_LDFLAGS = ${AM_LDFLAGS} -no-undefined -version-info ${WOLFSSH_LIBRARY_VERSION} src_libwolfssh_la_DEPENDENCIES = EXTRA_DIST += + +if !BUILD_INLINE +src_libwolfssh_la_SOURCES += src/misc.c +endif diff --git a/src/internal.c b/src/internal.c index aeaaa46e..5fa848d1 100644 --- a/src/internal.c +++ b/src/internal.c @@ -38,37 +38,12 @@ #include #include - -/* convert opaque to 32 bit integer */ -static INLINE void ato32(const uint8_t* c, uint32_t* u32) -{ - *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; -} - - -/* convert 32 bit integer to opaque */ -static INLINE void c32toa(uint32_t u32, uint8_t* c) -{ - c[0] = (u32 >> 24) & 0xff; - c[1] = (u32 >> 16) & 0xff; - c[2] = (u32 >> 8) & 0xff; - c[3] = u32 & 0xff; -} - - -/* check all length bytes for equality, return 0 on success */ -static INLINE int ConstantCompare(const uint8_t* a, const uint8_t* b, - uint32_t length) -{ - uint32_t i; - uint32_t compareSum = 0; - - for (i = 0; i < length; i++) { - compareSum |= a[i] ^ b[i]; - } - - return compareSum; -} +#ifdef NO_INLINE + #include +#else + #define WOLFSSH_MISC_INCLUDED + #include "src/misc.c" +#endif const char* GetErrorString(int err) diff --git a/src/io.c b/src/io.c index 94674bc4..3de4548f 100644 --- a/src/io.c +++ b/src/io.c @@ -235,7 +235,7 @@ void* wolfSSH_GetIOWriteCtx(WOLFSSH* ssh) /* Translates return codes returned from * send() and recv() if need be. */ -static WINLINE int TranslateReturnCode(int old, int sd) +static INLINE int TranslateReturnCode(int old, int sd) { (void)sd; @@ -255,7 +255,7 @@ static WINLINE int TranslateReturnCode(int old, int sd) return old; } -static WINLINE int LastError(void) +static INLINE int LastError(void) { #ifdef USE_WINDOWS_API return WSAGetLastError(); diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 00000000..e5356d12 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,110 @@ +/* misc.c + * + * Copyright (C) 2014-2016 wolfSSL Inc. + * + * This file is part of wolfSSH. + * + * wolfSSH is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +/* + * The misc module contains inline functions. This file is either included + * into source files or built separately depending on the inline configure + * option. + */ + + +#ifdef HAVE_CONFIG_H + #include +#endif + + +#include + + +#ifndef WOLFSSH_MISC_C +#define WOLFSSH_MISC_C + + +#include + + +#ifdef NO_INLINE + #define STATIC +#else + #define STATIC static +#endif + + +/* Check for if compiling misc.c when not needed. */ +#if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE) + #error misc.c does not need to be compiled when not defined NO_INLINE +#endif + + +#ifndef min +STATIC INLINE uint32_t min(uint32_t a, uint32_t b) +{ + return a > b ? b : a; +} +#endif /* min */ + + +/* convert opaque to 32 bit integer */ +STATIC INLINE void ato32(const uint8_t* c, uint32_t* u32) +{ + *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; +} + + +/* convert 32 bit integer to opaque */ +STATIC INLINE void c32toa(uint32_t u32, uint8_t* c) +{ + c[0] = (u32 >> 24) & 0xff; + c[1] = (u32 >> 16) & 0xff; + c[2] = (u32 >> 8) & 0xff; + c[3] = u32 & 0xff; +} + + +/* Make sure compiler doesn't skip */ +STATIC INLINE void ForceZero(const void* mem, uint32_t length) +{ + volatile uint8_t* z = (volatile uint8_t*)mem; + + while (length--) *z++ = 0; +} + + +/* check all length bytes for equality, return 0 on success */ +STATIC INLINE int ConstantCompare(const uint8_t* a, const uint8_t* b, + uint32_t length) +{ + uint32_t i; + uint32_t compareSum = 0; + + for (i = 0; i < length; i++) { + compareSum |= a[i] ^ b[i]; + } + + return compareSum; +} + + +#undef STATIC + + +#endif /* WOLFSSH_MISC_C */ diff --git a/src/ssh.c b/src/ssh.c index d29d0d0f..581efbcd 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -36,22 +36,12 @@ #include #include - -#ifndef min -static INLINE uint32_t min(uint32_t a, uint32_t b) -{ - return a > b ? b : a; -} -#endif /* min */ - - -/* Make sure compiler doesn't skip */ -static INLINE void ForceZero(const void* mem, uint32_t length) -{ - volatile byte* z = (volatile byte*)mem; - - while (length--) *z++ = 0; -} +#ifdef NO_INLINE + #include +#else + #define WOLFSSH_MISC_INCLUDED + #include "src/misc.c" +#endif int wolfSSH_Init(void) diff --git a/wolfssh/misc.h b/wolfssh/misc.h new file mode 100644 index 00000000..53ac5d55 --- /dev/null +++ b/wolfssh/misc.h @@ -0,0 +1,58 @@ +/* misc.h + * + * Copyright (C) 2006-2016 wolfSSL Inc. + * + * This file is part of wolfSSH. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#pragma once + +#ifndef WOLFSSH_MISC_H +#define WOLFSSH_MISC_H + + +#ifdef __cplusplus + extern "C" { +#endif + + +#include + + +#ifdef NO_INLINE + + +#ifndef min +WOLFSSH_LOCAL uint32_t min(uint32_t, uint32_t); +#endif /* min */ + +WOLFSSH_LOCAL void ato32(const uint8_t*, uint32_t*); +WOLFSSH_LOCAL void c32toa(uint32_t, uint8_t*); +WOLFSSH_LOCAL void ForceZero(const void*, uint32_t); +WOLFSSH_LOCAL int ConstantCompare(const uint8_t*, const uint8_t*, uint32_t); + + +#endif /* NO_INLINE */ + + +#ifdef __cplusplus + } /* extern "C" */ +#endif + + +#endif /* WOLFSSH_MISC_H */ + diff --git a/wolfssh/port.h b/wolfssh/port.h index 972aa77c..e4bd9f63 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -85,23 +85,23 @@ extern "C" { /* setup compiler inlining */ -#ifndef WINLINE +#ifndef INLINE #ifndef NO_INLINE #ifdef _MSC_VER - #define WINLINE __inline + #define INLINE __inline #elif defined(__GNUC__) - #define WINLINE inline + #define INLINE inline #elif defined(__IAR_SYSTEMS_ICC__) - #define WINLINE inline + #define INLINE inline #elif defined(THREADX) - #define WINLINE _Inline + #define INLINE _Inline #else - #define WINLINE + #define INLINE #endif #else - #define WINLINE + #define INLINE #endif -#endif /* WINLINE */ +#endif /* INLINE */