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
|
/* ====================================================================
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.sprm;
import java.util.Arrays;
import org.apache.poi.common.Duplicatable;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
@Internal
public final class SprmBuffer implements Duplicatable {
byte[] _buf;
boolean _istd;
int _offset;
private final int _sprmsStartOffset;
public SprmBuffer(SprmBuffer other) {
_buf = (other._buf == null) ? null : other._buf.clone();
_istd = other._istd;
_offset = other._offset;
_sprmsStartOffset = other._sprmsStartOffset;
}
public SprmBuffer(byte[] buf, boolean istd, int sprmsStartOffset) {
_offset = buf.length;
_buf = buf;
_istd = istd;
_sprmsStartOffset = sprmsStartOffset;
}
public SprmBuffer(byte[] buf, int _sprmsStartOffset) {
this(buf, false, _sprmsStartOffset);
}
public SprmBuffer(int sprmsStartOffset) {
_buf = IOUtils.safelyAllocate(sprmsStartOffset + 4L, SprmUtils.MAX_RECORD_LENGTH);
_offset = sprmsStartOffset;
_sprmsStartOffset = sprmsStartOffset;
}
public void addSprm(short opcode, byte operand) {
int addition = LittleEndianConsts.SHORT_SIZE + LittleEndianConsts.BYTE_SIZE;
ensureCapacity(addition);
LittleEndian.putShort(_buf, _offset, opcode);
_offset += LittleEndianConsts.SHORT_SIZE;
_buf[_offset++] = operand;
}
public void addSprm(short opcode, byte[] operand) {
int addition = LittleEndianConsts.SHORT_SIZE + LittleEndianConsts.BYTE_SIZE + operand.length;
ensureCapacity(addition);
LittleEndian.putShort(_buf, _offset, opcode);
_offset += LittleEndianConsts.SHORT_SIZE;
_buf[_offset++] = (byte) operand.length;
System.arraycopy(operand, 0, _buf, _offset, operand.length);
}
public void addSprm(short opcode, int operand) {
int addition = LittleEndianConsts.SHORT_SIZE + LittleEndianConsts.INT_SIZE;
ensureCapacity(addition);
LittleEndian.putShort(_buf, _offset, opcode);
_offset += LittleEndianConsts.SHORT_SIZE;
LittleEndian.putInt(_buf, _offset, operand);
_offset += LittleEndianConsts.INT_SIZE;
}
public void addSprm(short opcode, short operand) {
int addition = LittleEndianConsts.SHORT_SIZE + LittleEndianConsts.SHORT_SIZE;
ensureCapacity(addition);
LittleEndian.putShort(_buf, _offset, opcode);
_offset += LittleEndianConsts.SHORT_SIZE;
LittleEndian.putShort(_buf, _offset, operand);
_offset += LittleEndianConsts.SHORT_SIZE;
}
public void append(byte[] grpprl) {
append(grpprl, 0);
}
public void append(byte[] grpprl, int offset) {
ensureCapacity(grpprl.length - offset);
System.arraycopy(grpprl, offset, _buf, _offset, grpprl.length - offset);
_offset += grpprl.length - offset;
}
@Override
public SprmBuffer copy() {
return new SprmBuffer(this);
}
private void ensureCapacity(int addition) {
if (_offset + addition >= _buf.length) {
// add 6 more than they need for use the next iteration
//
// commented - buffer shall not contain any additional bytes --
// sergey
// byte[] newBuf = new byte[_offset + addition + 6];
IOUtils.safelyAllocateCheck(_offset + (long)addition, SprmUtils.MAX_RECORD_LENGTH);
_buf = Arrays.copyOf(_buf, _offset + addition);
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof SprmBuffer)) return false;
SprmBuffer sprmBuf = (SprmBuffer) obj;
return (Arrays.equals(_buf, sprmBuf._buf));
}
@Override
public int hashCode() {
assert false : "hashCode not designed";
return 42; // any arbitrary constant will do
}
public SprmOperation findSprm(short opcode) {
int operation = SprmOperation.getOperationFromOpcode(opcode);
int type = SprmOperation.getTypeFromOpcode(opcode);
SprmIterator si = new SprmIterator(_buf, 2);
while (si.hasNext()) {
SprmOperation i = si.next();
if (i.getOperation() == operation && i.getType() == type)
return i;
}
return null;
}
private int findSprmOffset(short opcode) {
SprmOperation sprmOperation = findSprm(opcode);
if (sprmOperation == null)
return -1;
return sprmOperation.getGrpprlOffset();
}
public byte[] toByteArray() {
return _buf;
}
public SprmIterator iterator() {
return new SprmIterator(_buf, _sprmsStartOffset);
}
public void updateSprm(short opcode, byte operand) {
int grpprlOffset = findSprmOffset(opcode);
if (grpprlOffset != -1) {
_buf[grpprlOffset] = operand;
return;
}
addSprm(opcode, operand);
}
public void updateSprm(short opcode, boolean operand) {
int grpprlOffset = findSprmOffset(opcode);
if (grpprlOffset != -1) {
_buf[grpprlOffset] = (byte) (operand ? 1 : 0);
return;
}
addSprm(opcode, operand ? 1 : 0);
}
public void updateSprm(short opcode, int operand) {
int grpprlOffset = findSprmOffset(opcode);
if (grpprlOffset != -1) {
LittleEndian.putInt(_buf, grpprlOffset, operand);
return;
}
addSprm(opcode, operand);
}
public void updateSprm(short opcode, short operand) {
int grpprlOffset = findSprmOffset(opcode);
if (grpprlOffset != -1) {
LittleEndian.putShort(_buf, grpprlOffset, operand);
return;
}
addSprm(opcode, operand);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Sprms (");
stringBuilder.append(_buf.length);
stringBuilder.append(" byte(s)): ");
for (SprmIterator iterator = iterator(); iterator.hasNext(); ) {
try {
stringBuilder.append(iterator.next());
} catch (Exception exc) {
stringBuilder.append("error");
}
stringBuilder.append("; ");
}
return stringBuilder.toString();
}
}
|