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.StaticUtils;
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 null element, which does not hold a value.  Null
052 * elements are generally used as placeholders that can be substituted for other
053 * types of elements.
054 */
055@NotMutable()
056@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
057public final class ASN1Null
058       extends ASN1Element
059{
060  /**
061   * A pre-allocated ASN.1 null element with the universal null BER type.
062   */
063  public static final ASN1Null UNIVERSAL_NULL_ELEMENT = new ASN1Null();
064
065
066
067  /**
068   * The serial version UID for this serializable class.
069   */
070  private static final long serialVersionUID = -3264450066845549348L;
071
072
073
074  /**
075   * Creates a new ASN.1 null element with the default BER type.
076   */
077  public ASN1Null()
078  {
079    super(ASN1Constants.UNIVERSAL_NULL_TYPE);
080  }
081
082
083
084  /**
085   * Creates a new ASN.1 null element with the specified BER type.
086   *
087   * @param  type  The BER type to use for this ASN.1 null element.
088   */
089  public ASN1Null(final byte type)
090  {
091    super(type);
092  }
093
094
095
096  /**
097   * Decodes the contents of the provided byte array as a null element.
098   *
099   * @param  elementBytes  The byte array to decode as an ASN.1 null element.
100   *
101   * @return  The decoded ASN.1 null element.
102   *
103   * @throws  ASN1Exception  If the provided array cannot be decoded as a null
104   *                         element.
105   */
106  public static ASN1Null decodeAsNull(final byte[] elementBytes)
107         throws ASN1Exception
108  {
109    try
110    {
111      int valueStartPos = 2;
112      int length = (elementBytes[1] & 0x7F);
113      if (length != elementBytes[1])
114      {
115        final int numLengthBytes = length;
116
117        length = 0;
118        for (int i=0; i < numLengthBytes; i++)
119        {
120          length <<= 8;
121          length |= (elementBytes[valueStartPos++] & 0xFF);
122        }
123      }
124
125      if ((elementBytes.length - valueStartPos) != length)
126      {
127        throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length,
128                                     (elementBytes.length - valueStartPos)));
129      }
130
131      if (length != 0)
132      {
133        throw new ASN1Exception(ERR_NULL_HAS_VALUE.get());
134      }
135
136      return new ASN1Null(elementBytes[0]);
137    }
138    catch (final ASN1Exception ae)
139    {
140      Debug.debugException(ae);
141      throw ae;
142    }
143    catch (final Exception e)
144    {
145      Debug.debugException(e);
146      throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e);
147    }
148  }
149
150
151
152  /**
153   * Decodes the provided ASN.1 element as a null element.
154   *
155   * @param  element  The ASN.1 element to be decoded.
156   *
157   * @return  The decoded ASN.1 null element.
158   *
159   * @throws  ASN1Exception  If the provided element cannot be decoded as a null
160   *                         element.
161   */
162  public static ASN1Null decodeAsNull(final ASN1Element element)
163         throws ASN1Exception
164  {
165    if (element.getValue().length != 0)
166    {
167      throw new ASN1Exception(ERR_NULL_HAS_VALUE.get());
168    }
169
170    return new ASN1Null(element.getType());
171  }
172
173
174
175  /**
176   * {@inheritDoc}
177   */
178  @Override()
179  public void toString(final StringBuilder buffer)
180  {
181    buffer.append("ASN1Null(type=");
182    StaticUtils.toHex(getType(), buffer);
183    buffer.append(')');
184  }
185}