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