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

📄 base64.cpp

📁 base64 功能
💻 CPP
字号:
/**
* @file base64.cpp
* @brief Convert  to base64code and output
* @author hart (thesunof2000@yahoo.com.cn)
* @version 1.0.0
* @date 2007/11/20
* @history 2007/11/21,   //修正读取写入文件速度慢的问题,修正一些参数的定义和合法 
*/
#include <cstdlib>
#include <iostream>
using namespace std;
char * cpsourcefile;//read
char * cpaimfile;  //write
FILE * g_fpwrite;
    
char g_cmapcode[]=
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
 '0','1','2','3','4','5','6','7','8','9','+','/','='};
class base64
{
   public:
    void show_message(int nargc);
    
    void open_file(int nflag);
    void close_file(int nflag);
    
    void decode_readsource();
    void decode_writeaimfile(char cwrite);   
    void decode_convertonenum(char * get_input,int nbytenum);
    void decode_splittofour(int get_input,int nbytenum);
    void encode_readfile();
    void encode_toforpart(char * input);
    void encode_toint(int * forpart,int nequflag);
    void encode_writeaimfile(char cwrite);
};

int main(int argc, char *argv[])
{  
    cpsourcefile=argv[2];
    cpaimfile=argv[3];
    base64 base64obj;//new a obj;
    base64obj.show_message(argc);
    if(*argv[1]=='e')
    {
      base64obj.open_file(1);
      base64obj.decode_readsource();
      base64obj.close_file(1);
    }else
    if(*argv[1]=='d')
    {
      base64obj.open_file(1);
      base64obj.encode_readfile();
      base64obj.close_file(1); 
    }
    return 0;
}
void base64::close_file(int nflag)
{
     fclose(g_fpwrite);
}
void base64::open_file(int nflag)
{     
      g_fpwrite=fopen(cpaimfile,"ab+");
     
 }
/*
*@brief to a int num
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment int * forpart
*/
void base64:: encode_toint(int * forpart,int nequflag)
{   
     int ntemp=0;
     int nresult=0;
     ntemp=ntemp|( ((*forpart))<<18);
     ntemp=ntemp|( (*(forpart+1))<<12);
     ntemp=ntemp|( (*(forpart+2))<<6);
     ntemp=ntemp|( (*(forpart+3))); 
     if(nequflag)
     {
        encode_writeaimfile(ntemp>>16);
        if(!((ntemp>>8)&255))
        {
        encode_writeaimfile((ntemp>>8)&255);
        }
        if(!(ntemp&255))
        {
        encode_writeaimfile(ntemp&255); 
        }                    
     } 
     else 
     {  
     encode_writeaimfile(ntemp>>16);
     encode_writeaimfile((ntemp>>8)&255);
     encode_writeaimfile(ntemp&255);
     }    
 }
