1 /*
2  * Copyright (c) 2024-2024 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 #include <cinttypes>
17 #include <algorithm>
18 #include <stdexcept>
19 #include <string>
20 
21 #include "zip_utils.h"
22 
23 namespace OHOS {
24 namespace SignatureTools {
SetCentralDirectoryOffset(ByteBuffer& eocd, int64_t offset)25 bool ZipUtils::SetCentralDirectoryOffset(ByteBuffer& eocd, int64_t offset)
26 {
27     if (!SetUInt32ToBuffer(eocd, eocd.GetPosition() + ZIP_CENTRAL_DIR_OFFSET_IN_EOCD, offset)) {
28         SIGNATURE_TOOLS_LOGE("Set Central Directory Offset failed.");
29         return false;
30     }
31     return true;
32 }
33 
SetUInt32ToBuffer(ByteBuffer& buffer, int offset, int64_t value)34 bool ZipUtils::SetUInt32ToBuffer(ByteBuffer& buffer, int offset, int64_t value)
35 {
36     SIGNATURE_TOOLS_LOGI("offset: %d, value: %" PRId64 ", UINT32_MAX_VALUE: %" PRId64,
37                          offset, value, UINT32_MAX_VALUE);
38     if ((value < 0) || (value > UINT32_MAX_VALUE)) {
39         SIGNATURE_TOOLS_LOGE("invalid_argument. value of out range: %" PRId64, value);
40         return false;
41     }
42     buffer.PutInt32(offset, static_cast<int>(value));
43     return true;
44 }
45 } // namespace SignatureTools
46 } // namespace OHOS