jdk/src/java.naming/share/classes/com/sun/jndi/ldap/BerEncoder.java
author stefank
Mon, 25 Aug 2014 09:10:13 +0200
changeset 26314 f8bc1966fb30
parent 25859 3317bb8137f4
permissions -rw-r--r--
8055416: Several vm/gc/heap/summary "After GC" events emitted for the same GC ID Reviewed-by: brutisso, ehelin
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 encoder.
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 Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public final class BerEncoder extends Ber {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private int curSeqIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private int seqOffset[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final int INITIAL_SEQUENCES = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final int DEFAULT_BUFSIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // When buf is full, expand its size by the following factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int BUF_GROWTH_FACTOR = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Creates a BER buffer for encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public BerEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this(DEFAULT_BUFSIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a BER buffer of a specified size for encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Specify the initial bufsize.  Buffer will be expanded as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param bufsize The number of bytes for the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public BerEncoder(int bufsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        buf = new byte[bufsize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.bufsize = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        seqOffset = new int[INITIAL_SEQUENCES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        curSeqIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Resets encoder to state when newly constructed.  Zeros out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * internal data structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        while (offset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            buf[--offset] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        while (curSeqIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            seqOffset[--curSeqIndex] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
// ------------------ Accessor methods ------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Gets the number of encoded bytes in this BER buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public int getDataLen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Gets the buffer that contains the BER encoding. Throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * exception if unmatched beginSeq() and endSeq() pairs were
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * encountered. Not entire buffer contains encoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Use getDataLen() to determine number of encoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Use getBuffer(true) to get rid of excess bytes in array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws IllegalStateException If buffer contains unbalanced sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public byte[] getBuf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (curSeqIndex != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IllegalStateException("BER encode error: Unbalanced SEQUENCEs.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
17483
ed57f2f26fb6 8010814: More buffers are stored or returned without cloning
xuelei
parents: 5506
diff changeset
   102
        return buf;     // shared buffer, be careful to use this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Gets the buffer that contains the BER encoding, trimming unused bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws IllegalStateException If buffer contains unbalanced sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public byte[] getTrimmedBuf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int len = getDataLen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte[] trimBuf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.arraycopy(getBuf(), 0, trimBuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return trimBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
// -------------- encoding methods -------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Begin encoding a sequence with a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void beginSeq(int tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Double the size of the SEQUENCE array if it overflows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (curSeqIndex >= seqOffset.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            int[] seqOffsetTmp = new int[seqOffset.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            for (int i = 0; i < seqOffset.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                seqOffsetTmp[i] = seqOffset[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            seqOffset = seqOffsetTmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        encodeByte(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        seqOffset[curSeqIndex] = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Save space for sequence length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // %%% Currently we save enough space for sequences up to 64k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        //     For larger sequences we'll need to shift the data to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        //     in endSeq().  If we could instead pad the length field with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        //     zeros, it would be a big win.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ensureFreeBytes(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        offset += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        curSeqIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
      * Terminate a BER sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void endSeq() throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        curSeqIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (curSeqIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new IllegalStateException("BER encode error: Unbalanced SEQUENCEs.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int start = seqOffset[curSeqIndex] + 3; // index beyond length field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int len = offset - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (len <= 0x7f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            shiftSeqData(start, len, -2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            buf[seqOffset[curSeqIndex]] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else if (len <= 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            shiftSeqData(start, len, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            buf[seqOffset[curSeqIndex]] = (byte) 0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            buf[seqOffset[curSeqIndex] + 1] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else if (len <= 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            buf[seqOffset[curSeqIndex]] = (byte) 0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            buf[seqOffset[curSeqIndex] + 1] = (byte) (len >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            buf[seqOffset[curSeqIndex] + 2] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else if (len <= 0xffffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            shiftSeqData(start, len, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            buf[seqOffset[curSeqIndex]] = (byte) 0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            buf[seqOffset[curSeqIndex] + 1] = (byte) (len >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            buf[seqOffset[curSeqIndex] + 2] = (byte) (len >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            buf[seqOffset[curSeqIndex] + 3] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new EncodeException("SEQUENCE too long");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Shifts contents of buf in the range [start,start+len) a specified amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Positive shift value means shift to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void shiftSeqData(int start, int len, int shift) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (shift > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            ensureFreeBytes(shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        System.arraycopy(buf, start, buf, start + shift, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        offset += shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Encode a single byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public void encodeByte(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        ensureFreeBytes(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        buf[offset++] = (byte) b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private void deleteByte() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Encodes an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * BER integer ::= 0x02 berlength byte {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void encodeInt(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        encodeInt(i, 0x02);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Encodes an int and a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * BER integer w tag ::= tag berlength byte {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void encodeInt(int i, int tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int mask = 0xff800000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int intsize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        while( (((i & mask) == 0) || ((i & mask) == mask)) && (intsize > 1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            intsize--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            i <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        encodeInt(i, tag, intsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // encodes an int using numbytes for the actual encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private void encodeInt(int i, int tag, int intsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // integer ::= 0x02 asnlength byte {byte}*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (intsize > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new IllegalArgumentException("BER encode error: INTEGER too long.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        ensureFreeBytes(2 + intsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        buf[offset++] = (byte) tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        buf[offset++] = (byte) intsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int mask = 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        while (intsize-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            buf[offset++] = (byte) ((i & mask) >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            i <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Encodes a boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * BER boolean ::= 0x01 0x01 {0xff|0x00}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void encodeBoolean(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        encodeBoolean(b, ASN_BOOLEAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Encodes a boolean and a tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * BER boolean w TAG ::= tag 0x01 {0xff|0x00}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public void encodeBoolean(boolean b, int tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        ensureFreeBytes(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        buf[offset++] = (byte) tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        buf[offset++] = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        buf[offset++] = b ? (byte) 0xff : (byte) 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Encodes a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * BER string ::= 0x04 strlen byte1 byte2...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * The string is converted into bytes using UTF-8 or ISO-Latin-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void encodeString(String str, boolean encodeUTF8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        encodeString(str, ASN_OCTET_STR, encodeUTF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Encodes a string and a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * BER string w TAG ::= tag strlen byte1 byte2...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void encodeString(String str, int tag, boolean encodeUTF8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        encodeByte(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else if (encodeUTF8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                bytes = str.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                count = bytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                throw new EncodeException("UTF8 not available on platform");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                bytes = str.getBytes("8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                count = bytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                throw new EncodeException("8859_1 not available on platform");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        encodeLength(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        ensureFreeBytes(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        while (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            buf[offset++] = bytes[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Encodes a portion of an octet string and a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public void encodeOctetString(byte tb[], int tag, int tboffset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        encodeByte(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        encodeLength(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            ensureFreeBytes(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            System.arraycopy(tb, tboffset, buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            offset += length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
      * Encodes an octet string and a tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public void encodeOctetString(byte tb[], int tag) throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        encodeOctetString(tb, tag, 0, tb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private void encodeLength(int len) throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        ensureFreeBytes(4);     // worst case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (len < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            buf[offset++] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else if (len <= 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            buf[offset++] = (byte) 0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            buf[offset++] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else if (len <= 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            buf[offset++] = (byte) 0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            buf[offset++] = (byte) (len >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            buf[offset++] = (byte) (len & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } else if (len <= 0xffffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            buf[offset++] = (byte) 0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            buf[offset++] = (byte) (len >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            buf[offset++] = (byte) (len >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            buf[offset++] = (byte) (len & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new EncodeException("string too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Encodes an array of strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public void encodeStringArray(String strs[], boolean encodeUTF8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        throws EncodeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (strs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        for (int i = 0; i < strs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            encodeString(strs[i], encodeUTF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private void encodeNull() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // NULL ::= 0x05 0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        encodeByte(0x05);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        encodeByte(0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Ensures that there are at least "len" unused bytes in "buf".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * When more space is needed "buf" is expanded by a factor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * BUF_GROWTH_FACTOR, then "len" bytes are added if "buf" still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * isn't large enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private void ensureFreeBytes(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (bufsize - offset < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            int newsize = bufsize * BUF_GROWTH_FACTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (newsize - offset < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                newsize += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            byte newbuf[] = new byte[newsize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // Only copy bytes in the range [0, offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            System.arraycopy(buf, 0, newbuf, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            bufsize = newsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}