source: npl/internetserver/poptop_ppp/patches/ppp-2.4.3-winbind.base64.patch @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 2.5 KB
  • ppp-2.4.3/pppd/plugins/winbind.c

    old new  
    200200        return num_chars;
    201201}
    202202
    203 static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     203static const char *codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    204204
    205205/**
    206206 * Encode a base64 string into a malloc()ed string caller to free.
    207  *
    208  *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
    209207 **/
    210208char * base64_encode(const char *data)
    211209{
    212         int bits = 0;
    213         int char_count = 0;
    214         size_t out_cnt = 0;
    215         size_t len = strlen(data);
    216         size_t output_len = strlen(data) * 2;
    217         char *result = malloc(output_len); /* get us plenty of space */
    218 
    219         while (len-- && out_cnt < (output_len) - 5) {
    220                 int c = (unsigned char) *(data++);
    221                 bits += c;
    222                 char_count++;
    223                 if (char_count == 3) {
    224                         result[out_cnt++] = b64[bits >> 18];
    225                         result[out_cnt++] = b64[(bits >> 12) & 0x3f];
    226                         result[out_cnt++] = b64[(bits >> 6) & 0x3f];
    227             result[out_cnt++] = b64[bits & 0x3f];
    228             bits = 0;
    229             char_count = 0;
    230         } else {
    231             bits <<= 8;
    232         }
     210    int i;
     211    size_t len = strlen(data);
     212    size_t leven = 3*(len / 3);
     213    size_t output_len = 4 * ((len + 2) / 3) + 1;
     214    char *p, *result = malloc(output_len); /* get us plenty of space */
     215    const char *in;
     216    p = result;
     217    in = data;
     218    for (i = 0; i < leven; i += 3)
     219    {
     220        *p++ = codes[(in[0] >> 2) & 0x3F];
     221        *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
     222        *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
     223        *p++ = codes[in[2] & 0x3F];
     224        in += 3;
    233225    }
    234     if (char_count != 0) {
    235         bits <<= 16 - (8 * char_count);
    236         result[out_cnt++] = b64[bits >> 18];
    237         result[out_cnt++] = b64[(bits >> 12) & 0x3f];
    238         if (char_count == 1) {
    239             result[out_cnt++] = '=';
    240             result[out_cnt++] = '=';
    241         } else {
    242             result[out_cnt++] = b64[(bits >> 6) & 0x3f];
    243             result[out_cnt++] = '=';
    244         }
     226    /* Pad it if necessary...  */
     227    if (i < len)
     228    {
     229        unsigned a = in[0];
     230        unsigned b = (i+1 < len) ? in[1] : 0;
     231        *p++ = codes[(a >> 2) & 0x3F];
     232        *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
     233        *p++ = (i+1 < len) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
     234        *p++ = '=';
    245235    }
    246     result[out_cnt] = '\0';     /* terminate */
     236    /* append a NULL byte */
     237    *p = '\0';
    247238    return result;
    248239}
    249240
Note: See TracBrowser for help on using the repository browser.