C base64 編碼檔案

來源:互聯網
上載者:User

功能:讀取二進位檔案,轉化為BASE64編碼字串,檔案會增大1/3,詳見BASE64編碼原理,這樣就可以在文章中儲存檔案了,沒辦法,公司不許上傳附件,POST也只能一次6KB,之前還寫了一個類比HTTP發送的程式(http://www.cnblogs.com/yangyh/archive/2010/07/21/1781845.html),實為CNBLOG的小組自動認可程式,不過現在CNBLOG添加了防ROBOT功能了,一次只能發32帖了

 

#include "stdafx.h"#include "stdio.h"#include "string.h"#include "stdlib.h"#define  MAXLEN 1024#include <iostream>using namespace std;long int encode(  char *src,long int src_len, char *dst);long int decode(char *src, long int src_len, char *dst);long int getFileLength(const char *filePath){FILE *fp;fp = fopen(filePath, "rb");if (!fp) {printf("Could not load  file '%s'.  Exiting.\n", filePath);exit(1);}fseek(fp, 0, SEEK_END);long int ret =  ftell(fp);fclose(fp);return ret;}long int getFileStr(const char *filePath, char*);long int getFileStr(const char *filePath, char * back) {FILE *fp;int result;string buff;char temp;fp = fopen(filePath, "rb");if (!fp) {printf("Could not load  file '%s'.  Exiting.\n", filePath);exit(1);}fseek(fp, 0, SEEK_END);long int fileLength = ftell(fp);fseek(fp, 0, SEEK_SET);printf("file size:%ld\n",fileLength);long int j = 0;while((result = fread(&temp, 1, 1, fp))==1){back[j++]=temp;//printf("%d",temp);}if(j!=fileLength){printf("read file error");exit(-1);}back[j]=0;fclose(fp);//printf("length:%d\n",strlen(back));return fileLength;}void Fill0(char t[]){        if(strlen(t)<2)    {        t[1]=t[0];        t[0]='0';        t[2]=0;    }}int   binaryToString(char *from,char *to)   {   FILE *out;char *dest;long int len = getFileLength(from);out=fopen(to,"wb");if(!out){printf("error write %s",to);exit(-1);}dest = new  char[len+1];if(!dest){printf("malloc error :%ld",len+1);exit(-1);}char * ret= new char[len*2];if(!ret){printf("malloc error :%ld",len*2);exit(-1);}getFileStr(from,dest);//printf("read:%ld\n",dest[1]);long int newlen =  encode(dest,len,ret);//  puts(ret);fwrite(ret,newlen,1,out);fclose(out);free(dest);free(ret);return   0;   }  long int encode( char *src,long int src_len, char *dst){long int i = 0, j = 0;//printf("%ld\n",src[2]);char base64_map[65] = "BADCFEGHIJKLMNOPQRSTUVWXYZbadcfeghijklmnopqrstuvwxyz0123456789+/";for (; i < src_len - src_len % 3; i += 3) {//printf("%ld\n",(unsigned char)src[i]);//getchar();dst[j++] = base64_map[(src[i] >> 2) & 0x3F];// dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];// dst[j++] = base64_map[((src[i + 1] << 2) & 0x3C) + ((src[i + 2] >> 6) & 0x3)];// dst[j++] = base64_map[src[i + 2] & 0x3F];//printf("%d",dst[j]);}if (src_len % 3 == 1) {dst[j++] = base64_map[(src[i] >> 2) & 0x3F];dst[j++] = base64_map[(src[i] << 4) & 0x30];dst[j++] = '=';dst[j++] = '=';}else if (src_len % 3 == 2) {dst[j++] = base64_map[(src[i] >> 2) & 0x3F];dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];dst[j++] = base64_map[(src[i + 1] << 2) & 0x3C];dst[j++] = '=';}dst[j] = '\0';//puts(dst);printf("newlength:%ld\n",j);return j;} int stringToBinary(char *from,char *to){FILE *out;char *dest;long int len = getFileLength(from);out=fopen(to,"wb");if(!out){printf("error write %s",to);exit(-1);}dest = new  char[len+1];if(!dest){printf("malloc error :%ld",len+1);exit(-1);}char * ret= new char[len*2];if(!ret){printf("malloc error :%ld",len*2);exit(-1);}getFileStr(from,dest);//printf("read:%ld\n",dest[1]);long int newlen =  decode(dest,len,ret);//  puts(ret);fwrite(ret,newlen-1,1,out);fclose(out);free(dest);free(ret);return   0;   }void main(int argc,char* argv[]){    //binaryToString("D:\\Users\\Administrator\\Documents\\VC6 WorkSpace\\base64\\Debug\\2.txt","D:\\Users\\Administrator\\Documents\\VC6 WorkSpace\\base64\\Debug\\2.rar");//stringToBinary("D:\\Users\\Administrator\\Documents\\VC6 WorkSpace\\base64\\Debug\\2.rar","D:\\Users\\Administrator\\Documents\\VC6 WorkSpace\\base64\\Debug\\2.1.txt");//printf("%d",'a');    if(argc!=4){printf("cmd -tos|tob <srcFile> <destFile>\n");getchar();return ;}if(strcmp(argv[1],"tos")==0){binaryToString(argv[2],argv[3]);}else if(strcmp(argv[1],"tob")==0){stringToBinary(argv[2],argv[3]);}}long int decode(char *src, long int src_len, char *dst){longint i = 0, j = 0;unsigned char base64_decode_map[256] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,255, 0, 255, 255, 255, 1, 0, 3, 2, 5, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14,15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 27, 26, 29,28, 31, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};        for (; i < src_len; i += 4) {dst[j++] = base64_decode_map[src[i]] << 2 |base64_decode_map[src[i + 1]] >> 4;dst[j++] = base64_decode_map[src[i + 1]] << 4 |base64_decode_map[src[i + 2]] >> 2;dst[j++] = base64_decode_map[src[i + 2]] << 6 |base64_decode_map[src[i + 3]];        }        dst[j] = '\0';printf("decode length :%ld\n",j);return j;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.