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

char* ldb_base64_encode ( void *  mem_ctx,
const char *  buf,
int  len 
)

Base64 encode a buffer

Parameters:
mem_ctx the memory context that the result is allocated from.
buf pointer to the array that is to be encoded
len the number of elements in the array to be encoded
Returns:
pointer to an array containing the encoded data
Note:
The caller is responsible for freeing the result

Definition at line 154 of file ldb_ldif.c.

{
      const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      int bit_offset, byte_offset, idx, i;
      const uint8_t *d = (const uint8_t *)buf;
      int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
      char *out;

      out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
      if (!out) return NULL;

      for (i=0;i<bytes;i++) {
            byte_offset = (i*6)/8;
            bit_offset = (i*6)%8;
            if (bit_offset < 3) {
                  idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
            } else {
                  idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
                  if (byte_offset+1 < len) {
                        idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
                  }
            }
            out[i] = b64[idx];
      }

      for (;i<bytes+pad_bytes;i++)
            out[i] = '=';
      out[i] = 0;

      return out;
}


Generated by  Doxygen 1.6.0   Back to index