• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_ARRAY_BUFFER_HELPER
17 #define PANDA_PLUGINS_ETS_RUNTIME_ARRAY_BUFFER_HELPER
18 
19 #include <string_view>
20 #include <cstdint>
21 #include <variant>
22 
23 #include "include/mem/panda_string.h"
24 #include "include/mem/panda_containers.h"
25 
26 namespace ark::ets {
27 class EtsEscompatArrayBuffer;
28 }  // namespace ark::ets
29 
30 namespace ark::ets::intrinsics::helpers {
31 
32 template <typename T>
33 class Err {
34 private:
35     T errorMessage_;
36 
37 public:
Err(T msg)38     explicit Err(T msg) : errorMessage_(std::move(msg)) {}
Message()39     const T &Message() const
40     {
41         return errorMessage_;
42     }
43 };
44 template <typename T>
45 using Result = std::variant<T, Err<PandaString>>;
46 
47 namespace base64 {
48 /**
49  * @brief Validates whether the input string is a valid Base64-encoded string.
50  *
51  * @param input The Base64 string to validate.
52  * @return true if valid, false otherwise.
53  */
54 [[nodiscard]] bool ValidateBase64Input(std::string_view input) noexcept;
55 
56 [[nodiscard]] PandaString Decode(std::string_view encodedData);
57 [[nodiscard]] PandaString Encode(const PandaVector<uint8_t> &binaryData);
58 
59 }  // namespace base64
60 
61 namespace encoding {
62 
63 [[nodiscard]] Result<bool> ValidateBuffer(const EtsEscompatArrayBuffer *buffer) noexcept;
64 [[nodiscard]] Result<bool> ValidateIndices(int byteLength, int start, int end);
65 
66 /**
67  * @brief Retrieves the encoding string from an optional encoding pointer.
68  * Defaults to "utf8" if the pointer is null.
69  */
70 [[nodiscard]] PandaString GetEncoding(const PandaString *encodingObj) noexcept;
71 
72 PandaString ConvertUtf8Encoding(const PandaVector<uint8_t> &bytes);
73 
74 PandaString ConvertUtf16Encoding(const PandaVector<uint8_t> &bytes);
75 
76 PandaString ConvertBase64Encoding(const PandaVector<uint8_t> &bytes, std::string_view encoding);
77 
78 PandaString ConvertHexEncoding(const PandaVector<uint8_t> &bytes);
79 
80 PandaString ConvertLatinEncoding(const PandaVector<uint8_t> &bytes);
81 
82 Result<PandaVector<uint8_t>> ConvertStringToBytes(const PandaString &input, std::string_view encoding);
83 
84 /// @brief Calculates the number of bytes required to represent the string in the given encoding.
85 Result<int32_t> CalculateStringBytesLength(std::string_view input, std::string_view encoding);
86 
87 }  // namespace encoding
88 
89 }  // namespace ark::ets::intrinsics::helpers
90 
91 #endif  // PANDA_PLUGINS_ETS_RUNTIME_ARRAY_BUFFER_HELPER
92