1 /* dvdisaster: Additional error correction for optical media.
2 * Copyright (C) 2004-2015 Carsten Gnoerlich.
3 *
4 * Email: carsten@dvdisaster.org -or- cgnoerlich@fsfe.org
5 * Project homepage: http://www.dvdisaster.org
6 *
7 * This file is part of dvdisaster.
8 *
9 * dvdisaster is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * dvdisaster is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with dvdisaster. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "dvdisaster.h"
24
25 #include "scsi-layer.h"
26 #include "rs02-includes.h"
27
28 /***
29 *** Create an uninitialized CRC buffer
30 ***/
31
CreateCrcBuf(guint64 sectors)32 CrcBuf *CreateCrcBuf(guint64 sectors)
33 { CrcBuf *cb = g_malloc(sizeof(CrcBuf));
34
35 cb->crcbuf = g_malloc(sectors * sizeof(guint32));
36 cb->size = sectors;
37 cb->valid = CreateBitmap0(sectors);
38
39 return cb;
40 }
41
42 /***
43 *** Test a 2048 byte block against the checksum in the buffer
44 ***/
45
CheckAgainstCrcBuffer(CrcBuf * cb,gint64 idx,unsigned char * buf)46 int CheckAgainstCrcBuffer(CrcBuf *cb, gint64 idx, unsigned char *buf)
47 { guint32 crc;
48
49 if(idx < 0 || idx >= cb->size)
50 return CRC_OUTSIDE_BOUND;
51
52 crc = Crc32(buf, 2048);
53
54 if(!GetBit(cb->valid, idx))
55 return CRC_UNKNOWN;
56
57 if(crc == cb->crcbuf[idx])
58 return CRC_GOOD;
59
60 return CRC_BAD;
61 }
62
63
64 /***
65 *** Clean up
66 ***/
67
FreeCrcBuf(CrcBuf * cb)68 void FreeCrcBuf(CrcBuf *cb)
69 {
70 g_free(cb->crcbuf);
71 FreeBitmap(cb->valid);
72 g_free(cb);
73 }
74