summaryrefslogtreecommitdiff
path: root/xdelta3/badcopy.c
blob: c42e2b5c7664c28533e9ee5c5c7cad2f9252f218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define BUFSZ (1 << 22)

typedef unsigned int usize_t;

double error_prob   = 0.0001;
usize_t mean_change  = 100;
usize_t total_change = 0;
usize_t total_size   = 0;
usize_t max_change   = 0;
usize_t num_change   = 0;

int last_end = 0;

static int
edist (usize_t mean, usize_t max)
{
  double mean_d = mean;
  double erand  = log (1.0 / drand48 ());
  usize_t x = (usize_t) (mean_d * erand + 0.5);

  return (x < max) ? (x > 0 ? x : 1) : max;
}

void modify (char *buf, usize_t size)
{
  usize_t bufpos = 0, j;

  last_end = 0;

  for (;; /* bufpos and j are incremented in the inner loop */)
    {
      /* The size of the next modification. */
      usize_t next_size = edist (mean_change, 1 << 31);
      /* The expected interval of such a change. */
      double expect_interval = ((double) next_size * (1.0 - error_prob)) / error_prob;
      /* The number of bytes until the next modification. */
      usize_t next_mod  = edist (expect_interval, 1 << 31);

      if (next_size + next_mod + bufpos > size) { break; }

      if (max_change < next_size) { max_change = next_size; }

      bufpos += next_mod;

      fprintf (stderr, "COPY: %u-%u (%u)\n", total_size + last_end, total_size + bufpos, bufpos - last_end);

      fprintf (stderr, "ADD:  %u-%u (%u) is change %u\n", total_size + bufpos , total_size + bufpos + next_size, next_size, num_change);

      total_change += next_size;
      num_change   += 1;

      for (j = 0; j < next_size; j += 1, bufpos += 1)
	{
	  buf[bufpos] = lrand48 () >> 3;
	}

      last_end = bufpos;
    }

  fprintf (stderr, "COPY: %u-%u (%u)\n", total_size + last_end, total_size + size, size - last_end);

  total_size += size;
}

int main(int argc, char **argv)
{
  char buf[BUFSZ];
  int c, ret;

  if (argc > 3)
    {
      fprintf (stderr, "usage: badcopy [byte_error_prob [mean_error_size]]\n");
      return 1;
    }

  if (argc > 2) { mean_change = atoi (argv[2]); }
  if (argc > 1) { error_prob  = atof (argv[1]); }

  if (error_prob < 0.0 || error_prob > 1.0)
    {
      fprintf (stderr, "warning: error probability out of range\n");
      return 1;
    }

  do
    {
      c = fread (buf, 1, BUFSZ, stdin);

      if (c == 0) { break; }

      modify (buf, c);

      ret = fwrite (buf, 1, c, stdout);
    }
  while (c == BUFSZ);

  if ((ret = fclose (stdout)))
    {
      perror ("fclose");
      return 1;
    }

  fprintf (stderr, "add_prob %f; %u adds; total_change %u of %u bytes; add percentage %f; max add size %u\n",
	   error_prob, num_change, total_change, total_size, (double) total_change / (double) total_size, max_change);

  return 0;
}