Merge pull request #2 from ejohnstown/inline-option

Inline Option
pull/5/head v0.1
dgarske 2016-04-01 15:07:37 -07:00
commit 2888063b15
11 changed files with 217 additions and 65 deletions

1
.gitignore vendored
View File

@ -42,6 +42,7 @@ aminclude.am
# vim
*.swp
*.swo
tags
diff
.vimrc

View File

@ -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"

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -38,37 +38,12 @@
#include <cyassl/ctaocrypt/rsa.h>
#include <cyassl/ctaocrypt/hmac.h>
/* 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 <wolfssh/misc.h>
#else
#define WOLFSSH_MISC_INCLUDED
#include "src/misc.c"
#endif
const char* GetErrorString(int err)

View File

@ -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();

110
src/misc.c 100644
View File

@ -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 <config.h>
#endif
#include <wolfssh/settings.h>
#ifndef WOLFSSH_MISC_C
#define WOLFSSH_MISC_C
#include <wolfssh/misc.h>
#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 */

View File

@ -36,22 +36,12 @@
#include <cyassl/ctaocrypt/rsa.h>
#include <cyassl/ctaocrypt/asn.h>
#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 <wolfssh/misc.h>
#else
#define WOLFSSH_MISC_INCLUDED
#include "src/misc.c"
#endif
int wolfSSH_Init(void)

58
wolfssh/misc.h 100644
View File

@ -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 <wolfssh/port.h>
#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 */

View File

@ -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 */