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