source: libabac/abac_m64.c @ a9494ad

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since a9494ad was a9494ad, checked in by Mei <mei@…>, 12 years ago

1) update credential string's storage from plain to base64 encoded

in creddy and also in yap db

2) add alice_rt1_typed example directory (complex params)

  • Property mode set to 100644
File size: 4.8 KB
Line 
1
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7static int debug=1;
8char* abac_decode_string(char *);
9
10/* encode table */
11static const char encode64url[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
13/* decode table Tables */
14/*
15static const char decode64url[]="=====rstuvwxyz========>?@ABCDEFGHIJKLMNOPQRSTUVW======XYZ[\\]^_=abcdefghijklmnopq";
16*/
17/*                                    6       14          26                   48      5557      65              81*/
18
19static const char decode64url[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
20
21/* in 3, out 4 */
22static void _encode_block( char in[3], char out[4] , int len )
23{
24    out[0] = encode64url[ in[0] >> 2 ];
25    out[1] = encode64url[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
26    out[2] = (unsigned char) (len > 1 ? encode64url[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
27    out[3] = (unsigned char) (len > 2 ? encode64url[ in[2] & 0x3f ] : '=');
28if(0) {
29    printf("len: %d\n", len);
30    printf("IN: (%c)  (%c)  (%c)\n", in[0], in[1], in[2]);
31    printf("OUT: (%c)  (%c)  (%c) (%c)\n", out[0], out[1], out[2], out[3]);
32}
33}
34
35/* in 4, out 3 */
36void _decode_block( char in[4], char out[3] )
37{   
38    out[0] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
39    out[1] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
40    out[2] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
41if(0) {
42    printf("IN: (%c)  (%c)  (%c) (%c)\n", in[0], in[1], in[2], in[3]);
43    printf("OUT: (%c)  (%c)  (%c)\n", out[0], out[1], out[2]);
44}
45}
46
47
48/*
49return a deep copy of encoded string
50*/
51char* abac_encode_string(char* string)
52{
53    char *inbufptr, *outbufptr;
54    int len=strlen(string);
55    int olen, nlen;
56    char *orig_string, *new_string;
57    int hops;
58    int i;
59
60    if(len == 0) return strdup("");
61
62    if((len/3)*3 != len) {
63        hops=(len/3)+1;
64        orig_string=malloc(sizeof(char)*olen); 
65        strcpy(orig_string,string);
66        for(i=len; i< olen; i++)
67            orig_string[i]=0;
68        } else {
69        hops=len/3;
70        orig_string=strdup(string);
71    }
72    olen=hops*3;
73    nlen=(olen*4/3 +1);
74    new_string = (char *) malloc(sizeof(char)*nlen);
75 
76    inbufptr=orig_string;
77    outbufptr=new_string;
78
79    int idx=0;
80    for(i=1; i<=hops;i++) {
81        int v=(len>(i*3))?3:(len-((i-1)*3));
82        _encode_block(inbufptr,outbufptr,v);
83        inbufptr=inbufptr+3;
84        outbufptr=outbufptr+4;
85        idx=idx+3;
86    }
87    free(orig_string);
88    new_string[nlen-1]='\0';
89
90    if(debug) {
91        char *d=abac_decode_string(new_string);
92        if(strcmp(d,string) !=0) {
93            printf("ERROR, base64 encoding NOT OKAY\n");
94            printf("orig,(%s)\n", string);
95            printf("new,(%s)\n", new_string);
96            printf("decode,(%s)\n",d);
97        } else printf(" base64 encoding OKAY\n");
98        /* free(d); */
99    }
100    return new_string;
101}
102
103char* abac_decode_string(char *string)
104{
105    char *inbufptr, *outbufptr;
106    int len=strlen(string);
107    int nlen;
108    char *new_string;
109    char *orig_string;
110    int idx=0;
111    int v=0;
112
113    if(len==0) return strdup("");
114    int hops=len/4; // ignore any that is not in the size 4 blocks
115    new_string=(char *)malloc(sizeof(char)*(hops*3+1));
116    new_string[hops*3]='\0';
117    orig_string=strdup(string);
118
119    inbufptr=orig_string;
120    outbufptr=new_string;
121    char inbuf[4];
122    int i, j;
123
124    for(i=1; i<=hops; i++) {
125        for(j=0; j<4; j++) {
126            char c=inbufptr[j];
127            if(c > 122 || c <43) {
128                c=0;
129                } else {
130                   if(0) printf("from %d, (%c)\n", c, c);
131                   c=decode64url[c-43];
132                   if(0) printf("to %d, (%c)\n", c, c);
133            }
134            if(c) { 
135                if(c=='$') c=0;
136                    else c=c-61;
137            }
138            if(c) {
139                inbufptr[j]=(c-1);
140                } else {
141                    inbufptr[j]=0;
142            }             
143        }
144        _decode_block( inbufptr, outbufptr );
145        outbufptr=outbufptr+3;
146        inbufptr=inbufptr+4;
147    }
148    free(orig_string);
149    return new_string;
150}
151
152
153/*****************
154int main( int argc, char **argv )
155{
156 
157    if(argc != 2) return 1;
158
159    FILE *infile = fopen( argv[1], "r" );
160
161    int num=1000;
162    char *string=(char *)malloc(sizeof(char)*num);
163
164    int cnt=getline(&string,&num,infile);
165    fclose(infile);
166    if(cnt==0) return 0;
167    if(string[cnt-1]=='\n')
168        string[cnt-1]='\0';
169
170    char *i=abac_encode_string(string);
171    printf("encoding into (%s)\n", i);
172    char *o=abac_decode_string(i);
173    printf("decode into (%s)\n", o);
174
175    printf(" %d:%d:%d\n", strlen(string),strlen(i), strlen(o));
176    if(strcmp(string,o)==0)
177         printf("OKAY \n");
178         else
179              printf("NOT OKAY \n");
180    free(string);
181    free(o);
182}
183****************/
Note: See TracBrowser for help on using the repository browser.