From db3c402c9e645aed554a3d0e6e8471ddbd290804 Mon Sep 17 00:00:00 2001 From: leslie Date: Wed, 16 Sep 2009 06:52:37 +0800 Subject: [PATCH] cardclient-camd: fix block receive --- systems/cardclient/camd.c | 42 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/systems/cardclient/camd.c b/systems/cardclient/camd.c index d477ad2..67a2ef5 100644 --- a/systems/cardclient/camd.c +++ b/systems/cardclient/camd.c @@ -485,32 +485,24 @@ bool cCardClientCamd35::SendBlock(struct CmdBlock *cb, int datalen) int cCardClientCamd35::RecvBlock(struct CmdBlock *cb, int maxlen, int to) { unsigned char *m=(unsigned char *)cb; - int n=cCardClient::RecvMsg(m,16+UCSIZE(cb),to); - if(n<=0) { - if(n<0) PRINTF(L_CC_CAMD35,"packet receive failed"); - return n; - } - Decrypt(m+UCSIZE(cb),16); - n=cb->udp_header.len+HDSIZE(cb); - if(n>maxlen) { - PRINTF(L_CC_CAMD35,"received buffer overflow"); - return -1; - } - if(cCardClient::RecvMsg(m+16+UCSIZE(cb),n-(16+UCSIZE(cb)),200)<0) { - PRINTF(L_CC_CAMD35,"short packet received(2)"); - return -1; - } - Decrypt(m+16+UCSIZE(cb),n-(16+UCSIZE(cb))); - if(cb->ucrc!=ucrc) { - PRINTF(L_CC_CAMD35,"wrong ucrc: got %08x, want %08x",cb->ucrc,ucrc); - return -1; - } - if(cb->udp_header.crc!=bswap_32(crc32_le(0,&cb->data[0],cb->udp_header.len))) { - PRINTF(L_CC_CAMD35,"data crc failed"); - return -1; + int n=cCardClient::RecvMsg(m,-maxlen,to); + if(n<=0) return n; + if((unsigned int)n>=HDSIZE(cb)) { + if(cb->ucrc==ucrc) { + Decrypt(m+UCSIZE(cb),n-UCSIZE(cb)); + if((unsigned int)nudp_header.len+HDSIZE(cb)) + PRINTF(L_CC_CAMD35,"packet length doesn't match data length"); + else if(cb->udp_header.crc!=bswap_32(crc32_le(0,&cb->data[0],cb->udp_header.len))) + PRINTF(L_CC_CAMD35,"data crc failed"); + else { + HEXDUMP(L_CC_CAMDEXTR,cb,n,"recv:"); + return n; + } + } + else PRINTF(L_CC_CAMD35,"wrong ucrc: got %08x, want %08x",cb->ucrc,ucrc); } - HEXDUMP(L_CC_CAMDEXTR,cb,n,"recv:"); - return n; + else PRINTF(L_CC_CAMD35,"short packet received"); + return -1; } bool cCardClientCamd35::Init(const char *config) -- 2.39.5