-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsector_tools.h
368 lines (354 loc) · 11.6 KB
/
sector_tools.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*******************************************************************************
*
* Created by Daniel Carrasco at https://www.electrosoftcloud.com
*
* 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/>.
*
******************************************************************************/
//////////////////////////////////////////////////////////////////
//
// Sector Detector class
//
// This class allows to detect the sector type of a CD-ROM sector
// From now is only able to detect the following sectors types:
// * CDDA: If the sector type is unrecognized, will be threated as raw (like CDDA)
// * CDDA_GAP: As above sector type, unrecognized type. The difference is that GAP is zeroed
////////////////////////////////////////////////////////////////////////////////
//
// Sector types
//
// CDDA
// -----------------------------------------------------
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0000h [---DATA...
// ...
// 0920h ...DATA---]
// -----------------------------------------------------
//
// Mode 1
// -----------------------------------------------------
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0000h 00 FF FF FF FF FF FF FF FF FF FF 00 [-MSF -] 01
// 0010h [---DATA...
// ...
// 0800h ...DATA---]
// 0810h [---EDC---] 00 00 00 00 00 00 00 00 [---ECC...
// ...
// 0920h ...ECC---]
// -----------------------------------------------------
//
// Mode 2: This mode is not widely used
// -----------------------------------------------------
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0000h 00 FF FF FF FF FF FF FF FF FF FF 00 [-MSF -] 02
// 0010h [---DATA...
// ...
// 0920h ...DATA---]
// -----------------------------------------------------
//
// Mode 2 (XA), form 1
// -----------------------------------------------------
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0000h 00 FF FF FF FF FF FF FF FF FF FF 00 [-MSF -] 02
// 0010h [--FLAGS--] [--FLAGS--] [---DATA...
// ...
// 0810h ...DATA---] [---EDC---] [---ECC...
// ...
// 0920h ...ECC---]
// -----------------------------------------------------
//
// Mode 2 (XA), form 2
// -----------------------------------------------------
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0000h 00 FF FF FF FF FF FF FF FF FF FF 00 [-MSF -] 02
// 0010h [--FLAGS--] [--FLAGS--] [---DATA...
// ...
// 0920h ...DATA---] [---EDC---]
// -----------------------------------------------------
//
// MSF: Sector address, encoded as minutes:seconds:frames in BCD
// FLAGS: Used in Mode 2 (XA) sectors describing the type of sector; repeated
// twice for redundancy
// DATA: Area of the sector which contains the actual data itself
// EDC: Error Detection Code
// ECC: Error Correction Code
//
// First sector address looks like is always 00:02:00, so we will use this number on it
#include <cstddef>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "compressor.h"
//
// Return codes of the class methods
//
enum sector_tools_returns {
STR_OK = 100,
STR_ERROR_NO_ENOUGH_SECTORS
};
//
// Sector types detectable by the class
//
enum sector_tools_types : uint8_t {
STT_UNKNOWN = 0,
STT_CDDA,
STT_CDDA_GAP,
STT_MODE1,
STT_MODE1_GAP,
STT_MODE1_RAW,
STT_MODE2,
STT_MODE2_GAP,
STT_MODE2_1,
STT_MODE2_1_GAP,
STT_MODE2_2,
STT_MODE2_2_GAP,
STT_MODEX
};
//
// Stream Compressions
//
enum sector_tools_stream_types : uint8_t {
STST_UNKNOWN = 0,
STST_AUDIO,
STST_DATA
};
//
// Optimization options available
//
enum optimization_options : uint8_t {
OO_NONE = 0, // Just copy the input. Surelly will not be used
OO_REMOVE_SYNC = 1, // Remove sync bytes (a.k.a first 12 bytes)
OO_REMOVE_MSF = 1<<1, // Remove the MSF bytes
OO_REMOVE_MODE = 1<<2, // Remove the MODE byte
OO_REMOVE_BLANKS = 1<<3, // If sector type is a GAP, remove the data
OO_REMOVE_REDUNDANT_FLAG = 1<<4, // Remove the redundant copy of FLAG bytes in Mode 2 XA sectors
OO_REMOVE_ECC = 1<<5, // Remove the ECC
OO_REMOVE_EDC = 1<<6, // Remove the EDC
OO_REMOVE_GAP = 1<<7 // If sector type is a GAP, remove the data
};
inline optimization_options operator|(optimization_options a, optimization_options b)
{
return static_cast<optimization_options>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
}
//
// sector_tools Class
//
class sector_tools {
public:
// Public methods
sector_tools();
static uint32_t get32lsb(const uint8_t* src);
static void put32lsb(uint8_t* dest, uint32_t value);
sector_tools_types detect(uint8_t* sector);
static sector_tools_stream_types detect_stream(sector_tools_types type);
uint32_t edc_compute(
uint32_t edc,
const uint8_t* src,
size_t size
);
static int8_t write_type_count(
uint8_t* outBuffer,
sector_tools_types type,
uint32_t count,
uint8_t& generated_bytes
);
static int8_t write_stream_type_count(
uint8_t* outBuffer,
sector_tools_compression compression,
sector_tools_stream_types type,
uint32_t count,
uint8_t& generated_bytes
);
static int8_t read_type_count(
uint8_t* inBuffer,
sector_tools_types& type,
uint32_t& count,
uint8_t& readed_bytes
);
static int8_t read_stream_type_count(
uint8_t* inBuffer,
sector_tools_compression compression,
sector_tools_stream_types& type,
uint32_t& count,
uint8_t& readed_bytes
);
// sector cleaner CDDA
static int8_t clean_sector_cdda(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector cleaner Mode 1
static int8_t clean_sector_mode1(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector cleaner Mode 2
static int8_t clean_sector_mode2(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector cleaner Mode 2 XA 1
static int8_t clean_sector_mode2_xa1(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector cleaner Mode 2 XA 1
static int8_t clean_sector_mode2_xa2(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector cleaner Unknown Mode
static int8_t clean_sector_modex(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector cleaner switcher
static int8_t clean_sector(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t& output_size,
optimization_options options
);
// sector regenerator CDDA
int8_t regenerate_sector_cdda(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t current_pos,
uint16_t& bytes_readed,
optimization_options options
);
// sector regenerator Mode 1
int8_t regenerate_sector_mode1(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t current_pos,
uint16_t& bytes_readed,
optimization_options options
);
// sector regenerator Mode 2
int8_t regenerate_sector_mode2(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t current_pos,
uint16_t& bytes_readed,
optimization_options options
);
// sector regenerator Mode 2 XA 1
int8_t regenerate_sector_mode2_xa1(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t current_pos,
uint16_t& bytes_readed,
optimization_options options
);
// sector regenerator Mode 2 XA 2
int8_t regenerate_sector_mode2_xa2(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t current_pos,
uint16_t& bytes_readed,
optimization_options options
);
// sector regenerator Unknown mode
int8_t regenerate_sector_modex(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint16_t current_pos,
uint16_t& bytes_readed,
optimization_options options
);
int8_t regenerate_sector(
uint8_t* out,
uint8_t* sector,
sector_tools_types type,
uint32_t sector_number,
uint16_t& bytes_readed,
optimization_options options
);
static int8_t encoded_sector_size(
sector_tools_types type,
size_t& output_size,
optimization_options options
);
static void sector_to_time(
uint8_t* out,
uint32_t sector_number
);
// Public attributes
int8_t last_sector_type = -1;
private:
// Private methods
bool is_gap(uint8_t *sector, uint16_t length);
void eccedc_init(void);
int8_t ecc_checkpq(
const uint8_t* address,
const uint8_t* data,
size_t major_count,
size_t minor_count,
size_t major_mult,
size_t minor_inc,
const uint8_t* ecc
);
int8_t ecc_checksector(
const uint8_t *address,
const uint8_t *data,
const uint8_t *ecc
);
void ecc_writepq(
const uint8_t* address,
const uint8_t* data,
size_t major_count,
size_t minor_count,
size_t major_mult,
size_t minor_inc,
uint8_t* ecc
);
void ecc_writesector(
const uint8_t *address,
const uint8_t *data,
uint8_t *ecc
);
// Private attributes
//
const uint8_t zeroaddress[4] = {0, 0, 0, 0};
// LUTs used for computing ECC/EDC
uint8_t ecc_f_lut[256];
uint8_t ecc_b_lut[256];
uint32_t edc_lut [256];
};