tinydtls  0.8.6
numeric.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  *
8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
9  * and the Eclipse Distribution License is available at
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  *
12  * Contributors:
13  * Olaf Bergmann - initial API and implementation
14  * Hauke Mehrtens - memory optimization, ECC integration
15  *
16  *******************************************************************************/
17 
18 #ifndef _DTLS_NUMERIC_H_
19 #define _DTLS_NUMERIC_H_
20 
21 #include <stdint.h>
22 
23 #ifndef min
24 #define min(A,B) ((A) <= (B) ? (A) : (B))
25 #endif
26 
27 #ifndef max
28 #define max(A,B) ((A) < (B) ? (B) : (A))
29 #endif
30 
31 /* this one is for consistency... */
32 static inline int dtls_int_to_uint8(unsigned char *field, uint8_t value)
33 {
34  field[0] = value & 0xff;
35  return 1;
36 }
37 
38 static inline int dtls_int_to_uint16(unsigned char *field, uint16_t value)
39 {
40  field[0] = (value >> 8) & 0xff;
41  field[1] = value & 0xff;
42  return 2;
43 }
44 
45 static inline int dtls_int_to_uint24(unsigned char *field, uint32_t value)
46 {
47  field[0] = (value >> 16) & 0xff;
48  field[1] = (value >> 8) & 0xff;
49  field[2] = value & 0xff;
50  return 3;
51 }
52 
53 static inline int dtls_int_to_uint32(unsigned char *field, uint32_t value)
54 {
55  field[0] = (value >> 24) & 0xff;
56  field[1] = (value >> 16) & 0xff;
57  field[2] = (value >> 8) & 0xff;
58  field[3] = value & 0xff;
59  return 4;
60 }
61 
62 static inline int dtls_int_to_uint48(unsigned char *field, uint64_t value)
63 {
64  field[0] = (value >> 40) & 0xff;
65  field[1] = (value >> 32) & 0xff;
66  field[2] = (value >> 24) & 0xff;
67  field[3] = (value >> 16) & 0xff;
68  field[4] = (value >> 8) & 0xff;
69  field[5] = value & 0xff;
70  return 6;
71 }
72 
73 static inline int dtls_int_to_uint64(unsigned char *field, uint64_t value)
74 {
75  field[0] = (value >> 56) & 0xff;
76  field[1] = (value >> 48) & 0xff;
77  field[2] = (value >> 40) & 0xff;
78  field[3] = (value >> 32) & 0xff;
79  field[4] = (value >> 24) & 0xff;
80  field[5] = (value >> 16) & 0xff;
81  field[6] = (value >> 8) & 0xff;
82  field[7] = value & 0xff;
83  return 8;
84 }
85 
86 static inline uint8_t dtls_uint8_to_int(const unsigned char *field)
87 {
88  return (uint8_t)field[0];
89 }
90 
91 static inline uint16_t dtls_uint16_to_int(const unsigned char *field)
92 {
93  return ((uint16_t)field[0] << 8)
94  | (uint16_t)field[1];
95 }
96 
97 static inline uint32_t dtls_uint24_to_int(const unsigned char *field)
98 {
99  return ((uint32_t)field[0] << 16)
100  | ((uint32_t)field[1] << 8)
101  | (uint32_t)field[2];
102 }
103 
104 static inline uint32_t dtls_uint32_to_int(const unsigned char *field)
105 {
106  return ((uint32_t)field[0] << 24)
107  | ((uint32_t)field[1] << 16)
108  | ((uint32_t)field[2] << 8)
109  | (uint32_t)field[3];
110 }
111 
112 static inline uint64_t dtls_uint48_to_int(const unsigned char *field)
113 {
114  return ((uint64_t)field[0] << 40)
115  | ((uint64_t)field[1] << 32)
116  | ((uint64_t)field[2] << 24)
117  | ((uint64_t)field[3] << 16)
118  | ((uint64_t)field[4] << 8)
119  | (uint64_t)field[5];
120 }
121 
122 static inline uint64_t dtls_uint64_to_int(const unsigned char *field)
123 {
124  return ((uint64_t)field[0] << 56)
125  | ((uint64_t)field[1] << 48)
126  | ((uint64_t)field[2] << 40)
127  | ((uint64_t)field[3] << 32)
128  | ((uint64_t)field[4] << 24)
129  | ((uint64_t)field[5] << 16)
130  | ((uint64_t)field[6] << 8)
131  | (uint64_t)field[7];
132 }
133 
134 #endif /* _DTLS_NUMERIC_H_ */
static uint32_t dtls_uint32_to_int(const unsigned char *field)
Definition: numeric.h:104
static uint64_t dtls_uint64_to_int(const unsigned char *field)
Definition: numeric.h:122
static int dtls_int_to_uint48(unsigned char *field, uint64_t value)
Definition: numeric.h:62
static int dtls_int_to_uint24(unsigned char *field, uint32_t value)
Definition: numeric.h:45
static uint8_t dtls_uint8_to_int(const unsigned char *field)
Definition: numeric.h:86
static int dtls_int_to_uint64(unsigned char *field, uint64_t value)
Definition: numeric.h:73
static int dtls_int_to_uint16(unsigned char *field, uint16_t value)
Definition: numeric.h:38
static uint16_t dtls_uint16_to_int(const unsigned char *field)
Definition: numeric.h:91
unsigned int uint32_t
Definition: uthash.h:78
static int dtls_int_to_uint32(unsigned char *field, uint32_t value)
Definition: numeric.h:53
static uint64_t dtls_uint48_to_int(const unsigned char *field)
Definition: numeric.h:112
static int dtls_int_to_uint8(unsigned char *field, uint8_t value)
Definition: numeric.h:32
unsigned char uint8_t
Definition: uthash.h:79
static uint32_t dtls_uint24_to_int(const unsigned char *field)
Definition: numeric.h:97