#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char
base64CharsArr[] =
"gJ1BRjQie/FIWhEslq7GxbnL26M4+HXUtcpmVTKaydOP38of5v90ZSwrkYzCAuND"
;
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]=
"GQTZlSqQXZ/ghxxwhju3hbuZ4wufWjujWrhYe7Rce7ju"
;
base64decode(str);
return
0;
}