-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpfs_utils.h
138 lines (118 loc) · 3.88 KB
/
vpfs_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
VPFS - Vita PKG File System
Copyright © 2018 VitaSmith
Copyright © 2017-2018 Martins Mozeiko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#if defined(_MSC_VER)
# define NORETURN __declspec(noreturn)
# define PKG_ALIGN(x) __declspec(align(x))
#else
# define NORETURN __attribute__((noreturn))
# define PKG_ALIGN(x) __attribute__((aligned(x)))
#endif
static inline uint32_t min32(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
static inline uint64_t min64(uint64_t a, uint64_t b)
{
return a < b ? a : b;
}
static inline uint16_t get16le(const uint8_t* bytes)
{
return (bytes[0]) | (bytes[1] << 8);
}
static inline uint32_t get32le(const uint8_t* bytes)
{
return (bytes[0]) | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
}
static inline uint64_t get64le(const uint8_t* bytes)
{
return (uint64_t)bytes[0]
| ((uint64_t)bytes[1] << 8)
| ((uint64_t)bytes[2] << 16)
| ((uint64_t)bytes[3] << 24)
| ((uint64_t)bytes[4] << 32)
| ((uint64_t)bytes[5] << 40)
| ((uint64_t)bytes[6] << 48)
| ((uint64_t)bytes[7] << 56);
}
static inline uint16_t get16be(const uint8_t* bytes)
{
return (bytes[1]) | (bytes[0] << 8);
}
static inline uint32_t get32be(const uint8_t* bytes)
{
return (bytes[3]) | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24);
}
static inline uint64_t get64be(const uint8_t* bytes)
{
return (uint64_t)bytes[7]
| ((uint64_t)bytes[6] << 8)
| ((uint64_t)bytes[5] << 16)
| ((uint64_t)bytes[4] << 24)
| ((uint64_t)bytes[3] << 32)
| ((uint64_t)bytes[2] << 40)
| ((uint64_t)bytes[1] << 48)
| ((uint64_t)bytes[0] << 56);
}
static inline void set16le(void* bytes, uint16_t x)
{
((uint8_t*)bytes)[0] = (uint8_t)x;
((uint8_t*)bytes)[1] = (uint8_t)(x >> 8);
}
static inline void set32le(void* bytes, uint32_t x)
{
((uint8_t*)bytes)[0] = (uint8_t)x;
((uint8_t*)bytes)[1] = (uint8_t)(x >> 8);
((uint8_t*)bytes)[2] = (uint8_t)(x >> 16);
((uint8_t*)bytes)[3] = (uint8_t)(x >> 24);
}
static inline void set64le(void* bytes, uint64_t x)
{
((uint8_t*)bytes)[0] = (uint8_t)x;
((uint8_t*)bytes)[1] = (uint8_t)(x >> 8);
((uint8_t*)bytes)[2] = (uint8_t)(x >> 16);
((uint8_t*)bytes)[3] = (uint8_t)(x >> 24);
((uint8_t*)bytes)[4] = (uint8_t)(x >> 32);
((uint8_t*)bytes)[5] = (uint8_t)(x >> 40);
((uint8_t*)bytes)[6] = (uint8_t)(x >> 48);
((uint8_t*)bytes)[7] = (uint8_t)(x >> 56);
}
static inline void set16be(void* bytes, uint16_t x)
{
((uint8_t*)bytes)[0] = (uint8_t)(x >> 8);
((uint8_t*)bytes)[1] = (uint8_t)x;
}
static inline void set32be(void* bytes, uint32_t x)
{
((uint8_t*)bytes)[0] = (uint8_t)(x >> 24);
((uint8_t*)bytes)[1] = (uint8_t)(x >> 16);
((uint8_t*)bytes)[2] = (uint8_t)(x >> 8);
((uint8_t*)bytes)[3] = (uint8_t)x;
}
static inline void set64be(void* bytes, uint64_t x)
{
((uint8_t*)bytes)[0] = (uint8_t)(x >> 56);
((uint8_t*)bytes)[1] = (uint8_t)(x >> 48);
((uint8_t*)bytes)[2] = (uint8_t)(x >> 40);
((uint8_t*)bytes)[3] = (uint8_t)(x >> 32);
((uint8_t*)bytes)[4] = (uint8_t)(x >> 24);
((uint8_t*)bytes)[5] = (uint8_t)(x >> 16);
((uint8_t*)bytes)[6] = (uint8_t)(x >> 8);
((uint8_t*)bytes)[7] = (uint8_t)x;
}