summaryrefslogtreecommitdiff
path: root/xdelta3/badcopy.c
diff options
context:
space:
mode:
Diffstat (limited to 'xdelta3/badcopy.c')
-rwxr-xr-xxdelta3/badcopy.c50
1 files changed, 40 insertions, 10 deletions
diff --git a/xdelta3/badcopy.c b/xdelta3/badcopy.c
index c42e2b5..0fcbe21 100755
--- a/xdelta3/badcopy.c
+++ b/xdelta3/badcopy.c
@@ -15,7 +15,24 @@ usize_t num_change = 0;
15 15
16int last_end = 0; 16int last_end = 0;
17 17
18static int 18#ifdef WIN32
19// whatever
20static
21double drand48() {
22 double r = rand() / (double)RAND_MAX;
23 return r;
24}
25long lrand48() {
26 long l = 0;
27 int i;
28 for (i = 0; i < 32; i++) {
29 l = l ^ (l << 2) ^ (l << 1) ^ rand();
30 }
31 return l;
32}
33#endif
34
35static usize_t
19edist (usize_t mean, usize_t max) 36edist (usize_t mean, usize_t max)
20{ 37{
21 double mean_d = mean; 38 double mean_d = mean;
@@ -38,7 +55,7 @@ void modify (char *buf, usize_t size)
38 /* The expected interval of such a change. */ 55 /* The expected interval of such a change. */
39 double expect_interval = ((double) next_size * (1.0 - error_prob)) / error_prob; 56 double expect_interval = ((double) next_size * (1.0 - error_prob)) / error_prob;
40 /* The number of bytes until the next modification. */ 57 /* The number of bytes until the next modification. */
41 usize_t next_mod = edist (expect_interval, 1 << 31); 58 usize_t next_mod = edist ((usize_t)expect_interval, 1 << 31);
42 59
43 if (next_size + next_mod + bufpos > size) { break; } 60 if (next_size + next_mod + bufpos > size) { break; }
44 61
@@ -47,7 +64,6 @@ void modify (char *buf, usize_t size)
47 bufpos += next_mod; 64 bufpos += next_mod;
48 65
49 fprintf (stderr, "COPY: %u-%u (%u)\n", total_size + last_end, total_size + bufpos, bufpos - last_end); 66 fprintf (stderr, "COPY: %u-%u (%u)\n", total_size + last_end, total_size + bufpos, bufpos - last_end);
50
51 fprintf (stderr, "ADD: %u-%u (%u) is change %u\n", total_size + bufpos , total_size + bufpos + next_size, next_size, num_change); 67 fprintf (stderr, "ADD: %u-%u (%u) is change %u\n", total_size + bufpos , total_size + bufpos + next_size, next_size, num_change);
52 68
53 total_change += next_size; 69 total_change += next_size;
@@ -55,7 +71,7 @@ void modify (char *buf, usize_t size)
55 71
56 for (j = 0; j < next_size; j += 1, bufpos += 1) 72 for (j = 0; j < next_size; j += 1, bufpos += 1)
57 { 73 {
58 buf[bufpos] = lrand48 () >> 3; 74 buf[bufpos] = (char)(lrand48 () >> 3);
59 } 75 }
60 76
61 last_end = bufpos; 77 last_end = bufpos;
@@ -68,17 +84,31 @@ void modify (char *buf, usize_t size)
68 84
69int main(int argc, char **argv) 85int main(int argc, char **argv)
70{ 86{
71 char buf[BUFSZ]; 87 FILE *in, *out;
88 char *buf = malloc(BUFSZ);
72 int c, ret; 89 int c, ret;
73 90
74 if (argc > 3) 91 if (argc > 5)
75 { 92 {
76 fprintf (stderr, "usage: badcopy [byte_error_prob [mean_error_size]]\n"); 93 fprintf (stderr, "usage: badcopy [byte_error_prob [mean_error_size]]\n");
77 return 1; 94 return 1;
78 } 95 }
79 96
80 if (argc > 2) { mean_change = atoi (argv[2]); } 97 if (argc > 4) { mean_change = atoi (argv[4]); }
81 if (argc > 1) { error_prob = atof (argv[1]); } 98 if (argc > 3) { error_prob = atof (argv[3]); }
99 fprintf (stderr, "mean change = %u; error_prob = %0.10f\n", mean_change, error_prob);
100
101 in = fopen(argv[1], "rb");
102 out = fopen(argv[2], "wb");
103
104 if (in == NULL) {
105 fprintf (stderr, "Cannot open input: %s\n", argv[1]);
106 return 1;
107 }
108 if (out == NULL) {
109 fprintf (stderr, "Cannot open output: %s\n", argv[2]);
110 return 1;
111 }
82 112
83 if (error_prob < 0.0 || error_prob > 1.0) 113 if (error_prob < 0.0 || error_prob > 1.0)
84 { 114 {
@@ -88,13 +118,13 @@ int main(int argc, char **argv)
88 118
89 do 119 do
90 { 120 {
91 c = fread (buf, 1, BUFSZ, stdin); 121 c = fread (buf, 1, BUFSZ, in);
92 122
93 if (c == 0) { break; } 123 if (c == 0) { break; }
94 124
95 modify (buf, c); 125 modify (buf, c);
96 126
97 ret = fwrite (buf, 1, c, stdout); 127 ret = fwrite (buf, 1, c, out);
98 } 128 }
99 while (c == BUFSZ); 129 while (c == BUFSZ);
100 130