source: nikanabo/current/xdelta/diy/badcopy.c@ 663

Last change on this file since 663 was 185, checked in by geyser, 18 years ago
File size: 3.5 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4
5#define BUFSZ (1 << 22)
6
7#ifdef WIN32
8// whatever
9static
10double drand48() {
11 double r = rand() / (double)RAND_MAX;
12 return r;
13}
14long lrand48() {
15 long l = 0;
16 int i;
17 for (i = 0; i < 32; i++) {
18 l = l ^ (l << 2) ^ (l << 1) ^ rand();
19 }
20 return l;
21}
22#endif
23
24#ifdef _WIN32
25#define XD3_WIN32 1
26#else
27#define XD3_POSIX 1
28#endif
29#define XD3_MAIN 1
30#define main notmain
31#define EXTERNAL_COMPRESSION 0
32#define XD3_USE_LARGEFILE64 1
33#include "xdelta3.c"
34#undef main
35
36
37double error_prob = 0.0001;
38usize_t mean_change = 100;
39xoff_t total_change = 0;
40xoff_t total_size = 0;
41usize_t max_change = 0;
42usize_t num_change = 0;
43
44
45static usize_t
46edist (usize_t mean, usize_t max)
47{
48 double mean_d = mean;
49 double erand = log (1.0 / drand48 ());
50 usize_t x = (usize_t) (mean_d * erand + 0.5);
51
52 return (x < max) ? (x > 0 ? x : 1) : max;
53}
54
55void modify (char *buf, usize_t size)
56{
57 usize_t bufpos = 0, j;
58 usize_t last_end = 0;
59
60 for (;; /* bufpos and j are incremented in the inner loop */)
61 {
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);
68
69 if (next_size + next_mod + bufpos > size) { break; }
70
71 if (max_change < next_size) { max_change = next_size; }
72
73 bufpos += next_mod;
74
75 fprintf (stderr, "COPY: %I64u-%I64u (%u)\n",
76 total_size + (xoff_t)last_end,
77 total_size + (xoff_t)bufpos,
78 bufpos - last_end);
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);
83
84 total_change += next_size;
85 num_change += 1;
86
87 for (j = 0; j < next_size; j += 1, bufpos += 1)
88 {
89 buf[bufpos] = (char)(lrand48 () >> 3);
90 }
91
92 last_end = bufpos;
93 }
94
95 fprintf (stderr, "COPY: %I64u-%I64u (%u)\n",
96 total_size + last_end,
97 total_size + size, size - last_end);
98
99 total_size += size;
100}
101
102int main(int argc, char **argv)
103{
104 main_file inp, out;
105 char *buf = malloc(BUFSZ);
106 int c, ret;
107 main_file_init(&inp);
108 main_file_init(&out);
109 option_force = 1;
110 if (argc > 5)
111 {
112 fprintf (stderr, "usage: badcopy [byte_error_prob [mean_error_size]]\n");
113 return 1;
114 }
115
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);
119
120 if ((ret = main_file_open (&inp, argv[1], XO_READ)) != 0) {
121 return 1;
122 }
123 if ((ret = main_file_open (&out, argv[2], XO_WRITE)) != 0) {
124 return 1;
125 }
126
127 if (error_prob < 0.0 || error_prob > 1.0)
128 {
129 fprintf (stderr, "warning: error probability out of range\n");
130 return 1;
131 }
132
133 do
134 {
135 if ((ret = main_file_read (&inp, buf, BUFSZ, &c, "read failed")) != 0) {
136 return 1;
137 }
138
139 if (c == 0) { break; }
140
141 modify (buf, c);
142
143 if ((ret = main_file_write (&out, buf, c, "write failed")) != 0) {
144 return 1;
145 }
146 }
147 while (c == BUFSZ);
148
149 if ((ret = main_file_close (&out)))
150 {
151 return 1;
152 }
153
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);
156
157 return 0;
158}
Note: See TracBrowser for help on using the repository browser.