mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-09 22:00:39 +01:00
add message checksumming for android too by making it part of util
(compiled out by default)
This commit is contained in:
parent
dbf156fca7
commit
a522266ce4
10 changed files with 106 additions and 37 deletions
|
@ -10,7 +10,7 @@ local_C_INCLUDES+= \
|
||||||
|
|
||||||
local_LDLIBS += -llog
|
local_LDLIBS += -llog
|
||||||
|
|
||||||
# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING
|
# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING -DCOMMS_CHECKSUM
|
||||||
local_DEFINES += \
|
local_DEFINES += \
|
||||||
$(local_DEBUG) \
|
$(local_DEBUG) \
|
||||||
-DXWFEATURE_RELAY \
|
-DXWFEATURE_RELAY \
|
||||||
|
|
|
@ -370,8 +370,8 @@ parseDict( AndDictionaryCtxt* ctxt, XP_U8 const* ptr, XP_U32 dictLength,
|
||||||
#endif
|
#endif
|
||||||
) {
|
) {
|
||||||
JNIEnv* env = ctxt->env;
|
JNIEnv* env = ctxt->env;
|
||||||
jstring jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name,
|
jstring jsum = and_util_getMD5SumForDict( ctxt->jniutil,
|
||||||
NULL, 0 );
|
ctxt->super.name, NULL, 0 );
|
||||||
XP_UCHAR* md5Sum = NULL;
|
XP_UCHAR* md5Sum = NULL;
|
||||||
/* If we have a cached sum, check that it's correct. */
|
/* If we have a cached sum, check that it's correct. */
|
||||||
if ( NULL != jsum && NULL != ctxt->super.md5Sum ) {
|
if ( NULL != jsum && NULL != ctxt->super.md5Sum ) {
|
||||||
|
@ -385,7 +385,7 @@ parseDict( AndDictionaryCtxt* ctxt, XP_U8 const* ptr, XP_U32 dictLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( NULL == jsum ) {
|
if ( NULL == jsum ) {
|
||||||
jsum = and_util_getMD5SumFor( ctxt->jniutil, ctxt->super.name,
|
jsum = and_util_getMD5SumForDict( ctxt->jniutil, ctxt->super.name,
|
||||||
ptr, end - ptr );
|
ptr, end - ptr );
|
||||||
}
|
}
|
||||||
if ( NULL == md5Sum ) {
|
if ( NULL == md5Sum ) {
|
||||||
|
|
|
@ -93,7 +93,7 @@ and_util_splitFaces( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len,
|
||||||
}
|
}
|
||||||
|
|
||||||
jstring
|
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 )
|
const XP_U8* bytes, jsize len )
|
||||||
{
|
{
|
||||||
JNIEnv* env = *jniutil->envp;
|
JNIEnv* env = *jniutil->envp;
|
||||||
|
@ -107,3 +107,20 @@ and_util_getMD5SumFor( JNIUtilCtxt* jniutil, const XP_UCHAR* name,
|
||||||
deleteLocalRefs( env, jname, jbytes, DELETE_NO_REF );
|
deleteLocalRefs( env, jname, jbytes, DELETE_NO_REF );
|
||||||
return result;
|
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
|
||||||
|
|
|
@ -35,7 +35,12 @@ jobject and_util_makeJBitmap( JNIUtilCtxt* jniu, int nCols, int nRows,
|
||||||
const jboolean* colors );
|
const jboolean* colors );
|
||||||
jobject and_util_splitFaces( JNIUtilCtxt* jniu, const XP_U8* bytes, int len,
|
jobject and_util_splitFaces( JNIUtilCtxt* jniu, const XP_U8* bytes, int len,
|
||||||
XP_Bool isUTF8 );
|
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 );
|
const XP_U8* bytes, jsize len );
|
||||||
|
|
||||||
|
#ifdef COMMS_CHECKSUM
|
||||||
|
jstring and_util_getMD5SumForBytes( JNIUtilCtxt* jniutil, const XP_U8* bytes,
|
||||||
|
jsize len );
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -623,6 +623,22 @@ and_util_engineStopping( XW_UtilCtxt* uc )
|
||||||
}
|
}
|
||||||
#endif
|
#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*
|
XW_UtilCtxt*
|
||||||
makeUtil( MPFORMAL JNIEnv** envp, jobject jutil, CurGameInfo* gi,
|
makeUtil( MPFORMAL JNIEnv** envp, jobject jutil, CurGameInfo* gi,
|
||||||
AndGlobals* closure )
|
AndGlobals* closure )
|
||||||
|
@ -704,6 +720,10 @@ makeUtil( MPFORMAL JNIEnv** envp, jobject jutil, CurGameInfo* gi,
|
||||||
SET_PROC(engineStarting);
|
SET_PROC(engineStarting);
|
||||||
SET_PROC(engineStopping);
|
SET_PROC(engineStopping);
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef COMMS_CHECKSUM
|
||||||
|
SET_PROC(md5sum);
|
||||||
|
#endif
|
||||||
|
|
||||||
#undef SET_PROC
|
#undef SET_PROC
|
||||||
return (XW_UtilCtxt*)util;
|
return (XW_UtilCtxt*)util;
|
||||||
} /* makeUtil */
|
} /* makeUtil */
|
||||||
|
|
|
@ -26,5 +26,6 @@ public interface JNIUtils {
|
||||||
|
|
||||||
// Stuff I can't do in C....
|
// Stuff I can't do in C....
|
||||||
String[][] splitFaces( byte[] chars, boolean isUTF8 );
|
String[][] splitFaces( byte[] chars, boolean isUTF8 );
|
||||||
|
String getMD5SumFor( byte[] bytes );
|
||||||
String getMD5SumFor( String dictName, byte[] bytes );
|
String getMD5SumFor( String dictName, byte[] bytes );
|
||||||
}
|
}
|
|
@ -129,12 +129,8 @@ public class JNIUtilsImpl implements JNIUtils {
|
||||||
faces.add( face.toArray( new String[face.size()] ) );
|
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;
|
byte[] digest = null;
|
||||||
try {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
@ -152,8 +148,16 @@ public class JNIUtilsImpl implements JNIUtils {
|
||||||
} catch ( java.security.NoSuchAlgorithmException nsae ) {
|
} catch ( java.security.NoSuchAlgorithmException nsae ) {
|
||||||
DbgUtils.loge( 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.
|
// Is this needed? Caller might be doing it anyway.
|
||||||
DBUtils.dictsSetMD5Sum( m_context, dictName, result );
|
DBUtils.dictsSetMD5Sum( m_context, dictName, result );
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ typedef struct MsgQueueElem {
|
||||||
#endif
|
#endif
|
||||||
MsgID msgID; /* saved for ease of deletion */
|
MsgID msgID; /* saved for ease of deletion */
|
||||||
#ifdef COMMS_CHECKSUM
|
#ifdef COMMS_CHECKSUM
|
||||||
gchar* checksum;
|
XP_UCHAR* checksum;
|
||||||
#endif
|
#endif
|
||||||
} MsgQueueElem;
|
} MsgQueueElem;
|
||||||
|
|
||||||
|
@ -631,8 +631,7 @@ comms_makeFromStream( MPFORMAL XWStreamCtxt* stream, XW_UtilCtxt* util,
|
||||||
msg->msg = (XP_U8*)XP_MALLOC( mpool, msg->len );
|
msg->msg = (XP_U8*)XP_MALLOC( mpool, msg->len );
|
||||||
stream_getBytes( stream, msg->msg, msg->len );
|
stream_getBytes( stream, msg->msg, msg->len );
|
||||||
#ifdef COMMS_CHECKSUM
|
#ifdef COMMS_CHECKSUM
|
||||||
msg->checksum = g_compute_checksum_for_data( G_CHECKSUM_MD5,
|
msg->checksum = util_md5sum( comms->util, msg->msg, msg->len );
|
||||||
msg->msg, msg->len );
|
|
||||||
#endif
|
#endif
|
||||||
msg->next = (MsgQueueElem*)NULL;
|
msg->next = (MsgQueueElem*)NULL;
|
||||||
*prevsQueueNext = comms->msgQueueTail = msg;
|
*prevsQueueNext = comms->msgQueueTail = msg;
|
||||||
|
@ -1012,8 +1011,7 @@ makeElemWithID( CommsCtxt* comms, MsgID msgID, AddressRecord* rec,
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef COMMS_CHECKSUM
|
#ifdef COMMS_CHECKSUM
|
||||||
newMsgElem->checksum = g_compute_checksum_for_data( G_CHECKSUM_MD5,
|
newMsgElem->checksum = util_md5sum( comms->util, newMsgElem->msg,
|
||||||
newMsgElem->msg,
|
|
||||||
newMsgElem->len );
|
newMsgElem->len );
|
||||||
#endif
|
#endif
|
||||||
return newMsgElem;
|
return newMsgElem;
|
||||||
|
@ -1129,7 +1127,7 @@ freeElem( const CommsCtxt* XP_UNUSED_DBG(comms), MsgQueueElem* elem )
|
||||||
{
|
{
|
||||||
XP_FREE( comms->mpool, elem->msg );
|
XP_FREE( comms->mpool, elem->msg );
|
||||||
#ifdef COMMS_CHECKSUM
|
#ifdef COMMS_CHECKSUM
|
||||||
g_free( elem->checksum );
|
XP_FREE( comms->mpool, elem->checksum );
|
||||||
#endif
|
#endif
|
||||||
XP_FREE( comms->mpool, elem );
|
XP_FREE( comms->mpool, elem );
|
||||||
}
|
}
|
||||||
|
@ -1866,9 +1864,9 @@ comms_checkIncomingStream( CommsCtxt* comms, XWStreamCtxt* stream,
|
||||||
XP_U16 len = stream_getSize( stream );
|
XP_U16 len = stream_getSize( stream );
|
||||||
// stream_getPtr pts at base, but sum excludes relay header
|
// stream_getPtr pts at base, but sum excludes relay header
|
||||||
const XP_U8* ptr = initialLen - len + stream_getPtr( stream );
|
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 );
|
XP_LOGF( "%s: got message of len %d with sum %s", __func__, len, sum );
|
||||||
g_free( sum );
|
XP_FREE( comms->mpool, sum );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -202,6 +202,10 @@ typedef struct UtilVtable {
|
||||||
void (*m_util_engineStopping)( XW_UtilCtxt* uc );
|
void (*m_util_engineStopping)( XW_UtilCtxt* uc );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef COMMS_CHECKSUM
|
||||||
|
XP_UCHAR* (*m_util_md5sum)( XW_UtilCtxt* uc, const XP_U8* ptr, XP_U16 len );
|
||||||
|
#endif
|
||||||
|
|
||||||
} UtilVtable;
|
} UtilVtable;
|
||||||
|
|
||||||
|
|
||||||
|
@ -348,4 +352,8 @@ struct XW_UtilCtxt {
|
||||||
# define util_engineStopping( uc )
|
# define util_engineStopping( uc )
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
#ifdef COMMS_CHECKSUM
|
||||||
|
# define util_md5sum( uc, p, l ) (uc)->vtable->m_util_md5sum((uc), (p), (l))
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -378,6 +378,19 @@ linux_util_deviceRegistered( XW_UtilCtxt* uc, DevIDType typ,
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
void
|
||||||
linux_util_vt_init( MPFORMAL XW_UtilCtxt* util )
|
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_getDevID = linux_util_getDevID;
|
||||||
util->vtable->m_util_deviceRegistered = linux_util_deviceRegistered;
|
util->vtable->m_util_deviceRegistered = linux_util_deviceRegistered;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef COMMS_CHECKSUM
|
||||||
|
util->vtable->m_util_md5sum = linux_util_md5sum;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Add table
Reference in a new issue