1 /* dvdisaster: Additional error correction for optical media. 2 * Copyright (C) 2004-2007 Carsten Gnoerlich. 3 * Project home page: http://www.dvdisaster.com 4 * Email: carsten@dvdisaster.com -or- cgnoerlich@fsfe.org 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, 19 * or direct your browser at http://www.gnu.org. 20 */ 21 22 #ifndef DVDISASTER_H 23 #define DVDISASTER_H 24 25 /* "Dare to be gorgeous and unique. 26 * But don't ever be cryptic or otherwise unfathomable. 27 * Make it unforgettably great." 28 * 29 * From "A Final Note on Style", 30 * Amiga Intuition Reference Manual, 1986, p. 231 31 */ 32 33 /*** 34 *** I'm too lazy to mess with #include dependencies. 35 *** Everything #includeable is rolled up herein... 36 */ 37 38 #include <stdint.h> 39 #include <ctype.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <math.h> 43 #include <sys/stat.h> 44 #include <stdarg.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 /*** 51 *** dvdisaster.c 52 ***/ 53 54 void PrepareDeadSector(void); 55 56 void CreateEcc(void); 57 void FixEcc(void); 58 void Verify(void); 59 60 uint32_t EDCCrc32(const unsigned char*, int); 61 62 /*** 63 *** galois.c 64 *** 65 * This is currently the hardcoded GF(2**8). 66 * int32_t gives abundant space for the GF. 67 * Squeezing it down to uint8 won't probably gain much, 68 * so we implement this defensively here. 69 * 70 * Note that some performance critical stuff needs to 71 * be #included from galois-inlines.h 72 */ 73 74 /* Galois field parameters for 8bit symbol Reed-Solomon code */ 75 76 #define GF_SYMBOLSIZE 8 77 #define GF_FIELDSIZE (1<<GF_SYMBOLSIZE) 78 #define GF_FIELDMAX (GF_FIELDSIZE-1) 79 #define GF_ALPHA0 GF_FIELDMAX 80 81 /* Lookup tables for Galois field arithmetic */ 82 83 typedef struct _GaloisTables 84 { int32_t gfGenerator; /* GF generator polynomial */ 85 int32_t *indexOf; /* log */ 86 int32_t *alphaTo; /* inverse log */ 87 int32_t *encAlphaTo; /* inverse log optimized for encoder */ 88 } GaloisTables; 89 90 /* Lookup and working tables for the ReedSolomon codecs */ 91 92 typedef struct _ReedSolomonTables 93 { GaloisTables *gfTables;/* from above */ 94 int32_t *gpoly; /* RS code generator polynomial */ 95 int32_t fcr; /* first consecutive root of RS generator polynomial */ 96 int32_t primElem; /* primitive field element */ 97 int32_t nroots; /* degree of RS generator polynomial */ 98 int32_t ndata; /* data bytes per ecc block */ 99 } ReedSolomonTables; 100 101 GaloisTables* CreateGaloisTables(int32_t); 102 void FreeGaloisTables(GaloisTables*); 103 104 ReedSolomonTables *CreateReedSolomonTables(GaloisTables*, int32_t, int32_t, int); 105 void FreeReedSolomonTables(ReedSolomonTables*); 106 107 /*** 108 *** l-ec.c 109 ***/ 110 111 #define N_P_VECTORS 86 /* 43 16bit p vectors */ 112 #define P_VECTOR_SIZE 26 /* using RS(26,24) ECC */ 113 114 #define N_Q_VECTORS 52 /* 26 16bit q vectors */ 115 #define Q_VECTOR_SIZE 45 /* using RS(45,43) ECC */ 116 117 #define P_PADDING 229 /* padding values for */ 118 #define Q_PADDING 210 /* shortened RS code */ 119 120 int PToByteIndex(int, int); 121 int QToByteIndex(int, int); 122 void ByteIndexToP(int, int*, int*); 123 void ByteIndexToQ(int, int*, int*); 124 125 void GetPVector(unsigned char*, unsigned char*, int); 126 void SetPVector(unsigned char*, unsigned char*, int); 127 void FillPVector(unsigned char*, unsigned char, int); 128 void AndPVector(unsigned char*, unsigned char, int); 129 void OrPVector(unsigned char*, unsigned char, int); 130 131 void GetQVector(unsigned char*, unsigned char*, int); 132 void SetQVector(unsigned char*, unsigned char*, int); 133 void FillQVector(unsigned char*, unsigned char, int); 134 void AndQVector(unsigned char*, unsigned char, int); 135 void OrQVector(unsigned char*, unsigned char, int); 136 137 int DecodePQ(ReedSolomonTables*, unsigned char*, int, int*, int); 138 139 int CountC2Errors(unsigned char*); 140 141 /*** 142 *** misc.c 143 ***/ 144 145 char* sgettext(char*); 146 char* sgettext_utf8(char*); 147 148 int64_t uchar_to_int64_t(unsigned char*); 149 void int64_t_to_uchar(unsigned char*, int64_t); 150 151 void CalcSectors(int64_t, int64_t*, int*); 152 153 /*** 154 *** recover-raw.c 155 ***/ 156 157 #define CD_RAW_SECTOR_SIZE 2352 158 #define CD_RAW_C2_SECTOR_SIZE (2352+294) /* main channel plus C2 vector */ 159 160 int CheckEDC(const unsigned char*, bool); 161 int CheckMSF(unsigned char*, int); 162 163 164 int ValidateRawSector(unsigned char *frame, bool xaMode); 165 bool Init_LEC_Correct(void); 166 void Kill_LEC_Correct(void); 167 168 169 #endif /* DVDISASTER_H */ 170