?? base64my.pc
字號:
/* base64.c: MIME BASE64編碼與解碼器,讀標準輸入. 趙建軍, 2000.1.12.a *//* 2000.2.19.A: 增文件處理. */#include "stdio.h"#include "string.h"#include "stdlib.h"#include "ctype.h"char *sBaseStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";union{ struct{ unsigned long four:6; unsigned long three:6; unsigned long two:6; unsigned long one:6; }short_string; char new_other[3];}union_string;int decode=0;main(int argc,char **argv){ extern char *optarg; extern int optind; char temp_string[500]; char out_string[500]; int i; char cChar; decode=1; memset(&temp_string,0,sizeof(temp_string)); memset(&out_string,0,sizeof(out_string)); strcpy(temp_string,"27jrWz2sxrVbR+pnyg6jWHhgNk4sZo46"); for(i=0;i<strlen(temp_string);i++){ cChar=temp_string[i]; printf("%c,",cChar); } base64decode(temp_string,out_string); printf("out string:\n"); for(i=0;i<strlen(out_string);i++){ printf("[%c],[%d]",out_string[i],out_string[i]); } printf("\n"); exit(0);}/*編碼*/char *base64encode(char *input,char *output){ char cString; char sTemp[1000]; char sTmp[10]; int i,n; memset(&sTemp,0,sizeof(sTemp)); strcpy(sTemp,input); for(i=0;i<strlen(sTemp);i+=3) { union_string.new_other[0]=union_string.new_other[1]=union_string.new_other[2]=0; for (n=0;n<3;n++) { cString=sTemp[i+n]; if (cString==EOF) break; union_string.new_other[2-n]=cString; } if (n==0) break; switch(n) { case 1: memset(&sTmp,0,sizeof(sTmp)); sprintf(sTmp,"%c%c==",sBaseStr[union_string.short_string.one],sBaseStr[union_string.short_string.two]); strcat(sTemp,sTmp); break; case 2: memset(&sTmp,0,sizeof(sTmp)); sprintf(sTmp,"%c%c%c=",sBaseStr[union_string.short_string.one],sBaseStr[union_string.short_string.two],sBaseStr[union_string.short_string.three]); strcat(sTemp,sTmp); break; case 3: memset(&sTmp,0,sizeof(sTmp)); sprintf(sTmp,"%c%c%c%c",sBaseStr[union_string.short_string.one],sBaseStr[union_string.short_string.two],sBaseStr[union_string.short_string.three],sBaseStr[union_string.short_string.four]); strcat(sTemp,sTmp); break; } } strcpy(output,sTemp); return(output);}/*解碼*/char *base64decode(char *input,char *output){ char cStr1,cStr2,cStr3,cStr4; char sTmp[10]; char sTemp[1000]; char sReturn[1000]; int i; memset(&sTemp,0,sizeof(sTemp)); memset(&sReturn,0,sizeof(sReturn)); strcpy(sTemp,input); for(i=0;i<strlen(sTemp);i+=4){ cStr1=sTemp[i]; printf("cStr1:[%02X],cStr1:[%c]\n",cStr1,cStr1); if (cStr1==EOF||cStr1=='\n'||cStr1=='\0'){ break; } if (strchr(sBaseStr,cStr1)==NULL&&cStr1!='=') { printf("Error: %c : not BASE64 code!\n",cStr1); strcpy(output,""); return output; } cStr2=sTemp[i+1]; printf("cStr2:[%02X],cStr2:[%c]\n",cStr2,cStr2); if (cStr2==EOF) { printf("Error: unexpected EOF in BASE64 code!\n"); strcpy(output,""); return output; } if (strchr(sBaseStr,cStr2)==NULL&&cStr2!='=') { printf("Error: %c : not BASE64 code!\n",cStr2); strcpy(output,""); return output; } cStr3=sTemp[i+2]; printf("cStr3:[%02X],cStr3:[%c]\n",cStr3,cStr3); if (cStr3==EOF) { printf("Error: unexpected EOF in BASE64 code!\n"); strcpy(output,""); return output; } if (strchr(sBaseStr,cStr3)==NULL&&cStr3!='=') { printf("Error: %c : not BASE64 code!\n",cStr3); strcpy(output,""); return output; } cStr4=sTemp[i+3]; if (cStr4==EOF) { printf("Error: unexpected EOF in BASE64 code!\n"); strcpy(output,""); return output; } if (strchr(sBaseStr,cStr4)==NULL&&cStr4!='=') { printf("Error: %c : not BASE64 code!\n",cStr4); strcpy(output,""); return output; } if (strchr(sBaseStr,cStr1)&&strchr(sBaseStr,cStr2)&&(strchr(sBaseStr,cStr3)||cStr3=='=')&& ((cStr3=='='&&cStr4=='=')||(cStr3!='='&&(cStr4=='='||strchr(sBaseStr,cStr4))))) { union_string.short_string.one=strchr(sBaseStr,cStr1)-sBaseStr; union_string.short_string.two=strchr(sBaseStr,cStr2)-sBaseStr; if (cStr3=='=') { memset(&sTmp,0,sizeof(sTmp)); sprintf(sTmp,"%c",union_string.new_other[2]); strcat(sReturn,sTmp); } else if (cStr4=='=') { union_string.short_string.three=strchr(sBaseStr,cStr3)-sBaseStr; memset(&sTmp,0,sizeof(sTmp)); sprintf(sTmp,"%c%c",union_string.new_other[2],union_string.new_other[1]); strcat(sReturn,sTmp); } else { union_string.short_string.three=strchr(sBaseStr,cStr3)-sBaseStr; union_string.short_string.four=strchr(sBaseStr,cStr4)-sBaseStr; memset(&sTmp,0,sizeof(sTmp)); sprintf(sTmp,"%c%c%c",union_string.new_other[2],union_string.new_other[1],union_string.new_other[0]); strcat(sReturn,sTmp); } } else { printf("Error: %c%c%c%c : not BASE64 code!\n",cStr1,cStr2,cStr3,cStr4); strcpy(output,""); return output; } } strcpy(output,sReturn); return(output);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -