mirror of
https://github.com/openharmony/js_util_module.git
synced 2026-07-20 21:58:22 -04:00
@@ -102,12 +102,12 @@ base/compileruntime/js_util_module/
|
||||
| readonly fatal : boolean | Get the setting that throws the exception. |
|
||||
| readonly ignoreBOM : boolean | Get whether to ignore the setting of the bom flag. |
|
||||
| decode(input : Uint8Array, options?: { stream?: false }) : string | Input the data to be decoded, and solve the corresponding string character string.The first parameter input represents the data to be decoded, and the second parameter options represents a bool flag, which means that additional data will be followed. The default is false. |
|
||||
| encode(src: Uint8Array, flag: number): Uint8Array; | Encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
|
||||
| encodeToString(src: Uint8Array, flag: number): string; | Encodes the specified byte array as a String using the Base64 encoding scheme. |
|
||||
| decode(src: Uint8Array \| string, flag: number): Uint8Array; | Decodes the Base64-encoded string or input u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
|
||||
| encodeAsync(src: Uint8Array, flag: number): Promise\<Uint8Array\>; | Asynchronously encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
|
||||
| encodeToStringAsync(src: Uint8Array, flag: number): Promise\<string\>; | Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. |
|
||||
| decodeAsync(src: Uint8Array \| string, flag: number): Promise\<Uint8Array\>; | Use the Base64 encoding scheme to asynchronously decode a Base64-encoded string or input u8 array into a newly allocated u8 array. |
|
||||
| encode(src: Uint8Array): Uint8Array; | Encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
|
||||
| encodeToString(src: Uint8Array): string; | Encodes the specified byte array as a String using the Base64 encoding scheme. |
|
||||
| decode(src: Uint8Array \| string): Uint8Array; | Decodes the Base64-encoded string or input u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
|
||||
| encodeAsync(src: Uint8Array): Promise\<Uint8Array\>; | Asynchronously encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
|
||||
| encodeToStringAsync(src: Uint8Array): Promise\<string\>; | Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. |
|
||||
| decodeAsync(src: Uint8Array \| string): Promise\<Uint8Array\>; | Use the Base64 encoding scheme to asynchronously decode a Base64-encoded string or input u8 array into a newly allocated u8 array. |
|
||||
| static createRationalFromString(rationalString: string): RationalNumber | Create a RationalNumber object based on the given string. |
|
||||
| compareTo(another: RationalNumber): number | Compare the current RationalNumber object with the given object. |
|
||||
| equals(obj: object): number | Check if the given object is the same as the current RationalNumber object.|
|
||||
@@ -271,59 +271,45 @@ newPromiseObj.then(res => {
|
||||
import util from '@ohos.util'
|
||||
var that = new util.Base64();
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
var result = that.encode(array,num);
|
||||
var result = that.encode(array);
|
||||
```
|
||||
14.encodeToString()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var that = new util.Base64();
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
var result = that.encodeToString(array,num);
|
||||
var result = that.encodeToString(array);
|
||||
```
|
||||
15.decode()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var that = new util.Base64()
|
||||
var buff = 'czEz';
|
||||
var num = 0;
|
||||
var result = that.decode(buff,num);
|
||||
var result = that.decode(buff);
|
||||
```
|
||||
16.encodeAsync()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
it('EncodeAsync_test_001', 0, async function () {
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var rarray = new Uint8Array([99,122,69,122]);
|
||||
var num = 0;
|
||||
that.encodeAsync(array,num).then(val=>{
|
||||
}
|
||||
})
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
that.encodeAsync(array).then(val=>{
|
||||
}
|
||||
```
|
||||
7.encodeToStringAsync()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
it('EncodeToStringAsync_test_001', 0, async function () {
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
that.encodeToStringAsync(array,num).then(val=>{
|
||||
})
|
||||
}),
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
that.encodeToStringAsync(array).then(val=>{
|
||||
})
|
||||
```
|
||||
18.decodeAsync()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
it('DecodeAsync_test_001', 0, async function () {
|
||||
var that = new util.Base64()
|
||||
var buff = 'czEz';
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
that.decodeAsync(buff,num).then(val=>{
|
||||
})
|
||||
}),
|
||||
var that = new util.Base64()
|
||||
var buff = 'czEz';
|
||||
that.decodeAsync(buff).then(val=>{
|
||||
})
|
||||
```
|
||||
19.createRationalFromString()
|
||||
```
|
||||
|
||||
+21
-35
@@ -103,12 +103,12 @@ base/compileruntime/js_util_module/
|
||||
| readonly fatal : boolean | 获取抛出异常的设置。 |
|
||||
| readonly ignoreBOM : boolean | 获取是否忽略bom标志的设置。 |
|
||||
| decode(input : Uint8Array, options?: { stream?: false }) : string | 输入要解码的数据,解出对应的string字符串。第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。 |
|
||||
| encode(src: Uint8Array, flag: number): Uint8Array; | 使用Base64编码方案将指定u8数组中的所有字节编码到新分配的u8数组中。 |
|
||||
| encodeToString(src: Uint8Array,flag: number): string; | 使用Base64编码方案将指定的字节数组编码为String。 |
|
||||
| decode(src: Uint8Array \| string, flag: number): Uint8Array; | 使用Base64编码方案将Base64编码的字符串或输入u8数组解码为新分配的u8数组。 |
|
||||
| encodeAsync(src: Uint8Array, flag: number): Promise\<Uint8Array\>; | 使用Base64编码方案将指定u8数组中的所有字节异步编码到新分配的u8数组中。 |
|
||||
| encodeToStringAsync(src: Uint8Array, flag: number): Promise\<string\>; | 使用Base64编码方案将指定的字节数组异步编码为String。 |
|
||||
| decodeAsync(src: Uint8Array \| string, flag: number): Promise\<Uint8Array\>; | 使用Base64编码方案将Base64编码的字符串或输入u8数组异步解码为新分配的u8数组。 |
|
||||
| encode(src: Uint8Array): Uint8Array; | 使用Base64编码方案将指定u8数组中的所有字节编码到新分配的u8数组中。 |
|
||||
| encodeToString(src: Uint8Array): string; | 使用Base64编码方案将指定的字节数组编码为String。 |
|
||||
| decode(src: Uint8Array \| string): Uint8Array; | 使用Base64编码方案将Base64编码的字符串或输入u8数组解码为新分配的u8数组。 |
|
||||
| encodeAsync(src: Uint8Array): Promise\<Uint8Array\>; | 使用Base64编码方案将指定u8数组中的所有字节异步编码到新分配的u8数组中。 |
|
||||
| encodeToStringAsync(src: Uint8Array): Promise\<string\>; | 使用Base64编码方案将指定的字节数组异步编码为String。 |
|
||||
| decodeAsync(src: Uint8Array \| string): Promise\<Uint8Array\>; | 使用Base64编码方案将Base64编码的字符串或输入u8数组异步解码为新分配的u8数组。 |
|
||||
| static createRationalFromString(rationalString: string): RationalNumber | 基于给定的字符串创建一个RationalNumber对象。 |
|
||||
| compareTo(another: RationalNumber): number | 将当前的RationalNumber对象与给定的对象进行比较。 |
|
||||
| equals(obj: object): number | 检查给定对象是否与当前 RationalNumber 对象相同。 |
|
||||
@@ -270,60 +270,46 @@ newPromiseObj.then(res => {
|
||||
import util from '@ohos.util'
|
||||
var that = new util.Base64();
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
var result = that.encode(array,num);
|
||||
var result = that.encode(array);
|
||||
```
|
||||
14.encodeToString()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var that = new util.Base64();
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
var result = that.encodeToString(array,num);
|
||||
var result = that.encodeToString(array);
|
||||
```
|
||||
15.decode()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var that = new util.Base64()
|
||||
var buff = 'czEz';
|
||||
var num = 0;
|
||||
var result = that.decode(buff,num);
|
||||
var result = that.decode(buff);
|
||||
|
||||
```
|
||||
16.encodeAsync()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
it('EncodeAsync_test_001', 0, async function () {
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var rarray = new Uint8Array([99,122,69,122]);
|
||||
var num = 0;
|
||||
that.encodeAsync(array,num).then(val=>{
|
||||
}
|
||||
})
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
that.encodeAsync(array).then(val=>{
|
||||
}
|
||||
```
|
||||
17.encodeToStringAsync()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
it('EncodeToStringAsync_test_001', 0, async function () {
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
that.encodeToStringAsync(array,num).then(val=>{
|
||||
})
|
||||
}),
|
||||
var that = new util.Base64()
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
that.encodeToStringAsync(array).then(val=>{
|
||||
})
|
||||
```
|
||||
18.decodeAsync()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
it('DecodeAsync_test_001', 0, async function () {
|
||||
var that = new util.Base64()
|
||||
var buff = 'czEz';
|
||||
var array = new Uint8Array([115,49,51]);
|
||||
var num = 0;
|
||||
that.decodeAsync(buff,num).then(val=>{
|
||||
})
|
||||
}),
|
||||
var that = new util.Base64()
|
||||
var buff = 'czEz';
|
||||
that.decodeAsync(buff).then(val=>{
|
||||
})
|
||||
```
|
||||
19.createRationalFromString()
|
||||
```
|
||||
|
||||
@@ -193,7 +193,7 @@ HWTEST_F(NativeEngineTest, GetEncoding001, testing::ext::TestSize.Level0)
|
||||
size_t strLength = 0;
|
||||
char* buffer = nullptr;
|
||||
if (bufferSize > 0) {
|
||||
buffer = new char[bufferSize + 1]{ 0 };
|
||||
buffer = new char[bufferSize + 1]();
|
||||
napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength);
|
||||
}
|
||||
const char *result = tmpTestStr.c_str();
|
||||
|
||||
+41
-122
@@ -35,17 +35,11 @@ namespace OHOS::Util {
|
||||
106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
|
||||
121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 61
|
||||
};
|
||||
const char BASE0[] = {
|
||||
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
|
||||
83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
|
||||
121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 61
|
||||
};
|
||||
}
|
||||
Base64::Base64(napi_env env_) : env(env_) {}
|
||||
|
||||
/* base64 encode */
|
||||
napi_value Base64::Encode(napi_value src, napi_value flags)
|
||||
napi_value Base64::Encode(napi_value src)
|
||||
{
|
||||
napi_typedarray_type type;
|
||||
size_t byteOffset = 0;
|
||||
@@ -54,11 +48,7 @@ namespace OHOS::Util {
|
||||
napi_value resultBuffer = nullptr;
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset));
|
||||
inputEncode_ = static_cast<const unsigned char*>(resultData) + byteOffset;
|
||||
int32_t iflag = 0;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, flags, &iflag));
|
||||
size_t flag = 0;
|
||||
flag = static_cast<size_t>(iflag);
|
||||
const unsigned char *rets = EncodeAchieve(inputEncode_, length, flag);
|
||||
const unsigned char *rets = EncodeAchieve(inputEncode_, length);
|
||||
void *data = nullptr;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
size_t bufferSize = outputLen;
|
||||
@@ -75,7 +65,7 @@ namespace OHOS::Util {
|
||||
}
|
||||
|
||||
/* base64 encodeToString */
|
||||
napi_value Base64::EncodeToString(napi_value src, napi_value flags)
|
||||
napi_value Base64::EncodeToString(napi_value src)
|
||||
{
|
||||
napi_typedarray_type type;
|
||||
size_t byteOffset = 0;
|
||||
@@ -84,42 +74,21 @@ namespace OHOS::Util {
|
||||
napi_value resultBuffer = nullptr;
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset));
|
||||
inputEncode_ = static_cast<const unsigned char*>(resultData) + byteOffset;
|
||||
int32_t iflag = 0;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, flags, &iflag));
|
||||
size_t flag = 0;
|
||||
flag = static_cast<size_t>(iflag);
|
||||
unsigned char *ret = EncodeAchieve(inputEncode_, length, flag);
|
||||
char *rstring = nullptr;
|
||||
if (outputLen > 0) {
|
||||
rstring = new char[outputLen + 1];
|
||||
if (memset_s(rstring, outputLen + 1, '\0', outputLen + 1) != 0) {
|
||||
FreeMemory(ret);
|
||||
FreeMemory(rstring);
|
||||
napi_throw_error(env, "-1", "decode rstring memset_s failed");
|
||||
}
|
||||
} else {
|
||||
napi_throw_error(env, "-2", "outputLen is error !");
|
||||
}
|
||||
for (size_t i = 0; i < outputLen; i++) {
|
||||
rstring[i] = char(ret[i]);
|
||||
}
|
||||
std::string finalString = rstring;
|
||||
const char *outString = finalString.c_str();
|
||||
const char *encString = static_cast<const char*>(outString);
|
||||
unsigned char *ret = EncodeAchieve(inputEncode_, length);
|
||||
const char *encString = reinterpret_cast<const char*>(ret);
|
||||
napi_value resultStr = nullptr;
|
||||
NAPI_CALL(env, napi_create_string_utf8(env, encString, strlen(encString), &resultStr));
|
||||
FreeMemory(ret);
|
||||
FreeMemory(rstring);
|
||||
return resultStr;
|
||||
}
|
||||
|
||||
unsigned char *Base64::EncodeAchieve(const unsigned char *input, size_t inputLen, size_t iflag)
|
||||
unsigned char *Base64::EncodeAchieve(const unsigned char *input, size_t inputLen)
|
||||
{
|
||||
size_t inp = 0;
|
||||
size_t temp = 0;
|
||||
size_t bitWise = 0;
|
||||
unsigned char *ret = nullptr;
|
||||
unsigned char *bosom = nullptr;
|
||||
size_t index = 0;
|
||||
outputLen = (inputLen / TRAGET_THREE) * TRAGET_FOUR;
|
||||
if ((inputLen % TRAGET_THREE) > 0) {
|
||||
outputLen += TRAGET_FOUR;
|
||||
@@ -133,7 +102,6 @@ namespace OHOS::Util {
|
||||
} else {
|
||||
napi_throw_error(env, "-2", "outputLen is error !");
|
||||
}
|
||||
bosom = ret;
|
||||
while (inp < inputLen) {
|
||||
temp = 0;
|
||||
bitWise = 0;
|
||||
@@ -147,24 +115,19 @@ namespace OHOS::Util {
|
||||
}
|
||||
bitWise = (bitWise << ((TRAGET_THREE - temp) * TRAGET_EIGHT));
|
||||
for (size_t i = 0; i < TRAGET_FOUR; i++) {
|
||||
if (temp < i && iflag == 0) {
|
||||
*bosom = BASE[BIT_FLG];
|
||||
} else if (temp < i && iflag != 0) {
|
||||
*bosom = BASE0[BIT_FLG];
|
||||
} else if (temp >= i && iflag == 0) {
|
||||
*bosom = BASE[(bitWise >> ((TRAGET_THREE - i) * TRAGET_SIX)) & SIXTEEN_FLG];
|
||||
} else if (temp >= i && iflag != 0) {
|
||||
*bosom = BASE0[(bitWise >> ((TRAGET_THREE - i) * TRAGET_SIX)) & SIXTEEN_FLG];
|
||||
if (temp < i) {
|
||||
ret[index++] = BASE[BIT_FLG];
|
||||
} else if (temp >= i) {
|
||||
ret[index++] = BASE[(bitWise >> ((TRAGET_THREE - i) * TRAGET_SIX)) & SIXTEEN_FLG];
|
||||
}
|
||||
bosom++;
|
||||
}
|
||||
}
|
||||
*bosom = '\0';
|
||||
ret[index] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* base64 decode */
|
||||
napi_value Base64::Decode(napi_value src, napi_value flags)
|
||||
napi_value Base64::Decode(napi_value src)
|
||||
{
|
||||
napi_valuetype valuetype = napi_undefined;
|
||||
napi_typeof(env, src, &valuetype);
|
||||
@@ -177,10 +140,6 @@ namespace OHOS::Util {
|
||||
if (valuetype != napi_valuetype::napi_string) {
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset));
|
||||
}
|
||||
int32_t iflag = 0;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, flags, &iflag));
|
||||
size_t flag = 0;
|
||||
flag = static_cast<size_t>(iflag);
|
||||
if (valuetype == napi_valuetype::napi_string) {
|
||||
size_t prolen = 0;
|
||||
napi_get_value_string_utf8(env, src, nullptr, 0, &prolen);
|
||||
@@ -194,10 +153,10 @@ namespace OHOS::Util {
|
||||
napi_throw_error(env, "-2", "prolen is error !");
|
||||
}
|
||||
napi_get_value_string_utf8(env, src, inputString, prolen + 1, &prolen);
|
||||
pret = DecodeAchieve(inputString, prolen, flag);
|
||||
pret = DecodeAchieve(inputString, prolen);
|
||||
} else if (type == napi_typedarray_type::napi_uint8_array) {
|
||||
inputDecode_ = static_cast<const char*>(resultData) + byteOffset;
|
||||
pret = DecodeAchieve(inputDecode_, length, flag);
|
||||
pret = DecodeAchieve(inputDecode_, length);
|
||||
}
|
||||
void *data = nullptr;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
@@ -216,12 +175,12 @@ namespace OHOS::Util {
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned char *Base64::DecodeAchieve(const char *input, size_t inputLen, size_t iflag)
|
||||
unsigned char *Base64::DecodeAchieve(const char *input, size_t inputLen)
|
||||
{
|
||||
retLen = (inputLen / TRAGET_FOUR) * TRAGET_THREE;
|
||||
decodeOutLen = retLen;
|
||||
size_t equalCount = 0;
|
||||
unsigned char *bosom = nullptr;
|
||||
size_t index = 0;
|
||||
size_t inp = 0;
|
||||
size_t temp = 0;
|
||||
size_t bitWise = 0;
|
||||
@@ -244,7 +203,6 @@ namespace OHOS::Util {
|
||||
} else {
|
||||
napi_throw_error(env, "-2", "retLen is error !");
|
||||
}
|
||||
bosom = retDecode;
|
||||
while (inp < (inputLen - equalCount)) {
|
||||
temp = 0;
|
||||
bitWise = 0;
|
||||
@@ -252,7 +210,7 @@ namespace OHOS::Util {
|
||||
if (inp >= (inputLen - equalCount)) {
|
||||
break;
|
||||
}
|
||||
bitWise = (bitWise << TRAGET_SIX) | (Finds(input[inp], iflag));
|
||||
bitWise = (bitWise << TRAGET_SIX) | (Finds(input[inp]));
|
||||
inp++;
|
||||
temp++;
|
||||
}
|
||||
@@ -261,11 +219,10 @@ namespace OHOS::Util {
|
||||
if (i == temp) {
|
||||
break;
|
||||
}
|
||||
*bosom = static_cast<char>((bitWise >> ((TRAGET_TWO - i) * TRAGET_EIGHT)) & XFF_FLG);
|
||||
bosom++;
|
||||
retDecode[index++] = static_cast<char>((bitWise >> ((TRAGET_TWO - i) * TRAGET_EIGHT)) & XFF_FLG);
|
||||
}
|
||||
}
|
||||
*bosom = '\0';
|
||||
retDecode[index] = 0;
|
||||
return retDecode;
|
||||
}
|
||||
|
||||
@@ -296,22 +253,13 @@ namespace OHOS::Util {
|
||||
}
|
||||
|
||||
/* Decoding lookup function */
|
||||
size_t Base64::Finds(char ch, size_t iflag)
|
||||
size_t Base64::Finds(char ch)
|
||||
{
|
||||
size_t couts = 0;
|
||||
if (iflag == 0) {
|
||||
// 65:Number of elements in the encoding table.
|
||||
for (size_t i = 0; i < 65; i++) {
|
||||
if (BASE[i] == ch) {
|
||||
couts = i;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 65:Number of elements in the encoding table.
|
||||
for (size_t i = 0; i < 65; i++) {
|
||||
if (BASE0[i] == ch) {
|
||||
couts = i;
|
||||
}
|
||||
for (size_t i = 0; i < 65; i++) {
|
||||
if (BASE[i] == ch) {
|
||||
couts = i;
|
||||
}
|
||||
}
|
||||
return couts;
|
||||
@@ -335,7 +283,7 @@ namespace OHOS::Util {
|
||||
}
|
||||
}
|
||||
|
||||
napi_value Base64::EncodeAsync(napi_value src, napi_value flags)
|
||||
napi_value Base64::EncodeAsync(napi_value src)
|
||||
{
|
||||
napi_typedarray_type type;
|
||||
size_t byteOffset = 0;
|
||||
@@ -345,15 +293,11 @@ namespace OHOS::Util {
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset));
|
||||
unsigned char *inputEncode = nullptr;
|
||||
inputEncode = static_cast<unsigned char*>(resultData) + byteOffset;
|
||||
int32_t iflag = 0;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, flags, &iflag));
|
||||
size_t flag = 0;
|
||||
flag = static_cast<size_t>(iflag);
|
||||
CreatePromise(inputEncode, length, flag);
|
||||
CreateEncodePromise(inputEncode, length);
|
||||
return stdEncodeInfo_->promise;
|
||||
}
|
||||
|
||||
napi_value Base64::EncodeToStringAsync(napi_value src, napi_value flags)
|
||||
napi_value Base64::EncodeToStringAsync(napi_value src)
|
||||
{
|
||||
napi_typedarray_type type;
|
||||
size_t byteOffset = 0;
|
||||
@@ -363,21 +307,16 @@ namespace OHOS::Util {
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset));
|
||||
unsigned char *inputEncode = nullptr;
|
||||
inputEncode = static_cast<unsigned char*>(resultData) + byteOffset;
|
||||
int32_t iflag = 0;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, flags, &iflag));
|
||||
size_t flag = 0;
|
||||
flag = static_cast<size_t>(iflag);
|
||||
CreatePromise01(inputEncode, length, flag);
|
||||
CreateEncodeToStringPromise(inputEncode, length);
|
||||
return stdEncodeInfo_->promise;
|
||||
}
|
||||
|
||||
void Base64::CreatePromise(unsigned char *inputDecode, size_t length, size_t flag)
|
||||
void Base64::CreateEncodePromise(unsigned char *inputDecode, size_t length)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
stdEncodeInfo_ = new EncodeInfo();
|
||||
stdEncodeInfo_->sinputEncode = inputDecode;
|
||||
stdEncodeInfo_->slength = length;
|
||||
stdEncodeInfo_->sflag = flag;
|
||||
stdEncodeInfo_->env = env;
|
||||
napi_create_promise(env, &stdEncodeInfo_->deferred, &stdEncodeInfo_->promise);
|
||||
napi_create_string_utf8(env, "ReadStdEncode", NAPI_AUTO_LENGTH, &resourceName);
|
||||
@@ -386,13 +325,12 @@ namespace OHOS::Util {
|
||||
napi_queue_async_work(env, stdEncodeInfo_->worker);
|
||||
}
|
||||
|
||||
void Base64::CreatePromise01(unsigned char *inputDecode, size_t length, size_t flag)
|
||||
void Base64::CreateEncodeToStringPromise(unsigned char *inputDecode, size_t length)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
stdEncodeInfo_ = new EncodeInfo();
|
||||
stdEncodeInfo_->sinputEncode = inputDecode;
|
||||
stdEncodeInfo_->slength = length;
|
||||
stdEncodeInfo_->sflag = flag;
|
||||
napi_create_promise(env, &stdEncodeInfo_->deferred, &stdEncodeInfo_->promise);
|
||||
napi_create_string_utf8(env, "ReadStdEncodeToString", NAPI_AUTO_LENGTH, &resourceName);
|
||||
napi_create_async_work(env, nullptr, resourceName, ReadStdEncodeToString, EndStdEncodeToString,
|
||||
@@ -404,7 +342,6 @@ namespace OHOS::Util {
|
||||
{
|
||||
const unsigned char *input = encodeInfo->sinputEncode;
|
||||
size_t inputLen = encodeInfo->slength;
|
||||
size_t iflag = encodeInfo->sflag;
|
||||
size_t inp = 0;
|
||||
size_t temp = 0;
|
||||
size_t bitWise = 0;
|
||||
@@ -437,14 +374,10 @@ namespace OHOS::Util {
|
||||
}
|
||||
bitWise = (bitWise << ((TRAGET_THREE - temp) * TRAGET_EIGHT));
|
||||
for (size_t i = 0; i < TRAGET_FOUR; i++) {
|
||||
if (temp < i && iflag == 0) {
|
||||
if (temp < i) {
|
||||
ret[index++] = BASE[BIT_FLG];
|
||||
} else if (temp < i && iflag != 0) {
|
||||
ret[index++] = BASE0[BIT_FLG];
|
||||
} else if (temp >= i && iflag == 0) {
|
||||
} else if (temp >= i) {
|
||||
ret[index++] = BASE[(bitWise >> ((TRAGET_THREE - i) * TRAGET_SIX)) & SIXTEEN_FLG];
|
||||
} else if (temp >= i && iflag != 0) {
|
||||
ret[index++] = BASE0[(bitWise >> ((TRAGET_THREE - i) * TRAGET_SIX)) & SIXTEEN_FLG];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -498,7 +431,7 @@ namespace OHOS::Util {
|
||||
delete stdEncodeInfo;
|
||||
}
|
||||
|
||||
napi_value Base64::DecodeAsync(napi_value src, napi_value flags)
|
||||
napi_value Base64::DecodeAsync(napi_value src)
|
||||
{
|
||||
napi_valuetype valuetype = napi_undefined;
|
||||
napi_typeof(env, src, &valuetype);
|
||||
@@ -512,10 +445,6 @@ namespace OHOS::Util {
|
||||
if (valuetype != napi_valuetype::napi_string) {
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset));
|
||||
}
|
||||
int32_t iflag = 0;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, flags, &iflag));
|
||||
size_t flag = 0;
|
||||
flag = static_cast<size_t>(iflag);
|
||||
if (valuetype == napi_valuetype::napi_string) {
|
||||
size_t prolen = 0;
|
||||
napi_get_value_string_utf8(env, src, nullptr, 0, &prolen);
|
||||
@@ -528,22 +457,21 @@ namespace OHOS::Util {
|
||||
napi_throw_error(env, "-2", "prolen is error !");
|
||||
}
|
||||
napi_get_value_string_utf8(env, src, inputString, prolen+1, &prolen);
|
||||
CreatePromise02(inputString, prolen, flag);
|
||||
CreateDecodePromise(inputString, prolen);
|
||||
} else if (type == napi_typedarray_type::napi_uint8_array) {
|
||||
inputDecode = static_cast<char*>(resultData) + byteOffset;
|
||||
CreatePromise02(inputDecode, length, flag);
|
||||
CreateDecodePromise(inputDecode, length);
|
||||
}
|
||||
return stdDecodeInfo_->promise;
|
||||
delete[] inputString;
|
||||
}
|
||||
|
||||
void Base64::CreatePromise02(char *inputDecode, size_t length, size_t flag)
|
||||
void Base64::CreateDecodePromise(char *inputDecode, size_t length)
|
||||
{
|
||||
napi_value resourceName = nullptr;
|
||||
stdDecodeInfo_ = new DecodeInfo();
|
||||
stdDecodeInfo_->sinputDecode = inputDecode;
|
||||
stdDecodeInfo_->slength = length;
|
||||
stdDecodeInfo_->sflag = flag;
|
||||
stdDecodeInfo_->env = env;
|
||||
napi_create_promise(env, &stdDecodeInfo_->deferred, &stdDecodeInfo_->promise);
|
||||
napi_create_string_utf8(env, "ReadStdDecode", NAPI_AUTO_LENGTH, &resourceName);
|
||||
@@ -553,20 +481,12 @@ namespace OHOS::Util {
|
||||
}
|
||||
|
||||
/* Decoding lookup function */
|
||||
size_t Finds(char ch, size_t iflag)
|
||||
size_t Finds(char ch)
|
||||
{
|
||||
size_t couts = 0;
|
||||
if (iflag == 0) {
|
||||
for (size_t i = 0; i < TRAGET_SIXTYFIVE; i++) {
|
||||
if (BASE[i] == ch) {
|
||||
couts = i;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < TRAGET_SIXTYFIVE; i++) {
|
||||
if (BASE0[i] == ch) {
|
||||
couts = i;
|
||||
}
|
||||
for (size_t i = 0; i < TRAGET_SIXTYFIVE; i++) {
|
||||
if (BASE[i] == ch) {
|
||||
couts = i;
|
||||
}
|
||||
}
|
||||
return couts;
|
||||
@@ -601,7 +521,6 @@ namespace OHOS::Util {
|
||||
{
|
||||
const char *input = decodeInfo->sinputDecode;
|
||||
size_t inputLen = decodeInfo->slength;
|
||||
size_t iflag = decodeInfo->sflag;
|
||||
size_t retLen = 0;
|
||||
retLen = (inputLen / TRAGET_FOUR) * TRAGET_THREE;
|
||||
decodeInfo->decodeOutLen = retLen;
|
||||
@@ -633,7 +552,7 @@ namespace OHOS::Util {
|
||||
if (inp >= (inputLen - equalCount)) {
|
||||
break;
|
||||
}
|
||||
bitWise = (bitWise << TRAGET_SIX) | (Finds(input[inp], iflag));
|
||||
bitWise = (bitWise << TRAGET_SIX) | (Finds(input[inp]));
|
||||
inp++;
|
||||
temp++;
|
||||
}
|
||||
|
||||
+12
-14
@@ -30,7 +30,6 @@ namespace OHOS::Util {
|
||||
unsigned char *sinputEncode = nullptr;
|
||||
unsigned char *sinputEncoding = nullptr;
|
||||
size_t slength = 0;
|
||||
size_t sflag = 0;
|
||||
size_t soutputLen = 0;
|
||||
napi_env env;
|
||||
};
|
||||
@@ -42,7 +41,6 @@ namespace OHOS::Util {
|
||||
char *sinputDecode = nullptr;
|
||||
unsigned char *sinputDecoding = nullptr;
|
||||
size_t slength = 0;
|
||||
size_t sflag = 0;
|
||||
size_t decodeOutLen = 0;
|
||||
size_t retLen = 0;
|
||||
napi_env env;
|
||||
@@ -61,18 +59,18 @@ namespace OHOS::Util {
|
||||
public:
|
||||
explicit Base64(napi_env env);
|
||||
virtual ~Base64(){}
|
||||
napi_value Encode(napi_value src, napi_value flags);
|
||||
napi_value EncodeToString(napi_value src, napi_value flags);
|
||||
napi_value Decode(napi_value src, napi_value flags);
|
||||
napi_value Encode(napi_value src);
|
||||
napi_value EncodeToString(napi_value src);
|
||||
napi_value Decode(napi_value src);
|
||||
|
||||
napi_value EncodeAsync(napi_value src, napi_value flags);
|
||||
napi_value EncodeToStringAsync(napi_value src, napi_value flags);
|
||||
napi_value DecodeAsync(napi_value src, napi_value flags);
|
||||
napi_value EncodeAsync(napi_value src);
|
||||
napi_value EncodeToStringAsync(napi_value src);
|
||||
napi_value DecodeAsync(napi_value src);
|
||||
private:
|
||||
napi_env env;
|
||||
unsigned char *DecodeAchieve(const char *input, size_t inputLen, size_t iflag);
|
||||
unsigned char *EncodeAchieve(const unsigned char *input, size_t inputLen, size_t iflag);
|
||||
size_t Finds(char ch, size_t iflag);
|
||||
unsigned char *DecodeAchieve(const char *input, size_t inputLen);
|
||||
unsigned char *EncodeAchieve(const unsigned char *input, size_t inputLen);
|
||||
size_t Finds(char ch);
|
||||
size_t DecodeOut(size_t equalCount, size_t retLen);
|
||||
void FreeMemory(const unsigned char *address);
|
||||
void FreeMemory(const char *address);
|
||||
@@ -84,9 +82,9 @@ namespace OHOS::Util {
|
||||
const char *inputDecode_ = nullptr;
|
||||
unsigned char *retDecode = nullptr;
|
||||
|
||||
void CreatePromise(unsigned char *inputDecode, size_t length, size_t flag);
|
||||
void CreatePromise01(unsigned char *inputDecode, size_t length, size_t flag);
|
||||
void CreatePromise02(char *inputDecode, size_t length, size_t flag);
|
||||
void CreateEncodePromise(unsigned char *inputDecode, size_t length);
|
||||
void CreateEncodeToStringPromise(unsigned char *inputDecode, size_t length);
|
||||
void CreateDecodePromise(char *inputDecode, size_t length);
|
||||
EncodeInfo *stdEncodeInfo_ = nullptr;
|
||||
DecodeInfo *stdDecodeInfo_ = nullptr;
|
||||
static void ReadStdEncode(napi_env env, void *data);
|
||||
|
||||
+24
-42
@@ -696,9 +696,9 @@ namespace OHOS::Util {
|
||||
static napi_value EncodeBase64(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
size_t requireArgc = 1;
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
|
||||
napi_typedarray_type valuetype0;
|
||||
@@ -707,22 +707,19 @@ namespace OHOS::Util {
|
||||
napi_value arraybuffer = nullptr;
|
||||
size_t byteOffset = 0;
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, args[0], &valuetype0, &length, &data, &arraybuffer, &byteOffset));
|
||||
napi_valuetype valuetype1;
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
|
||||
NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected.");
|
||||
NAPI_ASSERT(env, valuetype1 == napi_number, "Wrong argument type. Nmuber expected.");
|
||||
Base64 *object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
||||
napi_value result = object->Encode(args[0], args[1]);
|
||||
napi_value result = object->Encode(args[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
static napi_value EncodeToString(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
size_t requireArgc = 1;
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
|
||||
napi_typedarray_type valuetype0;
|
||||
@@ -731,22 +728,19 @@ namespace OHOS::Util {
|
||||
napi_value arraybuffer = nullptr;
|
||||
size_t byteOffset = 0;
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, args[0], &valuetype0, &length, &data, &arraybuffer, &byteOffset));
|
||||
napi_valuetype valuetype1;
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
|
||||
NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected.");
|
||||
NAPI_ASSERT(env, valuetype1 == napi_number, "Wrong argument type. Nmuber expected.");
|
||||
Base64 *object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
||||
napi_value result = object->EncodeToString(args[0], args[1]);
|
||||
napi_value result = object->EncodeToString(args[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
static napi_value DecodeBase64(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
size_t requireArgc = 1;
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
|
||||
napi_typedarray_type valuetype0;
|
||||
@@ -763,21 +757,18 @@ namespace OHOS::Util {
|
||||
if ((valuetype1 != napi_valuetype::napi_string) && (valuetype0 != napi_typedarray_type::napi_uint8_array)) {
|
||||
napi_throw_error(env, nullptr, "The parameter type is incorrect");
|
||||
}
|
||||
napi_valuetype valuetype2;
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype2));
|
||||
NAPI_ASSERT(env, valuetype2 == napi_number, "Wrong argument type. Nmuber expected.");
|
||||
Base64 *object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
||||
napi_value result = object->Decode(args[0], args[1]);
|
||||
napi_value result = object->Decode(args[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
static napi_value EncodeAsync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
size_t requireArgc = 1;
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
|
||||
napi_typedarray_type valuetype0;
|
||||
@@ -786,22 +777,19 @@ namespace OHOS::Util {
|
||||
napi_value arraybuffer = nullptr;
|
||||
size_t byteOffset = 0;
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, args[0], &valuetype0, &length, &data, &arraybuffer, &byteOffset));
|
||||
napi_valuetype valuetype1;
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
|
||||
NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected.");
|
||||
NAPI_ASSERT(env, valuetype1 == napi_number, "Wrong argument type. Nmuber expected.");
|
||||
Base64 *object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
||||
napi_value result = object->EncodeAsync(args[0], args[1]);
|
||||
napi_value result = object->EncodeAsync(args[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
static napi_value EncodeToStringAsync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
size_t requireArgc = 1;
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
|
||||
napi_typedarray_type valuetype0;
|
||||
@@ -810,22 +798,19 @@ namespace OHOS::Util {
|
||||
napi_value arraybuffer = nullptr;
|
||||
size_t byteOffset = 0;
|
||||
NAPI_CALL(env, napi_get_typedarray_info(env, args[0], &valuetype0, &length, &data, &arraybuffer, &byteOffset));
|
||||
napi_valuetype valuetype1;
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
|
||||
NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected.");
|
||||
NAPI_ASSERT(env, valuetype1 == napi_number, "Wrong argument type. Nmuber expected.");
|
||||
Base64 *object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
||||
napi_value result = object->EncodeToStringAsync(args[0], args[1]);
|
||||
napi_value result = object->EncodeToStringAsync(args[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
static napi_value DecodeAsync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value thisVar = nullptr;
|
||||
size_t requireArgc = 2;
|
||||
size_t argc = 2;
|
||||
napi_value args[2] = { nullptr };
|
||||
size_t requireArgc = 1;
|
||||
size_t argc = 1;
|
||||
napi_value args[1] = { nullptr };
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
|
||||
NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
|
||||
napi_typedarray_type valuetype0;
|
||||
@@ -842,12 +827,9 @@ namespace OHOS::Util {
|
||||
if ((valuetype1 != napi_valuetype::napi_string) && (valuetype0 != napi_typedarray_type::napi_uint8_array)) {
|
||||
napi_throw_error(env, nullptr, "The parameter type is incorrect");
|
||||
}
|
||||
napi_valuetype valuetype2;
|
||||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype2));
|
||||
NAPI_ASSERT(env, valuetype2 == napi_number, "Wrong argument type. Nmuber expected.");
|
||||
Base64 *object = nullptr;
|
||||
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
||||
napi_value result = object->DecodeAsync(args[0], args[1]);
|
||||
napi_value result = object->DecodeAsync(args[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user