Normalize line endings

This commit is contained in:
Braden McDaniel 2013-11-01 16:06:06 -04:00
parent f84e0d6086
commit d9461a9401
15 changed files with 3643 additions and 3643 deletions

View file

@ -1,105 +1,105 @@
# Create a library called "Hello" which includes the source file "hello.cxx".
# The extension is already found. Any number of sources could be listed here.
add_library (
friidumplib
${libfriidump_type}
#SHARED
#STATIC
brickblocker.h
brickblocker.c
byteorder.h
constants.h
disc.h
disc.c
dumper.h
dumper.c
dvd_drive.h
dvd_drive.c
hitachi.c
ecma-267.h
ecma-267.c
lite-on.c
misc.h
misc.c
renesas.c
rs.h
rs.c
unscrambler.h
unscrambler.c
vanilla_2064.c
vanilla_2384.c
win32compat.h
win32compat.c
)
set_target_properties (friidumplib PROPERTIES OUTPUT_NAME "friidump")
include_directories (
${FriiDump_SOURCE_DIR}/libmultihash
)
# Make sure the linker can find the Hello library once it is built.
link_directories (
${FriiDump_BINARY_DIR}/libmultihash
)
# Link the executable to the Hello library.
target_link_libraries (
friidumplib
multihashlib
)
# Before making a release, the LTVERSION string should be modified.
# The string is of the form CURRENT:REVISION:AGE.
#
# CURRENT (C)
# The most recent interface number that this library implements.
#
# REVISION (R)
# The implementation number that this library implements.
#
# AGE (A)
# The difference between the newest and oldest interfaces that this
# library implements. In other works, the library implements all the
# interface numbers in the range from number 'CURRENT - AGE' to
# 'CURRENT'.
#
# This means that:
#
# - If interfaces have been changed or added, but binary compatibility has
# been preserved, change to C+1:0:A+1
#
# - If binary compatibility has been broken (eg removed or changed
# interfaces) change to C+1:0:0
#
# - If the interface is the same as the previous version, change to C:R+1:A
#
#set_target_properties (friidumplib PROPERTIES SOVERSION 1.0.0)
# Windows stuff to correctly build DLL or static library
#get_target_property (libfriidump_type friidumplib TYPE)
if (WIN32)
if (libfriidump_type STREQUAL "SHARED")
# MESSAGE ("Building libfriidump DLL")
ADD_DEFINITIONS (-DFRIIDUMPLIB_BUILD_DLL)
set_target_properties (friidumplib PROPERTIES DEFINE_SYMBOL FRIIDUMPLIB_EXPORTS)
install (
TARGETS friidumplib
RUNTIME DESTINATION /
#ARCHIVE DESTINATION lib
)
endif (libfriidump_type STREQUAL "SHARED")
else (WIN32)
# Install stuff, only if a shared library is being built
if (libfriidump_type STREQUAL "SHARED")
install (
TARGETS friidumplib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static
)
endif (libfriidump_type STREQUAL "SHARED")
endif (WIN32)
# Create a library called "Hello" which includes the source file "hello.cxx".
# The extension is already found. Any number of sources could be listed here.
add_library (
friidumplib
${libfriidump_type}
#SHARED
#STATIC
brickblocker.h
brickblocker.c
byteorder.h
constants.h
disc.h
disc.c
dumper.h
dumper.c
dvd_drive.h
dvd_drive.c
hitachi.c
ecma-267.h
ecma-267.c
lite-on.c
misc.h
misc.c
renesas.c
rs.h
rs.c
unscrambler.h
unscrambler.c
vanilla_2064.c
vanilla_2384.c
win32compat.h
win32compat.c
)
set_target_properties (friidumplib PROPERTIES OUTPUT_NAME "friidump")
include_directories (
${FriiDump_SOURCE_DIR}/libmultihash
)
# Make sure the linker can find the Hello library once it is built.
link_directories (
${FriiDump_BINARY_DIR}/libmultihash
)
# Link the executable to the Hello library.
target_link_libraries (
friidumplib
multihashlib
)
# Before making a release, the LTVERSION string should be modified.
# The string is of the form CURRENT:REVISION:AGE.
#
# CURRENT (C)
# The most recent interface number that this library implements.
#
# REVISION (R)
# The implementation number that this library implements.
#
# AGE (A)
# The difference between the newest and oldest interfaces that this
# library implements. In other works, the library implements all the
# interface numbers in the range from number 'CURRENT - AGE' to
# 'CURRENT'.
#
# This means that:
#
# - If interfaces have been changed or added, but binary compatibility has
# been preserved, change to C+1:0:A+1
#
# - If binary compatibility has been broken (eg removed or changed
# interfaces) change to C+1:0:0
#
# - If the interface is the same as the previous version, change to C:R+1:A
#
#set_target_properties (friidumplib PROPERTIES SOVERSION 1.0.0)
# Windows stuff to correctly build DLL or static library
#get_target_property (libfriidump_type friidumplib TYPE)
if (WIN32)
if (libfriidump_type STREQUAL "SHARED")
# MESSAGE ("Building libfriidump DLL")
ADD_DEFINITIONS (-DFRIIDUMPLIB_BUILD_DLL)
set_target_properties (friidumplib PROPERTIES DEFINE_SYMBOL FRIIDUMPLIB_EXPORTS)
install (
TARGETS friidumplib
RUNTIME DESTINATION /
#ARCHIVE DESTINATION lib
)
endif (libfriidump_type STREQUAL "SHARED")
else (WIN32)
# Install stuff, only if a shared library is being built
if (libfriidump_type STREQUAL "SHARED")
install (
TARGETS friidumplib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static
)
endif (libfriidump_type STREQUAL "SHARED")
endif (WIN32)

File diff suppressed because it is too large Load diff

View file

