| 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: [email protected] -or- [email protected] |
| 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 "../mednafen-types.h" |
| 39 | |
| 40 | #include <ctype.h> |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <math.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <stdarg.h> |
| 46 | #include <stddef.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #ifdef _WIN32 |
| 51 | #include <direct.h> |
| 52 | #else |
| 53 | #include <unistd.h> |
| 54 | #endif |
| 55 | |
| 56 | /*** |
| 57 | *** dvdisaster.c |
| 58 | ***/ |
| 59 | |
| 60 | void PrepareDeadSector(void); |
| 61 | |
| 62 | void CreateEcc(void); |
| 63 | void FixEcc(void); |
| 64 | void Verify(void); |
| 65 | |
| 66 | uint32 EDCCrc32(const unsigned char*, int); |
| 67 | |
| 68 | /*** |
| 69 | *** galois.c |
| 70 | *** |
| 71 | * This is currently the hardcoded GF(2**8). |
| 72 | * int32 gives abundant space for the GF. |
| 73 | * Squeezing it down to uint8 won't probably gain much, |
| 74 | * so we implement this defensively here. |
| 75 | * |
| 76 | * Note that some performance critical stuff needs to |
| 77 | * be #included from galois-inlines.h |
| 78 | */ |
| 79 | |
| 80 | /* Galois field parameters for 8bit symbol Reed-Solomon code */ |
| 81 | |
| 82 | #define GF_SYMBOLSIZE 8 |
| 83 | #define GF_FIELDSIZE (1<<GF_SYMBOLSIZE) |
| 84 | #define GF_FIELDMAX (GF_FIELDSIZE-1) |
| 85 | #define GF_ALPHA0 GF_FIELDMAX |
| 86 | |
| 87 | /* Lookup tables for Galois field arithmetic */ |
| 88 | |
| 89 | typedef struct _GaloisTables |
| 90 | { int32 gfGenerator; /* GF generator polynomial */ |
| 91 | int32 *indexOf; /* log */ |
| 92 | int32 *alphaTo; /* inverse log */ |
| 93 | int32 *encAlphaTo; /* inverse log optimized for encoder */ |
| 94 | } GaloisTables; |
| 95 | |
| 96 | /* Lookup and working tables for the ReedSolomon codecs */ |
| 97 | |
| 98 | typedef struct _ReedSolomonTables |
| 99 | { GaloisTables *gfTables;/* from above */ |
| 100 | int32 *gpoly; /* RS code generator polynomial */ |
| 101 | int32 fcr; /* first consecutive root of RS generator polynomial */ |
| 102 | int32 primElem; /* primitive field element */ |
| 103 | int32 nroots; /* degree of RS generator polynomial */ |
| 104 | int32 ndata; /* data bytes per ecc block */ |
| 105 | } ReedSolomonTables; |
| 106 | |
| 107 | GaloisTables* CreateGaloisTables(int32); |
| 108 | void FreeGaloisTables(GaloisTables*); |
| 109 | |
| 110 | ReedSolomonTables *CreateReedSolomonTables(GaloisTables*, int32, int32, int); |
| 111 | void FreeReedSolomonTables(ReedSolomonTables*); |
| 112 | |
| 113 | /*** |
| 114 | *** l-ec.c |
| 115 | ***/ |
| 116 | |
| 117 | #define N_P_VECTORS 86 /* 43 16bit p vectors */ |
| 118 | #define P_VECTOR_SIZE 26 /* using RS(26,24) ECC */ |
| 119 | |
| 120 | #define N_Q_VECTORS 52 /* 26 16bit q vectors */ |
| 121 | #define Q_VECTOR_SIZE 45 /* using RS(45,43) ECC */ |
| 122 | |
| 123 | #define P_PADDING 229 /* padding values for */ |
| 124 | #define Q_PADDING 210 /* shortened RS code */ |
| 125 | |
| 126 | int PToByteIndex(int, int); |
| 127 | int QToByteIndex(int, int); |
| 128 | void ByteIndexToP(int, int*, int*); |
| 129 | void ByteIndexToQ(int, int*, int*); |
| 130 | |
| 131 | void GetPVector(unsigned char*, unsigned char*, int); |
| 132 | void SetPVector(unsigned char*, unsigned char*, int); |
| 133 | void FillPVector(unsigned char*, unsigned char, int); |
| 134 | void AndPVector(unsigned char*, unsigned char, int); |
| 135 | void OrPVector(unsigned char*, unsigned char, int); |
| 136 | |
| 137 | void GetQVector(unsigned char*, unsigned char*, int); |
| 138 | void SetQVector(unsigned char*, unsigned char*, int); |
| 139 | void FillQVector(unsigned char*, unsigned char, int); |
| 140 | void AndQVector(unsigned char*, unsigned char, int); |
| 141 | void OrQVector(unsigned char*, unsigned char, int); |
| 142 | |
| 143 | int DecodePQ(ReedSolomonTables*, unsigned char*, int, int*, int); |
| 144 | |
| 145 | int CountC2Errors(unsigned char*); |
| 146 | |
| 147 | /*** |
| 148 | *** misc.c |
| 149 | ***/ |
| 150 | |
| 151 | char* sgettext(char*); |
| 152 | char* sgettext_utf8(char*); |
| 153 | |
| 154 | int64 uchar_to_int64(unsigned char*); |
| 155 | void int64_to_uchar(unsigned char*, int64); |
| 156 | |
| 157 | void CalcSectors(int64, int64*, int*); |
| 158 | |
| 159 | /*** |
| 160 | *** recover-raw.c |
| 161 | ***/ |
| 162 | |
| 163 | #define CD_RAW_SECTOR_SIZE 2352 |
| 164 | #define CD_RAW_C2_SECTOR_SIZE (2352+294) /* main channel plus C2 vector */ |
| 165 | |
| 166 | int CheckEDC(const unsigned char*, bool); |
| 167 | int CheckMSF(unsigned char*, int); |
| 168 | |
| 169 | |
| 170 | int ValidateRawSector(unsigned char *frame, bool xaMode); |
| 171 | bool Init_LEC_Correct(void); |
| 172 | void Kill_LEC_Correct(void); |
| 173 | |
| 174 | |
| 175 | #endif /* DVDISASTER_H */ |