| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2543 人关注过本帖
标题:改写的base64编码部分
取消只看楼主 加入收藏
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
结帖率:88.89%
收藏
 问题点数:0 回复次数:3 
改写的base64编码部分
参照了linux里面base64的设计思路,编码的时候每行76个字符。
实现的编码,和linux自带工具的base64结果不同。

下面第一部分是linux自带工具生成编码的结尾部分,第二个是自己改写的部分生成的
编码部分结尾

VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVTEFNRTMuOTJVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVU=

VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVTEFNRTMuOTJVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVX//w==



贴上代码,不知道问题错在哪里?

void encoding(char *sourcefile,char *destfile)
{
    FILE *file,*dest;
    file=fopen(sourcefile,"rb");
    dest=fopen(destfile,"wb");
    byte f[3];
//    int flag=0;      // 这个是换行标志
    if(file==NULL)
    {
        printf("Cannot open the %s file!",sourcefile);
        exit(1);
    }
   
    int i=0;
    int c;
    int row=0;          // 增加的代码,每行76个字符
   
    while(!feof(file))
    {
        f[i]=fgetc(file);
        i++;
        if(i==3)
        {
            c=(f[0]>>2)&0x3F;
            
             if ( row==76 )
            {
               fputs("\n",dest);
               row = 0;
             }
         //   writechar(c,&flag,dest);
            fputc(code[c],dest);
            row++;

            c=((f[0]<<4)|(f[1]>>4))&0x3F;
            
            if ( row==76 )
            {
               fputs("\n",dest);
               row = 0;
             }
            // writechar(c,&flag,dest);
            
            fputc(code[c],dest);
            row++;

            c=((f[1]<<2)|(f[2]>>6))&0x3F;
            
             if ( row==76 )
            {
               fputs("\n",dest);
               row = 0;
             }
            
            //writechar(c,&flag,dest);
            fputc(code[c],dest);
            row++;

            c=f[2]&0x3F;
            
            if ( row==76 )
            {
               fputs("\n",dest);
               row = 0;
             }
            
            //writechar(c,&flag,dest);
            fputc(code[c],dest);
            row++;
            i=0;
        }
    }

    if(i==1)
    {
        c=(f[0]>>2)&0x3F;
        
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
      
        //writechar(c,&flag,dest);
        fputc(code[c],dest);
        row++;
        
        c=(f[0]<<4)&0x3F;
        
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        //writechar(c,&flag,dest);
        fputc(code[c],dest);
        row++;
        
           if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        fputc('=',dest);
        row++;
           if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }        
        fputc('=',dest);
        row++;
    }

    if(i==2)
    {
        c=(f[0]>>2)&0x3F;
        
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
//       writechar(c,&flag,dest);
        fputc(code[c],dest);
        row++;
        
        c=((f[0]<<4)|(f[1]>>4))&0x3F;
        
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        //writechar(c,&flag,dest);
        fputc(code[c],dest);
        row++;
        
        c=(f[1]<<2)&60;
        
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        //writechar(c,&flag,dest);
        fputc(code[c],dest);
        row++;            // 增加的代码
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        fputc('=',dest);      // 这句没有被执行到
        row++;
           if ( row==76 )
        {
           fputs("\n",dest);
           row = 0;
        }
        fputc('=',dest);     // 这句没有被执行到
        row++;               // 增加的代码
    }
    fclose(dest);
    fclose(file);
}






搜索更多相关主题的帖子: 编码 FILE flag row code 
2019-07-11 11:25
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
收藏
得分:0 
回复 2楼 rjsp
版主,我发现我用printf函数打印i的输出,发现 if i==3的部分并没有被执行到,
是不是这里要改成 if (i==0)
2019-07-11 13:48
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
收藏
得分:0 
回复 4楼 rjsp
#include <stdio.h>
#include <stdlib.h>

void encoding(char *sourcefile,char *destfile)
{
    FILE *outfile = fopen(destfile, "wb" );
    FILE *infile = fopen(sourcefile, "rb");
    unsigned char buf_input[3072];       // 编码包是3072字节
    unsigned char buf_ouput[4096];       // 解码包是4096字节

    if( outfile == NULL || infile == NULL )
    {
        printf("%s, %s",sourcefile,destfile,"not exit.\n");
        exit(1);
    }
    int rc;
    while( (rc = fread(buf_input,sizeof(unsigned char),3072,infile)) != 0 )
    {
        base64_encode(buf_input,buf_ouput);
        fwrite( buf_ouput, sizeof( unsigned char ), rc, outfile );   // 这里有点问题
    }
}

我找了个库想用读写缓冲区的方法进行编码,最后一行有点疑问。
        fwrite( buf_ouput, sizeof( unsigned char ), rc, outfile );
                                                    ~~
                                              这里每次写入的是4096字节,最后一个缓冲如何处理?
                                              写入的字节数是rc/3 * 4,还有padding的字节,应该
                                              如何处理?


2019-07-11 16:59
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
收藏
得分:0 
回复 6楼 rjsp
链接:https://pan.baidu.com/s/17IR3FfHMC_ITkyatgXgDMg
提取码:h25w
复制这段内容后打开百度网盘手机App,操作更方便哦

我想参考linux里面的开源代码的思路来的。上面写缓冲区的方法,不会生成像linux那样进行每行
76的字符形式存放的格式。

那个代码我直接在windows xp下面的msys 1.0下面,./configue生成makefile失败。
我就无法把它顺利移植到windows xp。现在想扒它的代码,用上面的思路进行移植。


[此贴子已经被作者于2019-7-11 17:28编辑过]

2019-07-11 17:23
快速回复:改写的base64编码部分
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022648 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved