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
use std::io::{ErrorKind as IoErrorKind, Read};
use error::{NiftiError, Result};
use byteorder::{ByteOrder, ReadBytesExt};
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub struct Extender([u8; 4]);
impl Extender {
pub fn from_stream<S: Read>(mut source: S) -> Result<Self> {
let mut extension = [0u8; 4];
source.read_exact(&mut extension)?;
Ok(extension.into())
}
pub fn from_stream_optional<S: Read>(mut source: S) -> Result<Option<Self>> {
let mut extension = [0u8; 4];
match source.read_exact(&mut extension) {
Ok(()) => Ok(Some(extension.into())),
Err(ref e) if e.kind() == IoErrorKind::UnexpectedEof => {
Ok(None)
}
Err(e) => Err(NiftiError::from(e)),
}
}
pub fn has_extensions(&self) -> bool {
self.0[0] != 0
}
pub fn as_bytes(&self) -> &[u8; 4] {
&self.0
}
}
impl From<[u8; 4]> for Extender {
fn from(extender: [u8; 4]) -> Self {
Extender(extender)
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Extension {
esize: i32,
ecode: i32,
edata: Vec<u8>,
}
impl Extension {
pub fn new(esize: i32, ecode: i32, edata: Vec<u8>) -> Self {
if esize as usize != 8 + edata.len() {
panic!(
"Illegal extension size: esize is {}, but full size is {}",
esize,
edata.len()
);
}
Extension {
esize,
ecode,
edata,
}
}
pub fn size(&self) -> i32 {
self.esize
}
pub fn code(&self) -> i32 {
self.ecode
}
pub fn data(&self) -> &Vec<u8> {
&self.edata
}
pub fn into_data(self) -> Vec<u8> {
self.edata
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct ExtensionSequence {
extender: Extender,
extensions: Vec<Extension>,
}
impl IntoIterator for ExtensionSequence {
type Item = Extension;
type IntoIter = ::std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.extensions.into_iter()
}
}
impl<'a> IntoIterator for &'a ExtensionSequence {
type Item = &'a Extension;
type IntoIter = ::std::slice::Iter<'a, Extension>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl ExtensionSequence {
pub fn from_stream<B: ByteOrder, S: Read>(
extender: Extender,
mut source: S,
len: usize,
) -> Result<Self> {
let mut extensions = Vec::new();
if extender.has_extensions() {
let mut offset = 0;
while offset < len {
let esize = source.read_i32::<B>()?;
let ecode = source.read_i32::<B>()?;
let data_size = esize as usize - 8;
let mut edata = vec![0u8; data_size];
source.read_exact(&mut edata)?;
extensions.push(Extension::new(esize, ecode, edata));
offset += esize as usize;
}
}
Ok(ExtensionSequence {
extender,
extensions,
})
}
pub fn iter(&self) -> ::std::slice::Iter<Extension> {
self.extensions.iter()
}
pub fn is_empty(&self) -> bool {
self.extensions.is_empty()
}
pub fn len(&self) -> usize {
self.extensions.len()
}
pub fn extender(&self) -> Extender {
self.extender
}
}