package ru.r2cloud.jradio.blocks;

import ru.r2cloud.jradio.util.Lfsr;

public class AdditiveScrambler {

	private final Lfsr lfsr;
	private final int bitsPerByte;

	public AdditiveScrambler(int mask, int seed, int length, int bitsPerByte) {
		if (bitsPerByte < 1 || bitsPerByte > 8) {
			throw new IllegalArgumentException("invalid bits per byte: " + bitsPerByte + " expected: [1;8]");
		}
		lfsr = new Lfsr(mask, seed, length);
		this.bitsPerByte = bitsPerByte;
	}

	public void shuffle(byte[] data) {
		for (int i = 0; i < data.length; i++) {
			int scrambleByte = 0x00;
			for (int k = 0; k < bitsPerByte; k++) {
				scrambleByte ^= (lfsr.nextBit() << k);
			}
			data[i] = (byte) ((data[i] & 0xFF) ^ scrambleByte);
		}
		lfsr.reset();
	}

	public void shufflePacked(byte[] data) {
		for (int i = 0; i < data.length; i++) {
			int currentIn = data[i] & 0xFF;
			int currentOut = 0;
			for (int j = 0; j < 8; j++) {
				currentOut <<= 1;
				currentOut |= ((currentIn >> (7 - j)) & 0x1) ^ lfsr.nextBit();
			}
			data[i] = (byte) currentOut;
		}
		lfsr.reset();
	}

}

@@ -741,331 +741,6 @@
 %09%7D%0A%0A
-%09public void shufflePacked(byte%5B%5D data) %7B%0A%09%09for (int i = 0; i %3C data.length; i++) %7B%0A%09%09%09int currentIn = data%5Bi%5D & 0xFF;%0A%09%09%09int currentOut = 0;%0A%09%09%09for (int j = 0; j %3C 8; j++) %7B%0A%09%09%09%09currentOut %3C%3C= 1;%0A%09%09%09%09currentOut %7C= ((currentIn %3E%3E (7 - j)) & 0x1) %5E lfsr.nextBit();%0A%09%09%09%7D%0A%09%09%09data%5Bi%5D = (byte) currentOut;%0A%09%09%7D%0A%09%09lfsr.reset();%0A%09%7D%0A%0A
 %7D%0A

remove unused