1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.utils;
21
22 import static java.nio.charset.StandardCharsets.US_ASCII;
23
24 import java.util.Arrays;
25
26 import org.apache.commons.compress.archivers.ArchiveEntry;
27
28
29
30
31 public class ArchiveUtils {
32
33 private static final int MAX_SANITIZED_NAME_LENGTH = 255;
34
35
36
37
38
39
40
41
42 public static boolean isArrayZero(final byte[] a, final int size) {
43 for (int i = 0; i < size; i++) {
44 if (a[i] != 0) {
45 return false;
46 }
47 }
48 return true;
49 }
50
51
52
53
54
55
56
57
58
59 @Deprecated
60 public static boolean isEqual(final byte[] buffer1, final byte[] buffer2) {
61 return Arrays.equals(buffer1, buffer2);
62 }
63
64
65
66
67
68
69
70
71
72 public static boolean isEqual(final byte[] buffer1, final byte[] buffer2, final boolean ignoreTrailingNulls) {
73 return isEqual(buffer1, 0, buffer1.length, buffer2, 0, buffer2.length, ignoreTrailingNulls);
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87 public static boolean isEqual(final byte[] buffer1, final int offset1, final int length1, final byte[] buffer2, final int offset2, final int length2) {
88 return isEqual(buffer1, offset1, length1, buffer2, offset2, length2, false);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public static boolean isEqual(final byte[] buffer1, final int offset1, final int length1, final byte[] buffer2, final int offset2, final int length2,
104 final boolean ignoreTrailingNulls) {
105 final int minLen = Math.min(length1, length2);
106 for (int i = 0; i < minLen; i++) {
107 if (buffer1[offset1 + i] != buffer2[offset2 + i]) {
108 return false;
109 }
110 }
111 if (length1 == length2) {
112 return true;
113 }
114 if (ignoreTrailingNulls) {
115 if (length1 > length2) {
116 for (int i = length2; i < length1; i++) {
117 if (buffer1[offset1 + i] != 0) {
118 return false;
119 }
120 }
121 } else {
122 for (int i = length1; i < length2; i++) {
123 if (buffer2[offset2 + i] != 0) {
124 return false;
125 }
126 }
127 }
128 return true;
129 }
130 return false;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 public static boolean isEqualWithNull(final byte[] buffer1, final int offset1, final int length1, final byte[] buffer2, final int offset2,
145 final int length2) {
146 return isEqual(buffer1, offset1, length1, buffer2, offset2, length2, true);
147 }
148
149
150
151
152
153
154
155
156 public static boolean matchAsciiBuffer(final String expected, final byte[] buffer) {
157 return matchAsciiBuffer(expected, buffer, 0, buffer.length);
158 }
159
160
161
162
163
164
165
166
167
168
169 public static boolean matchAsciiBuffer(final String expected, final byte[] buffer, final int offset, final int length) {
170 final byte[] buffer1 = expected.getBytes(US_ASCII);
171 return isEqual(buffer1, 0, buffer1.length, buffer, offset, length, false);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public static String sanitize(final String s) {
188 final char[] cs = s.toCharArray();
189 final char[] chars = cs.length <= MAX_SANITIZED_NAME_LENGTH ? cs : Arrays.copyOf(cs, MAX_SANITIZED_NAME_LENGTH);
190 if (cs.length > MAX_SANITIZED_NAME_LENGTH) {
191 Arrays.fill(chars, MAX_SANITIZED_NAME_LENGTH - 3, MAX_SANITIZED_NAME_LENGTH, '.');
192 }
193 final StringBuilder sb = new StringBuilder();
194 for (final char c : chars) {
195 if (!Character.isISOControl(c)) {
196 final Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
197 if (block != null && block != Character.UnicodeBlock.SPECIALS) {
198 sb.append(c);
199 continue;
200 }
201 }
202 sb.append('?');
203 }
204 return sb.toString();
205 }
206
207
208
209
210
211
212
213 public static byte[] toAsciiBytes(final String inputString) {
214 return inputString.getBytes(US_ASCII);
215 }
216
217
218
219
220
221
222
223 public static String toAsciiString(final byte[] inputBytes) {
224 return new String(inputBytes, US_ASCII);
225 }
226
227
228
229
230
231
232
233
234
235 public static String toAsciiString(final byte[] inputBytes, final int offset, final int length) {
236 return new String(inputBytes, offset, length, US_ASCII);
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public static String toString(final ArchiveEntry entry) {
253 final StringBuilder sb = new StringBuilder();
254 sb.append(entry.isDirectory() ? 'd' : '-');
255 final String size = Long.toString(entry.getSize());
256 sb.append(' ');
257
258 for (int i = 7; i > size.length(); i--) {
259 sb.append(' ');
260 }
261 sb.append(size);
262 sb.append(' ').append(entry.getName());
263 return sb.toString();
264 }
265
266
267 private ArchiveUtils() {
268 }
269
270 }