/*
*@brief to for num
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment char * input
*/
void base64::encode_toforpart(char * input)
{
     int nfourpart[4]={0,0,0,0};
     int nindex=0;
     int nequflag=0;
     for(nindex=0;nindex<65;nindex++)
     {
           if(g_cmapcode[nindex]==(* input))
           {
            nfourpart[0]=nindex;          
           }
           if(g_cmapcode[nindex]==(* (input+1)))
           {
             nfourpart[1]=nindex;    
           }
           if(g_cmapcode[nindex]==(* (input+2)))
           {
              nfourpart[2]=nindex;           
           }
            if(g_cmapcode[nindex]==(* (input+3)))
           {
            nfourpart[3]=nindex;            
           }                                        
     } 
    int i=0;
    for(i=0;i<4;i++)
    {
       if(nfourpart[i]==64)
       {
         nfourpart[i]=0;
         nequflag=1;//has read '='
       }
       
    }
    encode_toint(nfourpart,nequflag);      
    return;  
}
/*
*@brief write file
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment char cwrite
*/
void base64::encode_readfile()
{
   FILE * fpsource;
   fpsource=fopen(cpsourcefile,"rb");
   int  nreadcount=0; //isnot read 3 char?
   char creadin=' ';
   char creadtemp[4];
   memset(creadtemp,0,4);
   if(fpsource==NULL)
   {
     cout<<"read file failed,please check file isnot exist?\n";
     system("PAUSE");
     exit(1);
   }
   creadin=fgetc(fpsource);
   while(!feof(fpsource))
   {      
        if(creadin!=10&&creadin!=13)
        {              
            creadtemp[nreadcount]=creadin;
            nreadcount++;      
            if(nreadcount==4)
            { 
              encode_toforpart(creadtemp);
              memset(creadtemp,0,4);
              nreadcount=0;              
            }
        }     
       creadin=fgetc(fpsource);       
   }   
}
/*
*@brief write file
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment char cwrite
*/
void base64::encode_writeaimfile(char cwrite)
{    
   
     if(g_fpwrite==NULL)
     {
     cout<<"write file failed!try again,and check the power!\n";
     exit(1);
     }
     fputc(cwrite, g_fpwrite);
     
}
/*
*@brief write file
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment char cwrite
*/
void base64::decode_writeaimfile(char cwrite)
{    
     static int nnewline=0;
     nnewline++;
    
     if(g_fpwrite==NULL)
     {
     cout<<"write file failed!try again,and check the power!\n";
     exit(1);
     }
     fputc(cwrite, g_fpwrite);
     if(nnewline%77==0)
     {     
      fputc(13, g_fpwrite); 
      fputc(10, g_fpwrite);     
      }      
     
}
/*
*@brief split one int num to four num;
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment short * get_input
*/
void base64::decode_splittofour(int get_input,int nbytenum)
{  
     int nsplited_1=0;
     int nsplited_2=0;
     int nsplited_3=0;
     int nsplited_4=0;
     //split to four one has 6 wei
     nsplited_1=(get_input>>18)&0x3f;
     nsplited_2=(get_input>>12)&0x3f;
     nsplited_3=(get_input>>6)&0x3f;
     nsplited_4=(get_input)&0x3f;    
     if(nbytenum==2)// only 2 char
     {
     decode_writeaimfile(g_cmapcode[nsplited_1]);// go to write file 
     decode_writeaimfile(g_cmapcode[nsplited_2]);
     decode_writeaimfile(g_cmapcode[nsplited_3]);
     decode_writeaimfile(g_cmapcode[64]);  
     return;            
     }
     if(nbytenum==1)//only 1 char
     {
     decode_writeaimfile(g_cmapcode[nsplited_1]);// go to write file 
     decode_writeaimfile(g_cmapcode[nsplited_2]);
     decode_writeaimfile(g_cmapcode[64]);
     decode_writeaimfile(g_cmapcode[64]);  
     return;            
     }    
     decode_writeaimfile(g_cmapcode[nsplited_1]);// go to write file 
     decode_writeaimfile(g_cmapcode[nsplited_2]);
     decode_writeaimfile(g_cmapcode[nsplited_3]);
     decode_writeaimfile(g_cmapcode[nsplited_4]);
 }
/*
*@brief convert one int num
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment short * get_input
*/
void base64::decode_convertonenum(char * get_input,int nbytenum)
{
      int ntemp_0=0;
      int ntemp_1=0;
      int ntemp_2=0;
      int nresult=0;  //this is result     
      ntemp_0=((*get_input)&255)<<16;
      ntemp_1=((*(get_input+1))&255)<<8;
      ntemp_2=(*(get_input+2))&255;      //convert to a num
      nresult=ntemp_0|ntemp_1|ntemp_2; 
      decode_splittofour(nresult,nbytenum); 
}
/*
*@brief readsource file
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment char * cfilename
*/
void base64::decode_readsource()
{
   FILE * fpsource;
   fpsource=fopen(cpsourcefile,"rb");
   int  nreadcount=0; //isnot read 3 char?
   char creadin=' ';
   char creadtemp[3];
   memset(creadtemp,0,3);
   if(fpsource==NULL)
   {
     cout<<"read file failed,please check file isnot exist?\n";
     exit(1);
   }
   creadin=fgetc(fpsource);
   while(!feof(fpsource))
   {  
               
        creadtemp[nreadcount]=creadin;
        nreadcount++;
        
        if(nreadcount==3)
        { 
           decode_convertonenum(creadtemp,3);
           memset(creadtemp,0,3);
           nreadcount=0;              
        }       
          creadin=fgetc(fpsource);        
   }
       if(nreadcount!=0)
         {
            if(nreadcount%3==1)
            {
             decode_convertonenum(creadtemp,1);                  
            }
            if(nreadcount%3==2)
            {
               decode_convertonenum(creadtemp,2);
            }
         }            
}
/*
*@brief show wrong message
*@author hart (thesunof2000@yahoo.com.cn)
*@ parment int nargc
*/
void base64::show_message(int nargc)
{ 
   if(nargc!=4)
   {
    cout<<"cmd usage:\n";
    cout<<"you can input like this:\n";
    cout<<"./base64 type sourcefile destfile\n";
    cout<<"type has two choise:\n";
    cout<<"e is convert to base64code:\n";
    cout<<"d is convert to word:\n";
    cout<<"source is what file you want convert to base64code!\n";
    cout<<"destfile is what file you want convert base64code to word!\n";
    exit(0);
   }
   return;
}

⌨️ 快捷键说明

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