更新2:
移植到autotools并上传到SourceForge .
更新:
我将对此进行一些更严格的测试,但我相信该问题与缓存有关,并且两个测试用例的排序可能很重要。我也知道加密是可悲的,重复在两个文件中,并且代码低于初稿质量。我也知道我应该使用 makefile 等。我不建议在任何地方使用它。我将其整合在一起并进行简化,以尽可能地从其他人那里获得最佳的意见。
原始问题:
我完全被难住了。我创建了一个包装器来加密基于此的 shell 脚本 previous question而且效果很好;然而,在确定性能问题时,加密脚本花费的用户时间更少!你能帮我解释一下吗?我就是不明白。我已将所有内容剥离到基础内容并尽可能简化。如果您选择我的文件,您将必须拥有文件/usr/bin/shelldecrypt 的写入权限。
ojblass@XXXXX:~/source/shellcrypt>./build ojblass@XXXXX:~/source/shellcrypt>./run example.sh.bin is encrypted run it to view output ojblass@XXXXX:~/source/shellcrypt>./profile example.sh real 0m0.107s user 0m0.048s sys 0m0.052s example.sh.bin real 0m0.118s user 0m0.036s sys 0m0.068s ojblass@XXXXX:~/source/shellcrypt>
[build]
gcc shellencrypt.c mv a.out shellencrypt gcc shelldecrypt.c mv a.out shelldecrypt cp shelldecrypt /usr/bin
[example.sh]
#!/bin/bash ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null ls -lt >> /dev/null
[profile]
echo example.sh time example.sh echo example.sh.bin time example.sh.bin
[run]
rm -rf example.sh.bin ./shellencrypt example.sh chmod 755 example.sh.bin echo example.sh.bin is encrypted run it to view output
[shelldecrypt.c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
/* #define DEBUG */
static int flip( int a)
{
int b;
b = a;
b ^= 0x000C;
return b;
}
static void trace ( char * szMessage )
{
#ifdef DEBUG
if (szMessage != NULL)
{
printf("DEBUG Message %s\n",szMessage);
}
#endif
return;
}
int main(int argc, char *argv[]) {
FILE *fp = NULL;
int ch=(char) 0;
int foundnl=0;
char shellpath[4096]; /* what happened to PATH_MAX? */
char *ptest;
FILE *pipe = NULL;
/* TODO REMOVE memset(bangline, 0, sizeof(bangline)); */
trace("STARTING");
trace(argv[0]);
trace("ARGUMENT");
/* get the shell environment variable */
ptest = getenv("SHELL");
if (ptest == NULL)
{
trace("could not get shell environment variable");
return (EXIT_FAILURE);
}
else
{
strcpy(shellpath, getenv("SHELL"));
trace(shellpath);
}
if ((argc >=1) && (argv[1]!=NULL))
{
trace(argv[1]);
}
else
{
trace("(null)");
}
if (argc == 2) {
fp = fopen(argv[1],"r");
if (fp == NULL) {
fprintf(stderr,"Unable to open file %s. Exiting.\n",argv[1]);
exit(EXIT_FAILURE);
}
}
else
{
printf("Usage: %s <filename>\n",argv[0]);
exit (EXIT_SUCCESS);
}
/* strip out the bangline which is not encryped */
while ((ch = fgetc(fp)) != EOF) {
if (ch == 10)
{
foundnl = 1;
break;
}
}
if (foundnl!=1)
{
(void) fclose(fp);
trace("FOUND NO NEWLINE BEFORE END OF FIRST LINE");
return (EXIT_SUCCESS);
}
pipe = popen(shellpath, "w");
if (pipe == NULL)
{
trace("popen failed");
(void) fclose(fp);
return (EXIT_FAILURE);
}
else
{
trace("writing string to pipe");
while ((ch = fgetc(fp)) != EOF) {
(void) fputc(flip(ch),pipe);
}
/* (void) fputc(EOF,pipe); */
}
if (pipe != NULL)
(void) pclose(pipe);
if (fp != NULL)
(void) fclose(fp);
exit (EXIT_SUCCESS);
}
[shellcrypt.c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int flip( int a)
{
int b;
b = a;
b ^= 0x000C;
return b;
}
int main(int argc, char *argv[]) {
FILE *fp = NULL, *fpOut=NULL;
int ch;
char szOutputFileName[2000];
strcpy(szOutputFileName,"");
if (argc == 2) {
fp = fopen(argv[1],"r");
if (fp == NULL) {
fprintf(stderr,"Unable to open file %s. Exiting.\n",argv[1]);
exit(EXIT_FAILURE);
}
}
else
{
printf("Usage: %s <filename>\n",argv[0]);
exit (EXIT_SUCCESS);
}
strcpy(szOutputFileName, argv[1]);
strcat(szOutputFileName, ".bin");
fpOut = fopen(szOutputFileName,"wt");
if (fpOut == NULL)
{
fprintf(stderr,"Unable to open file %s. Exiting.\n",szOutputFileName);
if (fp)
(void) fclose(fp);
exit(EXIT_FAILURE);
}
/* print the header */
fprintf(fpOut,"#!/usr/bin/shelldecrypt\n");
/* read until the end of file, encrypting characters and writing them out to target file */
while ((ch = fgetc(fp)) != EOF) {
(void) fputc(flip(ch),fpOut);
}
if (fp)
(void) fclose(fp);
if (fpOut)
(void) fclose(fpOut);
return(EXIT_SUCCESS);
}
最佳答案
这个差异还不够显着,我无法断定有任何影响。更好的“配置文件”是:
#!/bin/bash
echo example.sh
/usr/bin/time sh -c 'for i in $(seq 1 1000); do ./example.sh; done'
echo example.sh.bin
/usr/bin/time sh -c 'for i in $(seq 1 1000); do ./example.sh.bin; done'
在我的机器上,我得到:
example.sh
39.46user 33.22system 1:16.92elapsed 94%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+8547221minor)pagefaults 0swaps
example.sh.bin
42.33user 42.13system 1:33.98elapsed 89%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+9376313minor)pagefaults 0swaps
这不足以明确地表明“加密”的速度较慢(尽管我显然认为是这样),但它确实表明加密的不会> 在简单的基准测试中持续减少用户时间。
此外,您的代码还存在一些其他严重问题。最明显的是,这与声音加密并不接近。正如其他人所说,您处理问题的方式是错误的。您“在几个小时内”想出的加密算法并不能替代健全的权限。
此外,您需要使用 makefile,而不是增加不必要的 shell 脚本。学习基本的 gcc 选项,例如 -o。不要尝试复制到/usr/bin/,除非用户运行安装目标(在这种情况下/usr/local/bin 仍然更好)。
关于c - shell加密程序如何使用更少的用户时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/790475/