1 /*
2  * Copyright (c) 2023 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 
16 #ifndef PKG_INFO_UTILS_H
17 #define PKG_INFO_UTILS_H
18 
19 #include <functional>
20 #include <string>
21 #include <vector>
22 #include "package/package.h"
23 
24 namespace Hpackage {
25 struct FileInfo;
26 struct PkgInfo;
27 using FileInfoPtr = FileInfo *;
28 using PkgInfoPtr = PkgInfo *;
29 
30 /**
31  * Error code definition
32  */
33 enum {
34     PKG_SUCCESS = 0,
35     PKG_ERROR_BASE = 100,
36     PKG_INVALID_NAME,
37     PKG_INVALID_PARAM,
38     PKG_INVALID_FILE,
39     PKG_INVALID_SIGNATURE,
40     PKG_INVALID_PKG_FORMAT,
41     PKG_INVALID_ALGORITHM,
42     PKG_INVALID_DIGEST,
43     PKG_INVALID_STREAM,
44     PKG_INVALID_VERSION,
45     PKG_INVALID_STATE,
46     PKG_INVALID_LZ4,
47     PKG_NONE_PERMISSION,
48     PKG_NONE_MEMORY,
49     PKG_VERIFY_FAIL,
50 };
51 
52 enum {
53     POST_TYPE_UPLOAD_PKG = 0,
54     POST_TYPE_DECODE_PKG,
55     POST_TYPE_VERIFY_PKG,
56     POST_TYPE_WRITE_PARTITION
57 };
58 
59 enum {
60     UPGRADE_FILE_COMP_OTHER_TPYE = 1,
61     UPGRADE_FILE_COMP_IMG_TPYE
62 };
63 
64 /**
65  * Package information
66  */
67 struct PkgInfo {
68     uint32_t entryCount = 0;
69     uint32_t updateFileHeadLen = 0;
70     uint8_t signMethod;
71     uint8_t digestMethod;
72     uint8_t pkgType;
73     uint8_t pkgFlags;
74 };
75 
76 /**
77  * File information
78  */
79 struct FileInfo {
80     uint8_t flags = 0;
81     uint8_t resType = 0;
82     uint8_t digestMethod = 0;
83     uint16_t packMethod = 0;
84     time_t modifiedTime = 0;
85     size_t packedSize = 0;
86     size_t unpackedSize = 0;
87     size_t headerOffset = 0;
88     size_t dataOffset = 0;
89     std::string identity;
90 };
91 
92 /**
93  * Header information of the update package
94  */
95 struct UpgradePkgInfo {
96     PkgInfo pkgInfo;
97     uint32_t updateFileVersion = 0;
98     std::string productUpdateId;
99     std::string softwareVersion;
100     std::string date;
101     std::string time;
102     std::string descriptPackageId;
103 };
104 
105 /**
106  * Component information of the update package
107  */
108 struct ComponentInfo {
109     FileInfo fileInfo;
110     std::string version;
111     uint8_t digest[DIGEST_MAX_LEN];
112     uint16_t id;
113     uint8_t resType;
114     uint8_t type;
115     uint8_t compFlags;
116     size_t originalSize;
117 };
118 
119 /**
120  * Lz4 file configuration information
121  */
122 struct Lz4FileInfo {
123     FileInfo fileInfo;
124     int8_t compressionLevel;
125     int8_t blockIndependence;
126     int8_t contentChecksumFlag;
127     int8_t blockSizeID;
128     int8_t autoFlush = 1;
129 };
130 
131 /**
132  * Zip file configuration information
133  */
134 struct ZipFileInfo {
135     FileInfo fileInfo;
136     int32_t method = -1; // The system automatically uses the default value if the value is -1.
137     int32_t level;
138     int32_t windowBits;
139     int32_t memLevel;
140     int32_t strategy;
141 };
142 
143 /**
144  * buff definition used for parsing
145  */
146 struct PkgBuffer {
147     uint8_t *buffer;
148     size_t length = 0; // buffer size
149 
150     std::vector<uint8_t> data;
151 
PkgBufferPkgBuffer152     PkgBuffer()
153     {
154         this->buffer = nullptr;
155         this->length = 0;
156     }
157 
PkgBufferPkgBuffer158     PkgBuffer(uint8_t *buffer, size_t bufferSize)
159     {
160         this->buffer = buffer;
161         this->length = bufferSize;
162     }
163 
PkgBufferPkgBuffer164     PkgBuffer(std::vector<uint8_t> &buffer)
165     {
166         this->buffer = buffer.data();
167         this->length = buffer.capacity();
168     }
169 
PkgBufferPkgBuffer170     PkgBuffer(size_t bufferSize)
171     {
172         data.resize(bufferSize, 0);
173         this->buffer = data.data();
174         this->length = bufferSize;
175     }
176 };
177 } // namespace Hpackage
178 #endif // PKG_MANAGER_H
179