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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::wmf::imports::*;
/// The DeviceIndependentBitmap (DIB) Object defines an image in
/// device-independent bitmap (DIB) format.
#[derive(Clone, Debug)]
pub struct DeviceIndependentBitmap {
/// DIBHeaderInfo (variable): Either a BitmapCoreHeader Object or a
/// BitmapInfoHeader Object that specifies information about the image.
///
/// The first 32 bits of this field is the HeaderSize value. If it is
/// 0x0000000C, then this is a BitmapCoreHeader; otherwise, this is a
/// BitmapInfoHeader.
pub dib_header_info: crate::wmf::parser::BitmapInfoHeader,
/// Colors (variable): An optional array of either RGBQuad Objects or
/// 16-bit unsigned integers that define a color table.
///
/// The size and contents of this field SHOULD be determined from the
/// metafile record or object that contains this DeviceIndependentBitmap
/// Object and from information in the DIBHeaderInfo field. See ColorUsage
/// Enumeration and BitCount Enumeration for additional details.
pub colors: Colors,
/// BitmapBuffer (variable): A buffer containing the image, which is not
/// required to be contiguous with the DIB header, unless this is a packed
/// bitmap.
pub bitmap_buffer: BitmapBuffer,
}
impl DeviceIndependentBitmap {
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip_all,
err(level = tracing::Level::ERROR, Display),
))]
pub(crate) fn parse_with_color_usage<R: crate::wmf::Read>(
buf: &mut R,
color_usage: crate::wmf::parser::ColorUsage,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
let (dib_header_info, mut consumed_bytes) =
crate::wmf::parser::BitmapInfoHeader::parse(buf)?;
let (colors, c) = Colors::parse(buf, color_usage, &dib_header_info)?;
consumed_bytes += c;
// TODO: Not written in [MS-WMF] how to parse this field.
let undefined_space = vec![];
let (a_data, c) = crate::wmf::parser::read_variable(buf, dib_header_info.size())?;
consumed_bytes += c;
Ok((
Self {
dib_header_info,
colors,
bitmap_buffer: BitmapBuffer {
undefined_space,
a_data,
},
},
consumed_bytes,
))
}
}
#[derive(Clone, Debug)]
pub enum Colors {
PaletteIndices(Vec<u16>),
RGBQuad(Vec<crate::wmf::parser::RGBQuad>),
RGBTriple(Vec<crate::wmf::parser::RGBTriple>),
Null,
}
impl Colors {
pub fn parse<R: crate::wmf::Read>(
buf: &mut R,
color_usage: crate::wmf::parser::ColorUsage,
dib_header_info: &crate::wmf::parser::BitmapInfoHeader,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
if matches!(color_usage, crate::wmf::parser::ColorUsage::DIB_PAL_INDICES)
|| matches!(
dib_header_info.bit_count(),
crate::wmf::parser::BitCount::BI_BITCOUNT_0
)
{
return Ok((Colors::Null, 0));
}
if matches!(
dib_header_info,
crate::wmf::parser::BitmapInfoHeader::Core { .. }
) {
return Self::parse_with_core_header(buf, color_usage, dib_header_info);
}
Self::parse_with_info_header(buf, color_usage, dib_header_info)
}
fn parse_with_core_header<R: crate::wmf::Read>(
buf: &mut R,
color_usage: crate::wmf::parser::ColorUsage,
dib_header_info: &crate::wmf::parser::BitmapInfoHeader,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
assert!(matches!(
dib_header_info,
crate::wmf::parser::BitmapInfoHeader::Core { .. }
));
// core header does not have color_used field.
if matches!(
dib_header_info.bit_count(),
crate::wmf::parser::BitCount::BI_BITCOUNT_5
) {
return Ok((Colors::Null, 0));
}
Self::parse_from_color_usage(
buf,
color_usage,
dib_header_info.color_used() as usize,
true,
)
}
fn parse_with_info_header<R: crate::wmf::Read>(
buf: &mut R,
color_usage: crate::wmf::parser::ColorUsage,
dib_header_info: &crate::wmf::parser::BitmapInfoHeader,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
assert!(matches!(
dib_header_info,
crate::wmf::parser::BitmapInfoHeader::Info { .. }
| crate::wmf::parser::BitmapInfoHeader::V4 { .. }
| crate::wmf::parser::BitmapInfoHeader::V5 { .. }
));
match dib_header_info.bit_count() {
crate::wmf::parser::BitCount::BI_BITCOUNT_0 => unreachable!(),
crate::wmf::parser::BitCount::BI_BITCOUNT_1
| crate::wmf::parser::BitCount::BI_BITCOUNT_2
| crate::wmf::parser::BitCount::BI_BITCOUNT_3 => Self::parse_from_color_usage(
buf,
color_usage,
dib_header_info.color_used() as usize,
false,
),
crate::wmf::parser::BitCount::BI_BITCOUNT_5 => {
// ignore result
let (_, bytes) = Self::parse_from_color_usage(
buf,
color_usage,
dib_header_info.color_used() as usize,
false,
)?;
Ok((Colors::Null, bytes))
}
crate::wmf::parser::BitCount::BI_BITCOUNT_4
| crate::wmf::parser::BitCount::BI_BITCOUNT_6 => {
match &dib_header_info {
crate::wmf::parser::BitmapInfoHeader::Core(_) => unreachable!(),
crate::wmf::parser::BitmapInfoHeader::Info(
crate::wmf::parser::BitmapInfoHeaderInfo { compression, .. },
)
| crate::wmf::parser::BitmapInfoHeader::V4(
crate::wmf::parser::BitmapInfoHeaderV4 { compression, .. },
)
| crate::wmf::parser::BitmapInfoHeader::V5(
crate::wmf::parser::BitmapInfoHeaderV5 { compression, .. },
) => match compression {
crate::wmf::parser::Compression::BI_RGB => {
// ignore result
let (_, bytes) = Self::parse_from_color_usage(
buf,
color_usage,
dib_header_info.color_used() as usize,
false,
)?;
Ok((Colors::Null, bytes))
}
crate::wmf::parser::Compression::BI_BITFIELDS => {
Self::parse_from_color_usage(
buf,
color_usage,
dib_header_info.color_used() as usize,
false,
)
}
_ => Ok((Colors::Null, 0)),
},
}
}
}
}
fn parse_from_color_usage<R: crate::wmf::Read>(
buf: &mut R,
color_usage: crate::wmf::parser::ColorUsage,
colors_length: usize,
core: bool,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
// colors_length는 BITMAPINFOHEADER의 colors_used(검증되지 않은 u32)에서
// 온다. 색상표가 존재하는 비트 심도(<=8bpp)에서 사양상 최대 항목 수는
// 2^8=256이므로 이를 상한으로 둔다. 상한이 없으면 Vec::with_capacity가
// 거대한 값을 그대로 예약해 OOM abort로 이어진다 (doc_info.rs tab_count,
// emf parse_points16 #2992와 동일한 클래스).
let colors_length = colors_length.min(256);
let mut consumed_bytes: usize = 0;
match color_usage {
crate::wmf::parser::ColorUsage::DIB_RGB_COLORS if core => {
let mut table = Vec::with_capacity(colors_length);
for _ in 0..colors_length {
let (v, c) = crate::wmf::parser::RGBTriple::parse(buf)?;
consumed_bytes += c;
table.push(v);
}
Ok((Colors::RGBTriple(table), consumed_bytes))
}
crate::wmf::parser::ColorUsage::DIB_RGB_COLORS => {
let mut table = Vec::with_capacity(colors_length);
for _ in 0..colors_length {
let (v, c) = crate::wmf::parser::RGBQuad::parse(buf)?;
consumed_bytes += c;
table.push(v);
}
Ok((Colors::RGBQuad(table), consumed_bytes))
}
crate::wmf::parser::ColorUsage::DIB_PAL_COLORS => {
let mut table = Vec::with_capacity(colors_length);
for _ in 0..colors_length {
let (v, c) = crate::wmf::parser::read_u16_from_le_bytes(buf)?;
consumed_bytes += c;
table.push(v);
}
Ok((Colors::PaletteIndices(table), consumed_bytes))
}
crate::wmf::parser::ColorUsage::DIB_PAL_INDICES => unreachable!(),
}
}
}
#[derive(Clone)]
pub struct BitmapBuffer {
/// UndefinedSpace (variable): An optional field that MUST be ignored. If
/// this DIB is a packed bitmap, this field MUST NOT be present.
pub undefined_space: Vec<u8>,
/// aData (variable): An array of bytes that define the image.
///
/// The size and format of this data is determined by information in the
/// DIBHeaderInfo field. If it is a BitmapCoreHeader, the size in bytes
/// MUST be calculated as follows:
///
/// ```text
/// (((Width * Planes * BitCount + 31) & ~31) / 8) * abs(Height)
/// ```
///
/// This formula SHOULD also be used to calculate the size of aData when
/// DIBHeaderInfo is a BitmapInfoHeader Object, using values from that
/// object, but only if its Compression value is BI_RGB, BI_BITFIELDS, or
/// BI_CMYK.
///
/// Otherwise, the size of aData MUST be the BitmapInfoHeader Object value
/// ImageSize.
pub a_data: Vec<u8>,
}
impl core::fmt::Debug for BitmapBuffer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BitmapBuffer")
.field(
"undefined_space",
&format!("[u8; {}]", self.undefined_space.len()),
)
.field("a_data", &format!("[u8; {}]", self.a_data.len()))
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
/// colors_length가 BITMAPINFOHEADER의 검증되지 않은 colors_used(u32)에서
/// 그대로 오는 경우, 거대한 값(u32::MAX)을 넘겨도 Vec::with_capacity가
/// 그 값을 그대로 예약하지 않고(256 상한) 데이터 부족으로 정상적으로
/// 에러를 반환해야 한다 (OOM abort 없이 graceful degradation).
#[test]
fn parse_from_color_usage_bounds_huge_colors_length() {
let mut data: &[u8] = &[0u8; 4]; // RGBQuad 1개 분량도 안 되는 소량 데이터
let result = Colors::parse_from_color_usage(
&mut data,
crate::wmf::parser::ColorUsage::DIB_RGB_COLORS,
u32::MAX as usize,
false,
);
assert!(result.is_err());
}
}