mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-08-25 15:31:16 +00:00
chore(cbindings): Adding cpp example that integrates the 'libwaku' (#2079)
* Adding cpp example that integrates the `libwaku` --------- Co-authored-by: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
co-authored by
NagyZoltanPeter
parent
aa55fa42d9
commit
fe8a9ec59d
@@ -0,0 +1,53 @@
|
||||
|
||||
#include <vector>
|
||||
#include "base64.h"
|
||||
|
||||
// Base64 encoding
|
||||
// source: https://nachtimwald.com/2017/11/18/base64-encode-and-decode-in-c/
|
||||
size_t b64_encoded_size(size_t inlen)
|
||||
{
|
||||
size_t ret;
|
||||
|
||||
ret = inlen;
|
||||
if (inlen % 3 != 0)
|
||||
ret += 3 - (inlen % 3);
|
||||
ret /= 3;
|
||||
ret *= 4;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
void b64_encode(char* in, size_t len, std::vector<char>& out)
|
||||
{
|
||||
size_t elen;
|
||||
size_t i;
|
||||
size_t j;
|
||||
size_t v;
|
||||
|
||||
if (in == NULL || len == 0)
|
||||
return;
|
||||
|
||||
elen = b64_encoded_size(len);
|
||||
out.reserve(elen+1);
|
||||
|
||||
for (i=0, j=0; i<len; i+=3, j+=4) {
|
||||
v = in[i];
|
||||
v = i+1 < len ? v << 8 | in[i+1] : v << 8;
|
||||
v = i+2 < len ? v << 8 | in[i+2] : v << 8;
|
||||
|
||||
out[j] = b64chars[(v >> 18) & 0x3F];
|
||||
out[j+1] = b64chars[(v >> 12) & 0x3F];
|
||||
if (i+1 < len) {
|
||||
out[j+2] = b64chars[(v >> 6) & 0x3F];
|
||||
} else {
|
||||
out[j+2] = '=';
|
||||
}
|
||||
if (i+2 < len) {
|
||||
out[j+3] = b64chars[v & 0x3F];
|
||||
} else {
|
||||
out[j+3] = '=';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user