File Coverage

src/sysendian.h
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine n/a
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             /*-
2             * Copyright 2007-2009 Colin Percival
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 AUTHOR 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 AUTHOR 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             * This file was originally written by Colin Percival as part of the Tarsnap
27             * online backup system.
28             */
29             #ifndef _SYSENDIAN_H_
30             #define _SYSENDIAN_H_
31              
32             #include "scrypt_platform.h"
33              
34             /* If we don't have be64enc, the we have isn't usable. */
35             #if !HAVE_DECL_BE64ENC
36             #undef HAVE_SYS_ENDIAN_H
37             #endif
38              
39             #ifdef HAVE_SYS_ENDIAN_H
40              
41             #include
42              
43             #else
44              
45             #ifdef _MSC_VER
46             #include "msinttypes.h"
47             #elif defined(__sun) || defined(__sun__)
48             #include
49             #else
50             #include
51             #endif
52              
53             static __inline uint32_t
54             be32dec(const void *pp)
55             {
56             const uint8_t *p = (uint8_t const *)pp;
57              
58 39104           return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
59 39104           ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
60             }
61              
62             static __inline void
63             be32enc(void *pp, uint32_t x)
64             {
65             uint8_t * p = (uint8_t *)pp;
66              
67 19971           p[3] = x & 0xff;
68 19971           p[2] = (x >> 8) & 0xff;
69 19971           p[1] = (x >> 16) & 0xff;
70 19971           p[0] = (x >> 24) & 0xff;
71             }
72              
73             static __inline uint64_t
74             be64dec(const void *pp)
75             {
76             const uint8_t *p = (uint8_t const *)pp;
77              
78             return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
79             ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
80             ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
81             ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
82             }
83              
84             static __inline void
85             be64enc(void *pp, uint64_t x)
86             {
87             uint8_t * p = (uint8_t *)pp;
88              
89             p[7] = (uint8_t)(x & 0xff);
90             p[6] = (uint8_t)((x >> 8) & 0xff);
91             p[5] = (uint8_t)((x >> 16) & 0xff);
92             p[4] = (uint8_t)((x >> 24) & 0xff);
93             p[3] = (uint8_t)((x >> 32) & 0xff);
94             p[2] = (uint8_t)((x >> 40) & 0xff);
95             p[1] = (uint8_t)((x >> 48) & 0xff);
96             p[0] = (uint8_t)((x >> 56) & 0xff);
97             }
98              
99             static __inline uint32_t
100             le32dec(const void *pp)
101             {
102             const uint8_t *p = (uint8_t const *)pp;
103              
104 7392           return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
105 7392           ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
106             }
107              
108             static __inline void
109             le32enc(void *pp, uint32_t x)
110             {
111             uint8_t * p = (uint8_t *)pp;
112              
113 7392           p[0] = (uint8_t)(x & 0xff);
114 7392           p[1] = (uint8_t)((x >> 8) & 0xff);
115 7392           p[2] = (uint8_t)((x >> 16) & 0xff);
116 7392           p[3] = (uint8_t)((x >> 24) & 0xff);
117             }
118              
119             static __inline uint64_t
120             le64dec(const void *pp)
121             {
122             const uint8_t *p = (uint8_t const *)pp;
123              
124             return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
125             ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
126             ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
127             ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
128             }
129              
130             static __inline void
131             le64enc(void *pp, uint64_t x)
132             {
133             uint8_t * p = (uint8_t *)pp;
134              
135             p[0] = (uint8_t)(x & 0xff);
136             p[1] = (uint8_t)((x >> 8) & 0xff);
137             p[2] = (uint8_t)((x >> 16) & 0xff);
138             p[3] = (uint8_t)((x >> 24) & 0xff);
139             p[4] = (uint8_t)((x >> 32) & 0xff);
140             p[5] = (uint8_t)((x >> 40) & 0xff);
141             p[6] = (uint8_t)((x >> 48) & 0xff);
142             p[7] = (uint8_t)((x >> 56) & 0xff);
143             }
144             #endif /* !HAVE_SYS_ENDIAN_H */
145              
146             #endif /* !_SYSENDIAN_H_ */