1 /*
2 (C) Copyright IBM Corp. 2006, 2013
3
4 THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5 ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6 CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7
8 You can obtain a current copy of the Eclipse Public License from
9 http://www.opensource.org/licenses/eclipse-1.0.php
10
11 @author : Endre Bak, ebak@de.ibm.com
12 *
13 * Flag Date Prog Description
14 * -------------------------------------------------------------------------------
15 * 1565892 2006-10-18 ebak Make SBLIM client JSR48 compliant
16 * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
17 * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
18 * 3572993 2012-10-01 blaschke-oss parseDouble("2.2250738585072012e-308") DoS vulnerability
19 * 2618 2013-02-27 blaschke-oss Need to add property to disable weak cipher suites for the secure indication
20 */
21
22 package org.metricshub.wbem.sblim.cimclient.internal.util;
23
24 /*-
25 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
26 * WBEM Java Client
27 * ჻჻჻჻჻჻
28 * Copyright 2023 - 2025 MetricsHub
29 * ჻჻჻჻჻჻
30 * Licensed under the Apache License, Version 2.0 (the "License");
31 * you may not use this file except in compliance with the License.
32 * You may obtain a copy of the License at
33 *
34 * http://www.apache.org/licenses/LICENSE-2.0
35 *
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an "AS IS" BASIS,
38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 * See the License for the specific language governing permissions and
40 * limitations under the License.
41 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
42 */
43
44 import java.math.BigDecimal;
45 import java.util.StringTokenizer;
46 import java.util.Vector;
47
48 /**
49 * Class Util is responsible for storing commonly used static methods.
50 */
51 public class Util {
52
53 /**
54 * Quotes the passed string.
55 *
56 * @param pStr
57 * @return the quoted string
58 */
59 public static String quote(String pStr) {
60 StringBuffer dstBuf = new StringBuffer();
61 dstBuf.append('"');
62 for (int i = 0; i < pStr.length(); i++) {
63 char ch = pStr.charAt(i);
64 if (ch == '\\' || ch == '"') dstBuf.append('\\');
65 dstBuf.append(ch);
66 }
67 dstBuf.append('"');
68 return dstBuf.toString();
69 }
70
71 /*
72 * Sun bug 4421494 identifies a range of <code>java.lang.Double</code>
73 * values that will hang the JVM due to an error in
74 * <code>FloatingDecimal.doubleValue()</code> that results in an infinite
75 * loop. The range is defined as (<code>lowBadDouble</code>,
76 * <code>hiBadDouble</code>).
77 */
78 private static final BigDecimal lowBadDouble = new BigDecimal("2.225073858507201136057409796709131975934E-308");
79
80 private static final BigDecimal hiBadDouble = new BigDecimal("2.225073858507201259573821257020768020078E-308");
81
82 /**
83 * isBadDoubleString checks if passed string could hang JVM.
84 *
85 * @param s
86 * A string to be converted to a Double.
87 * @return <code>true</code> if double is in range of bad values,
88 * <code>false</code> otherwise.
89 */
90 public static boolean isBadDoubleString(String s) {
91 BigDecimal val = new BigDecimal(s);
92 BigDecimal min = val.min(lowBadDouble);
93 BigDecimal max = val.max(hiBadDouble);
94
95 // Do not use string if min < value < max
96 return (min.compareTo(val) < 0 && max.compareTo(val) > 0);
97 }
98
99 /**
100 * Filters any elements listed in <code>pIgnoreElements</code> out of
101 * <code>pArray</code> and returns the updated array. For example, if
102 * <code>pArray = {"A", "B", "C", "D", "E", "F", "G"}</code> and
103 * <code>pIgnoreElements = "D,E,B"</code> then this method returns
104 * <code>{"A", "C", "F", "G"}</code>.
105 *
106 * @param pArray
107 * Original string array.
108 * @param pIgnoreElements
109 * Comma-separated list of array elements to ignore.
110 * @return Updated string array.
111 */
112 public static String[] getFilteredStringArray(String[] pArray, String pIgnoreElements) {
113 int i, j;
114
115 if (pArray == null || pArray.length == 0 || pIgnoreElements == null || pIgnoreElements.length() == 0) return pArray;
116
117 Vector<String> vecIgnore = new Vector<String>();
118 StringTokenizer strtok = new StringTokenizer(pIgnoreElements, ",");
119 while (strtok.hasMoreElements()) {
120 String str = ((String) strtok.nextElement()).trim();
121 if (str.length() > 0) vecIgnore.add(str);
122 }
123
124 if (vecIgnore.size() == 0) return pArray;
125
126 Vector<String> vecNew = new Vector<String>();
127 for (i = 0; i < pArray.length; i++) {
128 for (j = 0; j < vecIgnore.size(); j++) if (pArray[i].equalsIgnoreCase(vecIgnore.elementAt(j))) break;
129 if (j >= vecIgnore.size()) vecNew.add(pArray[i]);
130 }
131
132 return vecNew.toArray(new String[0]);
133 }
134 }