ByteArrayAccess.java 19.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

package sun.security.provider;

import static java.lang.Integer.reverseBytes;
import static java.lang.Long.reverseBytes;

import java.nio.ByteOrder;

import sun.misc.Unsafe;

/**
 * Optimized methods for converting between byte[] and int[]/long[], both for
 * big endian and little endian byte orders.
 *
 * Currently, it includes a default code path plus two optimized code paths.
 * One is for little endian architectures that support full speed int/long
 * access at unaligned addresses (i.e. x86/amd64). The second is for big endian
 * architectures (that only support correctly aligned access), such as SPARC.
 * These are the only platforms we currently support, but other optimized
 * variants could be added as needed.
 *
46 47
 * NOTE that ArrayIndexOutOfBoundsException will be thrown if the bounds checks
 * failed.
D
duke 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 *
 * This class may also be helpful in improving the performance of the
 * crypto code in the SunJCE provider. However, for now it is only accessible by
 * the message digest implementation in the SUN provider.
 *
 * @since   1.6
 * @author  Andreas Sterbenz
 */
final class ByteArrayAccess {

    private ByteArrayAccess() {
        // empty
    }

    private static final Unsafe unsafe = Unsafe.getUnsafe();

    // whether to use the optimized path for little endian platforms that
    // support full speed unaligned memory access.
    private static final boolean littleEndianUnaligned;

    // whether to use the optimzied path for big endian platforms that
    // support only correctly aligned full speed memory access.
    // (Note that on SPARC unaligned memory access is possible, but it is
    // implemented using a software trap and therefore very slow)
    private static final boolean bigEndian;

    private final static int byteArrayOfs = unsafe.arrayBaseOffset(byte[].class);

    static {
        boolean scaleOK = ((unsafe.arrayIndexScale(byte[].class) == 1)
            && (unsafe.arrayIndexScale(int[].class) == 4)
            && (unsafe.arrayIndexScale(long[].class) == 8)
            && ((byteArrayOfs & 3) == 0));

        ByteOrder byteOrder = ByteOrder.nativeOrder();
        littleEndianUnaligned =
            scaleOK && unaligned() && (byteOrder == ByteOrder.LITTLE_ENDIAN);
        bigEndian =
            scaleOK && (byteOrder == ByteOrder.BIG_ENDIAN);
    }

    // Return whether this platform supports full speed int/long memory access
    // at unaligned addresses.
    // This code was copied from java.nio.Bits because there is no equivalent
    // public API.
    private static boolean unaligned() {
        String arch = java.security.AccessController.doPrivileged
            (new sun.security.action.GetPropertyAction("os.arch", ""));
96 97
        return arch.equals("i386") || arch.equals("x86") || arch.equals("amd64")
            || arch.equals("x86_64");
D
duke 已提交
98 99 100 101 102 103
    }

