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