88#include " ../cppcodec/cppcodec/base32_crockford.hpp"
99#include " ../cppcodec/cppcodec/base32_hex.hpp"
1010
11+ #include " ../cppcodec/cppcodec/hex_upper.hpp"
12+ #include " ../cppcodec/cppcodec/hex_lower.hpp"
13+
1114#include < jsi/jsi.h>
1215#include < string>
1316#include < iostream>
@@ -34,6 +37,9 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
3437 using base32Rfc4648 = cppcodec::base32_rfc4648;
3538 using base32Crockford = cppcodec::base32_crockford;
3639 using base32Hex = cppcodec::base32_hex;
40+
41+ using base16Upper = cppcodec::hex_upper;
42+ using base16Lower = cppcodec::hex_lower;
3743
3844 if (propName == " encode" ) {
3945 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) {
5763 if (algorithm == " base64Rfc4648" ) {
5864 string stringEncoded = base64Rfc4648::encode (stringToEncode);
5965
60- result = stringEncoded. c_str () ;
66+ result = stringEncoded;
6167 }
6268
6369 if (algorithm == " base64Url" ) {
6470 string stringEncoded = base64Url::encode (stringToEncode);
6571
66- result = stringEncoded. c_str () ;
72+ result = stringEncoded;
6773 }
6874
6975 if (algorithm == " base64UrlUnpadded" ) {
7076 string stringEncoded = base64UrlUnpadded::encode (stringToEncode);
7177
72- result = stringEncoded. c_str () ;
78+ result = stringEncoded;
7379 }
7480
7581 if (algorithm == " base32Rfc4648" ) {
7682 string stringEncoded = base32Rfc4648::encode (stringToEncode);
7783
78- result = stringEncoded. c_str () ;
84+ result = stringEncoded;
7985 }
8086
8187 if (algorithm == " base32Crockford" ) {
8288 string stringEncoded = base32Crockford::encode (stringToEncode);
8389
84- result = stringEncoded. c_str () ;
90+ result = stringEncoded;
8591 }
8692
8793 if (algorithm == " base32Hex" ) {
8894 string stringEncoded = base32Hex::encode (stringToEncode);
8995
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;
91109 }
92110
93111 return String::createFromUtf8 (runtime, result);
@@ -113,51 +131,57 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
113131 throw JSError (runtime, " [react-native-jsi-base-coder] The `bytesToDecode` cannot be an empty string." );
114132 }
115133
116- vector< unsigned char > result;
134+ string result;
117135
118136 if (algorithm == " base64Rfc4648" ) {
119- vector<unsigned char > bytesDecoded = base64Rfc4648::decode (bytesToDecode);
137+ vector<uint8_t > bytesDecoded = base64Rfc4648::decode (bytesToDecode);
120138
121- result = bytesDecoded;
139+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
122140 }
123141
124142 if (algorithm == " base64Url" ) {
125- vector<unsigned char > bytesDecoded = base64Url::decode (bytesToDecode);
143+ vector<uint8_t > bytesDecoded = base64Url::decode (bytesToDecode);
126144
127- result = bytesDecoded;
145+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
128146 }
129147
130148 if (algorithm == " base64UrlUnpadded" ) {
131- vector<unsigned char > bytesDecoded = base64UrlUnpadded::decode (bytesToDecode);
149+ vector<uint8_t > bytesDecoded = base64UrlUnpadded::decode (bytesToDecode);
132150
133- result = bytesDecoded;
151+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
134152 }
135153
136154 if (algorithm == " base32Rfc4648" ) {
137- vector<unsigned char > bytesDecoded = base32Rfc4648::decode (bytesToDecode);
155+ vector<uint8_t > bytesDecoded = base32Rfc4648::decode (bytesToDecode);
138156
139- result = bytesDecoded;
157+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
140158 }
141159
142160 if (algorithm == " base32Crockford" ) {
143- vector<unsigned char > bytesDecoded = base32Crockford::decode (bytesToDecode);
161+ vector<uint8_t > bytesDecoded = base32Crockford::decode (bytesToDecode);
144162
145- result = bytesDecoded;
163+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
146164 }
147165
148166 if (algorithm == " base32Hex" ) {
149- vector<unsigned char > bytesDecoded = base32Hex::decode (bytesToDecode);
167+ vector<uint8_t > bytesDecoded = base32Hex::decode (bytesToDecode);
150168
151- result = bytesDecoded;
169+ result. assign (bytesDecoded. begin (), bytesDecoded. end ()) ;
152170 }
171+
172+ if (algorithm == " base16Upper" ) {
173+ vector<uint8_t > bytesDecoded = base16Upper::decode (bytesToDecode);
153174
154- string s;
175+ result.assign (bytesDecoded.begin (), bytesDecoded.end ());
176+ }
177+
178+ if (algorithm == " base16Lower" ) {
179+ vector<uint8_t > bytesDecoded = base16Lower::decode (bytesToDecode);
155180
156- transform (result.begin (), result.end (), back_inserter (s), [](char c) {
157- return c;
158- });
181+ result.assign (bytesDecoded.begin (), bytesDecoded.end ());
182+ }
159183
160- return String::createFromUtf8 (runtime, s );
184+ return String::createFromUtf8 (runtime, result );
161185 };
162186
163187 return Function::createFromHostFunction (runtime, PropNameID::forUtf8 (runtime, " decode" ), 0 , decode);
0 commit comments