ssh

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers misc.c Source File

misc.c

00001 /* misc.c
00002  *
00003  * Copyright (C) 2014-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSH.
00006  *
00007  * wolfSSH is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSH is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with wolfSSH.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 /*
00022  * The misc module contains inline functions. This file is either included
00023  * into source files or built separately depending on the inline configure
00024  * option.
00025  */
00026 
00027 
00028 #ifdef HAVE_CONFIG_H
00029     #include <config.h>
00030 #endif
00031 
00032 
00033 #include <wolfssh/settings.h>
00034 
00035 
00036 #ifndef WOLFSSH_MISC_C
00037 #define WOLFSSH_MISC_C
00038 
00039 
00040 #include <wolfssh/misc.h>
00041 
00042 
00043 #ifdef NO_INLINE
00044     #define STATIC
00045 #else
00046     #define STATIC static
00047 #endif
00048 
00049 
00050 /* Check for if compiling misc.c when not needed. */
00051 #if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE)
00052     #define MISC_WARNING "misc.c does not need to be compiled when using inline (NO_INLINE not defined))"
00053 
00054     #ifndef _MSC_VER
00055         #warning MISC_WARNING
00056     #else
00057         #pragma message("warning: " MISC_WARNING)
00058     #endif
00059 
00060 #else /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
00061 
00062 
00063 #ifndef min
00064 STATIC INLINE word32 min(word32 a, word32 b)
00065 {
00066     return a > b ? b : a;
00067 }
00068 #endif /* min */
00069 
00070 
00071 /* convert opaque to 32 bit integer */
00072 STATIC INLINE void ato32(const byte* c, word32* u32)
00073 {
00074     *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
00075 }
00076 
00077 
00078 /* convert 32 bit integer to opaque */
00079 STATIC INLINE void c32toa(word32 u32, byte* c)
00080 {
00081     c[0] = (u32 >> 24) & 0xff;
00082     c[1] = (u32 >> 16) & 0xff;
00083     c[2] = (u32 >>  8) & 0xff;
00084     c[3] =  u32 & 0xff;
00085 }
00086 
00087 
00088 /* Make sure compiler doesn't skip */
00089 STATIC INLINE void ForceZero(const void* mem, word32 length)
00090 {
00091     volatile byte* z = (volatile byte*)mem;
00092 
00093     while (length--) *z++ = 0;
00094 }
00095 
00096 
00097 /* check all length bytes for equality, return 0 on success */
00098 STATIC INLINE int ConstantCompare(const byte* a, const byte* b,
00099                                   word32 length)
00100 {
00101     word32 i;
00102     word32 compareSum = 0;
00103 
00104     for (i = 0; i < length; i++) {
00105         compareSum |= a[i] ^ b[i];
00106     }
00107 
00108     return compareSum;
00109 }
00110 
00111 
00112 #undef STATIC
00113 
00114 
00115 #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
00116 
00117 
00118 #endif /* WOLFSSH_MISC_C */