add message checksumming for android too by making it part of util

(compiled out by default)
This commit is contained in:
Eric House 2013-07-10 08:05:47 -07:00
parent dbf156fca7
commit a522266ce4
10 changed files with 106 additions and 37 deletions

View file

@ -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 \

View file

@ -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,7 +385,7 @@ parseDict( AndDictionaryCtxt* ctxt, XP_U8 const* ptr, XP_U32 dictLength,
}
if ( NULL == jsum ) {
jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name,
jsum = and_util_getMD5SumForDict( ctxt->jniutil, ctxt->super.name,
ptr, end - ptr );
}
if ( NULL == md5Sum ) {

View file

@ -93,7 +93,7 @@ and_util_splitFaces( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len,
}
jstring
and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name,
and_util_getMD5SumForDict( JNIUtilCtxt* jniutil, const XP_UCHAR* name,
const XP_U8* bytes, jsize len )
{
JNIEnv* env = *jniutil->envp;
@ -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

View file

@ -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,
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

View file

@ -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 */

View file

@ -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 );
}

View file

@ -129,12 +129,8 @@ public class JNIUtilsImpl implements JNIUtils {
faces.add( face.toArray( new String[face.size()] ) );
}
public String getMD5SumFor( String dictName, byte[] bytes )
public String getMD5SumFor( byte[] bytes )
{
String result = null;
if ( null == bytes ) {
result = DBUtils.dictsGetMD5Sum( m_context, dictName );
} else {
byte[] digest = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
@ -152,8 +148,16 @@ public class JNIUtilsImpl implements JNIUtils {
} catch ( java.security.NoSuchAlgorithmException nsae ) {
DbgUtils.loge( nsae );
}
result = Utils.digestToString( digest );
return Utils.digestToString( digest );
}
public String getMD5SumFor( String dictName, byte[] bytes )
{
String result = null;
if ( null == bytes ) {
result = DBUtils.dictsGetMD5Sum( m_context, dictName );
} else {
result = getMD5SumFor( bytes );
// Is this needed? Caller might be doing it anyway.
DBUtils.dictsSetMD5Sum( m_context, dictName, result );
}

View file

@ -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,8 +1011,7 @@ makeElemWithID( CommsCtxt* comms, MsgID msgID, AddressRecord* rec,
}
#ifdef COMMS_CHECKSUM
newMsgElem->checksum = g_compute_checksum_for_data( G_CHECKSUM_MD5,
newMsgElem->msg,
newMsgElem->checksum = util_md5sum( comms->util, newMsgElem->msg,
newMsgElem->len );
#endif
return newMsgElem;
@ -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

View file

@ -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

View file

@ -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