libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
ecc_cd.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 * ECC algorithm from ECM(c) 2002-2011 Neill Corlett
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdbool.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "aaruformat.h"
26#include "log.h"
27
36{
37 TRACE("Entering aaruf_ecc_cd_init()");
38 CdEccContext *context = NULL;
39 uint32_t edc = 0, i = 0, j = 0;
40
41 TRACE("Creating context");
42 context = (CdEccContext *)malloc(sizeof(CdEccContext));
43
44 if(context == NULL) return NULL;
45
46 TRACE("Allocating memory for ECC F table");
47 context->ecc_f_table = (uint8_t *)malloc(sizeof(uint8_t) * 256);
48
49 if(context->ecc_f_table == NULL)
50 {
51 free(context);
52 return NULL;
53 }
54
55 TRACE("Allocating memory for ECC B table");
56 context->ecc_b_table = (uint8_t *)malloc(sizeof(uint8_t) * 256);
57
58 if(context->ecc_b_table == NULL)
59 {
60 free(context->ecc_f_table);
61 free(context);
62 return NULL;
63 }
64
65 TRACE("Allocating memory for EDC table");
66
67 context->edc_table = (uint32_t *)malloc(sizeof(uint32_t) * 256);
68
69 if(context->edc_table == NULL)
70 {
71 free(context->ecc_f_table);
72 free(context->ecc_b_table);
73 free(context);
74 return NULL;
75 }
76
77 TRACE("Initializing EDC tables");
78 for(i = 0; i < 256; i++)
79 {
80 edc = i;
81 j = i << 1 ^ ((i & 0x80) == 0x80 ? 0x11D : 0);
82 context->ecc_f_table[i] = (uint8_t)j;
83 context->ecc_b_table[i ^ j] = (uint8_t)i;
84 for(j = 0; j < 8; j++) edc = edc >> 1 ^ ((edc & 1) > 0 ? 0xD8018001 : 0);
85 context->edc_table[i] = edc;
86 }
87
88 context->inited_edc = true;
89
90 TRACE("Exiting aaruf_ecc_cd_init()");
91 return context;
92}
93
100{
101 if(!ctx) return;
102
103 CdEccContext *context = (CdEccContext *)ctx;
104
105 free(context->ecc_f_table);
106 free(context->ecc_b_table);
107 free(context->edc_table);
108 free(context);
109}
110
118AARU_EXPORT bool AARU_CALL aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
119{
120 TRACE("Entering aaruf_ecc_cd_is_suffix_correct(%p, %p)", context, sector);
121 uint32_t edc;
122 int size, pos;
123
124 if(context == NULL || sector == NULL)
125 {
126 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
127 return false;
128 }
129
130 const CdEccContext *ctx = context;
131 if(!ctx->inited_edc)
132 {
133 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
134 return false;
135 }
136
137 if(sector[0x814] != 0x00 ||
138 // reserved (8 bytes)
139 sector[0x815] != 0x00 || sector[0x816] != 0x00 || sector[0x817] != 0x00 || sector[0x818] != 0x00 ||
140 sector[0x819] != 0x00 || sector[0x81A] != 0x00 || sector[0x81B] != 0x00)
141 {
142 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
143 return false;
144 }
145
146 const bool correct_ecc_p = aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C);
147 if(!correct_ecc_p)
148 {
149 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
150 return false;
151 }
152
153 const bool correct_ecc_q =
154 aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC);
155 if(!correct_ecc_q)
156 {
157 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
158 return false;
159 }
160
161 uint32_t stored_edc;
162 memcpy(&stored_edc, sector + 0x810, 4);
163 uint32_t calculated_edc = aaruf_edc_cd_compute(context, 0, sector, 0x810, 0);
164
165 if(stored_edc != calculated_edc)
166 {
167 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
168 return false;
169 }
170
171 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = %u == %u", calculated_edc, stored_edc);
172 return calculated_edc == stored_edc;
173}
174
182AARU_EXPORT bool AARU_CALL aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
183{
184 TRACE("Entering aaruf_ecc_cd_is_suffix_correct_mode2(%p, %p)", context, sector);
185 uint32_t edc;
186 int size, pos;
187 uint8_t zeroaddress[4] = {0};
188
189 if(context == NULL || sector == NULL)
190 {
191 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
192 return false;
193 }
194
195 CdEccContext *ctx = context;
196
197 if(!ctx->inited_edc)
198 {
199 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
200 return false;
201 }
202
203 const int form2 = sector[0x12] & 0x20;
204
205 const bool correct_ecc_p = aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C);
206 if(!correct_ecc_p)
207 {
208 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
209 return false;
210 }
211
212 const bool correct_ecc_q =
213 aaruf_ecc_cd_check(context, zeroaddress, sector, 52, 43, 86, 88, sector, 0, 0x10, 0x81C + 0xAC);
214 if(!correct_ecc_q)
215 {
216 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
217 return false;
218 }
219
220 uint32_t stored_edc;
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);
223
224 TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculated_edc, stored_edc);
225 return calculated_edc == stored_edc;
226}
227
244AARU_EXPORT bool AARU_CALL aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *data,
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)
249{
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);
252
253 if(context == NULL || address == NULL || data == NULL || ecc == NULL)
254 {
255 TRACE("Exiting aaruf_ecc_cd_check() with missing data");
256 return false;
257 }
258
259 CdEccContext *ctx = context;
260
261 if(!ctx->inited_edc)
262 {
263 TRACE("Exiting aaruf_ecc_cd_check() without initialized context");
264 return false;
265 }
266
267 uint32_t size = major_count * minor_count;
268 for(uint32_t major = 0; major < major_count; major++)
269 {
270 uint32_t idx = (major >> 1) * major_mult + (major & 1);
271 uint8_t ecc_a = 0;
272 uint8_t ecc_b = 0;
273 for(uint32_t minor = 0; minor < minor_count; minor++)
274 {
275 uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
276 idx += minor_inc;
277 if(idx >= size) idx -= size;
278 ecc_a ^= temp;
279 ecc_b ^= temp;
280 ecc_a = ctx->ecc_f_table[ecc_a];
281 }
282
283 ecc_a = ctx->ecc_b_table[ctx->ecc_f_table[ecc_a] ^ ecc_b];
284 if(ecc[major + ecc_offset] != ecc_a || ecc[major + major_count + ecc_offset] != (ecc_a ^ ecc_b))
285 {
286 TRACE("Exiting aaruf_ecc_cd_check() = false, ECC mismatch at major %u", major);
287 return false;
288 }
289 }
290
291 TRACE("Exiting aaruf_ecc_cd_check() = true, ECC matches");
292 return true;
293}
294
310AARU_EXPORT void AARU_CALL aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *data,
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)
315{
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);
318
319 if(context == NULL || address == NULL || data == NULL || ecc == NULL)
320 {
321 TRACE("Exiting aaruf_ecc_cd_write() with missing data");
322 return;
323 }
324
325 CdEccContext *ctx = context;
326
327 if(!ctx->inited_edc)
328 {
329 TRACE("Exiting aaruf_ecc_cd_write() without initialized context");
330 return;
331 }
332
333 uint32_t size = major_count * minor_count;
334 for(uint32_t major = 0; major < major_count; major++)
335 {
336 uint32_t idx = (major >> 1) * major_mult + (major & 1);
337 uint8_t ecc_a = 0;
338 uint8_t ecc_b = 0;
339
340 for(uint32_t minor = 0; minor < minor_count; minor++)
341 {
342 uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
343 idx += minor_inc;
344 if(idx >= size) idx -= size;
345 ecc_a ^= temp;
346 ecc_b ^= temp;
347 ecc_a = ctx->ecc_f_table[ecc_a];
348 }
349
350 ecc_a = ctx->ecc_b_table[ctx->ecc_f_table[ecc_a] ^ ecc_b];
351 ecc[major + ecc_offset] = ecc_a;
352 ecc[major + major_count + ecc_offset] = ecc_a ^ ecc_b;
353 }
354
355 TRACE("Exiting aaruf_ecc_cd_write()");
356}
357
369AARU_EXPORT void AARU_CALL aaruf_ecc_cd_write_sector(void *context, const uint8_t *address, const uint8_t *data,
370 uint8_t *ecc, const int32_t address_offset,
371 const int32_t data_offset, const int32_t ecc_offset)
372{
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);
375
376 aaruf_ecc_cd_write(context, address, data, 86, 24, 2, 86, ecc, address_offset, data_offset, ecc_offset); // P
377 aaruf_ecc_cd_write(context, address, data, 52, 43, 86, 88, ecc, address_offset, data_offset,
378 ecc_offset + 0xAC); // Q
379
380 TRACE("Exiting aaruf_ecc_cd_write_sector()");
381}
382
391AARU_LOCAL void AARU_CALL aaruf_cd_lba_to_msf(const int64_t pos, uint8_t *minute, uint8_t *second, uint8_t *frame)
392{
393 TRACE("Entering aaruf_cd_lba_to_msf(%lld, %p, %p, %p)", pos, minute, second, frame);
394
395 *minute = (uint8_t)((pos + 150) / 75 / 60);
396 *second = (uint8_t)((pos + 150) / 75 % 60);
397 *frame = (uint8_t)((pos + 150) % 75);
398
399 TRACE("Exiting aaruf_cd_lba_to_msf() = %u:%u:%u", *minute, *second, *frame);
400}
401
409AARU_EXPORT void AARU_CALL aaruf_ecc_cd_reconstruct_prefix(uint8_t *sector, const uint8_t type, const int64_t lba)
410{
411 TRACE("Entering aaruf_cd_reconstruct_prefix(%p, %u, %lld)", sector, type, lba);
412
413 uint8_t minute, second, frame;
414
415 if(sector == NULL) return;
416
417 //
418 // Sync
419 //
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;
432
433 aaruf_cd_lba_to_msf(lba, &minute, &second, &frame);
434
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);
438
439 switch((TrackType)type)
440 {
441 case CdMode1:
442 //
443 // Mode
444 //
445 sector[0x00F] = 0x01;
446 break;
447 case CdMode2Form1:
448 case CdMode2Form2:
449 case CdMode2Formless:
450 //
451 // Mode
452 //
453 sector[0x00F] = 0x02;
454 //
455 // Flags
456 //
457 sector[0x010] = sector[0x014];
458 sector[0x011] = sector[0x015];
459 sector[0x012] = sector[0x016];
460 sector[0x013] = sector[0x017];
461 break;
462 default:
463 return;
464 }
465
466 TRACE("Exiting aaruf_ecc_cd_reconstruct_prefix()");
467}
468
476AARU_EXPORT void AARU_CALL aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, const uint8_t type)
477{
478 TRACE("Entering aaruf_ecc_cd_reconstruct(%p, %p, %u)", context, sector, type);
479
480 uint32_t computed_edc;
481 uint8_t zeroaddress[4];
482
483 if(context == NULL || sector == NULL)
484 {
485 TRACE("Exiting aaruf_ecc_cd_reconstruct() with missing data");
486 return;
487 }
488
489 CdEccContext *ctx = context;
490
491 if(!ctx->inited_edc)
492 {
493 TRACE("Exiting aaruf_ecc_cd_reconstruct() without initialized context");
494 return;
495 }
496
497 switch(type)
498 {
499 //
500 // Compute EDC
501 //
502 case CdMode1:
503 computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x810, 0);
504 memcpy(sector + 0x810, &computed_edc, 4);
505 break;
506 case CdMode2Form1:
507 computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x808, 0x10);
508 memcpy(sector + 0x818, &computed_edc, 4);
509 break;
510 case CdMode2Form2:
511 computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x91C, 0x10);
512 memcpy(sector + 0x92C, &computed_edc, 4);
513 break;
514 default:
515 TRACE("Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
516 return;
517 }
518
519 memset(&zeroaddress, 0, 4);
520
521 switch(type)
522 {
523 //
524 // Compute ECC
525 //
526 case CdMode1:
527 //
528 // Reserved
529 //
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;
538 aaruf_ecc_cd_write_sector(context, sector, sector, sector, 0xC, 0x10, 0x81C);
539 break;
540 case CdMode2Form1:
541 aaruf_ecc_cd_write_sector(context, zeroaddress, sector, sector, 0, 0x10, 0x81C);
542 break;
543 default:
544 TRACE("Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
545 return;
546 }
547
548 //
549 // Done
550 //
551 TRACE("Exiting aaruf_ecc_cd_reconstruct()");
552}
553
564AARU_EXPORT uint32_t AARU_CALL aaruf_edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, int size, int pos)
565{
566 TRACE("Entering aaruf_edc_cd_compute(%p, %u, %p, %d, %d)", context, edc, src, size, pos);
567
568 if(context == NULL || src == NULL)
569 {
570 TRACE("Exiting aaruf_edc_cd_compute() with missing data");
571 return 0;
572 }
573
574 CdEccContext *ctx = context;
575
576 if(!ctx->inited_edc)
577 {
578 TRACE("Exiting aaruf_edc_cd_compute() without initialized context");
579 return 0;
580 }
581
582 for(; size > 0; size--) edc = edc >> 8 ^ ctx->edc_table[(edc ^ src[pos++]) & 0xFF];
583
584 TRACE("Exiting aaruf_edc_cd_compute() = 0x%08X", edc);
585 return edc;
586}
#define AARU_CALL
Definition decls.h:46
#define AARU_LOCAL
Definition decls.h:56
#define AARU_EXPORT
Definition decls.h:55
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.
Definition ecc_cd.c:244
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.
Definition ecc_cd.c:310
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.
Definition ecc_cd.c:391
void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, const uint8_t type)
Reconstructs the EDC and ECC fields of a CD sector.
Definition ecc_cd.c:476
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.
Definition ecc_cd.c:564
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.
Definition ecc_cd.c:409
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).
Definition ecc_cd.c:369
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).
Definition ecc_cd.c:182
void * aaruf_ecc_cd_init()
Initializes a Compact Disc ECC context.
Definition ecc_cd.c:35
void aaruf_ecc_cd_free(void *ctx)
Frees a Compact Disc ECC context and its internal tables.
Definition ecc_cd.c:99
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).
Definition ecc_cd.c:118
TrackType
Track (partitioning element) types for optical media.
Definition enums.h:200
@ CdMode1
Compact Disc Mode 1 data track.
Definition enums.h:203
@ CdMode2Form2
Compact Disc Mode 2 Form 2 data track.
Definition enums.h:206
@ CdMode2Form1
Compact Disc Mode 2 Form 1 data track.
Definition enums.h:205
@ CdMode2Formless
Compact Disc Mode 2 (formless) data track.
Definition enums.h:204
#define TRACE(fmt,...)
Definition log.h:25
Lookup tables and state for Compact Disc EDC/ECC (P/Q) regeneration / verification.
Definition context.h:89
uint8_t * ecc_f_table
Forward (F) ECC table.
Definition context.h:92
bool inited_edc
True once EDC/ECC tables have been initialized.
Definition context.h:90
uint32_t * edc_table
EDC (CRC) lookup table.
Definition context.h:93
uint8_t * ecc_b_table
Backward (B) ECC table (allocated, size implementation-defined).
Definition context.h:91