diff --git a/lrwn/src/applayer/fragmentation/v2.rs b/lrwn/src/applayer/fragmentation/v2.rs index 989f6f5a..5ae94a96 100644 --- a/lrwn/src/applayer/fragmentation/v2.rs +++ b/lrwn/src/applayer/fragmentation/v2.rs @@ -610,28 +610,26 @@ pub fn encode(payload: &[u8], fragment_size: usize, redundancy: usize) -> Result return Err(anyhow!("Payload size must be a multiple of fragment_size")); } - // Create vec of uncoded fragments. - let uncoded_f: Vec<Vec<u8>> = payload.chunks(fragment_size).map(|v| v.to_vec()).collect(); - let w = uncoded_f.len(); + // fragment the data into rows + let mut data_rows: Vec<Vec<u8>> = payload.chunks(fragment_size).map(|v| v.to_vec()).collect(); + let w = data_rows.len(); - let mut coded_f: Vec<Vec<u8>> = Vec::with_capacity(w + redundancy); - - for y in 0..(w + redundancy) { + for y in 0..redundancy { let mut s = vec![0; fragment_size]; - let a = matrix_line(y, w); + let a = matrix_line(y + 1, w); for x in 0..w { if a[x] == 1 { for (m, s_val) in s.iter_mut().enumerate() { - *s_val ^= uncoded_f[x][m]; + *s_val ^= data_rows[x][m]; } } } - coded_f.push(s); + data_rows.push(s); } - Ok(coded_f) + Ok(data_rows) } fn prbs23(x: usize) -> usize { @@ -646,14 +644,10 @@ fn is_power_2(num: usize) -> bool { fn matrix_line(n: usize, m: usize) -> Vec<usize> { let mut line = vec![0; m]; - if n < m { - line[n] = 1; - return line; - } let mm = if is_power_2(m) { 1 } else { 0 }; - let mut x = 1 + (1001 * (n + 1)); + let mut x = 1 + (1001 * n); let mut nb_coeff = 0; while nb_coeff < (m / 2) { @@ -1122,16 +1116,16 @@ mod test { vec![0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f], vec![0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59], vec![0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63], - vec![0x5a, 0x5b, 0x64, 0x65, 0x1e, 0x1f, 0x18, 0x19, 0x2, 0x3], - vec![0x3e, 0x3f, 0x8, 0x9, 0x72, 0x73, 0x4c, 0x4d, 0x46, 0x47], - vec![0x42, 0x43, 0x74, 0x75, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb], - vec![0x60, 0x61, 0x52, 0x53, 0x54, 0x55, 0x66, 0x67, 0x68, 0x69], - vec![0xc, 0xd, 0x6, 0x7, 0x70, 0x71, 0x7a, 0x7b, 0x74, 0x75], - vec![0xa, 0xb, 0xc, 0xd, 0x7e, 0x7f, 0x60, 0x61, 0x72, 0x73], - vec![0x42, 0x43, 0x74, 0x75, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb], - vec![0x1c, 0x1d, 0x16, 0x17, 0x60, 0x61, 0x5a, 0x5b, 0x44, 0x45], - vec![0x2a, 0x2b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x32, 0x33], - vec![0x1a, 0x1b, 0x24, 0x25, 0x5e, 0x5f, 0x48, 0x49, 0x42, 0x43], + vec![0x68, 0x69, 0x52, 0x53, 0x5c, 0x5d, 0x76, 0x77, 0x70, 0x71], + vec![0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x6a, 0x6b, 0x7c, 0x7d], + vec![0x5c, 0x5d, 0x6e, 0x6f, 0x10, 0x11, 0x2, 0x3, 0x4, 0x5], + vec![0x6c, 0x6d, 0x6e, 0x6f, 0x60, 0x61, 0x42, 0x43, 0x54, 0x55], + vec![0x12, 0x13, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x3a, 0x3b], + vec![0x48, 0x49, 0x7a, 0x7b, 0x7c, 0x7d, 0x6e, 0x6f, 0x70, 0x71], + vec![0x1c, 0x1d, 0x2e, 0x2f, 0x50, 0x51, 0x52, 0x53, 0x44, 0x45], + vec![0x58, 0x59, 0x52, 0x53, 0x2c, 0x2d, 0x16, 0x17, 0x0, 0x1], + vec![0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x30, 0x31, 0x22, 0x23], + vec![0x0, 0x1, 0x3a, 0x3b, 0x44, 0x45, 0x7e, 0x7f, 0x68, 0x69], ], expected_error: None, }, @@ -1151,11 +1145,11 @@ mod test { vec![0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f], vec![0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59], vec![0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63], - vec![0x5a, 0x5b, 0x64, 0x65, 0x1e, 0x1f, 0x18, 0x19, 0x2, 0x3], - vec![0x3e, 0x3f, 0x8, 0x9, 0x72, 0x73, 0x4c, 0x4d, 0x46, 0x47], - vec![0x42, 0x43, 0x74, 0x75, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb], - vec![0x60, 0x61, 0x52, 0x53, 0x54, 0x55, 0x66, 0x67, 0x68, 0x69], - vec![0xc, 0xd, 0x6, 0x7, 0x70, 0x71, 0x7a, 0x7b, 0x74, 0x75], + vec![0x68, 0x69, 0x52, 0x53, 0x5c, 0x5d, 0x76, 0x77, 0x70, 0x71], + vec![0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x6a, 0x6b, 0x7c, 0x7d], + vec![0x5c, 0x5d, 0x6e, 0x6f, 0x10, 0x11, 0x2, 0x3, 0x4, 0x5], + vec![0x6c, 0x6d, 0x6e, 0x6f, 0x60, 0x61, 0x42, 0x43, 0x54, 0x55], + vec![0x12, 0x13, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x3a, 0x3b], ], expected_error: None, },