• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Functions to compress and decompress low entropy entries in the model.
18 
19 #ifndef LIBTEXTCLASSIFIER_UTILS_ZLIB_ZLIB_H_
20 #define LIBTEXTCLASSIFIER_UTILS_ZLIB_ZLIB_H_
21 
22 #include <vector>
23 
24 #include "utils/base/integral_types.h"
25 #include "utils/zlib/buffer_generated.h"
26 #include <zlib.h>
27 
28 namespace libtextclassifier3 {
29 
30 class ZlibDecompressor {
31  public:
32   static std::unique_ptr<ZlibDecompressor> Instance(
33       const unsigned char* dictionary = nullptr,
34       unsigned int dictionary_size = 0);
35   ~ZlibDecompressor();
36 
37   bool Decompress(const uint8* buffer, const int buffer_size,
38                   const int uncompressed_size, std::string* out);
39   bool MaybeDecompress(const CompressedBuffer* compressed_buffer,
40                        std::string* out);
41   bool MaybeDecompress(const CompressedBufferT* compressed_buffer,
42                        std::string* out);
43   bool MaybeDecompressOptionallyCompressedBuffer(
44       const flatbuffers::String* uncompressed_buffer,
45       const CompressedBuffer* compressed_buffer, std::string* out);
46   bool MaybeDecompressOptionallyCompressedBuffer(
47       const flatbuffers::Vector<uint8>* uncompressed_buffer,
48       const CompressedBuffer* compressed_buffer, std::string* out);
49 
50  private:
51   ZlibDecompressor(const unsigned char* dictionary,
52                    const unsigned int dictionary_size);
53   z_stream stream_;
54   bool initialized_;
55 };
56 
57 class ZlibCompressor {
58  public:
59   static std::unique_ptr<ZlibCompressor> Instance(
60       const unsigned char* dictionary = nullptr,
61       unsigned int dictionary_size = 0);
62   ~ZlibCompressor();
63 
64   void Compress(const std::string& uncompressed_content,
65                 CompressedBufferT* out);
66 
67   bool GetDictionary(std::vector<unsigned char>* dictionary);
68 
69  private:
70   explicit ZlibCompressor(const unsigned char* dictionary = nullptr,
71                           const unsigned int dictionary_size = 0,
72                           const int level = Z_BEST_COMPRESSION,
73                           // Tmp. buffer size was set based on the current set
74                           // of patterns to be compressed.
75                           const int tmp_buffer_size = 64 * 1024);
76   z_stream stream_;
77   std::unique_ptr<Bytef[]> buffer_;
78   unsigned int buffer_size_;
79   bool initialized_;
80 };
81 
82 }  // namespace libtextclassifier3
83 
84 #endif  // LIBTEXTCLASSIFIER_UTILS_ZLIB_ZLIB_H_
85