-
Notifications
You must be signed in to change notification settings - Fork 16
/
numconv.h
173 lines (146 loc) · 3.95 KB
/
numconv.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright (C) 1997-2008 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
http://www.zsnes.com
http://sourceforge.net/projects/zsnes
https://zsnes.bountysource.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef NUMCONV_H
#define NUMCONV_H
#include <stdio.h>
// Get correct mask for particular bit
#define BIT(X) (1 << (X))
/*
Functions that the compiler should inline that will convert
uint32, uint24, uint16 to 4 byte, 3 byte, 2 byte arrays
and back. -Nach
*/
#if !defined(NUMCONV_BT16) && defined(NUMCONV_FR2)
#define NUMCONV_BT16
#endif
#if !defined(NUMCONV_BT24) && defined(NUMCONV_FR3)
#define NUMCONV_BT24
#endif
#if !defined(NUMCONV_BT32) && defined(NUMCONV_FR4)
#define NUMCONV_BT32
#endif
#if !defined(NUMCONV_16TB) && defined(NUMCONV_FW2)
#define NUMCONV_16TB
#endif
#if !defined(NUMCONV_24TB) && defined(NUMCONV_FW3)
#define NUMCONV_24TB
#endif
#if !defined(NUMCONV_32TB) && defined(NUMCONV_FW4)
#define NUMCONV_32TB
#endif
#ifdef NUMCONV_32TB
static unsigned char* uint32_to_bytes(unsigned int num)
{
static unsigned char buffer[4];
buffer[3] = (num >> 24) & 0xFF;
buffer[2] = (num >> 16) & 0xFF;
buffer[1] = (num >> 8) & 0xFF;
buffer[0] = num & 0xFF;
return (buffer);
}
#endif
#ifdef NUMCONV_BT32
static unsigned int bytes_to_uint32(const unsigned char buffer[4])
{
unsigned int num = (unsigned int)buffer[0];
num |= ((unsigned int)buffer[1]) << 8;
num |= ((unsigned int)buffer[2]) << 16;
num |= ((unsigned int)buffer[3]) << 24;
return (num);
}
#endif
#ifdef NUMCONV_24TB
static unsigned char* uint24_to_bytes(unsigned int num)
{
static unsigned char buffer[3];
buffer[2] = (num >> 16) & 0xFF;
buffer[1] = (num >> 8) & 0xFF;
buffer[0] = num & 0xFF;
return (buffer);
}
#endif
#ifdef NUMCONV_BT24
static unsigned int bytes_to_uint24(const unsigned char buffer[3])
{
unsigned int num = (unsigned int)buffer[0];
num |= ((unsigned int)buffer[1]) << 8;
num |= ((unsigned int)buffer[2]) << 16;
return (num);
}
#endif
#ifdef NUMCONV_16TB
static unsigned char* uint16_to_bytes(unsigned short num)
{
static unsigned char buffer[2];
buffer[1] = (num >> 8) & 0xFF;
buffer[0] = num & 0xFF;
return (buffer);
}
#endif
#ifdef NUMCONV_BT16
static unsigned short bytes_to_uint16(const unsigned char buffer[2])
{
unsigned short num = (unsigned short)buffer[0];
num |= ((unsigned short)buffer[1]) << 8;
return (num);
}
#endif
// Functions to read 2, 3, 4 bytes and convert to uint16, uint24, uint32
#ifdef NUMCONV_FR2
static unsigned short fread2(FILE* fp)
{
unsigned char uint16buf[2];
fread(uint16buf, 2, 1, fp);
return (bytes_to_uint16(uint16buf));
}
#endif
#ifdef NUMCONV_FR3
static unsigned int fread3(FILE* fp)
{
unsigned char uint24buf[3];
fread(uint24buf, 3, 1, fp);
return (bytes_to_uint24(uint24buf));
}
#endif
#ifdef NUMCONV_FR4
static unsigned int fread4(FILE* fp)
{
unsigned char uint32buf[4];
fread(uint32buf, 4, 1, fp);
return (bytes_to_uint32(uint32buf));
}
#endif
// Functions to write uint16, uint24, uint32 as 2, 3, 4 bytes
#ifdef NUMCONV_FW2
static void fwrite2(unsigned short var, FILE* fp)
{
fwrite(uint16_to_bytes(var), 2, 1, fp);
}
#endif
#ifdef NUMCONV_FW3
static void fwrite3(unsigned int var, FILE* fp)
{
fwrite(uint24_to_bytes(var), 3, 1, fp);
}
#endif
#ifdef NUMCONV_FW4
static void fwrite4(unsigned int var, FILE* fp)
{
fwrite(uint32_to_bytes(var), 4, 1, fp);
}
#endif
#endif