wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 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 | #include "net/third_party/quiche/src/quic/qbone/platform/internet_checksum.h" |
| 6 | #include "net/third_party/quiche/src/quic/platform/api/quic_endian.h" |
| 7 | |
| 8 | namespace quic { |
| 9 | |
| 10 | void InternetChecksum::Update(const char* data, size_t size) { |
| 11 | const char* current; |
| 12 | for (current = data; current + 1 < data + size; current += 2) { |
| 13 | accumulator_ += *reinterpret_cast<const uint16_t*>(current); |
| 14 | } |
| 15 | if (current < data + size) { |
| 16 | accumulator_ += *reinterpret_cast<const uint8_t*>(current); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | void InternetChecksum::Update(const uint8_t* data, size_t size) { |
| 21 | Update(reinterpret_cast<const char*>(data), size); |
| 22 | } |
| 23 | |
| 24 | uint16_t InternetChecksum::Value() const { |
| 25 | uint32_t total = accumulator_; |
| 26 | while (total & 0xffff0000u) { |
| 27 | total = (total >> 16u) + (total & 0xffffu); |
| 28 | } |
| 29 | return ~static_cast<uint16_t>(total); |
| 30 | } |
| 31 | |
| 32 | } // namespace quic |