12fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// Copyright (C) 2013 Google Inc.
22fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//
32fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// Licensed under the Apache License, Version 2.0 (the "License");
42fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// you may not use this file except in compliance with the License.
52fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// You may obtain a copy of the License at
62fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//
72fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// http://www.apache.org/licenses/LICENSE-2.0
82fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//
92fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// Unless required by applicable law or agreed to in writing, software
102fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// distributed under the License is distributed on an "AS IS" BASIS,
112fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// See the License for the specific language governing permissions and
132fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// limitations under the License.
142fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//
152fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// An object to wrap data with a checksum and a timestamp. These fields are used
162fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// to verify that the data is not stale or corrupted. Staleness threshold is 1
172fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// month.
182fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
192fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org#ifndef I18N_ADDRESSINPUT_VALIDATING_UTIL_H_
202fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org#define I18N_ADDRESSINPUT_VALIDATING_UTIL_H_
212fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
226c42355ce8c105bbadf57deb999c777b5c49df59roubert@google.com#include <libaddressinput/util/basictypes.h>
236c42355ce8c105bbadf57deb999c777b5c49df59roubert@google.com
242fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org#include <ctime>
252fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org#include <string>
262fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
272fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.orgnamespace i18n {
282fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.orgnamespace addressinput {
292fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
302fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org// Wraps data with a checksum and a timestamp. Sample usage:
31128ae075bbf50996c1068740746f27430f89136droubert@google.com//    std::string data = ...
32128ae075bbf50996c1068740746f27430f89136droubert@google.com//    ValidatingUtil::Wrap(time(NULL), &data);
33128ae075bbf50996c1068740746f27430f89136droubert@google.com//    Process(data);
342fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//
352fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//    std::string unwrapped = wrapped;
362fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//    if (ValidatingUtil::UnwrapTimestamp(&unwrapped, time(NULL)) &&
372fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//        ValidatingUtil::UnwrapChecksum(&unwrapped)) {
382fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//      Process(unwrapped);
392fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org//    }
402fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.orgclass ValidatingUtil {
412fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org public:
42128ae075bbf50996c1068740746f27430f89136droubert@google.com  // Adds checksum and given |timestamp| to |data|.
43128ae075bbf50996c1068740746f27430f89136droubert@google.com  static void Wrap(time_t timestamp, std::string* data);
442fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
452fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org  // Strips out the timestamp from |data|. Returns |true| if the timestamp is
462fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org  // present, formatted correctly, valid, and recent with respect to |now|.
472fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org  static bool UnwrapTimestamp(std::string* data, time_t now);
482fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
492fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org  // Strips out the checksum from |data|. Returns |true| if the checksum is
502fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org  // present, formatted correctly, and valid for this data.
512fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org  static bool UnwrapChecksum(std::string* data);
526c42355ce8c105bbadf57deb999c777b5c49df59roubert@google.com
536c42355ce8c105bbadf57deb999c777b5c49df59roubert@google.com private:
546c42355ce8c105bbadf57deb999c777b5c49df59roubert@google.com  DISALLOW_COPY_AND_ASSIGN(ValidatingUtil);
552fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org};
562fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
572fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org}  // namespace addressinput
582fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org}  // namespace i18n
592fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org
602fefd83dd2c65ee585a2aa05e4856eb4d0e93f0brouslan@chromium.org#endif  // I18N_ADDRESSINPUT_VALIDATING_UTIL_H_
61