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