Ignore:
Timestamp:
Oct 26, 2010 11:53:28 PM (14 years ago)
Author:
bennylp
Message:

Fixed #1152 (The base64 decoder should ignore whitespaces in the input). In fact, the base64 decoder now will silently ignore/skip any bad characters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/src/pjlib-util/base64.c

    r2394 r3356  
    4848        return (63); 
    4949    else { 
    50         pj_assert(!"Should not happen as '=' should have been filtered"); 
     50        /* It *may* happen on bad input, so this is not a good idea. 
     51         * pj_assert(!"Should not happen as '=' should have been filtered"); 
     52         */ 
    5153        return INV; 
    5254    } 
     
    125127    const char *buf = input->ptr; 
    126128    int len = input->slen; 
    127     int i, j; 
    128     int c1, c2, c3, c4; 
     129    int i, j, k; 
     130    int c[4]; 
    129131 
    130132    PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL); 
     
    136138                     PJ_ETOOSMALL); 
    137139 
    138     for (i=0, j=0; i+3 < len; i+=4) { 
    139         c1 = base256_char(buf[i]); 
    140         c2 = base256_char(buf[i+1]); 
    141         c3 = base256_char(buf[i+2]); 
    142         c4 = base256_char(buf[i+3]); 
     140    for (i=0, j=0; i<len; ) { 
     141        /* Fill up c, silently ignoring invalid characters */ 
     142        for (k=0; k<4 && i<len; ++k) { 
     143            do { 
     144                c[k] = base256_char(buf[i++]); 
     145            } while (c[k]==INV && i<len); 
     146        } 
    143147 
    144         out[j++] = (pj_uint8_t)((c1<<2) | ((c2 & 0x30)>>4)); 
    145         out[j++] = (pj_uint8_t)(((c2 & 0x0F)<<4) | ((c3 & 0x3C)>>2)); 
    146         out[j++] = (pj_uint8_t)(((c3 & 0x03)<<6) | (c4 & 0x3F)); 
    147     } 
    148  
    149     if (i < len) { 
    150         c1 = base256_char(buf[i]); 
    151  
    152         if (i+1 < len) 
    153             c2 = base256_char(buf[i+1]); 
    154         else  
    155             c2 = (INV); 
    156  
    157         if (i+2 < len) 
    158             c3 = base256_char(buf[i+2]); 
    159         else 
    160             c3 = (INV); 
    161  
    162         c4 = (INV); 
    163  
    164         if (c2 != INV) { 
    165             out[j++] = (pj_uint8_t)((c1<<2) | ((c2 & 0x30)>>4)); 
    166             if (c3 != INV) { 
    167                 out[j++] = (pj_uint8_t)(((c2 & 0x0F)<<4) | ((c3 & 0x3C)>>2)); 
    168                 if (c4 != INV) { 
    169                     out[j++] = (pj_uint8_t)(((c3 & 0x03)<<6) | (c4 & 0x3F)); 
     148        if (k<4) { 
     149            if (k > 1) { 
     150                out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4)); 
     151                if (k > 2) { 
     152                    out[j++] = (pj_uint8_t) 
     153                               (((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2)); 
    170154                } 
    171155            } 
     156            break; 
    172157        } 
    173          
     158 
     159        out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4)); 
     160        out[j++] = (pj_uint8_t)(((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2)); 
     161        out[j++] = (pj_uint8_t)(((c[2] & 0x03)<<6) | (c[3] & 0x3F)); 
    174162    } 
    175163 
Note: See TracChangeset for help on using the changeset viewer.