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 #include "utils/intents/zlib-utils.h" 18 19 #include <memory> 20 21 #include "utils/base/logging.h" 22 #include "utils/zlib/buffer_generated.h" 23 #include "utils/zlib/zlib.h" 24 25 namespace libtextclassifier3 { 26 CompressIntentModel(IntentFactoryModelT * intent_model)27bool CompressIntentModel(IntentFactoryModelT* intent_model) { 28 std::unique_ptr<ZlibCompressor> intent_zlib_compressor = 29 ZlibCompressor::Instance(); 30 for (auto& generator : intent_model->generator) { 31 generator->compressed_lua_template_generator.reset(new CompressedBufferT); 32 intent_zlib_compressor->Compress( 33 std::string(reinterpret_cast<const char*>( 34 generator->lua_template_generator.data()), 35 generator->lua_template_generator.size()), 36 generator->compressed_lua_template_generator.get()); 37 generator->lua_template_generator.clear(); 38 } 39 return true; 40 } 41 DecompressIntentModel(IntentFactoryModelT * intent_model)42bool DecompressIntentModel(IntentFactoryModelT* intent_model) { 43 std::unique_ptr<ZlibDecompressor> zlib_decompressor = 44 ZlibDecompressor::Instance(); 45 if (!zlib_decompressor) { 46 TC3_LOG(ERROR) << "Cannot initialize decompressor."; 47 return false; 48 } 49 50 for (std::unique_ptr<IntentFactoryModel_::IntentGeneratorT>& generator : 51 intent_model->generator) { 52 if (generator->compressed_lua_template_generator == nullptr) { 53 continue; 54 } 55 56 std::string lua_template_generator; 57 if (!zlib_decompressor->MaybeDecompress( 58 generator->compressed_lua_template_generator.get(), 59 &lua_template_generator)) { 60 TC3_LOG(ERROR) << "Cannot decompress intent template."; 61 return false; 62 } 63 generator->lua_template_generator = std::vector<uint8_t>( 64 lua_template_generator.begin(), lua_template_generator.end()); 65 66 generator->compressed_lua_template_generator.reset(nullptr); 67 } 68 return true; 69 } 70 71 } // namespace libtextclassifier3 72