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 "data_descriptor.h"
17#include "signature_tools_log.h"
18
19namespace OHOS {
20namespace SignatureTools {
21DataDescriptor* DataDescriptor::GetDataDescriptor(const std::string& bytes)
22{
23    if (bytes.size() != DES_LENGTH) {
24        SIGNATURE_TOOLS_LOGE("read Data Descriptor failed");
25        return nullptr;
26    }
27
28    ByteBuffer bf(bytes.c_str(), bytes.size());
29
30    DataDescriptor* data = new DataDescriptor();
31    int signValue;
32    bf.GetInt32(signValue);
33    if (signValue != SIGNATURE) {
34        delete data;
35        SIGNATURE_TOOLS_LOGE("read Data Descriptor failed");
36        return nullptr;
37    }
38    int crc2Value;
39    bf.GetInt32(crc2Value);
40    data->SetCrc32(crc2Value);
41
42    uint32_t dataDescUInt32Value;
43    bf.GetUInt32(dataDescUInt32Value);
44    data->SetCompressedSize(dataDescUInt32Value);
45
46    bf.GetUInt32(dataDescUInt32Value);
47    data->SetUnCompressedSize(dataDescUInt32Value);
48
49    return data;
50}
51
52std::string DataDescriptor::ToBytes()
53{
54    ByteBuffer bf(DES_LENGTH);
55    bf.PutInt32(SIGNATURE);
56    bf.PutInt32(m_crc32);
57    bf.PutUInt32(m_compressedSize);
58    bf.PutUInt32(m_unCompressedSize);
59
60    return bf.ToString();
61}
62
63int DataDescriptor::GetDesLength()
64{
65    return DES_LENGTH;
66}
67
68int DataDescriptor::GetSIGNATURE()
69{
70    return SIGNATURE;
71}
72
73int DataDescriptor::GetCrc32()
74{
75    return m_crc32;
76}
77
78void DataDescriptor::SetCrc32(int crc32)
79{
80    m_crc32 = crc32;
81}
82
83uint32_t DataDescriptor::GetCompressedSize()
84{
85    return m_compressedSize;
86}
87
88void DataDescriptor::SetCompressedSize(uint32_t compressedSize)
89{
90    m_compressedSize = compressedSize;
91}
92
93uint32_t DataDescriptor::GetUnCompressedSize()
94{
95    return m_unCompressedSize;
96}
97
98void DataDescriptor::SetUnCompressedSize(uint32_t unCompressedSize)
99{
100    m_unCompressedSize = unCompressedSize;
101}
102} // namespace SignatureTools
103} // namespace OHOS