xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/internet_checksum.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2019 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 #ifndef QUICHE_QUIC_CORE_INTERNET_CHECKSUM_H_
6 #define QUICHE_QUIC_CORE_INTERNET_CHECKSUM_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 #include "absl/strings/string_view.h"
12 #include "absl/types/span.h"
13 #include "quiche/common/platform/api/quiche_export.h"
14 
15 namespace quic {
16 
17 // Incrementally compute an Internet header checksum as described in RFC 1071.
18 class QUICHE_EXPORT InternetChecksum {
19  public:
20   // Update the checksum with the specified data.  Note that while the checksum
21   // is commutative, the data has to be supplied in the units of two-byte words.
22   // If there is an extra byte at the end, the function has to be called on it
23   // last.
24   void Update(const char* data, size_t size);
25   void Update(const uint8_t* data, size_t size);
26   void Update(absl::string_view data);
27   void Update(absl::Span<const uint8_t> data);
28 
29   uint16_t Value() const;
30 
31  private:
32   uint32_t accumulator_ = 0;
33 };
34 
35 }  // namespace quic
36 
37 #endif  // QUICHE_QUIC_CORE_INTERNET_CHECKSUM_H_
38