DEV/DEV4.4/TP1/Ex1/lfsr.c

48 lines
833 B
C
Raw Permalink Normal View History

2024-09-12 15:35:34 +02:00
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define MASK_BIT_7 0x80
#define MASK_BIT_6 0x40
#define MASK_BIT_5 0x20
#define MASK_BIT_4 0x10
#define MASK_BIT_3 0x08
#define MASK_BIT_2 0x04
#define MASK_BIT_1 0x02
#define MASK_BIT_0 0x01
unsigned char next(unsigned char lfsr)
{
lfsr = (lfsr << 1);
if(__builtin_parity(lfsr)==0){
lfsr ^= 1 << 0;
}
return lfsr;
}
int main(int argc, char *argv[])
{
int fd_in,fd_out;
unsigned char w,buf;;
assert(argc >= 4);
fd_in = open(argv[1],O_RDONLY);
fd_out = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600);
w = (unsigned char)strtol(argv[3],NULL,0);
while(1){
ssize_t nb = read(fd_in,&buf,1);
if (nb <=0)
break;
buf ^= w;
write(fd_out,&buf,1);
w=next(w);
}
return 0;
}