MCPcopy Create free account
hub / github.com/MaxBelkov/visualsyslog / base64_decode

Function base64_decode

sourcecommon/base64.cpp:58–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56}
57
58unsigned char *
59base64_decode(const char *input, int length, int *outlen)
60{
61 int i = 0;
62 int j;
63 int r = 0;
64 int idx = 0;
65 unsigned char char_array_4[4], char_array_3[3];
66 unsigned char *output = new unsigned char[length*3/4];
67
68 while (length-- && input[idx] != '=') {
69 //skip invalid or padding based chars
70 if (!is_base64(input[idx])) {
71 idx++;
72 continue;
73 }
74 char_array_4[i++] = input[idx++];
75 if (i == 4) {
76 for (i = 0; i < 4; i++)
77 char_array_4[i] = strchr(base64_chars, char_array_4[i]) - base64_chars;
78
79 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
80 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
81 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
82
83 for (i = 0; (i < 3); i++)
84 output[r++] = char_array_3[i];
85 i = 0;
86 }
87 }
88
89 if (i) {
90 for (j = i; j <4; j++)
91 char_array_4[j] = 0;
92
93 for (j = 0; j <4; j++)
94 char_array_4[j] = strchr(base64_chars, char_array_4[j]) - base64_chars;
95
96 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
97 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
98 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
99
100 for (j = 0; (j < i - 1); j++)
101 output[r++] = char_array_3[j];
102 }
103
104 *outlen = r;
105
106 return output;
107}

Callers

nothing calls this directly

Calls 1

is_base64Function · 0.85

Tested by

no test coverage detected