1 /*
2  * Copyright (c) 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 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 OHOS_HDI_CODEC_IMAGE_V2_0_HEIF_ENCODE_TEST_HELPER
17 #define OHOS_HDI_CODEC_IMAGE_V2_0_HEIF_ENCODE_TEST_HELPER
18 
19 #include <list>
20 #include <securec.h>
21 #include <sys/mman.h>
22 #include "hdf_log.h"
23 #include "ashmem.h"
24 #include "v2_0/icodec_image.h"
25 
26 #define IF_TRUE_RETURN_VAL(cond, val)  \
27     do {                               \
28         if (cond) {                    \
29             return val;                \
30         }                              \
31     } while (0)
32 
33 class PropWriter {
34 public:
35     PropWriter() = default;
~PropWriter()36     ~PropWriter()
37     {
38         for (auto iter = data_.begin(); iter != data_.end(); ++iter) {
39             delete [] iter->data;
40         }
41         data_.clear();
42     }
43     template <typename T>
AddData(OHOS::HDI::Codec::Image::V2_0::PropertyType key,T & value)44     bool AddData(OHOS::HDI::Codec::Image::V2_0::PropertyType key, T& value)
45     {
46         std::size_t keySize = sizeof(key);
47         std::size_t valueSize = sizeof(value);
48         std::size_t dataSize = keySize + valueSize;
49         uint8_t* p = new uint8_t[dataSize];
50         IF_TRUE_RETURN_VAL(p == nullptr, false);
51         data_.emplace_back(DataBlock {
52             .data = p,
53             .len = dataSize
54         });
55         totalSize_ += dataSize;
56         errno_t ret = memset_s(p, dataSize, 0, dataSize);
57         IF_TRUE_RETURN_VAL(ret != EOK, false);
58         ret = memcpy_s(p, dataSize, reinterpret_cast<uint8_t*>(&key), keySize);
59         IF_TRUE_RETURN_VAL(ret != EOK, false);
60         ret = memcpy_s(p + keySize, valueSize, reinterpret_cast<uint8_t*>(&value), valueSize);
61         IF_TRUE_RETURN_VAL(ret != EOK, false);
62         return true;
63     }
Finalize(std::vector<uint8_t> & dst)64     bool Finalize(std::vector<uint8_t>& dst)
65     {
66         dst.clear();
67         dst.resize(totalSize_);
68         uint8_t* dstStart = reinterpret_cast<uint8_t*>(dst.data());
69         size_t offset = 0;
70         errno_t ret = EOK;
71         for (auto iter = data_.begin(); (iter != data_.end()) && (ret == EOK); ++iter) {
72             ret = memcpy_s(dstStart + offset, iter->len, iter->data, iter->len);
73             offset += iter->len;
74         }
75         return (ret == EOK);
76     }
77 private:
78     struct DataBlock {
79         uint8_t* data = nullptr;
80         std::size_t len = 0;
81     };
82 private:
83     std::list<DataBlock> data_;
84     std::size_t totalSize_ = 0;
85 };
86 #endif // OHOS_HDI_CODEC_IMAGE_V2_0_HEIF_ENCODE_TEST_HELPER