aboutsummaryrefslogtreecommitdiffstats
path: root/include/utility.h
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2016-08-28 14:46:31 +0900
committeripknHama <ipknhama@gmail.com>2016-08-28 14:46:31 +0900
commit967adf0de55afcb52881cdb1a7b16788c7c283db (patch)
treedbe4fe620a136bdb462a4ad29e83d6d699b3b447 /include/utility.h
parent45f6d12fd382662675000fb1c60909287733127c (diff)
downloadcrow-967adf0de55afcb52881cdb1a7b16788c7c283db.tar.gz
crow-967adf0de55afcb52881cdb1a7b16788c7c283db.zip
Add websocket feature
Diffstat (limited to 'include/utility.h')
-rw-r--r--include/utility.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/include/utility.h b/include/utility.h
index 183d65b..fe9029e 100644
--- a/include/utility.h
+++ b/include/utility.h
@@ -499,5 +499,47 @@ template <typename F, typename Set>
using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
};
+ std::string base64encode(const char* data, size_t size, const char* key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
+ {
+ std::string ret;
+ ret.resize((size+2) / 3 * 4);
+ auto it = ret.begin();
+ while(size >= 3)
+ {
+ *it++ = key[(((unsigned char)*data)&0xFC)>>2];
+ unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
+ *it++ = key[h|((((unsigned char)*data)&0xF0)>>4)];
+ h = (((unsigned char)*data++) & 0x0F) << 2;
+ *it++ = key[h|((((unsigned char)*data)&0xC0)>>6)];
+ *it++ = key[((unsigned char)*data++)&0x3F];
+
+ size -= 3;
+ }
+ if (size == 1)
+ {
+ *it++ = key[(((unsigned char)*data)&0xFC)>>2];
+ unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
+ *it++ = key[h];
+ *it++ = '=';
+ *it++ = '=';
+ }
+ else if (size == 2)
+ {
+ *it++ = key[(((unsigned char)*data)&0xFC)>>2];
+ unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
+ *it++ = key[h|((((unsigned char)*data)&0xF0)>>4)];
+ h = (((unsigned char)*data++) & 0x0F) << 2;
+ *it++ = key[h];
+ *it++ = '=';
+ }
+ return ret;
+ }
+
+ std::string base64encode_urlsafe(const char* data, size_t size)
+ {
+ return base64encode(data, size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
+ }
+
+
} // namespace utility
}