@ -1,109 +1,109 @@
/*
unscrambler 0.4: unscramble not standard IVs scrambled DVDs thru
bruteforce, intended for Gamecube/WII Optical Disks.
Copyright (C) 2006 Victor Muñoz (xt5@ingenieria-inversa.cl)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ecma-267.h"
/* EDC stuff */
unsigned int edc_table[256] = {
0x00000000, 0x80000011, 0x80000033, 0x00000022, 0x80000077, 0x00000066, 0x00000044, 0x80000055,
0x800000FF, 0x000000EE, 0x000000CC, 0x800000DD, 0x00000088, 0x80000099, 0x800000BB, 0x000000AA,
0x800001EF, 0x000001FE, 0x000001DC, 0x800001CD, 0x00000198, 0x80000189, 0x800001AB, 0x000001BA,
0x00000110, 0x80000101, 0x80000123, 0x00000132, 0x80000167, 0x00000176, 0x00000154, 0x80000145,
0x800003CF, 0x000003DE, 0x000003FC, 0x800003ED, 0x000003B8, 0x800003A9, 0x8000038B, 0x0000039A,
0x00000330, 0x80000321, 0x80000303, 0x00000312, 0x80000347, 0x00000356, 0x00000374, 0x80000365,
0x00000220, 0x80000231, 0x80000213, 0x00000202, 0x80000257, 0x00000246, 0x00000264, 0x80000275,
0x800002DF, 0x000002CE, 0x000002EC, 0x800002FD, 0x000002A8, 0x800002B9, 0x8000029B, 0x0000028A,
0x8000078F, 0x0000079E, 0x000007BC, 0x800007AD, 0x000007F8, 0x800007E9, 0x800007CB, 0x000007DA,
0x00000770, 0x80000761, 0x80000743, 0x00000752, 0x80000707, 0x00000716, 0x00000734, 0x80000725,
0x00000660, 0x80000671, 0x80000653, 0x00000642, 0x80000617, 0x00000606, 0x00000624, 0x80000635,
0x8000069F, 0x0000068E, 0x000006AC, 0x800006BD, 0x000006E8, 0x800006F9, 0x800006DB, 0x000006CA,
0x00000440, 0x80000451, 0x80000473, 0x00000462, 0x80000437, 0x00000426, 0x00000404, 0x80000415,
0x800004BF, 0x000004AE, 0x0000048C, 0x8000049D, 0x000004C8, 0x800004D9, 0x800004FB, 0x000004EA,
0x800005AF, 0x000005BE, 0x0000059C, 0x8000058D, 0x000005D8, 0x800005C9, 0x800005EB, 0x000005FA,
0x00000550, 0x80000541, 0x80000563, 0x00000572, 0x80000527, 0x00000536, 0x00000514, 0x80000505,
0x80000F0F, 0x00000F1E, 0x00000F3C, 0x80000F2D, 0x00000F78, 0x80000F69, 0x80000F4B, 0x00000F5A,
0x00000FF0, 0x80000FE1, 0x80000FC3, 0x00000FD2, 0x80000F87, 0x00000F96, 0x00000FB4, 0x80000FA5,
0x00000EE0, 0x80000EF1, 0x80000ED3, 0x00000EC2, 0x80000E97, 0x00000E86, 0x00000EA4, 0x80000EB5,
0x80000E1F, 0x00000E0E, 0x00000E2C, 0x80000E3D, 0x00000E68, 0x80000E79, 0x80000E5B, 0x00000E4A,
0x00000CC0, 0x80000CD1, 0x80000CF3, 0x00000CE2, 0x80000CB7, 0x00000CA6, 0x00000C84, 0x80000C95,
0x80000C3F, 0x00000C2E, 0x00000C0C, 0x80000C1D, 0x00000C48, 0x80000C59, 0x80000C7B, 0x00000C6A,
0x80000D2F, 0x00000D3E, 0x00000D1C, 0x80000D0D, 0x00000D58, 0x80000D49, 0x80000D6B, 0x00000D7A,
0x00000DD0, 0x80000DC1, 0x80000DE3, 0x00000DF2, 0x80000DA7, 0x00000DB6, 0x00000D94, 0x80000D85,
0x00000880, 0x80000891, 0x800008B3, 0x000008A2, 0x800008F7, 0x000008E6, 0x000008C4, 0x800008D5,
0x8000087F, 0x0000086E, 0x0000084C, 0x8000085D, 0x00000808, 0x80000819, 0x8000083B, 0x0000082A,
0x8000096F, 0x0000097E, 0x0000095C, 0x8000094D, 0x00000918, 0x80000909, 0x8000092B, 0x0000093A,
0x00000990, 0x80000981, 0x800009A3, 0x000009B2, 0x800009E7, 0x000009F6, 0x000009D4, 0x800009C5,
0x80000B4F, 0x00000B5E, 0x00000B7C, 0x80000B6D, 0x00000B38, 0x80000B29, 0x80000B0B, 0x00000B1A,
0x00000BB0, 0x80000BA1, 0x80000B83, 0x00000B92, 0x80000BC7, 0x00000BD6, 0x00000BF4, 0x80000BE5,
0x00000AA0, 0x80000AB1, 0x80000A93, 0x00000A82, 0x80000AD7, 0x00000AC6, 0x00000AE4, 0x80000AF5,
0x80000A5F, 0x00000A4E, 0x00000A6C, 0x80000A7D, 0x00000A28, 0x80000A39, 0x80000A1B, 0x00000A0AL
};
u32 edc_calc(u32 edc, u8 *ptr, u32 len) {
while (len--) edc=edc_table[((edc>>24)^*ptr++)&0xFF]^(edc<<8);
return edc;
}
/* end of EDC stuff */
/* LFSR stuff */
u16 ecma267_ivs[]= {
0x0001, 0x5500, 0x0002, 0x2A00,
0x0004, 0x5400, 0x0008, 0x2800,
0x0010, 0x5000, 0x0020, 0x2001,
0x0040, 0x4002, 0x0080, 0x0005
};
unsigned short LFSR;
void LFSR_ecma_init(int iv) {
LFSR=ecma267_ivs[iv];
}
void LFSR_init(u16 seed) {
LFSR=seed;
}
int LFSR_tick() {
int ret;
int n;
ret=LFSR>>14;
n=ret^((LFSR>>10)&1);
LFSR=((LFSR<<1)|n)&0x7FFF;
return ret;
}
unsigned char LFSR_byte() {
u8 ret;
int i;
ret=0;
for(i=0; i<8; i++) ret=(ret<<1)|LFSR_tick();
return ret;
}
/* end of LFSR stuff */
/*
unscrambler 0.4: unscramble not standard IVs scrambled DVDs thru
bruteforce, intended for Gamecube/WII Optical Disks.
Copyright (C) 2006 Victor Muñoz (xt5@ingenieria-inversa.cl)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ecma-267.h"
/* EDC stuff */
unsigned int edc_table[256] = {
0x00000000, 0x80000011, 0x80000033, 0x00000022, 0x80000077, 0x00000066, 0x00000044, 0x80000055,
0x800000FF, 0x000000EE, 0x000000CC, 0x800000DD, 0x00000088, 0x80000099, 0x800000BB, 0x000000AA,
0x800001EF, 0x000001FE, 0x000001DC, 0x800001CD, 0x00000198, 0x80000189, 0x800001AB, 0x000001BA,
0x00000110, 0x80000101, 0x80000123, 0x00000132, 0x80000167, 0x00000176, 0x00000154, 0x80000145,
0x800003CF, 0x000003DE, 0x000003FC, 0x800003ED, 0x000003B8, 0x800003A9, 0x8000038B, 0x0000039A,
0x00000330, 0x80000321, 0x80000303, 0x00000312, 0x80000347, 0x00000356, 0x00000374, 0x80000365,
0x00000220, 0x80000231, 0x80000213, 0x00000202, 0x80000257, 0x00000246, 0x00000264, 0x80000275,
0x800002DF, 0x000002CE, 0x000002EC, 0x800002FD, 0x000002A8, 0x800002B9, 0x8000029B, 0x0000028A,
0x8000078F, 0x0000079E, 0x000007BC, 0x800007AD, 0x000007F8, 0x800007E9, 0x800007CB, 0x000007DA,
0x00000770, 0x80000761, 0x80000743, 0x00000752, 0x80000707, 0x00000716, 0x00000734, 0x80000725,
0x00000660, 0x80000671, 0x80000653, 0x00000642, 0x80000617, 0x00000606, 0x00000624, 0x80000635,
0x8000069F, 0x0000068E, 0x000006AC, 0x800006BD, 0x000006E8, 0x800006F9, 0x800006DB, 0x000006CA,
0x00000440, 0x80000451, 0x80000473, 0x00000462, 0x80000437, 0x00000426, 0x00000404, 0x80000415,
0x800004BF, 0x000004AE, 0x0000048C, 0x8000049D, 0x000004C8, 0x800004D9, 0x800004FB, 0x000004EA,
0x800005AF, 0x000005BE, 0x0000059C, 0x8000058D, 0x000005D8, 0x800005C9, 0x800005EB, 0x000005FA,
0x00000550, 0x80000541, 0x80000563, 0x00000572, 0x80000527, 0x00000536, 0x00000514, 0x80000505,
0x80000F0F, 0x00000F1E, 0x00000F3C, 0x80000F2D, 0x00000F78, 0x80000F69, 0x80000F4B, 0x00000F5A,
0x00000FF0, 0x80000FE1, 0x80000FC3, 0x00000FD2, 0x80000F87, 0x00000F96, 0x00000FB4, 0x80000FA5,
0x00000EE0, 0x80000EF1, 0x80000ED3, 0x00000EC2, 0x80000E97, 0x00000E86, 0x00000EA4, 0x80000EB5,
0x80000E1F, 0x00000E0E, 0x00000E2C, 0x80000E3D, 0x00000E68, 0x80000E79, 0x80000E5B, 0x00000E4A,
0x00000CC0, 0x80000CD1, 0x80000CF3, 0x00000CE2, 0x80000CB7, 0x00000CA6, 0x00000C84, 0x80000C95,
0x80000C3F, 0x00000C2E, 0x00000C0C, 0x80000C1D, 0x00000C48, 0x80000C59, 0x80000C7B, 0x00000C6A,
0x80000D2F, 0x00000D3E, 0x00000D1C, 0x80000D0D, 0x00000D58, 0x80000D49, 0x80000D6B, 0x00000D7A,
0x00000DD0, 0x80000DC1, 0x80000DE3, 0x00000DF2, 0x80000DA7, 0x00000DB6, 0x00000D94, 0x80000D85,
0x00000880, 0x80000891, 0x800008B3, 0x000008A2, 0x800008F7, 0x000008E6, 0x000008C4, 0x800008D5,
0x8000087F, 0x0000086E, 0x0000084C, 0x8000085D, 0x00000808, 0x80000819, 0x8000083B, 0x0000082A,
0x8000096F, 0x0000097E, 0x0000095C, 0x8000094D, 0x00000918, 0x80000909, 0x8000092B, 0x0000093A,
0x00000990, 0x80000981, 0x800009A3, 0x000009B2, 0x800009E7, 0x000009F6, 0x000009D4, 0x800009C5,
0x80000B4F, 0x00000B5E, 0x00000B7C, 0x80000B6D, 0x00000B38, 0x80000B29, 0x80000B0B, 0x00000B1A,
0x00000BB0, 0x80000BA1, 0x80000B83, 0x00000B92, 0x80000BC7, 0x00000BD6, 0x00000BF4, 0x80000BE5,
0x00000AA0, 0x80000AB1, 0x80000A93, 0x00000A82, 0x80000AD7, 0x00000AC6, 0x00000AE4, 0x80000AF5,
0x80000A5F, 0x00000A4E, 0x00000A6C, 0x80000A7D, 0x00000A28, 0x80000A39, 0x80000A1B, 0x00000A0AL
};
u32 edc_calc(u32 edc, u8 *ptr, u32 len) {
while (len--) edc=edc_table[((edc>>24)^*ptr++)&0xFF]^(edc<<8);
return edc;
}
/* end of EDC stuff */
/* LFSR stuff */
u16 ecma267_ivs[]= {
0x0001, 0x5500, 0x0002, 0x2A00,
0x0004, 0x5400, 0x0008, 0x2800,
0x0010, 0x5000, 0x0020, 0x2001,
0x0040, 0x4002, 0x0080, 0x0005
};
unsigned short LFSR;
void LFSR_ecma_init(int iv) {
LFSR=ecma267_ivs[iv];
}
void LFSR_init(u16 seed) {
LFSR=seed;
}
int LFSR_tick() {
int ret;
int n;
ret=LFSR>>14;
n=ret^((LFSR>>10)&1);
LFSR=((LFSR<<1)|n)&0x7FFF;
return ret;
}
unsigned char LFSR_byte() {
u8 ret;
int i;
ret=0;
for(i=0; i<8; i++) ret=(ret<<1)|LFSR_tick();
return ret;
}
/* end of LFSR stuff */

