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 <cstring> 17 18 #ifdef IOS_PLATFORM 19 #include <sys/sysctl.h> 20 #else 21 #include <sys/sysinfo.h> 22 #endif 23 24 #include <unistd.h> 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 28 #ifndef UTIL_JS_BASE64_H 29 #define UTIL_JS_BASE64_H 30 31 namespace OHOS::Util { 32 struct EncodeInfo { 33 napi_async_work worker = nullptr; 34 napi_deferred deferred = nullptr; 35 napi_value promise = nullptr; 36 unsigned char *sinputEncode = nullptr; 37 unsigned char *sinputEncoding = nullptr; 38 size_t slength = 0; 39 size_t soutputLen = 0; 40 napi_env env; 41 }; 42 43 struct DecodeInfo { 44 napi_async_work worker = nullptr; 45 napi_deferred deferred = nullptr; 46 napi_value promise = nullptr; 47 char *sinputDecode = nullptr; 48 unsigned char *sinputDecoding = nullptr; 49 size_t slength = 0; 50 size_t decodeOutLen = 0; 51 size_t retLen = 0; 52 napi_env env; 53 }; 54 55 enum ConverterFlags { 56 BIT_FLG = 0x40, 57 SIXTEEN_FLG = 0x3F, 58 XFF_FLG = 0xFF, 59 }; 60 61 void FreeMemory(unsigned char *address); 62 void FreeMemory(char *address); 63 unsigned char *EncodeAchieves(EncodeInfo *encodeInfo); 64 unsigned char *DecodeAchieves(DecodeInfo *decodeInfo); 65 66 class Base64 { 67 public: 68 /** 69 * Constructor of Base64. 70 */ Base64()71 explicit Base64() {} 72 73 /** 74 * Destructor of Base64. 75 */ ~Base64()76 virtual ~Base64() {} 77 78 /** 79 * Output the corresponding text after encoding the input parameters. 80 * 81 * @param env NAPI environment parameters. 82 * @param src Encode the input uint8 array. 83 */ 84 napi_value EncodeSync(napi_env env, napi_value src); 85 86 /** 87 * Output the corresponding text after encoding the input parameters. 88 * 89 * @param env NAPI environment parameters. 90 * @param src Encode the input uint8 array. 91 */ 92 napi_value EncodeToStringSync(napi_env env, napi_value src); 93 94 /** 95 * Output the corresponding text after encoding the input parameters. 96 * 97 * @param env NAPI environment parameters. 98 * @param src Decode the input uint8 array or string. 99 */ 100 napi_value DecodeSync(napi_env env, napi_value src); 101 102 /** 103 * Output the corresponding text after asynchronously encoding the input parameters. 104 * 105 * @param env NAPI environment parameters. 106 * @param src Asynchronously encoded input uint8 array. 107 */ 108 napi_value Encode(napi_env env, napi_value src); 109 110 /** 111 * Output the corresponding text after asynchronously encoding the input parameters. 112 * 113 * @param env NAPI environment parameters. 114 * @param src Asynchronously encoded input uint8 array. 115 */ 116 napi_value EncodeToString(napi_env env, napi_value src); 117 118 /** 119 * Output the corresponding text after asynchronously encoding the input parameters. 120 * 121 * @param env NAPI environment parameters. 122 * @param src Asynchronously decode the input uint8 array or string. 123 */ 124 napi_value Decode(napi_env env, napi_value src); 125 126 private: 127 unsigned char *DecodeAchieve(napi_env env, const char *input, size_t inputLen); 128 unsigned char *EncodeAchieve(const unsigned char *input, size_t inputLen); 129 size_t Finds(napi_env env, char ch); 130 size_t DecodeOut(size_t equalCount, size_t retLen); 131 size_t retLen = 0; 132 size_t decodeOutLen = 0; 133 size_t outputLen = 0; 134 unsigned char *pret = nullptr; 135 const unsigned char *inputEncode_ = nullptr; 136 const char *inputDecode_ = nullptr; 137 unsigned char *retDecode = nullptr; 138 void CreateEncodePromise(napi_env env, unsigned char *inputDecode, size_t length); 139 void CreateEncodeToStringPromise(napi_env env, unsigned char *inputDecode, size_t length); 140 void CreateDecodePromise(napi_env env, char *inputDecode, size_t length); 141 EncodeInfo *stdEncodeInfo_ = nullptr; 142 DecodeInfo *stdDecodeInfo_ = nullptr; 143 static void ReadStdEncode(napi_env env, void *data); 144 static void EndStdEncode(napi_env env, napi_status status, void *buffer); 145 static void ReadStdEncodeToString(napi_env env, void *data); 146 static void EndStdEncodeToString(napi_env env, napi_status status, void *buffer); 147 static void ReadStdDecode(napi_env env, void *data); 148 static void EndStdDecode(napi_env env, napi_status status, void *buffer); 149 }; 150 } 151 #endif // UTIL_JS_BASE64_H 152