jdk/src/java.naming/share/classes/com/sun/jndi/ldap/BerDecoder.java
author avstepan
Tue, 19 May 2015 16:04:14 +0400
changeset 30655 d83f50188ca9
parent 28551 6533404b7ce1
permissions -rw-r--r--
8080422: some docs cleanup for core libs Summary: some docs cleanup Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17483
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  * A BER decoder. Contains methods to parse a BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  * @author Jagane Sundar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public final class BerDecoder extends Ber {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int origOffset;  // The start point in buf to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Creates a BER decoder that reads bytes from the specified buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public BerDecoder(byte buf[], int offset, int bufsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
17483
ed57f2f26fb6 8010814: More buffers are stored or returned without cloning
xuelei
parents: 5506
diff changeset
    45
        this.buf = buf;         // shared buffer, be careful to use this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        this.bufsize = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.origOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Resets this decode to start parsing from the initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * (ie., same state as after calling the constructor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        offset = origOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      * Returns the current parse position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      * It points to the byte that will be parsed next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      * Useful for parsing sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public int getParsePosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      * Parses a possibly variable length field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public int parseLength() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int lengthbyte = parseByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if ((lengthbyte & 0x80) == 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            lengthbyte &= 0x7f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (lengthbyte == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throw new DecodeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    "Indefinite length not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (lengthbyte > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                throw new DecodeException("encoding too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (bufsize - offset < lengthbyte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            for( int i = 0; i < lengthbyte; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                retval = (retval << 8) + (buf[offset++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
    98
            if (retval < 0) {
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
    99
              throw new DecodeException("Invalid length bytes");
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   100
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return lengthbyte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Parses the next sequence in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param rlen An array for returning size of the sequence in bytes. If null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *          the size is not returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return The sequence's tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public int parseSeq(int rlen[]) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int seq = parseByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (rlen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            rlen[0] = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Used to skip bytes. Usually used when trying to recover from parse error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Don't need to be public right now?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param i The number of bytes to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    void seek(int i) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (offset + i > bufsize || offset + i < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new DecodeException("array index out of bounds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        offset += i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Parses the next byte in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return The byte parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public int parseByte() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (bufsize - offset < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return buf[offset++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the next byte in this BER buffer without consuming it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return The next byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public int peekByte() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (bufsize - offset < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return buf[offset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Parses an ASN_BOOLEAN tagged integer from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return true if the tagged integer is 0; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public boolean parseBoolean() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return ((parseIntWithTag(ASN_BOOLEAN) == 0x00) ? false : true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Parses an ASN_ENUMERATED tagged integer from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return The tag of enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int parseEnumeration() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return parseIntWithTag(ASN_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Parses an ASN_INTEGER tagged integer from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return The value of the integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public int parseInt() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return parseIntWithTag(ASN_INTEGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      * Parses an integer that's preceded by a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      * BER integer ::= tag length byte {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private int parseIntWithTag(int tag) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (parseByte() != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new DecodeException("Encountered ASN.1 tag " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                Integer.toString(buf[offset - 1] & 0xff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                " (expected tag " + Integer.toString(tag) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (len > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new DecodeException("INTEGER too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else if (len > bufsize - offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        byte fb = buf[offset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        value = fb & 0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for( int i = 1 /* first byte already read */ ; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            value <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            value |= (buf[offset++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if ((fb & 0x80) == 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            value = -value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
      * Parses a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public String parseString(boolean decodeUTF8) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return parseStringWithTag(ASN_SIMPLE_STRING, decodeUTF8, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
      * Parses a string of a given tag from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
      *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
      *BER simple string ::= tag length {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      * @param rlen An array for holding the relative parsed offset; if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      *  offset not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
      * @param decodeUTF8 If true, use UTF-8 when decoding the string; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
      * use ISO-Latin-1 (8859_1). Use true for LDAPv3; false for LDAPv2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
      * @param tag The tag that precedes the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
      * @return The non-null parsed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public String parseStringWithTag(int tag, boolean decodeUTF8, int rlen[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int origOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if ((st = parseByte()) != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new DecodeException("Encountered ASN.1 tag " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                Integer.toString((byte)st) + " (expected tag " + tag + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (len > bufsize - offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        String retstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            retstr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            byte[] buf2 = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            System.arraycopy(buf, offset, buf2, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (decodeUTF8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    retstr = new String(buf2, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    throw new DecodeException("UTF8 not available on platform");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    retstr = new String(buf2, "8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    throw new DecodeException("8859_1 not available on platform");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            offset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (rlen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            rlen[0] = offset - origOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return retstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Parses an octet string of a given type(tag) from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * BER Binary Data of type "tag" ::= tag length {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param tag The tag to look for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param rlen An array for returning the relative parsed position. If null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *          the relative parsed position is not returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return A non-null array containing the octet string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @throws DecodeException If the next byte in the BER buffer is not
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 28551
diff changeset
   298
     * {@code tag}, or if length specified in the BER buffer exceeds the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * number of bytes left in the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public byte[] parseOctetString(int tag, int rlen[]) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int origOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if ((st = parseByte()) != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new DecodeException("Encountered ASN.1 tag " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                Integer.toString(st) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                " (expected tag " + Integer.toString(tag) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (len > bufsize - offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        byte retarr[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            System.arraycopy(buf, offset, retarr, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            offset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (rlen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            rlen[0] = offset - origOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return retarr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the number of unparsed bytes in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public int bytesLeft() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return bufsize - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}