View file

@ -1,47 +1,47 @@
/*
unscrambler 0.4: unscramble not standard IVs scrambled DVDs thru
bruteforce, intended for Gamecube/WII Optical Disks.
Copyright (C) 2006 Victor Muñoz (xt5@ingenieria-inversa.cl)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
typedef unsigned int u32;
typedef int s32;
typedef unsigned short u16;
typedef short s16;
typedef unsigned char u8;
typedef char s8;
/* EDC stuff */
u32 edc_calc(u32 edc, u8 *ptr, u32 len);
/* end of EDC stuff */
/* LFSR stuff */
void LFSR_ecma_init(int iv);
void LFSR_init(u16 seed);
int LFSR_tick();
u8 LFSR_byte();
/* end of LFSR stuff */
/*
unscrambler 0.4: unscramble not standard IVs scrambled DVDs thru
bruteforce, intended for Gamecube/WII Optical Disks.
Copyright (C) 2006 Victor Muñoz (xt5@ingenieria-inversa.cl)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
typedef unsigned int u32;
typedef int s32;
typedef unsigned short u16;
typedef short s16;
typedef unsigned char u8;
typedef char s8;
/* EDC stuff */
u32 edc_calc(u32 edc, u8 *ptr, u32 len);
/* end of EDC stuff */
/* LFSR stuff */
void LFSR_ecma_init(int iv);
void LFSR_init(u16 seed);
int LFSR_tick();
u8 LFSR_byte();
/* end of LFSR stuff */

View file

