jdk/src/java.base/share/classes/sun/security/provider/ByteArrayAccess.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30338 957ee8b5a33a
child 33656 ef901bc43f7a
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30338
957ee8b5a33a 8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents: 25859
diff changeset
     2
 * Copyright (c) 2006, 2015, 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 sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import static java.lang.Integer.reverseBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import static java.lang.Long.reverseBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Optimized methods for converting between byte[] and int[]/long[], both for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * big endian and little endian byte orders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Currently, it includes a default code path plus two optimized code paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * One is for little endian architectures that support full speed int/long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * access at unaligned addresses (i.e. x86/amd64). The second is for big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * architectures (that only support correctly aligned access), such as SPARC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * These are the only platforms we currently support, but other optimized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * variants could be added as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
    46
 * NOTE that ArrayIndexOutOfBoundsException will be thrown if the bounds checks
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
    47
 * failed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This class may also be helpful in improving the performance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * crypto code in the SunJCE provider. However, for now it is only accessible by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the message digest implementation in the SUN provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
final class ByteArrayAccess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private ByteArrayAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // whether to use the optimized path for little endian platforms that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // support full speed unaligned memory access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final boolean littleEndianUnaligned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // whether to use the optimzied path for big endian platforms that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // support only correctly aligned full speed memory access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // (Note that on SPARC unaligned memory access is possible, but it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // implemented using a software trap and therefore very slow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final boolean bigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30338
diff changeset
    74
    private static final int byteArrayOfs = unsafe.arrayBaseOffset(byte[].class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        boolean scaleOK = ((unsafe.arrayIndexScale(byte[].class) == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            && (unsafe.arrayIndexScale(int[].class) == 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            && (unsafe.arrayIndexScale(long[].class) == 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            && ((byteArrayOfs & 3) == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ByteOrder byteOrder = ByteOrder.nativeOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        littleEndianUnaligned =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            scaleOK && unaligned() && (byteOrder == ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        bigEndian =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            scaleOK && (byteOrder == ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Return whether this platform supports full speed int/long memory access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // at unaligned addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static boolean unaligned() {
30338
957ee8b5a33a 8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents: 25859
diff changeset
    92
        return unsafe.unalignedAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * byte[] to int[] conversion, little endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void b2iLittle(byte[] in, int inOfs, int[] out, int outOfs, int len) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
    99
        if ((inOfs < 0) || ((in.length - inOfs) < len) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   100
            (outOfs < 0) || ((out.length - outOfs) < len/4)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   101
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   102
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                out[outOfs++] = unsafe.getInt(in, (long)inOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } else if (bigEndian && ((inOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                out[outOfs++] = reverseBytes(unsafe.getInt(in, (long)inOfs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                out[outOfs++] = ((in[inOfs    ] & 0xff)      )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                              | ((in[inOfs + 1] & 0xff) <<  8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                              | ((in[inOfs + 2] & 0xff) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                              | ((in[inOfs + 3]       ) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // Special optimization of b2iLittle(in, inOfs, out, 0, 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static void b2iLittle64(byte[] in, int inOfs, int[] out) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   131
        if ((inOfs < 0) || ((in.length - inOfs) < 64) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   132
            (out.length < 16)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   133
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   134
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            out[ 0] = unsafe.getInt(in, (long)(inOfs     ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            out[ 1] = unsafe.getInt(in, (long)(inOfs +  4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            out[ 2] = unsafe.getInt(in, (long)(inOfs +  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            out[ 3] = unsafe.getInt(in, (long)(inOfs + 12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            out[ 4] = unsafe.getInt(in, (long)(inOfs + 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            out[ 5] = unsafe.getInt(in, (long)(inOfs + 20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            out[ 6] = unsafe.getInt(in, (long)(inOfs + 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            out[ 7] = unsafe.getInt(in, (long)(inOfs + 28));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            out[ 8] = unsafe.getInt(in, (long)(inOfs + 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            out[ 9] = unsafe.getInt(in, (long)(inOfs + 36));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            out[10] = unsafe.getInt(in, (long)(inOfs + 40));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            out[11] = unsafe.getInt(in, (long)(inOfs + 44));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            out[12] = unsafe.getInt(in, (long)(inOfs + 48));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out[13] = unsafe.getInt(in, (long)(inOfs + 52));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            out[14] = unsafe.getInt(in, (long)(inOfs + 56));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            out[15] = unsafe.getInt(in, (long)(inOfs + 60));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else if (bigEndian && ((inOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            out[ 0] = reverseBytes(unsafe.getInt(in, (long)(inOfs     )));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            out[ 1] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  4)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            out[ 2] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  8)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            out[ 3] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 12)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            out[ 4] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 16)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            out[ 5] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 20)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            out[ 6] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 24)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            out[ 7] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 28)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            out[ 8] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 32)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            out[ 9] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 36)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            out[10] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 40)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out[11] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 44)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            out[12] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 48)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            out[13] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 52)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            out[14] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 56)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            out[15] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 60)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            b2iLittle(in, inOfs, out, 0, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * int[] to byte[] conversion, little endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static void i2bLittle(int[] in, int inOfs, byte[] out, int outOfs, int len) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   180
        if ((inOfs < 0) || ((in.length - inOfs) < len/4) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   181
            (outOfs < 0) || ((out.length - outOfs) < len)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   182
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   183
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            outOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                unsafe.putInt(out, (long)outOfs, in[inOfs++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                outOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else if (bigEndian && ((outOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            outOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                unsafe.putInt(out, (long)outOfs, reverseBytes(in[inOfs++]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                outOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                int i = in[inOfs++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                out[outOfs++] = (byte)(i      );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                out[outOfs++] = (byte)(i >>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                out[outOfs++] = (byte)(i >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                out[outOfs++] = (byte)(i >> 24);
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
    // Store one 32-bit value into out[outOfs..outOfs+3] in little endian order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static void i2bLittle4(int val, byte[] out, int outOfs) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   212
        if ((outOfs < 0) || ((out.length - outOfs) < 4)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   213
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   214
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } else if (bigEndian && ((outOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), reverseBytes(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            out[outOfs    ] = (byte)(val      );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            out[outOfs + 1] = (byte)(val >>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            out[outOfs + 2] = (byte)(val >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            out[outOfs + 3] = (byte)(val >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * byte[] to int[] conversion, big endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    static void b2iBig(byte[] in, int inOfs, int[] out, int outOfs, int len) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   231
        if ((inOfs < 0) || ((in.length - inOfs) < len) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   232
            (outOfs < 0) || ((out.length - outOfs) < len/4)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   233
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   234
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                out[outOfs++] = reverseBytes(unsafe.getInt(in, (long)inOfs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else if (bigEndian && ((inOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                out[outOfs++] = unsafe.getInt(in, (long)inOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                out[outOfs++] = ((in[inOfs + 3] & 0xff)      )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                              | ((in[inOfs + 2] & 0xff) <<  8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                              | ((in[inOfs + 1] & 0xff) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                              | ((in[inOfs    ]       ) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // Special optimization of b2iBig(in, inOfs, out, 0, 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    static void b2iBig64(byte[] in, int inOfs, int[] out) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   263
        if ((inOfs < 0) || ((in.length - inOfs) < 64) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   264
            (out.length < 16)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   265
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   266
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            out[ 0] = reverseBytes(unsafe.getInt(in, (long)(inOfs     )));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            out[ 1] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  4)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            out[ 2] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  8)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            out[ 3] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 12)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            out[ 4] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 16)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            out[ 5] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 20)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            out[ 6] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 24)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            out[ 7] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 28)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            out[ 8] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 32)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            out[ 9] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 36)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            out[10] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 40)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            out[11] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 44)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            out[12] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 48)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            out[13] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 52)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            out[14] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 56)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            out[15] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 60)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else if (bigEndian && ((inOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            out[ 0] = unsafe.getInt(in, (long)(inOfs     ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            out[ 1] = unsafe.getInt(in, (long)(inOfs +  4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            out[ 2] = unsafe.getInt(in, (long)(inOfs +  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            out[ 3] = unsafe.getInt(in, (long)(inOfs + 12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            out[ 4] = unsafe.getInt(in, (long)(inOfs + 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            out[ 5] = unsafe.getInt(in, (long)(inOfs + 20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            out[ 6] = unsafe.getInt(in, (long)(inOfs + 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            out[ 7] = unsafe.getInt(in, (long)(inOfs + 28));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            out[ 8] = unsafe.getInt(in, (long)(inOfs + 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            out[ 9] = unsafe.getInt(in, (long)(inOfs + 36));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            out[10] = unsafe.getInt(in, (long)(inOfs + 40));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            out[11] = unsafe.getInt(in, (long)(inOfs + 44));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            out[12] = unsafe.getInt(in, (long)(inOfs + 48));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            out[13] = unsafe.getInt(in, (long)(inOfs + 52));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            out[14] = unsafe.getInt(in, (long)(inOfs + 56));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            out[15] = unsafe.getInt(in, (long)(inOfs + 60));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            b2iBig(in, inOfs, out, 0, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
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[] to byte[] conversion, big endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static void i2bBig(int[] in, int inOfs, byte[] out, int outOfs, int len) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   312
        if ((inOfs < 0) || ((in.length - inOfs) < len/4) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   313
            (outOfs < 0) || ((out.length - outOfs) < len)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   314
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   315
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            outOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                unsafe.putInt(out, (long)outOfs, reverseBytes(in[inOfs++]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                outOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else if (bigEndian && ((outOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            outOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                unsafe.putInt(out, (long)outOfs, in[inOfs++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                outOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                int i = in[inOfs++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                out[outOfs++] = (byte)(i >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                out[outOfs++] = (byte)(i >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                out[outOfs++] = (byte)(i >>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                out[outOfs++] = (byte)(i      );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
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
    // Store one 32-bit value into out[outOfs..outOfs+3] in big endian order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static void i2bBig4(int val, byte[] out, int outOfs) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   344
        if ((outOfs < 0) || ((out.length - outOfs) < 4)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   345
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   346
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), reverseBytes(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } else if (bigEndian && ((outOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            out[outOfs    ] = (byte)(val >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            out[outOfs + 1] = (byte)(val >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            out[outOfs + 2] = (byte)(val >>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            out[outOfs + 3] = (byte)(val      );
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * byte[] to long[] conversion, big endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    static void b2lBig(byte[] in, int inOfs, long[] out, int outOfs, int len) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   363
        if ((inOfs < 0) || ((in.length - inOfs) < len) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   364
            (outOfs < 0) || ((out.length - outOfs) < len/8)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   365
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   366
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                out[outOfs++] = reverseBytes(unsafe.getLong(in, (long)inOfs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                inOfs += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        } else if (bigEndian && ((inOfs & 3) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // In the current HotSpot memory layout, the first element of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // byte[] is only 32-bit aligned, not 64-bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // That means we could use getLong() only for offset 4, 12, etc.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // which would rarely occur in practice. Instead, we use an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // optimization that uses getInt() so that it works for offset 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                out[outOfs++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                      ((long)unsafe.getInt(in, (long)inOfs) << 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                          | (unsafe.getInt(in, (long)(inOfs + 4)) & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                inOfs += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            len += inOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            while (inOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                int i1 = ((in[inOfs + 3] & 0xff)      )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                       | ((in[inOfs + 2] & 0xff) <<  8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                       | ((in[inOfs + 1] & 0xff) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                       | ((in[inOfs    ]       ) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                int i2 = ((in[inOfs + 3] & 0xff)      )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                       | ((in[inOfs + 2] & 0xff) <<  8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                       | ((in[inOfs + 1] & 0xff) << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                       | ((in[inOfs    ]       ) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                out[outOfs++] = ((long)i1 << 32) | (i2 & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                inOfs += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // Special optimization of b2lBig(in, inOfs, out, 0, 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    static void b2lBig128(byte[] in, int inOfs, long[] out) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   408
        if ((inOfs < 0) || ((in.length - inOfs) < 128) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   409
            (out.length < 16)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   410
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   411
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (littleEndianUnaligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            inOfs += byteArrayOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            out[ 0] = reverseBytes(unsafe.getLong(in, (long)(inOfs      )));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            out[ 1] = reverseBytes(unsafe.getLong(in, (long)(inOfs +   8)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            out[ 2] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  16)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            out[ 3] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  24)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            out[ 4] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  32)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            out[ 5] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  40)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            out[ 6] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  48)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            out[ 7] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  56)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            out[ 8] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  64)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            out[ 9] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  72)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            out[10] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  80)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            out[11] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  88)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            out[12] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  96)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            out[13] = reverseBytes(unsafe.getLong(in, (long)(inOfs + 104)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            out[14] = reverseBytes(unsafe.getLong(in, (long)(inOfs + 112)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            out[15] = reverseBytes(unsafe.getLong(in, (long)(inOfs + 120)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // no optimization for big endian, see comments in b2lBig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            b2lBig(in, inOfs, out, 0, 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * long[] to byte[] conversion, big endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    static void l2bBig(long[] in, int inOfs, byte[] out, int outOfs, int len) {
22302
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   440
        if ((inOfs < 0) || ((in.length - inOfs) < len/8) ||
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   441
            (outOfs < 0) || ((out.length - outOfs) < len)) {
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   442
            throw new ArrayIndexOutOfBoundsException();
ce697a6fefa2 8022927: Input validation for byte/endian conversions
valeriep
parents: 12538
diff changeset
   443
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        len += outOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        while (outOfs < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            long i = in[inOfs++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            out[outOfs++] = (byte)(i >> 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            out[outOfs++] = (byte)(i >> 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            out[outOfs++] = (byte)(i >> 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            out[outOfs++] = (byte)(i >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            out[outOfs++] = (byte)(i >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            out[outOfs++] = (byte)(i >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            out[outOfs++] = (byte)(i >>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            out[outOfs++] = (byte)(i      );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
}