#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char
base64CharsArr[] =
"qvEJAfHmUYjBac+u8Ph5n9Od17FrICL/X0gVtM4Qk6T2z3wNSsyoebilxWKGZpRD"
;
void
base64decode(
char
str[]) {
int
length =
strlen
(str);
int
padding = 0;
if
(str[length - 1] ==
'='
) {
padding++;
if
(str[length - 2] ==
'='
)
padding++;
}
int
decodedLength = (length * 3) / 4 - padding;
char
* decodedStr = (
char
*)
malloc
(decodedLength + 1);
int
outIndex = 0;
for
(
int
i = 0; i < length; i += 4) {
char
char1 = -1, char2 = -1, char3 = -1, char4 = -1;
for
(
int
j = 0; j < 64; j++) {
if
(base64CharsArr[j] == str[i]) {
char1 = j;
break
;
}
}
for
(
int
j = 0; j < 64; j++) {
if
(base64CharsArr[j] == str[i + 1]) {
char2 = j;
break
;
}
}
for
(
int
j = 0; j < 64; j++) {
if
(base64CharsArr[j] == str[i + 2]) {
char3 = j;
break
;
}
}
for
(
int
j = 0; j < 64; j++) {
if
(base64CharsArr[j] == str[i + 3]) {
char4 = j;
break
;
}
}
decodedStr[outIndex++] = (char1 << 2) | ((char2 & 0x30) >> 4);
if
(char3 != -1)
decodedStr[outIndex++] = ((char2 & 0xf) << 4) | ((char3 & 0x3c) >> 2);
if
(char4 != -1)
decodedStr[outIndex++] = ((char3 & 0x3) << 6) | char4;
}
decodedStr[decodedLength] =
'\0'
;
printf
(
"Decoded string: %s\n"
, decodedStr);
free
(decodedStr);
}
int
main() {
char
str[100]=
"5Mc58bPHLiAx7J8ocJIlaVUxaJvMcoYMaoPMaOfg15c475tscHfM/8=="
;
base64decode(str);
return
0;
}