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 #include "cf_blob.h"
17
18 #include <securec.h>
19 #include "cf_memory.h"
20 #include "cf_log.h"
21 #include "cf_result.h"
22
CfBlobFree(CfBlob ** blob)23 void CfBlobFree(CfBlob **blob)
24 {
25 if (blob == NULL) {
26 return;
27 }
28 CfBlobDataFree(*blob);
29 CfFree(*blob);
30 *blob = NULL;
31 }
32
CfBlobDataFree(CfBlob * blob)33 void CfBlobDataFree(CfBlob *blob)
34 {
35 if ((blob == NULL) || (blob->data == NULL)) {
36 return;
37 }
38 CfFree(blob->data);
39 blob->data = NULL;
40 blob->size = 0;
41 }
42
CfBlobDataClearAndFree(CfBlob * blob)43 void CfBlobDataClearAndFree(CfBlob *blob)
44 {
45 if ((blob == NULL) || (blob->data == NULL)) {
46 LOGD("The input blob is null, no need to free.");
47 return;
48 }
49 (void)memset_s(blob->data, blob->size, 0, blob->size);
50 CfFree(blob->data);
51 blob->data = NULL;
52 blob->size = 0;
53 }
54
CfEncodingBlobDataFree(CfEncodingBlob * encodingBlob)55 void CfEncodingBlobDataFree(CfEncodingBlob *encodingBlob)
56 {
57 if ((encodingBlob == NULL) || (encodingBlob->data == NULL)) {
58 LOGD("The input encodingBlob is null, no need to free.");
59 return;
60 }
61 CfFree(encodingBlob->data);
62 encodingBlob->data = NULL;
63 encodingBlob->len = 0;
64 }
65
CfArrayDataClearAndFree(CfArray * array)66 void CfArrayDataClearAndFree(CfArray *array)
67 {
68 if (array == NULL) {
69 LOGD("The input array is null, no need to free.");
70 return;
71 }
72 for (uint32_t i = 0; i < array->count; ++i) {
73 CfFree(array->data[i].data);
74 array->data[i].data = NULL;
75 array->data[i].size = 0;
76 }
77 array->count = 0;
78 CfFree(array->data);
79 array->data = NULL;
80 }
81
FreeCfBlobArray(CfBlob * array,uint32_t arrayLen)82 void FreeCfBlobArray(CfBlob *array, uint32_t arrayLen)
83 {
84 if (array == NULL) {
85 return;
86 }
87
88 for (uint32_t i = 0; i < arrayLen; ++i) {
89 array[i].size = 0;
90 CF_FREE_PTR(array[i].data);
91 }
92
93 CfFree(array);
94 }
95
CfBlobIsStr(const CfBlob * blob)96 bool CfBlobIsStr(const CfBlob *blob)
97 {
98 if (blob == NULL || blob->data == NULL || blob->size == 0) {
99 return false;
100 }
101 if (blob->data[blob->size - 1] == 0) {
102 return true;
103 }
104 return false;
105 }
106