jdk/src/share/classes/sun/jkernel/StandaloneByteArrayAccess.java
changeset 3111 fefdeafb7ab9
child 5506 202f599c92aa
equal deleted inserted replaced
2790:e9771c308d06 3111:fefdeafb7ab9
       
     1 /*
       
     2  * Copyright 2008 - 2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * This is a pure subset of package-private class
       
    28  * sun.security.provider.ByteArrayAccess. The subset consists of only the simple
       
    29  * shift and boolean operations needed for the one current client of this
       
    30  * class (sun.jkernel.StandaloneSHA) and omits optimization code and comments
       
    31  * not relevant to the subset.  No semantic changes have been made.
       
    32  * A few long lines were broken to conform to JDK coding style.
       
    33  * Pete Soper, August, 2007.
       
    34  */
       
    35 
       
    36 package sun.jkernel;
       
    37 
       
    38 /**
       
    39  * Methods for converting between byte[] and int[]/long[].
       
    40  *
       
    41  * @since   1.6
       
    42  * @version 1.1, 05/26/06
       
    43  * @author  Andreas Sterbenz
       
    44  */
       
    45 final class StandaloneByteArrayAccess {
       
    46 
       
    47     private StandaloneByteArrayAccess() {
       
    48         // empty
       
    49     }
       
    50 
       
    51     /**
       
    52      * byte[] to int[] conversion, little endian byte order.
       
    53      */
       
    54     static void b2iLittle(byte[] in, int inOfs, int[] out, int outOfs,
       
    55         int len) {
       
    56         len += inOfs;
       
    57         while (inOfs < len) {
       
    58             out[outOfs++] = ((in[inOfs    ] & 0xff)      )
       
    59                           | ((in[inOfs + 1] & 0xff) <<  8)
       
    60                           | ((in[inOfs + 2] & 0xff) << 16)
       
    61                           | ((in[inOfs + 3]       ) << 24);
       
    62             inOfs += 4;
       
    63         }
       
    64     }
       
    65 
       
    66     /**
       
    67      * int[] to byte[] conversion, little endian byte order.
       
    68      */
       
    69     static void i2bLittle(int[] in, int inOfs, byte[] out, int outOfs,
       
    70         int len) {
       
    71         len += outOfs;
       
    72         while (outOfs < len) {
       
    73             int i = in[inOfs++];
       
    74             out[outOfs++] = (byte)(i      );
       
    75             out[outOfs++] = (byte)(i >>  8);
       
    76             out[outOfs++] = (byte)(i >> 16);
       
    77             out[outOfs++] = (byte)(i >> 24);
       
    78         }
       
    79     }
       
    80 
       
    81     /**
       
    82      * byte[] to int[] conversion, big endian byte order.
       
    83      */
       
    84     static void b2iBig(byte[] in, int inOfs, int[] out, int outOfs, int len) {
       
    85         len += inOfs;
       
    86         while (inOfs < len) {
       
    87             out[outOfs++] = ((in[inOfs + 3] & 0xff)      )
       
    88                           | ((in[inOfs + 2] & 0xff) <<  8)
       
    89                           | ((in[inOfs + 1] & 0xff) << 16)
       
    90                           | ((in[inOfs    ]       ) << 24);
       
    91             inOfs += 4;
       
    92         }
       
    93     }
       
    94 
       
    95     /**
       
    96      * int[] to byte[] conversion, big endian byte order.
       
    97      */
       
    98     static void i2bBig(int[] in, int inOfs, byte[] out, int outOfs, int len) {
       
    99         len += outOfs;
       
   100         while (outOfs < len) {
       
   101             int i = in[inOfs++];
       
   102             out[outOfs++] = (byte)(i >> 24);
       
   103             out[outOfs++] = (byte)(i >> 16);
       
   104             out[outOfs++] = (byte)(i >>  8);
       
   105             out[outOfs++] = (byte)(i      );
       
   106         }
       
   107     }
       
   108 
       
   109     // Store one 32-bit value into out[outOfs..outOfs+3] in big endian order.
       
   110     static void i2bBig4(int val, byte[] out, int outOfs) {
       
   111         out[outOfs    ] = (byte)(val >> 24);
       
   112         out[outOfs + 1] = (byte)(val >> 16);
       
   113         out[outOfs + 2] = (byte)(val >>  8);
       
   114         out[outOfs + 3] = (byte)(val      );
       
   115     }
       
   116 
       
   117     /**
       
   118      * byte[] to long[] conversion, big endian byte order.
       
   119      */
       
   120     static void b2lBig(byte[] in, int inOfs, long[] out, int outOfs, int len) {
       
   121         len += inOfs;
       
   122         while (inOfs < len) {
       
   123             int i1 = ((in[inOfs + 3] & 0xff)      )
       
   124                    | ((in[inOfs + 2] & 0xff) <<  8)
       
   125                    | ((in[inOfs + 1] & 0xff) << 16)
       
   126                    | ((in[inOfs    ]       ) << 24);
       
   127             inOfs += 4;
       
   128             int i2 = ((in[inOfs + 3] & 0xff)      )
       
   129                    | ((in[inOfs + 2] & 0xff) <<  8)
       
   130                    | ((in[inOfs + 1] & 0xff) << 16)
       
   131                    | ((in[inOfs    ]       ) << 24);
       
   132             out[outOfs++] = ((long)i1 << 32) | (i2 & 0xffffffffL);
       
   133             inOfs += 4;
       
   134         }
       
   135     }
       
   136 
       
   137     /**
       
   138      * long[] to byte[] conversion
       
   139      */
       
   140     static void l2bBig(long[] in, int inOfs, byte[] out, int outOfs, int len) {
       
   141         len += outOfs;
       
   142         while (outOfs < len) {
       
   143             long i = in[inOfs++];
       
   144             out[outOfs++] = (byte)(i >> 56);
       
   145             out[outOfs++] = (byte)(i >> 48);
       
   146             out[outOfs++] = (byte)(i >> 40);
       
   147             out[outOfs++] = (byte)(i >> 32);
       
   148             out[outOfs++] = (byte)(i >> 24);
       
   149             out[outOfs++] = (byte)(i >> 16);
       
   150             out[outOfs++] = (byte)(i >>  8);
       
   151             out[outOfs++] = (byte)(i      );
       
   152         }
       
   153     }
       
   154 
       
   155 }