24package org.cipherpack;
27import java.nio.ByteBuffer;
28import java.nio.ByteOrder;
29import java.nio.IntBuffer;
30import java.nio.ShortBuffer;
31import java.security.PrivilegedAction;
32import java.util.ArrayList;
33import java.util.Iterator;
35import java.util.StringTokenizer;
40final class PlatformToolkit {
42 UNIX, MACOS,
IOS, WINDOWS;
110 this.is32Bit = is32Bit;
118 if(
null == other ) {
120 }
else if( other ==
this ) {
123 return this.family == other.family &&
129 if(
null == cpuABILower ) {
130 throw new IllegalArgumentException(
"Null cpuABILower arg");
132 if( cpuABILower.equals(
"x86") ||
133 cpuABILower.equals(
"i386") ||
134 cpuABILower.equals(
"i486") ||
135 cpuABILower.equals(
"i586") ||
136 cpuABILower.equals(
"i686") ) {
138 }
else if( cpuABILower.equals(
"x86_64") ||
139 cpuABILower.equals(
"amd64") ) {
141 }
else if( cpuABILower.equals(
"ia64") ) {
143 }
else if( cpuABILower.equals(
"aarch64") ) {
145 }
else if( cpuABILower.startsWith(
"arm") ) {
146 if( cpuABILower.equals(
"armv8-a") ||
147 cpuABILower.equals(
"arm-v8-a") ||
148 cpuABILower.equals(
"arm-8-a") ||
149 cpuABILower.equals(
"arm64-v8a") ) {
151 }
else if( cpuABILower.startsWith(
"arm64") ) {
153 }
else if( cpuABILower.startsWith(
"armv7") ||
154 cpuABILower.startsWith(
"arm-v7") ||
155 cpuABILower.startsWith(
"arm-7") ||
156 cpuABILower.startsWith(
"armeabi-v7") ) {
158 }
else if( cpuABILower.startsWith(
"armv5") ||
159 cpuABILower.startsWith(
"arm-v5") ||
160 cpuABILower.startsWith(
"arm-5") ) {
162 }
else if( cpuABILower.startsWith(
"armv6") ||
163 cpuABILower.startsWith(
"arm-v6") ||
164 cpuABILower.startsWith(
"arm-6") ) {
169 }
else if( cpuABILower.equals(
"sparcv9") ) {
171 }
else if( cpuABILower.equals(
"sparc") ) {
173 }
else if( cpuABILower.equals(
"pa_risc2.0") ) {
175 }
else if( cpuABILower.startsWith(
"ppc64") ) {
177 }
else if( cpuABILower.startsWith(
"ppc") ) {
179 }
else if( cpuABILower.startsWith(
"mips64") ) {
181 }
else if( cpuABILower.startsWith(
"mips") ) {
183 }
else if( cpuABILower.startsWith(
"superh") ) {
186 throw new RuntimeException(
"Please port CPUType detection to your platform (CPU_ABI string '" + cpuABILower +
"')");
210 if(
null == other ) {
213 return other ==
this;
218 if(
null == cpuType ) {
219 throw new IllegalArgumentException(
"Null cpuType");
220 }
else if(
null == cpuABILower ) {
221 throw new IllegalArgumentException(
"Null cpuABILower");
226 return EABI_GNU_ARMHF;
234 static final String os_name;
236 static final String os_arch;
237 private static final String user_dir;
238 private static final String java_user_lib_path;
239 private static final String java_boot_lib_path;
244 static final String os_and_arch;
245 static final OSType OS_TYPE;
246 private static final boolean isOSX;
248 private static final String prefix;
249 private static final String suffix;
253 final String[] props =
256 public String[] run() {
257 final String[] props =
new String[5];
259 props[i++] = System.getProperty(
"os.name").toLowerCase();
260 props[i++] = System.getProperty(
"os.arch").toLowerCase();
261 props[i++] = System.getProperty(
"user.dir");
262 props[i++] = System.getProperty(
"java.library.path");
263 props[i++] = System.getProperty(
"sun.boot.library.path");
268 os_name = props[i++];
269 final String _os_arch1 = props[i++];
270 user_dir = props[i++];
271 java_user_lib_path = props[i++];
272 java_boot_lib_path = props[i++];
274 final boolean LITTLE_ENDIAN = queryIsLittleEndianImpl();
275 final CPUType CPU_TYPE = CPUType.query(_os_arch1);
276 final ABIType ABI_TYPE = ABIType.query(CPU_TYPE, _os_arch1);
277 final String _os_arch2 = getArchName(CPU_TYPE, ABI_TYPE, LITTLE_ENDIAN);
278 os_arch =
null != _os_arch2 ? _os_arch2 : _os_arch1;
279 os_and_arch = os_name+
"-"+os_arch;
280 if( CPFactory.DEBUG ) {
281 System.err.println(
"PlatformToolkit: os_name "+os_name+
", os_arch ("+_os_arch1+
" -> "+_os_arch2+
" ->) "+os_arch+
" (final), "+
282 "CPU_TYPE "+CPU_TYPE+
", ABI_TYPE "+ABI_TYPE+
", LITTLE_ENDIAN "+LITTLE_ENDIAN);
286 if ( os_name.startsWith(
"mac os x") ||
287 os_name.startsWith(
"darwin") ) {
288 OS_TYPE = OSType.MACOS;
290 }
else if ( os_name.startsWith(
"ios") ) {
291 OS_TYPE = OSType.IOS;
293 }
else if ( os_name.startsWith(
"windows") ) {
294 OS_TYPE = OSType.WINDOWS;
297 OS_TYPE = OSType.UNIX;
321 private static final boolean queryIsLittleEndianImpl() {
322 final ByteBuffer tst_b = ByteBuffer.allocateDirect(4 ).order(ByteOrder.nativeOrder());
323 final IntBuffer tst_i = tst_b.asIntBuffer();
324 final ShortBuffer tst_s = tst_b.asShortBuffer();
325 tst_i.put(0, 0x0A0B0C0D);
326 return 0x0C0D == tst_s.get(0);
328 private static final String getArchName(
final CPUType cpuType,
final ABIType abiType,
final boolean littleEndian) {
346 return littleEndian ?
"mipsel" :
"mips";
355 return littleEndian ?
"ppc64le" :
"ppc64";
403 private static final List<String> enumerateLibraryPaths(
final String libBaseName,
404 final boolean searchSystemPath,
405 final boolean searchSystemPathFirst,
406 final ClassLoader loader) {
407 final List<String> paths =
new ArrayList<String>();
409 if ( libBaseName ==
null || libBaseName.length() == 0 ) {
413 final String libPlatformName = getPlatformName(libBaseName);
415 if( searchSystemPath && searchSystemPathFirst ) {
419 addPath(
"osx-1",
"/Library/Frameworks/" + libBaseName +
".framework", libPlatformName, paths);
421 addPath(
"osx-2",
"/System/Library/Frameworks/" + libBaseName +
".framework", libPlatformName, paths);
423 addMultiPaths(
"java-boot-libpath", java_boot_lib_path, libPlatformName, paths);
426 addPath(
"cwd.os_and_arch", user_dir+File.separator+os_and_arch, libPlatformName, paths);
427 addMultiPaths2(
"java-user-libpath.os_and_arch", java_user_lib_path, os_and_arch, libPlatformName, paths);
429 addPath(
"cwd", user_dir, libPlatformName, paths);
430 addMultiPaths(
"java-user-libpath", java_user_lib_path, libPlatformName, paths);
432 if( searchSystemPath && !searchSystemPathFirst ) {
436 addPath(
"osx-1",
"/Library/Frameworks/" + libBaseName +
".Framework", libPlatformName, paths);
438 addPath(
"osx-2",
"/System/Library/Frameworks/" + libBaseName +
".Framework", libPlatformName, paths);
440 addMultiPaths(
"java-boot-libpath", java_boot_lib_path, libPlatformName, paths);
447 private static final String getPlatformName(
final String libBaseName) {
448 return prefix + libBaseName + suffix;
450 private static final String getCanonicalPath(
final String path) {
451 return CPFactory.doPrivileged(
new PrivilegedAction<String>() {
453 public String run() {
455 final File f =
new File(path);
457 return f.getCanonicalPath();
458 }
catch (
final Throwable t) {
459 if( CPFactory.DEBUG ) {
460 System.err.println(
"getAbsolutePath("+path+
") failed: "+t.getMessage());
466 private static final void addPath(
final String msg,
final String path,
final String platformName,
final List<String> paths) {
467 if(
null != path && path.length() > 0 ) {
468 final String fullpath = path + File.separator + platformName;
469 final String abspath = getCanonicalPath(fullpath);
470 if(
null != abspath ) {
471 final boolean isDup = paths.contains(abspath);
472 if( CPFactory.DEBUG ) {
473 System.err.println(
" "+abspath+
" (addPath "+msg+
", dropped duplicate "+isDup+
")");
481 private static final void addMultiPaths(
final String msg,
final String pathList,
final String platformName,
final List<String> paths) {
482 if(
null != pathList && pathList.length() > 0 ) {
483 final StringTokenizer tokenizer =
new StringTokenizer(pathList, File.pathSeparator);
485 while (tokenizer.hasMoreTokens()) {
486 addPath(msg+
":"+i, tokenizer.nextToken(), platformName, paths);
491 private static final void addMultiPaths2(
final String msg,
final String pathList,
final String subDir,
final String platformName,
final List<String> paths) {
492 if(
null != pathList && pathList.length() > 0 &&
null != subDir && subDir.length() > 0 ) {
493 final StringTokenizer tokenizer =
new StringTokenizer(pathList, File.pathSeparator);
495 while (tokenizer.hasMoreTokens()) {
496 final String path = tokenizer.nextToken() + File.separator + subDir;
497 addPath(msg+
":"+i, path, platformName, paths);
542 static boolean loadLibrary(
final String libBaseName,
final ClassLoader cl,
final Throwable[] t) {
543 if( CPFactory.DEBUG ) {
544 System.err.println();
545 System.err.println(
"PlatformToolkit.loadLibrary: libBaseName "+libBaseName+
":");
547 final List<String> possiblePaths = enumerateLibraryPaths(libBaseName,
true ,
false , cl);
548 if( CPFactory.DEBUG ) {
549 System.err.println();
553 for (
final Iterator<String> iter = possiblePaths.iterator(); iter.hasNext(); ) {
554 final String path = iter.next();
557 if( CPFactory.DEBUG ) {
558 System.err.println(
" "+path+
" success");
561 }
catch (
final Throwable t0) {
562 if( CPFactory.DEBUG ) {
563 System.err.println(
" "+path+
" failed: "+t0.getMessage());
571 System.loadLibrary(libBaseName);
572 if( CPFactory.DEBUG ) {
573 System.err.println(
" "+libBaseName+
" success");
576 }
catch (
final Throwable t0) {
577 if( CPFactory.DEBUG ) {
578 System.err.println(
" "+libBaseName+
" failed: "+t0.getMessage());
Cipherpack Factory, called by automatically to load the native library.
static< T > T doPrivileged(final PrivilegedAction< T > o)
Deprecated call to java.security.AccessController#doPrivileged(PrivilegedAction) w/o warnings.