1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 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 MINDSPORE_LITE_MINDSPORE_LITE_SRC_HUFFMAN_DECODE_H_ 18 #define MINDSPORE_LITE_MINDSPORE_LITE_SRC_HUFFMAN_DECODE_H_ 19 20 #include <cstring> 21 #include <utility> 22 #include <string> 23 #include <vector> 24 25 #include "include/errorcode.h" 26 #include "src/common/log_adapter.h" 27 28 namespace mindspore { 29 namespace lite { 30 const int PSEUDO_EOF = 128; 31 32 struct HuffmanNode { 33 int key; 34 unsigned int freq; 35 std::string code; 36 HuffmanNode *left = nullptr; 37 HuffmanNode *right = nullptr; 38 HuffmanNode *parent = nullptr; 39 }; 40 41 using HuffmanNodePtr = HuffmanNode *; 42 43 class HuffmanDecode { 44 public: 45 virtual ~HuffmanDecode() = default; 46 47 static STATUS DoHuffmanDecode(const std::string &input_str, void *decoded_data, size_t data_len); 48 49 private: 50 HuffmanDecode() = default; 51 52 static void FreeHuffmanNodeTree(HuffmanNodePtr root); 53 54 static STATUS RebuildHuffmanTree(std::string key, std::string code, const HuffmanNodePtr &root); 55 56 static STATUS DoHuffmanDecompress(HuffmanNodePtr root, std::string encoded_data, std::string *decoded_str); 57 Str2Vec(std::string s)58 static std::vector<std::string> Str2Vec(std::string s) { 59 size_t i = 0; 60 std::vector<std::string> vec; 61 while (i < s.length()) { 62 size_t j = i; 63 while (j < s.length() && s[j] != ' ') { 64 j++; 65 } 66 if (j != i) { 67 vec.push_back(s.substr(i, j - i)); 68 i = j + 1; 69 } else { 70 i = j; 71 } 72 } 73 return vec; 74 } 75 }; 76 77 } // namespace lite 78 } // namespace mindspore 79 #endif // MINDSPORE_LITE_MINDSPORE_LITE_SRC_HUFFMAN_DECODE_H_ 80