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
134
135
136
137
138
139
use bits::Dibit;
use consts;
pub fn descramble(dibits: &[Dibit; consts::FRAME_DIBITS], idx: usize) -> u32 {
DESCRAMBLERS[idx].descramble(dibits)
}
const DESCRAMBLERS: [VoiceFrameDescrambler; 8] = [
VoiceFrameDescrambler(&[
ZigZag::hi(0, 23),
]),
VoiceFrameDescrambler(&[
ZigZag::lo(69, 1),
ZigZag::lo(0, 22),
]),
VoiceFrameDescrambler(&[
ZigZag::lo(66, 2),
ZigZag::hi(1, 21),
]),
VoiceFrameDescrambler(&[
ZigZag::lo(64, 3),
ZigZag::lo(1, 20),
]),
VoiceFrameDescrambler(&[
ZigZag::lo(61, 4),
ZigZag::hi(2, 11),
]),
VoiceFrameDescrambler(&[
ZigZag::lo(35, 13),
ZigZag::lo(2, 2),
]),
VoiceFrameDescrambler(&[
ZigZag::lo(8, 15),
]),
VoiceFrameDescrambler(&[
ZigZag::hi(53, 7),
]),
];
struct VoiceFrameDescrambler(&'static [ZigZag]);
impl VoiceFrameDescrambler {
pub fn descramble(&self, dibits: &[Dibit; consts::FRAME_DIBITS]) -> u32 {
self.0.iter().fold(0, |word, zz| {
zz.fold(word, |buf, (idx, hi)| {
buf << 1 | if hi {
dibits[idx].hi() as u32
} else {
dibits[idx].lo() as u32
}
})
})
}
}
#[derive(Clone, Copy)]
struct ZigZag {
hi: bool,
idx: usize,
remain: usize,
}
impl ZigZag {
pub const fn hi(start: usize, num: usize) -> ZigZag {
ZigZag {
hi: true,
idx: start,
remain: num,
}
}
pub const fn lo(start: usize, num: usize) -> ZigZag {
ZigZag {
hi: false,
idx: start,
remain: num,
}
}
}
impl Iterator for ZigZag {
type Item = (usize, bool);
fn next(&mut self) -> Option<Self::Item> {
if self.remain == 0 {
return None;
}
let cur = (self.idx, self.hi);
self.idx += 3;
self.remain -= 1;
self.hi = !self.hi;
Some(cur)
}
}
#[cfg(test)]
mod test {
use super::DESCRAMBLERS;
use consts;
#[test]
fn test_steps_exhaustive() {
let mut visited = [0u32; consts::FRAME_DIBITS];
for d in DESCRAMBLERS.iter() {
for &zz in d.0.iter() {
for (idx, _) in zz {
visited[idx] += 1;
}
}
}
for &v in visited.iter() {
assert_eq!(v, 2);
}
}
}