001/*
002 * Copyright 2017-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-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) 2017-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.Nullable;
044import com.unboundid.util.StaticUtils;
045import com.unboundid.util.ThreadSafety;
046import com.unboundid.util.ThreadSafetyLevel;
047
048import static com.unboundid.asn1.ASN1Messages.*;
049
050
051
052/**
053 * This class provides an ASN.1 printable string element that can hold any
054 * empty or non-empty string comprised only of the characters listed below.
055 * <UL>
056 *   <LI>The uppercase ASCII letters A through Z.</LI>
057 *   <LI>The lowercase ASCII letters a through z.</LI>
058 *   <LI>The ASCII digits 0 through 9.</LI>
059 *   <LI>The ASCII space.</LI>
060 *   <LI>The ASCII apostrophe (aka single quote).</LI>
061 *   <LI>The ASCII left parenthesis.</LI>
062 *   <LI>The ASCII right parenthesis.</LI>
063 *   <LI>The ASCII plus sign.</LI>
064 *   <LI>The ASCII comma.</LI>
065 *   <LI>The ASCII minus sign (aka hyphen).</LI>
066 *   <LI>The ASCII period (aka full stop).</LI>
067 *   <LI>The ASCII forward slash (aka solidus).</LI>
068 *   <LI>The ASCII colon.</LI>
069 *   <LI>The ASCII equal sign.</LI>
070 *   <LI>The ASCII question mark.</LI>
071 * </UL>
072 */
073@NotMutable()
074@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
075public final class ASN1PrintableString
076       extends ASN1Element
077{
078  /**
079   * The serial version UID for this serializable class.
080   */
081  private static final long serialVersionUID = 7489436088285132189L;
082
083
084
085  // The string value for this element.
086  @NotNull private final String stringValue;
087
088
089
090  /**
091   * Creates a new ASN.1 printable string element with the default BER type and
092   * the provided value.
093   *
094   * @param  stringValue  The string value to use for this element.  It may be
095   *                      {@code null} or empty if the value should be empty.
096   *                      It must only contain characters allowed in printable
097   *                      strings.
098   *
099   * @throws  ASN1Exception  If the provided string does not represent a valid
100   *                         printable string.
101   */
102  public ASN1PrintableString(@Nullable final String stringValue)
103         throws ASN1Exception
104  {
105    this(ASN1Constants.UNIVERSAL_PRINTABLE_STRING_TYPE, stringValue);
106  }
107
108
109
110  /**
111   * Creates a new ASN.1 printable string element with the specified BER type
112   * and the provided value.
113   *
114   * @param  type         The BER type for this element.
115   * @param  stringValue  The string value to use for this element.  It may be
116   *                      {@code null} or empty if the value should be empty.
117   *                      It must only contain characters allowed in printable
118   *                      strings.
119   *
120   * @throws  ASN1Exception  If the provided string does not represent a valid
121   *                         printable string.
122   */
123  public ASN1PrintableString(final byte type,
124                             @Nullable final String stringValue)
125         throws ASN1Exception
126  {
127    this(type, stringValue, StaticUtils.getBytes(stringValue));
128  }
129
130
131
132  /**
133   * Creates a new ASN.1 printable string element with the specified BER type
134   * and the provided value.
135   *
136   * @param  type          The BER type for this element.
137   * @param  stringValue   The string value to use for this element.  It may be
138   *                       {@code null} or empty if the value should be empty.
139   *                       It must only contain characters allowed in printable
140   *                       strings.
141   * @param  encodedValue  The bytes that comprise the encoded element value.
142   *
143   * @throws  ASN1Exception  If the provided string does not represent a valid
144   *                         printable string.
145   */
146  private ASN1PrintableString(final byte type,
147                              @Nullable final String stringValue,
148                              @NotNull final byte[] encodedValue)
149          throws ASN1Exception
150  {
151    super(type, encodedValue);
152
153    if (stringValue == null)
154    {
155      this.stringValue = "";
156    }
157    else
158    {
159      this.stringValue = stringValue;
160      if (! StaticUtils.isPrintableString(encodedValue))
161      {
162        throw new ASN1Exception(
163             ERR_PRINTABLE_STRING_DECODE_VALUE_NOT_PRINTABLE.get());
164      }
165    }
166  }
167
168
169
170  /**
171   * Retrieves the string value for this element.
172   *
173   * @return  The string value for this element.
174   */
175  @NotNull()
176  public String stringValue()
177  {
178    return stringValue;
179  }
180
181
182
183  /**
184   * Decodes the contents of the provided byte array as a printable string
185   * element.
186   *
187   * @param  elementBytes  The byte array to decode as an ASN.1 printable string
188   *                       element.
189   *
190   * @return  The decoded ASN.1 printable string element.
191   *
192   * @throws  ASN1Exception  If the provided array cannot be decoded as a
193   *                         printable string element.
194   */
195  @NotNull()
196  public static ASN1PrintableString decodeAsPrintableString(
197                                         @NotNull final byte[] elementBytes)
198         throws ASN1Exception
199  {
200    try
201    {
202      int valueStartPos = 2;
203      int length = (elementBytes[1] & 0x7F);
204      if (length != elementBytes[1])
205      {
206        final int numLengthBytes = length;
207
208        length = 0;
209        for (int i=0; i < numLengthBytes; i++)
210        {
211          length <<= 8;
212          length |= (elementBytes[valueStartPos++] & 0xFF);
213        }
214      }
215
216      if ((elementBytes.length - valueStartPos) != length)
217      {
218        throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length,
219                                     (elementBytes.length - valueStartPos)));
220      }
221
222      final byte[] elementValue = new byte[length];
223      System.arraycopy(elementBytes, valueStartPos, elementValue, 0, length);
224
225      return new ASN1PrintableString(elementBytes[0],
226           StaticUtils.toUTF8String(elementValue), elementValue);
227    }
228    catch (final ASN1Exception ae)
229    {
230      Debug.debugException(ae);
231      throw ae;
232    }
233    catch (final Exception e)
234    {
235      Debug.debugException(e);
236      throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e);
237    }
238  }
239
240
241
242  /**
243   * Decodes the provided ASN.1 element as a printable string element.
244   *
245   * @param  element  The ASN.1 element to be decoded.
246   *
247   * @return  The decoded ASN.1 printable string element.
248   *
249   * @throws  ASN1Exception  If the provided element cannot be decoded as a
250   *                         printable string element.
251   */
252  @NotNull()
253  public static ASN1PrintableString decodeAsPrintableString(
254                                         @NotNull final ASN1Element element)
255         throws ASN1Exception
256  {
257    final byte[] elementValue = element.getValue();
258    return new ASN1PrintableString(element.getType(),
259         StaticUtils.toUTF8String(elementValue), elementValue);
260  }
261
262
263
264  /**
265   * {@inheritDoc}
266   */
267  @Override()
268  public void toString(@NotNull final StringBuilder buffer)
269  {
270    buffer.append(stringValue);
271  }
272}