fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <stdexcept>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <cstring>
  7. #include <boost/algorithm/hex.hpp>
  8. #include <boost/variant.hpp>
  9. #include <boost/variant/apply_visitor.hpp>
  10.  
  11. // Stored VIN
  12. std::string m_vinId {"3030303030"};
  13.  
  14. // Simulated payload
  15. boost::variant<std::string, std::vector<uint8_t>> payload = R"({
  16. "message version": "1.0",
  17. "COmmand Type": "ACTIVE",
  18. "COmmand 1": "5"
  19. })";
  20.  
  21. // Function to verify VIN
  22. bool verifyVIN(const std::string& payload) {
  23. // Find the position of "VIN" in the payload
  24. size_t vinPos = payload.find("\"VIN\"");
  25. if (vinPos == std::string::npos) {
  26. std::cout << "VIN not found in payload" << std::endl;
  27. return false;
  28. }
  29.  
  30. // Extract the VIN value from the payload
  31. size_t startPos = payload.find("\"", vinPos + 5) + 1; // Find starting quote
  32. size_t endPos = payload.find("\"", startPos); // Find ending quote
  33. std::string vinValue = payload.substr(startPos, endPos - startPos);
  34.  
  35. // Check if the VIN value is empty
  36. if (vinValue.empty()) {
  37. std::cout << "VIN is empty" << std::endl;
  38. return false;
  39. }
  40.  
  41. // Check if the received VIN matches the stored VIN
  42. if (vinValue != m_vinId) {
  43. std::cout << "Received VIN does not match stored VIN" << std::endl;
  44. return false;
  45. }
  46.  
  47. std::cout << "VIN is valid and matches stored VIN: " << vinValue << std::endl;
  48. return true;
  49. }
  50.  
  51. // Function to parse the SMS content (simulated payload here)
  52. std::string parseSmscontent(const boost::variant<std::string, std::vector<uint8_t>>& payload) {
  53. std::string message;
  54.  
  55. /* If the payload is a string, assign it to message */
  56. if (const std::string* strPayload = boost::get<std::string>(&payload)) {
  57. message = *strPayload;
  58. } else if (const std::vector<uint8_t>* binaryPayload = boost::get<std::vector<uint8_t>>(&payload)) {
  59. /* Convert binary payload to string if necessary */
  60. message.assign(binaryPayload->begin(), binaryPayload->end());
  61. } else {
  62. std::cout << "Unknown payload type!"<< std::endl;
  63. return "";
  64. }
  65.  
  66. if (!verifyVIN(message)) {
  67. std::cout << "Invalid SMS content. Ignoring..." << std::endl;
  68. return "";
  69. }
  70.  
  71. return message;
  72. }
  73.  
  74. int main() {
  75. std::string message = parseSmscontent(payload);
  76.  
  77. if (message.empty()) {
  78. std::cout << "Invalid SMS content. Ignoring... in main" << std::endl;
  79. } else {
  80. std::cout << "Processed message: " << message << std::endl;
  81. }
  82.  
  83. return 0;
  84. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
VIN not found in payload
Invalid SMS content. Ignoring...
Invalid SMS content. Ignoring... in main