Revision: 8817 https://osdn.net/projects/ttssh2/scm/svn/commits/8817 Author: zmatsuo Date: 2020-06-26 00:28:14 +0900 (Fri, 26 Jun 2020) Log Message: ----------- crosses initialization エラーを修正 - gcc,clang ででるエラーを修正 - パスも含めたファイル名の MAX_PATH 制限をなくした Modified Paths: -------------- trunk/teraterm/susie_plugin/libsusieplugin.cpp -------------- next part -------------- Modified: trunk/teraterm/susie_plugin/libsusieplugin.cpp =================================================================== --- trunk/teraterm/susie_plugin/libsusieplugin.cpp 2020-06-25 05:19:41 UTC (rev 8816) +++ trunk/teraterm/susie_plugin/libsusieplugin.cpp 2020-06-25 15:28:14 UTC (rev 8817) @@ -58,11 +58,26 @@ * \x89摜\x83t\x83@\x83C\x83\x8B\x96\xBC\x82̓v\x83\x89\x83O\x83C\x83\x93\x93\xE0\x82ő\xBD\x95\xAA\x8Eg\x97p\x82\xB3\x82\xEA\x82Ȃ\xA2 * \x83v\x83\x89\x83O\x83C\x83\x93\x93\xE0\x82Ńt\x83@\x83C\x83\x8B\x82͈\xB5\x82\xED\x82Ȃ\xA2\x82̂\xC5 Unicode\x89\xBB\x82\xCDok\x82Ǝv\x82\xED\x82\xEA\x82\xE9 */ -BOOL LoadPictureWithSPI(const wchar_t *nameSPI, const wchar_t *nameFile, unsigned char *bufFile, long sizeFile, HLOCAL *hbuf, +BOOL LoadPictureWithSPI(const wchar_t *nameSPI, const wchar_t *nameFile, unsigned char *bufFile, size_t sizeFile, HLOCAL *hbuf, HLOCAL *hbmi) { + // \x89摜\x83t\x83@\x83C\x83\x8B\x82̃t\x83@\x83C\x83\x8B\x96\xBC\x95\x94\x95\xAA\x82\xF0\x8E\xE6\x82\xE8\x8Fo\x82\xB7 + const wchar_t *image_base = wcsrchr(nameFile, L'\\'); + if (image_base != NULL) { + image_base++; + } + else { + image_base = wcsrchr(nameFile, L'/'); + if (image_base != NULL) { + image_base++; + } + else { + image_base = nameFile; + } + } + char nameFileA[MAX_PATH]; - WideCharToMultiByte(CP_ACP, 0, nameFile, -1, nameFileA, _countof(nameFileA), NULL, NULL); + WideCharToMultiByte(CP_ACP, 0, image_base, -1, nameFileA, _countof(nameFileA), NULL, NULL); HINSTANCE hSPI; char spiVersion[8]; @@ -77,10 +92,10 @@ // SPI \x82\xF0\x83\x8D\x81[\x83h hSPI = LoadLibraryW(nameSPI); + if (!hSPI) { + return FALSE; + } - if (!hSPI) - goto error; - FARPROC *p = (FARPROC *)&SPI_GetPluginInfo; *p = GetProcAddress(hSPI, "GetPluginInfo"); p = (FARPROC *)&SPI_IsSupported; @@ -113,28 +128,11 @@ return ret; } -/** - * Susie\x83v\x83\x89\x83O\x83C\x83\x93\x82\xF0\x8Eg\x82\xC1\x82ĉ摜\x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x8D\x81[\x83h\x82\xB7\x82\xE9 - * \x8Ew\x92\xE8\x83t\x83H\x83\x8B\x83_\x93\xE0\x82̃v\x83\x89\x83O\x83C\x83\x93\x82\xF0\x8Eg\x82\xC1\x82ă\x8D\x81[\x83h\x82\xF0\x8E\x8E\x82݂\xE9 - * - * @param[in] image_file \x89摜\x83t\x83@\x83C\x83\x8B - * @param[in] spi_path "c:\\path\\to\\spi" - * \x82\xB1\x82̃t\x83H\x83\x8B\x83_\x82ɂ\xA0\x82\xE9\x83v\x83\x89\x83O\x83C\x83\x93\x83t\x83@\x83C\x83\x8B\x82ʼn摜\x82̃\x8D\x81[\x83h\x82\xF0\x8E\x8E\x82݂\xE9 - * \x83p\x83X\x82̍Ō\xE3\x82\xC9 "\\" \x82\xAA\x82\xA0\x82\xC1\x82Ă\xE0\x82Ȃ\xAD\x82Ă\xE0\x97ǂ\xA2 - * @param[out] pHBInfo BITMAPINFO LocalFree()\x82\xB7\x82邱\x82\xC6 - * @param[out] pHBm bitmap data LocalFree()\x82\xB7\x82邱\x82\xC6 - * @retval TRUE \x83\x8D\x81[\x83hok - * @retval FALSE \x83\x8D\x81[\x83h\x82ł\xAB\x82Ȃ\xA9\x82\xC1\x82\xBD - */ -BOOL SusieLoadPicture(const wchar_t *image_file, const wchar_t *spi_path, HANDLE *pHBInfo, HANDLE *pHBm) +static unsigned char *LoadImageFile(const wchar_t *image_file, size_t *file_size) { - BOOL result = FALSE; - *pHBInfo = NULL; - *pHBm = NULL; - - //\x83t\x83@\x83C\x83\x8B\x82\xF0\x93ǂݍ\x9E\x82\xDE HANDLE hPictureFile = CreateFileW(image_file, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hPictureFile == INVALID_HANDLE_VALUE) { + *file_size = 0; return FALSE; } DWORD fileSize = GetFileSize(hPictureFile, 0); @@ -148,61 +146,112 @@ ReadFile(hPictureFile, fileBuf, fileSize, &readByte, 0); CloseHandle(hPictureFile); - // spi_path \x82\xF0\x90\xE2\x91p\x83X\x82ɕϊ\xB7 - wchar_t spi_path_full[MAX_PATH]; - if (!GetFullPathNameW(spi_path, _countof(spi_path_full), spi_path_full, NULL)) { - goto finish; + *file_size = fileSize; + return fileBuf; +} + +static wchar_t *NormalizePath(const wchar_t *path) +{ + size_t len = GetFullPathNameW(path, 0, NULL, NULL); // include L'\0' + if (len == 0) { + return NULL; } - wchar_t *p = wcsrchr(spi_path_full, L'\\'); + wchar_t *normalized_path = (wchar_t *)malloc(sizeof(wchar_t) * len); + len = GetFullPathNameW(path, (DWORD)len, normalized_path, NULL); + if (len == 0) { + free(normalized_path); + return NULL; + } + wchar_t *p = wcsrchr(normalized_path, L'\\'); if (p != NULL) { if (*(p + 1) == 0) { *p = 0; // delete last '\\' } } - DWORD attr = GetFileAttributesW(spi_path_full); + DWORD attr = GetFileAttributesW(normalized_path); if (attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { - goto finish; + free(normalized_path); + return NULL; } + return normalized_path; +} - wchar_t spi_path_mask[MAX_PATH]; - wcsncpy_s(spi_path_mask, _countof(spi_path_mask), spi_path_full, _TRUNCATE); - wcsncat_s(spi_path_mask, _countof(spi_path_mask), L"\\*.*", _TRUNCATE); +/** + * Susie\x83v\x83\x89\x83O\x83C\x83\x93\x82\xF0\x8Eg\x82\xC1\x82ĉ摜\x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x8D\x81[\x83h\x82\xB7\x82\xE9 + * \x8Ew\x92\xE8\x83t\x83H\x83\x8B\x83_\x93\xE0\x82̃v\x83\x89\x83O\x83C\x83\x93\x82\xF0\x8Eg\x82\xC1\x82ă\x8D\x81[\x83h\x82\xF0\x8E\x8E\x82݂\xE9 + * + * @param[in] image_file \x89摜\x83t\x83@\x83C\x83\x8B + * @param[in] spi_path "c:\\path\\to\\spi" + * \x82\xB1\x82̃t\x83H\x83\x8B\x83_\x82ɂ\xA0\x82\xE9\x83v\x83\x89\x83O\x83C\x83\x93\x83t\x83@\x83C\x83\x8B\x82ʼn摜\x82̃\x8D\x81[\x83h\x82\xF0\x8E\x8E\x82݂\xE9 + * \x83p\x83X\x82̍Ō\xE3\x82\xC9 "\\" \x82\xAA\x82\xA0\x82\xC1\x82Ă\xE0\x82Ȃ\xAD\x82Ă\xE0\x97ǂ\xA2 + * @param[out] pHBInfo BITMAPINFO LocalFree()\x82\xB7\x82邱\x82\xC6 + * @param[out] pHBm bitmap data LocalFree()\x82\xB7\x82邱\x82\xC6 + * @retval TRUE \x83\x8D\x81[\x83hok + * @retval FALSE \x83\x8D\x81[\x83h\x82ł\xAB\x82Ȃ\xA9\x82\xC1\x82\xBD + */ +BOOL SusieLoadPicture(const wchar_t *image_file, const wchar_t *spi_path, HANDLE *pHBInfo, HANDLE *pHBm) +{ + BOOL result = FALSE; + *pHBInfo = NULL; + *pHBm = NULL; + size_t file_size; + unsigned char *file_ptr = LoadImageFile(image_file, &file_size); + if (file_ptr == NULL) { + return FALSE; + } + + // spi_path \x82\xF0\x90\xE2\x91p\x83X\x82ɕϊ\xB7 + wchar_t *spi_path_full = NormalizePath(spi_path); + if (spi_path_full == NULL) { + free(file_ptr); + return FALSE; + } + const size_t spi_path_full_len = wcslen(spi_path_full); + + // mask\x8D쐬 + const size_t spi_path_mask_len = spi_path_full_len + 4 + 1; + wchar_t *spi_path_mask = (wchar_t *)malloc(spi_path_mask_len * sizeof(wchar_t)); + wcsncpy_s(spi_path_mask, spi_path_mask_len, spi_path_full, _TRUNCATE); + wcsncat_s(spi_path_mask, spi_path_mask_len, L"\\*.*", _TRUNCATE); + //\x83v\x83\x89\x83O\x83C\x83\x93\x82\xBD\x82\xC1\x82Ă\xA2\x82\xAD WIN32_FIND_DATAW fd; HANDLE hFind = FindFirstFileW(spi_path_mask, &fd); - if (hFind == INVALID_HANDLE_VALUE) { - goto finish; - } - do { - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - continue; - const wchar_t *ext = wcsrchr(fd.cFileName, L'.'); - if (ext == NULL) { - // \x8Ag\x92\xA3\x8Eq\x82\xAA\x82Ȃ\xA2\x83t\x83@\x83C\x83\x8B? - continue; - } - if (wcscmp(ext, L".dll") != 0 && wcscmp(ext, PLUGIN_EXT) != 0) { - // .dll or .spi(or sph) \x88ȊO\x82̃t\x83@\x83C\x83\x8B - continue; - } + if (hFind != INVALID_HANDLE_VALUE) { + const size_t spiFileNameLen = spi_path_full_len + 1 + _countof(fd.cFileName); + wchar_t *spiFileName = (wchar_t *)malloc(spiFileNameLen * sizeof(wchar_t)); + do { + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + continue; + const wchar_t *ext = wcsrchr(fd.cFileName, L'.'); + if (ext == NULL) { + // \x8Ag\x92\xA3\x8Eq\x82\xAA\x82Ȃ\xA2\x83t\x83@\x83C\x83\x8B? + continue; + } + if (wcscmp(ext, L".dll") != 0 && wcscmp(ext, PLUGIN_EXT) != 0) { + // .dll or .spi(or sph) \x88ȊO\x82̃t\x83@\x83C\x83\x8B + continue; + } - wchar_t spiFileName[MAX_PATH]; - wcsncpy_s(spiFileName, _countof(spiFileName), spi_path_full, _TRUNCATE); - wcsncat_s(spiFileName, _countof(spiFileName), L"\\", _TRUNCATE); - wcsncat_s(spiFileName, _countof(spiFileName), fd.cFileName, _TRUNCATE); + wcsncpy_s(spiFileName, spiFileNameLen, spi_path_full, _TRUNCATE); + wcsncat_s(spiFileName, spiFileNameLen, L"\\", _TRUNCATE); + wcsncat_s(spiFileName, spiFileNameLen, fd.cFileName, _TRUNCATE); - HLOCAL hbuf, hbmi; - if (LoadPictureWithSPI(spiFileName, image_file, fileBuf, fileSize, &hbuf, &hbmi)) { - *pHBInfo = hbmi; - *pHBm = hbuf; - result = TRUE; - break; - } - } while (FindNextFileW(hFind, &fd)); - FindClose(hFind); + HLOCAL hbuf, hbmi; + if (LoadPictureWithSPI(spiFileName, image_file, file_ptr, file_size, &hbuf, &hbmi)) { + *pHBInfo = hbmi; + *pHBm = hbuf; + result = TRUE; + break; + } + } while (FindNextFileW(hFind, &fd)); + free(spiFileName); + FindClose(hFind); + } -finish: - free(fileBuf); + free(spi_path_full); + free(spi_path_mask); + free(file_ptr); return result; }