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