#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>
using
namespace
std;
#pragma comment(lib,"ws2_32.lib")
#pragma pack(1)
typedef
struct
{
unsigned
short
flag;
unsigned
short
size;
unsigned
short
cmd;
unsigned
short
seq;
unsigned
int
tickcnt;
unsigned
char
v1;
unsigned
char
valid;
unsigned
char
v2;
unsigned
char
v3;
}EMERSON_PACK_HDR;
#pragma pack()
extern
"C"
__declspec
(
dllexport
) unsigned
short
__cdecl EmersonChecksum(unsigned
__int8
*a1, unsigned
int
a2);
#define _BYTE BYTE
#define BYTE1(x) (x>>8)
unsigned
char
byte_2013A508[512] = {
0x00, 0x00, 0x89, 0x11, 0x12, 0x23, 0x9B, 0x32, 0x24, 0x46, 0xAD, 0x57, 0x36, 0x65, 0xBF, 0x74,
0x48, 0x8C, 0xC1, 0x9D, 0x5A, 0xAF, 0xD3, 0xBE, 0x6C, 0xCA, 0xE5, 0xDB, 0x7E, 0xE9, 0xF7, 0xF8,
0x81, 0x10, 0x08, 0x01, 0x93, 0x33, 0x1A, 0x22, 0xA5, 0x56, 0x2C, 0x47, 0xB7, 0x75, 0x3E, 0x64,
0xC9, 0x9C, 0x40, 0x8D, 0xDB, 0xBF, 0x52, 0xAE, 0xED, 0xDA, 0x64, 0xCB, 0xFF, 0xF9, 0x76, 0xE8,
0x02, 0x21, 0x8B, 0x30, 0x10, 0x02, 0x99, 0x13, 0x26, 0x67, 0xAF, 0x76, 0x34, 0x44, 0xBD, 0x55,
0x4A, 0xAD, 0xC3, 0xBC, 0x58, 0x8E, 0xD1, 0x9F, 0x6E, 0xEB, 0xE7, 0xFA, 0x7C, 0xC8, 0xF5, 0xD9,
0x83, 0x31, 0x0A, 0x20, 0x91, 0x12, 0x18, 0x03, 0xA7, 0x77, 0x2E, 0x66, 0xB5, 0x54, 0x3C, 0x45,
0xCB, 0xBD, 0x42, 0xAC, 0xD9, 0x9E, 0x50,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xE6, 0x9D, 0xD4, 0x14, 0xC5, 0xAB, 0xB1, 0x22, 0xA0, 0xB9, 0x92, 0x30, 0x83,
0xC7, 0x7B, 0x4E, 0x6A, 0xD5, 0x58, 0x5C, 0x49, 0xE3, 0x3D, 0x6A, 0x2C, 0xF1, 0x1E, 0x78, 0x0F
};
unsigned
int
sub_2010CDB0(
DWORD
v2, unsigned
int
a2)
{
unsigned
int
v3;
unsigned
__int16
v4;
unsigned
int
result;
v3 = *(_BYTE *)(v2 + 3) + 16 + v2 + (*(_BYTE *)(v2 + 2) << 8);
v4 = *(_BYTE *)(v2 + 5) + (*(_BYTE *)(v2 + 4) << 8);
if
(v4 != 2 && v4 != 6 && v4 != 14 && v4 != 22 && v4 != 23)
v3 -= 2;
result = a2 >> 8;
*(_BYTE *)v3 = BYTE1(a2);
*(_BYTE *)(v3 + 1) = (
BYTE
)a2;
return
result;
}
unsigned
short
__cdecl EmersonChecksum(unsigned
__int8
*a1, unsigned
int
a2)
{
HMODULE
h = LoadLibraryA(
"D:\\work\\vsproject\\test\\x64\\Release\\EmersonDll.dll"
);
if
(h == 0)
{
MessageBoxA(0,
"error"
,
"error"
, MB_OK);
}
else
{
MessageBoxA(0,
"ok"
,
"ok"
, MB_OK);
}
unsigned
int
v2;
signed
int
result;
unsigned
int
v4;
unsigned
short
* word_2013A508 = (unsigned
short
*)byte_2013A508;
v2 = 0;
result = 0xFFFF;
if
(a2)
{
do
{
v4 = (unsigned
__int8
)result ^ a1[v2++];
result = (unsigned
__int16
)(word_2013A508[v4] ^ ((unsigned
__int16
)result >> 8));
}
while
(v2 < a2);
}
result = ((result & 0xff) << 8) + ((result >>8)&0xff);
return
result;
}
int
__stdcall DllMain(_In_
HINSTANCE
hInstance, _In_
DWORD
fdwReason, _In_
LPVOID
lpvReserved) {
return
true
;
}
void
test_crc() {
int
myresult = 0;
unsigned
char
pack0[] = {
0xfa,0xce,0x00,0x42,0x00,0x02,0x21,0x2e,0x00,0x01,0x38,0xf4,0x54,0x80,0x04,0x00,
0x06,0x01,0x00,0x00,0x00,0x00,0x00,0x29,0x01,0x68,0x00,0xdc,0x00,0x00,0x00,0x00,
0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,0x00,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0b,0x00,0x43,0x00,0x54,0x00,0x4c,
0x00,0x52,0x00,0x2d,0x00,0x33,0x00,0x32,0x00,0x44,0x00,0x44,0x00,0x39,0x00,0x34,
0x00,0x00
};
char
pack[] =
"\xfa\xce\x00\x17\x00\x08\x04\xcb\x00\x00\x00\x00\x00\x80\x00\x00"
\
"\xda\xc5\x00\x01\x01\x01\x00\x0f\x00\x00\x00\x2d\x66\xc2\xfc\x83"
\
"\xe0\xff\xff\x8f\x80\x14\xe8"
;
unsigned
int
crc = EmersonChecksum((unsigned
char
*)pack,
sizeof
(pack) - 1 - 2);
unsigned
int
result = ((crc == 0x14e8) ? 1 : 0);
cout <<
"result:"
<< result << endl;
int
pack0length =
sizeof
(pack0);
crc = EmersonChecksum((unsigned
char
*)pack0, pack0length);
char
pack2[] =
"\xfa\xce\x00\xb4\x00\x0e\x00\x00\xff\xff\x8a\xff\x10\x80\x00\x00"
\
"\x18\x00\x00\x00\xda\xc5\x00\x01\xc7\xf3\x75\xc8\xbb\x73\x97\x41"
\
"\x8e\x57\x88\xe8\x41\x51\x0f\x1a\x06\x06\x00\x00\x01\x02\x01\x00"
\
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a\x00\x00\x00\x04\x00\x22"
\
"\x00\x06\x00\x23\x00\x06\x00\x27\x00\x00\x00\x00\x00\x0f\x00\x44"
\
"\x00\x45\x00\x53\x00\x4b\x00\x54\x00\x4f\x00\x50\x00\x2d\x00\x46"
\
"\x00\x33\x00\x39\x00\x53\x00\x37\x00\x4b\x00\x55\x00\x00\x01\xda"
\
"\xc5\x00\x01\x00\x00\x01\x90\x01\xd7\x52\x0e\x98\x6b\xa4\x50\x01"
\
"\xd7\x52\x0e\x8f\x0b\x17\xe0\x01\xd7\x52\x0e\x8f\x22\xec\xb9\x00"
\
"\x00\x00\x00\x00\x00\x00\x00\x01\xd7\x4e\xe9\x98\x9b\xe8\xe0\x00"
\
"\x00\x0b\xb8\x01\xd7\x52\x0e\x96\xa6\xe9\xe0\x00\x01\xd7\x52\x0e"
\
"\x90\xf1\x6c\xbd\x01\xd7\x52\x0e\x90\xc2\x69\xea\x01\xd7\x4e\xe9"
\
"\x98\x9e\x59\xe0\xfc\xb7"
;
int
pack2size =
sizeof
(pack2) - 2 - 1;
unsigned
int
crc2 = EmersonChecksum((unsigned
char
*)pack2,
sizeof
(pack2) - 1 - 2);
printf
(
"crc:%x\r\n"
, crc2);
return
;
}