From a522266ce4298847180fbc234cadd6e576878a99 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 10 Jul 2013 08:05:47 -0700 Subject: [PATCH] add message checksumming for android too by making it part of util (compiled out by default) --- xwords4/android/XWords4/jni/Android.mk | 2 +- xwords4/android/XWords4/jni/anddict.c | 8 ++-- xwords4/android/XWords4/jni/jniutlswrapper.c | 21 +++++++++- xwords4/android/XWords4/jni/jniutlswrapper.h | 9 +++- xwords4/android/XWords4/jni/utilwrapper.c | 20 +++++++++ .../org/eehouse/android/xw4/jni/JNIUtils.java | 1 + .../eehouse/android/xw4/jni/JNIUtilsImpl.java | 42 ++++++++++--------- xwords4/common/comms.c | 16 ++++--- xwords4/common/util.h | 8 ++++ xwords4/linux/linuxutl.c | 16 +++++++ 10 files changed, 106 insertions(+), 37 deletions(-) diff --git a/xwords4/android/XWords4/jni/Android.mk b/xwords4/android/XWords4/jni/Android.mk index 2c72a0f92..e71299191 100644 --- a/xwords4/android/XWords4/jni/Android.mk +++ b/xwords4/android/XWords4/jni/Android.mk @@ -10,7 +10,7 @@ local_C_INCLUDES+= \ local_LDLIBS += -llog -# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING +# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING -DCOMMS_CHECKSUM local_DEFINES += \ $(local_DEBUG) \ -DXWFEATURE_RELAY \ diff --git a/xwords4/android/XWords4/jni/anddict.c b/xwords4/android/XWords4/jni/anddict.c index 072f8de29..837f1c8d1 100644 --- a/xwords4/android/XWords4/jni/anddict.c +++ b/xwords4/android/XWords4/jni/anddict.c @@ -370,8 +370,8 @@ parseDict( AndDictionaryCtxt* ctxt, XP_U8 const* ptr, XP_U32 dictLength, #endif ) { JNIEnv* env = ctxt->env; - jstring jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name, - NULL, 0 ); + jstring jsum = and_util_getMD5SumForDict( ctxt->jniutil, + ctxt->super.name, NULL, 0 ); XP_UCHAR* md5Sum = NULL; /* If we have a cached sum, check that it's correct. */ if ( NULL != jsum && NULL != ctxt->super.md5Sum ) { @@ -385,8 +385,8 @@ parseDict( AndDictionaryCtxt* ctxt, XP_U8 const* ptr, XP_U32 dictLength, } if ( NULL == jsum ) { - jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name, - ptr, end - ptr ); + jsum = and_util_getMD5SumForDict( ctxt->jniutil, ctxt->super.name, + ptr, end - ptr ); } if ( NULL == md5Sum ) { md5Sum = getStringCopy( MPPARM(ctxt->super.mpool) env, jsum ); diff --git a/xwords4/android/XWords4/jni/jniutlswrapper.c b/xwords4/android/XWords4/jni/jniutlswrapper.c index 5e5ea9f57..5fa50cae3 100644 --- a/xwords4/android/XWords4/jni/jniutlswrapper.c +++ b/xwords4/android/XWords4/jni/jniutlswrapper.c @@ -93,8 +93,8 @@ and_util_splitFaces( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len, } jstring -and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name, - const XP_U8* bytes, jsize len ) +and_util_getMD5SumForDict( JNIUtilCtxt* jniutil, const XP_UCHAR* name, + const XP_U8* bytes, jsize len ) { JNIEnv* env = *jniutil->envp; jmethodID mid = getMethodID( env, jniutil->jjniutil, "getMD5SumFor", @@ -107,3 +107,20 @@ and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name, deleteLocalRefs( env, jname, jbytes, DELETE_NO_REF ); return result; } + +#ifdef COMMS_CHECKSUM +jstring +and_util_getMD5SumForBytes( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len ) +{ + JNIEnv* env = *jniutil->envp; + jmethodID mid = getMethodID( env, jniutil->jjniutil, "getMD5SumFor", + "([B)Ljava/lang/String;" ); + + jbyteArray jbytes = NULL == bytes? NULL + : makeByteArray( env, len, (jbyte*)bytes ); + jstring result = + (*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jbytes ); + deleteLocalRef( env, jbytes ); + return result; +} +#endif diff --git a/xwords4/android/XWords4/jni/jniutlswrapper.h b/xwords4/android/XWords4/jni/jniutlswrapper.h index dc75abe67..a918ba3ba 100644 --- a/xwords4/android/XWords4/jni/jniutlswrapper.h +++ b/xwords4/android/XWords4/jni/jniutlswrapper.h @@ -35,7 +35,12 @@ jobject and_util_makeJBitmap( JNIUtilCtxt* jniu, int nCols, int nRows, const jboolean* colors ); jobject and_util_splitFaces( JNIUtilCtxt* jniu, const XP_U8* bytes, int len, XP_Bool isUTF8 ); -jstring and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name, - const XP_U8* bytes, jsize len ); +jstring and_util_getMD5SumForDict( JNIUtilCtxt* jniutil, const XP_UCHAR* name, + const XP_U8* bytes, jsize len ); + +#ifdef COMMS_CHECKSUM +jstring and_util_getMD5SumForBytes( JNIUtilCtxt* jniutil, const XP_U8* bytes, + jsize len ); +#endif #endif diff --git a/xwords4/android/XWords4/jni/utilwrapper.c b/xwords4/android/XWords4/jni/utilwrapper.c index d29d610c0..869b8df79 100644 --- a/xwords4/android/XWords4/jni/utilwrapper.c +++ b/xwords4/android/XWords4/jni/utilwrapper.c @@ -623,6 +623,22 @@ and_util_engineStopping( XW_UtilCtxt* uc ) } #endif +#ifdef COMMS_CHECKSUM +static XP_UCHAR* +and_util_md5sum( XW_UtilCtxt* uc, const XP_U8* ptr, XP_U16 len ) +{ + AndUtil* util = (AndUtil*)uc; + JNIEnv* env = *util->env; + AndGlobals* globals = (AndGlobals*)uc->closure; + struct JNIUtilCtxt* jniutil = globals->jniutil; + jstring jsum = and_util_getMD5SumForBytes( jniutil, ptr, len ); + XP_UCHAR* result = getStringCopy( MPPARM(uc->mpool) env, jsum ); + deleteLocalRef( env, jsum ); + return result; +} +#endif + + XW_UtilCtxt* makeUtil( MPFORMAL JNIEnv** envp, jobject jutil, CurGameInfo* gi, AndGlobals* closure ) @@ -704,6 +720,10 @@ makeUtil( MPFORMAL JNIEnv** envp, jobject jutil, CurGameInfo* gi, SET_PROC(engineStarting); SET_PROC(engineStopping); #endif +#ifdef COMMS_CHECKSUM + SET_PROC(md5sum); +#endif + #undef SET_PROC return (XW_UtilCtxt*)util; } /* makeUtil */ diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtils.java index db56f7704..7da31c05d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtils.java @@ -26,5 +26,6 @@ public interface JNIUtils { // Stuff I can't do in C.... String[][] splitFaces( byte[] chars, boolean isUTF8 ); + String getMD5SumFor( byte[] bytes ); String getMD5SumFor( String dictName, byte[] bytes ); } \ No newline at end of file diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtilsImpl.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtilsImpl.java index 64bdab7de..bde3707fd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtilsImpl.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIUtilsImpl.java @@ -129,31 +129,35 @@ public class JNIUtilsImpl implements JNIUtils { faces.add( face.toArray( new String[face.size()] ) ); } + public String getMD5SumFor( byte[] bytes ) + { + byte[] digest = null; + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] buf = new byte[128]; + int nLeft = bytes.length; + int offset = 0; + while ( 0 < nLeft ) { + int len = Math.min( buf.length, nLeft ); + System.arraycopy( bytes, offset, buf, 0, len ); + md.update( buf, 0, len ); + nLeft -= len; + offset += len; + } + digest = md.digest(); + } catch ( java.security.NoSuchAlgorithmException nsae ) { + DbgUtils.loge( nsae ); + } + return Utils.digestToString( digest ); + } + public String getMD5SumFor( String dictName, byte[] bytes ) { String result = null; if ( null == bytes ) { result = DBUtils.dictsGetMD5Sum( m_context, dictName ); } else { - byte[] digest = null; - try { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] buf = new byte[128]; - int nLeft = bytes.length; - int offset = 0; - while ( 0 < nLeft ) { - int len = Math.min( buf.length, nLeft ); - System.arraycopy( bytes, offset, buf, 0, len ); - md.update( buf, 0, len ); - nLeft -= len; - offset += len; - } - digest = md.digest(); - } catch ( java.security.NoSuchAlgorithmException nsae ) { - DbgUtils.loge( nsae ); - } - result = Utils.digestToString( digest ); - + result = getMD5SumFor( bytes ); // Is this needed? Caller might be doing it anyway. DBUtils.dictsSetMD5Sum( m_context, dictName, result ); } diff --git a/xwords4/common/comms.c b/xwords4/common/comms.c index d735becc4..f8f61c4c5 100644 --- a/xwords4/common/comms.c +++ b/xwords4/common/comms.c @@ -60,7 +60,7 @@ typedef struct MsgQueueElem { #endif MsgID msgID; /* saved for ease of deletion */ #ifdef COMMS_CHECKSUM - gchar* checksum; + XP_UCHAR* checksum; #endif } MsgQueueElem; @@ -631,8 +631,7 @@ comms_makeFromStream( MPFORMAL XWStreamCtxt* stream, XW_UtilCtxt* util, msg->msg = (XP_U8*)XP_MALLOC( mpool, msg->len ); stream_getBytes( stream, msg->msg, msg->len ); #ifdef COMMS_CHECKSUM - msg->checksum = g_compute_checksum_for_data( G_CHECKSUM_MD5, - msg->msg, msg->len ); + msg->checksum = util_md5sum( comms->util, msg->msg, msg->len ); #endif msg->next = (MsgQueueElem*)NULL; *prevsQueueNext = comms->msgQueueTail = msg; @@ -1012,9 +1011,8 @@ makeElemWithID( CommsCtxt* comms, MsgID msgID, AddressRecord* rec, } #ifdef COMMS_CHECKSUM - newMsgElem->checksum = g_compute_checksum_for_data( G_CHECKSUM_MD5, - newMsgElem->msg, - newMsgElem->len ); + newMsgElem->checksum = util_md5sum( comms->util, newMsgElem->msg, + newMsgElem->len ); #endif return newMsgElem; } /* makeElemWithID */ @@ -1129,7 +1127,7 @@ freeElem( const CommsCtxt* XP_UNUSED_DBG(comms), MsgQueueElem* elem ) { XP_FREE( comms->mpool, elem->msg ); #ifdef COMMS_CHECKSUM - g_free( elem->checksum ); + XP_FREE( comms->mpool, elem->checksum ); #endif XP_FREE( comms->mpool, elem ); } @@ -1866,9 +1864,9 @@ comms_checkIncomingStream( CommsCtxt* comms, XWStreamCtxt* stream, XP_U16 len = stream_getSize( stream ); // stream_getPtr pts at base, but sum excludes relay header const XP_U8* ptr = initialLen - len + stream_getPtr( stream ); - gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, ptr, len ); + XP_UCHAR* sum = util_md5sum( comms->util, ptr, len ); XP_LOGF( "%s: got message of len %d with sum %s", __func__, len, sum ); - g_free( sum ); + XP_FREE( comms->mpool, sum ); } #endif diff --git a/xwords4/common/util.h b/xwords4/common/util.h index f9866042b..00b7a8e74 100644 --- a/xwords4/common/util.h +++ b/xwords4/common/util.h @@ -202,6 +202,10 @@ typedef struct UtilVtable { void (*m_util_engineStopping)( XW_UtilCtxt* uc ); #endif +#ifdef COMMS_CHECKSUM + XP_UCHAR* (*m_util_md5sum)( XW_UtilCtxt* uc, const XP_U8* ptr, XP_U16 len ); +#endif + } UtilVtable; @@ -348,4 +352,8 @@ struct XW_UtilCtxt { # define util_engineStopping( uc ) # endif +#ifdef COMMS_CHECKSUM +# define util_md5sum( uc, p, l ) (uc)->vtable->m_util_md5sum((uc), (p), (l)) +#endif + #endif diff --git a/xwords4/linux/linuxutl.c b/xwords4/linux/linuxutl.c index 3da49dde6..d678d6d68 100644 --- a/xwords4/linux/linuxutl.c +++ b/xwords4/linux/linuxutl.c @@ -378,6 +378,19 @@ linux_util_deviceRegistered( XW_UtilCtxt* uc, DevIDType typ, } #endif +#ifdef COMMS_CHECKSUM +static XP_UCHAR* +linux_util_md5sum( XW_UtilCtxt* uc, const XP_U8* ptr, XP_U16 len ) +{ + gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, ptr, len ); + XP_U16 sumlen = 1 + strlen( sum ); + XP_UCHAR* result = XP_MALLOC( uc->mpool, sumlen ); + XP_MEMCPY( result, sum, sumlen ); + g_free( sum ); + return result; +} +#endif + void linux_util_vt_init( MPFORMAL XW_UtilCtxt* util ) { @@ -394,6 +407,9 @@ linux_util_vt_init( MPFORMAL XW_UtilCtxt* util ) util->vtable->m_util_getDevID = linux_util_getDevID; util->vtable->m_util_deviceRegistered = linux_util_deviceRegistered; #endif +#ifdef COMMS_CHECKSUM + util->vtable->m_util_md5sum = linux_util_md5sum; +#endif } void