diff options
author | Android Chromium Automerger <chromium-automerger@android> | 2014-06-16 14:44:22 +0000 |
---|---|---|
committer | Android Chromium Automerger <chromium-automerger@android> | 2014-06-16 14:44:22 +0000 |
commit | 4e065f4283db7f3b540a0d89857ef87d8400da35 (patch) | |
tree | 39ace770b394b411d5e3a4ca500462df60ed1209 /cpp/src/validating_util.cc | |
parent | be7edd6e608768a423c246f9f13c632349a31c5f (diff) | |
parent | 1cc079798198a961e938b74e573588986afdca3b (diff) | |
download | src-4e065f4283db7f3b540a0d89857ef87d8400da35.tar.gz |
Merge third_party/libaddressinput/src from https://chromium.googlesource.com/external/libaddressinput.git at 1cc079798198a961e938b74e573588986afdca3b
This commit was generated by merge_from_chromium.py.
Change-Id: Ief2f36f1e9dfbac8319d3500b0c000685c9e345d
Diffstat (limited to 'cpp/src/validating_util.cc')
-rw-r--r-- | cpp/src/validating_util.cc | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/cpp/src/validating_util.cc b/cpp/src/validating_util.cc new file mode 100644 index 0000000..de43f80 --- /dev/null +++ b/cpp/src/validating_util.cc @@ -0,0 +1,140 @@ +// Copyright (C) 2013 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ValidatingUtil wraps data with checksum and timestamp. Format: +// +// timestamp=<timestamp> +// checksum=<checksum> +// <data> +// +// The timestamp is the time_t that was returned from time() function. The +// timestamp does not need to be portable because it is written and read only by +// ValidatingUtil. The value is somewhat human-readable: it is the number of +// seconds since the epoch. +// +// The checksum is the 32-character hexadecimal MD5 checksum of <data>. It is +// meant to protect from random file changes on disk. + +#include "validating_util.h" + +#include <cassert> +#include <cstddef> +#include <cstdio> +#include <cstdlib> +#include <ctime> +#include <string> + +#include "util/md5.h" + +namespace i18n { +namespace addressinput { + +namespace { + +const char kTimestampPrefix[] = "timestamp="; +const size_t kTimestampPrefixLength = sizeof kTimestampPrefix - 1; + +const char kChecksumPrefix[] = "checksum="; +const size_t kChecksumPrefixLength = sizeof kChecksumPrefix - 1; + +const char kSeparator = '\n'; + +// Places the header value into |header_value| parameter and erases the header +// from |data|. Returns |true| if the header format is valid. +bool UnwrapHeader(const char* header_prefix, + size_t header_prefix_length, + std::string* data, + std::string* header_value) { + assert(header_prefix != NULL); + assert(data != NULL); + assert(header_value != NULL); + + if (data->compare( + 0, header_prefix_length, header_prefix, header_prefix_length) != 0) { + return false; + } + + std::string::size_type separator_position = + data->find(kSeparator, header_prefix_length); + if (separator_position == std::string::npos) { + return false; + } + + header_value->assign( + *data, header_prefix_length, separator_position - header_prefix_length); + data->erase(0, separator_position + 1); + + return true; +} + +} // namespace + +// static +std::string ValidatingUtil::Wrap(const std::string& data, time_t timestamp) { + char timestamp_string[2 + 3 * sizeof timestamp]; + snprintf(timestamp_string, sizeof timestamp_string, "%ld", timestamp); + + std::string wrapped; + wrapped.append(kTimestampPrefix, kTimestampPrefixLength); + wrapped.append(timestamp_string); + wrapped.push_back(kSeparator); + + wrapped.append(kChecksumPrefix, kChecksumPrefixLength); + wrapped.append(MD5String(data)); + wrapped.push_back(kSeparator); + wrapped.append(data); + + return wrapped; +} + +// static +bool ValidatingUtil::UnwrapTimestamp(std::string* data, time_t now) { + assert(data != NULL); + if (now < 0) { + return false; + } + + std::string timestamp_string; + if (!UnwrapHeader( + kTimestampPrefix, kTimestampPrefixLength, data, ×tamp_string)) { + return false; + } + + time_t timestamp = atol(timestamp_string.c_str()); + if (timestamp < 0) { + return false; + } + + // One month contains: + // 30 days * + // 24 hours per day * + // 60 minutes per hour * + // 60 seconds per minute. + static const double kOneMonthInSeconds = 30.0 * 24.0 * 60.0 * 60.0; + double age_in_seconds = difftime(now, timestamp); + return !(age_in_seconds < 0.0) && age_in_seconds < kOneMonthInSeconds; +} + +// static +bool ValidatingUtil::UnwrapChecksum(std::string* data) { + assert(data != NULL); + std::string checksum; + if (!UnwrapHeader(kChecksumPrefix, kChecksumPrefixLength, data, &checksum)) { + return false; + } + return checksum == MD5String(*data); +} + +} // namespace addressinput +} // namespace i18n |