summaryrefslogtreecommitdiff
path: root/plugins/CryptoPP/base64.cpp
diff options
context:
space:
mode:
authorVadim Dashevskiy <watcherhd@gmail.com>2012-05-15 10:38:20 +0000
committerVadim Dashevskiy <watcherhd@gmail.com>2012-05-15 10:38:20 +0000
commit48540940b6c28bb4378abfeb500ec45a625b37b6 (patch)
tree2ef294c0763e802f91d868bdef4229b6868527de /plugins/CryptoPP/base64.cpp
parent5c350913f011e119127baeb32a6aedeb4f0d33bc (diff)
initial commit
git-svn-id: http://svn.miranda-ng.org/main/trunk@2 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/CryptoPP/base64.cpp')
-rw-r--r--plugins/CryptoPP/base64.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/plugins/CryptoPP/base64.cpp b/plugins/CryptoPP/base64.cpp
new file mode 100644
index 0000000000..f639799b92
--- /dev/null
+++ b/plugins/CryptoPP/base64.cpp
@@ -0,0 +1,96 @@
+#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;
+ int len = buf.length();
+ char *plain = base64decode(buf.data(), &len);
+ out.assign(plain,len);
+ free(plain);
+ return out;
+}
+
+
+string base64decode(const char *buf) {
+ string out;
+ int len = strlen(buf);
+ char *plain = base64decode(buf, &len);
+ out.assign(plain,len);
+ free(plain);
+ return out;
+}
+
+
+char *base64decode(const char *inBuffer, int *count) {
+
+ int srcIndex = 0, destIndex = 0;
+ char *outBuffer = (char *) malloc(*count);
+
+ while(srcIndex < *count) {
+ BYTE c0, c1, c2 = 0, c3 = 0;
+ const int 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