deps/libcows/libcows_base64.c
char *
libcows_base64_encode(const void *data, size_t data_size, void *out_buf, size_t *out_size) {
	char *ou;
	const unsigned char *p = (const unsigned char *)data;
	const void *data_end = (const char *)data + data_size;
	size_t estimated_output_size;
	int nc = 0;

	if(data == NULL) {
		errno = EINVAL;
		return NULL;
	}

	estimated_output_size = + (4 * (data_size + 2) / 3) + 1;
	estimated_output_size += estimated_output_size / 76;	/* For \n's */
    if(!out_size || *out_size < estimated_output_size)
        return 0;

	ou = out_buf;

	while((char *)data_end - (char *)p >= 3) {
		*ou = _sf_uc_ib[ *p >> 2 ];
		ou[1] = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
		ou[2] = _sf_uc_ib[ ((p[1] & 0x0F) << 2) | (p[2] >> 6) ];
		ou[3] = _sf_uc_ib[ p[2] & 0x3F ];

		p+=3;
		ou+=4;

		nc+=4;
		if((nc % 76) == 0) *ou++='\n';
	}
	if((char *)data_end - (char *)p == 2) {
		*ou++ = _sf_uc_ib[ *p >> 2 ];
		*ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
		*ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ];
		*ou++ = '=';
	} else if((char *)data_end - (char *)p == 1) {
		*ou++ = _sf_uc_ib[ *p >> 2 ];
		*ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ];
		*ou++ = '=';
		*ou++ = '=';
	}

	*ou = '\0';

	assert(*out_size > ((void *)ou - out_buf));
	*out_size = ((void *)ou - out_buf);

	return out_buf;
}
/*
 * Encode the given data using Base64 encoding.
 */
Encode the given data using Base64 encoding.
Encode the given data using Base64 encoding .
char *
libcows_base64_encode(const void *data, size_t data_size, void *out_buf, size_t *out_size) {
	char *ou;
	const unsigned char *p = (const unsigned char *)data;
	const void *data_end = (const char *)data + data_size;
	size_t estimated_output_size;
	int nc = 0;
	if(data == NULL) {
		errno = EINVAL;
		return NULL;
	}
	estimated_output_size = + (4 * (data_size + 2) / 3) + 1;
	estimated_output_size += estimated_output_size / 76;	
    if(!out_size || *out_size < estimated_output_size)
        return 0;
	ou = out_buf;
	while((char *)data_end - (char *)p >= 3) {
		*ou = _sf_uc_ib[ *p >> 2 ];
		ou[1] = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
		ou[2] = _sf_uc_ib[ ((p[1] & 0x0F) << 2) | (p[2] >> 6) ];
		ou[3] = _sf_uc_ib[ p[2] & 0x3F ];
		p+=3;
		ou+=4;
		nc+=4;
		if((nc % 76) == 0) *ou++='\n';
	}
	if((char *)data_end - (char *)p == 2) {
		*ou++ = _sf_uc_ib[ *p >> 2 ];
		*ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
		*ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ];
		*ou++ = '=';
	} else if((char *)data_end - (char *)p == 1) {
		*ou++ = _sf_uc_ib[ *p >> 2 ];
		*ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ];
		*ou++ = '=';
		*ou++ = '=';
	}
	*ou = '\0';
	assert(*out_size > ((void *)ou - out_buf));
	*out_size = ((void *)ou - out_buf);
	return out_buf;
}
char * libcows_base64_encode ( const void * data , size_t data_size , void * out_buf , size_t * out_size ) { char * ou ; const unsigned char * p = ( const unsigned char * ) data ; const void * data_end = ( const char * ) data + data_size ; size_t estimated_output_size ; int nc = 0 ; if ( data == NULL ) { errno = EINVAL ; return NULL ; } estimated_output_size = + ( 4 * ( data_size + 2 ) / 3 ) + 1 ; estimated_output_size += estimated_output_size / 76 ; if ( ! out_size || * out_size < estimated_output_size ) return 0 ; ou = out_buf ; while ( ( char * ) data_end - ( char * ) p >= 3 ) { * ou = _sf_uc_ib [ * p >> 2 ] ; ou [ 1 ] = _sf_uc_ib [ ( ( * p & 0x03 ) << 4 ) | ( p [ 1 ] >> 4 ) ] ; ou [ 2 ] = _sf_uc_ib [ ( ( p [ 1 ] & 0x0F ) << 2 ) | ( p [ 2 ] >> 6 ) ] ; ou [ 3 ] = _sf_uc_ib [ p [ 2 ] & 0x3F ] ; p += 3 ; ou += 4 ; nc += 4 ; if ( ( nc % 76 ) == 0 ) * ou ++ = ' \n ' ; } if ( ( char * ) data_end - ( char * ) p == 2 ) { * ou ++ = _sf_uc_ib [ * p >> 2 ] ; * ou ++ = _sf_uc_ib [ ( ( * p & 0x03 ) << 4 ) | ( p [ 1 ] >> 4 ) ] ; * ou ++ = _sf_uc_ib [ ( ( p [ 1 ] & 0x0F ) << 2 ) ] ; * ou ++ = ' ' ; } else if ( ( char * ) data_end - ( char * ) p == 1 ) { * ou ++ = _sf_uc_ib [ * p >> 2 ] ; * ou ++ = _sf_uc_ib [ ( ( * p & 0x03 ) << 4 ) ] ; * ou ++ = ' ' ; * ou ++ = ' ' ; } * ou = ' \0 ' ; assert ( * out_size > ( ( void * ) ou - out_buf ) ) ; * out_size = ( ( void * ) ou - out_buf ) ; return out_buf ; }
Encode the given data using Base64 encoding.
Encode the given data using Base64 encoding .
/* For \n's */