| OLD | NEW |
| 1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 wrapped.append(kChecksumPrefix, kChecksumPrefixLength); | 98 wrapped.append(kChecksumPrefix, kChecksumPrefixLength); |
| 99 wrapped.append(MD5String(data)); | 99 wrapped.append(MD5String(data)); |
| 100 wrapped.push_back(kSeparator); | 100 wrapped.push_back(kSeparator); |
| 101 wrapped.append(data); | 101 wrapped.append(data); |
| 102 | 102 |
| 103 return wrapped; | 103 return wrapped; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // static | 106 // static |
| 107 bool ValidatingUtil::UnwrapTimestamp(std::string* data, time_t now) { | 107 ValidatingUtil::TimestampStatus ValidatingUtil::UnwrapTimestamp( |
| 108 std::string* data, |
| 109 time_t now) { |
| 108 assert(data != NULL); | 110 assert(data != NULL); |
| 109 if (now < 0) { | 111 if (now < 0) { |
| 110 return false; | 112 return TIMESTAMP_INVALID; |
| 111 } | 113 } |
| 112 | 114 |
| 113 std::string timestamp_string; | 115 std::string timestamp_string; |
| 114 if (!UnwrapHeader( | 116 if (!UnwrapHeader( |
| 115 kTimestampPrefix, kTimestampPrefixLength, data, ×tamp_string)) { | 117 kTimestampPrefix, kTimestampPrefixLength, data, ×tamp_string)) { |
| 116 return false; | 118 return TIMESTAMP_INVALID; |
| 117 } | 119 } |
| 118 | 120 |
| 119 time_t timestamp = atol(timestamp_string.c_str()); | 121 time_t timestamp = atol(timestamp_string.c_str()); |
| 120 if (timestamp < 0) { | 122 if (timestamp < 0) { |
| 121 return false; | 123 return TIMESTAMP_INVALID; |
| 124 } |
| 125 |
| 126 double age_in_seconds = difftime(now, timestamp); |
| 127 if (age_in_seconds < 0.0) { |
| 128 return TIMESTAMP_INVALID; |
| 122 } | 129 } |
| 123 | 130 |
| 124 // One month contains: | 131 // One month contains: |
| 125 // 30 days * | 132 // 30 days * |
| 126 // 24 hours per day * | 133 // 24 hours per day * |
| 127 // 60 minutes per hour * | 134 // 60 minutes per hour * |
| 128 // 60 seconds per minute. | 135 // 60 seconds per minute. |
| 129 static const double kOneMonthInSeconds = 30.0 * 24.0 * 60.0 * 60.0; | 136 static const double kOneMonthInSeconds = 30.0 * 24.0 * 60.0 * 60.0; |
| 130 double age_in_seconds = difftime(now, timestamp); | 137 return age_in_seconds < kOneMonthInSeconds ? TIMESTAMP_VALID |
| 131 return !(age_in_seconds < 0.0) && age_in_seconds < kOneMonthInSeconds; | 138 : TIMESTAMP_STALE; |
| 132 } | 139 } |
| 133 | 140 |
| 134 // static | 141 // static |
| 135 bool ValidatingUtil::UnwrapChecksum(std::string* data) { | 142 bool ValidatingUtil::UnwrapChecksum(std::string* data) { |
| 136 assert(data != NULL); | 143 assert(data != NULL); |
| 137 std::string checksum; | 144 std::string checksum; |
| 138 if (!UnwrapHeader(kChecksumPrefix, kChecksumPrefixLength, data, &checksum)) { | 145 if (!UnwrapHeader(kChecksumPrefix, kChecksumPrefixLength, data, &checksum)) { |
| 139 return false; | 146 return false; |
| 140 } | 147 } |
| 141 return checksum == MD5String(*data); | 148 return checksum == MD5String(*data); |
| 142 } | 149 } |
| 143 | 150 |
| 144 } // namespace addressinput | 151 } // namespace addressinput |
| 145 } // namespace i18n | 152 } // namespace i18n |
| OLD | NEW |