Use Unicode functions on Windows. (TCHAR functions)

tchar_xplt.h: TCHAR definitions for non-Windows platforms.
This commit is contained in:
David Korth 2017-07-07 20:52:31 -04:00
parent fdcef01062
commit 96edf7b3ae
2 changed files with 94 additions and 41 deletions

View File

@ -17,6 +17,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#ifdef _WIN32
// Enable Unicode functions.
#define UNICODE 1
#define _UNICODE 1
#endif
#ifndef _MSC_VER
// MinGW/Unix: Use Large File Support if necessary.
# define _LARGEFILE_SOURCE 1
@ -36,7 +42,10 @@
# define ftell _ftelli64
#endif
int main(int argc, char *argv[])
// TCHAR definitions for non-Windows systems.
#include "tchar_xplt.h"
int _tmain(int argc, TCHAR *argv[])
{
// Allocated memory.
FILE *f_src = NULL, *f_hack = NULL;
@ -57,69 +66,69 @@ int main(int argc, char *argv[])
uint32_t ui32;
int ret = EXIT_FAILURE;
if (argc >= 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
if (argc >= 2 && (!_tcscmp(argv[1], _T("-h")) || !_tcscmp(argv[1], _T("--help")))) {
// Usage information.
fprintf(stderr,
"HACK DETECTION!\n"
"Check if a hacked ROM is a hex-edited hack.\n"
"\n"
"Syntax: %s [source ROM] [hacked ROM]\n"
"- [source ROM]: The original ROM image.\n"
"- [hacked ROM]: The hacked ROM image.\n"
"\n"
"Both ROM images must be at least 32 KB and less than 16 MB.\n",
_ftprintf(stderr,
_T("HACK DETECTION!\n")
_T("Check if a hacked ROM is a hex-edited hack.\n")
_T("\n")
_T("Syntax: %s [source ROM] [hacked ROM]\n")
_T("- [source ROM]: The original ROM image.\n")
_T("- [hacked ROM]: The hacked ROM image.\n")
_T("\n")
_T("Both ROM images must be at least 32 KB and less than 16 MB.\n"),
argv[0]);
return EXIT_SUCCESS;
}
if (argc != 3) {
fprintf(stderr, "Syntax: %s [source ROM] [hacked ROM]\n"
"Try '%s --help' for more information.\n", argv[0], argv[0]);
_ftprintf(stderr, _T("Syntax: %s [source ROM] [hacked ROM]\n")
_T("Try '%s --help' for more information.\n"), argv[0], argv[0]);
return EXIT_FAILURE;
}
// Open the source ROM.
f_src = fopen(argv[1], "rb");
f_src = _tfopen(argv[1], _T("rb"));
if (!f_src) {
fprintf(stderr, "*** ERROR opening source ROM '%s': %s\n", argv[1], strerror(errno));
_ftprintf(stderr, _T("*** ERROR opening source ROM '%s': %s\n"), argv[1], _tcserror(errno));
goto cleanup;
}
fseek(f_src, 0, SEEK_END);
sz64_tmp = ftell(f_src);
if (sz64_tmp < 0) {
fprintf(stderr, "*** ERROR: Unable to determine the size of source ROM '%s'.\n", argv[1]);
_ftprintf(stderr, _T("*** ERROR: Unable to determine the size of source ROM '%s'.\n"), argv[1]);
goto cleanup;
} else if (sz64_tmp == 0) {
fprintf(stderr, "*** ERROR: Source ROM '%s' is empty. (0 bytes)\n", argv[1]);
_ftprintf(stderr, _T("*** ERROR: Source ROM '%s' is empty. (0 bytes)\n"), argv[1]);
goto cleanup;
} else if (sz64_tmp < 32*1024) {
fprintf(stderr, "*** ERROR: Source ROM '%s' is smaller than 32 KB.\n", argv[1]);
_ftprintf(stderr, _T("*** ERROR: Source ROM '%s' is smaller than 32 KB.\n"), argv[1]);
goto cleanup;
} else if (sz64_tmp > 16*1024*1024) {
fprintf(stderr, "*** ERROR: Source ROM '%s' is larger than 16 MB.\n", argv[1]);
_ftprintf(stderr, _T("*** ERROR: Source ROM '%s' is larger than 16 MB.\n"), argv[1]);
goto cleanup;
}
sz_src = (uint32_t)sz64_tmp;
// Open the hacked ROM.
f_hack = fopen(argv[2], "rb");
f_hack = _tfopen(argv[2], _T("rb"));
if (!f_hack) {
fprintf(stderr, "*** ERROR opening hacked ROM '%s': %s\n", argv[2], strerror(errno));
_ftprintf(stderr, _T("*** ERROR opening hacked ROM '%s': %s\n"), argv[2], _tcserror(errno));
goto cleanup;
}
fseek(f_hack, 0, SEEK_END);
sz64_tmp = ftell(f_hack);
if (sz64_tmp < 0) {
fprintf(stderr, "*** ERROR: Unable to determine the size of hacked ROM '%s'.\n", argv[2]);
_ftprintf(stderr, _T("*** ERROR: Unable to determine the size of hacked ROM '%s'.\n"), argv[2]);
goto cleanup;
} else if (sz64_tmp == 0) {
fprintf(stderr, "*** ERROR: Hacked ROM '%s' is empty. (0 bytes)\n", argv[2]);
_ftprintf(stderr, _T("*** ERROR: Hacked ROM '%s' is empty. (0 bytes)\n"), argv[2]);
goto cleanup;
} else if (sz64_tmp < 32*1024) {
fprintf(stderr, "*** ERROR: Hacked ROM '%s' is smaller than 32 KB.\n", argv[2]);
_ftprintf(stderr, _T("*** ERROR: Hacked ROM '%s' is smaller than 32 KB.\n"), argv[2]);
goto cleanup;
} else if (sz64_tmp > 16*1024*1024) {
fprintf(stderr, "*** ERROR: Hacked ROM '%s' is larger than 16 MB.\n", argv[2]);
_ftprintf(stderr, _T("*** ERROR: Hacked ROM '%s' is larger than 16 MB.\n"), argv[2]);
goto cleanup;
}
sz_hack = (uint32_t)sz64_tmp;
@ -127,26 +136,26 @@ int main(int argc, char *argv[])
// Load the source ROM into memory.
p_src = malloc(sz_src);
if (!p_src) {
fprintf(stderr, "*** ERROR: Unable to allocate %u bytes for source ROM '%s'.\n", sz_src, argv[1]);
_ftprintf(stderr, _T("*** ERROR: Unable to allocate %u bytes for source ROM '%s'.\n"), sz_src, argv[1]);
goto cleanup;
}
fseek(f_src, 0, SEEK_SET);
sz = fread(p_src, 1, sz_src, f_src);
if (sz != sz_src) {
fprintf(stderr, "*** ERROR: Unable to read source ROM '%s' into memory.\n", argv[1]);
_ftprintf(stderr, _T("*** ERROR: Unable to read source ROM '%s' into memory.\n"), argv[1]);
goto cleanup;
}
// Load the hacked ROM into memory.
p_hack = malloc(sz_hack);
if (!p_hack) {
fprintf(stderr, "*** ERROR: Unable to allocate %u bytes for hacked ROM '%s'.\n", sz_hack, argv[2]);
_ftprintf(stderr, _T("*** ERROR: Unable to allocate %u bytes for hacked ROM '%s'.\n"), sz_hack, argv[2]);
goto cleanup;
}
fseek(f_hack, 0, SEEK_SET);
sz = fread(p_hack, 1, sz_hack, f_hack);
if (sz != sz_hack) {
fprintf(stderr, "*** ERROR: Unable to read hacked ROM '%s' into memory.\n", argv[2]);
_ftprintf(stderr, _T("*** ERROR: Unable to read hacked ROM '%s' into memory.\n"), argv[2]);
goto cleanup;
}
@ -160,17 +169,17 @@ int main(int argc, char *argv[])
p--;
}
if (p < p_src) {
fprintf(stderr, "*** ERROR: Source ROM '%s' is all padding. (all bytes are 0x%02X)\n", argv[1], pad_byte);
_ftprintf(stderr, _T("*** ERROR: Source ROM '%s' is all padding. (all bytes are 0x%02X)\n"), argv[1], pad_byte);
goto cleanup;
}
sz_check = p - p_src; // TODO: Verify that this isn't off by one.
printf("Source ROM: %s (%u bytes, %u less padding)\n", argv[1], sz_src, sz_check);
_tprintf(_T("Source ROM: %s (%u bytes, %u less padding)\n"), argv[1], sz_src, sz_check);
} else {
// No padding.
sz_check = sz_src;
printf("Source ROM: %s (%u bytes)\n", argv[1], sz_src);
_tprintf(_T("Source ROM: %s (%u bytes)\n"), argv[1], sz_src);
}
printf("Hacked ROM: %s (%u bytes)\n\n", argv[2], sz_hack);
_tprintf(_T("Hacked ROM: %s (%u bytes)\n\n"), argv[2], sz_hack);
if (sz_hack < sz_check) {
// Hacked ROM is smaller than the source ROM, less padding.
@ -178,7 +187,7 @@ int main(int argc, char *argv[])
sz_check = sz_hack;
}
printf("Checking %u bytes...\n", sz_check);
_tprintf(_T("Checking %u bytes...\n"), sz_check);
pc_src = p_src;
pc_hack = p_hack;
sz_common = 0;
@ -193,19 +202,19 @@ int main(int argc, char *argv[])
hack_detection = (sz_common >= (sz_check / 2));
if (hack_detection) {
// TODO: Colorize "* HACK DETECTION *" on Windows and Linux.
printf("\n*** HACK DETECTION ***\n\n");
_tprintf(_T("\n*** HACK DETECTION ***)\n\n"));
} else {
printf("\n");
_tprintf(_T("\n"));
}
printf("%u of %u bytes (%0.1f%%) are common between both the\n"
"source ROM and the hacked ROM.\n\n",
_tprintf(_T("%u of %u bytes (%0.1f%%) are common between both the\n")
_T("source ROM and the hacked ROM.\n\n"),
sz_common, sz_check, (((double)sz_common / (double)sz_check) * 100.0));
if (hack_detection) {
printf("The hacked ROM is most likely a hex-edited binary hack.\n");
_tprintf(_T("The hacked ROM is most likely a hex-edited binary hack.\n"));
} else {
printf("The hacked ROM is either a disassembly hack or not related\n"
"to the source ROM at all.\n");
_tprintf(_T("The hacked ROM is either a disassembly hack or not related\n")
_T("to the source ROM at all.\n"));
}
// TODO: Separate return codes for "not hacked", "HACK DETECTION", and "error"?
ret = 0;

44
tchar_xplt.h Normal file
View File

@ -0,0 +1,44 @@
/****************************************************************************
* HACK DETECTION! HACK DETECTION! HACK DETECTION? HACK DETECTION! *
* *
* Copyright (c) 2017 by David Korth. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#ifndef __HACK_DETECTION_TCHAR_XPLT_H__
#define __HACK_DETECTION_TCHAR_XPLT_H__
#ifdef _WIN32
// Windows uses tchar.h directly.
#include <tchar.h>
#else /* !_WIN32 */
// Non-Windows systems use UTF-8.
#define _T(x) x
#define TCHAR char
#define _tmain main
#define _tcscmp strcmp
#define _tprintf printf
#define _ftprintf fprintf
#define _tfopen fopen
#define _tcserror strerror
#endif /* _WIN32 */
#endif /* __HACK_DETECTION_TCHAR_XPLT_H__ */