簡體   English   中英

c ++ / c訪問具有不同名稱和大小的多個char數組

[英]c++/c access multiple char arrays with different names and sizes

我正在分析一個pcap文件,我在Wireshark中導出了一個解剖為c Arrays,我需要從有問題的字節中提取一些數據。 但是我不知道如何訪問所有這些數組。 它們看起來像這樣:

/* Frame (73 bytes) */
static const unsigned char pkt1324[73] = {
0x80, 0xe6, 0x50, 0x06, 0xe7, 0xae, 0x48, 0xfd, /* ..P...H. */
0x8e, 0xdf, 0x2f, 0x06, 0x86, 0xdd, 0x60, 0x00, /* ../...`. */
0x00, 0x00, 0x00, 0x13, 0x11, 0x30, 0x20, 0x01, /* .....0 . */
0x06, 0x60, 0x32, 0x07, 0x04, 0xc0, 0x00, 0x00, /* .`2..... */
0x00, 0x00, 0x00, 0x00, 0x40, 0x61, 0x20, 0x01, /* ....@a . */
0x08, 0x18, 0xdb, 0xf8, 0x70, 0x00, 0xcd, 0x3e, /* ....p..> */
0x83, 0xa5, 0x98, 0x71, 0x9b, 0x42, 0x16, 0x33, /* ...q.B.3 */
0xe8, 0xeb, 0x00, 0x13, 0x96, 0xfa, 0x50, 0x45, /* ......PE */
0xea, 0x50, 0x41, 0x0a, 0x21, 0xa8, 0xff, 0x31, /* .PA.!..1 */
0x37                                            /* 7 */
};
this is an empty line
/* Frame (84 bytes) */-> next frame

我的問題是,這些數組是在.c / .h文件中,我想訪問所有數組以提取一些數據,但它們的名稱和大小會發生變化。

知道我需要讀取幾百個數組並提取某些字節的最佳方法是什么?

您可以使用這樣的工具: https//github.com/seladb/PcapPlusPlus PcapPlusPlus是一個多平台C ++網絡嗅探和數據包解析和制作框架。 PcapPlusPlus意味着輕巧,高效且易於使用。 它是流行引擎的C ++包裝器,如libpcap,WinPcap,DPDK和PF_RING http://seladb.github.io/PcapPlusPlus-Doc

您可以使用在c++11添加的regex來解析文件

// g++ --std=c++11

#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>
#include <string>


class Array
{
    std::string m_Name;
    size_t m_Size;

public:
    Array() = delete;

    Array(const std::string &name, std::string size)
    {
        this->m_Name = name;
        this->m_Size = static_cast<size_t>(stoi(size));
    }

    const std::string &GetName() const { return m_Name; }
    size_t GetSize() const { return m_Size; }
};


std::string readFile(const std::string &path)
{
    std::ifstream fileStream(path);
    std::stringstream buffer;

    std::string line;

    while (std::getline(fileStream, line))
        buffer << line << std::endl;

    return buffer.str();
}


void writeFile(const std::string &path, const std::string &data)
{
    std::ofstream fileStream(path);

    fileStream << data;
}


std::vector<Array> parseData(const std::string &data)
{
    std::regex reg("static const unsigned char (pkt\\d+)\\[(\\d+)\\]");

    auto begin = std::sregex_iterator(data.begin(), data.end(), reg);
    auto end = std::sregex_iterator();

    std::vector<Array> arrays;

    for (std::sregex_iterator i = begin; i != end; i++)
    {
        std::smatch match = *i;
        std::string name = match[1];
        std::string size = match[2];

        arrays.push_back(Array(name, size));
    }

    return arrays;
}


int main()
{
    std::string pktPath = "a.pkt";
    std::string data = readFile(pktPath);

    std::vector<Array> arrays = parseData(data);

    std::stringstream names;
    std::stringstream sizes;

    names << "const unsigned char *names[] = {";
    sizes << "size_t sizes[] = {";

    for (const Array &arr : arrays)
    {
        names << arr.GetName() << ",";
        sizes << arr.GetSize() << ",";
    }

    names << "};";
    sizes << "};";

    std::stringstream headerStream;
    headerStream << "#include <cinttypes>" << std::endl;
    headerStream << "#include \"" << pktPath << "\"" << std::endl << std::endl;
    headerStream << "size_t sizeOfArrays = " << arrays.size() << ";" << std::endl;
    headerStream << names.str() << std::endl;
    headerStream << sizes.str() << std::endl;

    std::string header = headerStream.str();
    std::string headerPath = pktPath + ".h";
    writeFile(headerPath, header);
}

此代碼創建一個名為a.pkt.h新文件,其代碼如下:

#include <cinttypes>
#include "a.pkt"

size_t sizeOfArrays = 1;
const unsigned char *names[] = {pkt1324,};
size_t sizes[] = {73,};

現在您已經解析了.pkt文件並獲得了標題,其中包含了所有數組和大小。

#include "a.pkt.h"


int main()
{
    for (size_t i = 0; i < sizeOfArrays; i++)
    {
        const unsigned char *array = names[i];
        size_t size = sizes[i];

        doSomething(array, size);
    }
}

如果您對我的代碼有任何疑問,請對其進行評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM