| | varnish-cache/include/vend.h |
| 0 |
|
/*- |
| 1 |
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD |
| 2 |
|
* |
| 3 |
|
* Copyright (c) 2003,2010 Poul-Henning Kamp <phk@FreeBSD.org> |
| 4 |
|
* All rights reserved. |
| 5 |
|
* |
| 6 |
|
* Redistribution and use in source and binary forms, with or without |
| 7 |
|
* modification, are permitted provided that the following conditions |
| 8 |
|
* are met: |
| 9 |
|
* 1. Redistributions of source code must retain the above copyright |
| 10 |
|
* notice, this list of conditions and the following disclaimer. |
| 11 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer in the |
| 13 |
|
* documentation and/or other materials provided with the distribution. |
| 14 |
|
* |
| 15 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND |
| 16 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE |
| 19 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 |
|
* SUCH DAMAGE. |
| 26 |
|
* |
| 27 |
|
* From: |
| 28 |
|
* $FreeBSD: head/sys/sys/endian.h 121122 2003-10-15 20:05:57Z obrien $ |
| 29 |
|
* |
| 30 |
|
* Endian conversion functions |
| 31 |
|
*/ |
| 32 |
|
|
| 33 |
|
#ifndef VEND_H_INCLUDED |
| 34 |
|
#define VEND_H_INCLUDED |
| 35 |
|
|
| 36 |
|
/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ |
| 37 |
|
|
| 38 |
|
static __inline uint16_t |
| 39 |
10853 |
vbe16dec(const void *pp) |
| 40 |
|
{ |
| 41 |
10853 |
uint8_t const *p = (uint8_t const *)pp; |
| 42 |
|
|
| 43 |
10853 |
return ((p[0] << 8) | p[1]); |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
static __inline uint32_t |
| 47 |
29244 |
vbe32dec(const void *pp) |
| 48 |
|
{ |
| 49 |
29244 |
uint8_t const *p = (uint8_t const *)pp; |
| 50 |
|
|
| 51 |
29244 |
return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
static __inline uint64_t |
| 55 |
9480 |
vbe64dec(const void *pp) |
| 56 |
|
{ |
| 57 |
9480 |
uint8_t const *p = (uint8_t const *)pp; |
| 58 |
|
|
| 59 |
9480 |
return (((uint64_t)vbe32dec(p) << 32) | vbe32dec(p + 4)); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
#if 0 |
| 63 |
|
static __inline uint16_t |
| 64 |
|
vle16dec(const void *pp) |
| 65 |
|
{ |
| 66 |
|
uint8_t const *p = (uint8_t const *)pp; |
| 67 |
|
|
| 68 |
|
return ((p[1] << 8) | p[0]); |
| 69 |
|
} |
| 70 |
|
#endif |
| 71 |
|
|
| 72 |
|
static __inline uint32_t |
| 73 |
1808 |
vle32dec(const void *pp) |
| 74 |
|
{ |
| 75 |
1808 |
uint8_t const *p = (uint8_t const *)pp; |
| 76 |
|
|
| 77 |
1808 |
return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
#if 0 |
| 81 |
|
static __inline uint64_t |
| 82 |
|
vle64dec(const void *pp) |
| 83 |
|
{ |
| 84 |
|
uint8_t const *p = (uint8_t const *)pp; |
| 85 |
|
|
| 86 |
|
return (((uint64_t)vle32dec(p + 4) << 32) | vle32dec(p)); |
| 87 |
|
} |
| 88 |
|
#endif |
| 89 |
|
|
| 90 |
|
static __inline void |
| 91 |
5186 |
vbe16enc(void *pp, uint16_t u) |
| 92 |
|
{ |
| 93 |
5186 |
uint8_t *p = (uint8_t *)pp; |
| 94 |
|
|
| 95 |
5186 |
p[0] = (u >> 8) & 0xff; |
| 96 |
5186 |
p[1] = u & 0xff; |
| 97 |
5186 |
} |
| 98 |
|
|
| 99 |
|
static __inline void |
| 100 |
23473 |
vbe32enc(void *pp, uint32_t u) |
| 101 |
|
{ |
| 102 |
23473 |
uint8_t *p = (uint8_t *)pp; |
| 103 |
|
|
| 104 |
23473 |
p[0] = (u >> 24) & 0xff; |
| 105 |
23473 |
p[1] = (u >> 16) & 0xff; |
| 106 |
23473 |
p[2] = (u >> 8) & 0xff; |
| 107 |
23473 |
p[3] = u & 0xff; |
| 108 |
23473 |
} |
| 109 |
|
|
| 110 |
|
static __inline void |
| 111 |
9428 |
vbe64enc(void *pp, uint64_t u) |
| 112 |
|
{ |
| 113 |
9428 |
uint8_t *p = (uint8_t *)pp; |
| 114 |
|
|
| 115 |
9428 |
vbe32enc(p, (uint32_t)(u >> 32)); |
| 116 |
9428 |
vbe32enc(p + 4, (uint32_t)(u & 0xffffffffU)); |
| 117 |
9428 |
} |
| 118 |
|
|
| 119 |
|
static __inline void |
| 120 |
160 |
vle16enc(void *pp, uint16_t u) |
| 121 |
|
{ |
| 122 |
160 |
uint8_t *p = (uint8_t *)pp; |
| 123 |
|
|
| 124 |
160 |
p[0] = u & 0xff; |
| 125 |
160 |
p[1] = (u >> 8) & 0xff; |
| 126 |
160 |
} |
| 127 |
|
|
| 128 |
|
static __inline void |
| 129 |
82 |
vle32enc(void *pp, uint32_t u) |
| 130 |
|
{ |
| 131 |
82 |
uint8_t *p = (uint8_t *)pp; |
| 132 |
|
|
| 133 |
82 |
p[0] = u & 0xff; |
| 134 |
82 |
p[1] = (u >> 8) & 0xff; |
| 135 |
82 |
p[2] = (u >> 16) & 0xff; |
| 136 |
82 |
p[3] = (u >> 24) & 0xff; |
| 137 |
82 |
} |
| 138 |
|
|
| 139 |
|
#if 0 |
| 140 |
|
static __inline void |
| 141 |
|
vle64enc(void *pp, uint64_t u) |
| 142 |
|
{ |
| 143 |
|
uint8_t *p = (uint8_t *)pp; |
| 144 |
|
|
| 145 |
|
vle32enc(p, (uint32_t)(u & 0xffffffffU)); |
| 146 |
|
vle32enc(p + 4, (uint32_t)(u >> 32)); |
| 147 |
|
} |
| 148 |
|
#endif |
| 149 |
|
|
| 150 |
|
#endif |