• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Dragon  
            Dragon
            日歷
            <2008年9月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011
            統(tǒng)計
            • 隨筆 - 58
            • 文章 - 0
            • 評論 - 55
            • 引用 - 0

            導航

            常用鏈接

            留言簿(3)

            隨筆分類(58)

            隨筆檔案(58)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             
            /*
             *Base64Coder.h
             
            */

            #ifndef BASE64CODER_H
            #define BASE64CODER_H

            #include 
            <string.h>

            #ifdef   __BORLANDC__
            #define Boolean bool
            #define False false
            #define True true
            #else

            typedef unsigned Boolean;
            #ifndef False
            const Boolean False = 0;
            #endif
            #ifndef True
            const Boolean True = 1;
            #endif

            #endif

            class Base64Coder
            {
            public:
                Base64Coder();
                
            ~Base64Coder();

                
            char* Base64Encode(char const* origSigned, unsigned origLength);
                unsigned 
            char* Base64Decode(char* in, unsigned& resultSize,Boolean trimTrailingZeros); 

            private:
                Boolean haveInitedBase64DecodeTable;
                
            void initBase64DecodeTable();
                
            char* strDupSize(char const* str);
                
            char base64DecodeTable[256];
                
            char base64Char[64];
            }
            ;
            #endif
            /*
             *Base64Coder.cpp
             
            */

            #include 
            "Base64Coder.h"

            Base64Coder::Base64Coder()
            {
                
            int i,j=0;
                
            for (i = 'A'; i <= 'Z'++i) base64Char[j++= i;
                
            for (i = 'a'; i <= 'z'++i) base64Char[j++= i;
                
            for (i = '0'; i <= '9'++i) base64Char[j++= i;
                base64Char[j
            ++= '+';
                base64Char[j
            ++= '/';

                haveInitedBase64DecodeTable
            =False;
            }


            Base64Coder::
            ~Base64Coder()
            {

            }


            void Base64Coder::initBase64DecodeTable()
            {
                
            int i;
                
            for (i = 0; i < 256++i) base64DecodeTable[i] = (char)0x80;
                
            // default value: invalid

                
            for (i = 'A'; i <= 'Z'++i) base64DecodeTable[i] = 0 + (i - 'A');
                
            for (i = 'a'; i <= 'z'++i) base64DecodeTable[i] = 26 + (i - 'a');
                
            for (i = '0'; i <= '9'++i) base64DecodeTable[i] = 52 + (i - '0');
                base64DecodeTable[(unsigned 
            char)'+'= 62;
                base64DecodeTable[(unsigned 
            char)'/'= 63;
                base64DecodeTable[(unsigned 
            char)'='= 0;
            }


            unsigned 
            char* Base64Coder::Base64Decode(char* in, unsigned& resultSize,Boolean trimTrailingZeros) 
            {
                
            if (!haveInitedBase64DecodeTable) {
                    initBase64DecodeTable();
                    haveInitedBase64DecodeTable 
            = True;
                }


                unsigned 
            char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
                int k = 0;
                
            int const jMax = strlen(in- 3;
                
            // in case "in" is not a multiple of 4 bytes (although it should be)
                for (int j = 0; j < jMax; j += 4{
                    
            char inTmp[4], outTmp[4];
                    
            for (int i = 0; i < 4++i) {
                        inTmp[i] 
            = in[i+j];
                        outTmp[i] 
            = base64DecodeTable[(unsigned char)inTmp[i]];
                        
            if ((outTmp[i]&0x80!= 0) outTmp[i] = 0// pretend the input was 'A'
                    }


                    
            out[k++= (outTmp[0]<<2| (outTmp[1]>>4);
                    
            out[k++= (outTmp[1]<<4| (outTmp[2]>>2);
                    
            out[k++= (outTmp[2]<<6| outTmp[3];
                }


                
            if (trimTrailingZeros) {
                    
            while (k > 0 && out[k-1== '\0'--k;
                }

                resultSize 
            = k;
                unsigned 
            char* result = new unsigned char[resultSize];
                memmove(result, 
            out, resultSize);
                delete[] 
            out;

                
            return result;
            }


            char* Base64Coder::Base64Encode(char const* origSigned, unsigned origLength) 
            {
                unsigned 
            char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set
                if (orig == NULL) return NULL;

                unsigned 
            const numOrig24BitValues = origLength/3;
                Boolean havePadding 
            = origLength > numOrig24BitValues*3;
                Boolean havePadding2 
            = origLength == numOrig24BitValues*3 + 2;
                unsigned 
            const numResultBytes = 4*(numOrig24BitValues + havePadding);
                
            char* result = new char[numResultBytes+1]; // allow for trailing '\0'

                
            // Map each full group of 3 input bytes into 4 output base-64 characters:
                unsigned i;
                
            for (i = 0; i < numOrig24BitValues; ++i) {
                    result[
            4*i+0= base64Char[(orig[3*i]>>2)&0x3F];
                    result[
            4*i+1= base64Char[(((orig[3*i]&0x3)<<4| (orig[3*i+1]>>4))&0x3F];
                    result[
            4*i+2= base64Char[((orig[3*i+1]<<2| (orig[3*i+2]>>6))&0x3F];
                    result[
            4*i+3= base64Char[orig[3*i+2]&0x3F];
                }


                
            // Now, take padding into account.  (Note: i == numOrig24BitValues)
                if (havePadding) {
                    result[
            4*i+0= base64Char[(orig[3*i]>>2)&0x3F];
                    
            if (havePadding2) {
                        result[
            4*i+1= base64Char[(((orig[3*i]&0x3)<<4| (orig[3*i+1]>>4))&0x3F];
                        result[
            4*i+2= base64Char[(orig[3*i+1]<<2)&0x3F];
                    }
             else {
                        result[
            4*i+1= base64Char[((orig[3*i]&0x3)<<4)&0x3F];
                        result[
            4*i+2= '=';
                    }

                    result[
            4*i+3= '=';
                }


                result[numResultBytes] 
            = '\0';
                
            return result;
            }


            char* Base64Coder::strDupSize(char const* str) 
            {
                
            if (str == NULL) return NULL;
                
            int len = strlen(str) + 1;
                
            char* copy = new char[len];

                
            return copy;
            }

            下面是測試程序:
            /*
             *main.cpp
             
            */

            #include 
            "Base64Coder.h"

            #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))

            int main(void)
            {
                
            int i;
                Base64Coder 
            * p_Base64Coder=new Base64Coder();
                
            struct test {
                    unsigned 
            char *data;
                    
            char *encoded_ref;
                }
             tests[] = {
                    
            {(unsigned char*)"",        ""},
                    
            {(unsigned char*)"1",       "MQ=="},
                    
            {(unsigned char*)"22",      "MjI="},
                    
            {(unsigned char*)"333",     "MzMz"},
                    
            {(unsigned char*)"4444",    "NDQ0NA=="},
                    
            {(unsigned char*)"55555",   "NTU1NTU="},
                    
            {(unsigned char*)"666666",  "NjY2NjY2"},
                    
            {(unsigned char*)"abc:def""YWJjOmRlZg=="},
                }
            ;

                printf(
            "Encoding/decoding tests\n");
                
            for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
                
            {
                    unsigned 
            char * res;
                    unsigned length;
                    res
            =p_Base64Coder->Base64Decode(tests[i].encoded_ref,length,True);
                    
            if (strcmp((char *)res,(char *)tests[i].data)!=0)
                    
            {
                        printf(
            "Passed!  ");
                    }

                    
            else
                    
            {
                        printf(
            "Failed!  ");
                    }

                    
            for (int j=0;j<length;j++)
                    
            {
                        printf(
            "%c",res[j]);
                    }

                    printf(
            " Length:%d\n",length);
                    printf(
            "\n");
                }

                
            return 0;
            }

            開始時不小心,以為ffmpeg的Base64解碼不正確,就用了Live555的,后來發(fā)現(xiàn)ffmpeg的其實也是正確的。
            Live555的Decode函數(shù)最后的參數(shù)trimTrailingZeros設置成True就和ffmpeg的Base64一樣了,意為將尾部的0去掉。
            可以將上面測試程序中的True改成False,然后看打印出來的結(jié)果就明白了(打印的尾部0是看不到的)。
            posted on 2010-12-14 14:10 Dragon 閱讀(1432) 評論(0)  編輯 收藏 引用 所屬分類: C++
             
            Copyright © Dragon Powered by: 博客園 模板提供:滬江博客
            日本精品久久久久中文字幕| 久久精品国产只有精品66| 亚洲а∨天堂久久精品9966| 久久久WWW成人免费毛片| 99久久免费国产精精品| 成人午夜精品久久久久久久小说| 久久精品国产亚洲一区二区| 久久er热视频在这里精品| 色欲综合久久躁天天躁蜜桃| 伊人久久久AV老熟妇色| 9久久9久久精品| 久久综合丝袜日本网| 99久久精品国产一区二区蜜芽| 开心久久婷婷综合中文字幕| 久久天天躁狠狠躁夜夜不卡| 国产产无码乱码精品久久鸭| 久久婷婷五月综合成人D啪| 久久一区二区免费播放| 久久精品国产清自在天天线 | 久久强奷乱码老熟女网站| 精品综合久久久久久98| 久久免费精品视频| 日本精品久久久久久久久免费| 亚洲欧美国产日韩综合久久| 国产产无码乱码精品久久鸭| 国产国产成人久久精品| 性欧美丰满熟妇XXXX性久久久 | 久久久亚洲欧洲日产国码aⅴ | 精品熟女少妇AV免费久久| 97久久超碰国产精品旧版| 久久亚洲av无码精品浪潮| 久久99精品久久久久婷婷| 日韩亚洲国产综合久久久| 国产一久久香蕉国产线看观看| 青青草原综合久久大伊人导航| 国产V亚洲V天堂无码久久久| 狠狠综合久久综合中文88| 久久黄视频| 亚洲国产日韩欧美综合久久| 久久不见久久见免费影院www日本| 久久精品国产亚洲AV无码麻豆|