aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/test_v27scrambler.c
blob: 68d96da89ecf01d5a302130fe8ca4fcd2902332c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "../libv27/scrambler.h"

static int show_bin(uint8_t *data1, uint8_t *data2, int len)
{
	int i, j, error = 0;;
	uint8_t bit1, bit2;

	for (i = 0; i < len; i++) {
		printf(".");
		for (j = 0; j < 8; j++) {
			bit1 = (data1[i] >> j) & 1;
			bit2 = (data2[i] >> j) & 1;
			if (bit1 == bit2)
				printf("%d", bit1);
			else {
				printf("X");
				error++;
			}
		}
	}

	printf("\n");

	return error;
}

static int check_repetition(uint8_t *data, int len, int repeat, int start)
{
	int i;
	uint8_t b1, b2;

	for (i = start; i < (len * 8 - repeat); i++) {
		b1 = (data[i >> 3] >> (i & 7)) & 1;
		b2 = (data[(i+repeat) >> 3] >> ((i+repeat) & 7)) & 1;
		if (b1 != b2)
			return i - start + repeat;
	}

	return 0;
}

int main(void)
{
	v27scrambler_t scram, descram;

	char message[] = "Jolly Roger~~~~";
	int len = strlen(message);
	uint8_t data[len];
	int ret;

	printf("Message: %s\n", message);

	memcpy(data, message, len);
	show_bin(data, (uint8_t *)message, len);

	v27_scrambler_init(&scram, 1, 0);
	v27_scrambler_block(&scram, data, len);

	printf("Scrambled:\n");
	show_bin(data, data, len);

	v27_scrambler_init(&descram, 1, 1);
	v27_scrambler_block(&descram, data, len);
	
	printf("Descramble without corruption?\n");

	ret = show_bin(data, (uint8_t *)message, len);
	if (ret) {
		printf("Descrambling failed!\n");
		return 1;
	}
	printf("Yes!\n");

	printf("\n");

	v27_scrambler_init(&scram, 1, 0);
	v27_scrambler_block(&scram, data, len);

	data[0] = 'B';
	data[1] = 'U';
	data[2] = 'G';

	v27_scrambler_init(&descram, 1, 1);
	v27_scrambler_block(&descram, data, len);
	
	printf("Descramble with 3 bytes corruption: (should fix itself after 4 bytes)\n");

	show_bin(data, (uint8_t *)message, len);

	printf("\n");

	printf("Descramble a scrambled sequence of 8 bit repetitions with V.27: 01111110\n");

	memset(data, 0x7e, len);

	v27_scrambler_init(&descram, 0, 1);
	v27_scrambler_block(&descram, data, len);

	show_bin(data, (uint8_t *)data, len);

	/* note at position 6 we have no more change towards 8 bit offset */
	ret = check_repetition(data, len, 8, 6);
	if (ret) {
		printf("Theres is a change of repetition after %d bits after start %d, please fix!\n", ret, 6);
		return 1;
	}
	printf("Repetition not detected, good!\n");
	
	printf("\n");

	printf("Descramble a scrambled sequence of 8 bit repetitions with V.27bis/ter: 01111110\n");

	memset(data, 0x7e, len);

	v27_scrambler_init(&descram, 1, 1);
	v27_scrambler_block(&descram, data, len);

	show_bin(data, (uint8_t *)data, len);

	/* note at position 6 we have no more change towards 8 bit offset */
	ret = check_repetition(data, len, 8, 6);
	if (ret != 34) {
		printf("Theres is NO change of repetition after 34 bits, but after %d bits, which should not happen!\n", ret);
		return 1;
	}
	printf("Repetition detected after %d bits from start %d, good!\n", ret, 6);
	
	return 0;
}