Skip to content

Commit 536b23a

Browse files
committed
feat: add base64 encoding and decoding functions to HybridInspireFace
1 parent 3f92a7e commit 536b23a

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

cpp/HybridInspireFace.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,4 +792,76 @@ namespace margelo::nitro::nitroinspireface
792792
return isSupported != 0;
793793
}
794794

795+
std::shared_ptr<ArrayBuffer> HybridInspireFace::fromBase64(const std::string &base64)
796+
{
797+
std::vector<unsigned char> decoded = base64_decode(base64);
798+
if (decoded.empty())
799+
{
800+
throw std::runtime_error("Failed to decode base64 string");
801+
}
802+
return ArrayBuffer::copy(decoded.data(), decoded.size());
803+
}
804+
805+
std::string HybridInspireFace::toBase64(const std::shared_ptr<ArrayBuffer> &buffer)
806+
{
807+
if (!buffer || buffer->size() == 0)
808+
{
809+
throw std::runtime_error("Invalid buffer");
810+
}
811+
return base64_encode(reinterpret_cast<const unsigned char *>(buffer->data()), buffer->size());
812+
}
813+
814+
// Add these helper functions to FSDKApi class
815+
std::string HybridInspireFace::base64_encode(const unsigned char *data, size_t len)
816+
{
817+
static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
818+
819+
std::string ret;
820+
ret.reserve(((len + 2) / 3) * 4);
821+
822+
for (size_t i = 0; i < len; i += 3)
823+
{
824+
unsigned char b3[3] = {0};
825+
for (size_t j = 0; j < 3; j++)
826+
{
827+
if (i + j < len)
828+
b3[j] = data[i + j];
829+
}
830+
831+
ret.push_back(base64_chars[(b3[0] & 0xfc) >> 2]);
832+
ret.push_back(base64_chars[((b3[0] & 0x03) << 4) + ((b3[1] & 0xf0) >> 4)]);
833+
ret.push_back(i + 1 < len ? base64_chars[((b3[1] & 0x0f) << 2) + ((b3[2] & 0xc0) >> 6)] : '=');
834+
ret.push_back(i + 2 < len ? base64_chars[b3[2] & 0x3f] : '=');
835+
}
836+
837+
return ret;
838+
}
839+
840+
std::vector<unsigned char> HybridInspireFace::base64_decode(const std::string &encoded)
841+
{
842+
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
843+
844+
std::vector<unsigned char> ret;
845+
int val = 0, valb = -8;
846+
847+
for (unsigned char c : encoded)
848+
{
849+
if (c == '=')
850+
break;
851+
if (base64_chars.find(c) == std::string::npos)
852+
continue;
853+
854+
val = (val << 6) + base64_chars.find(c);
855+
valb += 6;
856+
857+
if (valb >= 0)
858+
{
859+
ret.push_back(char((val >> valb) & 0xFF));
860+
valb -= 8;
861+
}
862+
}
863+
864+
return ret;
865+
}
866+
795867
} // namespace margelo::nitro::nitroinspireface

cpp/HybridInspireFace.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace margelo::nitro::nitroinspireface
3737

3838
private:
3939
std::shared_ptr<HybridAssetManagerSpec> assetManager;
40+
std::string base64_encode(const unsigned char *data, size_t len);
41+
std::vector<unsigned char> base64_decode(const std::string &encoded);
4042

4143
public:
4244
std::string getVersion() override;
@@ -81,6 +83,8 @@ namespace margelo::nitro::nitroinspireface
8183
void printCudaDeviceInfo() override;
8284
double getNumCudaDevices() override;
8385
bool checkCudaDeviceSupport() override;
86+
std::shared_ptr<ArrayBuffer> fromBase64(const std::string &base64) override;
87+
std::string toBase64(const std::shared_ptr<ArrayBuffer> &buffer) override;
8488
};
8589

8690
} // namespace margelo::nitro::nitroinspireface

src/InspireFace.nitro.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,16 @@ export interface InspireFace
256256
* Check if CUDA device support is available.
257257
*/
258258
checkCudaDeviceSupport(): boolean;
259+
260+
/**
261+
* Convert a base64 string to an ArrayBuffer.
262+
* @param base64 Base64 string to convert
263+
*/
264+
fromBase64(base64: string): ArrayBuffer;
265+
266+
/**
267+
* Convert an ArrayBuffer to a base64 string.
268+
* @param buffer ArrayBuffer to convert
269+
*/
270+
toBase64(buffer: ArrayBuffer): string;
259271
}

0 commit comments

Comments
 (0)