⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.h

📁 C++ patterns设计模式
💻 H
字号:
////////////////////////////////////////////////////////////////////////////////
// Base64 编/解码
////////////////////////////////////////////////////////////////////////////////
// Author      : Darkay Li                                                        
// Description : Base64编码解码模块
//
// Version     : 1.0
//
// Standard include files : std_inc.hpp(in precompile header)
//
// Change Log  : 
// 2003年5月9日 by Darkay Li 
// -- explant from xerces
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_BASE64
#define INCLUDED_BASE64

#if defined(HAS_PRAGMA_ONCE)
#pragma PRAGMA_ONCE_DECLARE
#endif

namespace stk
{
    class Base64
    {
    public :
        //* Encodes octets into Base64 data
        //*
        //* NOTE: The returned buffer is dynamically allocated and is the
        //* responsibility of the caller to delete it when not longer needed.
        //* You can call XMLString::release to release this returned buffer.
        //*
        //* @param inputData Binary data in unsigned char stream.
        //* @param inputLength Length of the unsigned char stream.
        //* @param outputLength Length of the encoded Base64 byte stream.
        //* @return Encoded Base64 data in unsigned char stream,
        static unsigned char* encode(const unsigned char* const inputData,
                                     const unsigned int   inputLength,
                                     unsigned int*        outputLength);

        //* Decodes Base64 data into octets
        //*
        //* NOTE: The returned buffer is dynamically allocated and is the
        //* responsibility of the caller to delete it when not longer needed.
        //*
        //* @param inputData Base64 data in unsigned char stream.
        //* @param outputLength Length of decoded unsigned char stream.
        //* @return Decoded binary data in unsigned char stream,
        //*      or NULL if input data can not be decoded.
        static unsigned char* decode(const unsigned char* const inputData,
            unsigned int*        outputLength);

        //* Get data length
        //*
        //* Returns length of decoded data given an array
        //* containing encoded data.
        //*
        //* @param inputData Base64 data in XMLCh stream.
        //* @return Length of decoded data,
        //*      or -1 if input data can not be decoded.
        //static int getDataLength(const XMLCh* const inputData );

    private :

        // -----------------------------------------------------------------------
        //  Helper methods
        // -----------------------------------------------------------------------

        static void init();

        static bool isData(const unsigned char& octet);
        static bool isPad(const unsigned char& octet);

        static unsigned char set1stOctet(const unsigned char&, const unsigned char&);
        static unsigned char set2ndOctet(const unsigned char&, const unsigned char&);
        static unsigned char set3rdOctet(const unsigned char&, const unsigned char&);

        static void split1stOctet(const unsigned char&, unsigned char&, unsigned char&);
        static void split2ndOctet(const unsigned char&, unsigned char&, unsigned char&);
        static void split3rdOctet(const unsigned char&, unsigned char&, unsigned char&);

        // -----------------------------------------------------------------------
        //  Unimplemented constructors and operators
        // -----------------------------------------------------------------------
        Base64();
        Base64(const Base64&);

        // -----------------------------------------------------------------------
        //  Private data members
        //
        //  base64Alphabet
        //     The Base64 alphabet (see RFC 2045).
        //
        //  base64Padding
        //     Padding character (see RFC 2045).
        //
        //  base64Inverse
        //     Table used in decoding base64.
        //
        //  isInitialized
        //     Set once base64Inverse is initalized.
        //
        //  quadsPerLine
        //     Number of quadruplets per one line. The encoded output
        //     stream must be represented in lines of no more
        //     than 19 quadruplets each.
        //
        // -----------------------------------------------------------------------

        static const unsigned char  base64Alphabet[];
        static const unsigned char  base64Padding;

        static unsigned char  base64Inverse[];
        static bool  isInitialized;

        static const unsigned int  quadsPerLine;
    };

    // -----------------------------------------------------------------------
    //  Helper methods
    // -----------------------------------------------------------------------
    inline bool Base64::isPad(const unsigned char& octet)
    {
        return ( octet == base64Padding );
    }

    inline unsigned char Base64::set1stOctet(const unsigned char& b1, const unsigned char& b2)
    {
        return (( b1 << 2 ) | ( b2 >> 4 ));
    }

    inline unsigned char Base64::set2ndOctet(const unsigned char& b2, const unsigned char& b3)
    {
        return (( b2 << 4 ) | ( b3 >> 2 ));
    }

    inline unsigned char Base64::set3rdOctet(const unsigned char& b3, const unsigned char& b4)
    {
        return (( b3 << 6 ) | b4 );
    }

    inline void Base64::split1stOctet(const unsigned char& ch, unsigned char& b1, unsigned char& b2) {
        b1 = ch >> 2;
        b2 = ( ch & 0x3 ) << 4;
    }

    inline void Base64::split2ndOctet(const unsigned char& ch, unsigned char& b2, unsigned char& b3) {
        b2 |= ch >> 4;  // combine with previous value
        b3 = ( ch & 0xf ) << 2;
    }

    inline void Base64::split3rdOctet(const unsigned char& ch, unsigned char& b3, unsigned char& b4) {
        b3 |= ch >> 6;  // combine with previous value
        b4 = ( ch & 0x3f );
    }
};

//define stk name
typedef stk::Base64 CTBase64;

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -