diff options
author | Kirill Volinsky <mataes2007@gmail.com> | 2012-07-20 16:21:49 +0000 |
---|---|---|
committer | Kirill Volinsky <mataes2007@gmail.com> | 2012-07-20 16:21:49 +0000 |
commit | f424a18112032cf61d2871a6b91a5af607c171ae (patch) | |
tree | 88fedc4e28941ceecda7026f0b06eba6271f91d5 /plugins/CryptoPP/src/base64.cpp | |
parent | bfe1bd0fc087be44c70904aee0fe4276643d206d (diff) |
CryptoPP:
changed folder structure
git-svn-id: http://svn.miranda-ng.org/main/trunk@1083 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/CryptoPP/src/base64.cpp')
-rw-r--r-- | plugins/CryptoPP/src/base64.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/plugins/CryptoPP/src/base64.cpp b/plugins/CryptoPP/src/base64.cpp new file mode 100644 index 0000000000..7495dd234f --- /dev/null +++ b/plugins/CryptoPP/src/base64.cpp @@ -0,0 +1,97 @@ +#include "commonheaders.h"
+
+
+string base64encode(const string buf)
+{
+ string out;
+ char *base64 = base64encode(buf.data(), buf.length());
+ out.assign(base64);
+ free(base64);
+ return out;
+}
+
+
+char *base64encode(const char *inBuffer, const int count) {
+
+ int srcIndex = 0, destIndex = 0, remainder = count % 3;
+ char *outBuffer = (char *) malloc(count*2+1);
+ BYTE *inBufferPtr = (BYTE *) inBuffer;
+
+ while(srcIndex < count) {
+ outBuffer[destIndex++] = encode64(inBufferPtr[srcIndex] >> 2);
+ outBuffer[destIndex++] = encode64(((inBufferPtr[srcIndex] << 4) & 0x30) | ((inBufferPtr[srcIndex + 1] >> 4) & 0x0F));
+ srcIndex++;
+ outBuffer[destIndex++] = encode64(((inBufferPtr[srcIndex] << 2) & 0x3C) | ((inBufferPtr[srcIndex + 1] >> 6) & 0x03));
+ srcIndex++;
+ outBuffer[destIndex++] = encode64(inBufferPtr[srcIndex++] & 0x3F);
+ }
+
+ if (remainder == 2) {
+ outBuffer[destIndex - 1] = BPAD;
+ outBuffer[destIndex - 2] = encode64((inBufferPtr[srcIndex - 2] << 2) & 0x3C);
+ }
+ else if (remainder == 1) {
+ outBuffer[destIndex - 2] = outBuffer[destIndex - 1] = BPAD;
+ outBuffer[destIndex - 3] = encode64((inBufferPtr[srcIndex - 3] << 4) & 0x30);
+ }
+ destIndex -= (3 - remainder) % 3;
+ outBuffer[destIndex] = '\0';
+
+ return outBuffer;
+}
+
+
+string base64decode(const string buf) {
+ string out;
+ size_t len = buf.length();
+ char *plain = base64decode(buf.data(), &len);
+ out.assign(plain,len);
+ free(plain);
+ return out;
+}
+
+
+string base64decode(const char *buf) {
+ string out;
+ size_t len = strlen(buf);
+ char *plain = base64decode(buf, &len);
+ out.assign(plain,len);
+ free(plain);
+ return out;
+}
+
+
+char *base64decode(const char *inBuffer, size_t *count) {
+
+ int srcIndex = 0, destIndex = 0;
+ char *outBuffer = (char *) malloc(*count);
+
+ while(srcIndex < *count) {
+ BYTE c0, c1, c2 = 0, c3 = 0;
+ const size_t delta = *count - srcIndex;
+ c0 = decode64(inBuffer[srcIndex++]);
+ c1 = decode64(inBuffer[srcIndex++]);
+ if (delta > 2) {
+ c2 = decode64(inBuffer[srcIndex++]);
+ if (delta > 3)
+ c3 = decode64(inBuffer[srcIndex++]);
+ }
+ if ((c0 | c1 | c2 | c3) == BERR) {
+ free(outBuffer);
+ return(NULL);
+ }
+ outBuffer[destIndex++] = (c0 << 2) | (c1 >> 4);
+ if (delta > 2) {
+ outBuffer[destIndex++] = (c1 << 4) | (c2 >> 2);
+ if (delta > 3 )
+ outBuffer[destIndex++] = (c2 << 6) | (c3);
+ }
+ }
+ outBuffer[destIndex] = '\0';
+ *count = destIndex;
+
+ return outBuffer;
+}
+
+
+// EOF
|