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