From 64014944f3fcf71f578afeb160e6d0fd3f95546f Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 13 Jul 2017 07:40:28 -0700 Subject: [PATCH] finish: using java's base64 en/decoder from java Added a wrapper function since for compatibility with the jni's encoding the flags matter and need to be the same everywhere. Or at least there should be no chance of their getting changed. --- .../android/xw4/ConnStatusHandler.java | 5 +-- .../java/org/eehouse/android/xw4/DBUtils.java | 4 +- .../org/eehouse/android/xw4/RelayService.java | 2 +- .../java/org/eehouse/android/xw4/Utils.java | 11 ++++++ .../org/eehouse/android/xw4/WiDirService.java | 4 +- .../org/eehouse/android/xw4/jni/XwJNI.java | 21 ---------- xwords4/android/jni/Android.mk | 1 - xwords4/android/jni/xwjni.c | 38 ------------------- 8 files changed, 18 insertions(+), 68 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConnStatusHandler.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConnStatusHandler.java index 01b2e3455..64c3abf1d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConnStatusHandler.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/ConnStatusHandler.java @@ -356,7 +356,7 @@ public class ConnStatusHandler { R.string.key_connstat_data ); if ( null != as64 && 0 < as64.length() ) { try { - byte[] bytes = XwJNI.base64DecodeJava( as64 ); + byte[] bytes = Utils.base64Decode( as64 ); ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(bytes) ); s_records = @@ -477,8 +477,7 @@ public class ConnStatusHandler { = new ObjectOutputStream( bas ); out.writeObject( s_records ); out.flush(); - String as64 = - XwJNI.base64EncodeJava( bas.toByteArray() ); + String as64 = Utils.base64Encode( bas.toByteArray() ); XWPrefs.setPrefsString( context, R.string.key_connstat_data, as64 ); } catch ( java.io.IOException ioe ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java index 5fba6759c..11f9bd75a 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java @@ -2381,7 +2381,7 @@ public class DBUtils { public static void setBytesFor( Context context, String key, byte[] bytes ) { // DbgUtils.logf( "setBytesFor: writing %d bytes", bytes.length ); - String asStr = XwJNI.base64EncodeJava( bytes ); + String asStr = Utils.base64Encode( bytes ); setStringFor( context, key, asStr ); } @@ -2390,7 +2390,7 @@ public class DBUtils { byte[] bytes = null; String asStr = getStringFor( context, key, null ); if ( null != asStr ) { - bytes = XwJNI.base64DecodeJava( asStr ); + bytes = Utils.base64Decode( asStr ); // DbgUtils.logf( "getBytesFor: loaded %d bytes", bytes.length ); } return bytes; diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java index 7230117c8..740aa945d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java @@ -1345,7 +1345,7 @@ public class RelayService extends XWService byte[][][] msgs = new byte[1][count][]; for ( int ii = 0; ii < count; ++ii ) { - msgs[0][ii] = XwJNI.base64DecodeJava( msgs64[ii] ); + msgs[0][ii] = Utils.base64Decode( msgs64[ii] ); } return msgs; } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java index b15b011cb..fdb4f3e15 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Utils.java @@ -43,6 +43,7 @@ import android.support.v4.app.NotificationCompat; import android.telephony.PhoneNumberUtils; import android.telephony.TelephonyManager; +import android.util.Base64; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -486,6 +487,16 @@ public class Utils { return result; } + public static String base64Encode( byte[] in ) + { + return Base64.encodeToString( in, Base64.NO_WRAP ); + } + + public static byte[] base64Decode( String in ) + { + return Base64.decode( in, Base64.NO_WRAP ); + } + private static void setFirstBootStatics( Context context ) { if ( null == s_isFirstBootThisVersion ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirService.java index 6f147679d..1b482bf92 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/WiDirService.java @@ -319,7 +319,7 @@ public class WiDirService extends XWService { if ( null != wrap ) { XWPacket packet = new XWPacket( XWPacket.CMD.MSG ) .put( KEY_SRC, getMyMacAddress() ) - .put( KEY_DATA, XwJNI.base64EncodeJava( buf ) ) + .put( KEY_DATA, Utils.base64Encode( buf ) ) .put( KEY_GAMEID, gameID ) ; if ( forwarding[0] ) { @@ -746,7 +746,7 @@ public class WiDirService extends XWService { { Log.d( TAG, "handleGotMessage(%s)", intent.toString() ); int gameID = intent.getIntExtra( KEY_GAMEID, 0 ); - byte[] data = XwJNI.base64DecodeJava( intent.getStringExtra( KEY_DATA ) ); + byte[] data = Utils.base64Decode( intent.getStringExtra( KEY_DATA ) ); String macAddress = intent.getStringExtra( KEY_RETADDR ); CommsAddrRec addr = new CommsAddrRec( CommsConnType.COMMS_CONN_P2P ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java index 9fc341de6..3a7c4ba48 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java @@ -21,7 +21,6 @@ package org.eehouse.android.xw4.jni; import android.graphics.Rect; -import android.util.Base64; import java.util.Arrays; @@ -469,26 +468,6 @@ public class XwJNI { String prefix ); public static native String dict_iter_getDesc( int closure ); - // base64 stuff since 2.1 doesn't support it in java - private static native String base64Encode( byte[] in ); - private static native byte[] base64Decode( String in ); - - public static String base64EncodeJava( byte[] in ) - { - String str1 = base64Encode( in ); - String str2 = Base64.encodeToString( in, Base64.NO_WRAP ); - Assert.assertTrue( str2.equals(str1) ); - return str1; - } - - public static byte[] base64DecodeJava( String in ) - { - byte[] b1 = base64Decode( in ); - byte[] b2 = Base64.decode( in, Base64.NO_WRAP ); - Assert.assertTrue( Arrays.equals( b1, b2 ) ); - return b1; - } - // Private methods -- called only here private static native int initGlobals(); private static native void cleanGlobals( int globals ); diff --git a/xwords4/android/jni/Android.mk b/xwords4/android/jni/Android.mk index 014116c1e..6ae652010 100644 --- a/xwords4/android/jni/Android.mk +++ b/xwords4/android/jni/Android.mk @@ -37,7 +37,6 @@ LOCAL_DEFINES += \ -DXWFEATURE_BONUSALL \ -DMAX_ROWS=32 \ -DHASH_STREAM \ - -DXWFEATURE_BASE64 \ -DXWFEATURE_DEVID \ -DXWFEATURE_CHAT \ -DCOMMON_LAYOUT \ diff --git a/xwords4/android/jni/xwjni.c b/xwords4/android/jni/xwjni.c index f36df82e9..6aaf43f31 100644 --- a/xwords4/android/jni/xwjni.c +++ b/xwords4/android/jni/xwjni.c @@ -2346,42 +2346,4 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1getDesc return result; } -#ifdef XWFEATURE_BASE64 -JNIEXPORT jstring JNICALL -Java_org_eehouse_android_xw4_jni_XwJNI_base64Encode -( JNIEnv* env, jclass C, jbyteArray jbytes ) -{ - int inlen = (*env)->GetArrayLength( env, jbytes ); - jbyte* elems = (*env)->GetByteArrayElements( env, jbytes, NULL ); - XP_ASSERT( !!elems ); - - XP_UCHAR out[4+(inlen*4/3)]; - XP_U16 outlen = VSIZE( out ); - binToSms( out, &outlen, (const XP_U8*)elems, inlen ); - - (*env)->ReleaseByteArrayElements( env, jbytes, elems, 0 ); - - jstring result = (*env)->NewStringUTF( env, out ); - return result; -} - -JNIEXPORT jbyteArray JNICALL -Java_org_eehouse_android_xw4_jni_XwJNI_base64Decode -( JNIEnv* env, jclass C, jstring jstr ) -{ - jbyteArray result = NULL; - const char* instr = (*env)->GetStringUTFChars( env, jstr, NULL ); - XP_U16 inlen = (*env)->GetStringUTFLength( env, jstr ); - XP_U8 out[inlen]; - XP_U16 outlen = VSIZE(out); - if ( smsToBin( out, &outlen, instr, inlen ) ) { - result = makeByteArray( env, outlen, (jbyte*)out ); - } else { - XP_ASSERT(0); - } - (*env)->ReleaseStringUTFChars( env, jstr, instr ); - return result; -} -#endif - #endif /* XWFEATURE_BOARDWORDS */