Skip to content

Commit

Permalink
flipbit filter
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Aug 21, 2024
1 parent 98b2946 commit de78f23
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
45 changes: 31 additions & 14 deletions concieggs/compiled/flipbit.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>

void flipline(void) {
char* line = NULL;
size_t n;
ssize_t m;
while ((m = getline(&line, &n, stdin)) > 0) {
int i = rand() % m;
int j = rand() % 8;
line[i] ^= i<<j;
fputs(line, stdout);
}
}

int main(int argc, char* argv[]) {
assert(argc==2);
FILE* f = fopen(argv[1], "r+");
srand(getpid());
assert(f != NULL);
assert(fseek(f, 0, SEEK_END) == 0);
int size = ftell(f);
if (size == 0) return 0;
int i = rand() % size;
int j = rand() % 8;
assert(fseek(f, i, SEEK_SET) == 0);
unsigned char c;
assert(fread(&c, 1, 1, f) == 1);
c ^= (1<<j);
assert(fseek(f, i, SEEK_SET) == 0);
assert(fwrite(&c, 1, 1, f) == 1);
if (argc == 1) {
flipline();
} else {
assert(argc==2);
FILE* f = fopen(argv[1], "r+");
assert(f != NULL);
assert(fseek(f, 0, SEEK_END) == 0);
int size = ftell(f);
if (size == 0) return 0;
int i = rand() % size;
int j = rand() % 8;
assert(fseek(f, i, SEEK_SET) == 0);
unsigned char c;
assert(fread(&c, 1, 1, f) == 1);
c ^= 1<<j;
assert(fseek(f, i, SEEK_SET) == 0);
assert(fwrite(&c, 1, 1, f) == 1);
}
}
3 changes: 3 additions & 0 deletions concieggs/filters/flipbit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

exec flipbit

0 comments on commit de78f23

Please sign in to comment.