1 /*
2 * Copyright (c) 2024-2025 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 "asset_system_api.h"
17
18 #include "securec.h"
19
20 #include "asset_log.h"
21 #include "asset_mem.h"
22
23 int32_t add_asset(const AssetAttr *attributes, uint32_t attr_cnt);
24 int32_t remove_asset(const AssetAttr *query, uint32_t query_cnt);
25 int32_t update_asset(const AssetAttr *query, uint32_t query_cnt,
26 const AssetAttr *attributes_to_update, uint32_t update_cnt);
27 int32_t pre_query_asset(const AssetAttr *query, uint32_t query_cnt, AssetBlob *challenge);
28 int32_t query_asset(const AssetAttr *query, uint32_t query_cnt, AssetResultSet *result_set);
29 int32_t post_query_asset(const AssetAttr *handle, uint32_t handle_cnt);
30 int32_t query_sync_result(const AssetAttr *query, uint32_t query_cnt, AssetSyncResult *sync_result);
31
AssetAdd(const AssetAttr * attributes,uint32_t attrCnt)32 int32_t AssetAdd(const AssetAttr *attributes, uint32_t attrCnt)
33 {
34 return add_asset(attributes, attrCnt);
35 }
36
AssetRemove(const AssetAttr * query,uint32_t queryCnt)37 int32_t AssetRemove(const AssetAttr *query, uint32_t queryCnt)
38 {
39 return remove_asset(query, queryCnt);
40 }
41
AssetUpdate(const AssetAttr * query,uint32_t queryCnt,const AssetAttr * attributesToUpdate,uint32_t updateCnt)42 int32_t AssetUpdate(const AssetAttr *query, uint32_t queryCnt,
43 const AssetAttr *attributesToUpdate, uint32_t updateCnt)
44 {
45 return update_asset(query, queryCnt, attributesToUpdate, updateCnt);
46 }
47
AssetPreQuery(const AssetAttr * query,uint32_t queryCnt,AssetBlob * challenge)48 int32_t AssetPreQuery(const AssetAttr *query, uint32_t queryCnt, AssetBlob *challenge)
49 {
50 return pre_query_asset(query, queryCnt, challenge);
51 }
52
AssetQuery(const AssetAttr * query,uint32_t queryCnt,AssetResultSet * resultSet)53 int32_t AssetQuery(const AssetAttr *query, uint32_t queryCnt, AssetResultSet *resultSet)
54 {
55 return query_asset(query, queryCnt, resultSet);
56 }
57
AssetPostQuery(const AssetAttr * handle,uint32_t handleCnt)58 int32_t AssetPostQuery(const AssetAttr *handle, uint32_t handleCnt)
59 {
60 return post_query_asset(handle, handleCnt);
61 }
62
AssetQuerySyncResult(const AssetAttr * query,uint32_t queryCnt,AssetSyncResult * syncResult)63 int32_t AssetQuerySyncResult(const AssetAttr *query, uint32_t queryCnt, AssetSyncResult *syncResult)
64 {
65 return query_sync_result(query, queryCnt, syncResult);
66 }
67
AssetParseAttr(const AssetResult * result,AssetTag tag)68 AssetAttr *AssetParseAttr(const AssetResult *result, AssetTag tag)
69 {
70 if (result == NULL || result->attrs == NULL || result->count == 0) {
71 LOGE("[FATAL][SDK]Argument is NULL.");
72 return NULL;
73 }
74 for (uint32_t i = 0; i < result->count; i++) {
75 if (result->attrs[i].tag == tag) {
76 return &result->attrs[i];
77 }
78 }
79 LOGE("[FATAL][SDK]Attribute not found.");
80 return NULL;
81 }
82
AssetFreeBlob(AssetBlob * blob)83 void AssetFreeBlob(AssetBlob *blob)
84 {
85 if (blob == NULL || blob->data == NULL || blob->size == 0) {
86 return;
87 }
88 (void)memset_s(blob->data, blob->size, 0, blob->size);
89 AssetFree(blob->data);
90 blob->data = NULL;
91 blob->size = 0;
92 }
93
AssetFreeResultSet(AssetResultSet * resultSet)94 void AssetFreeResultSet(AssetResultSet *resultSet)
95 {
96 if (resultSet == NULL || resultSet->results == NULL || resultSet->count == 0) {
97 return;
98 }
99
100 for (uint32_t i = 0; i < resultSet->count; i++) {
101 AssetAttr *attrs = resultSet->results[i].attrs;
102 uint32_t attrCnt = resultSet->results[i].count;
103 if (attrs == NULL || attrCnt == 0) {
104 continue;
105 }
106 for (uint32_t j = 0; j < attrCnt; j++) {
107 if ((attrs[j].tag & SEC_ASSET_TAG_TYPE_MASK) == SEC_ASSET_TYPE_BYTES) {
108 AssetFreeBlob(&attrs[j].value.blob);
109 }
110 }
111 AssetFree(resultSet->results[i].attrs);
112 resultSet->results[i].attrs = NULL;
113 resultSet->results[i].count = 0;
114 }
115 AssetFree(resultSet->results);
116 resultSet->results = NULL;
117 resultSet->count = 0;
118 }