Advertisement
TShiva

NetworkPacketAnalyzer 0.1

Jun 6th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <fstream>
  2. #include <sstream>
  3. #include <string>
  4. #include <iostream>
  5. #include <vector>
  6.  
  7.  
  8. template <typename T>
  9. T GetBigEndianBytes(char* pData)
  10. {
  11.     T seg = 0;
  12.     for (int i = 0; i < sizeof(T); i++) {
  13.         seg = (seg << 8) + *reinterpret_cast<unsigned char*>(pData);
  14.         pData++;
  15.     }
  16.     return seg;
  17. };
  18.  
  19. template<typename T>
  20. void CuttingByteArray(std::ifstream& data, std::ofstream& output)
  21. {
  22.     std::vector<std::pair<T, T>> blocks{};
  23.     //std::vector<T> blocks{};
  24.  
  25.     int type_size = sizeof(T);
  26.     int counter = 0;
  27.     while (!data.eof())
  28.     {
  29.         char* le = new char[8];
  30.         data.read(le , type_size);
  31.         //data.seekg(counter, std::ios_base::beg);
  32.         auto pair = std::make_pair(*reinterpret_cast<T*>(le), GetBigEndianBytes<T>(le));
  33.         //blocks.push_back(*reinterpret_cast<T*>(le));
  34.         blocks.push_back(pair);
  35.         counter++;
  36.         delete[] le;
  37.     }
  38.  
  39.  
  40.     std::string strBlocks = "";
  41.     for (auto i : blocks)
  42.     {
  43.         strBlocks += "{" + std::to_string(i.first) + "," + std::to_string(i.second) + "}\n";
  44.     //  strBlocks += "{" + std::to_string(i) +"}";
  45.     }
  46.     output << strBlocks;
  47.     //std::cout << strBlocks;
  48. };
  49.  
  50. void CuttingByteArray(std::ifstream& data, std::ofstream& output)
  51. {
  52.     std::vector<uint8_t> blocks{};
  53.     while (!data.eof())
  54.     {
  55.         char* le = new char[1];
  56.         data.read(le,1);
  57.         blocks.push_back(*reinterpret_cast<char*>(le));
  58.         delete[] le;
  59.     }
  60.     std::string strBlocks = "";
  61.     for (auto i : blocks)
  62.     {
  63.         strBlocks += "{" + std::to_string(i) +"}";
  64.     }
  65.     output << strBlocks;
  66. };
  67.  
  68. int main() {
  69.     std::ifstream fileBuffer("C:\\Users\\trixter\\source\\repos\\BinPacketAnalyzer\\BinPacketAnalyzer\\1", std::ios::in | std::ios::binary);
  70.     std::ofstream outputBuffer("Logi.txt", std::ios::out);
  71.     if (!fileBuffer.is_open())
  72.     {
  73.         std::cerr << " Файл не открыт" << std::endl;
  74.     }
  75.    
  76.     CuttingByteArray<uint8_t>(fileBuffer, outputBuffer);
  77.     outputBuffer << "РАЗДЕЛЕНИЕ" << std::endl;
  78.     CuttingByteArray<uint16_t>(fileBuffer, outputBuffer);
  79.  
  80.     outputBuffer.close();
  81.     fileBuffer.close();
  82.     system("PAUSE");
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement