From 9e5e21b8c9642892c1ffb0c59ca7eaa39009d73b Mon Sep 17 00:00:00 2001 From: Brett Vickers Date: Thu, 8 Aug 2019 20:50:29 -0700 Subject: [PATCH] Prevent uninitialized value bugs and improve string safety. This change does two things: 1. Updates the registry APIs to reduce the likelihood of uninitialized variables. The code wasn't always checking the return value of registry load operations. In some cases, this led to uninitialized memory being used, and crashes could result. For example, LoadConfiguration in Applewin.cpp was using an uninitialized value for the computer type if no registry variable for the "Apple 2 type" was set. New registry reading methods and macros have also been introduced, allowing default value fallbacks for the cases where a registry variable is not found. This makes registry access simpler and safer when a default value is known in advance. The registry code's style has also been updated to conform with the rest of the code base (tabs instead of spaces, naming conventions, etc.) 2. Introduces string safety improvements. A number of code paths have been modified to use safe-string functions instead of their unsafe counterparts (e.g., strcpy, sprintf). In the process, some strings were converted from "char" to "TCHAR". This was done mostly for consistency with the rest of the code-base. --- source/Applewin.cpp | 86 ++++++------ source/Applewin.h | 2 +- source/Configuration/About.cpp | 6 +- source/Configuration/PageConfig.cpp | 5 +- source/Configuration/PageConfigTfe.cpp | 6 +- source/Configuration/PageDisk.cpp | 4 +- source/Configuration/PropertySheetHelper.cpp | 5 +- source/Disk.cpp | 90 ++++++------- source/Frame.cpp | 15 ++- source/Harddisk.cpp | 14 +- source/Registry.cpp | 135 +++++++++++-------- source/Registry.h | 17 +-- source/StdAfx.h | 1 + source/Video.cpp | 67 ++++----- 14 files changed, 239 insertions(+), 214 deletions(-) diff --git a/source/Applewin.cpp b/source/Applewin.cpp index 6a988215..0d852de9 100644 --- a/source/Applewin.cpp +++ b/source/Applewin.cpp @@ -60,9 +60,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "Configuration/PropertySheet.h" #include "Tfe/Tfe.h" +#define VERSIONSTRING_SIZE 16 + static UINT16 g_AppleWinVersion[4] = {0}; -char VERSIONSTRING[16] = "xx.yy.zz.ww"; static UINT16 g_OldAppleWinVersion[4] = {0}; +TCHAR VERSIONSTRING[VERSIONSTRING_SIZE] = "xx.yy.zz.ww"; const TCHAR *g_pAppTitle = NULL; @@ -522,7 +524,7 @@ static void LoadConfigOldJoystick_v1(const UINT uJoyNum) //Reads configuration from the registry entries void LoadConfiguration(void) { - DWORD dwComputerType; + DWORD dwComputerType = 0; eApple2Type apple2Type = A2TYPE_APPLE2EENHANCED; if (REGLOAD(TEXT(REGVALUE_APPLE2_TYPE), &dwComputerType)) @@ -559,16 +561,15 @@ void LoadConfiguration(void) apple2Type = (eApple2Type) dwComputerType; } - else // Support older AppleWin registry entries + else if (REGLOAD(TEXT(REGVALUE_OLD_APPLE2_TYPE), &dwComputerType)) // Support older AppleWin registry entries { - REGLOAD(TEXT(REGVALUE_OLD_APPLE2_TYPE), &dwComputerType); switch (dwComputerType) { // NB. No A2TYPE_APPLE2E (this is correct) case 0: apple2Type = A2TYPE_APPLE2; break; case 1: apple2Type = A2TYPE_APPLE2PLUS; break; case 2: apple2Type = A2TYPE_APPLE2EENHANCED; break; - default: apple2Type = A2TYPE_APPLE2EENHANCED; + default: apple2Type = A2TYPE_APPLE2EENHANCED; break; } } @@ -576,18 +577,11 @@ void LoadConfiguration(void) // - DWORD dwCpuType; - eCpuType cpu = CPU_65C02; - - if (REGLOAD(TEXT(REGVALUE_CPU_TYPE), &dwCpuType)) - { - if (dwCpuType != CPU_6502 && dwCpuType != CPU_65C02) - dwCpuType = CPU_65C02; - - cpu = (eCpuType) dwCpuType; - } - - SetMainCpu(cpu); + DWORD dwMainCpuType; + REGLOAD_DEFAULT(TEXT(REGVALUE_CPU_TYPE), &dwMainCpuType, CPU_65C02); + if (dwMainCpuType != CPU_6502 && dwMainCpuType != CPU_65C02) + dwMainCpuType = CPU_65C02; + SetMainCpu((eCpuType)dwMainCpuType); // @@ -607,7 +601,7 @@ void LoadConfiguration(void) LoadConfigOldJoystick_v1(JN_JOYSTICK1); DWORD dwSoundType; - REGLOAD(TEXT("Sound Emulation"), &dwSoundType); + REGLOAD_DEFAULT(TEXT("Sound Emulation"), &dwSoundType, REG_SOUNDTYPE_NONE); switch (dwSoundType) { case REG_SOUNDTYPE_NONE: @@ -621,29 +615,32 @@ void LoadConfiguration(void) break; } - char aySerialPortName[ CSuperSerialCard::SIZEOF_SERIALCHOICE_ITEM ]; - if (RegLoadString( TEXT(REG_CONFIG), + TCHAR serialPortName[CSuperSerialCard::SIZEOF_SERIALCHOICE_ITEM]; + if (RegLoadString( + TEXT(REG_CONFIG), TEXT(REGVALUE_SERIAL_PORT_NAME), TRUE, - aySerialPortName, - sizeof(aySerialPortName) ) ) + serialPortName, + CSuperSerialCard::SIZEOF_SERIALCHOICE_ITEM)) { - sg_SSC.SetSerialPortName(aySerialPortName); + sg_SSC.SetSerialPortName(serialPortName); } - REGLOAD(TEXT(REGVALUE_EMULATION_SPEED) ,&g_dwSpeed); + REGLOAD_DEFAULT(TEXT(REGVALUE_EMULATION_SPEED), &g_dwSpeed, SPEED_NORMAL); Config_Load_Video(); SetCurrentCLK6502(); // Pre: g_dwSpeed && Config_Load_Video()->SetVideoRefreshRate() DWORD dwEnhanceDisk; - REGLOAD(TEXT(REGVALUE_ENHANCE_DISK_SPEED), &dwEnhanceDisk); + REGLOAD_DEFAULT(TEXT(REGVALUE_ENHANCE_DISK_SPEED), &dwEnhanceDisk, 1); sg_Disk2Card.SetEnhanceDisk(dwEnhanceDisk ? true : false); - REGLOAD(TEXT("Uthernet Active") ,(DWORD *)&tfe_enabled); + DWORD dwTfeEnabled; + REGLOAD_DEFAULT(TEXT("Uthernet Active"), &dwTfeEnabled, 0); + tfe_enabled = dwTfeEnabled ? 1 : 0; // - DWORD dwTmp; + DWORD dwTmp = 0; if(REGLOAD(TEXT(REGVALUE_FS_SHOW_SUBUNIT_STATUS), &dwTmp)) SetFullScreenShowSubunitStatus(dwTmp ? true : false); @@ -704,10 +701,10 @@ void LoadConfiguration(void) // - char szFilename[MAX_PATH] = {0}; + TCHAR szFilename[MAX_PATH]; - RegLoadString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_HDV_START_DIR), 1, szFilename, MAX_PATH); - if (szFilename[0] == 0) + RegLoadString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_HDV_START_DIR), 1, szFilename, MAX_PATH, TEXT("")); + if (szFilename[0] == '\0') GetCurrentDirectory(sizeof(szFilename), szFilename); SetCurrentImageDir(szFilename); @@ -717,8 +714,8 @@ void LoadConfiguration(void) // // Current/Starting Dir is the "root" of where the user keeps his disk images - RegLoadString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_START_DIR), 1, szFilename, MAX_PATH); - if (szFilename[0] == 0) + RegLoadString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_START_DIR), 1, szFilename, MAX_PATH, TEXT("")); + if (szFilename[0] == '\0') GetCurrentDirectory(sizeof(szFilename), szFilename); SetCurrentImageDir(szFilename); @@ -727,21 +724,17 @@ void LoadConfiguration(void) // - szFilename[0] = 0; - RegLoadString(TEXT(REG_CONFIG),TEXT(REGVALUE_SAVESTATE_FILENAME),1,szFilename,sizeof(szFilename)); + RegLoadString(TEXT(REG_CONFIG), TEXT(REGVALUE_SAVESTATE_FILENAME), 1, szFilename, MAX_PATH, TEXT("")); Snapshot_SetFilename(szFilename); // If not in Registry than default will be used (ie. g_sCurrentDir + default filename) - szFilename[0] = 0; - RegLoadString(TEXT(REG_CONFIG),TEXT(REGVALUE_PRINTER_FILENAME),1,szFilename,sizeof(szFilename)); + RegLoadString(TEXT(REG_CONFIG), TEXT(REGVALUE_PRINTER_FILENAME), 1, szFilename, MAX_PATH, TEXT("")); Printer_SetFilename(szFilename); // If not in Registry than default will be used - dwTmp = 10; - REGLOAD(TEXT(REGVALUE_PRINTER_IDLE_LIMIT), &dwTmp); + REGLOAD_DEFAULT(TEXT(REGVALUE_PRINTER_IDLE_LIMIT), &dwTmp, 10); Printer_SetIdleLimit(dwTmp); - char szUthernetInt[MAX_PATH] = {0}; - RegLoadString(TEXT(REG_CONFIG),TEXT("Uthernet Interface"),1,szUthernetInt,MAX_PATH); - update_tfe_interface(szUthernetInt,NULL); + RegLoadString(TEXT(REG_CONFIG), TEXT("Uthernet Interface"), 1, szFilename, MAX_PATH, TEXT("")); + update_tfe_interface(szFilename, NULL); if (REGLOAD(TEXT(REGVALUE_WINDOW_SCALE), &dwTmp)) SetViewportScale(dwTmp); @@ -1169,14 +1162,15 @@ static void InsertHardDisks(LPSTR szImageName_harddisk[NUM_HARDDISKS], bool& bBo static bool CheckOldAppleWinVersion(void) { - char szOldAppleWinVersion[sizeof(VERSIONSTRING)] = {0}; - RegLoadString(TEXT(REG_CONFIG), TEXT(REGVALUE_VERSION), 1, szOldAppleWinVersion, sizeof(szOldAppleWinVersion)); + TCHAR szOldAppleWinVersion[VERSIONSTRING_SIZE + 1]; + RegLoadString(TEXT(REG_CONFIG), TEXT(REGVALUE_VERSION), 1, szOldAppleWinVersion, VERSIONSTRING_SIZE, TEXT("")); const bool bShowAboutDlg = strcmp(szOldAppleWinVersion, VERSIONSTRING) != 0; // version: xx.yy.zz.ww - // offset : 0123456789 char* p0 = szOldAppleWinVersion; - szOldAppleWinVersion[strlen(szOldAppleWinVersion)] = '.'; // Overwrite null terminator with '.' + int len = strlen(szOldAppleWinVersion); + szOldAppleWinVersion[len] = '.'; // append a null terminator + szOldAppleWinVersion[len + 1] = '\0'; for (UINT i=0; i<4; i++) { char* p1 = strstr(p0, "."); @@ -1515,7 +1509,7 @@ int APIENTRY WinMain(HINSTANCE passinstance, HINSTANCE, LPSTR lpCmdLine, int) unsigned long minor = g_AppleWinVersion[1] = pFixedFileInfo->dwFileVersionMS & 0xffff; unsigned long fix = g_AppleWinVersion[2] = pFixedFileInfo->dwFileVersionLS >> 16; unsigned long fix_minor = g_AppleWinVersion[3] = pFixedFileInfo->dwFileVersionLS & 0xffff; - sprintf(VERSIONSTRING, "%d.%d.%d.%d", major, minor, fix, fix_minor); // potential buffer overflow + StringCbPrintf(VERSIONSTRING, VERSIONSTRING_SIZE, "%d.%d.%d.%d", major, minor, fix, fix_minor); } delete [] pVerInfoBlock; diff --git a/source/Applewin.h b/source/Applewin.h index c1c5899a..91ccf334 100644 --- a/source/Applewin.h +++ b/source/Applewin.h @@ -9,7 +9,7 @@ void LogFileTimeUntilFirstKeyRead(void); bool SetCurrentImageDir(const char* pszImageDir); extern const UINT16* GetOldAppleWinVersion(void); -extern char VERSIONSTRING[]; // Constructed in WinMain() +extern TCHAR VERSIONSTRING[]; // Constructed in WinMain() extern const TCHAR *g_pAppTitle; diff --git a/source/Configuration/About.cpp b/source/Configuration/About.cpp index bc082cc9..0b907569 100644 --- a/source/Configuration/About.cpp +++ b/source/Configuration/About.cpp @@ -27,7 +27,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "../Frame.h" #include "../resource/resource.h" -static const char g_szGPL[] = +static const TCHAR g_szGPL[] = "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.\r\n\ \r\n\ 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.\r\n\ @@ -65,8 +65,8 @@ static BOOL CALLBACK DlgProcAbout(HWND hWnd, UINT message, WPARAM wparam, LPARAM HICON hIcon = LoadIcon(g_hInstance, TEXT("APPLEWIN_ICON")); SendDlgItemMessage(hWnd, IDC_APPLEWIN_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon); - char szAppleWinVersion[50]; - sprintf(szAppleWinVersion, "AppleWin v%s", VERSIONSTRING); + TCHAR szAppleWinVersion[50]; + StringCbPrintf(szAppleWinVersion, 50, "AppleWin v%s", VERSIONSTRING); SendDlgItemMessage(hWnd, IDC_APPLEWIN_VERSION, WM_SETTEXT, 0, (LPARAM)szAppleWinVersion); SendDlgItemMessage(hWnd, IDC_GPL_TEXT, WM_SETTEXT, 0, (LPARAM)g_szGPL); diff --git a/source/Configuration/PageConfig.cpp b/source/Configuration/PageConfig.cpp index 7d548589..6fdf6bb1 100644 --- a/source/Configuration/PageConfig.cpp +++ b/source/Configuration/PageConfig.cpp @@ -217,8 +217,9 @@ BOOL CPageConfig::DlgProcInternal(HWND hWnd, UINT message, WPARAM wparam, LPARAM BOOL bCustom = TRUE; if (g_dwSpeed == SPEED_NORMAL) { - bCustom = FALSE; - REGLOAD(TEXT(REGVALUE_CUSTOM_SPEED),(DWORD *)&bCustom); + DWORD dwCustomSpeed; + REGLOAD_DEFAULT(TEXT(REGVALUE_CUSTOM_SPEED), &dwCustomSpeed, 0); + bCustom = dwCustomSpeed ? TRUE : FALSE; } CheckRadioButton(hWnd, IDC_AUTHENTIC_SPEED, IDC_CUSTOM_SPEED, bCustom ? IDC_CUSTOM_SPEED : IDC_AUTHENTIC_SPEED); SetFocus(GetDlgItem(hWnd, bCustom ? IDC_SLIDER_CPU_SPEED : IDC_AUTHENTIC_SPEED)); diff --git a/source/Configuration/PageConfigTfe.cpp b/source/Configuration/PageConfigTfe.cpp index c12f644b..929c1cf2 100644 --- a/source/Configuration/PageConfigTfe.cpp +++ b/source/Configuration/PageConfigTfe.cpp @@ -143,10 +143,10 @@ int CPageConfigTfe::gray_ungray_items(HWND hwnd) int enable; int number; - int disabled = 0; - //resources_get_value("ETHERNET_DISABLED", (void *)&disabled); - REGLOAD(TEXT("Uthernet Disabled") ,(DWORD *)&disabled); + DWORD dwDisabled; + REGLOAD_DEFAULT(TEXT("Uthernet Disabled"), &dwDisabled, 0); + int disabled = dwDisabled ? 1 : 0; get_disabled_state(&disabled); if (disabled) diff --git a/source/Configuration/PageDisk.cpp b/source/Configuration/PageDisk.cpp index 5ab97f43..c8d3e9ac 100644 --- a/source/Configuration/PageDisk.cpp +++ b/source/Configuration/PageDisk.cpp @@ -148,8 +148,8 @@ BOOL CPageDisk::DlgProcInternal(HWND hWnd, UINT message, WPARAM wparam, LPARAM l InitComboHDD(hWnd); - TCHAR PathToCiderPress[MAX_PATH] = ""; - RegLoadString(TEXT(REG_CONFIG), REGVALUE_CIDERPRESSLOC, 1, PathToCiderPress,MAX_PATH); + TCHAR PathToCiderPress[MAX_PATH]; + RegLoadString(TEXT(REG_CONFIG), REGVALUE_CIDERPRESSLOC, 1, PathToCiderPress, MAX_PATH, TEXT("")); SendDlgItemMessage(hWnd, IDC_CIDERPRESS_FILENAME ,WM_SETTEXT, 0, (LPARAM)PathToCiderPress); CheckDlgButton(hWnd, IDC_HDD_ENABLE, HD_CardIsEnabled() ? BST_CHECKED : BST_UNCHECKED); diff --git a/source/Configuration/PropertySheetHelper.cpp b/source/Configuration/PropertySheetHelper.cpp index 94967fc4..f66381fa 100644 --- a/source/Configuration/PropertySheetHelper.cpp +++ b/source/Configuration/PropertySheetHelper.cpp @@ -141,8 +141,7 @@ std::string CPropertySheetHelper::BrowseToFile(HWND hWindow, TCHAR* pszTitle, TC strcpy(PathToFile, Snapshot_GetFilename()); //RAPCS, line 2. TCHAR szDirectory[MAX_PATH] = TEXT(""); TCHAR szFilename[MAX_PATH]; - strcpy(szFilename, ""); - RegLoadString(TEXT("Configuration"), REGVALUE, 1, szFilename ,MAX_PATH); + RegLoadString(TEXT("Configuration"), REGVALUE, 1, szFilename, MAX_PATH, TEXT("")); std::string PathName = szFilename; OPENFILENAME ofn; @@ -175,7 +174,7 @@ std::string CPropertySheetHelper::BrowseToFile(HWND hWindow, TCHAR* pszTitle, TC } else // Cancel is pressed { - RegLoadString(TEXT("Configuration"), REGVALUE, 1, szFilename,MAX_PATH); + RegLoadString(TEXT("Configuration"), REGVALUE, 1, szFilename, MAX_PATH, TEXT("")); PathName = szFilename; } diff --git a/source/Disk.cpp b/source/Disk.cpp index 8f88fbc7..864843b6 100644 --- a/source/Disk.cpp +++ b/source/Disk.cpp @@ -154,17 +154,13 @@ void Disk2InterfaceCard::LoadLastDiskImage(const int drive) { _ASSERT(drive == DRIVE_1 || drive == DRIVE_2); - char sFilePath[ MAX_PATH + 1]; - sFilePath[0] = 0; + const TCHAR *pRegKey = (drive == DRIVE_1) + ? TEXT(REGVALUE_PREF_LAST_DISK_1) + : TEXT(REGVALUE_PREF_LAST_DISK_2); - const char *pRegKey = (drive == DRIVE_1) - ? REGVALUE_PREF_LAST_DISK_1 - : REGVALUE_PREF_LAST_DISK_2; - - if (RegLoadString(TEXT(REG_PREFS), pRegKey, 1, sFilePath, MAX_PATH)) + TCHAR sFilePath[MAX_PATH]; + if (RegLoadString(TEXT(REG_PREFS), pRegKey, 1, sFilePath, MAX_PATH, TEXT(""))) { - sFilePath[ MAX_PATH ] = 0; - m_saveDiskImage = false; // Pass in ptr to local copy of filepath, since RemoveDisk() sets DiskPathFilename = "" InsertDisk(drive, sFilePath, IMAGE_USE_FILES_WRITE_PROTECT_STATUS, IMAGE_DONT_CREATE); @@ -181,21 +177,21 @@ void Disk2InterfaceCard::SaveLastDiskImage(const int drive) if (!m_saveDiskImage) return; - const char *pFileName = m_floppyDrive[drive].m_disk.m_fullname; + const TCHAR *pFileName = m_floppyDrive[drive].m_disk.m_fullname; if (drive == DRIVE_1) - RegSaveString(TEXT(REG_PREFS), REGVALUE_PREF_LAST_DISK_1, TRUE, pFileName); + RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_DISK_1), TRUE, pFileName); else - RegSaveString(TEXT(REG_PREFS), REGVALUE_PREF_LAST_DISK_2, TRUE, pFileName); + RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_DISK_2), TRUE, pFileName); // - char szPathName[MAX_PATH]; - strcpy(szPathName, DiskGetFullPathName(drive)); - if (_tcsrchr(szPathName, TEXT('\\'))) + TCHAR szPathName[MAX_PATH]; + StringCbCopy(szPathName, MAX_PATH, DiskGetFullPathName(drive)); + TCHAR* slash = _tcsrchr(szPathName, TEXT('\\')); + if (slash != NULL) { - char* pPathEnd = _tcsrchr(szPathName, TEXT('\\'))+1; - *pPathEnd = 0; + slash[1] = '\0'; RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_START_DIR), 1, szPathName); } } @@ -678,52 +674,51 @@ bool Disk2InterfaceCard::IsConditionForFullSpeed(void) void Disk2InterfaceCard::NotifyInvalidImage(const int drive, LPCTSTR pszImageFilename, const ImageError_e Error) { - TCHAR szBuffer[MAX_PATH+128]; - szBuffer[sizeof(szBuffer)-1] = 0; + TCHAR szBuffer[MAX_PATH + 128]; switch (Error) { case eIMAGE_ERROR_UNABLE_TO_OPEN: case eIMAGE_ERROR_UNABLE_TO_OPEN_GZ: case eIMAGE_ERROR_UNABLE_TO_OPEN_ZIP: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to open the file %s."), pszImageFilename); break; case eIMAGE_ERROR_BAD_SIZE: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to use the file %s\nbecause the ") TEXT("disk image is an unsupported size."), pszImageFilename); break; case eIMAGE_ERROR_BAD_FILE: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to use the file %s\nbecause the ") TEXT("OS can't access it."), pszImageFilename); break; case eIMAGE_ERROR_UNSUPPORTED: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to use the file %s\nbecause the ") TEXT("disk image format is not recognized."), pszImageFilename); break; case eIMAGE_ERROR_UNSUPPORTED_HDV: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to use the file %s\n") TEXT("because this UniDisk 3.5/Apple IIGS/hard-disk image is not supported.\n") TEXT("Try inserting as a hard-disk image instead."), @@ -731,9 +726,9 @@ void Disk2InterfaceCard::NotifyInvalidImage(const int drive, LPCTSTR pszImageFil break; case eIMAGE_ERROR_UNSUPPORTED_MULTI_ZIP: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to use the file %s\nbecause the ") TEXT("first file (%s) in this multi-zip archive is not recognized.\n") TEXT("Try unzipping and using the disk images directly.\n"), @@ -743,34 +738,34 @@ void Disk2InterfaceCard::NotifyInvalidImage(const int drive, LPCTSTR pszImageFil case eIMAGE_ERROR_GZ: case eIMAGE_ERROR_ZIP: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to use the compressed file %s\nbecause the ") TEXT("compressed disk image is corrupt/unsupported."), pszImageFilename); break; case eIMAGE_ERROR_FAILED_TO_GET_PATHNAME: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unable to GetFullPathName() for the file: %s."), pszImageFilename); break; case eIMAGE_ERROR_ZEROLENGTH_WRITEPROTECTED: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Unsupported zero-length write-protected file: %s."), pszImageFilename); break; case eIMAGE_ERROR_FAILED_TO_INIT_ZEROLENGTH: - _snprintf( + StringCbPrintf( szBuffer, - sizeof(szBuffer)-1, + MAX_PATH + 128, TEXT("Failed to resize the zero-length file: %s."), pszImageFilename); break; @@ -1346,8 +1341,8 @@ void Disk2InterfaceCard::DumpTrackWOZ(FloppyDisk floppy) // pass a copy of m_flo nibbleCount++; - char str[10]; - sprintf(str, "%02X ", shiftReg); + TCHAR str[10]; + StringCbPrintf(str, 10, "%02X ", shiftReg); OutputDebugString(str); if ((nibbleCount % 32) == 0) OutputDebugString("\n"); @@ -1400,15 +1395,14 @@ void Disk2InterfaceCard::ResetSwitches(void) bool Disk2InterfaceCard::UserSelectNewDiskImage(const int drive, LPCSTR pszFilename/*=""*/) { - TCHAR directory[MAX_PATH] = TEXT(""); - TCHAR filename[MAX_PATH] = TEXT(""); + TCHAR directory[MAX_PATH]; + TCHAR filename[MAX_PATH]; TCHAR title[40]; - strcpy(filename, pszFilename); + StringCbCopy(filename, MAX_PATH, pszFilename); - RegLoadString(TEXT(REG_PREFS), REGVALUE_PREF_START_DIR, 1, directory, MAX_PATH); - _tcscpy(title, TEXT("Select Disk Image For Drive ")); - _tcscat(title, drive ? TEXT("2") : TEXT("1")); + RegLoadString(TEXT(REG_PREFS), REGVALUE_PREF_START_DIR, 1, directory, MAX_PATH, TEXT("")); + StringCbPrintf(title, 40, TEXT("Select Disk Image For Drive %d"), drive + 1); _ASSERT(sizeof(OPENFILENAME) == sizeof(OPENFILENAME_NT4)); // Required for Win98/ME support (selected by _WIN32_WINNT=0x0400 in stdafx.h) @@ -1431,7 +1425,7 @@ bool Disk2InterfaceCard::UserSelectNewDiskImage(const int drive, LPCSTR pszFilen if (GetOpenFileName(&ofn)) { if ((!ofn.nFileExtension) || !filename[ofn.nFileExtension]) - _tcscat(filename,TEXT(".dsk")); + StringCbCat(filename, MAX_PATH, TEXT(".dsk")); ImageError_e Error = InsertDisk(drive, filename, ofn.Flags & OFN_READONLY, IMAGE_CREATE); if (Error == eIMAGE_ERROR_NONE) diff --git a/source/Frame.cpp b/source/Frame.cpp index d37291be..3038076d 100644 --- a/source/Frame.cpp +++ b/source/Frame.cpp @@ -2050,10 +2050,17 @@ static void ProcessButtonClick(int button, bool bFromButtonUI /*=false*/) // http://www.codeproject.com/menu/MenusForBeginners.asp?df=100&forumid=67645&exp=0&select=903061 void ProcessDiskPopupMenu(HWND hwnd, POINT pt, const int iDrive) -{ - //This is the default installation path of CiderPress. It shall not be left blank, otherwise an explorer window will be open. - TCHAR PathToCiderPress[MAX_PATH] = "C:\\Program Files\\faddenSoft\\CiderPress\\CiderPress.exe"; - RegLoadString(TEXT("Configuration"), REGVALUE_CIDERPRESSLOC, 1, PathToCiderPress,MAX_PATH); +{ + // This is the default installation path of CiderPress. + // It shall not be left blank, otherwise an explorer window will be open. + TCHAR PathToCiderPress[MAX_PATH]; + RegLoadString( + TEXT("Configuration"), + REGVALUE_CIDERPRESSLOC, + 1, + PathToCiderPress, + MAX_PATH, + TEXT("C:\\Program Files\\faddenSoft\\CiderPress\\CiderPress.exe")); //TODO: A directory is open if an empty path to CiderPress is set. This has to be fixed. std::string filename1= "\""; diff --git a/source/Harddisk.cpp b/source/Harddisk.cpp index 23f173a3..74c28f27 100644 --- a/source/Harddisk.cpp +++ b/source/Harddisk.cpp @@ -210,17 +210,13 @@ void HD_LoadLastDiskImage(const int iDrive) { _ASSERT(iDrive == HARDDISK_1 || iDrive == HARDDISK_2); - char sFilePath[ MAX_PATH + 1]; - sFilePath[0] = 0; - const char *pRegKey = (iDrive == HARDDISK_1) ? REGVALUE_PREF_LAST_HARDDISK_1 : REGVALUE_PREF_LAST_HARDDISK_2; - if (RegLoadString(TEXT(REG_PREFS), pRegKey, 1, sFilePath, MAX_PATH)) + TCHAR sFilePath[MAX_PATH]; + if (RegLoadString(TEXT(REG_PREFS), pRegKey, 1, sFilePath, MAX_PATH, TEXT(""))) { - sFilePath[ MAX_PATH ] = 0; - g_bSaveDiskImage = false; // Pass in ptr to local copy of filepath, since RemoveDisk() sets DiskPathFilename = "" // todo: update comment for HD func HD_Insert(iDrive, sFilePath); @@ -419,13 +415,13 @@ BOOL HD_Insert(const int iDrive, LPCTSTR pszImageFilename) static bool HD_SelectImage(const int iDrive, LPCSTR pszFilename) { - TCHAR directory[MAX_PATH] = TEXT(""); - TCHAR filename[MAX_PATH] = TEXT(""); + TCHAR directory[MAX_PATH]; + TCHAR filename[MAX_PATH]; TCHAR title[40]; strcpy(filename, pszFilename); - RegLoadString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_HDV_START_DIR), 1, directory, MAX_PATH); + RegLoadString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_HDV_START_DIR), 1, directory, MAX_PATH, TEXT("")); _tcscpy(title, TEXT("Select HDV Image For HDD ")); _tcscat(title, iDrive ? TEXT("2") : TEXT("1")); diff --git a/source/Registry.cpp b/source/Registry.cpp index 06d81b16..83adb8ab 100644 --- a/source/Registry.cpp +++ b/source/Registry.cpp @@ -30,70 +30,95 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //=========================================================================== -BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, - LPTSTR buffer, DWORD chars) { - int success = 0; - TCHAR fullkeyname[256]; - wsprintf(fullkeyname, - TEXT("Software\\AppleWin\\CurrentVersion\\%s"), - (LPCTSTR)section); - HKEY keyhandle; - if (!RegOpenKeyEx((peruser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE), - fullkeyname, - 0, - KEY_READ, - &keyhandle)) { - DWORD type; - DWORD size = chars; - success = (!RegQueryValueEx(keyhandle,key,0,&type,(LPBYTE)buffer,&size)) && - size; - RegCloseKey(keyhandle); - } - return success; +BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars) +{ + TCHAR fullkeyname[256]; + StringCbPrintf(fullkeyname, 256, TEXT("Software\\AppleWin\\CurrentVersion\\%s"), section); + + BOOL success = FALSE; + HKEY keyhandle; + LSTATUS status = RegOpenKeyEx( + (peruser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE), + fullkeyname, + 0, + KEY_READ, + &keyhandle); + if (status == 0) + { + DWORD type; + DWORD size = chars; + status = RegQueryValueEx(keyhandle, key, NULL, &type, (LPBYTE)buffer, &size); + if (status == 0 && size != 0) + success = TRUE; + } + + RegCloseKey(keyhandle); + + return success; } //=========================================================================== -BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD *value) { - if (!value) - return 0; - TCHAR buffer[32] = TEXT(""); - if (!RegLoadString(section,key,peruser,buffer,32)) - return 0; - buffer[31] = 0; - *value = (DWORD)_ttoi(buffer); - return 1; +BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars, LPCTSTR defaultValue) +{ + BOOL success = RegLoadString(section, key, peruser, buffer, chars); + if (!success) + StringCbCopy(buffer, chars, defaultValue); + return success; +} + +//=========================================================================== +BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value) { + TCHAR buffer[32]; + if (!RegLoadString(section, key, peruser, buffer, 32)) + { + return FALSE; + } + + *value = (DWORD)_ttoi(buffer); + return TRUE; +} + +//=========================================================================== +BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value, DWORD defaultValue) { + BOOL success = RegLoadValue(section, key, peruser, value); + if (!success) + *value = defaultValue; + return success; } //=========================================================================== void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPCTSTR buffer) { - TCHAR fullkeyname[256]; - wsprintf(fullkeyname, - TEXT("Software\\AppleWin\\CurrentVersion\\%s"), - (LPCTSTR)section); - HKEY keyhandle; - DWORD disposition; - if (!RegCreateKeyEx((peruser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE), - fullkeyname, - 0, - NULL, - REG_OPTION_NON_VOLATILE, - KEY_READ | KEY_WRITE, - (LPSECURITY_ATTRIBUTES)NULL, - &keyhandle, - &disposition)) { - RegSetValueEx(keyhandle, - key, - 0, - REG_SZ, - (CONST BYTE *)buffer, - (_tcslen(buffer)+1)*sizeof(TCHAR)); - RegCloseKey(keyhandle); - } + TCHAR fullkeyname[256]; + StringCbPrintf(fullkeyname, 256, TEXT("Software\\AppleWin\\CurrentVersion\\%s"), section); + + HKEY keyhandle; + DWORD disposition; + LSTATUS status = RegCreateKeyEx( + (peruser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE), + fullkeyname, + 0, + NULL, + REG_OPTION_NON_VOLATILE, + KEY_READ | KEY_WRITE, + (LPSECURITY_ATTRIBUTES)NULL, + &keyhandle, + &disposition); + if (status == 0) + { + RegSetValueEx( + keyhandle, + key, + 0, + REG_SZ, + (CONST LPBYTE)buffer, + (_tcslen(buffer) + 1) * sizeof(TCHAR)); + RegCloseKey(keyhandle); + } } //=========================================================================== void RegSaveValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD value) { - TCHAR buffer[32] = TEXT(""); - _ultot(value,buffer,10); - RegSaveString(section,key,peruser,buffer); + TCHAR buffer[32] = TEXT(""); + StringCbPrintf(buffer, 32, "%d", value); + RegSaveString(section, key, peruser, buffer); } diff --git a/source/Registry.h b/source/Registry.h index 62defc4d..d4dbb891 100644 --- a/source/Registry.h +++ b/source/Registry.h @@ -1,11 +1,12 @@ #pragma once -#define REGLOAD(a,b) RegLoadValue(TEXT(REG_CONFIG),a,1,b) -#define REGSAVE(a,b) RegSaveValue(TEXT(REG_CONFIG),a,1,b) +#define REGLOAD(a, b) RegLoadValue(TEXT(REG_CONFIG), (a), TRUE, (b)) +#define REGLOAD_DEFAULT(a, b, c) RegLoadValue(TEXT(REG_CONFIG), (a), TRUE, (b), (c)) +#define REGSAVE(a, b) RegSaveValue(TEXT(REG_CONFIG), (a), TRUE, (b)) -BOOL RegLoadString (LPCTSTR,LPCTSTR,BOOL,LPTSTR,DWORD); -BOOL RegLoadValue (LPCTSTR,LPCTSTR,BOOL,DWORD *); -void RegSaveString (LPCTSTR,LPCTSTR,BOOL,LPCTSTR); -void RegSaveValue (LPCTSTR,LPCTSTR,BOOL,DWORD); - -BOOL RegLoadValue (LPCTSTR,LPCTSTR,BOOL,BOOL *); +BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars); +BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars, LPCTSTR defaultValue); +BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value); +BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value, DWORD defaultValue); +void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPCTSTR buffer); +void RegSaveValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD value); diff --git a/source/StdAfx.h b/source/StdAfx.h index f8dc4d5c..646ec14f 100644 --- a/source/StdAfx.h +++ b/source/StdAfx.h @@ -36,6 +36,7 @@ typedef UINT64 uint64_t; #include #include // WM_MOUSEWHEEL +#include #include #include #include diff --git a/source/Video.cpp b/source/Video.cpp index a5229d6d..94aef116 100644 --- a/source/Video.cpp +++ b/source/Video.cpp @@ -125,9 +125,9 @@ static bool g_bVideoScannerNTSC = true; // NTSC video scanning (or PAL) bool g_bDisplayPrintScreenFileName = false; bool g_bShowPrintScreenWarningDialog = true; - void Util_MakeScreenShotFileName( char *pFinalFileName_ ); - bool Util_TestScreenShotFileName( const char *pFileName ); - void Video_SaveScreenShot( const VideoScreenShot_e ScreenShotType, const char *pScreenShotFileName ); + void Util_MakeScreenShotFileName( TCHAR *pFinalFileName_, DWORD chars ); + bool Util_TestScreenShotFileName( const TCHAR *pFileName ); + void Video_SaveScreenShot( const VideoScreenShot_e ScreenShotType, const TCHAR *pScreenShotFileName ); void Video_MakeScreenShot( FILE *pFile, const VideoScreenShot_e ScreenShotType ); void videoCreateDIBSection(); @@ -439,8 +439,8 @@ void VideoDisplayLogo () SetTextAlign(hFrameDC,TA_RIGHT | TA_TOP); SetBkMode(hFrameDC,TRANSPARENT); - char szVersion[ 64 ] = ""; - sprintf( szVersion, "Version %s", VERSIONSTRING ); + TCHAR szVersion[ 64 ]; + StringCbPrintf(szVersion, 64, "Version %s", VERSIONSTRING); int xoff = GetFullScreenOffsetX(), yoff = GetFullScreenOffsetY(); #define DRAWVERSION(x,y,c) \ @@ -461,7 +461,7 @@ void VideoDisplayLogo () } #if _DEBUG - sprintf( szVersion, "DEBUG" ); + StringCbPrintf(szVersion, 64, "DEBUG"); DRAWVERSION( 2, -358*scale,RGB(0x00,0x00,0x00)); DRAWVERSION( 1, -357*scale,RGB(0x00,0x00,0x00)); DRAWVERSION( 0, -356*scale,RGB(0xFF,0x00,0xFF)); @@ -902,32 +902,32 @@ bool VideoGetVblBar(const DWORD uExecutedCycles) static int g_nLastScreenShot = 0; const int nMaxScreenShot = 999999999; -static char *g_pLastDiskImageName = NULL; +static TCHAR *g_pLastDiskImageName = NULL; //=========================================================================== -void Video_ResetScreenshotCounter( char *pImageName ) +void Video_ResetScreenshotCounter( TCHAR *pImageName ) { g_nLastScreenShot = 0; g_pLastDiskImageName = pImageName; } //=========================================================================== -void Util_MakeScreenShotFileName( char *pFinalFileName_ ) +void Util_MakeScreenShotFileName( TCHAR *pFinalFileName_, DWORD chars ) { - char sPrefixScreenShotFileName[ 256 ] = "AppleWin_ScreenShot"; + const TCHAR * sPrefixScreenShotFileName = "AppleWin_ScreenShot"; // TODO: g_sScreenshotDir - char *pPrefixFileName = g_pLastDiskImageName ? g_pLastDiskImageName : sPrefixScreenShotFileName; + const TCHAR *pPrefixFileName = g_pLastDiskImageName ? g_pLastDiskImageName : sPrefixScreenShotFileName; #if SCREENSHOT_BMP - sprintf( pFinalFileName_, "%s_%09d.bmp", pPrefixFileName, g_nLastScreenShot ); + StringCbPrintf( pFinalFileName_, chars, TEXT("%s_%09d.bmp"), pPrefixFileName, g_nLastScreenShot ); #endif #if SCREENSHOT_TGA - sprintf( pFinalFileName_, "%s%09d.tga", pPrefixFileName, g_nLastScreenShot ); + StringCbPrintf( pFinalFileName_, chars, TEXT("%s%09d.tga"), pPrefixFileName, g_nLastScreenShot ); #endif } // Returns TRUE if file exists, else FALSE //=========================================================================== -bool Util_TestScreenShotFileName( const char *pFileName ) +bool Util_TestScreenShotFileName( const TCHAR *pFileName ) { bool bFileExists = false; FILE *pFile = fopen( pFileName, "rt" ); @@ -942,7 +942,7 @@ bool Util_TestScreenShotFileName( const char *pFileName ) //=========================================================================== void Video_TakeScreenShot( const VideoScreenShot_e ScreenShotType ) { - char sScreenShotFileName[ MAX_PATH ]; + TCHAR sScreenShotFileName[ MAX_PATH ]; // find last screenshot filename so we don't overwrite the existing user ones bool bExists = true; @@ -950,13 +950,14 @@ void Video_TakeScreenShot( const VideoScreenShot_e ScreenShotType ) { if (g_nLastScreenShot > nMaxScreenShot) // Holy Crap! User has maxed the number of screenshots!? { - sprintf( sScreenShotFileName, "You have more then %d screenshot filenames! They will no longer be saved.\n\nEither move some of your screenshots or increase the maximum in video.cpp\n", nMaxScreenShot ); - MessageBox( g_hFrameWindow, sScreenShotFileName, "Warning", MB_OK ); + TCHAR msg[512]; + StringCbPrintf( msg, 512, "You have more then %d screenshot filenames! They will no longer be saved.\n\nEither move some of your screenshots or increase the maximum in video.cpp\n", nMaxScreenShot ); + MessageBox( g_hFrameWindow, msg, "Warning", MB_OK ); g_nLastScreenShot = 0; return; } - Util_MakeScreenShotFileName( sScreenShotFileName ); + Util_MakeScreenShotFileName( sScreenShotFileName, MAX_PATH ); bExists = Util_TestScreenShotFileName( sScreenShotFileName ); if( !bExists ) { @@ -969,7 +970,7 @@ void Video_TakeScreenShot( const VideoScreenShot_e ScreenShotType ) g_nLastScreenShot++; } -void Video_RedrawAndTakeScreenShot( const char* pScreenshotFilename ) +void Video_RedrawAndTakeScreenShot( const TCHAR* pScreenshotFilename ) { _ASSERT(pScreenshotFilename); if (!pScreenshotFilename) @@ -1137,7 +1138,7 @@ static void Video_MakeScreenShot(FILE *pFile, const VideoScreenShot_e ScreenShot } //=========================================================================== -static void Video_SaveScreenShot( const VideoScreenShot_e ScreenShotType, const char *pScreenShotFileName ) +static void Video_SaveScreenShot( const VideoScreenShot_e ScreenShotType, const TCHAR *pScreenShotFileName ) { FILE *pFile = fopen( pScreenShotFileName, "wb" ); if( pFile ) @@ -1162,7 +1163,7 @@ static BYTE g_videoRom[kVideoRomSizeMax]; static UINT g_videoRomSize = 0; static bool g_videoRomRockerSwitch = false; -bool ReadVideoRomFile(const char* pRomFile) +bool ReadVideoRomFile(const TCHAR* pRomFile) { g_videoRomSize = 0; @@ -1227,23 +1228,29 @@ enum VideoType127_e void Config_Load_Video() { - REGLOAD(TEXT(REGVALUE_VIDEO_MODE) ,&g_eVideoType); - REGLOAD(TEXT(REGVALUE_VIDEO_STYLE) ,(DWORD*)&g_eVideoStyle); - REGLOAD(TEXT(REGVALUE_VIDEO_MONO_COLOR),&g_nMonochromeRGB); + DWORD dwTmp; - DWORD rate = VR_60HZ; - REGLOAD(TEXT(REGVALUE_VIDEO_REFRESH_RATE), &rate); - SetVideoRefreshRate((VideoRefreshRate_e)rate); + REGLOAD_DEFAULT(TEXT(REGVALUE_VIDEO_MODE), &dwTmp, (DWORD)VT_DEFAULT); + g_eVideoType = dwTmp; + + REGLOAD_DEFAULT(TEXT(REGVALUE_VIDEO_STYLE), &dwTmp, (DWORD)VS_HALF_SCANLINES); + g_eVideoStyle = (VideoStyle_e)dwTmp; + + REGLOAD_DEFAULT(TEXT(REGVALUE_VIDEO_MONO_COLOR), &dwTmp, (DWORD)RGB(0xC0, 0xC0, 0xC0)); + g_nMonochromeRGB = (COLORREF)dwTmp; + + REGLOAD_DEFAULT(TEXT(REGVALUE_VIDEO_REFRESH_RATE), &dwTmp, (DWORD)VR_60HZ); + SetVideoRefreshRate((VideoRefreshRate_e)dwTmp); // const UINT16* pOldVersion = GetOldAppleWinVersion(); if (pOldVersion[0] == 1 && pOldVersion[1] <= 28 && pOldVersion[2] <= 1) { - DWORD halfScanLines = 0; - REGLOAD(TEXT(REGVALUE_VIDEO_HALF_SCAN_LINES),&halfScanLines); + DWORD dwHalfScanLines; + REGLOAD_DEFAULT(TEXT(REGVALUE_VIDEO_HALF_SCAN_LINES), &dwHalfScanLines, 0); - if (halfScanLines) + if (dwHalfScanLines) g_eVideoStyle = (VideoStyle_e) ((DWORD)g_eVideoStyle | VS_HALF_SCANLINES); else g_eVideoStyle = (VideoStyle_e) ((DWORD)g_eVideoStyle & ~VS_HALF_SCANLINES);