• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GZIP_PKG_FILE_H
16 #define GZIP_PKG_FILE_H
17 
18 #include "pkg_info_utils.h"
19 #include "pkg_pkgfile.h"
20 #include "pkg_utils.h"
21 #include "pkg_zipfile.h"
22 #include "zlib.h"
23 
24 namespace Hpackage {
25 struct __attribute__((packed)) GZipHeader {
26     uint16_t magic = 0;
27     uint8_t method = 0;
28     uint8_t flags = 0;
29     time_t mtime = 0;
30     uint8_t xfl = 0;
31     uint8_t osFile = 0;
32 };
33 
34 struct __attribute__((packed)) GZipExtra {
35     uint8_t si1 = 0;
36     uint8_t si2 = 0;
37     uint16_t xlen = 0;
38 };
39 
40 class GZipFileEntry : public ZipFileEntry {
41 public:
GZipFileEntry(PkgFilePtr pkgFile,uint32_t nodeId)42     GZipFileEntry(PkgFilePtr pkgFile, uint32_t nodeId) : ZipFileEntry(pkgFile, nodeId) {}
43 
~GZipFileEntry()44     ~GZipFileEntry() override {}
45 
46     int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override;
47 
48     int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override;
49 
50     int32_t Unpack(PkgStreamPtr outStream) override;
51 
52     int32_t DecodeHeader(PkgBuffer &buffer, size_t, size_t, size_t &decodeLen) override;
53 
54 private:
55     void DecodeHeaderCalOffset(uint8_t flags, const PkgBuffer &buffer, size_t &offset,
56         std::string &fileName) const;
57     int32_t CheckFileInfo(PkgAlgorithmContext context, PkgStreamPtr inStream);
58     void GetUpGradeCompInfo(size_t &offset, PkgBuffer &buffer);
59 };
60 
61 class GZipPkgFile : public PkgFileImpl {
62 public:
63     explicit GZipPkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgInfoPtr header = nullptr)
PkgFileImpl(manager,stream,PkgFile::PKG_TYPE_GZIP)64         : PkgFileImpl(manager, stream, PkgFile::PKG_TYPE_GZIP)
65     {
66         UNUSED(header);
67         pkgInfo_.signMethod = PKG_SIGN_METHOD_RSA;
68         pkgInfo_.digestMethod = PKG_DIGEST_TYPE_SHA256;
69     }
70 
~GZipPkgFile()71     ~GZipPkgFile() override {}
72 
73     int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr inStream) override;
74 
75     int32_t SavePackage(size_t &offset) override;
76 
77     int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) override;
78 
79 private:
80     PkgInfo pkgInfo_ {};
81     size_t currentOffset_ = 0;
82 };
83 } // namespace Hpackage
84 #endif // GZIP_PKG_FILE_H
85