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 package gov.nist.secauto.cpe.naming;
53
54 import gov.nist.secauto.cpe.common.LogicalValue;
55 import gov.nist.secauto.cpe.common.Utilities;
56 import gov.nist.secauto.cpe.common.WellFormedName;
57
58 import java.text.ParseException;
59
60
61
62
63
64
65
66
67
68
69
70 public class CPENameUnbinder {
71
72 private CPENameUnbinder() {
73
74 }
75
76
77
78
79
80
81
82
83
84
85 public static WellFormedName unbindURI(String uri) throws ParseException {
86
87 Utilities.validateURI(uri);
88
89 WellFormedNameormedName.html#WellFormedName">WellFormedName result = new WellFormedName();
90
91 for (int i = 0; i != 8; i++) {
92
93 String value = getCompURI(uri, i);
94 if (i > 0) {
95
96 WellFormedName.Attribute attribute = WellFormedName.Attribute.values()[i - 1];
97
98 if (WellFormedName.Attribute.EDITION.equals(attribute)) {
99
100
101 if (value.equals("") || value.equals("-") || !Utilities.substr(value, 0, 1).equals("~")) {
102
103
104
105 result.set(attribute, decode(value));
106 } else {
107
108 unpack(value, result);
109 }
110 } else {
111 result.set(attribute, decode(value));
112 }
113 }
114 }
115 return result;
116 }
117
118
119
120
121
122
123
124
125
126
127 public static WellFormedName unbindFS(String fs) throws ParseException {
128
129 Utilities.validateFS(fs);
130
131 WellFormedNameormedName.html#WellFormedName">WellFormedName result = new WellFormedName();
132
133
134 for (int a = 2; a != 13; a++) {
135
136 Object value = getCompFS(fs, a);
137
138 value = unbindValueFS((String) value);
139
140
141 WellFormedName.Attribute attribute = WellFormedName.Attribute.values()[a - 2];
142
143
144 result.set(attribute, value);
145 }
146 return result;
147 }
148
149
150
151
152
153
154
155
156
157
158
159 private static String getCompFS(String fs, int index) {
160 if (index == 0) {
161
162
163 int colonIndex = Utilities.getUnescapedColonIndex(fs);
164
165
166 if (colonIndex == 0) {
167 return fs;
168 }
169 return Utilities.substr(fs, 0, colonIndex);
170 } else {
171 return getCompFS(Utilities.substr(fs, Utilities.getUnescapedColonIndex(fs) + 1, fs.length()), index - 1);
172 }
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186 private static Object unbindValueFS(String value) throws ParseException {
187 if (value.equals("*")) {
188 return LogicalValue.ANY;
189 }
190 if (value.equals("-")) {
191 return LogicalValue.NA;
192 }
193 return addQuoting(value);
194 }
195
196
197
198
199
200
201
202
203
204
205
206 private static String addQuoting(String str) throws ParseException {
207 String result = "";
208 int idx = 0;
209 boolean embedded = false;
210 while (idx < Utilities.strlen(str)) {
211 String ch = Utilities.substr(str, idx, idx + 1);
212 if (Utilities.isAlphanum(ch) || ch.equals("_")) {
213
214 result = Utilities.strcat(result, ch);
215 idx = idx + 1;
216 embedded = true;
217 continue;
218 }
219 if (ch.equals("\\")) {
220
221
222 result = Utilities.strcat(result, Utilities.substr(str, idx, idx + 2));
223 idx = idx + 2;
224 embedded = true;
225 continue;
226 }
227 if (ch.equals("*")) {
228
229
230 if (idx == 0 || idx == (Utilities.strlen(str) - 1)) {
231 result = Utilities.strcat(result, ch);
232 idx = idx + 1;
233 embedded = true;
234 continue;
235 } else {
236 throw new ParseException("Error! cannot have unquoted * embedded in formatted string.", 0);
237 }
238 }
239 if (ch.equals("?")) {
240
241
242
243
244 if (((idx == 0) || (idx == (Utilities.strlen(str) - 1)))
245 || (!embedded && (Utilities.substr(str, idx - 1, idx).equals("?")))
246 || (embedded && (Utilities.substr(str, idx + 1, idx + 2).equals("?")))) {
247 result = Utilities.strcat(result, ch);
248 idx = idx + 1;
249 embedded = false;
250 continue;
251 } else {
252 throw new ParseException("Error! cannot have unquoted ? embedded in formatted string.", 0);
253 }
254 }
255
256 result = Utilities.strcat(result, "\\", ch);
257 idx = idx + 1;
258 embedded = true;
259 }
260 return result;
261 }
262
263
264
265
266
267
268
269
270
271
272 private static String getCompURI(String uri, int index) {
273 if (index == 0) {
274 return Utilities.substr(uri, index, uri.indexOf("/"));
275 }
276 String[] sa = uri.split(":");
277
278
279 if (index >= sa.length) {
280 return "";
281 }
282 if (index == 1) {
283 return Utilities.substr(sa[index], 1, sa[index].length());
284 }
285 return sa[index];
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300 private static Object decode(String str) throws ParseException {
301 if (str.equals("")) {
302 return LogicalValue.ANY;
303 }
304 if (str.equals("-")) {
305 return LogicalValue.NA;
306 }
307
308
309 str = Utilities.toLowercase(str);
310 String result = "";
311 int idx = 0;
312 boolean embedded = false;
313 while (idx < Utilities.strlen(str)) {
314
315 String ch = Utilities.substr(str, idx, idx + 1);
316
317 if (ch.equals(".") || ch.equals("-") || ch.equals("~")) {
318 result = Utilities.strcat(result, "\\", ch);
319 idx = idx + 1;
320
321 embedded = true;
322 continue;
323 }
324 if (!ch.equals("%")) {
325 result = Utilities.strcat(result, ch);
326 idx = idx + 1;
327
328 embedded = true;
329 continue;
330 }
331
332 String form = Utilities.substr(str, idx, idx + 3);
333 if (form.equals("%01")) {
334 if ((idx == 0) || (idx == Utilities.strlen(str) - 3)
335 || (!embedded && Utilities.substr(str, idx - 3, idx - 1).equals("%01"))
336 || (embedded && (Utilities.strlen(str) >= idx + 6))
337 && (Utilities.substr(str, idx + 3, idx + 6).equals("%01"))) {
338 result = Utilities.strcat(result, "?");
339 idx = idx + 3;
340 continue;
341 } else {
342 throw new ParseException("Error decoding string", 0);
343 }
344 } else if (form.equals("%02")) {
345 if ((idx == 0) || (idx == (Utilities.strlen(str) - 3))) {
346 result = Utilities.strcat(result, "*");
347 } else {
348 throw new ParseException("Error decoding string", 0);
349 }
350 } else if (form.equals("%21")) {
351 result = Utilities.strcat(result, "\\!");
352 } else if (form.equals("%22")) {
353 result = Utilities.strcat(result, "\\\"");
354 } else if (form.equals("%23")) {
355 result = Utilities.strcat(result, "\\#");
356 } else if (form.equals("%24")) {
357 result = Utilities.strcat(result, "\\$");
358 } else if (form.equals("%25")) {
359 result = Utilities.strcat(result, "\\%");
360 } else if (form.equals("%26")) {
361 result = Utilities.strcat(result, "\\&");
362 } else if (form.equals("%27")) {
363 result = Utilities.strcat(result, "\\'");
364 } else if (form.equals("%28")) {
365 result = Utilities.strcat(result, "\\(");
366 } else if (form.equals("%29")) {
367 result = Utilities.strcat(result, "\\)");
368 } else if (form.equals("%2a")) {
369 result = Utilities.strcat(result, "\\*");
370 } else if (form.equals("%2b")) {
371 result = Utilities.strcat(result, "\\+");
372 } else if (form.equals("%2c")) {
373 result = Utilities.strcat(result, "\\,");
374 } else if (form.equals("%2f")) {
375 result = Utilities.strcat(result, "\\/");
376 } else if (form.equals("%3a")) {
377 result = Utilities.strcat(result, "\\)");
378 } else if (form.equals("%3b")) {
379 result = Utilities.strcat(result, "\\;");
380 } else if (form.equals("%3c")) {
381 result = Utilities.strcat(result, "\\<");
382 } else if (form.equals("%3d")) {
383 result = Utilities.strcat(result, "\\=");
384 } else if (form.equals("%3e")) {
385 result = Utilities.strcat(result, "\\>");
386 } else if (form.equals("%3f")) {
387 result = Utilities.strcat(result, "\\?");
388 } else if (form.equals("%40")) {
389 result = Utilities.strcat(result, "\\@");
390 } else if (form.equals("%5b")) {
391 result = Utilities.strcat(result, "\\[");
392 } else if (form.equals("%5c")) {
393 result = Utilities.strcat(result, "\\\\");
394 } else if (form.equals("%5d")) {
395 result = Utilities.strcat(result, "\\]");
396 } else if (form.equals("%5e")) {
397 result = Utilities.strcat(result, "\\^");
398 } else if (form.equals("%60")) {
399 result = Utilities.strcat(result, "\\`");
400 } else if (form.equals("%7b")) {
401 result = Utilities.strcat(result, "\\{");
402 } else if (form.equals("%7c")) {
403 result = Utilities.strcat(result, "\\|");
404 } else if (form.equals("%7d")) {
405 result = Utilities.strcat(result, "\\}");
406 } else if (form.equals("%7e")) {
407 result = Utilities.strcat(result, "\\~");
408 } else {
409 throw new ParseException("Unknown form: " + form, 0);
410 }
411 idx = idx + 3;
412 embedded = true;
413 }
414 return result;
415 }
416
417
418
419
420
421
422
423
424
425
426
427
428 private static WellFormedNamev/nist/secauto/cpe/common/WellFormedName.html#WellFormedName">WellFormedName unpack(String str, WellFormedName wfn) throws ParseException {
429
430 int start = 1;
431 int end = Utilities.strchr(str, '~', start);
432 String edition;
433 if (start == end) {
434 edition = "";
435 } else {
436 edition = Utilities.substr(str, start, end);
437 }
438
439 start = end + 1;
440 end = Utilities.strchr(str, '~', start);
441 String swEdition;
442 if (start == end) {
443 swEdition = "";
444 } else {
445 swEdition = Utilities.substr(str, start, end);
446 }
447
448 start = end + 1;
449 end = Utilities.strchr(str, '~', start);
450 String targetSoftware;
451 if (start == end) {
452 targetSoftware = "";
453 } else {
454 targetSoftware = Utilities.substr(str, start, end);
455 }
456
457 start = end + 1;
458 end = Utilities.strchr(str, '~', start);
459 String targetHardware;
460 if (start == end) {
461 targetHardware = "";
462 } else {
463 targetHardware = Utilities.substr(str, start, end);
464 }
465
466 start = end + 1;
467 String other;
468 if (start >= Utilities.strlen(str)) {
469 other = "";
470 } else {
471 other = Utilities.substr(str, start, Utilities.strlen(str) - 1);
472 }
473
474
475 wfn.set(WellFormedName.Attribute.EDITION, decode(edition));
476 wfn.set(WellFormedName.Attribute.SW_EDITION, decode(swEdition));
477 wfn.set(WellFormedName.Attribute.TARGET_SW, decode(targetSoftware));
478 wfn.set(WellFormedName.Attribute.TARGET_HW, decode(targetHardware));
479 wfn.set(WellFormedName.Attribute.OTHER, decode(other));
480 return wfn;
481 }
482 }