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

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/validating_util.cc

Issue 110533002: [rac] Temporarily copy libaddressinput to Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Avoid redefinition of basictypes.h Created 7 years 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (C) 2013 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // ValidatingUtil wraps data with checksum and timestamp. Format:
16 //
17 // timestamp=<timestamp>
18 // checksum=<checksum>
19 // <data>
20 //
21 // The timestamp is the time_t that was returned from time() function. The
22 // timestamp does not need to be portable because it is written and read only by
23 // ValidatingUtil. The value is somewhat human-readable: it is the number of
24 // seconds since the epoch.
25 //
26 // The checksum is the 32-character hexadecimal MD5 checksum of <data>. It is
27 // meant to protect from random file changes on disk.
28
29 #include "validating_util.h"
30
31 #include <cassert>
32 #include <cstddef>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <ctime>
36 #include <string>
37
38 #include "util/md5.h"
39
40 namespace i18n {
41 namespace addressinput {
42
43 namespace {
44
45 const char kTimestampPrefix[] = "timestamp=";
46 const size_t kTimestampPrefixLength = sizeof kTimestampPrefix - 1;
47
48 const char kChecksumPrefix[] = "checksum=";
49 const size_t kChecksumPrefixLength = sizeof kChecksumPrefix - 1;
50
51 const char kSeparator = '\n';
52
53 // Places the header value into |header_value| parameter and erases the header
54 // from |data|. Returns |true| if the header format is valid.
55 bool UnwrapHeader(const char* header_prefix,
56 size_t header_prefix_length,
57 std::string* data,
58 std::string* header_value) {
59 assert(header_prefix != NULL);
60 assert(data != NULL);
61 assert(header_value != NULL);
62
63 if (data->compare(
64 0, header_prefix_length, header_prefix, header_prefix_length) != 0) {
65 return false;
66 }
67
68 std::string::size_type separator_position =
69 data->find(kSeparator, header_prefix_length);
70 if (separator_position == std::string::npos) {
71 return false;
72 }
73
74 header_value->assign(
75 *data, header_prefix_length, separator_position - header_prefix_length);
76 data->erase(0, separator_position + 1);
77
78 return true;
79 }
80
81 } // namespace
82
83 // static
84 std::string ValidatingUtil::Wrap(const std::string& data, time_t timestamp) {
85 char timestamp_string[2 + 3 * sizeof timestamp];
86 snprintf(timestamp_string, sizeof timestamp_string, "%ld", timestamp);
87
88 std::string wrapped;
89 wrapped.append(kTimestampPrefix, kTimestampPrefixLength);
90 wrapped.append(timestamp_string);
91 wrapped.push_back(kSeparator);
92
93 wrapped.append(kChecksumPrefix, kChecksumPrefixLength);
94 wrapped.append(MD5String(data));
95 wrapped.push_back(kSeparator);
96 wrapped.append(data);
97
98 return wrapped;
99 }
100
101 // static
102 bool ValidatingUtil::UnwrapTimestamp(std::string* data, time_t now) {
103 assert(data != NULL);
104 if (now < 0) {
105 return false;
106 }
107
108 std::string timestamp_string;
109 if (!UnwrapHeader(
110 kTimestampPrefix, kTimestampPrefixLength, data, &timestamp_string)) {
111 return false;
112 }
113
114 time_t timestamp = atol(timestamp_string.c_str());
115 if (timestamp < 0) {
116 return false;
117 }
118
119 // One month contains:
120 // 30 days *
121 // 24 hours per day *
122 // 60 minutes per hour *
123 // 60 seconds per minute.
124 static const double kOneMonthInSeconds = 30.0 * 24.0 * 60.0 * 60.0;
125 double age_in_seconds = difftime(now, timestamp);
126 return !(age_in_seconds < 0.0) && age_in_seconds < kOneMonthInSeconds;
127 }
128
129 // static
130 bool ValidatingUtil::UnwrapChecksum(std::string* data) {
131 assert(data != NULL);
132 std::string checksum;
133 if (!UnwrapHeader(kChecksumPrefix, kChecksumPrefixLength, data, &checksum)) {
134 return false;
135 }
136 return checksum == MD5String(*data);
137 }
138
139 } // namespace addressinput
140 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698