Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <math.h>
- #include <string.h>
- #include <inttypes.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- const char *encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- //----------------------------------------------------------------------------------
- unsigned char pos_base64(char c)//Retorna posicion en Base64
- {
- int pos=0;
- const char *ref=encoding;
- while (c !=*encoding ){
- //printf ("Busca=%c=%d Compara=%c \n",c,pos,*encoding );
- encoding++;
- pos++;
- }
- encoding=ref;
- return pos;
- }
- //----------------------------------------------------------------------------------
- int sizeFile (FILE *source)
- {
- int size=0;
- unsigned char c=1;
- while (c!=(unsigned char)EOF){
- c=fgetc(source);
- size++;
- };
- size-=1;
- rewind (source);
- return size;
- }
- int to_base64(const char *dst_file, const char *src_file)
- {
- //const char *encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- char b24[4];
- unsigned char c=1;
- int size=0;
- int sizeOrg=0;
- int columna=0;
- //Abre ficheros
- FILE *src,*dst;
- src=fopen(src_file,"r");
- if (!src){
- fprintf (stderr," A ) Fallo apertura fichero fuente %s !!!",src_file);
- return -1;
- }else printf ("OK apertura %s\n",src_file);
- dst=fopen(dst_file,"w+");
- if (!dst){
- fprintf (stderr,"B ) Fallo apertura fichero destino %s !!!",dst_file);
- return -1;
- }else printf ("OK apertura %s\n",dst_file);
- //Analisis longitud
- while (c!=(unsigned char)EOF){
- c=fgetc(src);
- // printf ("%c",c);
- size++;
- };
- size-=1;
- sizeOrg=size;
- rewind (src);
- printf ("Size original = %d\n",size);
- while (size%3){size++;}//rectificador de tamano
- printf ("Size rectificado = %d\n",size);
- //Dimensiona la copia
- char * copia= (char*)calloc( (size+4) , sizeof ( char ) );
- char * copiaRef=copia;//direccion referencia
- int *org=(int*)copia;//32 bits;
- char *org2=copia;
- copiaRef=copia;
- c=1;
- //Efectua la copia
- while (c!=(unsigned char)EOF){
- c=fgetc(src);
- //printf ("Copia 0x%x %c\n",c,c);
- if (c<=254){*copia=c;}
- copia++;
- };
- *copia=EOF;//EOF
- copia=copiaRef;
- org= (int*)copiaRef;
- org2=copiaRef;
- for (int i=0 ;i<size;i+=3){
- b24[0]= encoding [(int) ((*org) & (0x000000FC))>>2];
- b24[1]= encoding [ (((int) ((*org) & (0x00000003)))<<4 ) | (int) ((*org) & (0x0000FF00))>>(8+4) ];
- b24[2]= encoding [ (((int) ((*org) & (0x00000F00))) >>(4+2) ) | (int) ((*org) & (0x00C00000))>>(16+6) ];
- b24[3]= encoding [ (((int) ((*org) & (0x003F0000))) >>(16) )];
- printf ("%c", b24[0]);fprintf(dst,"%c",b24[0] );
- if (i+1 >sizeOrg ){b24[1]='=';}
- printf ("%c", b24[1]);fprintf(dst,"%c",b24[1] );
- if (i+2 >sizeOrg ){b24[2]='=';}
- printf ("%c", b24[2]);fprintf(dst,"%c",b24[2] );
- if (i+3 >sizeOrg ){b24[3]='=';}
- printf ("%c", b24[3]);fprintf(dst,"%c",b24[3] );
- columna+=4;
- if (columna>=64){fprintf (dst,"%c",0x0a);columna=0;}
- //int fputc(int char, FILE *stream)
- org2+=3;
- org= (int*)org2;
- }
- printf ("\n\n");
- free (copia);
- fclose(src);
- fclose (dst);
- return sizeOrg;
- }
- int from_base64(const char *dst_file, const char *src_file)
- {
- int sizeOrg=0;
- unsigned char c=1;
- unsigned char buffer;
- FILE *src,*dst;
- src=fopen(src_file,"r");
- //Abre ficheros
- if (!src){
- fprintf (stderr," A ) Fallo apertura fichero fuente %s !!!",src_file);
- return -1;
- }else printf ("OK apertura %s\n",src_file);
- dst=fopen(dst_file,"w+");
- if (!dst){
- fprintf (stderr,"B ) Fallo apertura fichero destino %s !!!",dst_file);
- return -1;
- }else printf ("OK apertura %s\n",dst_file);
- sizeOrg=sizeFile (src);//Analiza tamano
- //----------------------------------------------------------------------------------
- // copia
- char * copia= (char*)calloc( (sizeOrg) , sizeof ( char ) );
- char * copiaRef=copia;//direccion referencia
- //----------------------------------------------------------------------------------
- //Efectua la copia
- while (c!=(unsigned char)EOF){
- c=fgetc(src);
- printf ("Copia 0x%x %c\n",c,c);
- *copia=c;
- copia++;
- };
- // *copia=EOF;//EOF
- if (*(copia)=='='){--sizeOrg;printf ("A rectifica size=%d\n",sizeOrg);}
- if (*(--copia)=='='){--sizeOrg;printf ("B rectifica size=%d\n",sizeOrg);}
- copia=copiaRef;
- //----------------------------------------------------------------------------------
- // Transformacion de Base64 a Equivalente por posicion 0 es A 1 22 es w
- for (int i=0;i<sizeOrg;i++){
- *copia = (pos_base64(*copia));//Retorna posicion en Base64
- //if (*copia==64 && i>(sizeOrg-4)){*copia=0;}
- ++copia;
- }
- copia=copiaRef;
- //----------------------------------------------------------------------------------
- printf ("NUevo tamano decodificado =%d\n",(sizeOrg-(sizeOrg/4)));
- #define MAX (sizeOrg-(sizeOrg/4))
- for (int i=0;i<(MAX);i+=3){
- char B0=(*(copia+0));
- char B1=(*(copia+1));
- char B2=(*(copia+2));
- char B3=(*(copia+3));
- //Tratamiento 1
- buffer=( (B0 <<2) | ((B1 & 0x30)>>4) );
- printf ("%c", buffer);
- //if (buffer && buffer<127)
- {fprintf(dst,"%c",buffer );}
- B1=(*(copia+1));
- //Tratamiento 2
- buffer = ((B1 & 0x0F)<<4 | (B2 )>>2 );
- printf ("%c", buffer);
- //if (buffer && buffer<127)
- {fprintf(dst,"%c",buffer );}
- B2=*(copia+2);
- //Tratamiento 3
- buffer=( (B2 & 0x03) <<6 | (B3 &0x3F) );
- printf ("%c", buffer);
- //if (buffer && buffer<127)
- {fprintf(dst,"%c",buffer );}
- copia+=4;
- printf ("pos=%d\n",i);
- }
- //fprintf (dst,"%c",0);
- //----------------------------------------------------------------------------------
- fclose (dst);
- fclose (src);
- return sizeOrg;
- }
- int main()
- {
- //char f []={"any carnal pleasure."};// YW55IGNhcm5hbCBwbGVhc3VyZS4= padding1
- //printf ("N° letras %d\n",to_base64("copia.txt","oneline.txt"));//OK
- // printf ("N° letras %d\n",to_base64("copia.txt","testi.txt"));//OK
- printf ("taille original =%d \n", from_base64("decodificado.txt","simple.bin.ref"));
- return 0;
- }
- /*
- This file has text only on one line. Wow. VGhpcyBmaWxlIGhhcyB0ZXh0IG9ubHkgb24gb25lIGxpbmUuIFdvdy4=
- La entrada termina con: any carnal pleasure. La salida termina con: YW55IGNhcm5hbCBwbGVhc3VyZS4=
- La entrada termina con: any carnal pleasure La salida termina con: YW55IGNhcm5hbCBwbGVhc3VyZQ==
- La entrada termina con: any carnal pleasur La salida termina con: YW55IGNhcm5hbCBwbGVhc3Vy
- La entrada termina con: any carnal pleasu La salida termina con: YW55IGNhcm5hbCBwbGVhc3U=
- La entrada termina con: any carnal pleas La salida termina con: YW55IGNhcm5hbCBwbGVhcw==
- */
- //Dennis Ritchie a dit un jour :
- //"La meilleure façon d'apprendre à programmer, c'est de programmer."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement