1 // Copyright (c) 2022 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.
4 
5 #include "quiche/quic/load_balancer/load_balancer_server_id.h"
6 
7 #include <array>
8 #include <cstdint>
9 #include <cstring>
10 #include <string>
11 
12 #include "absl/strings/escaping.h"
13 #include "absl/strings/string_view.h"
14 #include "absl/types/span.h"
15 #include "quiche/quic/platform/api/quic_bug_tracker.h"
16 
17 namespace quic {
18 
LoadBalancerServerId(absl::string_view data)19 LoadBalancerServerId::LoadBalancerServerId(absl::string_view data)
20     : LoadBalancerServerId(absl::MakeSpan(
21           reinterpret_cast<const uint8_t*>(data.data()), data.length())) {}
22 
LoadBalancerServerId(absl::Span<const uint8_t> data)23 LoadBalancerServerId::LoadBalancerServerId(absl::Span<const uint8_t> data)
24     : length_(data.length()) {
25   if (length_ == 0 || length_ > kLoadBalancerMaxServerIdLen) {
26     QUIC_BUG(quic_bug_433312504_02)
27         << "Attempted to create LoadBalancerServerId with length "
28         << static_cast<int>(length_);
29     length_ = 0;
30     return;
31   }
32   memcpy(data_.data(), data.data(), data.length());
33 }
34 
set_length(uint8_t length)35 void LoadBalancerServerId::set_length(uint8_t length) {
36   QUIC_BUG_IF(quic_bug_599862571_01,
37               length == 0 || length > kLoadBalancerMaxServerIdLen)
38       << "Attempted to set LoadBalancerServerId length to "
39       << static_cast<int>(length);
40   length_ = length;
41 }
42 
ToString() const43 std::string LoadBalancerServerId::ToString() const {
44   return absl::BytesToHexString(
45       absl::string_view(reinterpret_cast<const char*>(data_.data()), length_));
46 }
47 
48 }  // namespace quic
49