1/*
2 * Copyright (C) 2022 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 "idm_common.h"
17
18#include "securec.h"
19
20#include "adaptor_log.h"
21#include "adaptor_memory.h"
22
23void DestroyUserInfoNode(void *userInfo)
24{
25    if (userInfo == NULL) {
26        LOG_ERROR("userInfo is null");
27        return;
28    }
29    UserInfo *node = (UserInfo*)userInfo;
30    DestroyLinkedList(node->credentialInfoList);
31    DestroyLinkedList(node->enrolledInfoList);
32    Free(node);
33}
34
35void DestroyCredentialNode(void *credential)
36{
37    if (credential == NULL) {
38        LOG_ERROR("credential is null");
39        return;
40    }
41    Free(credential);
42}
43
44void DestroyEnrolledNode(void *enrolled)
45{
46    if (enrolled == NULL) {
47        LOG_ERROR("enrolled is null");
48        return;
49    }
50    Free(enrolled);
51}
52
53UserInfo *InitUserInfoNode(void)
54{
55    UserInfo *userInfo = Malloc(sizeof(UserInfo));
56    if (userInfo == NULL) {
57        LOG_ERROR("userInfo malloc failed");
58        return NULL;
59    }
60    (void)memset_s(userInfo, sizeof(UserInfo), 0, sizeof(UserInfo));
61
62    userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
63    if (userInfo->credentialInfoList == NULL) {
64        LOG_ERROR("create credentialInfoList failed");
65        Free(userInfo);
66        return NULL;
67    }
68    userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
69    if (userInfo->enrolledInfoList == NULL) {
70        LOG_ERROR("create enrolledInfoList failed");
71        DestroyLinkedList(userInfo->credentialInfoList);
72        Free(userInfo);
73        return NULL;
74    }
75    return userInfo;
76}