1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.xml.pom;
19
20 import java.io.BufferedInputStream;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25
26
27
28
29
30
31
32 public class PomProjectInputStream extends FilterInputStream {
33
34
35
36
37 private static final byte[] PROJECT = {60, 112, 114, 111, 106, 101, 99, 116};
38
39
40
41
42
43 protected static final int BUFFER_SIZE = 1024;
44
45
46
47
48
49
50
51
52 public PomProjectInputStream(InputStream in) throws IOException {
53 super(new BufferedInputStream(in));
54 skipToProject();
55 }
56
57
58
59
60
61
62
63 private void skipToProject() throws IOException {
64 final byte[] buffer = new byte[BUFFER_SIZE];
65 super.mark(BUFFER_SIZE);
66 int count = super.read(buffer, 0, BUFFER_SIZE);
67 while (count > 0) {
68 final int pos = findSequence(PROJECT, buffer);
69 if (pos >= 0) {
70 super.reset();
71 final long skipped = super.skip((long) pos);
72 if (skipped != pos) {
73 throw new IOException("Error skipping pom header information");
74 }
75 return;
76 } else if (count - PROJECT.length == 0) {
77 return;
78 }
79 super.reset();
80 final long skipTo = (long) count - PROJECT.length;
81 final long skipped = super.skip(skipTo);
82 if (skipped != skipTo) {
83 throw new IOException("Error skipping pom header information");
84 }
85 super.mark(BUFFER_SIZE);
86 count = super.read(buffer, 0, BUFFER_SIZE);
87 }
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101 private static boolean testRemaining(byte[] sequence, byte[] buffer, int pos) {
102 boolean match = true;
103 for (int i = 1; i < sequence.length; i++) {
104 if (buffer[pos + i] != sequence[i]) {
105 match = false;
106 break;
107 }
108 }
109 return match;
110 }
111
112
113
114
115
116
117
118
119
120
121 protected static int findSequence(byte[] sequence, byte[] buffer) {
122 int pos = -1;
123 for (int i = 0; i < buffer.length - sequence.length + 1; i++) {
124 if (buffer[i] == sequence[0] && testRemaining(sequence, buffer, i)) {
125 pos = i;
126 break;
127 }
128 }
129 return pos;
130 }
131 }