1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
#ifndef SG_UNALIGNED_H
#define SG_UNALIGNED_H
/*
* Copyright (c) 2014 Douglas Gilbert.
* All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the BSD_LICENSE file.
*/
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Borrowed from the Linux kernel, via mhvtl */
/* In the first section below, functions that copy unsigned integers in
* a computer's native format, to and from an unaligned big endian sequence
* of bytes. Big endian byte format "on the wire" is the default used by
* SCSI standards (www.t10.org). */
static inline uint16_t __get_unaligned_be16(const uint8_t *p)
{
return p[0] << 8 | p[1];
}
static inline uint32_t __get_unaligned_be32(const uint8_t *p)
{
return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
}
static inline uint64_t __get_unaligned_be64(const uint8_t *p)
{
return (uint64_t)__get_unaligned_be32(p) << 32 |
__get_unaligned_be32(p + 4);
}
static inline void __put_unaligned_be16(uint16_t val, uint8_t *p)
{
*p++ = val >> 8;
*p++ = val;
}
static inline void __put_unaligned_be32(uint32_t val, uint8_t *p)
{
__put_unaligned_be16(val >> 16, p);
__put_unaligned_be16(val, p + 2);
}
static inline void __put_unaligned_be64(uint64_t val, uint8_t *p)
{
__put_unaligned_be32(val >> 32, p);
__put_unaligned_be32(val, p + 4);
}
static inline uint16_t sg_get_unaligned_be16(const void *p)
{
return __get_unaligned_be16((const uint8_t *)p);
}
static inline uint32_t sg_get_unaligned_be24(const uint8_t *p)
{
return p[0] << 16 | p[1] << 8 | p[2];
}
static inline uint32_t sg_get_unaligned_be32(const void *p)
{
return __get_unaligned_be32((const uint8_t *)p);
}
static inline uint64_t sg_get_unaligned_be64(const void *p)
{
return __get_unaligned_be64((const uint8_t *)p);
}
static inline void sg_put_unaligned_be16(uint16_t val, void *p)
{
__put_unaligned_be16(val, (uint8_t *)p);
}
static inline void sg_put_unaligned_be24(uint32_t val, void *p)
{
((uint8_t *)p)[0] = (val >> 16) & 0xff;
((uint8_t *)p)[1] = (val >> 8) & 0xff;
((uint8_t *)p)[2] = val & 0xff;
}
static inline void sg_put_unaligned_be32(uint32_t val, void *p)
{
__put_unaligned_be32(val, (uint8_t *)p);
}
static inline void sg_put_unaligned_be64(uint64_t val, void *p)
{
__put_unaligned_be64(val, (uint8_t *)p);
}
/* Since cdb and parameter blocks are often memset to zero before these
* unaligned function partially fill them, then check for a val of zero
* and ignore if it is with these variants. */
static inline void sg_nz_put_unaligned_be16(uint16_t val, void *p)
{
if (val)
__put_unaligned_be16(val, (uint8_t *)p);
}
static inline void sg_nz_put_unaligned_be24(uint32_t val, void *p)
{
if (val) {
((uint8_t *)p)[0] = (val >> 16) & 0xff;
((uint8_t *)p)[1] = (val >> 8) & 0xff;
((uint8_t *)p)[2] = val & 0xff;
}
}
static inline void sg_nz_put_unaligned_be32(uint32_t val, void *p)
{
if (val)
__put_unaligned_be32(val, (uint8_t *)p);
}
static inline void sg_nz_put_unaligned_be64(uint64_t val, void *p)
{
if (val)
__put_unaligned_be64(val, (uint8_t *)p);
}
/* Below are the little endian equivalents of the big endian functions
* above. Little endian is used by ATA, networking and PCI.
* This section could take advantage of the
* 'uint32_t htonl(uint32_t hostlong)' [and the complementary ntohl()]
* family of functions but that would introduce a dependency on the
* <arpa/inet.h> header. Also they don't address moving to and from
* an unaligned sequence of bytes. The latter would still need to be
* done.
*/
static inline uint16_t __get_unaligned_le16(const uint8_t *p)
{
return p[1] << 8 | p[0];
}
static inline uint32_t __get_unaligned_le32(const uint8_t *p)
{
return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
}
static inline uint64_t __get_unaligned_le64(const uint8_t *p)
{
return (uint64_t)__get_unaligned_le32(p + 4) << 32 |
__get_unaligned_le32(p);
}
static inline void __put_unaligned_le16(uint16_t val, uint8_t *p)
{
*p++ = val;
*p++ = val >> 8;
}
static inline void __put_unaligned_le32(uint32_t val, uint8_t *p)
{
__put_unaligned_le16(val >> 16, p + 2);
__put_unaligned_le16(val, p);
}
static inline void __put_unaligned_le64(uint64_t val, uint8_t *p)
{
__put_unaligned_le32(val >> 32, p + 4);
__put_unaligned_le32(val, p);
}
static inline uint16_t sg_get_unaligned_le16(const void *p)
{
return __get_unaligned_le16((const uint8_t *)p);
}
static inline uint32_t sg_get_unaligned_le24(const uint8_t *p)
{
return p[2] << 16 | p[1] << 8 | p[0];
}
static inline uint32_t sg_get_unaligned_le32(const void *p)
{
return __get_unaligned_le32((const uint8_t *)p);
}
static inline uint64_t sg_get_unaligned_le64(const void *p)
{
return __get_unaligned_le64((const uint8_t *)p);
}
static inline void sg_put_unaligned_le16(uint16_t val, void *p)
{
__put_unaligned_le16(val, (uint8_t *)p);
}
static inline void sg_put_unaligned_le24(uint32_t val, void *p)
{
((uint8_t *)p)[2] = (val >> 16) & 0xff;
((uint8_t *)p)[1] = (val >> 8) & 0xff;
((uint8_t *)p)[0] = val & 0xff;
}
static inline void sg_put_unaligned_le32(uint32_t val, void *p)
{
__put_unaligned_le32(val, (uint8_t *)p);
}
static inline void sg_put_unaligned_le64(uint64_t val, void *p)
{
__put_unaligned_le64(val, (uint8_t *)p);
}
/* Since cdb and parameter blocks are often memset to zero before these
* unaligned function partially fill them, then check for a val of zero
* and ignore if it is with these variants. */
static inline void sg_nz_put_unaligned_le16(uint16_t val, void *p)
{
if (val)
__put_unaligned_le16(val, (uint8_t *)p);
}
static inline void sg_nz_put_unaligned_le24(uint32_t val, void *p)
{
if (val) {
((uint8_t *)p)[2] = (val >> 16) & 0xff;
((uint8_t *)p)[1] = (val >> 8) & 0xff;
((uint8_t *)p)[0] = val & 0xff;
}
}
static inline void sg_nz_put_unaligned_le32(uint32_t val, void *p)
{
if (val)
__put_unaligned_le32(val, (uint8_t *)p);
}
static inline void sg_nz_put_unaligned_le64(uint64_t val, void *p)
{
if (val)
__put_unaligned_le64(val, (uint8_t *)p);
}
#ifdef __cplusplus
}
#endif
#endif /* SG_UNALIGNED_H */
|