能解释这个小程序吗 谢谢啦
#include <stdio.h>#include <time.h>
#include <string.h>
#define BUFFER_MAX_LEN 65535
#define PACKET_MAX_LEN 400
#define PCAP_HEAD_LEN 24
#define TIME_LEN 8
#define DATA_LEN 4
#define STATE_PCAP_HEAD 0
#define STATE_TIME 1
#define STATE_DATA_LEN_1 2
#define STATE_DATA_LEN_2 3
#define STATE_DATA 4
int main(int argc, char **argv)
{
FILE* fp = NULL;
FILE* wfp = NULL;
unsigned char ctmp;
int res = 0;
unsigned char filename[50] = {0};
unsigned char databuf[2048] = {0};
unsigned char writebuf[BUFFER_MAX_LEN] = {0};
unsigned char ETHeadbuf[100] = {0};
unsigned char* wbuf;
int readCnt = 0;
unsigned char state = STATE_PCAP_HEAD;
unsigned int dataLen1 = 0;
unsigned int dataLen2 = 0;
int lenCnt = 0;
bool flgHeadET = false;
unsigned int packetLen = 0;
if (argc != 2)
return -1;
sprintf((char*)filename,"%s.msu",argv[1]);
wbuf = writebuf;
if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "open file %s error\n", argv[1]);
return -1;
}
if ((wfp = fopen((char*)filename, "w")) == NULL) {
fprintf(stderr, "open file %s error\n", filename);
return -1;
}
readCnt = 0;
while (res != EOF) {
res = fscanf(fp,"%c",&ctmp);
switch(state) {
case STATE_PCAP_HEAD: //数据不做处理
readCnt++;
if (readCnt >= PCAP_HEAD_LEN) {
state = STATE_TIME;
readCnt = 0;
}
break;
case STATE_TIME: //数据不做处理
readCnt++;
if (readCnt >= TIME_LEN) {
state = STATE_DATA_LEN_1;
readCnt = 0;
dataLen1 = 0;
lenCnt = 0;
memset(databuf,0,sizeof(databuf));
}
break;
case STATE_DATA_LEN_1:
readCnt++;
lenCnt++;
if (lenCnt >= DATA_LEN) {
dataLen1 = ntohl(dataLen1);
state = STATE_DATA_LEN_2;
readCnt = 0;
dataLen2 = 0;
lenCnt = 0;
}
else {
dataLen1 = dataLen1 + (ctmp & 0xFF);
dataLen1 <<= 8;
}
break;
case STATE_DATA_LEN_2:
readCnt++;
lenCnt++;
if (lenCnt >= DATA_LEN) {
dataLen2 = ntohl(dataLen2);
state = STATE_DATA;
readCnt = 2;
if (dataLen1 != dataLen2) {
if (dataLen1 > dataLen2)
dataLen1 = dataLen2;
}
databuf[readCnt++] = 0x55;
databuf[readCnt++] = 0xAA;
databuf[readCnt++] = 0x31;
databuf[readCnt++] = 0x32;
databuf[readCnt++] = 0x33;
databuf[readCnt++] = 0x34;
}
else {
dataLen2 = dataLen2 + (ctmp & 0xFF);
dataLen2 <<= 8;
}
break;
case STATE_DATA:
databuf[readCnt++] = ctmp;
if (readCnt > (dataLen1+8)) {
readCnt--;
databuf[1] = ((readCnt & 0xFFFF) >> 8);
databuf[0] = (readCnt & 0xFF);
memcpy(writebuf+8+packetLen,databuf,readCnt);
packetLen += readCnt;
if ((packetLen+8) >= PACKET_MAX_LEN) {
writebuf[0] = 'E';
writebuf[1] = 'T';
writebuf[2] = 192;
writebuf[3] = 168;
writebuf[4] = 17;
writebuf[5] = 51;
writebuf[7] = ((packetLen & 0xFFFF) >> 8);
writebuf[6] = (packetLen & 0xFF);
fwrite(writebuf, packetLen+8, 1, wfp);
fflush(wfp);
packetLen = 0;
}
state = STATE_TIME;
readCnt = 1;
}
break;
default:
break;
}
}
if (packetLen != 0) {
if (readCnt != 0) {
databuf[1] = ((readCnt & 0xFFFF) >> 8);
databuf[0] = (readCnt & 0xFF);
memcpy(writebuf+8+packetLen,databuf,readCnt);
packetLen += readCnt;
}
writebuf[0] = 'E';
writebuf[1] = 'T';
writebuf[2] = 192;
writebuf[3] = 168;
writebuf[4] = 17;
writebuf[5] = 51;
writebuf[7] = ((packetLen & 0xFFFF) >> 8);
writebuf[6] = (packetLen & 0xFF);
fwrite(writebuf, packetLen+8, 1, wfp);
fflush(wfp);
}
fclose(fp);
fclose(wfp);
return 0;
}