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 "tlv_base.h"
17
18 #include "securec.h"
19
20 #include "adaptor_memory.h"
21
22 #define TO_RIGHT_END 24
23 #define TO_RIGHT 8
24 #define TO_LEFT_END 24
25 #define TO_LEFT 8
26
27 // short convert endian
Ntohs(uint16_t data)28 uint16_t Ntohs(uint16_t data)
29 {
30 return data;
31 }
32
33 // uint32 convert endian
Ntohl(uint32_t data)34 uint32_t Ntohl(uint32_t data)
35 {
36 return data;
37 }
38
39 // uint64 convert endian
Ntohll(uint64_t data)40 uint64_t Ntohll(uint64_t data)
41 {
42 return data;
43 }
44
CreateTlvList(void)45 TlvListNode *CreateTlvList(void)
46 {
47 TlvListNode *node = (TlvListNode *)Malloc(sizeof(TlvListNode));
48 if (node == NULL) {
49 return NULL;
50 }
51 node->data.value = NULL;
52 node->next = NULL;
53 return node;
54 }
55
CreateTlvType(int32_t type,uint32_t length,const void * value)56 TlvType *CreateTlvType(int32_t type, uint32_t length, const void *value)
57 {
58 if (value == NULL || length == 0) {
59 return NULL;
60 }
61 TlvType *tlv = (TlvType *)Malloc(sizeof(TlvType));
62 if (tlv == NULL) {
63 return NULL;
64 }
65
66 tlv->type = type;
67 tlv->length = length;
68 tlv->value = (uint8_t *)Malloc(length);
69 if (tlv->value == NULL) {
70 Free(tlv);
71 return NULL;
72 }
73
74 if (memcpy_s(tlv->value, length, value, length) != EOK) {
75 Free(tlv->value);
76 tlv->value = NULL;
77 Free(tlv);
78 return NULL;
79 }
80 return tlv;
81 }
82
DestroyTlvList(TlvListNode * head)83 int32_t DestroyTlvList(TlvListNode *head)
84 {
85 if (head == NULL) {
86 return PARAM_ERR;
87 }
88 TlvListNode *currNode = head->next;
89 while (currNode != NULL) {
90 TlvListNode *nextNode = currNode->next;
91 TlvType *tlv = currNode->data.value;
92 if (tlv != NULL) {
93 if (tlv->value != NULL) {
94 Free(tlv->value);
95 }
96 tlv->value = NULL;
97 Free(tlv);
98 tlv = NULL;
99 }
100 Free(currNode);
101 currNode = nextNode;
102 }
103 Free(head);
104 return OPERA_SUCC;
105 }
106
AddTlvNode(TlvListNode * head,const TlvObject * object)107 int32_t AddTlvNode(TlvListNode *head, const TlvObject *object)
108 {
109 if (head == NULL || object == NULL) {
110 return PARAM_ERR;
111 }
112
113 TlvListNode *node = (TlvListNode *)Malloc(sizeof(TlvListNode));
114 if (node == NULL) {
115 return MALLOC_FAIL;
116 }
117 node->data = *object;
118 node->next = NULL;
119 TlvListNode *temp = head->next;
120 if (temp == NULL) {
121 head->next = node;
122 } else {
123 while (temp->next != NULL) {
124 temp = temp->next;
125 }
126 temp->next = node;
127 }
128 return OPERA_SUCC;
129 }