|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "brpc/checksum.h" |
| 19 | + |
| 20 | +#include "brpc/protocol.h" |
| 21 | +#include "butil/logging.h" |
| 22 | + |
| 23 | +namespace brpc { |
| 24 | + |
| 25 | +static const int MAX_HANDLER_SIZE = 1024; |
| 26 | +static ChecksumHandler s_handler_map[MAX_HANDLER_SIZE] = {{NULL, NULL, NULL}}; |
| 27 | + |
| 28 | +int RegisterChecksumHandler(ChecksumType type, ChecksumHandler handler) { |
| 29 | + if (NULL == handler.Compute) { |
| 30 | + LOG(FATAL) << "Invalid parameter: handler function is NULL"; |
| 31 | + return -1; |
| 32 | + } |
| 33 | + int index = type; |
| 34 | + if (index < 0 || index >= MAX_HANDLER_SIZE) { |
| 35 | + LOG(FATAL) << "ChecksumType=" << type << " is out of range"; |
| 36 | + return -1; |
| 37 | + } |
| 38 | + if (s_handler_map[index].Compute != NULL) { |
| 39 | + LOG(FATAL) << "ChecksumType=" << type << " was registered"; |
| 40 | + return -1; |
| 41 | + } |
| 42 | + s_handler_map[index] = handler; |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +// Find ChecksumHandler by type. |
| 47 | +// Returns NULL if not found |
| 48 | +inline const ChecksumHandler* FindChecksumHandler(ChecksumType type) { |
| 49 | + int index = type; |
| 50 | + if (index < 0 || index >= MAX_HANDLER_SIZE) { |
| 51 | + LOG(ERROR) << "ChecksumType=" << type << " is out of range"; |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + if (NULL == s_handler_map[index].Compute) { |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + return &s_handler_map[index]; |
| 58 | +} |
| 59 | + |
| 60 | +const char* ChecksumTypeToCStr(ChecksumType type) { |
| 61 | + if (type == CHECKSUM_TYPE_NONE) { |
| 62 | + return "none"; |
| 63 | + } |
| 64 | + const ChecksumHandler* handler = FindChecksumHandler(type); |
| 65 | + return (handler != NULL ? handler->name : "unknown"); |
| 66 | +} |
| 67 | + |
| 68 | +void ListChecksumHandler(std::vector<ChecksumHandler>* vec) { |
| 69 | + vec->clear(); |
| 70 | + for (int i = 0; i < MAX_HANDLER_SIZE; ++i) { |
| 71 | + if (s_handler_map[i].Compute != NULL) { |
| 72 | + vec->push_back(s_handler_map[i]); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Compute `data' checksum |
| 78 | +void ComputeDataChecksum(const butil::IOBuf& data, Controller* cntl, |
| 79 | + ChecksumType checksum_type) { |
| 80 | + if (checksum_type == CHECKSUM_TYPE_NONE) { |
| 81 | + return; |
| 82 | + } |
| 83 | + const ChecksumHandler* handler = FindChecksumHandler(checksum_type); |
| 84 | + if (NULL != handler) { |
| 85 | + handler->Compute(data, cntl); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Verify `data' checksum Returns true on success, false otherwise |
| 90 | +bool VerifyDataChecksum(const butil::IOBuf& data, const Controller& cntl, |
| 91 | + ChecksumType checksum_type) { |
| 92 | + if (checksum_type == CHECKSUM_TYPE_NONE) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + const ChecksumHandler* handler = FindChecksumHandler(checksum_type); |
| 96 | + if (NULL != handler) { |
| 97 | + return handler->Verify(data, cntl); |
| 98 | + } |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace brpc |
0 commit comments