1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci#ifndef GZIP_PKG_FILE_H
16fb299fa2Sopenharmony_ci#define GZIP_PKG_FILE_H
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include "pkg_info_utils.h"
19fb299fa2Sopenharmony_ci#include "pkg_pkgfile.h"
20fb299fa2Sopenharmony_ci#include "pkg_utils.h"
21fb299fa2Sopenharmony_ci#include "pkg_zipfile.h"
22fb299fa2Sopenharmony_ci#include "zlib.h"
23fb299fa2Sopenharmony_ci
24fb299fa2Sopenharmony_cinamespace Hpackage {
25fb299fa2Sopenharmony_cistruct __attribute__((packed)) GZipHeader {
26fb299fa2Sopenharmony_ci    uint16_t magic = 0;
27fb299fa2Sopenharmony_ci    uint8_t method = 0;
28fb299fa2Sopenharmony_ci    uint8_t flags = 0;
29fb299fa2Sopenharmony_ci    time_t mtime = 0;
30fb299fa2Sopenharmony_ci    uint8_t xfl = 0;
31fb299fa2Sopenharmony_ci    uint8_t osFile = 0;
32fb299fa2Sopenharmony_ci};
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_cistruct __attribute__((packed)) GZipExtra {
35fb299fa2Sopenharmony_ci    uint8_t si1 = 0;
36fb299fa2Sopenharmony_ci    uint8_t si2 = 0;
37fb299fa2Sopenharmony_ci    uint16_t xlen = 0;
38fb299fa2Sopenharmony_ci};
39fb299fa2Sopenharmony_ci
40fb299fa2Sopenharmony_ciclass GZipFileEntry : public ZipFileEntry {
41fb299fa2Sopenharmony_cipublic:
42fb299fa2Sopenharmony_ci    GZipFileEntry(PkgFilePtr pkgFile, uint32_t nodeId) : ZipFileEntry(pkgFile, nodeId) {}
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_ci    ~GZipFileEntry() override {}
45fb299fa2Sopenharmony_ci
46fb299fa2Sopenharmony_ci    int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override;
47fb299fa2Sopenharmony_ci
48fb299fa2Sopenharmony_ci    int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override;
49fb299fa2Sopenharmony_ci
50fb299fa2Sopenharmony_ci    int32_t Unpack(PkgStreamPtr outStream) override;
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_ci    int32_t DecodeHeader(PkgBuffer &buffer, size_t, size_t, size_t &decodeLen) override;
53fb299fa2Sopenharmony_ci
54fb299fa2Sopenharmony_ciprivate:
55fb299fa2Sopenharmony_ci    void DecodeHeaderCalOffset(uint8_t flags, const PkgBuffer &buffer, size_t &offset,
56fb299fa2Sopenharmony_ci        std::string &fileName) const;
57fb299fa2Sopenharmony_ci    int32_t CheckFileInfo(PkgAlgorithmContext context, PkgStreamPtr inStream);
58fb299fa2Sopenharmony_ci    void GetUpGradeCompInfo(size_t &offset, PkgBuffer &buffer);
59fb299fa2Sopenharmony_ci};
60fb299fa2Sopenharmony_ci
61fb299fa2Sopenharmony_ciclass GZipPkgFile : public PkgFileImpl {
62fb299fa2Sopenharmony_cipublic:
63fb299fa2Sopenharmony_ci    explicit GZipPkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgInfoPtr header = nullptr)
64fb299fa2Sopenharmony_ci        : PkgFileImpl(manager, stream, PkgFile::PKG_TYPE_GZIP)
65fb299fa2Sopenharmony_ci    {
66fb299fa2Sopenharmony_ci        UNUSED(header);
67fb299fa2Sopenharmony_ci        pkgInfo_.signMethod = PKG_SIGN_METHOD_RSA;
68fb299fa2Sopenharmony_ci        pkgInfo_.digestMethod = PKG_DIGEST_TYPE_SHA256;
69fb299fa2Sopenharmony_ci    }
70fb299fa2Sopenharmony_ci
71fb299fa2Sopenharmony_ci    ~GZipPkgFile() override {}
72fb299fa2Sopenharmony_ci
73fb299fa2Sopenharmony_ci    int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr inStream) override;
74fb299fa2Sopenharmony_ci
75fb299fa2Sopenharmony_ci    int32_t SavePackage(size_t &offset) override;
76fb299fa2Sopenharmony_ci
77fb299fa2Sopenharmony_ci    int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) override;
78fb299fa2Sopenharmony_ci
79fb299fa2Sopenharmony_ciprivate:
80fb299fa2Sopenharmony_ci    PkgInfo pkgInfo_ {};
81fb299fa2Sopenharmony_ci    size_t currentOffset_ = 0;
82fb299fa2Sopenharmony_ci};
83fb299fa2Sopenharmony_ci} // namespace Hpackage
84fb299fa2Sopenharmony_ci#endif // GZIP_PKG_FILE_H
85