Real-time base64 decoding code for VC, dedicated for vc6

Source: Internet
Author: User

Because vc6 does not have the base64 API, this is only for vc6, and vc6 and later versions all have their own APIs. This is also directly copied from the API to ensure it can be used. It is not very useful to find a few on the Internet.

Inline int decodebase64char (unsigned int ch) Throw () {// returns-1 if the character is invalid // or shocould be skipped // otherwise, returns the 6-bit code for the character // from the encoding tableif (CH> = 'A' & Ch <= 'Z') return ch-'A' + 0; // 0 range starts at 'A' if (CH> = 'A' & Ch <= 'Z') return ch-'A' + 26; // 26 range starts at 'A' if (CH> = '0' & Ch <= '9') return ch-'0' + 52; // 52 range Starts at '0' if (CH = '+') return 62; If (CH = '/') return 63; Return-1 ;} inline bool base64decode (lpcstr szsrc, int nsclen, byte * pbdest, int * pndestlen) Throw () {// walk the source buffer // each four character sequence is converted to 3 bytes // crlfs and =, and any characters not in the encoding table // are skipedif (szsrc = NULL | pndestlen = NULL) {atlassert (false); Return false;} lpcstr szsrcen D = szsrc + nsclen; int nwritten = 0; bool boverflow = (pbdest = NULL )? True: false; while (szsrc <szsrcend & (* szsrc )! = 0) {DWORD dwcurr = 0; int I; int nbits = 0; for (I = 0; I <4; I ++) {If (szsrc> = szsrcend) break; int NCH = decodebase64char (* szsrc); szsrc ++; If (NCH =-1) {// skip this Chari --; continue;} dwcurr <= 6; dwcurr | = NCH; nbits + = 6;} If (! Boverflow & nwritten + (nbits/8)> (* pndestlen) boverflow = true; // dwcurr has the 3 bytes to write to the output buffer // left to rightdwcurr <= 24-nbits; for (I = 0; I <nbits/8; I ++) {If (! Boverflow) {* pbdest = (byte) (dwcurr & 0x00ff0000)> 16); pbdest ++;} dwcurr <= 8; nwritten ++ ;}} * pndestlen = nwritten; If (boverflow) {If (pbdest! = NULL) {atlassert (false);} return false;} return true ;}

Directly call base64decode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.