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