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