    /**
     * byte[] to int[] conversion, little endian byte order.
     */
    static void b2iLittle(byte[] in, int inOfs, int[] out, int outOfs, int len) {
104 105 106 107
        if ((inOfs < 0) || ((in.length - inOfs) < len) ||
            (outOfs < 0) || ((out.length - outOfs) < len/4)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        if (littleEndianUnaligned) {
            inOfs += byteArrayOfs;
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = unsafe.getInt(in, (long)inOfs);
                inOfs += 4;
            }
        } else if (bigEndian && ((inOfs & 3) == 0)) {
            inOfs += byteArrayOfs;
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = reverseBytes(unsafe.getInt(in, (long)inOfs));
                inOfs += 4;
            }
        } else {
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = ((in[inOfs    ] & 0xff)      )
                              | ((in[inOfs + 1] & 0xff) <<  8)
                              | ((in[inOfs + 2] & 0xff) << 16)
                              | ((in[inOfs + 3]       ) << 24);
                inOfs += 4;
            }
        }
    }

    // Special optimization of b2iLittle(in, inOfs, out, 0, 64)
    static void b2iLittle64(byte[] in, int inOfs, int[] out) {
136 137 138 139
        if ((inOfs < 0) || ((in.length - inOfs) < 64) ||
            (out.length < 16)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        if (littleEndianUnaligned) {
            inOfs += byteArrayOfs;
            out[ 0] = unsafe.getInt(in, (long)(inOfs     ));
            out[ 1] = unsafe.getInt(in, (long)(inOfs +  4));
            out[ 2] = unsafe.getInt(in, (long)(inOfs +  8));
            out[ 3] = unsafe.getInt(in, (long)(inOfs + 12));
            out[ 4] = unsafe.getInt(in, (long)(inOfs + 16));
            out[ 5] = unsafe.getInt(in, (long)(inOfs + 20));
            out[ 6] = unsafe.getInt(in, (long)(inOfs + 24));
            out[ 7] = unsafe.getInt(in, (long)(inOfs + 28));
            out[ 8] = unsafe.getInt(in, (long)(inOfs + 32));
            out[ 9] = unsafe.getInt(in, (long)(inOfs + 36));
            out[10] = unsafe.getInt(in, (long)(inOfs + 40));
            out[11] = unsafe.getInt(in, (long)(inOfs + 44));
            out[12] = unsafe.getInt(in, (long)(inOfs + 48));
            out[13] = unsafe.getInt(in, (long)(inOfs + 52));
            out[14] = unsafe.getInt(in, (long)(inOfs + 56));
            out[15] = unsafe.getInt(in, (long)(inOfs + 60));
        } else if (bigEndian && ((inOfs & 3) == 0)) {
            inOfs += byteArrayOfs;
            out[ 0] = reverseBytes(unsafe.getInt(in, (long)(inOfs     )));
            out[ 1] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  4)));
            out[ 2] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  8)));
            out[ 3] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 12)));
            out[ 4] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 16)));
            out[ 5] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 20)));
            out[ 6] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 24)));
            out[ 7] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 28)));
            out[ 8] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 32)));
            out[ 9] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 36)));
            out[10] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 40)));
            out[11] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 44)));
            out[12] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 48)));
            out[13] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 52)));
            out[14] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 56)));
            out[15] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 60)));
        } else {
            b2iLittle(in, inOfs, out, 0, 64);
        }
    }

    /**
     * int[] to byte[] conversion, little endian byte order.
     */
    static void i2bLittle(int[] in, int inOfs, byte[] out, int outOfs, int len) {
185 186 187 188
        if ((inOfs < 0) || ((in.length - inOfs) < len/4) ||
            (outOfs < 0) || ((out.length - outOfs) < len)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        if (littleEndianUnaligned) {
            outOfs += byteArrayOfs;
            len += outOfs;
            while (outOfs < len) {
                unsafe.putInt(out, (long)outOfs, in[inOfs++]);
                outOfs += 4;
            }
        } else if (bigEndian && ((outOfs & 3) == 0)) {
            outOfs += byteArrayOfs;
            len += outOfs;
            while (outOfs < len) {
                unsafe.putInt(out, (long)outOfs, reverseBytes(in[inOfs++]));
                outOfs += 4;
            }
        } else {
            len += outOfs;
            while (outOfs < len) {
                int i = in[inOfs++];
                out[outOfs++] = (byte)(i      );
                out[outOfs++] = (byte)(i >>  8);
                out[outOfs++] = (byte)(i >> 16);
                out[outOfs++] = (byte)(i >> 24);
            }
        }
    }

    // Store one 32-bit value into out[outOfs..outOfs+3] in little endian order.
    static void i2bLittle4(int val, byte[] out, int outOfs) {
217 218 219
        if ((outOfs < 0) || ((out.length - outOfs) < 4)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        if (littleEndianUnaligned) {
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), val);
        } else if (bigEndian && ((outOfs & 3) == 0)) {
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), reverseBytes(val));
        } else {
            out[outOfs    ] = (byte)(val      );
            out[outOfs + 1] = (byte)(val >>  8);
            out[outOfs + 2] = (byte)(val >> 16);
            out[outOfs + 3] = (byte)(val >> 24);
        }
    }

    /**
     * byte[] to int[] conversion, big endian byte order.
     */
    static void b2iBig(byte[] in, int inOfs, int[] out, int outOfs, int len) {
236 237 238 239
        if ((inOfs < 0) || ((in.length - inOfs) < len) ||
            (outOfs < 0) || ((out.length - outOfs) < len/4)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        if (littleEndianUnaligned) {
            inOfs += byteArrayOfs;
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = reverseBytes(unsafe.getInt(in, (long)inOfs));
                inOfs += 4;
            }
        } else if (bigEndian && ((inOfs & 3) == 0)) {
            inOfs += byteArrayOfs;
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = unsafe.getInt(in, (long)inOfs);
                inOfs += 4;
            }
        } else {
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = ((in[inOfs + 3] & 0xff)      )
                              | ((in[inOfs + 2] & 0xff) <<  8)
                              | ((in[inOfs + 1] & 0xff) << 16)
                              | ((in[inOfs    ]       ) << 24);
                inOfs += 4;
            }
        }
    }

    // Special optimization of b2iBig(in, inOfs, out, 0, 64)
    static void b2iBig64(byte[] in, int inOfs, int[] out) {
268 269 270 271
        if ((inOfs < 0) || ((in.length - inOfs) < 64) ||
            (out.length < 16)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        if (littleEndianUnaligned) {
            inOfs += byteArrayOfs;
            out[ 0] = reverseBytes(unsafe.getInt(in, (long)(inOfs     )));
            out[ 1] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  4)));
            out[ 2] = reverseBytes(unsafe.getInt(in, (long)(inOfs +  8)));
            out[ 3] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 12)));
            out[ 4] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 16)));
            out[ 5] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 20)));
            out[ 6] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 24)));
            out[ 7] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 28)));
            out[ 8] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 32)));
            out[ 9] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 36)));
            out[10] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 40)));
            out[11] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 44)));
            out[12] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 48)));
            out[13] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 52)));
            out[14] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 56)));
            out[15] = reverseBytes(unsafe.getInt(in, (long)(inOfs + 60)));
        } else if (bigEndian && ((inOfs & 3) == 0)) {
            inOfs += byteArrayOfs;
            out[ 0] = unsafe.getInt(in, (long)(inOfs     ));
            out[ 1] = unsafe.getInt(in, (long)(inOfs +  4));
            out[ 2] = unsafe.getInt(in, (long)(inOfs +  8));
            out[ 3] = unsafe.getInt(in, (long)(inOfs + 12));
            out[ 4] = unsafe.getInt(in, (long)(inOfs + 16));
            out[ 5] = unsafe.getInt(in, (long)(inOfs + 20));
            out[ 6] = unsafe.getInt(in, (long)(inOfs + 24));
            out[ 7] = unsafe.getInt(in, (long)(inOfs + 28));
            out[ 8] = unsafe.getInt(in, (long)(inOfs + 32));
            out[ 9] = unsafe.getInt(in, (long)(inOfs + 36));
            out[10] = unsafe.getInt(in, (long)(inOfs + 40));
            out[11] = unsafe.getInt(in, (long)(inOfs + 44));
            out[12] = unsafe.getInt(in, (long)(inOfs + 48));
            out[13] = unsafe.getInt(in, (long)(inOfs + 52));
            out[14] = unsafe.getInt(in, (long)(inOfs + 56));
            out[15] = unsafe.getInt(in, (long)(inOfs + 60));
        } else {
            b2iBig(in, inOfs, out, 0, 64);
        }
    }

    /**
     * int[] to byte[] conversion, big endian byte order.
     */
    static void i2bBig(int[] in, int inOfs, byte[] out, int outOfs, int len) {
317 318 319 320
        if ((inOfs < 0) || ((in.length - inOfs) < len/4) ||
            (outOfs < 0) || ((out.length - outOfs) < len)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        if (littleEndianUnaligned) {
            outOfs += byteArrayOfs;
            len += outOfs;
            while (outOfs < len) {
                unsafe.putInt(out, (long)outOfs, reverseBytes(in[inOfs++]));
                outOfs += 4;
            }
        } else if (bigEndian && ((outOfs & 3) == 0)) {
            outOfs += byteArrayOfs;
            len += outOfs;
            while (outOfs < len) {
                unsafe.putInt(out, (long)outOfs, in[inOfs++]);
                outOfs += 4;
            }
        } else {
            len += outOfs;
            while (outOfs < len) {
                int i = in[inOfs++];
                out[outOfs++] = (byte)(i >> 24);
                out[outOfs++] = (byte)(i >> 16);
                out[outOfs++] = (byte)(i >>  8);
                out[outOfs++] = (byte)(i      );
            }
        }
    }

    // Store one 32-bit value into out[outOfs..outOfs+3] in big endian order.
    static void i2bBig4(int val, byte[] out, int outOfs) {
349 350 351
        if ((outOfs < 0) || ((out.length - outOfs) < 4)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        if (littleEndianUnaligned) {
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), reverseBytes(val));
        } else if (bigEndian && ((outOfs & 3) == 0)) {
            unsafe.putInt(out, (long)(byteArrayOfs + outOfs), val);
        } else {
            out[outOfs    ] = (byte)(val >> 24);
            out[outOfs + 1] = (byte)(val >> 16);
            out[outOfs + 2] = (byte)(val >>  8);
            out[outOfs + 3] = (byte)(val      );
        }
    }

    /**
     * byte[] to long[] conversion, big endian byte order.
     */
    static void b2lBig(byte[] in, int inOfs, long[] out, int outOfs, int len) {
368 369 370 371
        if ((inOfs < 0) || ((in.length - inOfs) < len) ||
            (outOfs < 0) || ((out.length - outOfs) < len/8)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        if (littleEndianUnaligned) {
            inOfs += byteArrayOfs;
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] = reverseBytes(unsafe.getLong(in, (long)inOfs));
                inOfs += 8;
            }
        } else if (bigEndian && ((inOfs & 3) == 0)) {
            // In the current HotSpot memory layout, the first element of a
            // byte[] is only 32-bit aligned, not 64-bit.
            // That means we could use getLong() only for offset 4, 12, etc.,
            // which would rarely occur in practice. Instead, we use an
            // optimization that uses getInt() so that it works for offset 0.
            inOfs += byteArrayOfs;
            len += inOfs;
            while (inOfs < len) {
                out[outOfs++] =
                      ((long)unsafe.getInt(in, (long)inOfs) << 32)
                          | (unsafe.getInt(in, (long)(inOfs + 4)) & 0xffffffffL);
                inOfs += 8;
            }
        } else {
            len += inOfs;
            while (inOfs < len) {
                int i1 = ((in[inOfs + 3] & 0xff)      )
                       | ((in[inOfs + 2] & 0xff) <<  8)
                       | ((in[inOfs + 1] & 0xff) << 16)
                       | ((in[inOfs    ]       ) << 24);
                inOfs += 4;
                int i2 = ((in[inOfs + 3] & 0xff)      )
                       | ((in[inOfs + 2] & 0xff) <<  8)
                       | ((in[inOfs + 1] & 0xff) << 16)
                       | ((in[inOfs    ]       ) << 24);
                out[outOfs++] = ((long)i1 << 32) | (i2 & 0xffffffffL);
                inOfs += 4;
            }
        }
    }

    // Special optimization of b2lBig(in, inOfs, out, 0, 128)
    static void b2lBig128(byte[] in, int inOfs, long[] out) {
413 414 415 416
        if ((inOfs < 0) || ((in.length - inOfs) < 128) ||
            (out.length < 16)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        if (littleEndianUnaligned) {
            inOfs += byteArrayOfs;
            out[ 0] = reverseBytes(unsafe.getLong(in, (long)(inOfs      )));
            out[ 1] = reverseBytes(unsafe.getLong(in, (long)(inOfs +   8)));
            out[ 2] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  16)));
            out[ 3] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  24)));
            out[ 4] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  32)));
            out[ 5] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  40)));
            out[ 6] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  48)));
            out[ 7] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  56)));
            out[ 8] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  64)));
            out[ 9] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  72)));
            out[10] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  80)));
            out[11] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  88)));
            out[12] = reverseBytes(unsafe.getLong(in, (long)(inOfs +  96)));
            out[13] = reverseBytes(unsafe.getLong(in, (long)(inOfs + 104)));
            out[14] = reverseBytes(unsafe.getLong(in, (long)(inOfs + 112)));
            out[15] = reverseBytes(unsafe.getLong(in, (long)(inOfs + 120)));
        } else {
            // no optimization for big endian, see comments in b2lBig
            b2lBig(in, inOfs, out, 0, 128);
        }
    }

    /**
     * long[] to byte[] conversion, big endian byte order.
     */
    static void l2bBig(long[] in, int inOfs, byte[] out, int outOfs, int len) {
445 446 447 448
        if ((inOfs < 0) || ((in.length - inOfs) < len/8) ||
            (outOfs < 0) || ((out.length - outOfs) < len)) {
            throw new ArrayIndexOutOfBoundsException();
        }
D
duke 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462
        len += outOfs;
        while (outOfs < len) {
            long i = in[inOfs++];
            out[outOfs++] = (byte)(i >> 56);
            out[outOfs++] = (byte)(i >> 48);
            out[outOfs++] = (byte)(i >> 40);
            out[outOfs++] = (byte)(i >> 32);
            out[outOfs++] = (byte)(i >> 24);
            out[outOfs++] = (byte)(i >> 16);
            out[outOfs++] = (byte)(i >>  8);
            out[outOfs++] = (byte)(i      );
        }
    }
}