Logo Search packages:      
Sourcecode: likewise-open version File versions

char* base64_encode_data_blob ( TALLOC_CTX *  mem_ctx,
DATA_BLOB  data 
)

Encode a base64 string into a talloc()ed string caller to free.

From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments

Definition at line 2426 of file util_str.c.

{
      int bits = 0;
      int char_count = 0;
      size_t out_cnt, len, output_len;
      char *result;

        if (!data.length || !data.data)
            return NULL;

      out_cnt = 0;
      len = data.length;
      output_len = data.length * 2;
      result = TALLOC_ARRAY(mem_ctx, char, output_len); /* get us plenty of space */
      SMB_ASSERT(result != NULL);

      while (len-- && out_cnt < (data.length * 2) - 5) {
            int c = (unsigned char) *(data.data++);
            bits += c;
            char_count++;
            if (char_count == 3) {
                  result[out_cnt++] = b64[bits >> 18];
                  result[out_cnt++] = b64[(bits >> 12) & 0x3f];
                  result[out_cnt++] = b64[(bits >> 6) & 0x3f];
                  result[out_cnt++] = b64[bits & 0x3f];
                  bits = 0;
                  char_count = 0;
            } else {
                  bits <<= 8;
            }
      }
      if (char_count != 0) {
            bits <<= 16 - (8 * char_count);
            result[out_cnt++] = b64[bits >> 18];
            result[out_cnt++] = b64[(bits >> 12) & 0x3f];
            if (char_count == 1) {
                  result[out_cnt++] = '=';
                  result[out_cnt++] = '=';
            } else {
                  result[out_cnt++] = b64[(bits >> 6) & 0x3f];
                  result[out_cnt++] = '=';
            }
      }
      result[out_cnt] = '\0'; /* terminate */
      return result;
}


Generated by  Doxygen 1.6.0   Back to index