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
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hwpf.model;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.ddf.DefaultEscherRecordFactory;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherRecordFactory;
import org.apache.poi.ddf.EscherRecordTypes;
import org.apache.poi.util.Internal;
import static org.apache.logging.log4j.util.Unbox.box;
/**
* Information about drawings in the document.
* <p>
* The {@code delay stream} referenced in {@code [MS-ODRAW]} is the {@code WordDocument} stream.
*/
@Internal
public final class OfficeArtContent {
protected static final Logger LOG = PoiLogManager.getLogger(OfficeArtContent.class);
/**
* {@link EscherRecordTypes#DGG_CONTAINER} containing drawing group information for the document.
*/
private final EscherContainerRecord drawingGroupData = new EscherContainerRecord();
/**
* {@link EscherRecordTypes#DG_CONTAINER} for drawings in the Main Document.
* <p>
* {@code null} to indicate that the document does not have a {@link EscherRecordTypes#DG_CONTAINER} for the Main
* Document.
*/
private EscherContainerRecord mainDocumentDgContainer;
/**
* {@link EscherRecordTypes#DG_CONTAINER} for drawings in the Header Document.
* <p>
* {@code null} to indicate that the document does not have a {@link EscherRecordTypes#DG_CONTAINER} for the Header
* Document.
*/
private EscherContainerRecord headerDocumentDgContainer;
public OfficeArtContent(byte[] data, int offset, int size) {
fillEscherRecords(data, offset, size);
}
/**
* Parses the records out of the given data.
*
* The thing to be aware of here is that if {@code size} is {@code 0}, the document does not contain images.
*
* @see FileInformationBlock#getLcbDggInfo()
*/
private void fillEscherRecords(byte[] data, int offset, int size) {
if (size == 0) return;
EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
int pos = offset;
pos += drawingGroupData.fillFields(data, pos, recordFactory);
if (drawingGroupData.getRecordId() == EscherRecordTypes.DGG_CONTAINER.typeID) {
LOG.debug("Invalid record-id for filling Escher records: {}", drawingGroupData.getRecordId());
}
/*
* After the drawingGroupData there is an array (2 slots max) that has data about drawings. According to the
* spec, the first slot is for the Main Document, the second for the Header Document. Additionally, the
* OfficeArtWordDrawing structure has a byte (dgglbl) that indicates whether the structure is for the Main or
* Header Document. In practice we've seen documents such as 61911.doc where the order of array entries does not
* match the dgglbl byte. As the byte is more likely to be reliable, we base the parsing off of that rather than
* array order.
*/
// This should loop at most twice
while (pos < offset + size) {
// Named this way to match section 2.9.172 of [MS-DOC] - v20191119.
byte dgglbl = data[pos];
if (dgglbl != 0x00 && dgglbl != 0x01) {
throw new IllegalArgumentException("Invalid dgglbl when filling Escher records: " + dgglbl);
}
pos++;
EscherContainerRecord dgContainer = new EscherContainerRecord();
pos+= dgContainer.fillFields(data, pos, recordFactory);
if (dgContainer.getRecordId() != EscherRecordTypes.DG_CONTAINER.typeID) {
throw new IllegalArgumentException("Did have an invalid record-type: " + dgContainer.getRecordId() +
" when filling Escher records");
}
switch (dgglbl) {
case 0x00:
mainDocumentDgContainer = dgContainer;
break;
case 0x01:
headerDocumentDgContainer = dgContainer;
break;
default:
PoiLogManager.getLogger(OfficeArtContent.class).atWarn()
.log("dgglbl {} for OfficeArtWordDrawing is out of bounds [0, 1]", box(dgglbl));
}
}
if (pos != offset + size) {
throw new IllegalStateException("Did not read all data when filling Escher records: "
+ "pos: " + pos + ", offset: " + offset + ", size: " + size);
}
}
private List<? extends EscherContainerRecord> getDgContainers() {
List<EscherContainerRecord> dgContainers = new ArrayList<>(2);
if (mainDocumentDgContainer != null) {
dgContainers.add(mainDocumentDgContainer);
}
if (headerDocumentDgContainer != null) {
dgContainers.add(headerDocumentDgContainer);
}
return dgContainers;
}
/**
* @return The {@link EscherRecordTypes#BSTORE_CONTAINER} or {@code null} if the document doesn't have one.
*/
public EscherContainerRecord getBStoreContainer() {
return drawingGroupData.getChildById(EscherRecordTypes.BSTORE_CONTAINER.typeID);
}
public List<? extends EscherContainerRecord> getSpgrContainers()
{
List<EscherContainerRecord> spgrContainers = new ArrayList<>(
1);
for ( EscherContainerRecord dgContainer : getDgContainers() )
{
for ( EscherRecord escherRecord : dgContainer )
{
if ( escherRecord.getRecordId() == (short) 0xF003 )
{
spgrContainers.add( (EscherContainerRecord) escherRecord );
}
}
}
return spgrContainers;
}
public List<? extends EscherContainerRecord> getSpContainers()
{
List<EscherContainerRecord> spContainers = new ArrayList<>(
1);
for ( EscherContainerRecord spgrContainer : getSpgrContainers() )
{
for ( EscherRecord escherRecord : spgrContainer )
{
if ( escherRecord.getRecordId() == (short) 0xF004 )
{
spContainers.add( (EscherContainerRecord) escherRecord );
}
}
}
return spContainers;
}
@Override
public String toString() {
return "OfficeArtContent{" +
"drawingGroupData=" + drawingGroupData +
", mainDocumentDgContainer=" + mainDocumentDgContainer +
", headerDocumentDgContainer=" + headerDocumentDgContainer +
'}';
}
}
|