src/gldns/parseutil.c
static int gldns_b64_ntop_base(uint8_t const *src, size_t srclength,
	char *target, size_t targsize, int base64url, int padding)
{
	char* b64;
	const char pad64 = '=';
	size_t i = 0, o = 0;
	if(base64url)
		b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
			"456789-_";
	else
		b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
			"456789+/";
	if(targsize < gldns_b64_ntop_calculate_size(srclength))
		return -1;
	/* whole chunks: xxxxxxyy yyyyzzzz zzwwwwww */
	while(i+3 <= srclength) {
		if(o+4 > targsize) return -1;
		target[o] = b64[src[i] >> 2];
		target[o+1] = b64[ ((src[i]&0x03)<<4) | (src[i+1]>>4) ];
		target[o+2] = b64[ ((src[i+1]&0x0f)<<2) | (src[i+2]>>6) ];
		target[o+3] = b64[ (src[i+2]&0x3f) ];
		i += 3;
		o += 4;
	}
	/* remainder */
	switch(srclength - i) {
	case 2:
		/* two at end, converted into A B C = */
		target[o] = b64[src[i] >> 2];
		target[o+1] = b64[ ((src[i]&0x03)<<4) | (src[i+1]>>4) ];
		target[o+2] = b64[ ((src[i+1]&0x0f)<<2) ];
		if(padding) {
			target[o+3] = pad64;
			/* i += 2; */
			o += 4;
		} else {
			o += 3;
		}
		break;
	case 1:
		/* one at end, converted into A B = = */
		target[o] = b64[src[i] >> 2];
		target[o+1] = b64[ ((src[i]&0x03)<<4) ];
		if(padding) {
			target[o+2] = pad64;
			target[o+3] = pad64;
			/* i += 1; */
			o += 4;
		} else {
			o += 2;
		}
		break;
	case 0:
	default:
		/* nothing */
		break;
	}
	/* assert: i == srclength */
	if(o+1 > targsize) return -1;
	target[o] = 0;
	return (int)o;
}
/* RFC 1521, section 5.2.
 *
 * The encoding process represents 24-bit groups of input bits as output
 * strings of 4 encoded characters. Proceeding from left to right, a
 * 24-bit input group is formed by concatenating 3 8-bit input groups.
 * These 24 bits are then treated as 4 concatenated 6-bit groups, each
 * of which is translated into a single digit in the base64 alphabet.
 *
 * This routine does not insert spaces or linebreaks after 76 characters.
 */


This routine does not insert spaces or linebreaks after 76 characters.
This routine does not insert spaces or linebreaks after 76 characters .
static int gldns_b64_ntop_base(uint8_t const *src, size_t srclength,
	char *target, size_t targsize, int base64url, int padding)
{
	char* b64;
	const char pad64 = '=';
	size_t i = 0, o = 0;
	if(base64url)
		b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
			"456789-_";
	else
		b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
			"456789+/";
	if(targsize < gldns_b64_ntop_calculate_size(srclength))
		return -1;
	while(i+3 <= srclength) {
		if(o+4 > targsize) return -1;
		target[o] = b64[src[i] >> 2];
		target[o+1] = b64[ ((src[i]&0x03)<<4) | (src[i+1]>>4) ];
		target[o+2] = b64[ ((src[i+1]&0x0f)<<2) | (src[i+2]>>6) ];
		target[o+3] = b64[ (src[i+2]&0x3f) ];
		i += 3;
		o += 4;
	}
	switch(srclength - i) {
	case 2:
		target[o] = b64[src[i] >> 2];
		target[o+1] = b64[ ((src[i]&0x03)<<4) | (src[i+1]>>4) ];
		target[o+2] = b64[ ((src[i+1]&0x0f)<<2) ];
		if(padding) {
			target[o+3] = pad64;
			o += 4;
		} else {
			o += 3;
		}
		break;
	case 1:
		target[o] = b64[src[i] >> 2];
		target[o+1] = b64[ ((src[i]&0x03)<<4) ];
		if(padding) {
			target[o+2] = pad64;
			target[o+3] = pad64;
			o += 4;
		} else {
			o += 2;
		}
		break;
	case 0:
	default:
		break;
	}
	if(o+1 > targsize) return -1;
	target[o] = 0;
	return (int)o;
}
static int gldns_b64_ntop_base ( uint8_t const * src , size_t srclength , char * target , size_t targsize , int base64url , int padding ) { char * b64 ; const char pad64 = ' ' ; size_t i = 0 , o = 0 ; if ( base64url ) b64 = " " " " ; else b64 = " " " " ; if ( targsize < gldns_b64_ntop_calculate_size ( srclength ) ) return -1 ; while ( i + 3 <= srclength ) { if ( o + 4 > targsize ) return -1 ; target [ o ] = b64 [ src [ i ] >> 2 ] ; target [ o + 1 ] = b64 [ ( ( src [ i ] & 0x03 ) << 4 ) | ( src [ i + 1 ] >> 4 ) ] ; target [ o + 2 ] = b64 [ ( ( src [ i + 1 ] & 0x0f ) << 2 ) | ( src [ i + 2 ] >> 6 ) ] ; target [ o + 3 ] = b64 [ ( src [ i + 2 ] & 0x3f ) ] ; i += 3 ; o += 4 ; } switch ( srclength - i ) { case 2 : target [ o ] = b64 [ src [ i ] >> 2 ] ; target [ o + 1 ] = b64 [ ( ( src [ i ] & 0x03 ) << 4 ) | ( src [ i + 1 ] >> 4 ) ] ; target [ o + 2 ] = b64 [ ( ( src [ i + 1 ] & 0x0f ) << 2 ) ] ; if ( padding ) { target [ o + 3 ] = pad64 ; o += 4 ; } else { o += 3 ; } break ; case 1 : target [ o ] = b64 [ src [ i ] >> 2 ] ; target [ o + 1 ] = b64 [ ( ( src [ i ] & 0x03 ) << 4 ) ] ; if ( padding ) { target [ o + 2 ] = pad64 ; target [ o + 3 ] = pad64 ; o += 4 ; } else { o += 2 ; } break ; case 0 : default : break ; } if ( o + 1 > targsize ) return -1 ; target [ o ] = 0 ; return ( int ) o ; }
RFC 1521, section 5.2.
RFC 1521 section 5 . 2 .
/* whole chunks: xxxxxxyy yyyyzzzz zzwwwwww */ /* remainder */ /* two at end, converted into A B C = */ /* i += 2; */ /* one at end, converted into A B = = */ /* i += 1; */ /* nothing */ /* assert: i == srclength */