Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/ByteArrayAccess.java
41159 views
1
/*
2
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.provider;
27
28
import java.lang.invoke.MethodHandles;
29
import java.lang.invoke.VarHandle;
30
import java.nio.ByteOrder;
31
32
/**
33
* Optimized methods for converting between byte[] and int[]/long[], both for
34
* big endian and little endian byte orders.
35
*
36
* NOTE that ArrayIndexOutOfBoundsException will be thrown if the bounds checks
37
* failed.
38
*
39
* This class may also be helpful in improving the performance of the
40
* crypto code in the SunJCE provider. However, for now it is only accessible by
41
* the message digest implementation in the SUN provider.
42
*
43
* @since 1.6
44
* @author Andreas Sterbenz
45
*/
46
final class ByteArrayAccess {
47
48
private ByteArrayAccess() {
49
// empty
50
}
51
52
static final class LE {
53
static final VarHandle INT_ARRAY
54
= MethodHandles.byteArrayViewVarHandle(int[].class,
55
ByteOrder.LITTLE_ENDIAN).withInvokeExactBehavior();
56
57
static final VarHandle LONG_ARRAY
58
= MethodHandles.byteArrayViewVarHandle(long[].class,
59
ByteOrder.LITTLE_ENDIAN).withInvokeExactBehavior();
60
}
61
62
static final class BE {
63
static final VarHandle INT_ARRAY
64
= MethodHandles.byteArrayViewVarHandle(int[].class,
65
ByteOrder.BIG_ENDIAN).withInvokeExactBehavior();
66
67
static final VarHandle LONG_ARRAY
68
= MethodHandles.byteArrayViewVarHandle(long[].class,
69
ByteOrder.BIG_ENDIAN).withInvokeExactBehavior();
70
}
71
72
/**
73
* int[] to byte[] conversion, little endian byte order.
74
*/
75
static void i2bLittle(int[] in, int inOfs, byte[] out, int outOfs, int len) {
76
len += outOfs;
77
while (outOfs < len) {
78
LE.INT_ARRAY.set(out, outOfs, in[inOfs++]);
79
outOfs += 4;
80
}
81
}
82
83
// Store one 32-bit value into out[outOfs..outOfs+3] in little endian order.
84
static void i2bLittle4(int val, byte[] out, int outOfs) {
85
LE.INT_ARRAY.set(out, outOfs, val);
86
}
87
88
/**
89
* byte[] to int[] conversion, big endian byte order.
90
*/
91
static void b2iBig(byte[] in, int inOfs, int[] out, int outOfs, int len) {
92
len += inOfs;
93
while (inOfs < len) {
94
out[outOfs++] = (int) BE.INT_ARRAY.get(in, inOfs);
95
inOfs += 4;
96
}
97
}
98
99
// Special optimization of b2iBig(in, inOfs, out, 0, 64)
100
static void b2iBig64(byte[] in, int inOfs, int[] out) {
101
out[ 0] = (int) BE.INT_ARRAY.get(in, inOfs );
102
out[ 1] = (int) BE.INT_ARRAY.get(in, inOfs + 4);
103
out[ 2] = (int) BE.INT_ARRAY.get(in, inOfs + 8);
104
out[ 3] = (int) BE.INT_ARRAY.get(in, inOfs + 12);
105
out[ 4] = (int) BE.INT_ARRAY.get(in, inOfs + 16);
106
out[ 5] = (int) BE.INT_ARRAY.get(in, inOfs + 20);
107
out[ 6] = (int) BE.INT_ARRAY.get(in, inOfs + 24);
108
out[ 7] = (int) BE.INT_ARRAY.get(in, inOfs + 28);
109
out[ 8] = (int) BE.INT_ARRAY.get(in, inOfs + 32);
110
out[ 9] = (int) BE.INT_ARRAY.get(in, inOfs + 36);
111
out[10] = (int) BE.INT_ARRAY.get(in, inOfs + 40);
112
out[11] = (int) BE.INT_ARRAY.get(in, inOfs + 44);
113
out[12] = (int) BE.INT_ARRAY.get(in, inOfs + 48);
114
out[13] = (int) BE.INT_ARRAY.get(in, inOfs + 52);
115
out[14] = (int) BE.INT_ARRAY.get(in, inOfs + 56);
116
out[15] = (int) BE.INT_ARRAY.get(in, inOfs + 60);
117
}
118
119
/**
120
* int[] to byte[] conversion, big endian byte order.
121
*/
122
static void i2bBig(int[] in, int inOfs, byte[] out, int outOfs, int len) {
123
len += outOfs;
124
while (outOfs < len) {
125
BE.INT_ARRAY.set(out, outOfs, in[inOfs++]);
126
outOfs += 4;
127
}
128
}
129
130
// Store one 32-bit value into out[outOfs..outOfs+3] in big endian order.
131
static void i2bBig4(int val, byte[] out, int outOfs) {
132
BE.INT_ARRAY.set(out, outOfs, val);
133
}
134
135
/**
136
* byte[] to long[] conversion, big endian byte order.
137
*/
138
static void b2lBig(byte[] in, int inOfs, long[] out, int outOfs, int len) {
139
len += inOfs;
140
while (inOfs < len) {
141
out[outOfs++] = (long) BE.LONG_ARRAY.get(in, inOfs);
142
inOfs += 8;
143
}
144
}
145
146
// Special optimization of b2lBig(in, inOfs, out, 0, 128)
147
static void b2lBig128(byte[] in, int inOfs, long[] out) {
148
out[ 0] = (long) BE.LONG_ARRAY.get(in, inOfs );
149
out[ 1] = (long) BE.LONG_ARRAY.get(in, inOfs + 8);
150
out[ 2] = (long) BE.LONG_ARRAY.get(in, inOfs + 16);
151
out[ 3] = (long) BE.LONG_ARRAY.get(in, inOfs + 24);
152
out[ 4] = (long) BE.LONG_ARRAY.get(in, inOfs + 32);
153
out[ 5] = (long) BE.LONG_ARRAY.get(in, inOfs + 40);
154
out[ 6] = (long) BE.LONG_ARRAY.get(in, inOfs + 48);
155
out[ 7] = (long) BE.LONG_ARRAY.get(in, inOfs + 56);
156
out[ 8] = (long) BE.LONG_ARRAY.get(in, inOfs + 64);
157
out[ 9] = (long) BE.LONG_ARRAY.get(in, inOfs + 72);
158
out[10] = (long) BE.LONG_ARRAY.get(in, inOfs + 80);
159
out[11] = (long) BE.LONG_ARRAY.get(in, inOfs + 88);
160
out[12] = (long) BE.LONG_ARRAY.get(in, inOfs + 96);
161
out[13] = (long) BE.LONG_ARRAY.get(in, inOfs + 104);
162
out[14] = (long) BE.LONG_ARRAY.get(in, inOfs + 112);
163
out[15] = (long) BE.LONG_ARRAY.get(in, inOfs + 120);
164
}
165
166
/**
167
* long[] to byte[] conversion, big endian byte order.
168
*/
169
static void l2bBig(long[] in, int inOfs, byte[] out, int outOfs, int len) {
170
len += outOfs;
171
while (outOfs < len) {
172
BE.LONG_ARRAY.set(out, outOfs, in[inOfs++]);
173
outOfs += 8;
174
}
175
}
176
177
/**
178
* byte[] to long[] conversion, little endian byte order
179
*/
180
static void b2lLittle(byte[] in, int inOfs, long[] out, int outOfs, int len) {
181
len += inOfs;
182
while (inOfs < len) {
183
out[outOfs++] = (long) LE.LONG_ARRAY.get(in, inOfs);
184
inOfs += 8;
185
}
186
}
187
188
189
/**
190
* long[] to byte[] conversion, little endian byte order
191
*/
192
static void l2bLittle(long[] in, int inOfs, byte[] out, int outOfs, int len) {
193
len += outOfs;
194
while (outOfs < len) {
195
LE.LONG_ARRAY.set(out, outOfs, in[inOfs++]);
196
outOfs += 8;
197
}
198
}
199
}
200
201