001/* 002 * Copyright 2007-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-2024 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) 2007-2024 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.NotNull; 043import com.unboundid.util.ThreadSafety; 044import com.unboundid.util.ThreadSafetyLevel; 045 046import static com.unboundid.asn1.ASN1Messages.*; 047 048 049 050/** 051 * This class provides an ASN.1 enumerated element. Enumerated elements are 052 * very similar to integer elements, and the only real difference between them 053 * is that the individual values of an enumerated element have a symbolic 054 * significance (i.e., each value is associated with a particular meaning), 055 * although this does not impact its encoding other than through the use of a 056 * different default BER type. 057 */ 058@NotMutable() 059@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 060public final class ASN1Enumerated 061 extends ASN1Element 062{ 063 /** 064 * The serial version UID for this serializable class. 065 */ 066 private static final long serialVersionUID = -5915912036130847725L; 067 068 069 070 // The int value for this element. 071 private final int intValue; 072 073 074 075 /** 076 * Creates a new ASN.1 enumerated element with the default BER type and the 077 * provided int value. 078 * 079 * @param intValue The int value to use for this element. 080 */ 081 public ASN1Enumerated(final int intValue) 082 { 083 super(ASN1Constants.UNIVERSAL_ENUMERATED_TYPE, 084 ASN1Integer.encodeIntValue(intValue)); 085 086 this.intValue = intValue; 087 } 088 089 090 091 /** 092 * Creates a new ASN.1 enumerated element with the specified BER type and the 093 * provided int value. 094 * 095 * @param type The BER type to use for this element. 096 * @param intValue The int value to use for this element. 097 */ 098 public ASN1Enumerated(final byte type, final int intValue) 099 { 100 super(type, ASN1Integer.encodeIntValue(intValue)); 101 102 this.intValue = intValue; 103 } 104 105 106 107 /** 108 * Creates a new ASN.1 enumerated element with the specified BER type and the 109 * provided int and pre-encoded values. 110 * 111 * @param type The BER type to use for this element. 112 * @param intValue The int value to use for this element. 113 * @param value The pre-encoded value to use for this element. 114 */ 115 private ASN1Enumerated(final byte type, final int intValue, 116 @NotNull final byte[] value) 117 { 118 super(type, value); 119 120 this.intValue = intValue; 121 } 122 123 124 125 /** 126 * Retrieves the int value for this element. 127 * 128 * @return The int value for this element. 129 */ 130 public int intValue() 131 { 132 return intValue; 133 } 134 135 136 137 /** 138 * Decodes the contents of the provided byte array as an enumerated element. 139 * 140 * @param elementBytes The byte array to decode as an ASN.1 enumerated 141 * element. 142 * 143 * @return The decoded ASN.1 enumerated element. 144 * 145 * @throws ASN1Exception If the provided array cannot be decoded as an 146 * enumerated element. 147 */ 148 @NotNull() 149 public static ASN1Enumerated decodeAsEnumerated( 150 @NotNull final byte[] elementBytes) 151 throws ASN1Exception 152 { 153 try 154 { 155 int valueStartPos = 2; 156 int length = (elementBytes[1] & 0x7F); 157 if (length != elementBytes[1]) 158 { 159 final int numLengthBytes = length; 160 161 length = 0; 162 for (int i=0; i < numLengthBytes; i++) 163 { 164 length <<= 8; 165 length |= (elementBytes[valueStartPos++] & 0xFF); 166 } 167 } 168 169 if ((elementBytes.length - valueStartPos) != length) 170 { 171 throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length, 172 (elementBytes.length - valueStartPos))); 173 } 174 175 final byte[] value = new byte[length]; 176 System.arraycopy(elementBytes, valueStartPos, value, 0, length); 177 178 int intValue; 179 switch (value.length) 180 { 181 case 1: 182 intValue = (value[0] & 0xFF); 183 if ((value[0] & 0x80) != 0x00) 184 { 185 intValue |= 0xFFFF_FF00; 186 } 187 break; 188 189 case 2: 190 intValue = ((value[0] & 0xFF) << 8) | (value[1] & 0xFF); 191 if ((value[0] & 0x80) != 0x00) 192 { 193 intValue |= 0xFFFF_0000; 194 } 195 break; 196 197 case 3: 198 intValue = ((value[0] & 0xFF) << 16) | ((value[1] & 0xFF) << 8) | 199 (value[2] & 0xFF); 200 if ((value[0] & 0x80) != 0x00) 201 { 202 intValue |= 0xFF00_0000; 203 } 204 break; 205 206 case 4: 207 intValue = ((value[0] & 0xFF) << 24) | ((value[1] & 0xFF) << 16) | 208 ((value[2] & 0xFF) << 8) | (value[3] & 0xFF); 209 break; 210 211 default: 212 throw new ASN1Exception(ERR_ENUMERATED_INVALID_LENGTH.get( 213 value.length)); 214 } 215 216 return new ASN1Enumerated(elementBytes[0], intValue, value); 217 } 218 catch (final ASN1Exception ae) 219 { 220 Debug.debugException(ae); 221 throw ae; 222 } 223 catch (final Exception e) 224 { 225 Debug.debugException(e); 226 throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e); 227 } 228 } 229 230 231 232 /** 233 * Decodes the provided ASN.1 element as an enumerated element. 234 * 235 * @param element The ASN.1 element to be decoded. 236 * 237 * @return The decoded ASN.1 enumerated element. 238 * 239 * @throws ASN1Exception If the provided element cannot be decoded as an 240 * enumerated element. 241 */ 242 @NotNull() 243 public static ASN1Enumerated decodeAsEnumerated( 244 @NotNull final ASN1Element element) 245 throws ASN1Exception 246 { 247 int intValue; 248 final byte[] value = element.getValue(); 249 switch (value.length) 250 { 251 case 1: 252 intValue = (value[0] & 0xFF); 253 if ((value[0] & 0x80) != 0x00) 254 { 255 intValue |= 0xFFFF_FF00; 256 } 257 break; 258 259 case 2: 260 intValue = ((value[0] & 0xFF) << 8) | (value[1] & 0xFF); 261 if ((value[0] & 0x80) != 0x00) 262 { 263 intValue |= 0xFFFF_0000; 264 } 265 break; 266 267 case 3: 268 intValue = ((value[0] & 0xFF) << 16) | ((value[1] & 0xFF) << 8) | 269 (value[2] & 0xFF); 270 if ((value[0] & 0x80) != 0x00) 271 { 272 intValue |= 0xFF00_0000; 273 } 274 break; 275 276 case 4: 277 intValue = ((value[0] & 0xFF) << 24) | ((value[1] & 0xFF) << 16) | 278 ((value[2] & 0xFF) << 8) | (value[3] & 0xFF); 279 break; 280 281 default: 282 throw new ASN1Exception(ERR_ENUMERATED_INVALID_LENGTH.get( 283 value.length)); 284 } 285 286 return new ASN1Enumerated(element.getType(), intValue, value); 287 } 288 289 290 291 /** 292 * {@inheritDoc} 293 */ 294 @Override() 295 public void toString(@NotNull final StringBuilder buffer) 296 { 297 buffer.append(intValue); 298 } 299}