OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 |
| 6 #include "rlz/lib/net_response_check.h" |
| 7 |
| 8 #include "base/strings/string_util.h" |
| 9 #include "rlz/lib/assert.h" |
| 10 #include "rlz/lib/crc32.h" |
| 11 #include "rlz/lib/string_utils.h" |
| 12 |
| 13 // Checksum validation convenience call for RLZ responses. |
| 14 namespace rlz_lib { |
| 15 |
| 16 bool IsPingResponseValid(const char* response, int* checksum_idx) { |
| 17 if (!response || !response[0]) |
| 18 return false; |
| 19 |
| 20 if (checksum_idx) |
| 21 *checksum_idx = -1; |
| 22 |
| 23 if (strlen(response) > kMaxPingResponseLength) { |
| 24 ASSERT_STRING("IsPingResponseValid: response is too long to parse."); |
| 25 return false; |
| 26 } |
| 27 |
| 28 // Find the checksum line. |
| 29 std::string response_string(response); |
| 30 |
| 31 std::string checksum_param("\ncrc32: "); |
| 32 int calculated_crc; |
| 33 int checksum_index = response_string.find(checksum_param); |
| 34 if (checksum_index >= 0) { |
| 35 // Calculate checksum of message preceeding checksum line. |
| 36 // (+ 1 to include the \n) |
| 37 std::string message(response_string.substr(0, checksum_index + 1)); |
| 38 if (!Crc32(message.c_str(), &calculated_crc)) |
| 39 return false; |
| 40 } else { |
| 41 checksum_param = "crc32: "; // Empty response case. |
| 42 if (!base::StartsWith(response_string, checksum_param, |
| 43 base::CompareCase::SENSITIVE)) |
| 44 return false; |
| 45 |
| 46 checksum_index = 0; |
| 47 if (!Crc32("", &calculated_crc)) |
| 48 return false; |
| 49 } |
| 50 |
| 51 // Find the checksum value on the response. |
| 52 int checksum_end = response_string.find("\n", checksum_index + 1); |
| 53 if (checksum_end < 0) |
| 54 checksum_end = response_string.size(); |
| 55 |
| 56 int checksum_begin = checksum_index + checksum_param.size(); |
| 57 std::string checksum = |
| 58 response_string.substr(checksum_begin, checksum_end - checksum_begin + 1); |
| 59 base::TrimWhitespaceASCII(checksum, base::TRIM_ALL, &checksum); |
| 60 |
| 61 if (checksum_idx) |
| 62 *checksum_idx = checksum_index; |
| 63 |
| 64 return calculated_crc == HexStringToInteger(checksum.c_str()); |
| 65 } |
| 66 |
| 67 } // namespace rlz_lib |
OLD | NEW |