26 lines
494 B
C
26 lines
494 B
C
#include <stdio.h>
|
|
|
|
#include "whitening.h"
|
|
|
|
void whitening_apply_in_place(uint8_t *data, size_t length)
|
|
{
|
|
/* 9-bit LFSR according to CCITT data whitening procedure. */
|
|
uint16_t lfsr = 0x1FF;
|
|
|
|
for(size_t i = 0; i < length; i++) {
|
|
uint8_t byte = 0;
|
|
|
|
// advance the LFSR and collect the output in a byte.
|
|
for(int k = 0; k < 8; k++) {
|
|
byte <<= 1;
|
|
byte |= lfsr & 0x001;
|
|
|
|
uint16_t tmp = lfsr ^ (lfsr >> 5);
|
|
lfsr >>= 1;
|
|
lfsr |= (tmp & 0x001) << 8;
|
|
}
|
|
|
|
data[i] ^= byte;
|
|
}
|
|
}
|