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