use new hash algorithm for new stream version, old for previous

versions.  Tested upgrade scenario from b44: works.
This commit is contained in:
Eric House 2012-05-28 11:07:59 -07:00
parent b343034cc2
commit adadc9c90c
6 changed files with 71 additions and 33 deletions

View file

@ -47,6 +47,9 @@
#endif
#define MAX_COLS MAX_ROWS
#ifdef HASH_STREAM
# define STREAM_VERS_HASHSTREAM 0x14
#endif
#if MAX_COLS > 16
# define STREAM_VERS_BIGBOARD 0x13
#endif
@ -78,7 +81,9 @@
#define STREAM_VERS_41B4 0x02
#define STREAM_VERS_405 0x01
#if MAX_COLS > 16
#ifdef STREAM_VERS_HASHSTREAM
# define CUR_STREAM_VERS STREAM_VERS_HASHSTREAM
#elif MAX_COLS > 16
# define CUR_STREAM_VERS STREAM_VERS_BIGBOARD
#else
# define CUR_STREAM_VERS STREAM_VERS_BLUETOOTH2

View file

@ -292,10 +292,42 @@ model_destroy( ModelCtxt* model )
} /* model_destroy */
XP_U32
model_getHash( const ModelCtxt* model )
model_getHash( const ModelCtxt* model, XP_U16 version )
{
XP_ASSERT( !!model->vol.stack );
return stack_getHash( model->vol.stack );
StackCtxt* stack = model->vol.stack;
XP_ASSERT( !!stack );
XP_U32 hash =
#ifdef STREAM_VERS_HASHSTREAM
STREAM_VERS_HASHSTREAM <= version ?
stack_getHash( stack ) :
#endif
stack_getHashOld( stack );
/* XP_LOGF( "%s(version=%x)=>%.8X", __func__, version, */
/* (unsigned int)hash ); */
return hash;
}
XP_Bool
model_hashMatches( const ModelCtxt* model, const XP_U32 hash )
{
StackCtxt* stack = model->vol.stack;
XP_U32 localHash = stack_getHash( stack );
XP_Bool matches = XP_FALSE;
if ( localHash == hash ) {
matches = XP_TRUE;
} else {
/* XP_LOGF( "%s: %.8X != %.8X", __func__, (unsigned int)hash, */
/* (unsigned int)localHash ); */
localHash = stack_getHashOld( stack );
if ( localHash == hash ) {
matches = XP_TRUE;
/* } else { */
/* XP_LOGF( "%s: %.8X != %.8X", __func__, (unsigned int)hash, */
/* (unsigned int)localHash ); */
}
}
// LOG_RETURNF( "%d", matches );
return matches;
}
#ifdef STREAM_VERS_BIGBOARD

View file

@ -114,7 +114,8 @@ void model_writeToTextStream( const ModelCtxt* model, XWStreamCtxt* stream );
void model_setSize( ModelCtxt* model, XP_U16 boardSize );
void model_destroy( ModelCtxt* model );
XP_U32 model_getHash( const ModelCtxt* model );
XP_U32 model_getHash( const ModelCtxt* model, XP_U16 version );
XP_Bool model_hashMatches( const ModelCtxt* model, XP_U32 hash );
void model_setNPlayers( ModelCtxt* model, XP_U16 numPlayers );
XP_U16 model_getNPlayers( const ModelCtxt* model );

View file

