summaryrefslogtreecommitdiff
path: root/entry/src/base64.cpp
diff options
context:
space:
mode:
author崔一鸣 <[email protected]>2020-05-10 00:33:12 +0800
committer崔一鸣 <[email protected]>2020-05-10 00:33:12 +0800
commitbff06936f8e108f791b48e247cfffe439742b94e (patch)
tree314319c5df1b8eab4a4c30dd409b4d461f363c88 /entry/src/base64.cpp
parentb7bb7b7b2a86310e8f1670acd5dde38ebb849f8f (diff)
init commitHEADmaster
Diffstat (limited to 'entry/src/base64.cpp')
-rw-r--r--entry/src/base64.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/entry/src/base64.cpp b/entry/src/base64.cpp
new file mode 100644
index 0000000..b38e169
--- /dev/null
+++ b/entry/src/base64.cpp
@@ -0,0 +1,143 @@
+
+/*
+ base64.c - by Joe DF ([email protected])
+ Released under the MIT License
+
+ See "base64.h", for more information.
+
+ Thank you for inspiration:
+ http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode
+*/
+
+#include "base64.h"
+#include <stdio.h>
+#include <algorithm>
+
+//Base64 char table - used internally for encoding
+unsigned char b64_chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+unsigned int b64_int(unsigned int ch) {
+
+ // ASCII to base64_int
+ // 65-90 Upper Case >> 0-25
+ // 97-122 Lower Case >> 26-51
+ // 48-57 Numbers >> 52-61
+ // 43 Plus (+) >> 62
+ // 47 Slash (/) >> 63
+ // 61 Equal (=) >> 64~
+ if (ch==43)
+ return 62;
+ if (ch==47)
+ return 63;
+ if (ch==61)
+ return 64;
+ if ((ch>47) && (ch<58))
+ return ch + 4;
+ if ((ch>64) && (ch<91))
+ return ch - 'A';
+ if ((ch>96) && (ch<123))
+ return (ch - 'a') + 26;
+ return 0;
+}
+
+/*
+unsigned int b64e_size(unsigned int in_size) {
+
+ // size equals 4*floor((1/3)*(in_size+2));
+ int i, j = 0;
+ for (i=0;i<in_size;i++) {
+ if (i % 3 == 0)
+ j += 1;
+ }
+ return (4*j);
+}
+*/
+
+unsigned int b64d_size(unsigned int in_size) {
+
+ return ((3*in_size)/4);
+}
+
+unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned char* out) {
+
+ unsigned int i=0, j=0, k=0, s[3];
+
+ for (i=0;i<in_len;i++) {
+ s[j++]=*(in+i);
+ if (j==3) {
+ out[k+0] = b64_chr[ (s[0]&255)>>2 ];
+ out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
+ out[k+2] = b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ];
+ out[k+3] = b64_chr[ s[2]&0x3F ];
+ j=0; k+=4;
+ }
+ }
+
+ if (j) {
+ if (j==1)
+ s[1] = 0;
+ out[k+0] = b64_chr[ (s[0]&255)>>2 ];
+ out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
+ if (j==2)
+ out[k+2] = b64_chr[ ((s[1]&0x0F)<<2) ];
+ else
+ out[k+2] = '=';
+ out[k+3] = '=';
+ k+=4;
+ }
+
+ out[k] = '\0';
+
+ return k;
+}
+
+unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out) {
+
+ unsigned int i=0, j=0, k=0, s[4];
+
+ for (i=0;i<in_len;i++) {
+ s[j++]=b64_int(*(in+i));
+ if (j==4) {
+ out[k+0] = ((s[0]&255)<<2)+((s[1]&0x30)>>4);
+ if (s[2]!=64) {
+ out[k+1] = ((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2);
+ if ((s[3]!=64)) {
+ out[k+2] = ((s[2]&0x03)<<6)+(s[3]); k+=3;
+ } else {
+ k+=2;
+ }
+ } else {
+ k+=1;
+ }
+ j=0;
+ }
+ }
+ return k;
+}
+
+int test(){
+ unsigned char in_buff[100] = "ab\0d\0fghi";
+ unsigned char out_buff[100];
+ unsigned char *in = in_buff;
+ unsigned char *out = out_buff;
+ int in_len = 10;
+ int out_len = b64_encode(in, in_len, out);
+ printf("encode: out_len = %d\n", out_len);
+ for(int i = 0; i < out_len; i++){
+ printf("%c ", out[i]);
+ }
+ printf("\n");
+ std::swap(in, out);
+ in_len = out_len;
+ out_len = b64_decode(in, in_len, out);
+ printf("decode: out_len = %d\n", out_len);
+ for(int i = 0; i < out_len; i++){
+ printf("%c ", out[i]);
+ }
+ printf("\n");
+ return 0;
+}
+
+int main(){
+ test();
+} \ No newline at end of file