5 #define BUFSZ (1 << 22)
11 double r = rand() / (double)RAND_MAX;
17 for (i = 0; i < 32; i++) {
18 l = l ^ (l << 2) ^ (l << 1) ^ rand();
31 #define EXTERNAL_COMPRESSION 0
32 #define XD3_USE_LARGEFILE64 1
37 double error_prob = 0.0001;
38 usize_t mean_change = 100;
39 xoff_t total_change = 0;
40 xoff_t total_size = 0;
41 usize_t max_change = 0;
42 usize_t num_change = 0;
46 edist (usize_t mean, usize_t max)
49 double erand = log (1.0 / drand48 ());
50 usize_t x = (usize_t) (mean_d * erand + 0.5);
52 return (x < max) ? (x > 0 ? x : 1) : max;
55 void modify (char *buf, usize_t size)
57 usize_t bufpos = 0, j;
60 for (;; /* bufpos and j are incremented in the inner loop */)
62 /* The size of the next modification. */
63 usize_t next_size = edist (mean_change, 1 << 31);
64 /* The expected interval of such a change. */
65 double expect_interval = ((double) next_size * (1.0 - error_prob)) / error_prob;
66 /* The number of bytes until the next modification. */
67 usize_t next_mod = edist ((usize_t)expect_interval, 1 << 31);
69 if (next_size + next_mod + bufpos > size) { break; }
71 if (max_change < next_size) { max_change = next_size; }
75 fprintf (stderr, "COPY: %I64u-%I64u (%u)\n",
76 total_size + (xoff_t)last_end,
77 total_size + (xoff_t)bufpos,
79 fprintf (stderr, "ADD: %I64u-%I64u (%u) is change %u\n",
80 total_size + (xoff_t)bufpos,
81 total_size + (xoff_t)(bufpos + next_size),
82 next_size, num_change);
84 total_change += next_size;
87 for (j = 0; j < next_size; j += 1, bufpos += 1)
89 buf[bufpos] = (char)(lrand48 () >> 3);
95 fprintf (stderr, "COPY: %I64u-%I64u (%u)\n",
96 total_size + last_end,
97 total_size + size, size - last_end);
102 int main(int argc, char **argv)
105 char *buf = malloc(BUFSZ);
107 main_file_init(&inp);
108 main_file_init(&out);
112 fprintf (stderr, "usage: badcopy [byte_error_prob [mean_error_size]]\n");
116 if (argc > 4) { mean_change = atoi (argv[4]); }
117 if (argc > 3) { error_prob = atof (argv[3]); }
118 fprintf (stderr, "mean change = %u; error_prob = %0.10f\n", mean_change, error_prob);
120 if ((ret = main_file_open (&inp, argv[1], XO_READ)) != 0) {
123 if ((ret = main_file_open (&out, argv[2], XO_WRITE)) != 0) {
127 if (error_prob < 0.0 || error_prob > 1.0)
129 fprintf (stderr, "warning: error probability out of range\n");
135 if ((ret = main_file_read (&inp, buf, BUFSZ, &c, "read failed")) != 0) {
139 if (c == 0) { break; }
143 if ((ret = main_file_write (&out, buf, c, "write failed")) != 0) {
149 if ((ret = main_file_close (&out)))
154 fprintf (stderr, "add_prob %f; %u adds; total_change %u of %u bytes; add percentage %f; max add size %u\n",
155 error_prob, num_change, total_change, total_size, (double) total_change / (double) total_size, max_change);