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 #ifndef ARKCOMPILER_TOOLCHAIN_WEBSOCKET_NETWORK_H
17 #define ARKCOMPILER_TOOLCHAIN_WEBSOCKET_NETWORK_H
18
19 #include <string>
20
21 namespace OHOS::ArkCompiler::Toolchain {
22 // Receives a message of size `buffer.size()`. Clears the string buffer on error.
23 bool Recv(int32_t client, std::string& buffer, int32_t flags);
24
25 bool Recv(int32_t client, char* buf, size_t totalLen, int32_t flags);
26
27 bool Recv(int32_t client, uint8_t* buf, size_t totalLen, int32_t flags);
28
29 bool Send(int32_t client, const std::string& message, int32_t flags);
30
31 bool Send(int32_t client, const char* buf, size_t totalLen, int32_t flags);
32
33 uint64_t NetToHostLongLong(uint8_t* buf, uint32_t len);
34
GetBase64EncodingLength(size_t inputLength)35 constexpr inline size_t GetBase64EncodingLength(size_t inputLength)
36 {
37 size_t paddingOffset = 2;
38 // base64-encoding produces padded output of 4 characters for every 3 bytes input
39 return ((inputLength + paddingOffset) / 3) * 4;
40 }
41 } // namespace OHOS::ArkCompiler::Toolchain
42
43 #endif // ARKCOMPILER_TOOLCHAIN_WEBSOCKET_NETWORK_H
44