using namespace std;
static inline
bool
is_base64(unsigned char c) {
return
(isalnum(c) || (c
=
=
'+'
) || (c
=
=
'/'
));
}
std::string base64_decode(std::string const& encoded_string) {
const std::string base64_chars
=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
;
int
in_len
=
encoded_string.size();
int
i
=
0
;
int
j
=
0
;
int
in_
=
0
;
unsigned char char_array_4[
4
], char_array_3[
3
];
std::string ret;
int
iArraySourceBase
=
0
;
unsigned char char_array_Source[
49
]
=
{
0
};
while
(in_len
-
-
&& (encoded_string[in_] !
=
'='
) && is_base64(encoded_string[in_])) {
char_array_4[i
+
+
]
=
encoded_string[in_]; in_
+
+
;
if
(i
=
=
4
) {
for
(i
=
0
; i <
4
; i
+
+
)
char_array_4[i]
=
base64_chars.find(char_array_4[i]);
char_array_3[
0
]
=
(char_array_4[
0
] <<
2
)
+
((char_array_4[
1
] &
0x30
) >>
4
);
char_array_3[
1
]
=
((char_array_4[
1
] &
0xf
) <<
4
)
+
((char_array_4[
2
] &
0x3c
) >>
2
);
char_array_3[
2
]
=
((char_array_4[
2
] &
0x3
) <<
6
)
+
char_array_4[
3
];
char_array_Source[
0
+
iArraySourceBase]
=
char_array_3[
0
];
char_array_Source[
1
+
iArraySourceBase]
=
char_array_3[
1
];
char_array_Source[
2
+
iArraySourceBase]
=
char_array_3[
2
];
for
(i
=
0
; (i <
3
); i
+
+
)
ret
+
=
char_array_3[i];
i
=
0
;
iArraySourceBase
+
=
3
;
}
}
if
(i) {
for
(j
=
i; j <
4
; j
+
+
)
char_array_4[j]
=
0
;
for
(j
=
0
; j <
4
; j
+
+
)
char_array_4[j]
=
base64_chars.find(char_array_4[j]);
char_array_3[
0
]
=
(char_array_4[
0
] <<
2
)
+
((char_array_4[
1
] &
0x30
) >>
4
);
char_array_3[
1
]
=
((char_array_4[
1
] &
0xf
) <<
4
)
+
((char_array_4[
2
] &
0x3c
) >>
2
);
char_array_3[
2
]
=
((char_array_4[
2
] &
0x3
) <<
6
)
+
char_array_4[
3
];
for
(j
=
0
; (j < i
-
1
); j
+
+
)
ret
+
=
char_array_3[j];
}
for
(
int
iIndex
=
5
; iIndex <
48
; iIndex
+
+
)
{
int
iYingSheIndex
=
(~iIndex &
0x7FFFFFFFu
)
%
(unsigned __int64)(((~iIndex &
0x7FFFFFFF
) >>
31
) ^ (unsigned
int
)(((~iIndex &
0x7FFFFFFF
) >>
31
)
+
64
));
/
/
上面的等价于 iYingSheIndex
=
63
-
iIndex;
unsigned char cc
=
base64_chars[iYingSheIndex];
char result
=
(char_array_Source[iIndex] ^ cc)
+
iIndex;
char_array_Source[iIndex]
=
result;
}
/
/
char_array_Source 是最终结果
printf(
"%s\n"
, char_array_Source);
return
ret;
}
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
base64_decode(std::string(
"zMXHz3SfvgTQwbWDFWiomWDYCxDpdK1VaWmbhrfsuvvEuGiceW0Rk0u8Ehm8Eee/"
));
getchar();
return
0
;
}