lundi 19 juillet 2021

String search in bytes stream

I have below program where I trying to search a substring in const unsigned char * byte

#include <iostream>
#include <string.h>

using namespace std;

int main()
{

const unsigned char bytes[] = { 
        0x49, 0x4e, 0x56, 0x49, 0x54, 0x45, 0x20, 0x73, 0x69, 0x70, 0x3a, 0x61, 0x62, 0x63, 0x40, 0x76
        , 0x6f, 0x64, 0x61, 0x66, 0x6f, 0x6e, 0x65, 0x2e, 0x75, 0x6b, 0x20, 0x53, 0x49, 0x50, 0x2f, 0x32
        , 0x2e, 0x30, 0x0d, 0x0a, 0x56, 0x69, 0x61, 0x3a, 0x20, 0x53, 0x49, 0x50, 0x2f, 0x32, 0x2e, 0x30
        , 0x2f, 0x55, 0x44, 0x50, 0x20, 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x31, 0x38, 0x2e, 0x31, 0x36
        , 0x38, 0x3a, 0x35, 0x30, 0x36, 0x31, 0x3b, 0x62, 0x72, 0x61, 0x6e, 0x63, 0x68, 0x3d, 0x7a, 0x39
        , 0x68, 0x47, 0x34, 0x62, 0x4b, 0x2d, 0x31, 0x31, 0x32, 0x31, 0x38, 0x2d, 0x31, 0x2d, 0x30, 0x0d
        , 0x0a, 0x46, 0x72, 0x6f, 0x6d, 0x3a, 0x20, 0x73, 0x69, 0x70, 0x70, 0x20, 0x3c, 0x73, 0x69, 0x70
        , 0x3a, 0x62, 0x62, 0x40, 0x6a, 0x69, 0x6f, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0x3b, 0x74, 0x61, 0x67
        , 0x3d, 0x31, 0x31, 0x32, 0x31, 0x38, 0x53, 0x49, 0x50, 0x70, 0x54, 0x61, 0x67, 0x30, 0x30, 0x31
        , 0x0d, 0x0a, 0x54, 0x6f, 0x3a, 0x20, 0x73, 0x75, 0x74, 0x20, 0x3c, 0x73, 0x69, 0x70, 0x3a, 0x61
        , 0x62, 0x63, 0x40, 0x76, 0x6f, 0x64, 0x61, 0x66, 0x6f, 0x6e, 0x65, 0x2e, 0x75, 0x6b, 0x3e, 0x0d
        , 0x0a, 0x43, 0x61, 0x6c, 0x6c, 0x2d, 0x49, 0x44, 0x3a, 0x20, 0x31, 0x2d, 0x31, 0x31, 0x32, 0x31
        , 0x38, 0x40, 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x31, 0x38, 0x2e, 0x31, 0x36, 0x38, 0x0d, 0x0a
        , 0x43, 0x53, 0x65, 0x71, 0x3a, 0x20, 0x31, 0x20, 0x49, 0x4e, 0x56, 0x49, 0x54, 0x45, 0x0d, 0x0a
        , 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x3a, 0x20, 0x73, 0x69, 0x70, 0x3a, 0x73, 0x69, 0x70
        , 0x70, 0x40, 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x31, 0x38, 0x2e, 0x31, 0x36, 0x38, 0x3a, 0x35
        , 0x30, 0x36, 0x31, 0x0d, 0x0a, 0x4d, 0x61, 0x78, 0x2d, 0x46, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64
        , 0x73, 0x3a, 0x20, 0x37, 0x30, 0x0d, 0x0a, 0x53, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x3a, 0x20
        , 0x50, 0x65, 0x72, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x63, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74
        , 0x0d, 0x0a, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x3a, 0x20, 0x3c, 0x73, 0x69, 0x70, 0x3a, 0x31, 0x30
        , 0x2e, 0x31, 0x30, 0x2e, 0x31, 0x38, 0x2e, 0x31, 0x35, 0x34, 0x3a, 0x35, 0x30, 0x37, 0x30, 0x3b
        , 0x6c, 0x72, 0x3e, 0x2c, 0x3c, 0x73, 0x69, 0x70, 0x3a, 0x61, 0x69, 0x72, 0x74, 0x65, 0x6c, 0x2e
        , 0x63, 0x6f, 0x6d, 0x3b, 0x6c, 0x72, 0x3e, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
        , 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69
        , 0x6f, 0x6e, 0x2f, 0x53, 0x44, 0x50, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d
        , 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x0d, 0x0a, 0x0d
        , 0x0a
    };

  const unsigned char* byte = bytes; // this is the buffer received from the other layers
 
  //I am trying to convert it to string using reinterpret_cast
  const std::string charStr = reinterpret_cast<const char*>(byte);


  if(!(charStr.find("REGIST")!= std::string::npos)) //the above bytes of character is having this string it should be found in the buffer
  {
     std::cout<<"Not found";
     return;
  }
std::cout<<"found";
return 0;
}

But this may cause core dump in high performance systems and find function is also expensive.

What is the best way to serach a string in const unsigned char* buffer? Let say advance c++ feature not supported.

And what is the best way to solve this problem with old versions of c++(03) and with advance version c++(11)?

Aucun commentaire:

Enregistrer un commentaire