// 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[] = …; const size_t kTimestampPrefixLength = …; const char kChecksumPrefix[] = …; const size_t kChecksumPrefixLength = …; const char kSeparator = …; // 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) { … } } // namespace // static void ValidatingUtil::Wrap(time_t timestamp, std::string* data) { … } // static bool ValidatingUtil::UnwrapTimestamp(std::string* data, time_t now) { … } // static bool ValidatingUtil::UnwrapChecksum(std::string* data) { … } } // namespace addressinput } // namespace i18n