1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HIDUMPER_DUMP_COMPRESSOR_H 16 #define HIDUMPER_DUMP_COMPRESSOR_H 17 18 #include <iostream> 19 #include <zlib.h> 20 #include "common.h" 21 22 23 const uint32_t MAX_COMPRESS_BUFFER_SIZE = 32 * 1024; 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 struct CompressBuffer { 28 uint32_t offset; 29 char content[MAX_COMPRESS_BUFFER_SIZE] = { 0 }; 30 }; 31 32 const uint16_t CHUNK = 16384; 33 const uint16_t WINDOWS_BITS = 16; 34 const uint16_t MEM_LEVEL = 8; 35 36 class DumpCompressor { 37 public: 38 DumpCompressor(); 39 ~DumpCompressor() = default; 40 41 /** 42 * Compress content in source buffer to dest buffer using zlib. 43 * 44 * @param srcBuffer, source buffer. 45 * @param destBuffer, dest buffer. 46 * @return DUMP_OK on sucess or DUMP_FAIL on any errors. 47 */ 48 DumpStatus Compress(CompressBuffer*& srcBuffer, CompressBuffer*& destBuffer); 49 50 private: 51 DumpStatus FillBuffer(int& flush, CompressBuffer*& srcBuffer, char*& buffIn, 52 size_t const toRead, size_t& src_pos, size_t srcBufferOffset); 53 DumpStatus DeflateBuffer(int flush, char*& buffOut, size_t& dst_pos); 54 void DeleteZData(); 55 56 private: 57 z_stream zStream_ = {0}; 58 unsigned char* zData_ = nullptr; 59 uint32_t zDataSize_ = 0; 60 }; 61 } // namespace HiviewDFX 62 } // namespace OHOS 63 #endif // HIDUMPER_COMPRESS_H 64