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()");
120 TRACE(
"Entering aaruf_ecc_cd_is_suffix_correct(%p, %p)", context, sector);
124 if(context == NULL || sector == NULL)
126 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
133 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
137 if(sector[0x814] != 0x00 ||
139 sector[0x815] != 0x00 || sector[0x816] != 0x00 || sector[0x817] != 0x00 || sector[0x818] != 0x00 ||
140 sector[0x819] != 0x00 || sector[0x81A] != 0x00 || sector[0x81B] != 0x00)
142 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
146 const bool correct_ecc_p =
aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C);
149 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
153 const bool correct_ecc_q =
154 aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC);
157 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
162 memcpy(&stored_edc, sector + 0x810, 4);
165 if(stored_edc != calculated_edc)
167 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = false");
171 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct() = %u == %u", calculated_edc, stored_edc);
172 return calculated_edc == stored_edc;
184 TRACE(
"Entering aaruf_ecc_cd_is_suffix_correct_mode2(%p, %p)", context, sector);
187 uint8_t zeroaddress[4] = {0};
189 if(context == NULL || sector == NULL)
191 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
199 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
203 const int form2 = sector[0x12] & 0x20;
205 const bool correct_ecc_p =
aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C);
208 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
212 const bool correct_ecc_q =
213 aaruf_ecc_cd_check(context, zeroaddress, sector, 52, 43, 86, 88, sector, 0, 0x10, 0x81C + 0xAC);
216 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
221 memcpy(&stored_edc, form2 ? sector + 0x92C : sector + 0x818, 4);
222 const uint32_t calculated_edc =
aaruf_edc_cd_compute(context, 0, sector, form2 ? 0x91C : 0x808, 0x10);
224 TRACE(
"Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculated_edc, stored_edc);
225 return calculated_edc == stored_edc;
245 const uint32_t major_count,
const uint32_t minor_count,
246 const uint32_t major_mult,
const uint32_t minor_inc,
const uint8_t *ecc,
247 const int32_t address_offset,
const int32_t data_offset,
248 const int32_t ecc_offset)
250 TRACE(
"Entering aaruf_ecc_cd_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
251 major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
253 if(context == NULL || address == NULL || data == NULL || ecc == NULL)
255 TRACE(
"Exiting aaruf_ecc_cd_check() with missing data");
263 TRACE(
"Exiting aaruf_ecc_cd_check() without initialized context");
267 uint32_t size = major_count * minor_count;
268 for(uint32_t major = 0; major < major_count; major++)
270 uint32_t idx = (major >> 1) * major_mult + (major & 1);
273 for(uint32_t minor = 0; minor < minor_count; minor++)
275 uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
277 if(idx >= size) idx -= size;
284 if(ecc[major + ecc_offset] != ecc_a || ecc[major + major_count + ecc_offset] != (ecc_a ^ ecc_b))
286 TRACE(
"Exiting aaruf_ecc_cd_check() = false, ECC mismatch at major %u", major);
291 TRACE(
"Exiting aaruf_ecc_cd_check() = true, ECC matches");
311 const uint32_t major_count,
const uint32_t minor_count,
312 const uint32_t major_mult,
const uint32_t minor_inc, uint8_t *ecc,
313 const int32_t address_offset,
const int32_t data_offset,
314 const int32_t ecc_offset)
316 TRACE(
"Entering aaruf_ecc_cd_write(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
317 major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
319 if(context == NULL || address == NULL || data == NULL || ecc == NULL)
321 TRACE(
"Exiting aaruf_ecc_cd_write() with missing data");
329 TRACE(
"Exiting aaruf_ecc_cd_write() without initialized context");
333 uint32_t size = major_count * minor_count;
334 for(uint32_t major = 0; major < major_count; major++)
336 uint32_t idx = (major >> 1) * major_mult + (major & 1);
340 for(uint32_t minor = 0; minor < minor_count; minor++)
342 uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
344 if(idx >= size) idx -= size;
351 ecc[major + ecc_offset] = ecc_a;
352 ecc[major + major_count + ecc_offset] = ecc_a ^ ecc_b;
355 TRACE(
"Exiting aaruf_ecc_cd_write()");
370 uint8_t *ecc,
const int32_t address_offset,
371 const int32_t data_offset,
const int32_t ecc_offset)
373 TRACE(
"Entering aaruf_ecc_cd_write_sector(%p, %p, %p, %p, %d, %d, %d)", context, address, data, ecc, address_offset,
374 data_offset, ecc_offset);
376 aaruf_ecc_cd_write(context, address, data, 86, 24, 2, 86, ecc, address_offset, data_offset, ecc_offset);
377 aaruf_ecc_cd_write(context, address, data, 52, 43, 86, 88, ecc, address_offset, data_offset,
380 TRACE(
"Exiting aaruf_ecc_cd_write_sector()");
393 TRACE(
"Entering aaruf_cd_lba_to_msf(%lld, %p, %p, %p)", pos, minute, second, frame);
395 *minute = (uint8_t)((pos + 150) / 75 / 60);
396 *second = (uint8_t)((pos + 150) / 75 % 60);
397 *frame = (uint8_t)((pos + 150) % 75);
399 TRACE(
"Exiting aaruf_cd_lba_to_msf() = %u:%u:%u", *minute, *second, *frame);
411 TRACE(
"Entering aaruf_cd_reconstruct_prefix(%p, %u, %lld)", sector, type, lba);
413 uint8_t minute, second, frame;
415 if(sector == NULL)
return;
420 sector[0x000] = 0x00;
421 sector[0x001] = 0xFF;
422 sector[0x002] = 0xFF;
423 sector[0x003] = 0xFF;
424 sector[0x004] = 0xFF;
425 sector[0x005] = 0xFF;
426 sector[0x006] = 0xFF;
427 sector[0x007] = 0xFF;
428 sector[0x008] = 0xFF;
429 sector[0x009] = 0xFF;
430 sector[0x00A] = 0xFF;
431 sector[0x00B] = 0x00;
435 sector[0x00C] = (uint8_t)(((minute / 10) << 4) + minute % 10);
436 sector[0x00D] = (uint8_t)(((second / 10) << 4) + second % 10);
437 sector[0x00E] = (uint8_t)(((frame / 10) << 4) + frame % 10);
445 sector[0x00F] = 0x01;
453 sector[0x00F] = 0x02;
457 sector[0x010] = sector[0x014];
458 sector[0x011] = sector[0x015];
459 sector[0x012] = sector[0x016];
460 sector[0x013] = sector[0x017];
466 TRACE(
"Exiting aaruf_ecc_cd_reconstruct_prefix()");
478 TRACE(
"Entering aaruf_ecc_cd_reconstruct(%p, %p, %u)", context, sector, type);
480 uint32_t computed_edc;
481 uint8_t zeroaddress[4];
483 if(context == NULL || sector == NULL)
485 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() with missing data");
493 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() without initialized context");
504 memcpy(sector + 0x810, &computed_edc, 4);
508 memcpy(sector + 0x818, &computed_edc, 4);
512 memcpy(sector + 0x92C, &computed_edc, 4);
515 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
519 memset(&zeroaddress, 0, 4);
530 sector[0x814] = 0x00;
531 sector[0x815] = 0x00;
532 sector[0x816] = 0x00;
533 sector[0x817] = 0x00;
534 sector[0x818] = 0x00;
535 sector[0x819] = 0x00;
536 sector[0x81A] = 0x00;
537 sector[0x81B] = 0x00;
544 TRACE(
"Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
551 TRACE(
"Exiting aaruf_ecc_cd_reconstruct()");
566 TRACE(
"Entering aaruf_edc_cd_compute(%p, %u, %p, %d, %d)", context, edc, src, size, pos);
568 if(context == NULL || src == NULL)
570 TRACE(
"Exiting aaruf_edc_cd_compute() with missing data");
578 TRACE(
"Exiting aaruf_edc_cd_compute() without initialized context");
582 for(; size > 0; size--) edc = edc >> 8 ^ ctx->
edc_table[(edc ^ src[pos++]) & 0xFF];
584 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.
void aaruf_ecc_cd_free(void *ctx)
Frees a Compact Disc ECC context and its internal tables.
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).