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 401755
vbe16dec(const void *pp)
41
{
42 401755
        uint8_t const *p = (uint8_t const *)pp;
43
44 401755
        return ((p[0] << 8) | p[1]);
45
}
46
47
static __inline uint32_t
48 15455876
vbe32dec(const void *pp)
49
{
50 15455876
        uint8_t const *p = (uint8_t const *)pp;
51
52 15455876
        return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
53
}
54
55
static __inline uint64_t
56 230958
vbe64dec(const void *pp)
57
{
58 230958
        uint8_t const *p = (uint8_t const *)pp;
59
60 230958
        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 72160
vle32dec(const void *pp)
75
{
76 72160
        uint8_t const *p = (uint8_t const *)pp;
77
78 72160
        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 178423
vbe16enc(void *pp, uint16_t u)
93
{
94 178423
        uint8_t *p = (uint8_t *)pp;
95
96 178423
        p[0] = (u >> 8) & 0xff;
97 178423
        p[1] = u & 0xff;
98 178423
}
99
100
static __inline void
101 13254235
vbe32enc(void *pp, uint32_t u)
102
{
103 13254235
        uint8_t *p = (uint8_t *)pp;
104
105 13254235
        p[0] = (u >> 24) & 0xff;
106 13254235
        p[1] = (u >> 16) & 0xff;
107 13254235
        p[2] = (u >> 8) & 0xff;
108 13254235
        p[3] = u & 0xff;
109 13254235
}
110
111
static __inline void
112 4587142
vbe64enc(void *pp, uint64_t u)
113
{
114 4587142
        uint8_t *p = (uint8_t *)pp;
115
116 4587142
        vbe32enc(p, (uint32_t)(u >> 32));
117 4587142
        vbe32enc(p + 4, (uint32_t)(u & 0xffffffffU));
118 4587142
}
119
120
static __inline void
121 6400
vle16enc(void *pp, uint16_t u)
122
{
123 6400
        uint8_t *p = (uint8_t *)pp;
124
125 6400
        p[0] = u & 0xff;
126 6400
        p[1] = (u >> 8) & 0xff;
127 6400
}
128
129
static __inline void
130 3040
vle32enc(void *pp, uint32_t u)
131
{
132 3040
        uint8_t *p = (uint8_t *)pp;
133
134 3040
        p[0] = u & 0xff;
135 3040
        p[1] = (u >> 8) & 0xff;
136 3040
        p[2] = (u >> 16) & 0xff;
137 3040
        p[3] = (u >> 24) & 0xff;
138 3040
}
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