Logo Search packages:      
Sourcecode: ldb version File versions  Download package

int ldb_base64_decode ( char *  s  ) 

Base64 decode a buffer

This function decodes a base64 encoded string in place.

Parameters:
s the string to decode.
Returns:
the length of the returned (decoded) string.
Note:
the string is null terminated, but the null terminator is not included in the length.

Definition at line 108 of file ldb_ldif.c.

{
      const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      int bit_offset=0, byte_offset, idx, i, n;
      uint8_t *d = (uint8_t *)s;
      char *p=NULL;

      n=i=0;

      while (*s && (p=strchr(b64,*s))) {
            idx = (int)(p - b64);
            byte_offset = (i*6)/8;
            bit_offset = (i*6)%8;
            d[byte_offset] &= ~((1<<(8-bit_offset))-1);
            if (bit_offset < 3) {
                  d[byte_offset] |= (idx << (2-bit_offset));
                  n = byte_offset+1;
            } else {
                  d[byte_offset] |= (idx >> (bit_offset-2));
                  d[byte_offset+1] = 0;
                  d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
                  n = byte_offset+2;
            }
            s++; i++;
      }
      if (bit_offset >= 3) {
            n--;
      }

      if (*s && !p) {
            /* the only termination allowed */
            if (*s != '=') {
                  return -1;
            }
      }

      /* null terminate */
      d[n] = 0;
      return n;
}


Generated by  Doxygen 1.6.0   Back to index