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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::{hint, Error, Parcel, Settings};
use std::io::prelude::*;
use std::{marker, mem};
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Aligned<T, ToSizeOfType>
where T: Parcel,
ToSizeOfType: Sized {
pub value: T,
_phantom: marker::PhantomData<ToSizeOfType>,
}
impl<T, ToSizeOfType> Aligned<T, ToSizeOfType>
where T: Parcel,
ToSizeOfType: Sized {
pub fn new(value: T) -> Self {
Aligned { value, _phantom: marker::PhantomData }
}
pub fn align_to_bytes() -> usize {
mem::size_of::<ToSizeOfType>()
}
}
impl<T, ToSizeOfType> Parcel for Aligned<T, ToSizeOfType>
where T: Parcel,
ToSizeOfType: Sized {
const TYPE_NAME: &'static str = "Aligned";
fn read_field(read: &mut dyn Read,
settings: &Settings,
hints: &mut hint::Hints) -> Result<Self, Error> {
let inner_value = T::read_field(read, settings, hints)?;
let value_size = inner_value.raw_bytes_field(settings, hints).unwrap().len();
let padding_size = calculate_padding(Self::align_to_bytes(), value_size);
for _ in 0..padding_size {
let padding_byte = u8::read(read, settings)?;
assert_eq!(0x00, padding_byte, "padding bytes should be zero");
}
Ok(Aligned { value: inner_value, _phantom: marker::PhantomData })
}
fn write_field(&self,
write: &mut dyn Write,
settings: &Settings,
hints: &mut hint::Hints) -> Result<(), Error> {
let unaligned_bytes = self.value.raw_bytes_field(settings, hints)?;
let aligned_bytes = align_to(Self::align_to_bytes(), 0x00, unaligned_bytes);
write.write(&aligned_bytes)?;
Ok(())
}
}
impl<T, ToSizeOfType> From<T> for Aligned<T, ToSizeOfType>
where T: Parcel,
ToSizeOfType: Sized {
fn from(value: T) -> Self {
Aligned { value, _phantom: marker::PhantomData }
}
}
fn align_to(align_to: usize,
padding_byte: u8,
bytes: Vec<u8>) -> Vec<u8> {
let extra_padding_needed = calculate_padding(align_to, bytes.len());
let extra_padding = (0..).into_iter().take(extra_padding_needed).map(|_| padding_byte);
let bytes: Vec<_> = bytes.into_iter().chain(extra_padding).collect();
assert_eq!(0, bytes.len() % align_to,
"failed to align");
bytes
}
fn calculate_padding(align_to: usize,
unaligned_size: usize) -> usize {
(align_to - (unaligned_size % align_to)) % align_to
}
#[cfg(test)]
mod test {
use super::*;
mod alignment_calculations {
use super::*;
#[test]
fn test_aligning_when_none_needed() {
assert_eq!(vec![1, 2], align_to(1, 0x00, vec![1, 2]));
assert_eq!(vec![1, 2], align_to(2, 0x00, vec![1, 2]));
}
#[test]
fn test_align_to_3_with_size_2() {
assert_eq!(vec![1, 2, 0], align_to(3, 0x00, vec![1, 2]));
}
#[test]
fn test_align_to_4_with_size_2() {
assert_eq!(vec![1, 2, 0xff, 0xff], align_to(4, 0xff, vec![1, 2]));
}
#[test]
fn test_align_to_3_with_size_5() {
assert_eq!(vec![1, 2, 3, 4, 5, 0], align_to(3, 0x00, vec![1, 2, 3, 4, 5]));
}
#[test]
fn test_align_to_4_with_size_97() {
let original = [1; 97];
let aligned = align_to(4, 0x00, original.to_vec());
let count_ones = aligned.iter().filter(|&&i| i == 1).count();
let count_zeros = aligned.iter().filter(|&&i| i == 0).count();
assert_eq!(97, count_ones);
assert_eq!(3, count_zeros);
}
}
}