1 /*
2 * Copyright (c) 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 "net_websocket_utils.h"
17
18 #include "securec.h"
19 #include "netstack_log.h"
20
21 namespace OHOS::NetStack::NetWebSocket {
22
MallocUInt8(const std::string & origin)23 uint8_t* MallocUInt8(const std::string& origin)
24 {
25 auto len = origin.length();
26 if (len <= 0) {
27 return nullptr;
28 }
29 char* res = static_cast<char*>(malloc(sizeof(char) * len));
30 if (res == nullptr) {
31 return nullptr;
32 }
33 return reinterpret_cast<uint8_t*>(std::char_traits<char>::copy(res, origin.c_str(), len));
34 }
35
MallocCString(const std::string & origin)36 char* MallocCString(const std::string &origin)
37 {
38 if (origin.empty()) {
39 return nullptr;
40 }
41 auto len = origin.length() + 1;
42 char *res = static_cast<char *>(malloc(sizeof(char) * len));
43 if (res == nullptr) {
44 return nullptr;
45 }
46 return std::char_traits<char>::copy(res, origin.c_str(), len);
47 }
48
Map2CArrString(std::map<std::string,std::string> map)49 CArrString Map2CArrString(std::map<std::string, std::string> map)
50 {
51 auto size = map.size() * MAP_TUPLE_SIZE;
52 CArrString ret{ .head = nullptr, .size = 0};
53 if (size <= 0) {
54 return ret;
55 }
56 ret.head = static_cast<char**>(malloc(sizeof(char*) * size));
57 if (ret.head == nullptr) {
58 return ret;
59 }
60 ret.size = static_cast<int64_t>(size);
61 int index = 0;
62 for (const auto& [key, value] : map) {
63 ret.head[index] = MallocCString(key);
64 ret.head[index + 1] = MallocCString(value);
65 index += MAP_TUPLE_SIZE;
66 }
67 return ret;
68 }
69
FreeCArrString(CArrString & arrStr)70 void FreeCArrString(CArrString& arrStr)
71 {
72 if (arrStr.head == nullptr) {
73 return;
74 }
75 for (int64_t i = 0; i < arrStr.size; i++) {
76 free(arrStr.head[i]);
77 }
78 free(arrStr.head);
79 arrStr.head = nullptr;
80 }
81
SecureChar()82 SecureChar::SecureChar() : data_(std::make_unique<char[]>(0)) {}
83
~SecureChar()84 SecureChar::~SecureChar()
85 {
86 (void)memset_s(data_.get(), length_, 0, length_);
87 }
88
SecureChar(const std::string & secureChar)89 SecureChar::SecureChar(const std::string &secureChar)
90 : length_(secureChar.length()), data_(std::make_unique<char[]>(length_ + 1))
91 {
92 if (length_ == 0) {
93 return;
94 }
95 data_.get()[length_] = 0;
96 if (memcpy_s(data_.get(), length_, secureChar.c_str(), length_) != ERR_OK) {
97 NETSTACK_LOGE("memcpy_s failed!");
98 return;
99 }
100 }
101
SecureChar(const uint8_t * secureChar,size_t length)102 SecureChar::SecureChar(const uint8_t *secureChar, size_t length)
103 {
104 data_ = std::make_unique<char[]>(length + 1);
105 length_ = length;
106 data_.get()[length_] = 0;
107 if (memcpy_s(data_.get(), length_, secureChar, length_) != ERR_OK) {
108 NETSTACK_LOGE("memcpy_s failed!");
109 }
110 }
111
SecureChar(const SecureChar & secureChar)112 SecureChar::SecureChar(const SecureChar &secureChar)
113 {
114 *this = secureChar;
115 }
116
operator =(const SecureChar & secureChar)117 SecureChar &SecureChar::operator=(const SecureChar &secureChar)
118 {
119 if (this != &secureChar) {
120 if (secureChar.Length() == 0) {
121 return *this;
122 }
123 length_ = secureChar.Length();
124 data_ = std::make_unique<char[]>(length_ + 1);
125 data_.get()[length_] = 0;
126 if (memcpy_s(data_.get(), length_, secureChar.Data(), length_) != ERR_OK) {
127 NETSTACK_LOGE("memcpy_s failed!");
128 }
129 }
130 return *this;
131 }
132
Data() const133 const char *SecureChar::Data() const
134 {
135 return data_.get();
136 }
137
Length() const138 size_t SecureChar::Length() const
139 {
140 return length_;
141 }
142 }