View Javadoc
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.sentrysoftware.wbem.sblim.cimclient.internal.util;
23  
24  /*-
25   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
26   * WBEM Java Client
27   * ჻჻჻჻჻჻
28   * Copyright (C) 2023 Sentry Software
29   * ჻჻჻჻჻჻
30   * This program is free software: you can redistribute it and/or modify
31   * it under the terms of the GNU Lesser General Public License as
32   * published by the Free Software Foundation, either version 3 of the
33   * License, or (at your option) any later version.
34   *
35   * This program is distributed in the hope that it will be useful,
36   * but WITHOUT ANY WARRANTY; without even the implied warranty of
37   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38   * GNU General Lesser Public License for more details.
39   *
40   * You should have received a copy of the GNU General Lesser Public
41   * License along with this program.  If not, see
42   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
43   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
44   */
45  
46  import java.math.BigDecimal;
47  import java.util.StringTokenizer;
48  import java.util.Vector;
49  
50  /**
51   * Class Util is responsible for storing commonly used static methods.
52   */
53  public class Util {
54  
55  	/**
56  	 * Quotes the passed string.
57  	 * 
58  	 * @param pStr
59  	 * @return the quoted string
60  	 */
61  	public static String quote(String pStr) {
62  		StringBuffer dstBuf = new StringBuffer();
63  		dstBuf.append('"');
64  		for (int i = 0; i < pStr.length(); i++) {
65  			char ch = pStr.charAt(i);
66  			if (ch == '\\' || ch == '"') dstBuf.append('\\');
67  			dstBuf.append(ch);
68  		}
69  		dstBuf.append('"');
70  		return dstBuf.toString();
71  	}
72  
73  	/*
74  	 * Sun bug 4421494 identifies a range of <code>java.lang.Double</code>
75  	 * values that will hang the JVM due to an error in
76  	 * <code>FloatingDecimal.doubleValue()</code> that results in an infinite
77  	 * loop. The range is defined as (<code>lowBadDouble</code>,
78  	 * <code>hiBadDouble</code>).
79  	 */
80  	private static final BigDecimal lowBadDouble = new BigDecimal(
81  			"2.225073858507201136057409796709131975934E-308");
82  
83  	private static final BigDecimal hiBadDouble = new BigDecimal(
84  			"2.225073858507201259573821257020768020078E-308");
85  
86  	/**
87  	 * isBadDoubleString checks if passed string could hang JVM.
88  	 * 
89  	 * @param s
90  	 *            A string to be converted to a Double.
91  	 * @return <code>true</code> if double is in range of bad values,
92  	 *         <code>false</code> otherwise.
93  	 */
94  	public static boolean isBadDoubleString(String s) {
95  		BigDecimal val = new BigDecimal(s);
96  		BigDecimal min = val.min(lowBadDouble);
97  		BigDecimal max = val.max(hiBadDouble);
98  
99  		// Do not use string if min < value < max
100 		return (min.compareTo(val) < 0 && max.compareTo(val) > 0);
101 	}
102 
103 	/**
104 	 * Filters any elements listed in <code>pIgnoreElements</code> out of
105 	 * <code>pArray</code> and returns the updated array. For example, if
106 	 * <code>pArray = {"A", "B", "C", "D", "E", "F", "G"}</code> and
107 	 * <code>pIgnoreElements = "D,E,B"</code> then this method returns
108 	 * <code>{"A", "C", "F", "G"}</code>.
109 	 * 
110 	 * @param pArray
111 	 *            Original string array.
112 	 * @param pIgnoreElements
113 	 *            Comma-separated list of array elements to ignore.
114 	 * @return Updated string array.
115 	 */
116 	public static String[] getFilteredStringArray(String[] pArray, String pIgnoreElements) {
117 		int i, j;
118 
119 		if (pArray == null || pArray.length == 0 || pIgnoreElements == null
120 				|| pIgnoreElements.length() == 0) return pArray;
121 
122 		Vector<String> vecIgnore = new Vector<String>();
123 		StringTokenizer strtok = new StringTokenizer(pIgnoreElements, ",");
124 		while (strtok.hasMoreElements()) {
125 			String str = ((String) strtok.nextElement()).trim();
126 			if (str.length() > 0) vecIgnore.add(str);
127 		}
128 
129 		if (vecIgnore.size() == 0) return pArray;
130 
131 		Vector<String> vecNew = new Vector<String>();
132 		for (i = 0; i < pArray.length; i++) {
133 			for (j = 0; j < vecIgnore.size(); j++)
134 				if (pArray[i].equalsIgnoreCase(vecIgnore.elementAt(j))) break;
135 			if (j >= vecIgnore.size()) vecNew.add(pArray[i]);
136 		}
137 
138 		return vecNew.toArray(new String[0]);
139 	}
140 }