| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2542 人关注过本帖
标题:改写的base64编码部分
只看楼主 加入收藏
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
结帖率:88.89%
收藏
 问题点数:0 回复次数:7 
改写的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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
但凡看到  while(!feof(file)) 之类,基本上可以认定为错误。
这个问题算得上是 月经贴 了。https://bbs.bccn.net/thread-495691-1-1.html
2019-07-11 13:36
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
收藏
得分:0 
回复 2楼 rjsp
版主,我发现我用printf函数打印i的输出,发现 if i==3的部分并没有被执行到,
是不是这里要改成 if (i==0)
2019-07-11 13:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
以下是引用chaw899在2019-7-11 13:48:22的发言:

版主,我发现我用printf函数打印i的输出,发现 if i==3的部分并没有被执行到,
是不是这里要改成 if (i==0)
我在2楼说过是 while(!feof(file)) 错了呀

你的代码不全,且又没给测试用例。我只能重写一个
程序代码:
#include <cstdio>

bool encoding( const char* sourcefile, const char* destfile )
{
    FILE *src, *dst;
    {
        src = fopen( sourcefile, "rb" );
        if( !src )
            return false;
        dst = fopen( destfile, "wt" );
        if( !dst )
        {
            fclose( src );
            return false;
        }
    }

    unsigned char f[3];
    for( size_t n, col=0; n=fread(f,1,3,src), n!=0; col%=76 )
    {
        if( n < 2 ) f[1] = 0;
        if( n < 3 ) f[2] = 0;
        static const char code[] = "ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        unsigned char a = code[ (f[0]>>2)&0x3F ];
        unsigned char b = code[ ((f[0]<<4)|(f[1]>>4))&0x3F ];
        unsigned char c = code[ ((f[1]<<2)|(f[2]>>6))&0x3F ];
        unsigned char d = code[ f[2]&0x3F ];
        if( n < 2 ) c = '=';
        if( n < 3 ) d = '=';

        fputc( a, dst );
        if( ++col == 76 ) fputc('\n',dst);
        fputc( b, dst );
        if( ++col == 76 ) fputc('\n',dst);
        fputc( c, dst );
        if( ++col == 76 ) fputc('\n',dst);
        fputc( d, dst );
        if( ++col == 76 ) fputc('\n',dst);
    }

    bool ret = !ferror(src) && !ferror(dst);
    fclose( dst );
    fclose( dst );
    return ret;
}

int main( void )
{
    if( !encoding("src.txt","dst.txt") )
    {
        puts( "文件读写失败." );
        return 1;
    }

    puts( "OK." );
    return 0;
}

假如你用的是 C语言 的话,那将
#include <cstdioh>
改为
#include <stdio.h>
#include <stdbool.h>
即可。



[此贴子已经被作者于2019-7-12 09:03编辑过]

2019-07-11 15:03
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
听不懂

这里每次写入的是4096字节
可能你 rc 是 3072 呀,哪里重新赋值了?
假设 base64_encode 是正确的,那应该是 fwrite( buf_ouput, sizeof(unsigned char), (rc+2)/3*4, outfile );



2019-07-11 17:12
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
回复 7楼 chaw899
你现在想要干什么?完整的代码我在4楼已经给你了。
如果你不想“每行76的字符形式存放”的话,那只要将所有的 if( ++col == 76 ) fputc('\n',dst); 删除即可;
如果你一定要“缓冲区”的话(其实没屁用,因为 FILE 本身就是带缓冲的),那只要将 fread 读3个字节改为读很多个字节就行了,代码如下:

程序代码:
#include <cstdio>

size_t encoding( const void* source, size_t src_len, void* destination )
{
    const unsigned char* src = (const unsigned char*)source;
    unsigned char* dst = (unsigned char*)destination;

    for( size_t i=0; i<src_len; i+=3 )
    {
        size_t n = src_len-i < 3 ? src_len-i : 3;
        unsigned char f[3] = { src[i], n>1?src[i+1]:0, n>2?src[i+2]:0 };

        const char* code = "ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        unsigned char a = code[ (f[0]>>2)&0x3F ];
        unsigned char b = code[ ((f[0]<<4)|(f[1]>>4))&0x3F ];
        unsigned char c = code[ ((f[1]<<2)|(f[2]>>6))&0x3F ];
        unsigned char d = code[ f[2]&0x3F ];

        *dst++ = a;
        *dst++ = b;
        *dst++ = n>1 ? c : '=';
        *dst++ = n>2 ? d : '=';
    }

    return (src_len+2)/3*4;
}

bool encoding_file( const char* sourcefile, const char* destfile )
{
    FILE *src, *dst;
    {
        src = fopen( sourcefile, "rb" );
        if( !src )
            return false;
        dst = fopen( destfile, "wt" );
        if( !dst )
        {
            fclose( src );
            return false;
        }
    }

    unsigned char buf_input[3072];
    unsigned char buf_ouput[(sizeof(buf_input)+2)/3*4];
    for( size_t n; n=fread(buf_input,1,sizeof(buf_input),src), n!=0; )
    {
        encoding( buf_input, n, buf_ouput );
        fwrite( buf_ouput, 1, (n+2)/3*4, dst );

    }

    bool ret = !ferror(src) && !ferror(dst);
    fclose( dst );
    fclose( dst );
    return ret;
}

int main( void )
{
    if( !encoding_file("src.txt","dst.txt") )
    {
        puts( "文件读写失败." );
        return 1;
    }

    puts( "OK." );
    return 0;
}

2019-07-12 09:15
快速回复:改写的base64编码部分
数据加载中...
 
   



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

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