001/* 002 * Copyright 2007-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-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.asn1; 037 038 039 040import com.unboundid.util.Debug; 041import com.unboundid.util.NotMutable; 042import com.unboundid.util.ThreadSafety; 043import com.unboundid.util.ThreadSafetyLevel; 044 045import static com.unboundid.asn1.ASN1Messages.*; 046 047 048 049/** 050 * This class provides an ASN.1 Boolean element, whose value is a single byte 051 * and represents either "TRUE" or "FALSE". A value whose only byte is 0x00 is 052 * considered "false", while any other single-byte value is considered "true". 053 */ 054@NotMutable() 055@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 056public final class ASN1Boolean 057 extends ASN1Element 058{ 059 /** 060 * A pre-allocated ASN.1 Boolean element with the universal Boolean BER type 061 * and a value of "FALSE". 062 */ 063 public static final ASN1Boolean UNIVERSAL_BOOLEAN_FALSE_ELEMENT = 064 new ASN1Boolean(false); 065 066 067 068 /** 069 * A pre-allocated ASN.1 Boolean element with the universal Boolean BER type 070 * and a value of "TRUE". 071 */ 072 public static final ASN1Boolean UNIVERSAL_BOOLEAN_TRUE_ELEMENT = 073 new ASN1Boolean(true); 074 075 076 077 /** 078 * The serial version UID for this serializable class. 079 */ 080 private static final long serialVersionUID = 7131700816847855524L; 081 082 083 084 // The boolean value for this element. 085 private final boolean booleanValue; 086 087 088 089 /** 090 * Creates a new ASN.1 Boolean element with the default BER type and the 091 * provided boolean value. 092 * 093 * @param booleanValue The boolean value to use for this element. 094 */ 095 public ASN1Boolean(final boolean booleanValue) 096 { 097 super(ASN1Constants.UNIVERSAL_BOOLEAN_TYPE, 098 (booleanValue 099 ? ASN1Constants.BOOLEAN_VALUE_TRUE 100 : ASN1Constants.BOOLEAN_VALUE_FALSE)); 101 102 this.booleanValue = booleanValue; 103 } 104 105 106 107 /** 108 * Creates a new ASN.1 Boolean element with the specified BER type and the 109 * provided boolean value. 110 * 111 * @param type The BER type to use for this element. 112 * @param booleanValue The boolean value to use for this element. 113 */ 114 public ASN1Boolean(final byte type, final boolean booleanValue) 115 { 116 super(type, (booleanValue 117 ? ASN1Constants.BOOLEAN_VALUE_TRUE 118 : ASN1Constants.BOOLEAN_VALUE_FALSE)); 119 120 this.booleanValue = booleanValue; 121 } 122 123 124 125 /** 126 * Creates a new ASN.1 Boolean element with the provided information. 127 * 128 * @param type The BER type to use for this element. 129 * @param booleanValue The boolean value to use for this element. 130 * @param value The pre-encoded value to use for this element. 131 */ 132 private ASN1Boolean(final byte type, final boolean booleanValue, 133 final byte[] value) 134 { 135 super(type, value); 136 137 this.booleanValue = booleanValue; 138 } 139 140 141 142 /** 143 * Retrieves the boolean value for this element. 144 * 145 * @return {@code true} if this element has a value of "TRUE", or 146 * {@code false} if it has a value of "FALSE". 147 */ 148 public boolean booleanValue() 149 { 150 return booleanValue; 151 } 152 153 154 155 /** 156 * Decodes the contents of the provided byte array as a Boolean element. 157 * 158 * @param elementBytes The byte array to decode as an ASN.1 Boolean element. 159 * 160 * @return The decoded ASN.1 Boolean element. 161 * 162 * @throws ASN1Exception If the provided array cannot be decoded as a 163 * Boolean element. 164 */ 165 public static ASN1Boolean decodeAsBoolean(final byte[] elementBytes) 166 throws ASN1Exception 167 { 168 try 169 { 170 int valueStartPos = 2; 171 int length = (elementBytes[1] & 0x7F); 172 if (length != elementBytes[1]) 173 { 174 final int numLengthBytes = length; 175 176 length = 0; 177 for (int i=0; i < numLengthBytes; i++) 178 { 179 length <<= 8; 180 length |= (elementBytes[valueStartPos++] & 0xFF); 181 } 182 } 183 184 if ((elementBytes.length - valueStartPos) != length) 185 { 186 throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length, 187 (elementBytes.length - valueStartPos))); 188 } 189 190 if (length != 1) 191 { 192 throw new ASN1Exception(ERR_BOOLEAN_INVALID_LENGTH.get()); 193 } 194 195 final byte[] value = { elementBytes[valueStartPos] }; 196 final boolean booleanValue = (value[0] != 0x00); 197 return new ASN1Boolean(elementBytes[0], booleanValue, value); 198 } 199 catch (final ASN1Exception ae) 200 { 201 Debug.debugException(ae); 202 throw ae; 203 } 204 catch (final Exception e) 205 { 206 Debug.debugException(e); 207 throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e); 208 } 209 } 210 211 212 213 /** 214 * Decodes the provided ASN.1 element as a Boolean element. 215 * 216 * @param element The ASN.1 element to be decoded. 217 * 218 * @return The decoded ASN.1 Boolean element. 219 * 220 * @throws ASN1Exception If the provided element cannot be decoded as a 221 * Boolean element. 222 */ 223 public static ASN1Boolean decodeAsBoolean(final ASN1Element element) 224 throws ASN1Exception 225 { 226 final byte[] value = element.getValue(); 227 if (value.length != 1) 228 { 229 throw new ASN1Exception(ERR_BOOLEAN_INVALID_LENGTH.get()); 230 } 231 232 if (value[0] == 0x00) 233 { 234 return new ASN1Boolean(element.getType(), false, value); 235 } 236 else 237 { 238 return new ASN1Boolean(element.getType(), true, value); 239 } 240 } 241 242 243 244 /** 245 * {@inheritDoc} 246 */ 247 @Override() 248 public void toString(final StringBuilder buffer) 249 { 250 buffer.append(booleanValue); 251 } 252}