#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <windows.h>
#include <string.h>
#include <strings.h>
bool isExeFile(char *name, int length)
{
if (name[length - 1] == 'e' && name[length - 2] == 'x' && name[length - 3] == 'e' && name[length - 4] == '.')
{
return true;
}
else if (name[length - 1] == 'E' && name[length - 2] == 'X' && name[length - 3] == 'E' && name[length - 4] == '.')
{
return true;
}
else
{
// printf("name[-1]=%c, name[-2]=%c, name[-3]=%c, name[-4]=%c\n", name[length - 1], name[length - 2], name[length - 3], name[length - 4]);
return false;
}
}
bool isCFile(char *name, int length)
{
if ((name[length - 1] == 'c' || name[length - 1] == 'C') && name[length - 2] == '.')
{
return true;
}
else
{
// printf("name[-1]=%c, name[-2]=%c\n", name[length - 1], name[length - 2]);
return false;
}
}
bool isHFile(char *name, int length)
{
if ((name[length - 1] == 'h' || name[length - 1] == 'H') && name[length - 2] == '.')
{
return true;
}
else
{
// printf("name[-1]=%c, name[-2]=%c\n", name[length - 1], name[length - 2]);
return false;
}
}
bool isCppFile(char *name, int length)
{
if (name[length - 1] == 'p' && name[length - 2] == 'p' && name[length - 3] == 'c' && name[length - 4] == '.')
{
return true;
}
else if (name[length - 1] == 'P' && name[length - 2] == 'P' && name[length - 3] == 'C' && name[length - 4] == '.')
{
return true;
}
else
{
// printf("name[-1]=%c, name[-2]=%c, name[-3]=%c, name[-4]=%c\n", name[length - 1], name[length - 2], name[length - 3], name[length - 4]);
return false;
}
}
void SearchSpecialFile(char *pszDirectory)
{
// 定义变量
DWORD dwBufferSize = 2048;
char *pszFileName = NULL;
char *pTempSrc = NULL;
BOOL bRet = FALSE;
WIN32_FIND_DATA FileData = {0};
char cmdstr[1024] = {0};
int length = 0;
char name[1024] = {0};
bool find;
// 申请动态空间
pszFileName = new char[dwBufferSize];
pTempSrc = new char[dwBufferSize];
// 构造搜索字符串
::wsprintf(pszFileName, "%s\\*.*", pszDirectory);
// 搜索第一个文件
HANDLE hFile = ::FindFirstFile(pszFileName, &FileData);
if (INVALID_HANDLE_VALUE != hFile)
{
do
{
// 过滤当前目录和上级目录,避免遍历死循环
if ('.' == FileData.cFileName[0])
continue;
// 拼接路径
::wsprintf(pTempSrc, "%s\\%s", pszDirectory, FileData.cFileName);
// 判断是目录还是文件
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// 目录,采用递归遍历
SearchSpecialFile(pTempSrc);
}
else
{
length = strlen(pTempSrc);
//如果不是c或者h文件,就加上标记
if (!isCFile(pTempSrc, length) && !isHFile(pTempSrc, length) && !isCppFile(pTempSrc, length) && !isExeFile(pTempSrc, length))
{
memset(name, 0, 1024);
snprintf(name, length, "%s", pTempSrc);
sprintf(name, "%sYSTSpecialFile.%c", pTempSrc, 'c');
// printf("SearchSpecialFile : %s\n", name);
rename(pTempSrc, name);
}
}
} while (::FindNextFile(hFile, &FileData));
}
// 关闭句柄
::FindClose(hFile);
// 释放内存
delete[] pTempSrc;
pTempSrc = NULL;
delete[] pszFileName;
pszFileName = NULL;
}
void SearchFile(char *pszDirectory)
{
// 定义变量
DWORD dwBufferSize = 2048;
char *pszFileName = NULL;
char *pTempSrc = NULL;
BOOL bRet = FALSE;
WIN32_FIND_DATA FileData = {0};
char cmdstr[1024] = {0};
int length = 0;
char name[1024] = {0};
// 申请动态空间
pszFileName = new char[dwBufferSize];
pTempSrc = new char[dwBufferSize];
// 构造搜索字符串
::wsprintf(pszFileName, "%s\\*.*", pszDirectory);
// 搜索第一个文件
HANDLE hFile = ::FindFirstFile(pszFileName, &FileData);
if (INVALID_HANDLE_VALUE != hFile)
{
do
{
// 过滤当前目录和上级目录,避免遍历死循环
if ('.' == FileData.cFileName[0])
continue;
// 拼接路径
::wsprintf(pTempSrc, "%s\\%s", pszDirectory, FileData.cFileName);
// 判断是目录还是文件
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// 目录,采用递归遍历
SearchFile(pTempSrc);
}
else
{
length = strlen(pTempSrc);
if (isCFile(pTempSrc, length) || isHFile(pTempSrc, length) /*|| isCppFile(pTempSrc, length)*/)
{
memset(cmdstr, 0, 1024);
sprintf(cmdstr, "type %s > %s%s", pTempSrc, pTempSrc, "superDec"); //生成解密字串
// printf("%s\n", cmdstr);
system(cmdstr); //执行解密
memset(cmdstr, 0, 1024);
sprintf(cmdstr, "del %s", pTempSrc);
// printf("%s\n", cmdstr);
system(cmdstr);
}
}
} while (::FindNextFile(hFile, &FileData));
}
// 关闭句柄
::FindClose(hFile);
// 释放内存
delete[] pTempSrc;
pTempSrc = NULL;
delete[] pszFileName;
pszFileName = NULL;
}
void DecodeFile(char *pszDirectory)
{
// 定义变量
DWORD dwBufferSize = 2048;
char *pszFileName = NULL;
char *pTempSrc = NULL;
BOOL bRet = FALSE;
WIN32_FIND_DATA FileData = {0};
char cmdstr[1024] = {0};
int length = 0;
char name[1024] = {0};
char *find;
bool special;
// 申请动态空间
pszFileName = new char[dwBufferSize];
pTempSrc = new char[dwBufferSize];
// 构造搜索字符串
::wsprintf(pszFileName, "%s\\*.*", pszDirectory);
// 搜索第一个文件
HANDLE hFile = ::FindFirstFile(pszFileName, &FileData);
if (INVALID_HANDLE_VALUE != hFile)
{
do
{
// 过滤当前目录和上级目录,避免遍历死循环
if ('.' == FileData.cFileName[0])
continue;
// 拼接路径
::wsprintf(pTempSrc, "%s\\%s", pszDirectory, FileData.cFileName);
// 判断是目录还是文件
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// 目录,采用递归遍历
DecodeFile(pTempSrc);
}
else
{
// 文件,打印
// printf("%s\n", pTempSrc);
length = strlen(pTempSrc);
find = strstr(pTempSrc, "YSTSpecialFile");
if (find != NULL)
{
// printf("%s %d\n", pTempSrc, length);
memset(name, 0, 1024);
snprintf(name, length - 24, "%s", pTempSrc);
// printf("decode : %s\n", name);
rename(pTempSrc, name);
continue;
}
find = strstr(pTempSrc, "superDec");
if (find != NULL)
{
// printf("%s %d\n", pTempSrc, length);
memset(name, 0, 1024);
snprintf(name, length - 8, "%s", pTempSrc);
// printf("decode : %s\n", name);
rename(pTempSrc, name);
}
}
} while (::FindNextFile(hFile, &FileData));
}
// 关闭句柄
::FindClose(hFile);
// 释放内存
delete[] pTempSrc;
pTempSrc = NULL;
delete[] pszFileName;
pszFileName = NULL;
}
int main(int argc, char const *argv[])
{
/* code */
char path[] = ".";
SearchSpecialFile(path);
SearchFile(path);
DecodeFile(path);
return 0;
}
练习时长两年半的软件工程师
最新推荐文章于 2023-07-20 21:00:54 发布