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 if (array->data != NULL) {
73 for (uint32_t i = 0; i < array->count; ++i) {
74 CfFree(array->data[i].data);
75 array->data[i].data = NULL;
76 array->data[i].size = 0;
77 }
78 CfFree(array->data);
79 }
80 array->count = 0;
81 array->data = NULL;
82 }
83
FreeCfBlobArray(CfBlob * array,uint32_t arrayLen)84 void FreeCfBlobArray(CfBlob *array, uint32_t arrayLen)
85 {
86 if (array == NULL) {
87 return;
88 }
89
90 for (uint32_t i = 0; i < arrayLen; ++i) {
91 array[i].size = 0;
92 CF_FREE_PTR(array[i].data);
93 }
94
95 CfFree(array);
96 }
97
CfBlobIsStr(const CfBlob * blob)98 bool CfBlobIsStr(const CfBlob *blob)
99 {
100 if (blob == NULL || blob->data == NULL || blob->size == 0) {
101 return false;
102 }
103 if (blob->data[blob->size - 1] == 0) {
104 return true;
105 }
106 return false;
107 }
108