Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1159)

Unified Diff: third_party/libaddressinput/chromium/cpp/src/wrapper.cc

Issue 144353002: [rac] Use stale libaddressinput data if download fails (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/libaddressinput/chromium/cpp/src/wrapper.cc
diff --git a/third_party/libaddressinput/chromium/cpp/src/wrapper.cc b/third_party/libaddressinput/chromium/cpp/src/wrapper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b613283162c6f5e5610b3704cd861392b2c55361
--- /dev/null
+++ b/third_party/libaddressinput/chromium/cpp/src/wrapper.cc
@@ -0,0 +1,128 @@
+// Copyright (C) 2014 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.
+//
+// Wrapper prepends timestamp and checksum to data. Format:
+//
+// timestamp=<timestamp>
+// checksum=<checksum>
+// <data>
+//
+// The timestamp is the time_t that was returned from time(NULL) function. The
+// timestamp does not need to be portable because it is written and read only by
+// Wrapper. 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 "wrapper.h"
+
+#include <cassert>
+#include <cstddef>
+#include <cstdlib>
+#include <ctime>
+#include <string>
+
+#include "time_to_string.h"
+#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 Wrapper::Wrap(const std::string& data) {
+ std::string wrapped;
+ wrapped.append(kTimestampPrefix, kTimestampPrefixLength);
+ wrapped.append(TimeToString(time(NULL)));
+ wrapped.push_back(kSeparator);
+
+ wrapped.append(kChecksumPrefix, kChecksumPrefixLength);
+ wrapped.append(MD5String(data));
+ wrapped.push_back(kSeparator);
+ wrapped.append(data);
+
+ return wrapped;
+}
+
+// static
+bool Wrapper::Unwrap(std::string* data, double* age_in_seconds) {
+ assert(data != NULL);
+ assert(age_in_seconds != NULL);
+
+ std::string timestamp_string;
+ if (!UnwrapHeader(
+ kTimestampPrefix, kTimestampPrefixLength, data, &timestamp_string)) {
+ return false;
+ }
+
+ time_t timestamp = atol(timestamp_string.c_str());
+ if (timestamp < 0) {
+ return false;
+ }
+
+ *age_in_seconds = difftime(time(NULL), timestamp);
+ if (*age_in_seconds < 0.0) {
+ return false;
+ }
+
+ std::string checksum;
+ if (!UnwrapHeader(kChecksumPrefix, kChecksumPrefixLength, data, &checksum)) {
+ return false;
+ }
+
+ return checksum == MD5String(*data);
+}
+
+} // namespace addressinput
+} // namespace i18n

Powered by Google App Engine
This is Rietveld 408576698