1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef NLP_SAFT_COMPONENTS_COMMON_MOBILE_MATH_CHECKSUM_H_ 18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_MATH_CHECKSUM_H_ 19 20 #include "lang_id/common/lite_base/integral-types.h" 21 #include "lang_id/common/lite_strings/stringpiece.h" 22 23 namespace libtextclassifier3 { 24 namespace mobile { 25 26 // Class to compute a 32bit Cyclic Redundancy Check (CRC) in a cummulative way. 27 // 28 // To use, create an instance of this class, repeatedly call Update() to "feed" 29 // it your pieces of data, and, when done, call Get(). 30 class Crc32 { 31 public: Crc32()32 Crc32() : current_(GetInitialCrc32()) {} 33 34 // Updates current CRC32 code to also take into account the |len| bytes that 35 // start at address |str|. 36 void Update(const char *str, int len); 37 38 // Updates current CRC32 code to also take into account the bytes from |s|. Update(StringPiece s)39 void Update(StringPiece s) { Update(s.data(), s.size()); } 40 41 // Returns the CRC32 code for the data so far. Get()42 uint32 Get() const { return current_; } 43 44 private: 45 // Returns the initial value for current_. 46 static uint32 GetInitialCrc32(); 47 48 // CRC32 for the data so far. 49 uint32 current_; 50 }; 51 52 } // namespace mobile 53 } // namespace nlp_saft 54 55 #endif // NLP_SAFT_COMPONENTS_COMMON_MOBILE_MATH_CHECKSUM_H_ 56