8
8
#include " ../cppcodec/cppcodec/base32_crockford.hpp"
9
9
#include " ../cppcodec/cppcodec/base32_hex.hpp"
10
10
11
+ #include " ../cppcodec/cppcodec/hex_upper.hpp"
12
+ #include " ../cppcodec/cppcodec/hex_lower.hpp"
13
+
11
14
#include < jsi/jsi.h>
12
15
#include < string>
13
16
#include < iostream>
@@ -34,6 +37,9 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
34
37
using base32Rfc4648 = cppcodec::base32_rfc4648;
35
38
using base32Crockford = cppcodec::base32_crockford;
36
39
using base32Hex = cppcodec::base32_hex;
40
+
41
+ using base16Upper = cppcodec::hex_upper;
42
+ using base16Lower = cppcodec::hex_lower;
37
43
38
44
if (propName == " encode" ) {
39
45
auto encode = [this ] (Runtime& runtime, const Value& thisValue, const Value* arguments, size_t count) -> Value {
@@ -57,37 +63,49 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
57
63
if (algorithm == " base64Rfc4648" ) {
58
64
string stringEncoded = base64Rfc4648::encode (stringToEncode);
59
65
60
- result = stringEncoded. c_str () ;
66
+ result = stringEncoded;
61
67
}
62
68
63
69
if (algorithm == " base64Url" ) {
64
70
string stringEncoded = base64Url::encode (stringToEncode);
65
71
66
- result = stringEncoded. c_str () ;
72
+ result = stringEncoded;
67
73
}
68
74
69
75
if (algorithm == " base64UrlUnpadded" ) {
70
76
string stringEncoded = base64UrlUnpadded::encode (stringToEncode);
71
77
72
- result = stringEncoded. c_str () ;
78
+ result = stringEncoded;
73
79
}
74
80
75
81
if (algorithm == " base32Rfc4648" ) {
76
82
string stringEncoded = base32Rfc4648::encode (stringToEncode);
77
83
78
- result = stringEncoded. c_str () ;
84
+ result = stringEncoded;
79
85
}
80
86
81
87
if (algorithm == " base32Crockford" ) {
82
88
string stringEncoded = base32Crockford::encode (stringToEncode);
83
89
84
- result = stringEncoded. c_str () ;
90
+ result = stringEncoded;
85
91
}
86
92
87
93
if (algorithm == " base32Hex" ) {
88
94
string stringEncoded = base32Hex::encode (stringToEncode);
89
95
90
- result = stringEncoded.c_str ();
96
+ result = stringEncoded;
97
+ }
98
+
99
+ if (algorithm == " base16Upper" ) {
100
+ string stringEncoded = base16Upper::encode (stringToEncode);
101
+
102
+ result = stringEncoded;
103
+ }
104
+
105
+ if (algorithm == " base16Lower" ) {
106
+ string stringEncoded = base16Lower::encode (stringToEncode);
107
+
108
+ result = stringEncoded;
91
109
}
92
110
93
111
return String::createFromUtf8 (runtime, result);
@@ -113,51 +131,57 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
113
131
throw JSError (runtime, " [react-native-jsi-base-coder] The `bytesToDecode` cannot be an empty string." );
114
132
}
115
133
116
- vector< unsigned char > result;
134
+ string result;
117
135
118
136
if (algorithm == " base64Rfc4648" ) {
119
- vector<unsigned char > bytesDecoded = base64Rfc4648::decode (bytesToDecode);
137
+ vector<uint8_t > bytesDecoded = base64Rfc4648::decode (bytesToDecode);
120
138
121
- result = bytesDecoded;
139
+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
122
140
}
123
141
124
142
if (algorithm == " base64Url" ) {
125
- vector<unsigned char > bytesDecoded = base64Url::decode (bytesToDecode);
143
+ vector<uint8_t > bytesDecoded = base64Url::decode (bytesToDecode);
126
144
127
- result = bytesDecoded;
145
+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
128
146
}
129
147
130
148
if (algorithm == " base64UrlUnpadded" ) {
131
- vector<unsigned char > bytesDecoded = base64UrlUnpadded::decode (bytesToDecode);
149
+ vector<uint8_t > bytesDecoded = base64UrlUnpadded::decode (bytesToDecode);
132
150
133
- result = bytesDecoded;
151
+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
134
152
}
135
153
136
154
if (algorithm == " base32Rfc4648" ) {
137
- vector<unsigned char > bytesDecoded = base32Rfc4648::decode (bytesToDecode);
155
+ vector<uint8_t > bytesDecoded = base32Rfc4648::decode (bytesToDecode);
138
156
139
- result = bytesDecoded;
157
+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
140
158
}
141
159
142
160
if (algorithm == " base32Crockford" ) {
143
- vector<unsigned char > bytesDecoded = base32Crockford::decode (bytesToDecode);
161
+ vector<uint8_t > bytesDecoded = base32Crockford::decode (bytesToDecode);
144
162
145
- result = bytesDecoded;
163
+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
146
164
}
147
165
148
166
if (algorithm == " base32Hex" ) {
149
- vector<unsigned char > bytesDecoded = base32Hex::decode (bytesToDecode);
167
+ vector<uint8_t > bytesDecoded = base32Hex::decode (bytesToDecode);
150
168
151
- result = bytesDecoded;
169
+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
152
170
}
171
+
172
+ if (algorithm == " base16Upper" ) {
173
+ vector<uint8_t > bytesDecoded = base16Upper::decode (bytesToDecode);
153
174
154
- string s;
175
+ result.assign (bytesDecoded.begin (), bytesDecoded.end ());
176
+ }
177
+
178
+ if (algorithm == " base16Lower" ) {
179
+ vector<uint8_t > bytesDecoded = base16Lower::decode (bytesToDecode);
155
180
156
- transform (result.begin (), result.end (), back_inserter (s), [](char c) {
157
- return c;
158
- });
181
+ result.assign (bytesDecoded.begin (), bytesDecoded.end ());
182
+ }
159
183
160
- return String::createFromUtf8 (runtime, s );
184
+ return String::createFromUtf8 (runtime, result );
161
185
};
162
186
163
187
return Function::createFromHostFunction (runtime, PropNameID::forUtf8 (runtime, " decode" ), 0 , decode);
0 commit comments