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

📄 base64.c

📁 linux下面的Base64编码源代码
💻 C
字号:
/* 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	*base="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";union{ struct {  unsigned long d:6;  unsigned long c:6;  unsigned long b:6;  unsigned long a:6; }s; unsigned char	c[3];}u;int	decode=0;main(int argc,char **argv){ extern char	*optarg; extern int	optind; int		c; FILE		*fp; while ((c=getopt(argc,argv,"u"))!=EOF) {  switch(c)  {   case 'u': decode=1; break;   default: usage(); break;  } } if (argc==optind) {  if (isatty(0)) usage();  if (decode) base64decode(stdin,stdout); else base64encode(stdin,stdout); } else for (;optind<argc;optind++) {  fp=fopen(argv[optind],"r");  if (fp==0) { fprintf(stderr,"%s: fopen %s error.\n",argv[0],argv[optind]); continue; }  if (decode) base64decode(fp,stdout); else base64encode(fp,stdout);  fclose(fp); } exit(0);}usage(){ fprintf(stderr,"base64: MIME BASE64 encoder and decoder, by zzz (zhsoft@371.net) at 2000.1.11.m\n"); fprintf(stderr,"Usage: base64 [-u] [file1] [file2] ...\n"); exit(1);}base64encode(FILE *fp,FILE *fq){ int	ch,n,h=0,flag=0; while (1) {  u.c[0]=u.c[1]=u.c[2]=0;  for (n=0;n<3;n++)  {   ch=fgetc(fp);   if (ch==EOF) break;   u.c[2-n]=ch;  }  if (n==0) break;  flag=1;  switch(n)  {   case 1: fprintf(fq,"%c%c==",base[u.s.a],base[u.s.b]);break;   case 2: fprintf(fq,"%c%c%c=",base[u.s.a],base[u.s.b],base[u.s.c]);break;   case 3: fprintf(fq,"%c%c%c%c",base[u.s.a],base[u.s.b],base[u.s.c],base[u.s.d]);break;  }  if (h==15) { h=0; fprintf(fq,"\n"); } else h++; } if (flag&&h) fprintf(fq,"\n");}base64decode(FILE *fp,FILE *fq){ int	c1,c2,c3,c4; while (1) {  /* c1 */  c1=fgetc(fp);  while (c1!=EOF&&isspace(c1)) c1=fgetc(fp);  if (c1==EOF) break;  if (strchr(base,c1)==NULL&&c1!='=')  {   fprintf(fq,"Error: %c : not BASE64 code!\n",c1);   return 1;  }  /* c2 */  c2=fgetc(fp);  if (c2==EOF)  {   fprintf(fq,"Error: unexpected EOF in BASE64 code!\n");   return 1;  }  if (strchr(base,c2)==NULL&&c2!='=')  {   fprintf(fq,"Error: %c : not BASE64 code!\n",c2);   return 1;  }  /* c3 */  c3=fgetc(fp);  if (c3==EOF)  {   fprintf(fq,"Error: unexpected EOF in BASE64 code!\n");   return 1;  }  if (strchr(base,c3)==NULL&&c3!='=')  {   fprintf(fq,"Error: %c : not BASE64 code!\n",c3);   return 1;  }  /* c4 */  c4=fgetc(fp);  if (c4==EOF)  {   fprintf(fq,"Error: unexpected EOF in BASE64 code!\n");   return 1;  }  if (strchr(base,c4)==NULL&&c4!='=')  {   fprintf(fq,"Error: %c : not BASE64 code!\n",c4);   return 1;  }  /* check format:[A-Za-z0-9+/][A-Za-z0-9+/][A-Za-z0-9+/=][A-Za-z0-9+/=] */  if (strchr(base,c1)&&strchr(base,c2)&&(strchr(base,c3)||c3=='=')&&  	((c3=='='&&c4=='=')||(c3!='='&&(c4=='='||strchr(base,c4)))))  {   u.s.a=strchr(base,c1)-base;   u.s.b=strchr(base,c2)-base;   if (c3=='=')   {    /* one byte output */    fprintf(fq,"%c",u.c[2]);   }   else   if (c4=='=')   {    /* two byte output */    u.s.c=strchr(base,c3)-base;    fprintf(fq,"%c%c",u.c[2],u.c[1]);   }   else   {    /* three byte output */    u.s.c=strchr(base,c3)-base;    u.s.d=strchr(base,c4)-base;    fprintf(fq,"%c%c%c",u.c[2],u.c[1],u.c[0]);   }  }  else  {   fprintf(fq,"Error: %c%c%c%c : not BASE64 code!\n",c1,c2,c3,c4);   return 1;  } } return 0;}

⌨️ 快捷键说明

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