@ -66,7 +66,6 @@ stack_init( StackCtxt* stack )
shrunk to fit as soon as we serialize/deserialize anyway. */
} /* stack_init */
#ifdef STREAM_VERS_BIGBOARD
static XP_U32
augmentHash( XP_U32 hash, const XP_U8* ptr, XP_U16 len )
{
@ -91,7 +90,6 @@ finishHash( XP_U32 hash )
return hash;
}
#ifndef HASH_STREAM
static XP_U32
augmentFor( XP_U32 hash, const StackEntry* entry )
{
@ -117,22 +115,12 @@ augmentFor( XP_U32 hash, const StackEntry* entry )
}
return hash;
}
#endif
XP_U32
stack_getHash( StackCtxt* stack )
stack_getHashOld( StackCtxt* stack )
{
XP_U32 hash;
#ifdef HASH_STREAM
XP_U16 len = 0;
stream_copyBits( stack->data, 0, stack->top, NULL, &len );
XP_U8 buf[len];
stream_copyBits( stack->data, 0, stack->top, buf, &len );
LOG_HEX( buf, len, __func__ );
hash = augmentHash( 0L, buf, len );
#else
XP_U16 nn, nEntries = stack->nEntries;
hash = 0L;
XP_U32 hash = 0L;
for ( nn = 0; nn < nEntries; ++nn ) {
StackEntry entry;
XP_MEMSET( &entry, 0, sizeof(entry) );
@ -143,12 +131,24 @@ stack_getHash( StackCtxt* stack )
// XP_LOGF( "hash after %d: %.8X", nn, (unsigned int)hash );
}
XP_ASSERT( 0 != hash );
#endif
hash = finishHash( hash );
// LOG_RETURNF( "%s: %.8X", __func__, (unsigned int)hash );
LOG_RETURNF( "%.8X", (unsigned int)hash );
return hash;
} /* stack_getHashOld */
XP_U32
stack_getHash( const StackCtxt* stack )
{
XP_U32 hash;
XP_U16 len = 0;
stream_copyBits( stack->data, 0, stack->top, NULL, &len );
XP_U8 buf[len];
stream_copyBits( stack->data, 0, stack->top, buf, &len );
// LOG_HEX( buf, len, __func__ );
hash = finishHash( augmentHash( 0L, buf, len ) );
LOG_RETURNF( "%.8X", (unsigned int)hash );
return hash;
} /* stack_getHash */
#endif
void
stack_setBitsPerTile( StackCtxt* stack, XP_U16 bitsPerTile )
@ -301,7 +301,7 @@ pushEntry( StackCtxt* stack, const StackEntry* entry )
++stack->nEntries;
stack->highWaterMark = stack->nEntries;
stack->top = stream_setPos( stream, POS_WRITE, oldLoc );
XP_LOGSTREAM( stack->data );
// XP_LOGSTREAM( stack->data );
} /* pushEntry */
static void
@ -474,7 +474,7 @@ stack_popEntry( StackCtxt* stack, StackEntry* entry )
setCacheReadyFor( stack, nn ); /* set cachedPos by side-effect */
stack->top = stack->cachedPos;
}
XP_LOGSTREAM( stack->data );
// XP_LOGSTREAM( stack->data );
return found;
} /* stack_popEntry */

View file

@ -69,7 +69,8 @@ StackCtxt* stack_make( MPFORMAL VTableMgr* vtmgr );
void stack_destroy( StackCtxt* stack );
void stack_init( StackCtxt* stack );
XP_U32 stack_getHash( StackCtxt* stack );
XP_U32 stack_getHashOld( StackCtxt* stack );
XP_U32 stack_getHash( const StackCtxt* stack );
void stack_setBitsPerTile( StackCtxt* stack, XP_U16 bitsPerTile );
void stack_loadFromStream( StackCtxt* stack, XWStreamCtxt* stream );

View file

@ -1906,8 +1906,10 @@ sendMoveTo( ServerCtxt* server, XP_U16 devIndex, XP_U16 turn,
stream = messageStreamWithHeader( server, devIndex, code );
#ifdef STREAM_VERS_BIGBOARD
if ( STREAM_VERS_BIGBOARD <= stream_getVersion( stream ) ) {
XP_U32 hash = model_getHash( server->vol.model );
XP_U16 version = stream_getVersion( stream );
if ( STREAM_VERS_BIGBOARD <= version ) {
XP_ASSERT( version == server->nv.streamVersion );
XP_U32 hash = model_getHash( server->vol.model, version );
// XP_LOGF( "%s: adding hash %x", __func__, (unsigned int)hash );
stream_putU32( stream, hash );
}
@ -1958,12 +1960,9 @@ readMoveInfo( ServerCtxt* server, XWStreamCtxt* stream,
#ifdef STREAM_VERS_BIGBOARD
if ( STREAM_VERS_BIGBOARD <= stream_getVersion( stream ) ) {
XP_U32 hashReceived = stream_getU32( stream );
XP_U32 hashLocal = model_getHash( server->vol.model );
success = hashReceived == hashLocal;
success = model_hashMatches( server->vol.model, hashReceived );
if ( !success ) {
XP_LOGF( "%s: hash mismatch: rcvd %.8X vs local %.8X; dropping move",
__func__, (unsigned int)hashReceived,
(unsigned int)hashLocal );
XP_LOGF( "%s: hash mismatch",__func__);
}
}
#endif
@ -2604,7 +2603,7 @@ writeProto( const ServerCtxt* server, XWStreamCtxt* stream, XW_Proto proto )
XP_ASSERT( server->nv.streamVersion > 0 );
if ( STREAM_SAVE_PREVWORDS < server->nv.streamVersion ) {
stream_putBits( stream, XWPROTO_NBITS, XWPROTO_NEW_PROTO );
stream_putBits( stream, 8, CUR_STREAM_VERS );
stream_putBits( stream, 8, server->nv.streamVersion );
}
stream_setVersion( stream, server->nv.streamVersion );
#else