On 9/24/07, Ilmari H. [email protected] wrote:
buf[i] |= 0x80808080;
(and the other necessary changes, of course).
And a version with 64-bit ints was even faster. But it had a bug too.
Anyone wish to contribute working versions :)?
Guess not, so here’s one:
$ time ./scramble64 trace trace.scramblec
real 0m0.022s
user 0m0.004s
sys 0m0.020s
$ export RUBYOPT= # -rubygems takes ~40ms
$ time ruby -rscramble -e ‘scramble_nobu(“trace”)’
real 0m0.049s
user 0m0.016s
sys 0m0.032s
$ diff trace.scramble*
$
$ cat scramble64.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUF_SIZE 4096
int main(int argc, char** argv) {
int i, j, k, rsz, wsz;
FILE *in_f, *out_f;
unsigned long long isz;
struct stat st;
long long buf[BUF_SIZE];
if (argc != 3) {
printf(“USAGE: %s INFILE OUTFILE\n”, argv[0]);
return 1;
}
isz = sizeof(long long);
in_f = fopen(argv[1], “r”);
if (NULL == in_f) {
printf(“Failed to open input file %s\n”, argv[0]);
return 2;
}
out_f = fopen(argv[2], “w”);
if (NULL == out_f) {
printf(“Failed to open output file %s\n”, argv[1]);
fclose(in_f);
return 4;
}
fstat(fileno(in_f), &st);
for(i=0,j = st.st_size / (iszBUF_SIZE); i<j; i++) {
rsz = fread(buf, isz, BUF_SIZE, in_f);
for(k=0; k<rsz; k++)
buf[k] |= 0x8080808080808080;
wsz = fwrite(buf, isz, rsz, out_f);
if (wsz != rsz) {
printf(“Failed to write to output file\n”);
break;
}
}
rsz = fread(buf, 1, iszBUF_SIZE, in_f);
for(k=0; k<rsz; k++)
((char*)buf)[k] |= 0x80;
wsz = fwrite(buf, 1, rsz, out_f);
if (wsz != rsz) {
printf(“Failed to write to output file\n”);
}
fclose(in_f);
fclose(out_f);
return 0;
}