12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#ifndef HILOG_COMPRESS_H
172498b56bSopenharmony_ci#define HILOG_COMPRESS_H
182498b56bSopenharmony_ci
192498b56bSopenharmony_ci#include <iostream>
202498b56bSopenharmony_ci#ifdef USING_ZSTD_COMPRESS
212498b56bSopenharmony_ci#define ZSTD_STATIC_LINKING_ONLY
222498b56bSopenharmony_ci#include "include/common.h"
232498b56bSopenharmony_ci#include "zstd.h"
242498b56bSopenharmony_ci#endif
252498b56bSopenharmony_ci#include <zlib.h>
262498b56bSopenharmony_ci
272498b56bSopenharmony_ci#include <hilog_common.h>
282498b56bSopenharmony_ci#include <log_utils.h>
292498b56bSopenharmony_ci
302498b56bSopenharmony_cinamespace OHOS {
312498b56bSopenharmony_cinamespace HiviewDFX {
322498b56bSopenharmony_ciusing LogPersisterBuffer = struct {
332498b56bSopenharmony_ci    char content[MAX_PERSISTER_BUFFER_SIZE];
342498b56bSopenharmony_ci    uint32_t offset;
352498b56bSopenharmony_ci};
362498b56bSopenharmony_ci
372498b56bSopenharmony_ciusing CompressAlg = enum {
382498b56bSopenharmony_ci    COMPRESS_TYPE_NONE = 0,
392498b56bSopenharmony_ci    COMPRESS_TYPE_ZSTD,
402498b56bSopenharmony_ci    COMPRESS_TYPE_ZLIB,
412498b56bSopenharmony_ci};
422498b56bSopenharmony_ci
432498b56bSopenharmony_ciclass LogCompress {
442498b56bSopenharmony_cipublic:
452498b56bSopenharmony_ci    LogCompress() = default;
462498b56bSopenharmony_ci    virtual ~LogCompress() = default;
472498b56bSopenharmony_ci    virtual int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) = 0;
482498b56bSopenharmony_ci    // Compress Types&Strings Map
492498b56bSopenharmony_ci    static std::string CompressType2Str(uint16_t compressType);
502498b56bSopenharmony_ci    static uint16_t Str2CompressType(const std::string& str);
512498b56bSopenharmony_ciprivate:
522498b56bSopenharmony_ci    static StringMap g_CompressTypes;
532498b56bSopenharmony_ci};
542498b56bSopenharmony_ci
552498b56bSopenharmony_ciclass NoneCompress : public LogCompress {
562498b56bSopenharmony_cipublic:
572498b56bSopenharmony_ci    int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) override;
582498b56bSopenharmony_ci};
592498b56bSopenharmony_ci
602498b56bSopenharmony_ciclass ZlibCompress : public LogCompress {
612498b56bSopenharmony_cipublic:
622498b56bSopenharmony_ci    int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) override;
632498b56bSopenharmony_ciprivate:
642498b56bSopenharmony_ci    static const uint16_t CHUNK = 16384;
652498b56bSopenharmony_ci    char buffIn[CHUNK] = {0};
662498b56bSopenharmony_ci    char buffOut[CHUNK] = {0};
672498b56bSopenharmony_ci
682498b56bSopenharmony_ci    z_stream cStream;
692498b56bSopenharmony_ci};
702498b56bSopenharmony_ci
712498b56bSopenharmony_ciclass ZstdCompress : public LogCompress {
722498b56bSopenharmony_cipublic:
732498b56bSopenharmony_ci    int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) override;
742498b56bSopenharmony_ciprivate:
752498b56bSopenharmony_ci#ifdef USING_ZSTD_COMPRESS
762498b56bSopenharmony_ci    static const uint16_t CHUNK = 16384;
772498b56bSopenharmony_ci    char buffIn[CHUNK] = {0};
782498b56bSopenharmony_ci    char buffOut[CHUNK] = {0};
792498b56bSopenharmony_ci    ZSTD_CCtx* cctx;
802498b56bSopenharmony_ci#endif
812498b56bSopenharmony_ci};
822498b56bSopenharmony_ci} // namespace HiviewDFX
832498b56bSopenharmony_ci} // namespace OHOS
842498b56bSopenharmony_ci#endif /* HILOG_COMPRESS_H */
85