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