📄 test.cpp
字号:
#include <signal.h>
#include <sys/timeb.h>
#include "LwhDes.h"
#include <string.h>
#include <memory.h>#include <stdlib.h>#include <unistd.h>#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
#include <sys/resource.h>#include <sys/times.h>#include <time.h>#include <sys/resource.h>#include <sched.h>
#include "sdel.h"
//#include "sdel.h"
//#define BLOCKSIZE 32769unsigned long bufsize = BLOCKSIZE;int encrypting;int outcount = 0;static char last[8];
char keyx[17],keyy[17];int slow = O_SYNC;
int fd;//char *malloc(), *strcpy(), *strcat();void key_get(char *);int pfile(char *,char *,FILE *,FILE*);void deout(FILE *,char*,int);char garbage(void);int ED_file(int ,char *);void key_get(char *mes,char *dir) /* get file key */
{ register int i, j;
char linebuf[256];
int count;
for (i=0; i<15; i++) keyy[i]=0;
printf("%s%s: ", mes, dir);
fflush(stdout);
count = read(0, linebuf, 256); /* read input line */
printf("\n");
linebuf[count] = 0; /* null terminate */
if (linebuf[count-1] == '\n') /* ignore any terminating newline */
{ linebuf[count-1] = 0;
count--;
}
if (count > 16) count = 16; /* only use 8 chars */
for (i = j = 0; count--;)
keyy[i++] = linebuf[j++];
}int pfile(char *buffer,char *outname,FILE *infile,FILE *outfile) /* process the file */
{ register int m, nsave;
register char *b;
int j;
//printf("ri\n"); /* if ((outfile = fopen(outname, "wb")) == NULL)
{ fprintf(stderr,"Can't write %s.\n", outname);
exit(1);
}*/
while (m = fread(buffer, 1, 1024*1024, infile))
{
if ((nsave = m) < 0) /* read error */
return(-1);
for (b=buffer; m>0; /* encrypt/decrypt 1 buffer-full*/
m -= 8, b += 8) /* 8-byte blocks */
{ if (encrypting)
{ if (m<8) /* don't have a full 64 bits */
{ for (j=0; j<8-m; j++)
b[m+j]=garbage(); /* fill block with trash */
nsave += 8-m; /* complete the block */
}
else j=0; /* number of nulls in last block*/
Des_Go(b, b, 8, keyy, strlen(keyy), ENCRYPT); /* don't need diff input, output*/
}
else /* decrypting */
{ if (m < 8) deout(outfile,b, 1); /* last byte in file: count */
else
{ Des_Go(b, b, 8, keyy, strlen(keyy), DECRYPT);; /* decrypt and output block */
deout(outfile,b, 0);
}
}
} //printf("%s\n",buf); //printf("%d\n",nsave);
if (encrypting) {if (fwrite(buffer,1,nsave,outfile)!=nsave)
return(-1);}
}
/* have now encrypted/decrypted the whole file;
* need to append the byte count for the last block if encrypting.
*/ //printf("ri\n");
if (encrypting) fputc(8 - j, outfile); /* how many good bytes? */
return(0);
} /* see when caught up with delay*/
void deout(FILE *outfile,char *block,int flag) /* 1-block delay on output */
/* 64-bit block, last block flag*/
{ /* previous input block */
register int i;
/* register char *c,*j; */ /* rwo: unused */
if (flag) /* output the last few bytes */
{
fwrite(last, 1, block[0] & 0377, outfile);
return;
}
if (outcount++) /* seen any blocks before? */
fwrite(last, 1, 8, outfile);
for (i = 0; i < 8; i++) last[i] = block[i]; /* copy the block */
}char garbage(void)
{
struct timeb tp;
ftime(&tp); /* get current time */
return tp.millitm; /* return time in milliseconds */
}
int ED_file(int fd,char *filename,unsigned long bufsize,unsigned long file_size){ char *inname, *outname;
FILE *infile, *outfile; register char *u;
char buf[1024*1024]; inname = filename;
outname = filename = (char *)malloc((unsigned) strlen(inname) + 3);
strcpy(filename, inname);
u = &filename[strlen(filename) - 2]; /* check last 2 chars */ if((infile=fdopen(fd,"rb"))==NULL)
return -1;
encrypting = (strcmp(".n", u) != 0);
if (!encrypting) *u = 0; /* strip .n from output filename */
else strcat(filename, ".n"); /* or add .n to output file */
if ((infile = fopen(inname, "rb")) == NULL)
{ fprintf(stderr,"Can't read %s.\n", inname);
return -1;
}
if ((outfile = fopen(outname, "rb")) != NULL)
{ fprintf(stderr, "%s would be overwritten.\n",outname);
return -1;
}
if ((outfile = fopen(outname, "wb")) == NULL)
{ fprintf(stderr,"Can't write %s.\n", outname);
return -1;
}
//desinit(keyx); /* set up tables for DES */
if (pfile(buf,outname,infile,outfile) == 0) { if(encrypting) { if(sdel_overwrite(3, fd, 0, bufsize, file_size > 0 ? file_size : 1) == 0) return sdel_unlink(inname, 0, 1, slow); } unlink(inname); } //printf("ri\n");
else fprintf(stderr,
"%s: I/O Error -- File unchanged\n", inname);
memset(last,0,sizeof(last)); outcount=0;
fclose(outfile);
fclose(infile);
return 0;
}int walk_along(char *filename)
{
struct stat filestat;
struct stat controlstat;
int i_am_a_directory = 0;
//unsigned long bufsize = BLOCKSIZE;
/* get the file stats */
if (lstat(filename, &filestat))
return 1;
if (S_ISREG(filestat.st_mode) && filestat.st_nlink > 1)
{//是否常规文件,链接数是否大于1
fprintf(stderr, "Error: File %s - file is hardlinked %d time(s), skipping!\n", filename, filestat.st_nlink - 1);
return -1;
}
/* if the blocksize on the filesystem is bigger than the on compiled with, enlarge! */
if (filestat.st_blksize > bufsize)
{
if (filestat.st_blksize > 65532)
{
bufsize = 65535;
}
else
{
bufsize = (((filestat.st_blksize / 3) + 1) * 3);
}
}
/* handle the recursive mode */
//if (recursive)//递归模式
if (S_ISDIR(filestat.st_mode))
{
//如果是目录文件
DIR *dir;
struct dirent *dir_entry;
struct stat cwd_stat;
char current_dir[4097];
int res;
int chdir_success = 1;
//if (verbose) printf("DIRECTORY (going recursive now)\n");
getcwd(current_dir, 4096);//取当前目录
current_dir[4096] = '\0';
/* a won race will chmod a file to 0700 if the user is owner/root
I'll think about a secure solution to this, however, I think
there isn't one - anyone with an idea? */
if (chdir(filename))
{
(void) chmod(filename, 0700); /* ignore permission errors */
if (chdir(filename))
{
fprintf(stderr,"Can't chdir() to %s, hence I can't E/D it.\n", filename);
chdir_success = 0;
}
}
if (chdir_success)
{
lstat(".", &controlstat);
lstat("..", &cwd_stat);
if ( (filestat.st_dev != controlstat.st_dev) || (filestat.st_ino != controlstat.st_ino) )
{
fprintf(stderr, "Race found! (directory %s became a link)\n", filename);
}
else
{
if ((dir = opendir (".")) != NULL)
{
(void) chmod(".", 0700); /* ignore permission errors */
dir = opendir (".");
}
if (dir != NULL)
{
while ((dir_entry = readdir(dir)) != NULL)// readdir 函数返回指定目录下的所有目录项
if (strcmp(dir_entry->d_name, ".") && strcmp(dir_entry->d_name, ".."))
{
//printf("Wiping %s ", dir_entry->d_name);
if ( (res = walk_along(dir_entry->d_name)) > 0)
{
if (res == 3)
fprintf(stderr,"File %s was raced, hence I won't E/D it.\n", dir_entry->d_name);
else
{
fprintf(stderr,"Couldn't E/D %s. ", dir_entry->d_name);
perror("");
}
}
else
printf(" Done\n");
}
closedir(dir);
}
}
if(chdir(current_dir) != 0)
{
fprintf(stderr, "Error: Can't chdir to %s (aborting) - ", current_dir);
perror("");
exit(1);
}
/*
lstat(current_dir, &controlstat);
if ( (cwd_stat.st_dev != controlstat.st_dev) || (cwd_stat.st_ino != controlstat.st_ino) ) {
fprintf(stderr, "Race found! (directory %s was exchanged or its your working directory)\n", current_dir);
exit(1);
}
*/
i_am_a_directory = 1;
}
}
/* end of recursive function */
if (S_ISREG(filestat.st_mode))
{//是否常规文件
/* open the file for writing in sync. mode */
if ((fd = open(filename, O_RDWR | O_LARGEFILE | slow)) < 0)
{
/* here again this has a race problem ... hmmm */
/* make it writable for us if possible */
(void) chmod(filename, 0600); /* ignore errors */
if ((fd = open(filename, O_RDWR | O_LARGEFILE | slow)) < 0)//O_LARGEFILE的作用就是在32位系统中支持大文件系统,允许打开那些用31位(2G)都不能表示其长度的大文件
return 1;
}
fstat(fd, &controlstat);
if ((filestat.st_dev != controlstat.st_dev) || (filestat.st_ino != controlstat.st_ino) || (! S_ISREG(controlstat.st_mode)))
{
close(fd);
return 3;
}
if (ED_file(fd,filename,bufsize,filestat.st_size)== 0)
//return sdel_unlink(filename, 0, 1, slow);
return 0;
} /* end IS_REG() */
else
{
if (S_ISDIR(filestat.st_mode))
{
if (i_am_a_directory == 0)
{
fprintf(stderr,"Warning: %s is a directory. I will not remove it, because the -r option is missing!\n", filename);
return 0;
}
else
//return sdel_unlink(filename, 1, 0, slow);
return 0;
}
else
if (! S_ISDIR(filestat.st_mode))
{
fprintf(stderr,"Warning: %s is not a regular file, rename/unlink only!", filename);
//if (! verbose)
printf("\n");
//return sdel_unlink(filename, 0, 0, slow);
}
}
return 99; // not reached
}void doit(char *str,clock_t time){ long tps=sysconf(_SC_CLK_TCK); printf("%s: %6.2f secs\n",str,(float)time/tps);} int main(int argc , char **argv)
{
register char *u;
char *filename; clock_t start,end; struct tms t_start,t_end; int result; int errors=0; pid_t pid=getpid(); sched_param sched; sched.__sched_priority=99; sched_setscheduler(pid,SCHED_FIFO,&sched);
if (argc < 2) /* filenames given? */
{ fprintf(stderr, "Usage: des file ...\n");
return 1;
}
key_get("Type password for ",*argv);
for (;;)
{ strcpy(keyx, keyy);
key_get("Verify password for ",*argv);
if (strcmp(keyx, keyy) == 0) break;
} sdel_init(slow); start=times(&t_start);
for (++argv; --argc; ++argv)
{ result=walk_along(*argv); switch (result)
{
case 0 : printf(" Done\n");
break;
case 1 : fprintf(stderr, "Error: File %s - ", *argv);
perror("");
break;
case -1: break;
case 3 : fprintf(stderr, "File %s was raced, hence I won't wipe it!\n", *argv);
break;
default: fprintf(stderr, "Unknown error\n");
}
if (result)
errors++;
} if(errors>0)
{
printf("after E/D,find errors %d\n",errors);
return 0;
} end=times(&t_end); doit("elapsed",end-start); puts("program times"); doit("\tuser CPU",t_end.tms_utime); doit("\tsys CPU",t_end.tms_stime);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -