int HD8583STRUCT::Unnormalize() { for(int i = 0;i < FIELD_LENGTH;++i) { if(_fields[i].bit_flag) { switch(_fields[i].attribute) { case IFT_BYTE: _fields[i].data[0] = *_fields[i]._bValue; break; case IFT_CHAR: _fields[i].data[0] = *_fields[i]._cValue; break; case IFT_SHORT: SHORT_2_BUF(*_fields[i]._sValue,_fields[i].data); break; case IFT_USHORT: SHORT_2_BUF(*_fields[i]._usValue,_fields[i].data); break; case IFT_INT: INT_2_BUF(*_fields[i]._iValue,_fields[i].data); break; case IFT_UINT: INT_2_BUF(*_fields[i]._uiValue,_fields[i].data); break; case IFT_LONG: LONG_2_BUF(*_fields[i]._lValue,_fields[i].data); break; case IFT_ULONG: LONG_2_BUF(*_fields[i]._ulValue,_fields[i].data); break; case IFT_STRING: memcpy(_fields[i].data,_fields[i]._cValue,_fields[i].length_in_byte); break; case IFT_BUFFER: memcpy(_fields[i].data,_fields[i]._bValue,_fields[i].length_in_byte); break; case IFT_DATETIME: memcpy(_fields[i].data,_fields[i]._bValue,_fields[i].length_in_byte); break; default: return -1; } } } return 0; }
int HDDEV::UnPackResponseStruct(HD8583STRUCT& resp,MESSAGETYPE* msg_type, void* buf,size_t buflen) { short datalen; HDISO8583Msg msg; char * input = (char*)buf; BUF_2_SHORT(datalen,input); if(datalen > buflen - 2) return -1; *msg_type = (MESSAGETYPE)(input[2] & 0xFF); unsigned char zero[2] = {0x00,0x00}; if(msg.Unpack(resp,input+3,datalen-1) > 0) { // 比较 MAC 地址 if(ACE_OS::memcmp(resp->mac,zero,2)) { // 2 byte 长度, 2 byte mac 码 unsigned short i = GenerateCRC16((unsigned char*)input+2,buflen-4); unsigned char tmp[4] = ""; SHORT_2_BUF(i,tmp); /*tmp[0] = (unsigned char)((i & 0xFF00) >> 8); tmp[1] = (unsigned char)(i & 0xFF);*/ tmp[2] = tmp[0]; tmp[0] = tmp[1]; tmp[1] = tmp[2]; for(int i = 0; i < 2; ++i) for(int j = 0; j < 4; ++j) tmp[i] ^= KsgGetGateway()->GetConfig()->_workKey[i+j*2]; return (ACE_OS::memcmp(resp->mac,tmp,2) == 0) ? 0 : -1; } return 0; } return -1; }
size_t HDDEV::PackRequestStruct(HD8583STRUCT& req,MESSAGETYPE msg_type ,void* buf,size_t maxlen,bool with_crc ) { HDISO8583Msg msg; char * output = (char*)buf; char * bitmap = output+3; size_t packlen = msg.Pack(req,output+3,maxlen-3); if(packlen <= 0) return 0; output[2] = (char)msg_type; packlen++; if(with_crc) { bitmap[FIELD_MACOFBASALBITMAP/8] |= 1 << (7 - FIELD_MACOFBASALBITMAP % 8); unsigned short i = GenerateCRC16((unsigned char*)output+2,static_cast<uint16>(packlen)); output[packlen+3] = i % 0x100; output[packlen+2] = i / 0x100; for(i = 0; i < 4; ++i) { output[packlen+2] ^= KsgGetGateway()->GetConfig()->_workKey[i*2]; output[packlen+3] ^= KsgGetGateway()->GetConfig()->_workKey[i*2+1]; } packlen+=2; } short tmp = static_cast<short>(packlen); SHORT_2_BUF(tmp,output); return packlen+2; }
int HDDEV::HDParsePosRecord(BYTE* buf,TRADERECORDSTRUCT* trade) { BUF_2_INT(trade->CardNumber,buf); BUF_2_INT(trade->Balance,buf+4); BUF_2_INT(trade->Amount,buf+8); trade->Year = *(buf+12); trade->Month = *(buf+13); trade->Day = *(buf+14); trade->Hour = *(buf+15); trade->Minute = *(buf+16); trade->Second = *(buf+17); BUF_2_SHORT(trade->TraceOfEW,buf+18); BUF_2_INT(trade->TraceOfPOS,buf+20); BUF_2_INT(trade->OrgTransTrace,buf+24); BUF_2_INT(trade->OperatorID,buf+28); BUF_2_INT(trade->TerminalSN,buf+32); // 终端号 BUF_2_SHORT(trade->AdditionalMoney,buf+36); // 管理费 BUF_2_SHORT(trade->GoodsNumber,buf+38); trade->IndexOfEW = *(buf+40); if(trade->IndexOfEW > 0) trade->IndexOfEW--; trade->RecordType = *(buf+41); trade->ConsumeMode = *(buf+42); memcpy(trade->MAC,buf+43,4); uint16 i = GenerateCRC16(buf,43); unsigned char tmp[4] = ""; SHORT_2_BUF(i,tmp); /*tmp[0] = (unsigned char)((i & 0xFF00) >> 8); tmp[1] = (unsigned char)(i & 0xFF);*/ tmp[2] = tmp[0]; tmp[0] = tmp[1]; tmp[1] = tmp[2]; for(int i = 0; i < 2; ++i) for(int j = 0; j < 4; ++j) tmp[i] ^= KsgGetGateway()->GetConfig()->_workKey[i+j*2]; return (ACE_OS::memcmp(trade->MAC,tmp,2) == 0) ? 0 : -1; /*if( (i & 0xFF) != trade->MAC[1] || ((i & 0xFF00)>>8) != trade->MAC[0]) return -1; return 0; */ }