tor  0.4.2.0-alpha-dev
bytes.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 #ifndef TOR_BYTES_H
7 #define TOR_BYTES_H
8 
16 #include <string.h>
17 #include "lib/cc/torint.h"
18 
19 /* The uint8 variants are defined to make the code more uniform. */
20 static inline uint8_t
21 get_uint8(const void *cp)
22 {
23  return *(const uint8_t*)(cp);
24 }
25 static inline void
26 set_uint8(void *cp, uint8_t v)
27 {
28  *(uint8_t*)cp = v;
29 }
30 
36 static inline uint16_t
37 get_uint16(const void *cp)
38 {
39  uint16_t v;
40  memcpy(&v,cp,2);
41  return v;
42 }
48 static inline uint32_t
49 get_uint32(const void *cp)
50 {
51  uint32_t v;
52  memcpy(&v,cp,4);
53  return v;
54 }
60 static inline uint64_t
61 get_uint64(const void *cp)
62 {
63  uint64_t v;
64  memcpy(&v,cp,8);
65  return v;
66 }
67 
72 static inline void
73 set_uint16(void *cp, uint16_t v)
74 {
75  memcpy(cp,&v,2);
76 }
81 static inline void
82 set_uint32(void *cp, uint32_t v)
83 {
84  memcpy(cp,&v,4);
85 }
90 static inline void
91 set_uint64(void *cp, uint64_t v)
92 {
93  memcpy(cp,&v,8);
94 }
95 
96 #ifdef WORDS_BIGENDIAN
97 static inline uint16_t
98 tor_htons(uint32_t a)
99 {
100  return a;
101 }
102 
103 static inline uint16_t
104 tor_ntohs(uint64_t a)
105 {
106  return a;
107 }
108 
109 static inline uint32_t
110 tor_htonl(uint32_t a)
111 {
112  return a;
113 }
114 
115 static inline uint32_t
116 tor_ntohl(uint64_t a)
117 {
118  return a;
119 }
120 
121 static inline uint64_t
122 tor_htonll(uint64_t a)
123 {
124  return a;
125 }
126 
127 static inline uint64_t
128 tor_ntohll(uint64_t a)
129 {
130  return a;
131 }
132 #else /* !(defined(WORDS_BIGENDIAN)) */
133 static inline uint16_t
134 tor_htons(uint16_t a)
135 {
136  /* Our compilers will indeed recognize this as bswap. */
137  return
138  ((a & 0x00ff) << 8) |
139  ((a & 0xff00) >> 8);
140 }
141 
142 static inline uint16_t
143 tor_ntohs(uint16_t a)
144 {
145  return tor_htons(a);
146 }
147 
148 static inline uint32_t
149 tor_htonl(uint32_t a)
150 {
151  /* Our compilers will indeed recognize this as bswap. */
152  return
153  ((a & 0x000000ff) <<24) |
154  ((a & 0x0000ff00) << 8) |
155  ((a & 0x00ff0000) >> 8) |
156  ((a & 0xff000000) >>24);
157 }
158 
159 static inline uint32_t
160 tor_ntohl(uint32_t a)
161 {
162  return tor_htonl(a);
163 }
164 
166 static inline uint64_t
167 tor_htonll(uint64_t a)
168 {
169  /* Little endian. The worst... */
170  return tor_htonl((uint32_t)(a>>32)) |
171  (((uint64_t)tor_htonl((uint32_t)a))<<32);
172 }
173 
175 static inline uint64_t
176 tor_ntohll(uint64_t a)
177 {
178  return tor_htonll(a);
179 }
180 #endif /* defined(WORDS_BIGENDIAN) */
181 
182 #endif /* !defined(TOR_BYTES_H) */
static uint64_t tor_ntohll(uint64_t a)
Definition: bytes.h:176
static void set_uint16(void *cp, uint16_t v)
Definition: bytes.h:73
static uint64_t get_uint64(const void *cp)
Definition: bytes.h:61
Integer definitions used throughout Tor.
static void set_uint32(void *cp, uint32_t v)
Definition: bytes.h:82
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:49
static void set_uint64(void *cp, uint64_t v)
Definition: bytes.h:91
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:37
static uint64_t tor_htonll(uint64_t a)
Definition: bytes.h:167