Skip to content

Commit 2048747

Browse files
committed
Added XOR for t=3 packets, added more verbiage, removed variable-length arrays.
1 parent 67d5900 commit 2048747

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Diff for: src/bchapply.c

+22-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
#include "bch.h"
44

55
#define SHOW_BYTES
6+
#define MAX_LENGTH 64
7+
8+
static uint8_t r2f_mask[] = { 0x07, 0x76, 0xa0 };
69

710
int test(bch_t *bch, char *msg, int *bits, int length) {
811
int corrected = 0;
9-
int temp_bits[length];
12+
int temp_bits[MAX_LENGTH];
1013
uint8_t bytes[8];
1114

1215
memcpy(temp_bits, bits, length * sizeof(int));
@@ -43,15 +46,21 @@ int main(int argc, char **argv) {
4346

4447

4548
if (argc != 4) {
46-
fprintf(stderr, "Expecting 3 arguments.\n");
49+
fprintf(stderr, "Expecting 3 arguments: m, length, and t.\n");
4750
return -1;
4851
}
4952

5053
sscanf(argv[1], "%d", &m);
5154
sscanf(argv[2], "%d", &length);
5255
sscanf(argv[3], "%d", &t);
5356

54-
int orig_bits[length+1];
57+
if (length > MAX_LENGTH) {
58+
fprintf(stderr, "Max supported length is %d\n", MAX_LENGTH);
59+
return -2;
60+
}
61+
62+
63+
int orig_bits[MAX_LENGTH+1];
5564

5665
init_bch(&bch, m, length, t);
5766
data_len = bch.k;
@@ -63,24 +72,27 @@ printf("data_len=%d, crc_len=%d\n", data_len, crc_len);
6372
//
6473
// THIS IS THE LSB-FIRST VERSION
6574
//
66-
fprintf(stderr, "Enter HCB+ATAD _WITH_ the parity bit intact and XOR already applied.\n");
75+
fprintf(stderr, "Enter HCB+ATAD _WITH_ the parity bit intact.\n");
76+
fprintf(stderr, "If 't' is 3, that implies an R2F packet and the given packet will be XOR'ed with 0x0776a0.\n");
6777
while (1) {
6878
for (int i = 0; i < 8; i++) {
6979
int temp;
7080
int status = scanf("%x ", &temp);
71-
bytes[i] = temp;
7281
if (status == EOF) {
7382
return 0;
7483
}
7584

7685
if (status != 1) {
7786
fprintf(stderr, "Error: %d", status);
7887
}
79-
printf("%0x ", bytes[i]);
88+
89+
bytes[i] = temp;
90+
if (t == 3 && i < sizeof(r2f_mask)) {
91+
bytes[i] ^= r2f_mask[i];
92+
}
8093
}
81-
printf("\n");
8294

83-
int temp[length];
95+
int temp[MAX_LENGTH];
8496

8597
// HCB + ATAD
8698
bytes_to_bits(bytes, orig_bits, length+1);
@@ -111,7 +123,7 @@ printf("\n");
111123
test(&bch, "DATA+BCH: ", temp, length);
112124

113125
// BCH+DATA
114-
int swap[length];
126+
int swap[MAX_LENGTH];
115127
bytes_to_bits(bytes, orig_bits, length+1);
116128
rotate_bits(orig_bits+1, temp, length);
117129
// now DATA+BCH
@@ -123,7 +135,7 @@ printf("\n");
123135
printf("\n");
124136
test(&bch, "BCH+DATA: ", swap, length);
125137

126-
int rot[length];
138+
int rot[MAX_LENGTH];
127139
// DATA + HCB
128140
bytes_to_bits(bytes, orig_bits, length+1);
129141
memcpy(rot+data_len, orig_bits + 1, crc_len * sizeof(int));

0 commit comments

Comments
 (0)