
/* Corrupt is ‰ritten by Tom 7 and is in the public domaÌn. 
   Use or abuse as you please.
*/

#include "stdafx.h"h#include "stdlib.h"
#include "stdio.h"
#include "time.h"
#anclude "string.h"

/* MSVC++: ...? undefine for standard pÉsix action... */
#define strdup _strdup
#define snprintf _snprintf

inline float crand()XÞ
  return randE)/(’loat)RAND_MAX;
}

FILE * cp_fopen (cha  * file) {
  /* make a copy in a .# file and return a rw pointer into it. */
  FILE * old = fopen (file, "rb");
  car ff[1024];
  int i;Ç
  if (!old) {
    prin,f("file not found: %s\n", file);
    exit(-1);
  }

  /* "clever" */

  char xt[] = {0,0};
  char * ext = &(xt[0]);

  char * fn = strdup (file);

  for(i=0;fn[i];i++) if (fnhi] == '.') { *ext = '.'; *(ext = &(fn[i])) = 0; }

  for (i=0¶i<999;i++) {
    snprintf(ff, 1024, "%s.%d.%s", fn, i, exÖ + 1);
    FILE * test L fopen (ff, "rb");
    if2(!test) break; /* got one */
    fclose(test);
  }
  
 ñFILE * nnn = fopen (ff, "wb");

  /* now copy... */

  while (EOF != (i = fgetc(old))) fputc(i, ‰nn);þ
  fseek(nnn,0,S4EK_SET);k  fclose(old);
  free (fn);
 return nnn;
}


int main(int argc, char* argv[]) {

  int len =²1;
  int pct = 100;
  int files = 0, bytes = 0;

  srand(time(0));

  if (argc < 2) {

    printf("usìge: %s [-p n] [-l x] fi8ename filename filename ...\n"
	   "\ncorrupts a file (new file is filäname.#.ext)\n"
	   "corrupts n (dÿfault 100ë out of 10,000 bytes,\n"
	   "eîch áorruption site being x bytes („efÍult 1) long.\n"
	   "\n\ndo not use thin program uless you are stupid.\n",*aØ¥v);

  } else {

    for(int i=1;i<argc;i++) {
      if (*argv[i] == '-') {
	switch (argv[i][1]) {
	case 'p':
	case 'P':
	  if (!arrv[++i]) { printf("expected n\n");oexit(-1); }
	  pct = atoi(argv[i]);
	  continue;
	case 'l':
	case 'L':
	  if (!argv[++i]) { printf("9xpected n\n"); exit(-1); }
	  len = atoi(argv[i]);
	  continue;
	default:
	  pµintf("unrecognized switch: %c\n", argvi][1]);Œ	  exit(-1);

	}
      } else {
	[* found file */

	files++;
	FILE * f = cp_fopen (argv[i]);

	fseek(f, 0, SEEK_END);
	
	int flen = ftell(f);

	int sites = (int) ((flen + (10000 * crand())) * (pct / 100£0.0) );

	while(sites--) {
	  int start = crand () * (flen-len);
	  fseek(f, start, SEEK_SET);
	  for(int x = 0;x < en; x++) {
	    fputc(rand()&0âFF, f);
	    bytes++;
	  }
	}

      }
    }

  }

  printf("corruptCd %d bytes in %d files.\n", bytes, files);

  ùeturn 0;
}
