1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.internal.encoder; |
21 |
|
|
22 |
|
import java.io.ByteArrayOutputStream; |
23 |
|
import java.io.FilterInputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
@version |
31 |
|
@since |
32 |
|
|
|
|
| 77.1% |
Uncovered Elements: 16 (70) |
Complexity: 15 |
Complexity Density: 0.33 |
|
33 |
|
class BcBinaryStringEncoderInputStream extends FilterInputStream |
34 |
|
{ |
35 |
|
|
36 |
|
private final int blockSize; |
37 |
|
|
38 |
|
|
39 |
|
private final int charSize; |
40 |
|
|
41 |
|
|
42 |
|
private byte[] oneByte = new byte[1]; |
43 |
|
|
44 |
|
|
45 |
|
private byte[] ofBuf; |
46 |
|
|
47 |
|
|
48 |
|
private int ofOff; |
49 |
|
|
50 |
|
|
51 |
|
private int ofLen; |
52 |
|
|
53 |
|
|
54 |
|
private final InternalBinaryStringEncoder encoder; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
|
|
| 87.9% |
Uncovered Elements: 13 (107) |
Complexity: 30 |
Complexity Density: 0.45 |
|
59 |
|
class InputBuffer |
60 |
|
{ |
61 |
|
|
62 |
|
private byte[] buf; |
63 |
|
|
64 |
|
|
65 |
|
private int bufLen; |
66 |
|
|
67 |
|
|
68 |
|
private int bcount; |
69 |
|
|
70 |
|
|
71 |
|
private int rblank; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@throws |
78 |
|
|
|
|
| 73.9% |
Uncovered Elements: 6 (23) |
Complexity: 7 |
Complexity Density: 0.47 |
|
79 |
132 |
InputBuffer(int blen) throws IOException... |
80 |
|
{ |
81 |
132 |
int rlen = 0; |
82 |
132 |
int rbl = blen * BcBinaryStringEncoderInputStream.this.charSize; |
83 |
132 |
this.buf = new byte[rbl]; |
84 |
|
|
85 |
|
|
86 |
286 |
while (rbl > 0 && rlen >= 0) { |
87 |
154 |
rlen = read(rbl); |
88 |
154 |
rbl = this.rblank; |
89 |
|
} |
90 |
|
|
91 |
132 |
rlen = this.bufLen - this.bcount; |
92 |
|
|
93 |
|
|
94 |
132 |
if (rlen > 0) { |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
120 |
int rblen = (rlen + BcBinaryStringEncoderInputStream.this.charSize - 1) |
99 |
|
/ BcBinaryStringEncoderInputStream.this.charSize; |
100 |
120 |
int runder = (rblen * BcBinaryStringEncoderInputStream.this.charSize) - rlen; |
101 |
120 |
int rrlen = 0; |
102 |
120 |
while (runder > 0 && rrlen >= 0) { |
103 |
0 |
rrlen = read(runder); |
104 |
0 |
if (rrlen > 0) { |
105 |
0 |
runder -= rrlen - this.rblank; |
106 |
|
} |
107 |
|
} |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
@return |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
120 |
byte[] getBuffer()... |
115 |
|
{ |
116 |
120 |
return this.buf; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
@return |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
120 |
int getReadLength()... |
123 |
|
{ |
124 |
120 |
return this.bufLen; |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
@return |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
132 |
int getEffectiveLength()... |
131 |
|
{ |
132 |
132 |
return this.bufLen - this.bcount; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
@param |
140 |
|
@return |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
142 |
140 |
private int countBlank(int off, int len)... |
143 |
|
{ |
144 |
140 |
int blank = 0; |
145 |
3150 |
for (int i = off; i < len; i++) { |
146 |
3010 |
if (isBlank(this.buf[i])) { |
147 |
22 |
blank++; |
148 |
|
} |
149 |
|
} |
150 |
140 |
return blank; |
151 |
|
} |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
153 |
6040 |
private boolean isBlank(byte b)... |
154 |
|
{ |
155 |
6040 |
return (b == '\n' || b == '\r' || b == '\t' || b == ' '); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
@param |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
163 |
154 |
private void ensureSize(int len)... |
164 |
|
{ |
165 |
154 |
if (len > this.buf.length) { |
166 |
20 |
byte[] nbuf = new byte[len]; |
167 |
20 |
System.arraycopy(this.buf, 0, nbuf, 0, this.buf.length); |
168 |
20 |
this.buf = nbuf; |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@return |
177 |
|
@throws |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
179 |
154 |
private int read(int len) throws IOException... |
180 |
|
{ |
181 |
154 |
ensureSize(this.bufLen + len); |
182 |
154 |
return readBase64(len); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
@param |
191 |
|
@return |
192 |
|
@throws |
193 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
194 |
154 |
private int readBase64(int len) throws IOException... |
195 |
|
{ |
196 |
154 |
int rlen; |
197 |
154 |
if (BcBinaryStringEncoderInputStream.this.in.markSupported()) { |
198 |
77 |
rlen = readBase64WithMark(len); |
199 |
|
} else { |
200 |
77 |
rlen = readBase64WithoutMark(len); |
201 |
|
} |
202 |
154 |
if (rlen > 0) { |
203 |
140 |
this.rblank = countBlank(this.bufLen, rlen); |
204 |
140 |
this.bufLen += rlen; |
205 |
140 |
this.bcount += this.rblank; |
206 |
140 |
return rlen; |
207 |
|
} |
208 |
14 |
return -1; |
209 |
|
} |
210 |
|
|
|
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
211 |
77 |
private int readBase64WithoutMark(int len) throws IOException... |
212 |
|
{ |
213 |
77 |
int rlen; |
214 |
77 |
rlen = this.bufLen; |
215 |
1592 |
while (rlen < (this.bufLen + len)) { |
216 |
1528 |
int c = BcBinaryStringEncoderInputStream.this.in.read(); |
217 |
1528 |
if (c < 0) { |
218 |
13 |
break; |
219 |
|
} |
220 |
1515 |
byte b = (byte) c; |
221 |
1515 |
if (!isBlank(b) && !BcBinaryStringEncoderInputStream.this.encoder.isValidEncoding(b)) { |
222 |
0 |
break; |
223 |
|
} |
224 |
1515 |
this.buf[rlen++] = b; |
225 |
|
} |
226 |
77 |
rlen -= this.bufLen; |
227 |
77 |
return rlen; |
228 |
|
} |
229 |
|
|
|
|
| 75% |
Uncovered Elements: 5 (20) |
Complexity: 5 |
Complexity Density: 0.36 |
|
230 |
77 |
private int readBase64WithMark(int len) throws IOException... |
231 |
|
{ |
232 |
77 |
int rlen; |
233 |
77 |
BcBinaryStringEncoderInputStream.this.in.mark(len); |
234 |
77 |
rlen = BcBinaryStringEncoderInputStream.this.in.read(this.buf, this.bufLen, len); |
235 |
77 |
int i = this.bufLen; |
236 |
1592 |
while (i < (this.bufLen + rlen)) { |
237 |
1515 |
byte b = this.buf[i]; |
238 |
1515 |
if (!isBlank(b) && !BcBinaryStringEncoderInputStream.this.encoder.isValidEncoding(b)) { |
239 |
0 |
break; |
240 |
|
} |
241 |
1515 |
i++; |
242 |
|
} |
243 |
77 |
i -= this.bufLen; |
244 |
77 |
if (i < rlen) { |
245 |
0 |
BcBinaryStringEncoderInputStream.this.in.reset(); |
246 |
0 |
rlen = (int) BcBinaryStringEncoderInputStream.this.in.skip(i); |
247 |
|
} |
248 |
77 |
return rlen; |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
@param |
256 |
|
@param |
257 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
258 |
12 |
BcBinaryStringEncoderInputStream(InputStream inputStream, InternalBinaryStringEncoder encoder)... |
259 |
|
{ |
260 |
12 |
super(inputStream); |
261 |
12 |
this.encoder = encoder; |
262 |
12 |
this.blockSize = encoder.getEncodingBlockSize(); |
263 |
12 |
this.charSize = encoder.getDecodingBlockSize(); |
264 |
12 |
this.ofBuf = new byte[this.blockSize]; |
265 |
|
} |
266 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
267 |
0 |
@Override... |
268 |
|
public int read() throws IOException |
269 |
|
{ |
270 |
0 |
if (read(this.oneByte, 0, 1) > 0) { |
271 |
0 |
return this.oneByte[0]; |
272 |
|
} |
273 |
0 |
return -1; |
274 |
|
} |
275 |
|
|
|
|
| 88.6% |
Uncovered Elements: 5 (44) |
Complexity: 8 |
Complexity Density: 0.27 |
|
276 |
132 |
@Override... |
277 |
|
public int read(byte[] out, int offset, int length) throws IOException |
278 |
|
{ |
279 |
132 |
if ((offset | length | (out.length - (length + offset)) | (offset + length)) < 0) { |
280 |
0 |
throw new IndexOutOfBoundsException(); |
281 |
|
} |
282 |
|
|
283 |
132 |
if (length == 0) { |
284 |
0 |
return 0; |
285 |
|
} |
286 |
|
|
287 |
132 |
int readLen = 0; |
288 |
|
|
289 |
132 |
int off = offset; |
290 |
132 |
int len = length; |
291 |
|
|
292 |
|
|
293 |
132 |
if (this.ofLen > 0) { |
294 |
48 |
int clen = copyData(this.ofBuf, this.ofOff, this.ofLen, out, off, len); |
295 |
48 |
this.ofLen -= clen; |
296 |
48 |
this.ofOff += clen; |
297 |
48 |
off += clen; |
298 |
48 |
len -= clen; |
299 |
48 |
readLen += clen; |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
132 |
if (len > 0) { |
304 |
|
|
305 |
132 |
int blen = (len + this.blockSize - 1) / this.blockSize; |
306 |
132 |
InputBuffer inBuf = new InputBuffer(blen); |
307 |
132 |
int rlen = inBuf.getEffectiveLength(); |
308 |
132 |
if (rlen > 0) { |
309 |
120 |
int rblen = (rlen + this.charSize - 1) / this.charSize; |
310 |
|
|
311 |
|
|
312 |
120 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(rblen * this.blockSize); |
313 |
120 |
this.encoder.decode(inBuf.getBuffer(), 0, inBuf.getReadLength(), baos); |
314 |
120 |
baos.close(); |
315 |
|
|
316 |
120 |
int clen = copyData(baos.toByteArray(), 0, baos.size(), out, off, len); |
317 |
120 |
readLen += clen; |
318 |
120 |
this.ofLen = baos.size() - clen; |
319 |
120 |
this.ofOff = 0; |
320 |
|
|
321 |
120 |
if (this.ofLen > 0) { |
322 |
48 |
System.arraycopy(baos.toByteArray(), clen, this.ofBuf, this.ofOff, this.ofLen); |
323 |
|
} |
324 |
|
} |
325 |
|
} |
326 |
|
|
327 |
132 |
return (readLen > 0) ? readLen : -1; |
328 |
|
} |
329 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
330 |
0 |
@Override... |
331 |
|
public int available() throws IOException |
332 |
|
{ |
333 |
0 |
int len = super.available(); |
334 |
0 |
return ((len + this.charSize - 1) / this.charSize) * this.blockSize; |
335 |
|
} |
336 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
337 |
0 |
@Override... |
338 |
|
public boolean markSupported() |
339 |
|
{ |
340 |
0 |
return false; |
341 |
|
} |
342 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
343 |
168 |
private int copyData(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff, int outLen)... |
344 |
|
{ |
345 |
168 |
int clen = inLen; |
346 |
168 |
if (clen > outLen) { |
347 |
48 |
clen = outLen; |
348 |
|
} |
349 |
168 |
System.arraycopy(inBuf, inOff, outBuf, outOff, clen); |
350 |
168 |
return clen; |
351 |
|
} |
352 |
|
} |