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 UTF-8 string element that can hold any string
054 * value that can be represented in the UTF-8 encoding.
055 */
056@NotMutable()
057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058public final class ASN1UTF8String
059       extends ASN1Element
060{
061  /**
062   * The serial version UID for this serializable class.
063   */
064  private static final long serialVersionUID = -2147537370903003997L;
065
066
067
068  // The string value for this element.
069  @NotNull private final String stringValue;
070
071
072
073  /**
074   * Creates a new ASN.1 UTF-8 string element with the default BER type and the
075   * provided value.
076   *
077   * @param  stringValue  The string value to use for this element.  It may be
078   *                      {@code null} or empty if the value should be empty.
079   */
080  public ASN1UTF8String(@Nullable final String stringValue)
081  {
082    this(ASN1Constants.UNIVERSAL_UTF_8_STRING_TYPE, stringValue);
083  }
084
085
086
087  /**
088   * Creates a new ASN.1 UTF-8 string element with the specified BER type and
089   * the provided value.
090   *
091   * @param  type         The BER type for this element.
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   */
095  public ASN1UTF8String(final byte type, @Nullable final String stringValue)
096  {
097    this(type, stringValue, StaticUtils.getBytes(stringValue));
098  }
099
100
101
102  /**
103   * Creates a new ASN.1 UTF-8 string element with the specified BER type and
104   * the provided value.
105   *
106   * @param  type          The BER type for this element.
107   * @param  stringValue   The string value to use for this element.  It may be
108   *                       {@code null} or empty if the value should be empty.
109   * @param  encodedValue  The encoded representation of the value.
110   */
111  private ASN1UTF8String(final byte type, @Nullable final String stringValue,
112                         @NotNull final byte[] encodedValue)
113  {
114    super(type, encodedValue);
115
116    if (stringValue == null)
117    {
118      this.stringValue = "";
119    }
120    else
121    {
122      this.stringValue = stringValue;
123    }
124  }
125
126
127
128  /**
129   * Retrieves the string value for this element.
130   *
131   * @return  The string value for this element.
132   */
133  @NotNull()
134  public String stringValue()
135  {
136    return stringValue;
137  }
138
139
140
141  /**
142   * Decodes the contents of the provided byte array as a UTF-8 string element.
143   *
144   * @param  elementBytes  The byte array to decode as an ASN.1 UTF-8 string
145   *                       element.
146   *
147   * @return  The decoded ASN.1 UTF-8 string element.
148   *
149   * @throws  ASN1Exception  If the provided array cannot be decoded as a UTF-8
150   *                         string element.
151   */
152  @NotNull()
153  public static ASN1UTF8String decodeAsUTF8String(
154                                    @NotNull final byte[] elementBytes)
155         throws ASN1Exception
156  {
157    try
158    {
159      int valueStartPos = 2;
160      int length = (elementBytes[1] & 0x7F);
161      if (length != elementBytes[1])
162      {
163        final int numLengthBytes = length;
164
165        length = 0;
166        for (int i=0; i < numLengthBytes; i++)
167        {
168          length <<= 8;
169          length |= (elementBytes[valueStartPos++] & 0xFF);
170        }
171      }
172
173      if ((elementBytes.length - valueStartPos) != length)
174      {
175        throw new ASN1Exception(ERR_ELEMENT_LENGTH_MISMATCH.get(length,
176                                     (elementBytes.length - valueStartPos)));
177      }
178
179      final byte[] elementValue = new byte[length];
180      System.arraycopy(elementBytes, valueStartPos, elementValue, 0, length);
181
182      if (! StaticUtils.isValidUTF8(elementValue))
183      {
184        throw new ASN1Exception(ERR_UTF_8_STRING_DECODE_VALUE_NOT_UTF_8.get());
185      }
186
187      return new ASN1UTF8String(elementBytes[0],
188           StaticUtils.toUTF8String(elementValue), elementValue);
189    }
190    catch (final ASN1Exception ae)
191    {
192      Debug.debugException(ae);
193      throw ae;
194    }
195    catch (final Exception e)
196    {
197      Debug.debugException(e);
198      throw new ASN1Exception(ERR_ELEMENT_DECODE_EXCEPTION.get(e), e);
199    }
200  }
201
202
203
204  /**
205   * Decodes the provided ASN.1 element as a UTF-8 string element.
206   *
207   * @param  element  The ASN.1 element to be decoded.
208   *
209   * @return  The decoded ASN.1 UTF-8 string element.
210   *
211   * @throws  ASN1Exception  If the provided element cannot be decoded as a
212   *                         UTF-8 string element.
213   */
214  @NotNull()
215  public static ASN1UTF8String decodeAsUTF8String(
216                                    @NotNull final ASN1Element element)
217         throws ASN1Exception
218  {
219    final byte[] elementValue = element.getValue();
220    if (! StaticUtils.isValidUTF8(elementValue))
221    {
222      throw new ASN1Exception(ERR_UTF_8_STRING_DECODE_VALUE_NOT_UTF_8.get());
223    }
224
225    return new ASN1UTF8String(element.getType(),
226         StaticUtils.toUTF8String(elementValue), elementValue);
227  }
228
229
230
231  /**
232   * {@inheritDoc}
233   */
234  @Override()
235  public void toString(@NotNull final StringBuilder buffer)
236  {
237    buffer.append(stringValue);
238  }
239}