感谢 pbos 帮助及其程序(已发布 here,用于异或大文件),我进行了一些测试,发现我还有另一个问题:将掩码更改为另一个 128 位的掩码,有没有所需的默认类型。
我认为一个解决方案是包含一个库来增加可用的整数类型……但我更喜欢解释每个 128 位值,例如字符串。在不损失性能的情况下这可能吗?
这是当前程序(存在错误“整数常量对于其类型来说太大”):
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 128
#define MASK 0xA37c54f173f02889a64be02f2bc44112 /* a 128 bits constant */
void
usage(const char *cmd)
{
fprintf(stderr, "Usage: %s <in-file> [<out-file>]\n", cmd);
exit (EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
if (argc < 3) usage(argv[0]);
FILE *in = fopen(argv[1], "rb");
if (in == NULL)
{
printf("Cannot open: %s", argv[2]);
return EXIT_FAILURE;
}
FILE *out = fopen(argv[2], "wb");
if (out == NULL)
{
fclose(in);
printf("Unable to open '%s' for writing.", argv[2]);
}
char buffer[BLOCKSIZE];
int count;
while (count = fread(buffer, 1, BLOCKSIZE, in))
{
int i;
for (i = 0; i < count; i++)
{
((unsigned long *)buffer)[i] ^= MASK; /* this line is bugged */
}
if (fwrite(buffer, 1, count, out) != count)
{
fclose(in);
fclose(out);
printf("Cannot write, disk full?\n");
return EXIT_FAILURE;
}
}
fclose(in);
fclose(out);
return EXIT_SUCCESS;
}
感谢任何建议。
道格
最佳答案
用前半部分屏蔽偶数“ block ”,用另一半屏蔽奇数“ block ”。
/* ... */
unsigned int xormask[2] = {0xA37c54f173f02889, 0xa64be02f2bc44112};
/* ... */
for(i = 0;i < end; ++i)
{
((unsigned int *)buffer)[i] ^= xormask[i & 1];
}
/* ... */
关于c - 128 位类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1657562/