Add details (where missing) for histograms and remove a few that are not worth provid...
[chromium-blink-merge.git] / net / quic / crypto / source_address_token.cc
blobb095e762265f758aa2d2c9be40d5488fd8a255b5
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/crypto/source_address_token.h"
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
12 using std::string;
13 using std::vector;
15 namespace net {
17 SourceAddressToken::SourceAddressToken() {
20 SourceAddressToken::~SourceAddressToken() {
23 string SourceAddressToken::SerializeAsString() const {
24 string out;
25 out.push_back(ip_.size());
26 out.append(ip_);
27 string time_str = base::Int64ToString(timestamp_);
28 out.push_back(time_str.size());
29 out.append(time_str);
30 return out;
33 bool SourceAddressToken::ParseFromArray(const char* plaintext,
34 size_t plaintext_length) {
35 if (plaintext_length == 0) {
36 return false;
38 size_t ip_len = plaintext[0];
39 if (plaintext_length <= 1 + ip_len) {
40 return false;
42 size_t time_len = plaintext[1 + ip_len];
43 if (plaintext_length != 1 + ip_len + 1 + time_len) {
44 return false;
47 string time_str(&plaintext[1 + ip_len + 1], time_len);
48 int64 timestamp;
49 if (!base::StringToInt64(time_str, &timestamp)) {
50 return false;
53 ip_.assign(&plaintext[1], ip_len);
54 timestamp_ = timestamp;
55 return true;
58 } // namespace net