1/*
2 * Copyright (C) 2023-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 "c_array.h"
17
18#include "securec.h"
19
20#include "adaptor_log.h"
21#include "adaptor_memory.h"
22
23void DestroyUint8Array(Uint8Array **array)
24{
25    IF_TRUE_LOGE_AND_RETURN(array == NULL);
26    if (*array == NULL) {
27        return;
28    }
29    if ((*array)->len != 0 && ((*array)->len < UINT32_MAX / sizeof(uint8_t))) {
30        uint32_t arraySize = sizeof(uint8_t) * (*array)->len;
31        (void)memset_s((*array)->data, arraySize, 0, arraySize);
32    }
33    IAM_FREE_AND_SET_NULL((*array)->data);
34    IAM_FREE_AND_SET_NULL(*array);
35}
36
37void DestroyUint64Array(Uint64Array **array)
38{
39    IF_TRUE_LOGE_AND_RETURN(array == NULL);
40    if (*array == NULL) {
41        return;
42    }
43    if ((*array)->len != 0 && ((*array)->len < UINT32_MAX / sizeof(uint64_t))) {
44        uint32_t arraySize = sizeof(uint64_t) * (*array)->len;
45        (void)memset_s((*array)->data, arraySize, 0, arraySize);
46    }
47    IAM_FREE_AND_SET_NULL((*array)->data);
48    IAM_FREE_AND_SET_NULL(*array);
49}
50
51Uint8Array *CreateUint8ArrayBySize(uint32_t size)
52{
53    Uint8Array *array = Malloc(sizeof(Uint8Array));
54    IF_TRUE_LOGE_AND_RETURN_VAL(array == NULL, NULL);
55    if (size == 0) {
56        LOG_INFO("create an empty uint8_t array");
57        return array;
58    }
59    array->data = Malloc(size);
60    if (array->data == NULL) {
61        LOG_ERROR("malloc fail");
62        Free(array);
63        return NULL;
64    }
65    array->len = size;
66
67    return array;
68}
69
70Uint8Array *CreateUint8ArrayByData(const uint8_t *data, uint32_t len)
71{
72    if (data == NULL && len != 0) {
73        LOG_ERROR("invalid para");
74        return NULL;
75    }
76
77    Uint8Array *array = CreateUint8ArrayBySize(len);
78    IF_TRUE_LOGE_AND_RETURN_VAL(array == NULL, NULL);
79    if (len == 0) {
80        return array;
81    }
82
83    if (memcpy_s(array->data, len, data, len) != EOK) {
84        LOG_ERROR("memcpy fail");
85        DestroyUint8Array(&array);
86        return NULL;
87    }
88    array->len = len;
89
90    return array;
91}
92
93Uint64Array *CreateUint64ArrayByData(const uint64_t *data, uint32_t len)
94{
95    if ((data == NULL && len != 0) || (len > UINT32_MAX / sizeof(uint64_t))) {
96        LOG_ERROR("invalid para");
97        return NULL;
98    }
99
100    Uint64Array *array = Malloc(sizeof(Uint64Array));
101    IF_TRUE_LOGE_AND_RETURN_VAL(array == NULL, NULL);
102
103    if (len == 0) {
104        return array;
105    }
106
107    array->data = Malloc(len * sizeof(uint64_t));
108    if (array->data == NULL) {
109        LOG_ERROR("malloc fail");
110        Free(array);
111        return NULL;
112    }
113
114    if (memcpy_s(array->data, len * sizeof(uint64_t), data, len * sizeof(uint64_t)) != EOK) {
115        LOG_ERROR("memcpy fail");
116        DestroyUint64Array(&array);
117        return NULL;
118    }
119    array->len = len;
120
121    return array;
122}