jdk/src/share/classes/com/sun/jndi/ldap/BerDecoder.java
author xuelei
Thu, 16 May 2013 04:30:45 -0700
changeset 17483 ed57f2f26fb6
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8010814: More buffers are stored or returned without cloning Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return lengthbyte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Parses the next sequence in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param rlen An array for returning size of the sequence in bytes. If null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *          the size is not returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return The sequence's tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public int parseSeq(int rlen[]) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int seq = parseByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (rlen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            rlen[0] = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Used to skip bytes. Usually used when trying to recover from parse error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Don't need to be public right now?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param i The number of bytes to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void seek(int i) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (offset + i > bufsize || offset + i < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new DecodeException("array index out of bounds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        offset += i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Parses the next byte in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return The byte parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int parseByte() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (bufsize - offset < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return buf[offset++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the next byte in this BER buffer without consuming it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return The next byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public int peekByte() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (bufsize - offset < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return buf[offset] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Parses an ASN_BOOLEAN tagged integer from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return true if the tagged integer is 0; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean parseBoolean() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return ((parseIntWithTag(ASN_BOOLEAN) == 0x00) ? false : true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Parses an ASN_ENUMERATED tagged integer from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return The tag of enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int parseEnumeration() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return parseIntWithTag(ASN_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Parses an ASN_INTEGER tagged integer from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return The value of the integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public int parseInt() throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return parseIntWithTag(ASN_INTEGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      * Parses an integer that's preceded by a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
      *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      * BER integer ::= tag length byte {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private int parseIntWithTag(int tag) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (parseByte() != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new DecodeException("Encountered ASN.1 tag " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                Integer.toString(buf[offset - 1] & 0xff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                " (expected tag " + Integer.toString(tag) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (len > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new DecodeException("INTEGER too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else if (len > bufsize - offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        byte fb = buf[offset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        value = fb & 0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        for( int i = 1 /* first byte already read */ ; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            value <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            value |= (buf[offset++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if ((fb & 0x80) == 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            value = -value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
      * Parses a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public String parseString(boolean decodeUTF8) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return parseStringWithTag(ASN_SIMPLE_STRING, decodeUTF8, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      * Parses a string of a given tag from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
      *BER simple string ::= tag length {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
      *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
      * @param rlen An array for holding the relative parsed offset; if null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
      *  offset not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      * @param decodeUTF8 If true, use UTF-8 when decoding the string; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      * use ISO-Latin-1 (8859_1). Use true for LDAPv3; false for LDAPv2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      * @param tag The tag that precedes the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
      * @return The non-null parsed string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public String parseStringWithTag(int tag, boolean decodeUTF8, int rlen[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int origOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if ((st = parseByte()) != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new DecodeException("Encountered ASN.1 tag " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                Integer.toString((byte)st) + " (expected tag " + tag + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (len > bufsize - offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        String retstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            retstr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            byte[] buf2 = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            System.arraycopy(buf, offset, buf2, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (decodeUTF8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    retstr = new String(buf2, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    throw new DecodeException("UTF8 not available on platform");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    retstr = new String(buf2, "8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    throw new DecodeException("8859_1 not available on platform");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            offset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (rlen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            rlen[0] = offset - origOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return retstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Parses an octet string of a given type(tag) from this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * BER Binary Data of type "tag" ::= tag length {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param tag The tag to look for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param rlen An array for returning the relative parsed position. If null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *          the relative parsed position is not returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return A non-null array containing the octet string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws DecodeException If the next byte in the BER buffer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <tt>tag</tt>, or if length specified in the BER buffer exceeds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * number of bytes left in the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public byte[] parseOctetString(int tag, int rlen[]) throws DecodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int origOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if ((st = parseByte()) != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new DecodeException("Encountered ASN.1 tag " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                Integer.toString(st) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                " (expected tag " + Integer.toString(tag) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (len > bufsize - offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new DecodeException("Insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        byte retarr[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            System.arraycopy(buf, offset, retarr, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            offset += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (rlen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            rlen[0] = offset - origOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return retarr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Returns the number of unparsed bytes in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public int bytesLeft() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return bufsize - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}