/[monit]/monit/http/base64.c
ViewVC logotype

Diff of /monit/http/base64.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by chopp, Wed Sep 3 15:29:29 2003 UTC revision 1.9 by martinp, Fri Sep 19 08:16:14 2003 UTC
# Line 57  static unsigned char decode(char c); Line 57  static unsigned char decode(char c);
57    
58    
59  /**  /**
60   * Base64 encode size data in 'src', into the allocated string pointed   * Base64 encode and return size data in 'src'. The caller must free the
61   * to by 'dest'. The caller must free the dest string.   * returned string.
  * @param dest a pointer to an allocated area with the base64  
  * encode data  
62   * @param size The size of the data in src   * @param size The size of the data in src
63   * @param src The data to be base64 encode   * @param src The data to be base64 encode
64   * @return TRUE (the length of the encoded string) if   * @return encoded string otherwise NULL
  * encode succeeded otherwise FALSE.  
65   */   */
66  int encode_base64(char **dest, int size, unsigned char *src) {  char *encode_base64(int size, unsigned char *src) {
67    
68    if(src ) {    int i;
69      char *out, *p;
70    
71      if(!src)
72        return NULL;
73    
74      if(!size)
75        size= strlen((char *)src);
76            
77      int i;    out= xcalloc(sizeof(char *), size*4/3+4);
78      char *out, *p;  
79      p= out;
80            
81      if(0 == size ) {    for(i=0; i<size; i+=3) {
82                
83        size= strlen((char *) src);      unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;
84                
85      }      b1 = src[i];
       
     out= xmalloc(size*4/3+4);  
       
     memset(out, 0, (size*4/3+4));  
   
     p= out;  
       
     for(i=0; i<size; i+=3) {  
86                
87        unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;      if(i+1<size)
88          b2 = src[i+1];
89                
90        b1 = src[i];      if(i+2<size)
91          b3 = src[i+2];
92                
93        if(i+1<size) {      b4= b1>>2;
94                b5= ((b1&0x3)<<4)|(b2>>4);
95          b2 = src[i+1];      b6= ((b2&0xf)<<2)|(b3>>6);
96                b7= b3&0x3f;
97        }        
98              *p++= encode(b4);
99        if(i+2<size) {      *p++= encode(b5);
100                  
101          b3 = src[i+2];      if(i+1<size) {
102                  *p++= encode(b6);
103        }      } else {
104                *p++= '=';
105        b4= b1>>2;      }
106        b5= ((b1&0x3)<<4)|(b2>>4);        
107        b6= ((b2&0xf)<<2)|(b3>>6);      if(i+2<size) {
108        b7= b3&0x3f;        *p++= encode(b7);
109              } else {
110                *p++= '=';
       *p++= encode(b4);  
       *p++= encode(b5);  
         
       if(i+1<size) {  
           
         *p++= encode(b6);  
           
       } else {  
           
         *p++= '=';  
           
       }  
         
       if(i+2<size) {  
           
         *p++= encode(b7);  
           
       } else {  
           
         *p++= '=';  
           
       }  
   
111      }      }
   
     *dest= out;  
       
     return((p-out));  
112    
113    }    }
114      
115    return FALSE;    return out;
116      
117  }  }
118    
119    

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26