mbed port of tinydtls

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers numeric.h Source File

numeric.h

00001 /* dtls -- a very basic DTLS implementation
00002  *
00003  * Copyright (C) 2011 Olaf Bergmann <bergmann@tzi.org>
00004  *
00005  * Permission is hereby granted, free of charge, to any person
00006  * obtaining a copy of this software and associated documentation
00007  * files (the "Software"), to deal in the Software without
00008  * restriction, including without limitation the rights to use, copy,
00009  * modify, merge, publish, distribute, sublicense, and/or sell copies
00010  * of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be
00014  * included in all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00020  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00021  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00023  * SOFTWARE.
00024  */
00025 
00026 #ifndef _NUMERIC_H_
00027 #define _NUMERIC_H_
00028 
00029 #include <stdint.h>
00030 
00031 #ifndef min
00032 #define min(A,B) ((A) <= (B) ? (A) : (B))
00033 #endif
00034 
00035 #ifndef max
00036 #define max(A,B) ((A) < (B) ? (B) : (A))
00037 #endif
00038 
00039 /**
00040  * Increments given \p Var of type \p Type by \c 1.
00041  *
00042  * \hideinitializer
00043  */
00044 #define inc_uint(Type,Var) {            \
00045     int i = sizeof(Type);           \
00046     while (i && !++((Var)[--i]));       \
00047   }
00048 
00049 /* this one is for consistency... */
00050 #define dtls_int_to_uint8(Field,Value) do {         \
00051     *(unsigned char*)(Field) = (Value) & 0xff;          \
00052   } while(0)
00053 
00054 #define dtls_int_to_uint16(Field,Value) do {            \
00055     *(unsigned char*)(Field) = ((Value) >> 8) & 0xff;       \
00056     *(((unsigned char*)(Field))+1) = ((Value) & 0xff);      \
00057   } while(0)
00058 
00059 #define dtls_int_to_uint24(Field,Value) do {            \
00060     *(unsigned char*)(Field) = ((Value) >> 16) & 0xff;      \
00061     dtls_int_to_uint16((((unsigned char*)(Field))+1),Value);    \
00062   } while(0)
00063 
00064 #define dtls_int_to_uint32(Field,Value) do {                \
00065     *(unsigned char*)(Field) = ((Value) >> 24) & 0xff;          \
00066     *(((unsigned char*)(Field))+1) = ((Value) >> 16) & 0xff;        \
00067     *(((unsigned char*)(Field))+2) = ((Value) >> 8) & 0xff;     \
00068     *(((unsigned char*)(Field))+3) = (Value) & 0xff;            \
00069   } while(0)
00070 
00071 #define dtls_ulong_to_uint48(Field,Value) do {              \
00072     *(unsigned char*)(Field) = ((Value) >> 40) & 0xff;          \
00073     *(((unsigned char*)(Field))+1) = ((Value) >> 32) & 0xff;        \
00074     *(((unsigned char*)(Field))+2) = ((Value) >> 24) & 0xff;        \
00075     *(((unsigned char*)(Field))+3) = ((Value) >> 16) & 0xff;        \
00076     *(((unsigned char*)(Field))+4) = ((Value) >> 8) & 0xff;     \
00077     *(((unsigned char*)(Field))+5) = (Value) & 0xff;            \
00078   } while(0)
00079 
00080 #define dtls_ulong_to_uint64(Field,Value) do {              \
00081     *(unsigned char*)(Field) = ((Value) >> 56) & 0xff;          \
00082     *(((unsigned char*)(Field))+1) = ((Value) >> 48) & 0xff;        \
00083     *(((unsigned char*)(Field))+2) = ((Value) >> 40) & 0xff;        \
00084     *(((unsigned char*)(Field))+3) = ((Value) >> 32) & 0xff;        \
00085     *(((unsigned char*)(Field))+4) = ((Value) >> 24) & 0xff;        \
00086     *(((unsigned char*)(Field))+5) = ((Value) >> 16) & 0xff;        \
00087     *(((unsigned char*)(Field))+6) = ((Value) >> 8) & 0xff;     \
00088     *(((unsigned char*)(Field))+7) = (Value) & 0xff;            \
00089   } while(0)
00090 
00091 #define dtls_uint8_to_int(Field) \
00092   (*(unsigned char*)(Field) & 0xFF)
00093 
00094 #define dtls_uint16_to_int(Field) \
00095   (((*(unsigned char*)(Field)) << 8) | (*(((unsigned char*)(Field))+1)))
00096 
00097 #define dtls_uint24_to_int(Field)       \
00098   (((*(((unsigned char*)(Field)))) << 16)   \
00099    | ((*(((unsigned char*)(Field))+1)) << 8)    \
00100    | ((*(((unsigned char*)(Field))+2))))
00101   
00102 #define dtls_uint48_to_ulong(Field)         \
00103   (((uint64_t) *(unsigned char*)(Field)) << 40)     \
00104   | (((uint64_t) *(((unsigned char*)(Field))+1)) << 32) \
00105   | (((uint64_t) *(((unsigned char*)(Field))+2)) << 24) \
00106   | (((uint64_t) *(((unsigned char*)(Field))+3)) << 16) \
00107   | (((uint64_t) *(((unsigned char*)(Field))+4)) << 8)  \
00108   | (((uint64_t) *(((unsigned char*)(n))+5)))
00109 
00110 #endif /* _NUMERIC_H_ */