yanglimingcn commented on code in PR #2967: URL: https://github.com/apache/brpc/pull/2967#discussion_r2115873760
########## src/brpc/policy/crc32c_checksum.cpp: ########## @@ -0,0 +1,59 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "brpc/policy/crc32c_checksum.h" + +#include "brpc/details/controller_private_accessor.h" +#include "brpc/log.h" +#include "butil/crc32c.h" +#include "butil/sys_byteorder.h" + +namespace brpc { +namespace policy { + +void Crc32cCompute(const butil::IOBuf& buf, Controller* cntl) { + butil::IOBufAsZeroCopyInputStream wrapper(buf); + const void* data; + int size; + uint32_t crc = 0; + while (wrapper.Next(&data, &size)) { + crc = butil::crc32c::Extend(crc, static_cast<const char*>(data), size); + } + RPC_VLOG << "Crc32cCompute crc=" << crc; + crc = butil::HostToNet32(butil::crc32c::Mask(crc)); + ControllerPrivateAccessor(cntl).set_checksum_value( + reinterpret_cast<char*>(&crc), sizeof(crc)); +} + +bool Crc32cVerify(const butil::IOBuf& buf, const Controller& cntl) { + butil::IOBufAsZeroCopyInputStream wrapper(buf); + const void* data; + int size; + uint32_t crc = 0; + while (wrapper.Next(&data, &size)) { + crc = butil::crc32c::Extend(crc, static_cast<const char*>(data), size); + } + auto& val = ControllerPrivateAccessor(const_cast<Controller*>(&cntl)) + .checksum_value(); + auto expected = *reinterpret_cast<const uint32_t*>(val.data()); Review Comment: val is assigned by a uint32_t, so it is length will always 4 bytes -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org