📄 dirsh.c
字号:
rename(pnode->ffd.cFileName,
getNewName(pnode->ffd.cFileName, newName, key)))
fprintf(stderr, "Decryption aborted: Cannot update file name %s with %s\n",
pnode->ffd.cFileName, newName);
else{
strncpy(pnode->ffd.cFileName, newName, MAX_PATH);
encodeFile(pnode->dir, pnode->ffd.cFileName, pnode->ffd.cFileName, blk_size);
}
}
// progress bar
iNode++;
percent = (float)iNode / nNode;
iBarNext = (int)(percent * (sizeof(bar) - 3));
while(iBar <= iBarNext)
bar[iBar++] = '=';
printf("%s\t%3d%%\r", bar, (int)(percent * 100));
}
pnode = pnode->next;
}
printf("\n");
}
void shellFile(List ls, char *cmd, char *str)
{
char command[COMMAND_MAXLEN];
char suffix[COMMAND_MAXLEN] = "";
char iTerm; // index of ending '\0' of string
List pnode = ls;
if(cmd == NULL)
return;
strncpy(command, cmd, COMMAND_MAXLEN);
if(str){
iTerm = strstr(command, str) - command;
strncpy(suffix, &command[iTerm + strlen(str)], COMMAND_MAXLEN);
}else
iTerm = strlen(command);
while(pnode){
command[iTerm] = '\0';
strcat(command, pnode->dir);
if(pnode->dir[3]) // Excludes root directory of drive
strcat(command, "\\");
strcat(command, pnode->ffd.cFileName);
strcat(command, suffix);
system(command);
pnode = pnode->next;
}
}
void generateMD5Index(List ls, char *idx_file, DWORD blk_size)
{
HANDLE hFile;
HANDLE hIndex; // Index file handle;
DWORD nRead;
DWORD nSize;
MD5_CTX context;
int i;
char line[IDX_LINE_MAXLEN];
char buf[FILE_BUFFER_LEN];
char bar[] = "[ ]";
List pnode = ls;
int nNode = 0;
int iNode = 0;
int iBar = 1;
int iBarNext;
float percent;
if(idx_file == NULL)
return;
while(pnode){
if((pnode->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
nNode++;
pnode = pnode->next;
}
if((hIndex = CreateFile(idx_file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE){
fprintf(stderr, "Cannot open MD5 index file %s\n", idx_file);
return;
}
while(ls){
if((ls->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0){
SetCurrentDirectory(ls->dir);
if((hFile = CreateFile(ls->ffd.cFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
fprintf(stderr, "Cannot open file %s\\%s\n", ls->dir, ls->ffd.cFileName);
else{
MD5Init(&context);
nSize = blk_size;
if(blk_size){
while(nSize && ReadFile(hFile, buf,
nSize > sizeof(buf)? sizeof(buf): nSize, &nRead, 0) && nRead){
MD5Update(&context, buf, nRead);
nSize -= nRead;
}
}else{
while(ReadFile(hFile, buf, sizeof(buf), &nRead, 0) && nRead)
MD5Update(&context, buf, nRead);
}
MD5Final(&context);
CloseHandle(hFile);
for(i = 0; i < 16; i++)
sprintf(&line[i << 1], "%02x", context.digest[i]);
sprintf(&line[32], "\t%s\n", ls->ffd.cFileName);
WriteFile(hIndex, line, strlen(line), &nRead, 0);
//progress bar
iNode++;
percent = (float)iNode / nNode;
iBarNext = (int)(percent * (sizeof(bar) - 3));
while(iBar <= iBarNext)
bar[iBar++] = '=';
printf("%s\t%3d%%\r", bar, (int)(percent * 100));
}
}
ls = ls->next;
}
CloseHandle(hIndex);
printf("\n");
}
void recoverFileNames(List ls, char *idx_file, DWORD blk_size)
{
Map map = NULL;
FILE *fidx;
HANDLE hFile;
MD5_CTX context;
DWORD nSize;
DWORD nRead;
int i;
int line_len;
char md5str[33];
char buf[FILE_BUFFER_LEN];
char line[IDX_LINE_MAXLEN];
char bar[] = "[ ]";
List pnode = ls;
int nNode = 0;
int iNode = 0;
int iBar = 1;
int iBarNext;
float percent;
if((fidx = fopen(idx_file, "r")) == NULL){
fprintf(stderr, "Cannot open MD5 index file %s\n", idx_file);
return;
}
while(pnode){
if((pnode->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
nNode++;
pnode = pnode->next;
}
while(fgets(line, IDX_LINE_MAXLEN, fidx)){
line_len = strlen(line);
if(line_len && line[line_len - 1] == '\n')
line[line_len - 1] = '\0';
line[32] = '\0'; // end of md5 hash
MapAssign(&map, line, &line[33]);
}
fclose(fidx);
// Update file names
while(ls){
if((ls->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0){
SetCurrentDirectory(ls->dir);
if((hFile = CreateFile(ls->ffd.cFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
fprintf(stderr, "Cannot open file %s\\%s\n", ls->dir, ls->ffd.cFileName);
else{
MD5Init(&context);
nSize = blk_size;
if(blk_size){
while(nSize && ReadFile(hFile, buf,
nSize > sizeof(buf)? sizeof(buf): nSize, &nRead, 0) && nRead){
MD5Update(&context, buf, nRead);
nSize -= nRead;
}
}else{
while(ReadFile(hFile, buf, sizeof(buf), &nRead, 0) && nRead)
MD5Update(&context, buf, nRead);
}
MD5Final(&context);
CloseHandle(hFile);
for(i = 0; i < 16; i++)
sprintf(&md5str[i << 1], "%02x", context.digest[i]);
if(rename(ls->ffd.cFileName, MapFind(map, md5str)))
fprintf(stderr, "Cannot update file %s with new name.\n", ls->ffd.cFileName);
//progress bar
iNode++;
percent = (float)iNode / nNode;
iBarNext = (int)(percent * (sizeof(bar) - 3));
while(iBar <= iBarNext)
bar[iBar++] = '=';
printf("%s\t%3d%%\r", bar, (int)(percent * 100));
}
}
ls = ls->next;
}
MapClear(map);
printf("\n");
}
int main(int argc, char *argv[])
{
BOOL enc_flag = FALSE;
BOOL dec_flag = FALSE;
BOOL ver_flag = FALSE;
BOOL rec_flag = FALSE; // recover file names
LPSTR key = NULL;
LPSTR cmd = NULL;
LPSTR str = NULL;
LPSTR pat = NULL;
LPSTR idx_file = NULL;
DWORD rnd_len = 0;
DWORD blk_size = 0;
int arg;
int nRoot = 0;
List ls;
if(argc < 3){
fprintf(stderr, Usage);
return -1;
}
for(arg = 1; arg < argc; arg++){
if(argv[arg][0] == '-'){
switch(argv[arg][1]){
case 'E':
case 'e':
enc_flag = TRUE;
break;
case 'D':
case 'd':
dec_flag = TRUE;
break;
case 'S':
case 's':
rec_flag = TRUE;
break;
case 'V':
case 'v':
ver_flag = TRUE;
break;
case 'B':
case 'b':
blk_size = strtoul(argv[arg + 1], 0, 10);
arg++;
break;
case 'R':
case 'r':
rnd_len = strtoul(argv[arg + 1], 0, 10);
arg++;
break;
case 'K':
case 'k':
key = argv[arg + 1];
arg++;
break;
case 'C':
case 'c':
cmd = argv[arg + 1];
arg++;
break;
case 'I':
case 'i':
str = argv[arg + 1];
arg++;
break;
case 'P':
case 'p':
pat = argv[arg + 1];
arg++;
break;
case 'L':
case 'l':
idx_file = argv[arg + 1];
arg++;
break;
default:
fprintf(stderr, "unknow argument - %s\n%s", argv[arg], Usage);
return -1;
}
}else{
if(nRoot){
fprintf(stderr, "unknow argument - %s\n%s", argv[arg], Usage);
return -1;
}
nRoot = arg;
}
}
if(!nRoot){
fprintf(stderr, "no root directory specified.\n%s", Usage);
return -1;
}
// Generate file list
if(ver_flag)
printf("Generating list of files ...\n");
if((ls = listFile(argv[nRoot], pat? pat: "*")) == NULL){
fprintf(stderr, "No file found in %s\n", argv[nRoot]);
return -1;
}
if(ver_flag)
printList(ls);
// Generate MD5 index file?
if(idx_file && rec_flag == FALSE){
if(ver_flag)
printf("Generating MD5 index ...\n");
generateMD5Index(ls, idx_file, blk_size);
}
// Randomization?
if(rnd_len && dec_flag == FALSE && rec_flag == FALSE){ // In decryption, file name is part of key
if(ver_flag)
printf("Randomizing file names ...\n");
randFile(ls, rnd_len);
}
// En/Decrytion
if(enc_flag ^ dec_flag){
if(ver_flag)
printf("Encoding files ...\n");
encodeList(ls, key, blk_size, rnd_len, enc_flag);
}
// Execute commands
if(cmd){
if(ver_flag)
printf("Executing commands ...\n");
shellFile(ls, cmd, str);
}
// Recover file names
if(idx_file && rec_flag){
if(ver_flag)
printf("Recovering file names ...\n");
recoverFileNames(ls, idx_file, blk_size);
}
// Cleanup
if(ver_flag)
printf("Clearing file list ...\n");
clearList(ls);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -