1 /* dtls -- a very basic DTLS implementation
2 *
3 * Copyright (C) 2011 Olaf Bergmann <bergmann@tzi.org>
4 *
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:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
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
23 * SOFTWARE.
24 */
25
26 #ifndef _DTLS_NUMERIC_H_
27 #define _DTLS_NUMERIC_H_
28
29 #include <stdint.h>
30
31 #ifndef min
32 #define min(A,B) ((A) <= (B) ? (A) : (B))
33 #endif
34
35 #ifndef max
36 #define max(A,B) ((A) < (B) ? (B) : (A))
37 #endif
38
39 /* this one is for consistency... */
dtls_int_to_uint8(unsigned char * field,uint8_t value)40 static inline int dtls_int_to_uint8(unsigned char *field, uint8_t value)
41 {
42 field[0] = value & 0xff;
43 return 1;
44 }
45
dtls_int_to_uint16(unsigned char * field,uint16_t value)46 static inline int dtls_int_to_uint16(unsigned char *field, uint16_t value)
47 {
48 field[0] = (value >> 8) & 0xff;
49 field[1] = value & 0xff;
50 return 2;
51 }
52
dtls_int_to_uint24(unsigned char * field,uint32_t value)53 static inline int dtls_int_to_uint24(unsigned char *field, uint32_t value)
54 {
55 field[0] = (value >> 16) & 0xff;
56 field[1] = (value >> 8) & 0xff;
57 field[2] = value & 0xff;
58 return 3;
59 }
60
dtls_int_to_uint32(unsigned char * field,uint32_t value)61 static inline int dtls_int_to_uint32(unsigned char *field, uint32_t value)
62 {
63 field[0] = (value >> 24) & 0xff;
64 field[1] = (value >> 16) & 0xff;
65 field[2] = (value >> 8) & 0xff;
66 field[3] = value & 0xff;
67 return 4;
68 }
69
dtls_int_to_uint48(unsigned char * field,uint64_t value)70 static inline int dtls_int_to_uint48(unsigned char *field, uint64_t value)
71 {
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;
78 return 6;
79 }
80
dtls_int_to_uint64(unsigned char * field,uint64_t value)81 static inline int dtls_int_to_uint64(unsigned char *field, uint64_t value)
82 {
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;
91 return 8;
92 }
93
dtls_uint8_to_int(const unsigned char * field)94 static inline uint8_t dtls_uint8_to_int(const unsigned char *field)
95 {
96 return (uint8_t)field[0];
97 }
98
dtls_uint16_to_int(const unsigned char * field)99 static inline uint16_t dtls_uint16_to_int(const unsigned char *field)
100 {
101 return ((uint16_t)field[0] << 8)
102 | (uint16_t)field[1];
103 }
104
dtls_uint24_to_int(const unsigned char * field)105 static inline uint32_t dtls_uint24_to_int(const unsigned char *field)
106 {
107 return ((uint32_t)field[0] << 16)
108 | ((uint32_t)field[1] << 8)
109 | (uint32_t)field[2];
110 }
111
dtls_uint32_to_int(const unsigned char * field)112 static inline uint32_t dtls_uint32_to_int(const unsigned char *field)
113 {
114 return ((uint32_t)field[0] << 24)
115 | ((uint32_t)field[1] << 16)
116 | ((uint32_t)field[2] << 8)
117 | (uint32_t)field[3];
118 }
119
dtls_uint48_to_int(const unsigned char * field)120 static inline uint64_t dtls_uint48_to_int(const unsigned char *field)
121 {
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];
128 }
129
dtls_uint64_to_int(const unsigned char * field)130 static inline uint64_t dtls_uint64_to_int(const unsigned char *field)
131 {
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];
140 }
141
142 #endif /* _DTLS_NUMERIC_H_ */
143