@ -1,316 +1,316 @@
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define mm 8 /* RS code over GF(2**mm) - change to suit */
#define n 256 /* n = size of the field */
#define nn 182 /* nn=2**mm -1 length of codeword */
#define kk 172 /* kk = nn-2*tt */ /* Degree of g(x) = 2*tt */
//#define NN n-1
//#define FCR 0
//#define PRIM 1
#define _NROOTS nn-kk
//#define PAD NN-nn
//#define A0 NN
//#define IPRIM 1
const int NN = n-1;
const int FCR = 0;
const int PRIM = 1;
const int NROOTS = nn-kk;
const int PAD = (n-1)-nn;
const int A0 = n-1;
const int IPRIM = 1;
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
/**** Primitive polynomial ****/
int pp [mm+1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1}; /* 1+x^2+x^3+x^4+x^8 */
/* generator polynomial, tables for Galois field */
int alpha_to[n], index_of[n], gg[nn-kk+1];
int b0 = 1;
/* data[] is the info vector, bb[] is the parity vector, recd[] is the
noise corrupted received vector */
int recd[nn], data[kk], bb[nn-kk];
int modnn(int x){
while (x >= 0xff) {
x -= 0xff;
x = (x >> 0xff) + (x & 0xff);
}
return x;
}
void generate_gf()
{
register int i, mask ;
mask = 1 ;
alpha_to[mm] = 0 ;
for (i=0; i<mm; i++)
{ alpha_to[i] = mask ;
index_of[alpha_to[i]] = i ;
if (pp[i]!=0) /* If pp[i] == 1 then, term @^i occurs in poly-repr of @^mm */
alpha_to[mm] ^= mask ; /* Bit-wise EXOR operation */
mask <<= 1 ; /* single left-shift */
}
index_of[alpha_to[mm]] = mm ;
/* Have obtained poly-repr of @^mm. Poly-repr of @^(i+1) is given by
poly-repr of @^i shifted left one-bit and accounting for any @^mm
term that may occur when poly-repr of @^i is shifted. */
mask >>= 1 ;
for (i=mm+1; i<255; i++)
{ if (alpha_to[i-1] >= mask)
alpha_to[i] = alpha_to[mm] ^ ((alpha_to[i-1]^mask)<<1) ;
else alpha_to[i] = alpha_to[i-1]<<1 ;
index_of[alpha_to[i]] = i ;
}
index_of[0] = A0 ;//-1
}
void gen_poly()
/* Obtain the generator polynomial of the tt-error correcting, length */
{
register int i, j, root;
gg[0] = 1;
for (i = 0,root=0*1; i < nn-kk; i++,root += 1) {
gg[i+1] = 1;
for (j = i; j > 0; j--){
if (gg[j] != 0)
gg[j] = gg[j-1] ^ alpha_to[modnn(index_of[gg[j]] + root)];
else
gg[j] = gg[j-1];
}
gg[0] = alpha_to[modnn(index_of[gg[0]] + root)];
}
for (i=0; i <= nn-kk; i++) {
gg[i] = index_of[gg[i]];
}
}
void rs_encode(unsigned char *data, unsigned char *bb)
{
register int i,j ;
int feedback;
for (i=0; i<NROOTS; i++) bb[i] = 0; //nullify result
for(i=0;i<NN-NROOTS-PAD;i++){
feedback = index_of[data[i] ^ bb[0]];
if(feedback != A0){ /* feedback term is non-zero */
for(j=1;j<NROOTS;j++) {
bb[j] ^= alpha_to[modnn(feedback + gg[NROOTS-j])];
}
}
/* Shift */
memmove(&bb[0],&bb[1], NROOTS-1);
//for (j=0; j<NROOTS-1; j++) bb[j] = bb[j+1];
if(feedback != A0)
bb[NROOTS-1] = alpha_to[modnn(feedback + gg[0])];
else
bb[NROOTS-1] = 0;
}
}
///*
int rs_decode(unsigned char *data, int *eras_pos, int no_eras){
int deg_lambda, el, deg_omega;
int i, j, r,k;
unsigned char u,q,tmp,num1,num2,den,discr_r;
unsigned char lambda[_NROOTS+1], s[_NROOTS];
unsigned char b[_NROOTS+1], t[_NROOTS+1], omega[_NROOTS+1];
unsigned char root[_NROOTS], reg[_NROOTS+1], loc[_NROOTS];
int syn_error, count;
// form the syndromes; i.e., evaluate data(x) at roots of g(x)
for(i=0;i<NROOTS;i++)
s[i] = data[0];
for(j=1;j<NN-PAD;j++){
for(i=0;i<NROOTS;i++){
if(s[i] == 0){
s[i] = data[j];
} else {
s[i] = data[j] ^ alpha_to[modnn(index_of[s[i]] + (FCR+i)*PRIM)];
}
}
}
// Convert syndromes to index form, checking for nonzero condition
syn_error = 0;
for(i=0;i<NROOTS;i++){
syn_error |= s[i];
s[i] = index_of[s[i]];
}
if (!syn_error) {
// if syndrome is zero, data[] is a codeword and there are no
// errors to correct. So return data[] unmodified
//
count = 0;
goto finish;
}
memset(&lambda[1],0,NROOTS*sizeof(lambda[0]));
lambda[0] = 1;
if (no_eras > 0) {
/* Init lambda to be the erasure locator polynomial */
lambda[1] = alpha_to[modnn(PRIM*(NN-1-eras_pos[0]))];
for (i = 1; i < no_eras; i++) {
u = modnn(PRIM*(NN-1-eras_pos[i]));
for (j = i+1; j > 0; j--) {
tmp = index_of[lambda[j - 1]];
if(tmp != A0)
lambda[j] ^= alpha_to[modnn(u + tmp)];
}
}
}
for(i=0;i<NROOTS+1;i++)
b[i] = index_of[lambda[i]];
/*
* Begin Berlekamp-Massey algorithm to determine error+erasure
* locator polynomial
*/
r = no_eras;
el = no_eras;
while (++r <= NROOTS) { /* r is the step number */
/* Compute discrepancy at the r-th step in poly-form */
discr_r = 0;
for (i = 0; i < r; i++){
if ((lambda[i] != 0) && (s[r-i-1] != A0)) {
discr_r ^= alpha_to[modnn(index_of[lambda[i]] + s[r-i-1])];
}
}
discr_r = index_of[discr_r]; /* Index form */
if (discr_r == A0) {
/* 2 lines below: B(x) <-- x*B(x) */
memmove(&b[1],b,NROOTS*sizeof(b[0]));
b[0] = A0;
} else {
/* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
t[0] = lambda[0];
for (i = 0 ; i < NROOTS; i++) {
if(b[i] != A0)
t[i+1] = lambda[i+1] ^ alpha_to[modnn(discr_r + b[i])];
else
t[i+1] = lambda[i+1];
}
if (2 * el <= r + no_eras - 1) {
el = r + no_eras - el;
/*
* 2 lines below: B(x) <-- inv(discr_r) *
* lambda(x)
*/
for (i = 0; i <= NROOTS; i++)
b[i] = (lambda[i] == 0) ? A0 : modnn(index_of[lambda[i]] - discr_r + NN);
} else {
/* 2 lines below: B(x) <-- x*B(x) */
memmove(&b[1],b,NROOTS*sizeof(b[0]));
b[0] = A0;
}
memcpy(lambda,t,(NROOTS+1)*sizeof(t[0]));
}
}
/* Convert lambda to index form and compute deg(lambda(x)) */
deg_lambda = 0;
for(i=0;i<NROOTS+1;i++){
lambda[i] = index_of[lambda[i]];
if(lambda[i] != A0)
deg_lambda = i;
}
/* Find roots of the error+erasure locator polynomial by Chien search */
memcpy(&reg[1],&lambda[1],NROOTS*sizeof(reg[0]));
count = 0; /* Number of roots of lambda(x) */
for (i = 1,k=IPRIM-1; i <= NN; i++,k = modnn(k+IPRIM)) {
q = 1; /* lambda[0] is always 0 */
for (j = deg_lambda; j > 0; j--){
if (reg[j] != A0) {
reg[j] = modnn(reg[j] + j);
q ^= alpha_to[reg[j]];
}
}
if (q != 0)
continue; /* Not a root */
/* store root (index-form) and error location number */
root[count] = i;
loc[count] = k;
/* If we've already found max possible roots,
* abort the search to save time
*/
if(++count == deg_lambda)
break;
}
if (deg_lambda != count) {
/*
* deg(lambda) unequal to number of roots => uncorrectable
* error detected
*/
count = -1;
goto finish;
}
/*
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
* x**NROOTS). in index form. Also find deg(omega).
*/
deg_omega = deg_lambda-1;
for (i = 0; i <= deg_omega;i++){
tmp = 0;
for(j=i;j >= 0; j--){
if ((s[i - j] != A0) && (lambda[j] != A0))
tmp ^= alpha_to[modnn(s[i - j] + lambda[j])];
}
omega[i] = index_of[tmp];
}
/*
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
* inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
*/
for (j = count-1; j >=0; j--) {
num1 = 0;
for (i = deg_omega; i >= 0; i--) {
if (omega[i] != A0)
num1 ^= alpha_to[modnn(omega[i] + i * root[j])];
}
num2 = alpha_to[modnn(root[j] * (FCR - 1) + NN)];
den = 0;
/* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
for (i = min(deg_lambda,NROOTS-1) & ~1; i >= 0; i -=2) {
if(lambda[i+1] != A0)
den ^= alpha_to[modnn(lambda[i+1] + i * root[j])];
}
/* Apply error to data */
if (num1 != 0 && loc[j] >= PAD) {
data[loc[j]-PAD] ^= alpha_to[modnn(index_of[num1] + index_of[num2] + NN - index_of[den])];
}
}
finish:
if(eras_pos != NULL){
for(i=0;i<count;i++)
eras_pos[i] = loc[i];
}
return count;
}
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define mm 8 /* RS code over GF(2**mm) - change to suit */
#define n 256 /* n = size of the field */
#define nn 182 /* nn=2**mm -1 length of codeword */
#define kk 172 /* kk = nn-2*tt */ /* Degree of g(x) = 2*tt */
//#define NN n-1
//#define FCR 0
//#define PRIM 1
#define _NROOTS nn-kk
//#define PAD NN-nn
//#define A0 NN
//#define IPRIM 1
const int NN = n-1;
const int FCR = 0;
const int PRIM = 1;
const int NROOTS = nn-kk;
const int PAD = (n-1)-nn;
const int A0 = n-1;
const int IPRIM = 1;
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
/**** Primitive polynomial ****/
int pp [mm+1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1}; /* 1+x^2+x^3+x^4+x^8 */
/* generator polynomial, tables for Galois field */
int alpha_to[n], index_of[n], gg[nn-kk+1];
int b0 = 1;
/* data[] is the info vector, bb[] is the parity vector, recd[] is the
noise corrupted received vector */
int recd[nn], data[kk], bb[nn-kk];
int modnn(int x){
while (x >= 0xff) {
x -= 0xff;
x = (x >> 0xff) + (x & 0xff);
}
return x;
}
void generate_gf()
{
register int i, mask ;
mask = 1 ;
alpha_to[mm] = 0 ;
for (i=0; i<mm; i++)
{ alpha_to[i] = mask ;
index_of[alpha_to[i]] = i ;
if (pp[i]!=0) /* If pp[i] == 1 then, term @^i occurs in poly-repr of @^mm */
alpha_to[mm] ^= mask ; /* Bit-wise EXOR operation */
mask <<= 1 ; /* single left-shift */
}
index_of[alpha_to[mm]] = mm ;
/* Have obtained poly-repr of @^mm. Poly-repr of @^(i+1) is given by
poly-repr of @^i shifted left one-bit and accounting for any @^mm
term that may occur when poly-repr of @^i is shifted. */
mask >>= 1 ;
for (i=mm+1; i<255; i++)
{ if (alpha_to[i-1] >= mask)
alpha_to[i] = alpha_to[mm] ^ ((alpha_to[i-1]^mask)<<1) ;
else alpha_to[i] = alpha_to[i-1]<<1 ;
index_of[alpha_to[i]] = i ;
}
index_of[0] = A0 ;//-1
}
void gen_poly()
/* Obtain the generator polynomial of the tt-error correcting, length */
{
register int i, j, root;
gg[0] = 1;
for (i = 0,root=0*1; i < nn-kk; i++,root += 1) {
gg[i+1] = 1;
for (j = i; j > 0; j--){
if (gg[j] != 0)
gg[j] = gg[j-1] ^ alpha_to[modnn(index_of[gg[j]] + root)];
else
gg[j] = gg[j-1];
}
gg[0] = alpha_to[modnn(index_of[gg[0]] + root)];
}
for (i=0; i <= nn-kk; i++) {
gg[i] = index_of[gg[i]];
}
}
void rs_encode(unsigned char *data, unsigned char *bb)
{
register int i,j ;
int feedback;
for (i=0; i<NROOTS; i++) bb[i] = 0; //nullify result
for(i=0;i<NN-NROOTS-PAD;i++){
feedback = index_of[data[i] ^ bb[0]];
if(feedback != A0){ /* feedback term is non-zero */
for(j=1;j<NROOTS;j++) {
bb[j] ^= alpha_to[modnn(feedback + gg[NROOTS-j])];
}
}
/* Shift */
memmove(&bb[0],&bb[1], NROOTS-1);
//for (j=0; j<NROOTS-1; j++) bb[j] = bb[j+1];
if(feedback != A0)
bb[NROOTS-1] = alpha_to[modnn(feedback + gg[0])];
else
bb[NROOTS-1] = 0;
}
}
///*
int rs_decode(unsigned char *data, int *eras_pos, int no_eras){
int deg_lambda, el, deg_omega;
int i, j, r,k;
unsigned char u,q,tmp,num1,num2,den,discr_r;
unsigned char lambda[_NROOTS+1], s[_NROOTS];
unsigned char b[_NROOTS+1], t[_NROOTS+1], omega[_NROOTS+1];
unsigned char root[_NROOTS], reg[_NROOTS+1], loc[_NROOTS];
int syn_error, count;
// form the syndromes; i.e., evaluate data(x) at roots of g(x)
for(i=0;i<NROOTS;i++)
s[i] = data[0];
for(j=1;j<NN-PAD;j++){
for(i=0;i<NROOTS;i++){
if(s[i] == 0){
s[i] = data[j];
} else {
s[i] = data[j] ^ alpha_to[modnn(index_of[s[i]] + (FCR+i)*PRIM)];
}
}
}
// Convert syndromes to index form, checking for nonzero condition
syn_error = 0;
for(i=0;i<NROOTS;i++){
syn_error |= s[i];
s[i] = index_of[s[i]];
}
if (!syn_error) {
// if syndrome is zero, data[] is a codeword and there are no
// errors to correct. So return data[] unmodified
//
count = 0;
goto finish;
}
memset(&lambda[1],0,NROOTS*sizeof(lambda[0]));
lambda[0] = 1;
if (no_eras > 0) {
/* Init lambda to be the erasure locator polynomial */
lambda[1] = alpha_to[modnn(PRIM*(NN-1-eras_pos[0]))];
for (i = 1; i < no_eras; i++) {
u = modnn(PRIM*(NN-1-eras_pos[i]));
for (j = i+1; j > 0; j--) {
tmp = index_of[lambda[j - 1]];
if(tmp != A0)
lambda[j] ^= alpha_to[modnn(u + tmp)];
}
}
}
for(i=0;i<NROOTS+1;i++)
b[i] = index_of[lambda[i]];
/*
* Begin Berlekamp-Massey algorithm to determine error+erasure
* locator polynomial
*/
r = no_eras;
el = no_eras;
while (++r <= NROOTS) { /* r is the step number */
/* Compute discrepancy at the r-th step in poly-form */
discr_r = 0;
for (i = 0; i < r; i++){
if ((lambda[i] != 0) && (s[r-i-1] != A0)) {
discr_r ^= alpha_to[modnn(index_of[lambda[i]] + s[r-i-1])];
}
}
discr_r = index_of[discr_r]; /* Index form */
if (discr_r == A0) {
/* 2 lines below: B(x) <-- x*B(x) */
memmove(&b[1],b,NROOTS*sizeof(b[0]));
b[0] = A0;
} else {
/* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
t[0] = lambda[0];
for (i = 0 ; i < NROOTS; i++) {
if(b[i] != A0)
t[i+1] = lambda[i+1] ^ alpha_to[modnn(discr_r + b[i])];
else
t[i+1] = lambda[i+1];
}
if (2 * el <= r + no_eras - 1) {
el = r + no_eras - el;
/*
* 2 lines below: B(x) <-- inv(discr_r) *
* lambda(x)
*/
for (i = 0; i <= NROOTS; i++)
b[i] = (lambda[i] == 0) ? A0 : modnn(index_of[lambda[i]] - discr_r + NN);
} else {
/* 2 lines below: B(x) <-- x*B(x) */
memmove(&b[1],b,NROOTS*sizeof(b[0]));
b[0] = A0;
}
memcpy(lambda,t,(NROOTS+1)*sizeof(t[0]));
}
}
/* Convert lambda to index form and compute deg(lambda(x)) */
deg_lambda = 0;
for(i=0;i<NROOTS+1;i++){
lambda[i] = index_of[lambda[i]];
if(lambda[i] != A0)
deg_lambda = i;
}
/* Find roots of the error+erasure locator polynomial by Chien search */
memcpy(&reg[1],&lambda[1],NROOTS*sizeof(reg[0]));
count = 0; /* Number of roots of lambda(x) */
for (i = 1,k=IPRIM-1; i <= NN; i++,k = modnn(k+IPRIM)) {
q = 1; /* lambda[0] is always 0 */
for (j = deg_lambda; j > 0; j--){
if (reg[j] != A0) {
reg[j] = modnn(reg[j] + j);
q ^= alpha_to[reg[j]];
}
}
if (q != 0)
continue; /* Not a root */
/* store root (index-form) and error location number */
root[count] = i;
loc[count] = k;
/* If we've already found max possible roots,
* abort the search to save time
*/
if(++count == deg_lambda)
break;
}
if (deg_lambda != count) {
/*
* deg(lambda) unequal to number of roots => uncorrectable
* error detected
*/
count = -1;
goto finish;
}
/*
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
* x**NROOTS). in index form. Also find deg(omega).
*/
deg_omega = deg_lambda-1;
for (i = 0; i <= deg_omega;i++){
tmp = 0;
for(j=i;j >= 0; j--){
if ((s[i - j] != A0) && (lambda[j] != A0))
tmp ^= alpha_to[modnn(s[i - j] + lambda[j])];
}
omega[i] = index_of[tmp];
}
/*
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
* inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
*/
for (j = count-1; j >=0; j--) {
num1 = 0;
for (i = deg_omega; i >= 0; i--) {
if (omega[i] != A0)
num1 ^= alpha_to[modnn(omega[i] + i * root[j])];
}
num2 = alpha_to[modnn(root[j] * (FCR - 1) + NN)];
den = 0;
/* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
for (i = min(deg_lambda,NROOTS-1) & ~1; i >= 0; i -=2) {
if(lambda[i+1] != A0)
den ^= alpha_to[modnn(lambda[i+1] + i * root[j])];
}
/* Apply error to data */
if (num1 != 0 && loc[j] >= PAD) {
data[loc[j]-PAD] ^= alpha_to[modnn(index_of[num1] + index_of[num2] + NN - index_of[den])];
}
}
finish:
if(eras_pos != NULL){
for(i=0;i<count;i++)
eras_pos[i] = loc[i];
}
return count;
}

View file

@ -1,10 +1,10 @@
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int modnn(int x);
void generate_gf();
void gen_poly();
void rs_encode(unsigned char *data, unsigned char *bb);
int rs_decode(unsigned char *data, int *eras_pos, int no_eras);
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int modnn(int x);
void generate_gf();
void gen_poly();
void rs_encode(unsigned char *data, unsigned char *bb);
int rs_decode(unsigned char *data, int *eras_pos, int no_eras);

View file

@ -1,370 +1,370 @@
/***************************************************************************
* Copyright (C) 2007 by Arep *
* Support is provided through the forums at *
* http://wii.console-tribe.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*! \file
* \brief Unscrambler for Nintendo GameCube/Wii discs.
*
* As Nintendo GameCube/Wii discs use the standars DVD-ROM scrambling algorithm, but with different, unknown, seeds, the actual seeds have to be brute-forced.
* The functions in this file take care of the brute-forcing and of the actual unscrambling of the read sectors.
*
* The code in this file has been derived from unscrambler 0.4, Copyright (C) 2006 Victor Muñoz (xt5@ingenieria-inversa.cl), GPL v2+,
* http://www.ingenieria-inversa.cl/?lp_lang_pref=en .
*/
#include "misc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "constants.h"
#include "byteorder.h"
#include "ecma-267.h"
#include "unscrambler.h"
// #define unscramblerdebug(...) debug (__VA_ARGS__);
#define unscramblerdebug(...)
/*! \brief Size of the seeds cache (Do not touch) */
#define MAX_SEEDS 4
/*! \brief Number of bytes of a sector on which the EDC is calculated */
#define EDC_LENGTH (RAW_SECTOR_SIZE - 4) /* The EDC value is contained in the bottom 4 bytes of a frame */
/*! \brief A structure that represents a seed
*/
typedef struct t_seed {
int seed; //!< The seed, in numeric format.
unsigned char streamcipher[SECTOR_SIZE]; //!< The stream cipher generated from the seed through the LFSR.
} t_seed;
/*! \brief A structure that represents an unscrambler
*/
struct unscrambler_s {
t_seed seeds[(MAX_SEEDS + 1) * 16]; //!< The seeds cache.
bool bruteforce_seeds; //!< If true, whenever a seed for a sector is not cached, it will be found via a bruteforce attack, otherwise an error will be returned.
};
void unscrambler_set_disctype (u_int8_t disc_type){
disctype = disc_type;
// fprintf (stdout,"%d",disctype);
}
/**
* Adds a seed to the cache, calculating its streamcipher.
* @param seeds The seed cache.
* @param seed The seed to add.
* @return A structure representing the added seed, or NULL if it could not be added.
*/
static t_seed *add_seed (t_seed *seeds, unsigned short seed) {
int i;
t_seed *out;
unscramblerdebug ("Caching seed %04x\n", seed);
if (seeds -> seed == -2) {
out = NULL;
} else {
seeds -> seed = seed;
LFSR_init (seed);
for (i = 0; i < SECTOR_SIZE; i++)
seeds -> streamcipher[i] = LFSR_byte ();
out = seeds;
}
return (out);
}
/**
* Tests if the specified seed is the one used for the specified sector block: the check is done comparing the generated EDC with the one at the bottom of each
* sector. Sectors are processed in blocks, as the same seed is used for 16 consecutive sectors.
* @param buf The sector.
* @param j The seed.
* @return true if the seed is correct, false otherwise.
*/
static bool test_seed (u_int8_t *buf, int j) {
int i;
u_int8_t tmp[RAW_SECTOR_SIZE];
u_int32_t edc_calculated, edc_correct;
bool out;
memcpy (tmp, buf, RAW_SECTOR_SIZE);
LFSR_init (j);
for (i = 12; i < EDC_LENGTH; i++)
tmp[i] ^= LFSR_byte ();
edc_calculated = edc_calc (0x00000000, tmp, EDC_LENGTH);
edc_correct = my_ntohl (*((u_int32_t *) (&tmp[EDC_LENGTH])));
if (edc_calculated == edc_correct)
out = true;
else
out = false;
return (out);
}
/**
* Unscramble a complete block, using an already-cached seed.
* @param seed The seed to use for the unscrambling.
* @param _bin The 16-sector block to unscramble (RAW_BLOCK_SIZE).
* @param _bout The unscrambled 16-sector block (BLOCK_SIZE).
* @return True if the unscrambling was successful, false otherwise.
*/
static bool unscramble_frame (t_seed *seed, u_int8_t *_bin, u_int8_t *_bout) {
int i, j;
u_int8_t tmp[RAW_SECTOR_SIZE], *bin, *bout;
u_int32_t *_4bin, *_4cipher, edc_calculated, edc_correct;
bool out;
out = true;
for(j = 0; j < 16; j++) {
bin = &_bin[RAW_SECTOR_SIZE * j];
bout = &_bout[SECTOR_SIZE * j];
memcpy (tmp, bin, RAW_SECTOR_SIZE);
_4bin = (u_int32_t *) &tmp[12]; /* Scrambled data begin at byte 12 */
_4cipher = (u_int32_t *) seed -> streamcipher;
for (i = 0; i < 512; i++) /* Well, the scrambling algorithm is just a bitwise XOR... */
_4bin[i] ^= _4cipher[i];
//memcpy (bout, tmp + 6, SECTOR_SIZE); // copy CPR_MAI bytes
if (disctype==3) { //Regular
memcpy (bout, tmp + 12, SECTOR_SIZE); // DVD: copy 2048 bytes (starting from CPR_MAI)
}
else { //Nintendo
memcpy (bout, tmp + 6, SECTOR_SIZE); // Nintendo: copy 2048 bytes (up to CPR_MAI)
memcpy (&_bin[(RAW_SECTOR_SIZE * j)+2054], &tmp[2054], 6);
}
edc_calculated = edc_calc (0x00000000, tmp, EDC_LENGTH);
edc_correct = my_ntohl (*((u_int32_t *) (&tmp[EDC_LENGTH])));
if (edc_calculated != edc_correct) {
debug ("Bad EDC (%08x), must be %08x (sector = %d)", edc_calculated, edc_correct, j);
out = false;
}
}
return (out);
}
/**
* Initializes the seed cache.
* @param u The unscrambler structure.
*/
static void unscrambler_init_seeds (unscrambler *u) {
int i, j;
for (i = 0; i < 16; i++) {
for (j = 0; j < MAX_SEEDS; j++)
u -> seeds[i * MAX_SEEDS + j].seed = -1;
u -> seeds[i * MAX_SEEDS + j].seed = -2; // TODO Check what this does
}
return;
}
/**
* Creates a new structure representing an unscrambler.
* @return The newly-created structure, to be used with the other commands.
*/
unscrambler *unscrambler_new (void) {
unscrambler *u;
u = (unscrambler *) malloc (sizeof (unscrambler));
unscrambler_init_seeds (u);
u -> bruteforce_seeds = true;
return (u);
}
/**
* Frees resources used by an unscrambler structure and destroys it.
* @param u The unscrambler structure.
* @return NULL.
*/
void *unscrambler_destroy (unscrambler *u) {
my_free (u);
return (NULL);
}
void unscrambler_set_bruteforce (unscrambler *u, bool b) {
u -> bruteforce_seeds = b;
debug ("Seed bruteforcing %s", b ? "enabled" : "disabled");
return;
}
/**
* Unscrambles a 16-sector block.
* @param u The unscrambler structure.
* @param sector_no The number of the first sector in the block.
* @param inbuf The 16-sector block to unscramble. Each block must be RAW_SECTOR_SIZE bytes long, so that the total size is RAW_BLOCK_SIZE.
* @param outbuf The unscrambled 16-sector block. Each block will be SECTOR_SIZE bytes long, so that the total size is BLOCK_SIZE.
* @return True if the unscrambling was successful, false otherwise.
*/
bool unscrambler_unscramble_16sectors (unscrambler *u, u_int32_t sector_no, u_int8_t *inbuf, u_int8_t *outbuf) {
t_seed *seeds;
t_seed *current_seed;
int j;
bool out;
out = true;
seeds = &(u -> seeds[((sector_no / 16) & 0x0F) * MAX_SEEDS]);
/* Try to find the seed used for this sector */
current_seed = NULL;
while (!current_seed && (seeds -> seed) >= 0) {
if (test_seed (inbuf, seeds -> seed))
current_seed = seeds;
else
seeds++;
}
if (!current_seed && u -> bruteforce_seeds) {
/* The seed is not cached, yet. Try to find it with brute force... */
unscramblerdebug ("Brute-forcing seed for sector %d...", sector_no);
for (j = 0; !current_seed && j < 0x7FFF; j++) {
if (test_seed (inbuf, j)) {
if (!(current_seed = add_seed (seeds, j))) {
error ("No enough cache space for caching seed");
out = false;
}
}
}
if (current_seed)
unscramblerdebug ("Seed found: %04x", --j);
}
if (current_seed) {
/* OK, somehow seed was found: unscramble frame, write it and go on */
if (!unscramble_frame (current_seed, inbuf, outbuf)) {
error ("Error unscrambling frame %u\n", sector_no);
out = false;
} else {
out = true;
}
} else {
/* Well, we only get here if there are read errors */
error ("Cannot find seed for frame %u", sector_no);
out = false;
}
return (out);
}
/**
* Unscrambles a complete file.
* @param u The unscrambler structure.
* @param infile The input file name.
* @param outfile The output file name.
* @param progress A function to be called repeatedly during the operation, useful to report progress data/statistics.
* @param progress_data Data to be passed as-is to the progress function.
* @return True if the unscrambling was successful, false otherwise.
*/
bool unscrambler_unscramble_file (unscrambler *u, char *infile, char *outfile, unscrambler_progress_func progress, void *progress_data, u_int32_t *current_sector) {
FILE *in, *outfp;
bool out;
u_int8_t b_in[RAW_BLOCK_SIZE], b_out[BLOCK_SIZE];
size_t r;
my_off_t filesize;
int s;
u_int32_t total_sectors;
out = false;
if(!(in = fopen (infile ? infile : "", "rb"))) {
error ("Cannot open input file \"%s\"", infile);
} else if (!(outfp = fopen (outfile ? outfile : "", "wb"))) {
error ("Cannot open output file \"%s\"", outfile);
fclose (in);
} else {
/* Find out how many sectors we need to process */
my_fseek (in, 0, SEEK_END);
filesize = my_ftell (in);
total_sectors = (u_int32_t) (filesize / RAW_SECTOR_SIZE);
rewind (in);
/* First call to progress function */
if (progress)
progress (true, 0, total_sectors, progress_data);
s = 0, out = true;
while ((r = fread (b_in, 1, RAW_BLOCK_SIZE, in)) > 0 && out) {
if (r < RAW_BLOCK_SIZE) {
warning ("Short block read (%u bytes), padding with zeroes!", r);
memset (b_in + r, 0, sizeof (b_in) - r);
}
if (unscrambler_unscramble_16sectors (u, s, b_in, b_out)) {
clearerr (outfp);
if (!b_out) {
error ("NULL buffer");
out = false;
*(current_sector) = s;
}
else fwrite (b_out, SECTOR_SIZE, SECTORS_PER_BLOCK, outfp);
if (ferror (outfp)) {
error ("fwrite() to ISO output file failed");
out = false;
*(current_sector) = s;
}
} else {
debug ("unscrambler_unscramble_16sectors() failed");
out = false;
*(current_sector) = s;
}
s += 16;
if ((s % 320 == 0) || (s == total_sectors)) { //speedhack
if (progress)
progress (false, s, total_sectors, progress_data);
}
}
if (out) {
debug ("Image successfully unscrambled");
}
fclose (in);
fclose (outfp);
}
return (out);
}
/***************************************************************************
* Copyright (C) 2007 by Arep *
* Support is provided through the forums at *
* http://wii.console-tribe.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*! \file
* \brief Unscrambler for Nintendo GameCube/Wii discs.
*
* As Nintendo GameCube/Wii discs use the standars DVD-ROM scrambling algorithm, but with different, unknown, seeds, the actual seeds have to be brute-forced.
* The functions in this file take care of the brute-forcing and of the actual unscrambling of the read sectors.
*
* The code in this file has been derived from unscrambler 0.4, Copyright (C) 2006 Victor Muñoz (xt5@ingenieria-inversa.cl), GPL v2+,
* http://www.ingenieria-inversa.cl/?lp_lang_pref=en .
*/
#include "misc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "constants.h"
#include "byteorder.h"
#include "ecma-267.h"
#include "unscrambler.h"
// #define unscramblerdebug(...) debug (__VA_ARGS__);
#define unscramblerdebug(...)
/*! \brief Size of the seeds cache (Do not touch) */
#define MAX_SEEDS 4
/*! \brief Number of bytes of a sector on which the EDC is calculated */
#define EDC_LENGTH (RAW_SECTOR_SIZE - 4) /* The EDC value is contained in the bottom 4 bytes of a frame */
/*! \brief A structure that represents a seed
*/
typedef struct t_seed {
int seed; //!< The seed, in numeric format.
unsigned char streamcipher[SECTOR_SIZE]; //!< The stream cipher generated from the seed through the LFSR.
} t_seed;
/*! \brief A structure that represents an unscrambler
*/
struct unscrambler_s {
t_seed seeds[(MAX_SEEDS + 1) * 16]; //!< The seeds cache.
bool bruteforce_seeds; //!< If true, whenever a seed for a sector is not cached, it will be found via a bruteforce attack, otherwise an error will be returned.
};
void unscrambler_set_disctype (u_int8_t disc_type){
disctype = disc_type;
// fprintf (stdout,"%d",disctype);
}
/**
* Adds a seed to the cache, calculating its streamcipher.
* @param seeds The seed cache.
* @param seed The seed to add.
* @return A structure representing the added seed, or NULL if it could not be added.
*/
static t_seed *add_seed (t_seed *seeds, unsigned short seed) {
int i;
t_seed *out;
unscramblerdebug ("Caching seed %04x\n", seed);
if (seeds -> seed == -2) {
out = NULL;
} else {
seeds -> seed = seed;
LFSR_init (seed);
for (i = 0; i < SECTOR_SIZE; i++)
seeds -> streamcipher[i] = LFSR_byte ();
out = seeds;
}
return (out);
}
/**
* Tests if the specified seed is the one used for the specified sector block: the check is done comparing the generated EDC with the one at the bottom of each
* sector. Sectors are processed in blocks, as the same seed is used for 16 consecutive sectors.
* @param buf The sector.
* @param j The seed.
* @return true if the seed is correct, false otherwise.
*/
static bool test_seed (u_int8_t *buf, int j) {
int i;
u_int8_t tmp[RAW_SECTOR_SIZE];
u_int32_t edc_calculated, edc_correct;
bool out;
memcpy (tmp, buf, RAW_SECTOR_SIZE);
LFSR_init (j);
for (i = 12; i < EDC_LENGTH; i++)
tmp[i] ^= LFSR_byte ();
edc_calculated = edc_calc (0x00000000, tmp, EDC_LENGTH);
edc_correct = my_ntohl (*((u_int32_t *) (&tmp[EDC_LENGTH])));
if (edc_calculated == edc_correct)
out = true;
else
out = false;
return (out);
}
/**
* Unscramble a complete block, using an already-cached seed.
* @param seed The seed to use for the unscrambling.
* @param _bin The 16-sector block to unscramble (RAW_BLOCK_SIZE).
* @param _bout The unscrambled 16-sector block (BLOCK_SIZE).
* @return True if the unscrambling was successful, false otherwise.
*/
static bool unscramble_frame (t_seed *seed, u_int8_t *_bin, u_int8_t *_bout) {
int i, j;
u_int8_t tmp[RAW_SECTOR_SIZE], *bin, *bout;
u_int32_t *_4bin, *_4cipher, edc_calculated, edc_correct;
bool out;
out = true;
for(j = 0; j < 16; j++) {
bin = &_bin[RAW_SECTOR_SIZE * j];
bout = &_bout[SECTOR_SIZE * j];
memcpy (tmp, bin, RAW_SECTOR_SIZE);
_4bin = (u_int32_t *) &tmp[12]; /* Scrambled data begin at byte 12 */
_4cipher = (u_int32_t *) seed -> streamcipher;
for (i = 0; i < 512; i++) /* Well, the scrambling algorithm is just a bitwise XOR... */
_4bin[i] ^= _4cipher[i];
//memcpy (bout, tmp + 6, SECTOR_SIZE); // copy CPR_MAI bytes
if (disctype==3) { //Regular
memcpy (bout, tmp + 12, SECTOR_SIZE); // DVD: copy 2048 bytes (starting from CPR_MAI)
}
else { //Nintendo
memcpy (bout, tmp + 6, SECTOR_SIZE); // Nintendo: copy 2048 bytes (up to CPR_MAI)
memcpy (&_bin[(RAW_SECTOR_SIZE * j)+2054], &tmp[2054], 6);
}
edc_calculated = edc_calc (0x00000000, tmp, EDC_LENGTH);
edc_correct = my_ntohl (*((u_int32_t *) (&tmp[EDC_LENGTH])));
if (edc_calculated != edc_correct) {
debug ("Bad EDC (%08x), must be %08x (sector = %d)", edc_calculated, edc_correct, j);
out = false;
}
}
return (out);
}
/**
* Initializes the seed cache.
* @param u The unscrambler structure.
*/
static void unscrambler_init_seeds (unscrambler *u) {
int i, j;
for (i = 0; i < 16; i++) {
for (j = 0; j < MAX_SEEDS; j++)
u -> seeds[i * MAX_SEEDS + j].seed = -1;
u -> seeds[i * MAX_SEEDS + j].seed = -2; // TODO Check what this does
}
return;
}
/**
* Creates a new structure representing an unscrambler.
* @return The newly-created structure, to be used with the other commands.
*/
unscrambler *unscrambler_new (void) {
unscrambler *u;
u = (unscrambler *) malloc (sizeof (unscrambler));
unscrambler_init_seeds (u);
u -> bruteforce_seeds = true;
return (u);
}
/**
* Frees resources used by an unscrambler structure and destroys it.
* @param u The unscrambler structure.
* @return NULL.
*/
void *unscrambler_destroy (unscrambler *u) {
my_free (u);
return (NULL);
}
void unscrambler_set_bruteforce (unscrambler *u, bool b) {
u -> bruteforce_seeds = b;
debug ("Seed bruteforcing %s", b ? "enabled" : "disabled");
return;
}
/**
* Unscrambles a 16-sector block.
* @param u The unscrambler structure.
* @param sector_no The number of the first sector in the block.
* @param inbuf The 16-sector block to unscramble. Each block must be RAW_SECTOR_SIZE bytes long, so that the total size is RAW_BLOCK_SIZE.
* @param outbuf The unscrambled 16-sector block. Each block will be SECTOR_SIZE bytes long, so that the total size is BLOCK_SIZE.
* @return True if the unscrambling was successful, false otherwise.
*/
bool unscrambler_unscramble_16sectors (unscrambler *u, u_int32_t sector_no, u_int8_t *inbuf, u_int8_t *outbuf) {
t_seed *seeds;
t_seed *current_seed;
int j;
bool out;
out = true;
seeds = &(u -> seeds[((sector_no / 16) & 0x0F) * MAX_SEEDS]);
/* Try to find the seed used for this sector */
current_seed = NULL;
while (!current_seed && (seeds -> seed) >= 0) {
if (test_seed (inbuf, seeds -> seed))
current_seed = seeds;
else
seeds++;
}
if (!current_seed && u -> bruteforce_seeds) {
/* The seed is not cached, yet. Try to find it with brute force... */
unscramblerdebug ("Brute-forcing seed for sector %d...", sector_no);
for (j = 0; !current_seed && j < 0x7FFF; j++) {
if (test_seed (inbuf, j)) {
if (!(current_seed = add_seed (seeds, j))) {
error ("No enough cache space for caching seed");
out = false;
}
}
}
if (current_seed)
unscramblerdebug ("Seed found: %04x", --j);
}
if (current_seed) {
/* OK, somehow seed was found: unscramble frame, write it and go on */
if (!unscramble_frame (current_seed, inbuf, outbuf)) {
error ("Error unscrambling frame %u\n", sector_no);
out = false;
} else {
out = true;
}
} else {
/* Well, we only get here if there are read errors */
error ("Cannot find seed for frame %u", sector_no);
out = false;
}
return (out);
}
/**
* Unscrambles a complete file.
* @param u The unscrambler structure.
* @param infile The input file name.
* @param outfile The output file name.
* @param progress A function to be called repeatedly during the operation, useful to report progress data/statistics.
* @param progress_data Data to be passed as-is to the progress function.
* @return True if the unscrambling was successful, false otherwise.
*/
bool unscrambler_unscramble_file (unscrambler *u, char *infile, char *outfile, unscrambler_progress_func progress, void *progress_data, u_int32_t *current_sector) {
FILE *in, *outfp;
bool out;
u_int8_t b_in[RAW_BLOCK_SIZE], b_out[BLOCK_SIZE];
size_t r;
my_off_t filesize;
int s;
u_int32_t total_sectors;
out = false;
if(!(in = fopen (infile ? infile : "", "rb"))) {
error ("Cannot open input file \"%s\"", infile);
} else if (!(outfp = fopen (outfile ? outfile : "", "wb"))) {
error ("Cannot open output file \"%s\"", outfile);
fclose (in);
} else {
/* Find out how many sectors we need to process */
my_fseek (in, 0, SEEK_END);
filesize = my_ftell (in);
total_sectors = (u_int32_t) (filesize / RAW_SECTOR_SIZE);
rewind (in);
/* First call to progress function */
if (progress)
progress (true, 0, total_sectors, progress_data);
s = 0, out = true;
while ((r = fread (b_in, 1, RAW_BLOCK_SIZE, in)) > 0 && out) {
if (r < RAW_BLOCK_SIZE) {
warning ("Short block read (%u bytes), padding with zeroes!", r);
memset (b_in + r, 0, sizeof (b_in) - r);
}
if (unscrambler_unscramble_16sectors (u, s, b_in, b_out)) {
clearerr (outfp);
if (!b_out) {
error ("NULL buffer");
out = false;
*(current_sector) = s;
}
else fwrite (b_out, SECTOR_SIZE, SECTORS_PER_BLOCK, outfp);
if (ferror (outfp)) {
error ("fwrite() to ISO output file failed");
out = false;
*(current_sector) = s;
}
} else {
debug ("unscrambler_unscramble_16sectors() failed");
out = false;
*(current_sector) = s;
}
s += 16;
if ((s % 320 == 0) || (s == total_sectors)) { //speedhack
if (progress)
progress (false, s, total_sectors, progress_data);
}
}
if (out) {
debug ("Image successfully unscrambled");
}
fclose (in);
fclose (outfp);
}
return (out);
}