#include #include #include #include #include #include "tlmpmail.h" #include "tlmpmail.m" static int base64_doenc (FILE *fin, FILE *fout) { int ret = -1; int size = 1000000; char *buf = (char*)malloc(size); int sofar = 0; int len; while ((len = fread(buf+sofar,1,size-sofar,fin))>0){ sofar += len; if (sofar == size){ size += 1000000; if (size >= 30*1024*1024){ tlmp_error (MSG_U(E_TOOBIG,"Input too big, max 30megs\n")); exit (-1); } buf = (char*)realloc(buf,size); } } char *tmp = base64_encode (buf,sofar); if (tmp != NULL){ fputs (tmp,fout); ret = 0; free (tmp); } free (buf); return ret; } int base64 (int argc, char *argv[]) { glocal bool encode = false; static const char *tbdic[]={"tlmpwork","tlmpsql","bolixo",NULL}; int ret = (argc,argv,tbdic); setproginfo ("base64",TLMPWORK_VERSION,TLMPWORK_RELEASE ,MSG_U(I_BASE64 ,"Encode and decode base64 data\n" "\n" "base64 [ options ] [ input-file [ output-file]]\n" )); setarg ('e',"encode",MSG_U(I_ENCODE,"Turn on encoding (decode by default)"),glocal.encode,false); int ret = -1; glocal FILE *fout = stdout; if (glocal.encode){ ret = base64_doenc (stdin,stdout); }else{ char src[200]; while (fgets(src,sizeof(src)-1,stdin)!=NULL){ char buf[100]; int len = base64_decode(buf, sizeof(buf)-1, src); fwrite (buf,len,1,glocal.fout); } ret = 0; } return ret; glocal int ret = -1; if (argc == 1 || argc == 2){ glocal FILE *fout = argc == 2 ? fopen (argv[1],"w") : stdout; if (glocal.encode){ FILE *fin = fopen (argv[0],"r"); if (fin != NULL){ glocal.ret = base64_doenc (fin,glocal.fout); }else{ tlmp_error (MSG_U(E_OPEN,"Can't open file %s (%s)\n") ,argv[0],strerror(errno)); } }else{ glocal.ret = (argv[0],true); char buf[100]; int len = base64_decode(buf, sizeof(buf)-1, line); fwrite (buf,len,1,glocal.fout); return 0; } if (glocal.fout != stdout) fclose (glocal.fout); }else{ usage(); } return glocal.ret; return ret; }