001/* 002 * Copyright 2008-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2008-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.matchingrules; 037 038 039 040import com.unboundid.asn1.ASN1OctetString; 041import com.unboundid.util.StaticUtils; 042import com.unboundid.util.ThreadSafety; 043import com.unboundid.util.ThreadSafetyLevel; 044 045 046 047/** 048 * This class provides an implementation of a matching rule that uses 049 * case-sensitive matching that also treats multiple consecutive (non-escaped) 050 * spaces as a single space. 051 */ 052@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 053public final class CaseExactStringMatchingRule 054 extends AcceptAllSimpleMatchingRule 055{ 056 /** 057 * The singleton instance that will be returned from the {@code getInstance} 058 * method. 059 */ 060 private static final CaseExactStringMatchingRule INSTANCE = 061 new CaseExactStringMatchingRule(); 062 063 064 065 /** 066 * The name for the caseExactMatch equality matching rule. 067 */ 068 public static final String EQUALITY_RULE_NAME = "caseExactMatch"; 069 070 071 072 /** 073 * The name for the caseExactMatch equality matching rule, formatted in all 074 * lowercase characters. 075 */ 076 static final String LOWER_EQUALITY_RULE_NAME = 077 StaticUtils.toLowerCase(EQUALITY_RULE_NAME); 078 079 080 081 /** 082 * The OID for the caseExactMatch equality matching rule. 083 */ 084 public static final String EQUALITY_RULE_OID = "2.5.13.5"; 085 086 087 088 /** 089 * The name for the caseExactOrderingMatch ordering matching rule. 090 */ 091 public static final String ORDERING_RULE_NAME = "caseExactOrderingMatch"; 092 093 094 095 /** 096 * The name for the caseExactOrderingMatch ordering matching rule, formatted 097 * in all lowercase characters. 098 */ 099 static final String LOWER_ORDERING_RULE_NAME = 100 StaticUtils.toLowerCase(ORDERING_RULE_NAME); 101 102 103 104 /** 105 * The OID for the caseExactOrderingMatch ordering matching rule. 106 */ 107 public static final String ORDERING_RULE_OID = "2.5.13.6"; 108 109 110 111 /** 112 * The name for the caseExactSubstringsMatch substring matching rule. 113 */ 114 public static final String SUBSTRING_RULE_NAME = "caseExactSubstringsMatch"; 115 116 117 118 /** 119 * The name for the caseExactSubstringsMatch substring matching rule, 120 * formatted in all lowercase characters. 121 */ 122 static final String LOWER_SUBSTRING_RULE_NAME = 123 StaticUtils.toLowerCase(SUBSTRING_RULE_NAME); 124 125 126 127 /** 128 * The OID for the caseExactSubstringsMatch substring matching rule. 129 */ 130 public static final String SUBSTRING_RULE_OID = "2.5.13.7"; 131 132 133 134 /** 135 * The serial version UID for this serializable class. 136 */ 137 private static final long serialVersionUID = -6336492464430413364L; 138 139 140 141 /** 142 * Creates a new instance of this case exact string matching rule. 143 */ 144 public CaseExactStringMatchingRule() 145 { 146 // No implementation is required. 147 } 148 149 150 151 /** 152 * Retrieves a singleton instance of this matching rule. 153 * 154 * @return A singleton instance of this matching rule. 155 */ 156 public static CaseExactStringMatchingRule getInstance() 157 { 158 return INSTANCE; 159 } 160 161 162 163 /** 164 * {@inheritDoc} 165 */ 166 @Override() 167 public String getEqualityMatchingRuleName() 168 { 169 return EQUALITY_RULE_NAME; 170 } 171 172 173 174 /** 175 * {@inheritDoc} 176 */ 177 @Override() 178 public String getEqualityMatchingRuleOID() 179 { 180 return EQUALITY_RULE_OID; 181 } 182 183 184 185 /** 186 * {@inheritDoc} 187 */ 188 @Override() 189 public String getOrderingMatchingRuleName() 190 { 191 return ORDERING_RULE_NAME; 192 } 193 194 195 196 /** 197 * {@inheritDoc} 198 */ 199 @Override() 200 public String getOrderingMatchingRuleOID() 201 { 202 return ORDERING_RULE_OID; 203 } 204 205 206 207 /** 208 * {@inheritDoc} 209 */ 210 @Override() 211 public String getSubstringMatchingRuleName() 212 { 213 return SUBSTRING_RULE_NAME; 214 } 215 216 217 218 /** 219 * {@inheritDoc} 220 */ 221 @Override() 222 public String getSubstringMatchingRuleOID() 223 { 224 return SUBSTRING_RULE_OID; 225 } 226 227 228 229 /** 230 * {@inheritDoc} 231 */ 232 @Override() 233 public boolean valuesMatch(final ASN1OctetString value1, 234 final ASN1OctetString value2) 235 { 236 // Try to use a quick, no-copy determination if possible. If this fails, 237 // then we'll fall back on a more thorough, but more costly, approach. 238 final byte[] value1Bytes = value1.getValue(); 239 final byte[] value2Bytes = value2.getValue(); 240 if (value1Bytes.length == value2Bytes.length) 241 { 242 for (int i=0; i< value1Bytes.length; i++) 243 { 244 final byte b1 = value1Bytes[i]; 245 final byte b2 = value2Bytes[i]; 246 247 if (((b1 & 0x7F) != (b1 & 0xFF)) || 248 ((b2 & 0x7F) != (b2 & 0xFF))) 249 { 250 return normalize(value1).equals(normalize(value2)); 251 } 252 else if (b1 != b2) 253 { 254 if ((b1 == ' ') || (b2 == ' ')) 255 { 256 return normalize(value1).equals(normalize(value2)); 257 } 258 else 259 { 260 return false; 261 } 262 } 263 } 264 265 // If we've gotten to this point, then the values must be equal. 266 return true; 267 } 268 else 269 { 270 return normalizeInternal(value1, false, (byte) 0x00).equals( 271 normalizeInternal(value2, false, (byte) 0x00)); 272 } 273 } 274 275 276 277 /** 278 * {@inheritDoc} 279 */ 280 @Override() 281 public ASN1OctetString normalize(final ASN1OctetString value) 282 { 283 return normalizeInternal(value, false, (byte) 0x00); 284 } 285 286 287 288 /** 289 * {@inheritDoc} 290 */ 291 @Override() 292 public ASN1OctetString normalizeSubstring(final ASN1OctetString value, 293 final byte substringType) 294 { 295 return normalizeInternal(value, true, substringType); 296 } 297 298 299 300 /** 301 * Normalizes the provided value for use in either an equality or substring 302 * matching operation. 303 * 304 * @param value The value to be normalized. 305 * @param isSubstring Indicates whether the value should be normalized as 306 * part of a substring assertion rather than an 307 * equality assertion. 308 * @param substringType The substring type for the element, if it is to be 309 * part of a substring assertion. 310 * 311 * @return The appropriately normalized form of the provided value. 312 */ 313 private static ASN1OctetString normalizeInternal(final ASN1OctetString value, 314 final boolean isSubstring, 315 final byte substringType) 316 { 317 final byte[] valueBytes = value.getValue(); 318 if (valueBytes.length == 0) 319 { 320 return value; 321 } 322 323 final boolean trimInitial; 324 final boolean trimFinal; 325 if (isSubstring) 326 { 327 switch (substringType) 328 { 329 case SUBSTRING_TYPE_SUBINITIAL: 330 trimInitial = true; 331 trimFinal = false; 332 break; 333 334 case SUBSTRING_TYPE_SUBFINAL: 335 trimInitial = false; 336 trimFinal = true; 337 break; 338 339 default: 340 trimInitial = false; 341 trimFinal = false; 342 break; 343 } 344 } 345 else 346 { 347 trimInitial = true; 348 trimFinal = true; 349 } 350 351 // Count the number of duplicate spaces in the value, and determine whether 352 // there are any non-space characters. Also, see if there are any non-ASCII 353 // characters. 354 boolean containsNonSpace = false; 355 boolean lastWasSpace = trimInitial; 356 int numDuplicates = 0; 357 for (final byte b : valueBytes) 358 { 359 if ((b & 0x7F) != (b & 0xFF)) 360 { 361 return normalizeNonASCII(value, trimInitial, trimFinal); 362 } 363 364 if (b == ' ') 365 { 366 if (lastWasSpace) 367 { 368 numDuplicates++; 369 } 370 else 371 { 372 lastWasSpace = true; 373 } 374 } 375 else 376 { 377 containsNonSpace = true; 378 lastWasSpace = false; 379 } 380 } 381 382 if (! containsNonSpace) 383 { 384 return new ASN1OctetString(" "); 385 } 386 387 if (lastWasSpace && trimFinal) 388 { 389 numDuplicates++; 390 } 391 392 393 // Create a new byte array to hold the normalized value. 394 lastWasSpace = trimInitial; 395 int targetPos = 0; 396 final byte[] normalizedBytes = new byte[valueBytes.length - numDuplicates]; 397 for (int i=0; i < valueBytes.length; i++) 398 { 399 if (valueBytes[i] == ' ') 400 { 401 if (lastWasSpace || (trimFinal && (i == (valueBytes.length - 1)))) 402 { 403 // No action is required. 404 } 405 else 406 { 407 // This condition is needed to handle the special case in which 408 // there are multiple spaces at the end of the value. 409 if (targetPos < normalizedBytes.length) 410 { 411 normalizedBytes[targetPos++] = ' '; 412 lastWasSpace = true; 413 } 414 } 415 } 416 else 417 { 418 normalizedBytes[targetPos++] = valueBytes[i]; 419 lastWasSpace = false; 420 } 421 } 422 423 424 return new ASN1OctetString(normalizedBytes); 425 } 426 427 428 429 /** 430 * Normalizes the provided value a string representation, properly handling 431 * any non-ASCII characters. 432 * 433 * @param value The value to be normalized. 434 * @param trimInitial Indicates whether to trim off all leading spaces at 435 * the beginning of the value. 436 * @param trimFinal Indicates whether to trim off all trailing spaces at 437 * the end of the value. 438 * 439 * @return The normalized form of the value. 440 */ 441 private static ASN1OctetString normalizeNonASCII(final ASN1OctetString value, 442 final boolean trimInitial, 443 final boolean trimFinal) 444 { 445 final StringBuilder buffer = new StringBuilder(value.stringValue()); 446 447 int pos = 0; 448 boolean lastWasSpace = trimInitial; 449 while (pos < buffer.length()) 450 { 451 final char c = buffer.charAt(pos++); 452 if (c == ' ') 453 { 454 if (lastWasSpace || (trimFinal && (pos >= buffer.length()))) 455 { 456 buffer.deleteCharAt(--pos); 457 } 458 else 459 { 460 lastWasSpace = true; 461 } 462 } 463 else 464 { 465 lastWasSpace = false; 466 } 467 } 468 469 // It is possible that there could be an extra space at the end. If that's 470 // the case, then remove it. 471 if (trimFinal && (buffer.length() > 0) && 472 (buffer.charAt(buffer.length() - 1) == ' ')) 473 { 474 buffer.deleteCharAt(buffer.length() - 1); 475 } 476 477 return new ASN1OctetString(buffer.toString()); 478 } 479}