Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00053 #include <stdint.h>
00054 #include <string.h>
00055
00056 #include <sys/heap.h>
00057 #include <sys/types.h>
00062
00063 static prog_char base64etab[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00064
00077 char *NutEncodeBase64(CONST char* str)
00078 {
00079 char *encoded;
00080 size_t length;
00081 size_t encoded_length;
00082 int idx, enc_pos;
00083 int32_t bits;
00084 int cols;
00085 int char_count;
00086
00087
00088
00089
00090
00091
00092
00093
00094 length = strlen(str);
00095
00096
00097 encoded_length = ((length + 1) * 4) / 3;
00098
00099 encoded_length += encoded_length % 3;
00100
00101 encoded_length += (encoded_length / 72) * 2 + 3;
00102 ;
00103 encoded = NutHeapAlloc(encoded_length);
00104
00105 if (encoded == NULL) return NULL;
00106
00107 enc_pos = 0;
00108 char_count = 0;
00109 bits = 0;
00110 cols = 0;
00111
00112 for (idx = 0; idx < length; idx ++) {
00113 bits += (int32_t)str[idx];
00114 char_count ++;
00115 if (char_count == 3) {
00116 encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 18) & 0x3f]);
00117 encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 12) & 0x3f]);
00118 encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 6) & 0x3f]);
00119 encoded[enc_pos++] = PRG_RDB(&base64etab[bits & 0x3f]);
00120 cols += 4;
00121 if (cols == 72) {
00122 encoded[enc_pos++] = '\r';
00123 encoded[enc_pos++] = '\n';
00124 cols =0;
00125 }
00126 bits = 0;
00127 char_count = 0;
00128 } else {
00129 bits <<= 8;
00130 }
00131 }
00132
00133 if (char_count != 0) {
00134 bits <<= 16 - (8 * char_count);
00135 encoded[enc_pos++] = PRG_RDB(&base64etab[bits >> 18]);
00136 encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 12) & 0x3f]);
00137 if (char_count == 1) {
00138 encoded[enc_pos++] = '=';
00139 encoded[enc_pos++] = '=';
00140 } else {
00141 encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 6) & 0x3f]);
00142 encoded[enc_pos++] = '=';
00143 }
00144 }
00145 encoded[enc_pos] = '\0';
00146 return encoded;
00147 }
00148