| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 337 人关注过本帖
标题:能解释这个小程序吗 谢谢啦
只看楼主 加入收藏
shuijingshi2
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-26
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:1 
能解释这个小程序吗 谢谢啦
#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;
}

搜索更多相关主题的帖子: 解释 
2010-08-26 17:59
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:5 
有点长 不小

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-08-26 18:02
快速回复:能解释这个小程序吗 谢谢啦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017054 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved