37 TRACE(
"Entering aaruf_ecc_cd_init()");
39 uint32_t edc = 0, i = 0, j = 0;
41 TRACE(
"Creating context");
44 if(context == NULL)
return NULL;
46 TRACE(
"Allocating memory for ECC F table");
47 context->
ecc_f_table = (uint8_t *)malloc(
sizeof(uint8_t) * 256);
55 TRACE(
"Allocating memory for ECC B table");
56 context->
ecc_b_table = (uint8_t *)malloc(
sizeof(uint8_t) * 256);
65 TRACE(
"Allocating memory for EDC table");
67 context->
edc_table = (uint32_t *)malloc(
sizeof(uint32_t) * 256);
77 TRACE(
"Initializing EDC tables");
78 for(i = 0; i < 256; i++)
81 j = i << 1 ^ ((i & 0x80) == 0x80 ? 0x11D : 0);
84 for(j = 0; j < 8; j++) edc = edc >> 1 ^ ((edc & 1) > 0 ? 0xD8018001 : 0);
90 TRACE(
"Exiting aaruf_ecc_cd_init()");
103 TRACE(
"Entering aaruf_ecc_cd_is_suffix_correct(%p, %p)", context, sector);
107 if(context == NULL || sector == NULL)
109 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
116 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
120 if(sector[0x814] != 0x00 ||
122 sector[0x815] != 0x00 || sector[0x816] != 0x00 || sector[0x817] != 0x00 || sector[0x818] != 0x00 ||
123 sector[0x819] != 0x00 || sector[0x81A] != 0x00 || sector[0x81B] != 0x00)
125 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
129 const bool correct_ecc_p =
aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C);
132 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
136 const bool correct_ecc_q =
137 aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC);
140 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
145 memcpy(&stored_edc, sector + 0x810, 4);
148 if(stored_edc != calculated_edc)
150 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
154 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = %u == %u", calculated_edc, stored_edc);
155 return calculated_edc == stored_edc;
167 TRACE(
"Entering aaruf_ecc_cd_is_suffix_correct_mode2(%p, %p)", context, sector);
170 uint8_t zeroaddress[4] = {0};
172 if(context == NULL || sector == NULL)
174 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
182 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
186 const int form2 = sector[0x12] & 0x20;
188 const bool correct_ecc_p =
aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C);
191 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
195 const bool correct_ecc_q =
196 aaruf_ecc_cd_check(context, zeroaddress, sector, 52, 43, 86, 88, sector, 0, 0x10, 0x81C + 0xAC);
199 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
204 memcpy(&stored_edc, form2 ? sector + 0x92C : sector + 0x818, 4);
205 const uint32_t calculated_edc =
aaruf_edc_cd_compute(context, 0, sector, form2 ? 0x91C : 0x808, 0x10);
207 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculated_edc, stored_edc);
208 return calculated_edc == stored_edc;
228 const uint32_t major_count,
const uint32_t minor_count,
229 const uint32_t major_mult,
const uint32_t minor_inc,
const uint8_t *ecc,
230 const int32_t address_offset,
const int32_t data_offset,
231 const int32_t ecc_offset)
233 TRACE(
"Entering aaruf_ecc_cd_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
234 major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
236 if(context == NULL || address == NULL || data == NULL || ecc == NULL)
238 TRACE(
"Exiting aaruf_ecc_cd_check() with missing data");
246 TRACE(
"Exiting aaruf_ecc_cd_check() without initialized context");
250 uint32_t size = major_count * minor_count;
251 for(uint32_t major = 0; major < major_count; major++)
253 uint32_t idx = (major >> 1) * major_mult + (major & 1);
256 for(uint32_t minor = 0; minor < minor_count; minor++)
258 uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
260 if(idx >= size) idx -= size;
267 if(ecc[major + ecc_offset] != ecc_a || ecc[major + major_count + ecc_offset] != (ecc_a ^ ecc_b))
269 TRACE(
"Exiting aaruf_ecc_cd_check() = false, ECC mismatch at major %u", major);
274 TRACE(
"Exiting aaruf_ecc_cd_check() = true, ECC matches");
294 const uint32_t major_count,
const uint32_t minor_count,
295 const uint32_t major_mult,
const uint32_t minor_inc, uint8_t *ecc,
296 const int32_t address_offset,
const int32_t data_offset,
297 const int32_t ecc_offset)
299 TRACE(
"Entering aaruf_ecc_cd_write(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
300 major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
302 if(context == NULL || address == NULL || data == NULL || ecc == NULL)
304 TRACE(
"Exiting aaruf_ecc_cd_write() with missing data");
312 TRACE(
"Exiting aaruf_ecc_cd_write() without initialized context");
316 uint32_t size = major_count * minor_count;
317 for(uint32_t major = 0; major < major_count; major++)
319 uint32_t idx = (major >> 1) * major_mult + (major & 1);
323 for(uint32_t minor = 0; minor < minor_count; minor++)
325 uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
327 if(idx >= size) idx -= size;
334 ecc[major + ecc_offset] = ecc_a;
335 ecc[major + major_count + ecc_offset] = ecc_a ^ ecc_b;
338 TRACE(
"Exiting aaruf_ecc_cd_write()");
353 uint8_t *ecc,
const int32_t address_offset,
354 const int32_t data_offset,
const int32_t ecc_offset)
356 TRACE(
"Entering aaruf_ecc_cd_write_sector(%p, %p, %p, %p, %d, %d, %d)", context, address, data, ecc, address_offset,
357 data_offset, ecc_offset);
359 aaruf_ecc_cd_write(context, address, data, 86, 24, 2, 86, ecc, address_offset, data_offset, ecc_offset);
360 aaruf_ecc_cd_write(context, address, data, 52, 43, 86, 88, ecc, address_offset, data_offset,
363 TRACE(
"Exiting aaruf_ecc_cd_write_sector()");
376 TRACE(
"Entering aaruf_cd_lba_to_msf(%lld, %p, %p, %p)", pos, minute, second, frame);
378 *minute = (uint8_t)((pos + 150) / 75 / 60);
379 *second = (uint8_t)((pos + 150) / 75 % 60);
380 *frame = (uint8_t)((pos + 150) % 75);
382 TRACE(
"Exiting aaruf_cd_lba_to_msf() = %u:%u:%u", *minute, *second, *frame);
394 TRACE(
"Entering aaruf_cd_reconstruct_prefix(%p, %u, %lld)", sector, type, lba);
396 uint8_t minute, second, frame;
398 if(sector == NULL)
return;
403 sector[0x000] = 0x00;
404 sector[0x001] = 0xFF;
405 sector[0x002] = 0xFF;
406 sector[0x003] = 0xFF;
407 sector[0x004] = 0xFF;
408 sector[0x005] = 0xFF;
409 sector[0x006] = 0xFF;
410 sector[0x007] = 0xFF;
411 sector[0x008] = 0xFF;
412 sector[0x009] = 0xFF;
413 sector[0x00A] = 0xFF;
414 sector[0x00B] = 0x00;
418 sector[0x00C] = (uint8_t)(((minute / 10) << 4) + minute % 10);
419 sector[0x00D] = (uint8_t)(((second / 10) << 4) + second % 10);
420 sector[0x00E] = (uint8_t)(((frame / 10) << 4) + frame % 10);
428 sector[0x00F] = 0x01;
436 sector[0x00F] = 0x02;
440 sector[0x010] = sector[0x014];
441 sector[0x011] = sector[0x015];
442 sector[0x012] = sector[0x016];
443 sector[0x013] = sector[0x017];
449 TRACE(
"Exiting aaruf_ecc_cd_reconstruct_prefix()");
461 TRACE(
"Entering aaruf_ecc_cd_reconstruct(%p, %p, %u)", context, sector, type);
463 uint32_t computed_edc;
464 uint8_t zeroaddress[4];
466 if(context == NULL || sector == NULL)
468 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() with missing data");
476 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() without initialized context");
487 memcpy(sector + 0x810, &computed_edc, 4);
491 memcpy(sector + 0x818, &computed_edc, 4);
495 memcpy(sector + 0x92C, &computed_edc, 4);
498 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
502 memset(&zeroaddress, 0, 4);
513 sector[0x814] = 0x00;
514 sector[0x815] = 0x00;
515 sector[0x816] = 0x00;
516 sector[0x817] = 0x00;
517 sector[0x818] = 0x00;
518 sector[0x819] = 0x00;
519 sector[0x81A] = 0x00;
520 sector[0x81B] = 0x00;
527 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
534 TRACE(
"Exiting aaruf_ecc_cd_reconstruct()");
549 TRACE(
"Entering aaruf_edc_cd_compute(%p, %u, %p, %d, %d)", context, edc, src, size, pos);
551 if(context == NULL || src == NULL)
553 TRACE(
"Exiting aaruf_edc_cd_compute() with missing data");
561 TRACE(
"Exiting aaruf_edc_cd_compute() without initialized context");
565 for(; size > 0; size--) edc = edc >> 8 ^ ctx->
edc_table[(edc ^ src[pos++]) & 0xFF];
567 TRACE(
"Exiting aaruf_edc_cd_compute() = 0x%08X", edc);
bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *data, const uint32_t major_count, const uint32_t minor_count, const uint32_t major_mult, const uint32_t minor_inc, const uint8_t *ecc, const int32_t address_offset, const int32_t data_offset, const int32_t ecc_offset)
Checks the ECC of a CD sector.
void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *data, const uint32_t major_count, const uint32_t minor_count, const uint32_t major_mult, const uint32_t minor_inc, uint8_t *ecc, const int32_t address_offset, const int32_t data_offset, const int32_t ecc_offset)
Writes ECC for a CD sector.
void aaruf_cd_lba_to_msf(const int64_t pos, uint8_t *minute, uint8_t *second, uint8_t *frame)
Converts a CD LBA (Logical Block Address) to MSF (Minute:Second:Frame) format.
void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, const uint8_t type)
Reconstructs the EDC and ECC fields of a CD sector.
uint32_t aaruf_edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, int size, int pos)
Computes the EDC (Error Detection Code) for a CD sector.
void aaruf_ecc_cd_reconstruct_prefix(uint8_t *sector, const uint8_t type, const int64_t lba)
Reconstructs the prefix (sync, address, mode) of a CD sector.
void aaruf_ecc_cd_write_sector(void *context, const uint8_t *address, const uint8_t *data, uint8_t *ecc, const int32_t address_offset, const int32_t data_offset, const int32_t ecc_offset)
Writes ECC for a full CD sector (both P and Q ECC).
bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
Checks if the suffix (EDC/ECC) of a CD sector is correct (Mode 2).
void * aaruf_ecc_cd_init()
Initializes a Compact Disc ECC context.
bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
Checks if the suffix (EDC/ECC) of a CD sector is correct (Mode 1).
TrackType
Track (partitioning element) types for optical media.
@ CdMode1
Compact Disc Mode 1 data track.
@ CdMode2Form2
Compact Disc Mode 2 Form 2 data track.
@ CdMode2Form1
Compact Disc Mode 2 Form 1 data track.
@ CdMode2Formless
Compact Disc Mode 2 (formless) data track.
Lookup tables and state for Compact Disc EDC/ECC (P/Q) regeneration / verification.
uint8_t * ecc_f_table
Forward (F) ECC table.
bool inited_edc
True once EDC/ECC tables have been initialized.
uint32_t * edc_table
EDC (CRC) lookup table.
uint8_t * ecc_b_table
Backward (B) ECC table (allocated, size implementation-defined).