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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use byteorder::{LE, ReadBytesExt};
use std::{fmt, io};
use std::borrow::Cow;
#[derive(Debug)]
pub enum Error {
Io(::std::io::Error),
Version(u32),
Magic([u8; 4]),
Length {
length: u32,
length_read: usize,
},
ChunkLength {
ty: ChunkType,
length: u32,
length_read: usize,
},
ChunkType(ChunkType),
UnknownChunkType([u8; 4]),
}
#[derive(Clone, Debug)]
pub struct Glb<'a> {
pub header: Header,
pub json: Cow<'a, [u8]>,
pub bin: Option<Cow<'a, [u8]>>,
}
#[derive(Copy, Clone, Debug)]
pub struct Header {
pub magic: [u8; 4],
pub version: u32,
pub length: u32,
}
#[derive(Copy, Clone, Debug)]
pub enum ChunkType {
Json,
Bin,
}
#[derive(Copy, Clone, Debug)]
struct ChunkHeader {
length: u32,
ty: ChunkType,
}
impl Header {
fn from_reader<R: io::Read>(mut reader: R) -> Result<Self, Error> {
use self::Error::Io;
let mut magic = [0; 4];
reader.read_exact(&mut magic).map_err(Io)?;
if &magic == b"glTF" {
Ok(Self {
magic,
version: reader.read_u32::<LE>().map_err(Io)?,
length: reader.read_u32::<LE>().map_err(Io)?,
})
} else {
Err(Error::Magic(magic))
}
}
fn size_of() -> usize { 12 }
}
impl ChunkHeader {
fn from_reader<R: io::Read>(mut reader: R) -> Result<Self, Error> {
use self::Error::Io;
let length = reader.read_u32::<LE>().map_err(Io)?;
let mut ty = [0; 4];
reader.read_exact(&mut ty).map_err(Io)?;
let ty = match &ty {
b"JSON" => Ok(ChunkType::Json),
b"BIN\0" => Ok(ChunkType::Bin),
_ => Err(Error::UnknownChunkType(ty)),
}?;
Ok(Self { length, ty })
}
}
impl<'a> Glb<'a> {
pub fn from_slice(mut data: &'a [u8]) -> Result<Self, ::Error> {
let header = Header::from_reader(&mut data)
.and_then(|header| {
let contents_length = header.length as usize - Header::size_of();
if contents_length <= data.len() {
Ok(header)
} else {
Err(Error::Length {
length: contents_length as u32,
length_read: data.len(),
})
}
})
.map_err(::Error::Glb)?;
match header.version {
2 => Self::from_v2(data)
.map(|(json, bin)| Glb { header, json: json.into(), bin: bin.map(Into::into) })
.map_err(::Error::Glb),
x => Err(::Error::Glb(Error::Version(x)))
}
}
pub fn from_reader<R: io::Read>(mut reader: R) -> Result<Self, ::Error> {
let header = Header::from_reader(&mut reader).map_err(::Error::Glb)?;
match header.version {
2 => {
let glb_len = header.length - Header::size_of() as u32;
let mut buf = vec![0; glb_len as usize];
if let Err(e) = reader.read_exact(&mut buf).map_err(Error::Io) {
Err(::Error::Glb(e))
} else {
Self::from_v2(&buf)
.map(|(json, bin)| Glb {
header,
json: json.to_vec().into(),
bin: bin.map(<[u8]>::to_vec).map(Into::into),
})
.map_err(::Error::Glb)
}
}
x => Err(::Error::Glb(Error::Version(x)))
}
}
fn from_v2(mut data: &'a [u8]) -> Result<(&'a [u8], Option<&'a [u8]>), Error> {
let (json, mut data) = ChunkHeader::from_reader(&mut data)
.and_then(|json_h| if let ChunkType::Json = json_h.ty {
Ok(json_h)
} else {
Err(Error::ChunkType(json_h.ty))
})
.and_then(|json_h| if json_h.length as usize <= data.len() {
Ok(json_h)
} else {
Err(Error::ChunkLength {
ty: json_h.ty,
length: json_h.length,
length_read: data.len(),
})
})
.map(|json_h| data.split_at(json_h.length as usize))?;
let bin = if data.len() > 0 {
ChunkHeader::from_reader(&mut data)
.and_then(|bin_h| if let ChunkType::Bin = bin_h.ty {
Ok(bin_h)
} else {
Err(Error::ChunkType(bin_h.ty))
})
.and_then(|bin_h| if bin_h.length as usize <= data.len() {
Ok(bin_h)
} else {
Err(Error::ChunkLength {
ty: bin_h.ty,
length: bin_h.length,
length_read: data.len(),
})
})
.map(|bin_h| data.split_at(bin_h.length as usize))
.map(|(x, _)| Some(x))?
} else {
None
};
Ok((json, bin))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::error::Error;
write!(f, "{}", self.description())
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Io(ref e) => e.description(),
Error::Version(_) => "unsupported version",
Error::Magic(_) => "not glTF magic",
Error::Length { .. } => "could not completely read the object",
Error::ChunkLength { ty, .. } => match ty {
ChunkType::Json => "JSON chunk length exceeds that of slice",
ChunkType::Bin => "BIN\\0 chunk length exceeds that of slice",
},
Error::ChunkType(ty) => match ty {
ChunkType::Json => "was not expecting JSON chunk",
ChunkType::Bin => "was not expecting BIN\\0 chunk",
},
Error::UnknownChunkType(_) => "unknown chunk type",
}
}
}