#include #include #include static int debug=1; char* abac_decode_string(char *); /* encode table */ static const char encode64url[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* decode table Tables */ /* static const char decode64url[]="=====rstuvwxyz========>?@ABCDEFGHIJKLMNOPQRSTUVW======XYZ[\\]^_=abcdefghijklmnopq"; */ /* 6 14 26 48 5557 65 81*/ static const char decode64url[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; /* in 3, out 4 */ static void _encode_block( char in[3], char out[4] , int len ) { out[0] = encode64url[ in[0] >> 2 ]; out[1] = encode64url[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ]; out[2] = (unsigned char) (len > 1 ? encode64url[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '='); out[3] = (unsigned char) (len > 2 ? encode64url[ in[2] & 0x3f ] : '='); if(0) { printf("len: %d\n", len); printf("IN: (%c) (%c) (%c)\n", in[0], in[1], in[2]); printf("OUT: (%c) (%c) (%c) (%c)\n", out[0], out[1], out[2], out[3]); } } /* in 4, out 3 */ void _decode_block( char in[4], char out[3] ) { out[0] = (unsigned char ) (in[0] << 2 | in[1] >> 4); out[1] = (unsigned char ) (in[1] << 4 | in[2] >> 2); out[2] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]); if(0) { printf("IN: (%c) (%c) (%c) (%c)\n", in[0], in[1], in[2], in[3]); printf("OUT: (%c) (%c) (%c)\n", out[0], out[1], out[2]); } } /* return a deep copy of encoded string */ char* abac_encode_string(char* string) { char *inbufptr, *outbufptr; int len=strlen(string); int olen, nlen; char *orig_string, *new_string; int hops; int i; if(len == 0) return strdup(""); if((len/3)*3 != len) { hops=(len/3)+1; orig_string=malloc(sizeof(char)*olen); strcpy(orig_string,string); for(i=len; i< olen; i++) orig_string[i]=0; } else { hops=len/3; orig_string=strdup(string); } olen=hops*3; nlen=(olen*4/3 +1); new_string = (char *) malloc(sizeof(char)*nlen); inbufptr=orig_string; outbufptr=new_string; int idx=0; for(i=1; i<=hops;i++) { int v=(len>(i*3))?3:(len-((i-1)*3)); _encode_block(inbufptr,outbufptr,v); inbufptr=inbufptr+3; outbufptr=outbufptr+4; idx=idx+3; } free(orig_string); new_string[nlen-1]='\0'; if(debug) { char *d=abac_decode_string(new_string); if(strcmp(d,string) !=0) { printf("ERROR, base64 encoding NOT OKAY\n"); printf("orig,(%s)\n", string); printf("new,(%s)\n", new_string); printf("decode,(%s)\n",d); } else printf(" base64 encoding OKAY\n"); /* free(d); */ } return new_string; } char* abac_decode_string(char *string) { char *inbufptr, *outbufptr; int len=strlen(string); int nlen; char *new_string; char *orig_string; int idx=0; int v=0; if(len==0) return strdup(""); int hops=len/4; // ignore any that is not in the size 4 blocks new_string=(char *)malloc(sizeof(char)*(hops*3+1)); new_string[hops*3]='\0'; orig_string=strdup(string); inbufptr=orig_string; outbufptr=new_string; char inbuf[4]; int i, j; for(i=1; i<=hops; i++) { for(j=0; j<4; j++) { char c=inbufptr[j]; if(c > 122 || c <43) { c=0; } else { if(0) printf("from %d, (%c)\n", c, c); c=decode64url[c-43]; if(0) printf("to %d, (%c)\n", c, c); } if(c) { if(c=='$') c=0; else c=c-61; } if(c) { inbufptr[j]=(c-1); } else { inbufptr[j]=0; } } _decode_block( inbufptr, outbufptr ); outbufptr=outbufptr+3; inbufptr=inbufptr+4; } free(orig_string); return new_string; } /***************** int main( int argc, char **argv ) { if(argc != 2) return 1; FILE *infile = fopen( argv[1], "r" ); int num=1000; char *string=(char *)malloc(sizeof(char)*num); int cnt=getline(&string,&num,infile); fclose(infile); if(cnt==0) return 0; if(string[cnt-1]=='\n') string[cnt-1]='\0'; char *i=abac_encode_string(string); printf("encoding into (%s)\n", i); char *o=abac_decode_string(i); printf("decode into (%s)\n", o); printf(" %d:%d:%d\n", strlen(string),strlen(i), strlen(o)); if(strcmp(string,o)==0) printf("OKAY \n"); else printf("NOT OKAY \n"); free(string); free(o); } ****************/