Reformat code.

This commit is contained in:
2020-05-03 16:04:20 +01:00
parent b012779b4d
commit f3b778c5df
3 changed files with 422 additions and 382 deletions

View File

@@ -1,65 +1,68 @@
////////////////////////////////////////////////////////////////////////////////
void banner_ok(void) {
void banner_ok(void)
{
printf(TITLE "\n"
#include "version.h"
" (%d-bit "
" (%d-bit "
#if defined(__CYGWIN__)
"Windows, Cygwin"
"Windows, Cygwin"
#elif defined(__MINGW32__)
"Windows, MinGW"
"Windows, MinGW"
#elif defined(_WIN32) && defined(_MSC_VER) && (defined(__alpha) || defined(__ALPHA) || defined(__Alpha_AXP))
"Windows, Digital AXP C"
"Windows, Digital AXP C"
#elif defined(_WIN32) && defined(_MSC_VER) && defined(_M_ALPHA)
"Windows, Microsoft C, Alpha"
"Windows, Microsoft C, Alpha"
#elif defined(_WIN32) && defined(_MSC_VER) && defined(_M_MRX000)
"Windows, Microsoft C, MIPS"
"Windows, Microsoft C, MIPS"
#elif defined(_WIN32) && defined(_MSC_VER)
"Windows, Microsoft C"
"Windows, Microsoft C"
#elif defined(__WIN32__) || defined(_WIN32)
"Windows"
"Windows"
#elif defined(__DJGPP__)
"DOS, DJGPP"
"DOS, DJGPP"
#elif defined(__MSDOS__) && defined(__TURBOC__)
"DOS, Turbo C"
"DOS, Turbo C"
#elif defined(_DOS) && defined(__WATCOMC__)
"DOS, Watcom"
"DOS, Watcom"
#elif defined(__MSDOS__) || defined(MSDOS) || defined(_DOS)
"DOS"
"DOS"
#elif defined(__APPLE__)
"Mac OS"
"Mac OS"
#elif defined(__linux) || defined(__linux__) || defined(__gnu_linux__) || defined(linux)
"Linux"
"Linux"
#elif defined(__OpenBSD__)
"OpenBSD"
"OpenBSD"
#elif defined(BSD)
"BSD"
#elif defined(human68k) || defined(HUMAN68K) || defined(__human68k) || defined(__HUMAN68K) || defined(__human68k__) || defined(__HUMAN68K__)
"Human68k"
"BSD"
#elif defined(human68k) || defined(HUMAN68K) || defined(__human68k) || defined(__HUMAN68K) || defined(__human68k__) || \
defined(__HUMAN68K__)
"Human68k"
#elif defined(__unix__) || defined(__unix) || defined(unix)
"unknown Unix"
"unknown Unix"
#else
"unknown platform"
"unknown platform"
#endif
"%s)\n"
" " COPYR "\n"
" http://www.claunia.com/\n"
" based on ecm v1.03\n"
" Copyright (C) 2002-2011 Neill Corlett"
"\n",
(int)(sizeof(size_t) * 8),
(sizeof(off_t) > 4 && sizeof(off_t) > sizeof(size_t)) ? ", large file support" : ""
);
"%s)\n"
" " COPYR "\n"
" http://www.claunia.com/\n"
" based on ecm v1.03\n"
" Copyright (C) 2002-2011 Neill Corlett"
"\n",
(int)(sizeof(size_t) * 8),
(sizeof(off_t) > 4 && sizeof(off_t) > sizeof(size_t)) ? ", large file support" : "");
}
void banner_error(void) {
void banner_error(void)
{
printf("Configuration error\n");
exit(1);
}
static void banner(void) {
static void banner(void)
{
((sizeof(off_t) >= sizeof(size_t)) ? banner_ok : banner_error)();
//
// If we've displayed the banner, we'll also want to warn that this is a

317
common.h
View File

@@ -39,16 +39,17 @@ int _dowildcard = -1;
////////////////////////////////////////////////////////////////////////////////
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
// MSC toolchains use sys/utime.h; everything else uses utime.h
#if defined(_MSC_VER)
@@ -69,7 +70,7 @@ int _dowildcard = -1;
// Fill in S_ISDIR
#if !defined(_POSIX_VERSION) && !defined(S_ISDIR)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
#endif
#if defined(__TURBOC__) || defined(__WATCOMC__) || defined(__MINGW32__) || defined(_MSC_VER)
@@ -112,11 +113,11 @@ int _dowildcard = -1;
#elif defined(_MSC_VER)
// On Visual Studio, use its integral types
typedef signed __int8 int8_t;
typedef signed __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef signed __int16 int16_t;
typedef signed __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef signed __int32 int32_t;
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
#else
@@ -171,10 +172,10 @@ typedef unsigned short uint16_t;
// int32_t
//
#ifndef __int32_t_defined
#if INT_MIN == -2147483648 && INT_MAX == 2147483647 && UINT_MAX == 4294967295
#if INT_MIN == -2147483648 && INT_MAX == 2147483647 && UINT_MAX == 4294967295
typedef signed int int32_t;
#elif LONG_MIN == -2147483648 && LONG_MAX == 2147483647 && ULONG_MAX == 4294967295
typedef signed long int32_t;
typedef signed long int32_t;
#else
#error Unknown how to define int32_t!
#endif
@@ -184,7 +185,7 @@ typedef signed long int32_t;
// uint32_t
//
#ifndef __uint32_t_defined
#if INT_MIN == -2147483648 && INT_MAX == 2147483647 && UINT_MAX == 4294967295
#if INT_MIN == -2147483648 && INT_MAX == 2147483647 && UINT_MAX == 4294967295
typedef unsigned int uint32_t;
#elif LONG_MIN == -2147483648 && LONG_MAX == 2147483647 && ULONG_MAX == 4294967295
typedef unsigned long uint32_t;
@@ -256,113 +257,125 @@ typedef unsigned long uint32_t;
// Add the ability to read off_t
// (assumes off_t is a signed type)
//
off_t strtoofft(const char* s_start, char** endptr, int base) {
off_t max =
((((off_t)1) << ((sizeof(off_t)*8)-2)) - 1) +
((((off_t)1) << ((sizeof(off_t)*8)-2)) );
off_t min = ((-1) - max);
const char* s = s_start;
off_t accumulator;
off_t limit_tens;
off_t limit_ones;
int c;
int negative = 0;
int anyinput;
do {
off_t strtoofft(const char *s_start, char **endptr, int base)
{
off_t max = ((((off_t)1) << ((sizeof(off_t) * 8) - 2)) - 1) + ((((off_t)1) << ((sizeof(off_t) * 8) - 2)));
off_t min = ((-1) - max);
const char *s = s_start;
off_t accumulator;
off_t limit_tens;
off_t limit_ones;
int c;
int negative = 0;
int anyinput;
do
{
c = *s++;
} while(isspace(c));
if(c == '-') {
if(c == '-')
{
negative = 1;
c = *s++;
} else if (c == '+') {
c = *s++;
}
else if(c == '+')
{
c = *s++;
}
if(
(base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')
) {
if((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X'))
{
c = s[1];
s += 2;
base = 16;
}
if(!base) {
base = (c == '0') ? 8 : 10;
}
if(!base) { base = (c == '0') ? 8 : 10; }
limit_ones = max % ((off_t)base);
limit_tens = max / ((off_t)base);
if(negative) {
if(negative)
{
limit_ones++;
if(limit_ones >= base) { limit_ones = 0; limit_tens++; }
if(limit_ones >= base)
{
limit_ones = 0;
limit_tens++;
}
}
for(accumulator = 0, anyinput = 0;; c = *s++) {
if(isdigit(c)) {
c -= '0';
} else if(isalpha(c)) {
for(accumulator = 0, anyinput = 0;; c = *s++)
{
if(isdigit(c)) { c -= '0'; }
else if(isalpha(c))
{
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
} else {
}
else
{
break;
}
if(c >= base) { break; }
if(
(anyinput < 0) ||
(accumulator < 0) ||
(accumulator > limit_tens) ||
(accumulator == limit_tens && c > limit_ones)
) {
anyinput = -1;
} else {
if((anyinput < 0) || (accumulator < 0) || (accumulator > limit_tens) ||
(accumulator == limit_tens && c > limit_ones))
{ anyinput = -1; }
else
{
anyinput = 1;
accumulator *= base;
accumulator += c;
}
}
if(anyinput < 0) {
if(anyinput < 0)
{
accumulator = negative ? min : max;
errno = ERANGE;
} else if(negative) {
errno = ERANGE;
}
else if(negative)
{
accumulator = -accumulator;
}
if(endptr) {
*endptr = (char*)(anyinput ? (char*)s - 1 : s_start);
}
if(endptr) { *endptr = (char *)(anyinput ? (char *)s - 1 : s_start); }
return accumulator;
}
//
// Add the ability to print off_t
//
void fprinthex(FILE* f, off_t off, int min_digits) {
void fprinthex(FILE *f, off_t off, int min_digits)
{
unsigned anydigit = 0;
int place;
for(place = 2 * sizeof(off_t) - 1; place >= 0; place--) {
if(sizeof(off_t) > (((size_t)(place)) / 2)) {
int place;
for(place = 2 * sizeof(off_t) - 1; place >= 0; place--)
{
if(sizeof(off_t) > (((size_t)(place)) / 2))
{
unsigned digit = (off >> (4 * place)) & 0xF;
anydigit |= digit;
if(anydigit || place < min_digits) {
fputc("0123456789ABCDEF"[digit], f);
}
if(anydigit || place < min_digits) { fputc("0123456789ABCDEF"[digit], f); }
}
}
}
static void fprintdec_digit(FILE* f, off_t off) {
static void fprintdec_digit(FILE *f, off_t off)
{
if(off == 0) { return; }
if(off >= 10) {
if(off >= 10)
{
fprintdec_digit(f, off / ((off_t)10));
off %= ((off_t)10);
}
fputc('0' + off, f);
}
void fprintdec(FILE* f, off_t off) {
if(off == 0) {
void fprintdec(FILE *f, off_t off)
{
if(off == 0)
{
fputc('0', f);
return;
}
if(off < 0) {
if(off < 0)
{
fputc('-', f);
off = -off;
if(off < 0) {
if(off < 0)
{
off_t ones = off % ((off_t)10);
off /= ((off_t)10);
off = -off;
@@ -380,32 +393,37 @@ void fprintdec(FILE* f, off_t off) {
//
#if !defined(_POSIX_VERSION)
#if (defined(__MSDOS__) || defined(MSDOS)) && (defined(__TURBOC__) || defined(__WATCOMC__))
#if(defined(__MSDOS__) || defined(MSDOS)) && (defined(__TURBOC__) || defined(__WATCOMC__))
#include <dos.h>
#include <io.h>
#include <fcntl.h>
int truncate(const char *filename, off_t size) {
if(size < 0) {
#include <io.h>
int truncate(const char *filename, off_t size)
{
if(size < 0)
{
errno = EINVAL;
return -1;
}
//
// Extend (or do nothing) if necessary
//
{ off_t end;
FILE* f = fopen(filename, "rb");
if(!f) {
return -1;
}
if(fseeko(f, 0, SEEK_END) != 0) {
{
off_t end;
FILE *f = fopen(filename, "rb");
if(!f) { return -1; }
if(fseeko(f, 0, SEEK_END) != 0)
{
fclose(f);
return -1;
}
end = ftello(f);
if(end <= size) {
for(; end < size; end++) {
if(fputc(0, f) == EOF) {
if(end <= size)
{
for(; end < size; end++)
{
if(fputc(0, f) == EOF)
{
fclose(f);
return -1;
}
@@ -418,16 +436,17 @@ int truncate(const char *filename, off_t size) {
//
// Shrink if necessary (DOS-specific call)
//
{ int doshandle = 0;
unsigned nwritten = 0;
if(_dos_open(filename, O_WRONLY, &doshandle)) {
return -1;
}
if(lseek(doshandle, size, SEEK_SET) == -1L) {
{
int doshandle = 0;
unsigned nwritten = 0;
if(_dos_open(filename, O_WRONLY, &doshandle)) { return -1; }
if(lseek(doshandle, size, SEEK_SET) == -1L)
{
_dos_close(doshandle);
return -1;
}
if(_dos_write(doshandle, &doshandle, 0, &nwritten)) {
if(_dos_write(doshandle, &doshandle, 0, &nwritten))
{
_dos_close(doshandle);
return -1;
}
@@ -439,11 +458,11 @@ int truncate(const char *filename, off_t size) {
return 0;
}
#elif (defined(_WIN32) && defined(_MSC_VER))
#elif(defined(_WIN32) && defined(_MSC_VER))
#if defined(_MSC_VER)
// Disable extension warnings for <windows.h> and friends
#pragma warning (disable: 4226)
#pragma warning(disable : 4226)
#endif
#include <windows.h>
@@ -452,27 +471,32 @@ int truncate(const char *filename, off_t size) {
#define INVALID_SET_FILE_POINTER ((DWORD)(-1))
#endif
int truncate(const char *filename, off_t size) {
if(size < 0) {
int truncate(const char *filename, off_t size)
{
if(size < 0)
{
errno = EINVAL;
return -1;
}
//
// Extend (or do nothing) if necessary
//
{ off_t end;
FILE* f = fopen(filename, "rb");
if(!f) {
return -1;
}
if(fseeko(f, 0, SEEK_END) != 0) {
{
off_t end;
FILE *f = fopen(filename, "rb");
if(!f) { return -1; }
if(fseeko(f, 0, SEEK_END) != 0)
{
fclose(f);
return -1;
}
end = ftello(f);
if(end <= size) {
for(; end < size; end++) {
if(fputc(0, f) == EOF) {
if(end <= size)
{
for(; end < size; end++)
{
if(fputc(0, f) == EOF)
{
fclose(f);
return -1;
}
@@ -485,34 +509,31 @@ int truncate(const char *filename, off_t size) {
//
// Shrink if necessary (Windows-specific call)
//
{ HANDLE f = CreateFile(
filename,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if(f == INVALID_HANDLE_VALUE) {
return -1;
}
if(size > ((off_t)0x7FFFFFFFL)) {
{
HANDLE f = CreateFile(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(f == INVALID_HANDLE_VALUE) { return -1; }
if(size > ((off_t)0x7FFFFFFFL))
{
// use fancy 64-bit SetFilePointer
LONG lo = size;
LONG hi = size >> 32;
if(SetFilePointer(f, lo, &hi, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
CloseHandle(f);
return -1;
}
} else {
// use plain 32-bit SetFilePointer
if(SetFilePointer(f, size, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
if(SetFilePointer(f, lo, &hi, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
CloseHandle(f);
return -1;
}
}
if(!SetEndOfFile(f)) {
else
{
// use plain 32-bit SetFilePointer
if(SetFilePointer(f, size, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
CloseHandle(f);
return -1;
}
}
if(!SetEndOfFile(f))
{
CloseHandle(f);
return -1;
}
@@ -531,17 +552,18 @@ int truncate(const char *filename, off_t size) {
//
// Normalize argv[0]
//
void normalize_argv0(char* argv0) {
void normalize_argv0(char *argv0)
{
size_t i;
size_t start = 0;
int c;
for(i = 0; argv0[i]; i++) {
if(argv0[i] == '/' || argv0[i] == '\\') {
start = i + 1;
}
int c;
for(i = 0; argv0[i]; i++)
{
if(argv0[i] == '/' || argv0[i] == '\\') { start = i + 1; }
}
i = 0;
do {
do
{
c = ((unsigned char)(argv0[start + i]));
if(c == '.') { c = 0; }
if(c != 0) { c = tolower(c); }
@@ -551,7 +573,8 @@ void normalize_argv0(char* argv0) {
////////////////////////////////////////////////////////////////////////////////
void printfileerror(FILE* f, const char* name) {
void printfileerror(FILE *f, const char *name)
{
int e = errno;
printf("Error: ");
if(name) { printf("%s: ", name); }
@@ -569,19 +592,19 @@ void printfileerror(FILE* f, const char* name) {
//
#include <windows.h>
static HWND getconsolewindow(void) {
HWND hConsoleWindow = NULL;
static HWND getconsolewindow(void)
{
HWND hConsoleWindow = NULL;
HANDLE k32;
//
// See if GetConsoleWindow is available (Windows 2000 or later)
//
k32 = GetModuleHandle(TEXT("kernel32.dll"));
if(k32) {
typedef HWND (* WINAPI gcw_t)(void);
if(k32)
{
typedef HWND (*WINAPI gcw_t)(void);
gcw_t gcw = (gcw_t)GetProcAddress(k32, TEXT("GetConsoleWindow"));
if(gcw) {
hConsoleWindow = gcw();
}
if(gcw) { hConsoleWindow = gcw(); }
}
//
// There is an alternative method that involves FindWindow, but it's too
@@ -590,8 +613,9 @@ static HWND getconsolewindow(void) {
return hConsoleWindow;
}
void commandlinewarning(void) {
HWND hConsoleWindow;
void commandlinewarning(void)
{
HWND hConsoleWindow;
DWORD processId = 0;
//
// This trick doesn't work in Win9x
@@ -603,13 +627,12 @@ void commandlinewarning(void) {
hConsoleWindow = getconsolewindow();
if(!hConsoleWindow) { return; }
GetWindowThreadProcessId(hConsoleWindow, &processId);
if(GetCurrentProcessId() == processId) {
printf(
"\n"
"Note: This is a command-line application.\n"
"It was meant to run from a Windows command prompt.\n\n"
"Press ENTER to close this window..."
);
if(GetCurrentProcessId() == processId)
{
printf("\n"
"Note: This is a command-line application.\n"
"It was meant to run from a Windows command prompt.\n\n"
"Press ENTER to close this window...");
fflush(stdout);
fgetc(stdin);
}
@@ -629,7 +652,7 @@ void commandlinewarning(void) {}
// 32-bit signed and unsigned mod seem buggy; this solves it
unsigned long __umodsi3(unsigned long a, unsigned long b) { return a - (a / b) * b; }
signed long __modsi3(signed long a, signed long b) { return a - (a / b) * b; }
signed long __modsi3(signed long a, signed long b) { return a - (a / b) * b; }
// Some kind of soft float linkage issue?
void __cmpdf2(void) {}

420
edccchk.c
View File

@@ -18,8 +18,8 @@
//
////////////////////////////////////////////////////////////////////////////////
#include "common.h"
#include "banner.h"
#include "common.h"
////////////////////////////////////////////////////////////////////////////////
//
@@ -68,18 +68,22 @@
////////////////////////////////////////////////////////////////////////////////
#ifdef DEBUG
#define DPRINTF(fmt, ...) \
do { printf("edccchk-debug: " fmt , ## __VA_ARGS__); } while (0)
#define DPRINTF(fmt, ...) \
do \
{ \
printf("edccchk-debug: " fmt, ##__VA_ARGS__); \
} while(0)
#else
#define DPRINTF(fmt, ...) do {} while(0)
#define DPRINTF(fmt, ...) \
do \
{ \
} while(0)
#endif
static uint32_t get32lsb(const uint8_t* src) {
return
(((uint32_t)(src[0])) << 0) |
(((uint32_t)(src[1])) << 8) |
(((uint32_t)(src[2])) << 16) |
(((uint32_t)(src[3])) << 24);
static uint32_t get32lsb(const uint8_t *src)
{
return (((uint32_t)(src[0])) << 0) | (((uint32_t)(src[1])) << 8) | (((uint32_t)(src[2])) << 16) |
(((uint32_t)(src[3])) << 24);
}
static uint32_t nondatasectors;
@@ -104,19 +108,19 @@ static uint32_t filledsectors;
//
static uint8_t ecc_f_lut[256];
static uint8_t ecc_b_lut[256];
static uint32_t edc_lut [256];
static uint32_t edc_lut[256];
static void eccedc_init(void) {
static void eccedc_init(void)
{
DPRINTF("Entering eccedc_init().\n");
size_t i;
for(i = 0; i < 256; i++) {
uint32_t edc = i;
size_t j = (i << 1) ^ (i & 0x80 ? 0x11D : 0);
ecc_f_lut[i] = j;
for(i = 0; i < 256; i++)
{
uint32_t edc = i;
size_t j = (i << 1) ^ (i & 0x80 ? 0x11D : 0);
ecc_f_lut[i] = j;
ecc_b_lut[i ^ j] = i;
for(j = 0; j < 8; j++) {
edc = (edc >> 1) ^ (edc & 1 ? 0xD8018001 : 0);
}
for(j = 0; j < 8; j++) { edc = (edc >> 1) ^ (edc & 1 ? 0xD8018001 : 0); }
edc_lut[i] = edc;
}
}
@@ -125,15 +129,10 @@ static void eccedc_init(void) {
//
// Compute EDC for a block
//
static uint32_t edc_compute(
uint32_t edc,
const uint8_t* src,
size_t size
) {
static uint32_t edc_compute(uint32_t edc, const uint8_t *src, size_t size)
{
DPRINTF("Entering edc_compute(%d, *%d, %d).\n");
for(; size; size--) {
edc = (edc >> 8) ^ edc_lut[(edc ^ (*src++)) & 0xFF];
}
for(; size; size--) { edc = (edc >> 8) ^ edc_lut[(edc ^ (*src++)) & 0xFF]; }
return edc;
}
@@ -142,28 +141,29 @@ static uint32_t edc_compute(
// Check ECC block (either P or Q)
// Returns true if the ECC data is an exact match
//
static int8_t ecc_checkpq(
const uint8_t* address,
const uint8_t* data,
size_t major_count,
size_t minor_count,
size_t major_mult,
size_t minor_inc,
const uint8_t* ecc
) {
static int8_t ecc_checkpq(const uint8_t *address,
const uint8_t *data,
size_t major_count,
size_t minor_count,
size_t major_mult,
size_t minor_inc,
const uint8_t *ecc)
{
DPRINTF("Entering ecc_checkpq(*%d, *%d, %d, %d, %d, %d, *%d).\n");
size_t size = major_count * minor_count;
size_t major;
for(major = 0; major < major_count; major++) {
size_t index = (major >> 1) * major_mult + (major & 1);
for(major = 0; major < major_count; major++)
{
size_t index = (major >> 1) * major_mult + (major & 1);
uint8_t ecc_a = 0;
uint8_t ecc_b = 0;
size_t minor;
for(minor = 0; minor < minor_count; minor++) {
size_t minor;
for(minor = 0; minor < minor_count; minor++)
{
uint8_t temp;
if(index < 4) {
temp = address[index];
} else {
if(index < 4) { temp = address[index]; }
else
{
temp = data[index - 4];
}
index += minor_inc;
@@ -174,12 +174,7 @@ static int8_t ecc_checkpq(
}
ecc_a = ecc_b_lut[ecc_f_lut[ecc_a] ^ ecc_b];
if(
ecc[major ] != (ecc_a ) ||
ecc[major + major_count] != (ecc_a ^ ecc_b)
) {
return 0;
}
if(ecc[major] != (ecc_a) || ecc[major + major_count] != (ecc_a ^ ecc_b)) { return 0; }
}
return 1;
}
@@ -188,15 +183,11 @@ static int8_t ecc_checkpq(
// Check ECC P and Q codes for a sector
// Returns true if the ECC data is an exact match
//
static int8_t ecc_checksector(
const uint8_t *address,
const uint8_t *data,
const uint8_t *ecc
) {
static int8_t ecc_checksector(const uint8_t *address, const uint8_t *data, const uint8_t *ecc)
{
DPRINTF("Entering ecc_checksector(*%d, *%d, *%d).\n");
return
ecc_checkpq(address, data, 86, 24, 2, 86, ecc) && // P
ecc_checkpq(address, data, 52, 43, 86, 88, ecc + 0xAC); // Q
return ecc_checkpq(address, data, 86, 24, 2, 86, ecc) && // P
ecc_checkpq(address, data, 52, 43, 86, 88, ecc + 0xAC); // Q
}
////////////////////////////////////////////////////////////////////////////////
@@ -210,25 +201,25 @@ static off_t mycounter_encode = (off_t)-1;
static off_t mycounter_decode = (off_t)-1;
static off_t mycounter_total = 0;
static void resetcounter(off_t total) {
static void resetcounter(off_t total)
{
mycounter_analyze = (off_t)-1;
mycounter_encode = (off_t)-1;
mycounter_decode = (off_t)-1;
mycounter_total = total;
}
static void encode_progress(void) {
static void encode_progress(void)
{
off_t a = (mycounter_analyze + 64) / 128;
off_t t = (mycounter_total + 64) / 128;
off_t t = (mycounter_total + 64) / 128;
if(!t) { t = 1; }
fprintf(stderr,
"Analyze(%02u%%)\r",
(unsigned)((((off_t)100) * a) / t)
);
fprintf(stderr, "Analyze(%02u%%)\r", (unsigned)((((off_t)100) * a) / t));
}
static void setcounter_analyze(off_t n) {
int8_t p = ((n >> 20) != (mycounter_analyze >> 20));
static void setcounter_analyze(off_t n)
{
int8_t p = ((n >> 20) != (mycounter_analyze >> 20));
mycounter_analyze = n;
if(p) { encode_progress(); }
}
@@ -237,18 +228,17 @@ static void setcounter_analyze(off_t n) {
//
// Returns nonzero on error
//
static int8_t ecmify(
const char* infilename
) {
static int8_t ecmify(const char *infilename)
{
DPRINTF("Entering ecmify(\"%s\").\n", infilename);
int8_t returncode = 0;
int8_t filled = 0;
int8_t filled = 0;
FILE* in = NULL;
FILE *in = NULL;
uint8_t* queue = NULL;
size_t queue_start_ofs = 0;
size_t queue_bytes_available = 0;
uint8_t *queue = NULL;
size_t queue_start_ofs = 0;
size_t queue_bytes_available = 0;
uint32_t input_edc = 0;
@@ -257,16 +247,15 @@ static int8_t ecmify(
off_t input_bytes_queued = 0;
size_t queue_size = ((size_t)(-1)) - 4095;
if((unsigned long)queue_size > 0x40000lu) {
queue_size = (size_t)0x40000lu;
}
if((unsigned long)queue_size > 0x40000lu) { queue_size = (size_t)0x40000lu; }
//
// Allocate space for queue
//
DPRINTF("ecmify(): Allocation memory for queue.\n");
queue = malloc(queue_size);
if(!queue) {
if(!queue)
{
printf("Out of memory\n");
goto error;
}
@@ -290,140 +279,130 @@ static int8_t ecmify(
if(input_file_length < 0) { goto error_in; }
resetcounter(input_file_length);
nondatasectors= 0;
mode0sectors= 0;
mode0errors= 0;
mode1sectors= 0;
mode1errors= 0;
mode2f1sectors= 0;
mode2f1errors= 0;
nondatasectors = 0;
mode0sectors = 0;
mode0errors = 0;
mode1sectors = 0;
mode1errors = 0;
mode2f1sectors = 0;
mode2f1errors = 0;
mode2f1warnings = 0;
mode2f2sectors= 0;
mode2f2errors= 0;
mode2f2sectors = 0;
mode2f2errors = 0;
mode2f2warnings = 0;
totalsectors= 0;
totalerrors= 0;
filledsectors=0;
totalsectors = 0;
totalerrors = 0;
filledsectors = 0;
DPRINTF("ecmify(): Entering main loop.\n");
for(;;) {
for(;;)
{
//
// Refill queue if necessary
//
if(
(queue_bytes_available < 2352) &&
(((off_t)queue_bytes_available) < (input_file_length - input_bytes_queued))
) {
if((queue_bytes_available < 2352) &&
(((off_t)queue_bytes_available) < (input_file_length - input_bytes_queued)))
{
DPRINTF("ecmify(): Refilling queue.\n");
//
// We need to read more data
//
off_t willread = input_file_length - input_bytes_queued;
off_t maxread = queue_size - queue_bytes_available;
if(willread > maxread) {
off_t maxread = queue_size - queue_bytes_available;
if(willread > maxread)
{
DPRINTF("Will read maximum.\n");
willread = maxread;
}
if(queue_start_ofs > 0) {
if(queue_start_ofs > 0)
{
memmove(queue, queue + queue_start_ofs, queue_bytes_available);
queue_start_ofs = 0;
}
if(willread) {
if(willread)
{
setcounter_analyze(input_bytes_queued);
if(fseeko(in, input_bytes_queued, SEEK_SET) != 0) {
goto error_in;
}
if(fread(queue + queue_bytes_available, 1, willread, in) != (size_t)willread) {
goto error_in;
}
if(fseeko(in, input_bytes_queued, SEEK_SET) != 0) { goto error_in; }
if(fread(queue + queue_bytes_available, 1, willread, in) != (size_t)willread) { goto error_in; }
input_edc = edc_compute(
input_edc,
queue + queue_bytes_available,
willread
);
input_edc = edc_compute(input_edc, queue + queue_bytes_available, willread);
input_bytes_queued += willread;
input_bytes_queued += willread;
queue_bytes_available += willread;
}
}
if(queue_bytes_available == 0) {
if(queue_bytes_available == 0)
{
DPRINTF("ecmify(): No data left in queue.\n");
//
// No data left to read -> quit
//
break;
}
uint8_t* sector = queue + queue_start_ofs;
uint8_t *sector = queue + queue_start_ofs;
// Data sector
if (
sector[0x000] == 0x00 && // sync (12 bytes)
sector[0x001] == 0xFF &&
sector[0x002] == 0xFF &&
sector[0x003] == 0xFF &&
sector[0x004] == 0xFF &&
sector[0x005] == 0xFF &&
sector[0x006] == 0xFF &&
sector[0x007] == 0xFF &&
sector[0x008] == 0xFF &&
sector[0x009] == 0xFF &&
sector[0x00A] == 0xFF &&
sector[0x00B] == 0x00
)
if(sector[0x000] == 0x00 && // sync (12 bytes)
sector[0x001] == 0xFF && sector[0x002] == 0xFF && sector[0x003] == 0xFF && sector[0x004] == 0xFF &&
sector[0x005] == 0xFF && sector[0x006] == 0xFF && sector[0x007] == 0xFF && sector[0x008] == 0xFF &&
sector[0x009] == 0xFF && sector[0x00A] == 0xFF && sector[0x00B] == 0x00)
{
DPRINTF("ecmify(): Data sector, address %02X:%02X:%02X.\n", sector[0x00C], sector[0x00D], sector[0x00E]);
// Just for debug
// fprintf(stderr, "Address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
// fprintf(stderr, "Address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(sector[0x00F] == 0x00) // mode (1 byte)
{
DPRINTF("ecmify(): Mode 0 sector at address %02X:%02X:%02X.\n", sector[0x00C], sector[0x00D], sector[0x00E]);
DPRINTF("ecmify(): Mode 0 sector at address %02X:%02X:%02X.\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
mode0sectors++;
for(int i=0x010;i < 0x930;i++)
for(int i = 0x010; i < 0x930; i++)
{
if(sector[i] != 0x00)
{
mode0errors++;
totalerrors++;
fprintf(stderr, "Mode 0 sector with error at address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 0 sector with error at address: %02X:%02X:%02X\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
break;
}
}
}
else if(sector[0x00F] == 0x01) // mode (1 byte)
{
DPRINTF("ecmify(): Mode 1 sector at address %02X:%02X:%02X.\n", sector[0x00C], sector[0x00D], sector[0x00E]);
DPRINTF("ecmify(): Mode 1 sector at address %02X:%02X:%02X.\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
mode1sectors++;
if(
!ecc_checksector(
sector + 0xC,
sector + 0x10,
sector + 0x81C
) ||
if(!ecc_checksector(sector + 0xC, sector + 0x10, sector + 0x81C) ||
edc_compute(0, sector, 0x810) != get32lsb(sector + 0x810) ||
sector[0x814] != 0x00 || // reserved (8 bytes)
sector[0x815] != 0x00 ||
sector[0x816] != 0x00 ||
sector[0x817] != 0x00 ||
sector[0x818] != 0x00 ||
sector[0x819] != 0x00 ||
sector[0x81A] != 0x00 ||
sector[0x81B] != 0x00
) {
sector[0x815] != 0x00 || sector[0x816] != 0x00 || sector[0x817] != 0x00 || sector[0x818] != 0x00 ||
sector[0x819] != 0x00 || sector[0x81A] != 0x00 || sector[0x81B] != 0x00)
{
mode1errors++;
totalerrors++;
fprintf(stderr, "Mode 1 sector with error at address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(edc_compute(0, sector, 0x810) != get32lsb(sector + 0x810))
fprintf(stderr, "%02X:%02X:%02X: Failed EDC\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(sector + 0xC, sector + 0x10, 86, 24, 2, 86, sector + 0x81C))
fprintf(stderr, "%02X:%02X:%02X: Failed ECC P\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(sector + 0xC, sector + 0x10, 52, 43, 86, 88, sector + 0x81C + 0xAC))
fprintf(stderr, "%02X:%02X:%02X: Failed ECC Q\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 1 sector with error at address: %02X:%02X:%02X\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
if(edc_compute(0, sector, 0x810) != get32lsb(sector + 0x810))
fprintf(stderr, "%02X:%02X:%02X: Failed EDC\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(sector + 0xC, sector + 0x10, 86, 24, 2, 86, sector + 0x81C))
fprintf(stderr, "%02X:%02X:%02X: Failed ECC P\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(sector + 0xC, sector + 0x10, 52, 43, 86, 88, sector + 0x81C + 0xAC))
fprintf(stderr, "%02X:%02X:%02X: Failed ECC Q\n", sector[0x00C], sector[0x00D], sector[0x00E]);
}
filled = 1;
@@ -439,30 +418,47 @@ static int8_t ecmify(
if(filled)
{
filledsectors++;
fprintf(stderr, "Mode 1 sector at address: %02X:%02X:%02X is filled with 55h\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 1 sector at address: %02X:%02X:%02X is filled with 55h\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
}
}
else if(sector[0x00F] == 0x02) // mode (1 byte)
{
DPRINTF("ecmify(): Mode 2 sector at address %02X:%02X:%02X.\n", sector[0x00C], sector[0x00D], sector[0x00E]);
uint8_t* m2sec = sector + 0x10;
DPRINTF("ecmify(): Mode 2 sector at address %02X:%02X:%02X.\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
uint8_t *m2sec = sector + 0x10;
if((sector[0x012] & 0x20) == 0x20) // mode 2 form 2
{
mode2f2sectors++;
if(edc_compute(0, m2sec, 0x91C) != get32lsb(m2sec + 0x91C) && get32lsb(m2sec + 0x91C) != 0)
{
fprintf(stderr, "Mode 2 form 2 sector with error at address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 2 form 2 sector with error at address: %02X:%02X:%02X\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
if(edc_compute(0, m2sec, 0x91C) != get32lsb(m2sec + 0x91C))
fprintf(stderr, "%02X:%02X:%02X: Failed EDC\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(
stderr, "%02X:%02X:%02X: Failed EDC\n", sector[0x00C], sector[0x00D], sector[0x00E]);
mode2f2errors++;
totalerrors++;
}
if(sector[0x010] != sector[0x014] || sector[0x011] != sector[0x015] || sector[0x012] != sector[0x016] || sector[0x013] != sector[0x017])
if(sector[0x010] != sector[0x014] || sector[0x011] != sector[0x015] ||
sector[0x012] != sector[0x016] || sector[0x013] != sector[0x017])
{
mode2f2warnings++;
totalwarnings++;
fprintf(stderr, "Subheader copies differ in mode 2 form 2 sector at address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Subheader copies differ in mode 2 form 2 sector at address: %02X:%02X:%02X\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
}
filled = 1;
@@ -478,35 +474,46 @@ static int8_t ecmify(
if(filled)
{
filledsectors++;
fprintf(stderr, "Mode 2 form 2 sector at address: %02X:%02X:%02X is filled with 55h\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 2 form 2 sector at address: %02X:%02X:%02X is filled with 55h\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
}
}
else
{
mode2f1sectors++;
if(
!ecc_checksector(
zeroaddress,
m2sec,
m2sec + 0x80C
) ||
if(!ecc_checksector(zeroaddress, m2sec, m2sec + 0x80C) ||
edc_compute(0, m2sec, 0x808) != get32lsb(m2sec + 0x808))
{
fprintf(stderr, "Mode 2 form 1 sector with error at address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 2 form 1 sector with error at address: %02X:%02X:%02X\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
if(edc_compute(0, m2sec, 0x808) != get32lsb(m2sec + 0x808))
fprintf(stderr, "%02X:%02X:%02X: Failed EDC\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(zeroaddress, m2sec, 86, 24, 2, 86, m2sec + 0x80C))
fprintf(stderr, "%02X:%02X:%02X: Failed ECC P\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(
stderr, "%02X:%02X:%02X: Failed EDC\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(zeroaddress, m2sec, 86, 24, 2, 86, m2sec + 0x80C))
fprintf(
stderr, "%02X:%02X:%02X: Failed ECC P\n", sector[0x00C], sector[0x00D], sector[0x00E]);
if(!ecc_checkpq(zeroaddress, m2sec, 52, 43, 86, 88, m2sec + 0x80C))
fprintf(stderr, "%02X:%02X:%02X: Failed ECC Q\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(
stderr, "%02X:%02X:%02X: Failed ECC Q\n", sector[0x00C], sector[0x00D], sector[0x00E]);
mode2f1errors++;
totalerrors++;
}
if(sector[0x010] != sector[0x014] || sector[0x011] != sector[0x015] || sector[0x012] != sector[0x016] || sector[0x013] != sector[0x017])
if(sector[0x010] != sector[0x014] || sector[0x011] != sector[0x015] ||
sector[0x012] != sector[0x016] || sector[0x013] != sector[0x017])
{
mode2f1warnings++;
totalwarnings++;
fprintf(stderr, "Subheader copies differ in mode 2 form 1 sector at address: %02X:%02X:%02X\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Subheader copies differ in mode 2 form 1 sector at address: %02X:%02X:%02X\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
}
filled = 1;
@@ -522,13 +529,21 @@ static int8_t ecmify(
if(filled)
{
filledsectors++;
fprintf(stderr, "Mode 2 form 1 sector at address: %02X:%02X:%02X is filled with 55h\n", sector[0x00C], sector[0x00D], sector[0x00E]);
fprintf(stderr,
"Mode 2 form 1 sector at address: %02X:%02X:%02X is filled with 55h\n",
sector[0x00C],
sector[0x00D],
sector[0x00E]);
}
}
}
else // Unknown sector mode!!!
{
DPRINTF("ecmify(): Unknown data sector with mode %d at address %02X:%02X:%02X.\n", sector[0x00F], sector[0x00C], sector[0x00D], sector[0x00E]);
DPRINTF("ecmify(): Unknown data sector with mode %d at address %02X:%02X:%02X.\n",
sector[0x00F],
sector[0x00C],
sector[0x00D],
sector[0x00E]);
nondatasectors++;
}
}
@@ -542,10 +557,10 @@ static int8_t ecmify(
// Advance to the next sector
//
totalsectors++;
input_bytes_checked += 2352;
queue_start_ofs += 2352;
input_bytes_checked += 2352;
queue_start_ofs += 2352;
queue_bytes_available -= 2352;
DPRINTF("ecmify.totalsectors = %d\n", totalsectors);
DPRINTF("ecmify.input_bytes_checked = %d\n", input_bytes_checked);
DPRINTF("ecmify.queue_start_ofs = %d\n", queue_start_ofs);
@@ -588,36 +603,37 @@ error:
done:
if(queue != NULL) { free(queue); }
if(in != NULL) { fclose(in ); }
if(in != NULL) { fclose(in); }
return returncode;
}
int main(int argc, char** argv) {
int main(int argc, char **argv)
{
DPRINTF("Entering main().\n");
int returncode = 0;
char* infilename = NULL;
int returncode = 0;
char *infilename = NULL;
DPRINTF("Normalizing argv[0].\n");
normalize_argv0(argv[0]);
DPRINTF("Showing banner.\n");
banner();
//
// Check command line
//
switch(argc) {
case 2:
infilename = argv[1];
//
// Initialize the ECC/EDC tables
//
eccedc_init();
if(ecmify(infilename)) { goto error; }
break;
default:
goto usage;
switch(argc)
{
case 2:
infilename = argv[1];
//
// Initialize the ECC/EDC tables
//
eccedc_init();
if(ecmify(infilename)) { goto error; }
break;
default: goto usage;
}
//
@@ -627,11 +643,9 @@ int main(int argc, char** argv) {
goto done;
usage:
printf(
"Usage:\n"
"\n"
" edccchk cdimagefile\n"
);
printf("Usage:\n"
"\n"
" edccchk cdimagefile\n");
error:
returncode = 1;