1 /* dtls -- a very basic DTLS implementation
3 * Copyright (C) 2011 Olaf Bergmann <bergmann@tzi.org>
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #ifndef _DTLS_NUMERIC_H_
27 #define _DTLS_NUMERIC_H_
32 #define min(A,B) ((A) <= (B) ? (A) : (B))
36 #define max(A,B) ((A) < (B) ? (B) : (A))
39 /* this one is for consistency... */
40 INLINE_API int dtls_int_to_uint8(unsigned char *field, uint8_t value)
42 field[0] = value & 0xff;
46 INLINE_API int dtls_int_to_uint16(unsigned char *field, uint16_t value)
48 field[0] = (value >> 8) & 0xff;
49 field[1] = value & 0xff;
53 INLINE_API int dtls_int_to_uint24(unsigned char *field, uint32_t value)
55 field[0] = (value >> 16) & 0xff;
56 field[1] = (value >> 8) & 0xff;
57 field[2] = value & 0xff;
61 INLINE_API int dtls_int_to_uint32(unsigned char *field, uint32_t value)
63 field[0] = (value >> 24) & 0xff;
64 field[1] = (value >> 16) & 0xff;
65 field[2] = (value >> 8) & 0xff;
66 field[3] = value & 0xff;
70 INLINE_API int dtls_int_to_uint48(unsigned char *field, uint64_t value)
72 field[0] = (value >> 40) & 0xff;
73 field[1] = (value >> 32) & 0xff;
74 field[2] = (value >> 24) & 0xff;
75 field[3] = (value >> 16) & 0xff;
76 field[4] = (value >> 8) & 0xff;
77 field[5] = value & 0xff;
81 INLINE_API int dtls_int_to_uint64(unsigned char *field, uint64_t value)
83 field[0] = (value >> 56) & 0xff;
84 field[1] = (value >> 48) & 0xff;
85 field[2] = (value >> 40) & 0xff;
86 field[3] = (value >> 32) & 0xff;
87 field[4] = (value >> 24) & 0xff;
88 field[5] = (value >> 16) & 0xff;
89 field[6] = (value >> 8) & 0xff;
90 field[7] = value & 0xff;
94 INLINE_API uint8_t dtls_uint8_to_int(const unsigned char *field)
96 return (uint8_t)field[0];
99 INLINE_API uint16_t dtls_uint16_to_int(const unsigned char *field)
101 return ((uint16_t)field[0] << 8)
102 | (uint16_t)field[1];
105 INLINE_API uint32_t dtls_uint24_to_int(const unsigned char *field)
107 return ((uint32_t)field[0] << 16)
108 | ((uint32_t)field[1] << 8)
109 | (uint32_t)field[2];
112 INLINE_API uint32_t dtls_uint32_to_int(const unsigned char *field)
114 return ((uint32_t)field[0] << 24)
115 | ((uint32_t)field[1] << 16)
116 | ((uint32_t)field[2] << 8)
117 | (uint32_t)field[3];
120 INLINE_API uint64_t dtls_uint48_to_int(const unsigned char *field)
122 return ((uint64_t)field[0] << 40)
123 | ((uint64_t)field[1] << 32)
124 | ((uint64_t)field[2] << 24)
125 | ((uint64_t)field[3] << 16)
126 | ((uint64_t)field[4] << 8)
127 | (uint64_t)field[5];
130 INLINE_API uint64_t dtls_uint64_to_int(const unsigned char *field)
132 return ((uint64_t)field[0] << 56)
133 | ((uint64_t)field[1] << 48)
134 | ((uint64_t)field[2] << 40)
135 | ((uint64_t)field[3] << 32)
136 | ((uint64_t)field[4] << 24)
137 | ((uint64_t)field[5] << 16)
138 | ((uint64_t)field[6] << 8)
139 | (uint64_t)field[7];
142 #endif /* _DTLS_NUMERIC_H_ */