src/gldns/parseutil.c
static int gldns_b64_pton_base(char const *src, size_t srcsize, uint8_t *target,
	size_t targsize, int base64url)
{
	const uint8_t pad64 = 64; /* is 64th in the b64 array */
	const char* s = src;
	uint8_t in[4];
	size_t o = 0, incount = 0;
	int check_padding = (srcsize) ? 0 : 1;

	while(*s && (check_padding || srcsize)) {
		/* skip any character that is not base64 */
		/* conceptually we do:
		const char* b64 =      pad'=' is appended to array
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		const char* d = strchr(b64, *s++);
		and use d-b64;
		*/
		char d = *s++;
		srcsize--;
		if(d <= 'Z' && d >= 'A')
			d -= 'A';
		else if(d <= 'z' && d >= 'a')
			d = d - 'a' + 26;
		else if(d <= '9' && d >= '0')
			d = d - '0' + 52;
		else if(!base64url && d == '+')
			d = 62;
		else if(base64url && d == '-')
			d = 62;
		else if(!base64url && d == '/')
			d = 63;
		else if(base64url && d == '_')
			d = 63;
		else if(d == '=') {
			if(!check_padding)
				continue;
			d = 64;
		} else	continue;

		in[incount++] = (uint8_t)d;
		/* work on block of 4, unless padding is not used and there are
		 * less than 4 chars left */
		if(incount != 4 && (check_padding || srcsize))
			continue;
		assert(!check_padding || incount==4);
		/* process whole block of 4 characters into 3 output bytes */
		if((incount == 2 ||
			(incount == 4 && in[3] == pad64 && in[2] == pad64))) { /* A B = = */
			if(o+1 > targsize)
				return -1;
			target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
			o += 1;
			break; /* we are done */
		} else if(incount == 3 ||
			(incount == 4 && in[3] == pad64)) { /* A B C = */
			if(o+2 > targsize)
				return -1;
			target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
			target[o+1]= ((in[1]&0x0f)<<4) | ((in[2]&0x3c)>>2);
			o += 2;
			break; /* we are done */
		} else {
			if(incount != 4 || o+3 > targsize)
				return -1;
			/* write xxxxxxyy yyyyzzzz zzwwwwww */
			target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
			target[o+1]= ((in[1]&0x0f)<<4) | ((in[2]&0x3c)>>2);
			target[o+2]= ((in[2]&0x03)<<6) | in[3];
			o += 3;
		}
		incount = 0;
	}
	return (int)o;
}
/* padding not required if srcsize is set */
padding not required if srcsize is set
padding not required if srcsize is set
static int gldns_b64_pton_base(char const *src, size_t srcsize, uint8_t *target,
	size_t targsize, int base64url)
{
	const uint8_t pad64 = 64; 
	const char* s = src;
	uint8_t in[4];
	size_t o = 0, incount = 0;
	int check_padding = (srcsize) ? 0 : 1;
	while(*s && (check_padding || srcsize)) {
		char d = *s++;
		srcsize--;
		if(d <= 'Z' && d >= 'A')
			d -= 'A';
		else if(d <= 'z' && d >= 'a')
			d = d - 'a' + 26;
		else if(d <= '9' && d >= '0')
			d = d - '0' + 52;
		else if(!base64url && d == '+')
			d = 62;
		else if(base64url && d == '-')
			d = 62;
		else if(!base64url && d == '/')
			d = 63;
		else if(base64url && d == '_')
			d = 63;
		else if(d == '=') {
			if(!check_padding)
				continue;
			d = 64;
		} else	continue;
		in[incount++] = (uint8_t)d;
		if(incount != 4 && (check_padding || srcsize))
			continue;
		assert(!check_padding || incount==4);
		if((incount == 2 ||
			(incount == 4 && in[3] == pad64 && in[2] == pad64))) { 
			if(o+1 > targsize)
				return -1;
			target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
			o += 1;
			break; 
		} else if(incount == 3 ||
			(incount == 4 && in[3] == pad64)) { 
			if(o+2 > targsize)
				return -1;
			target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
			target[o+1]= ((in[1]&0x0f)<<4) | ((in[2]&0x3c)>>2);
			o += 2;
			break; 
		} else {
			if(incount != 4 || o+3 > targsize)
				return -1;
			target[o] = (in[0]<<2) | ((in[1]&0x30)>>4);
			target[o+1]= ((in[1]&0x0f)<<4) | ((in[2]&0x3c)>>2);
			target[o+2]= ((in[2]&0x03)<<6) | in[3];
			o += 3;
		}
		incount = 0;
	}
	return (int)o;
}
static int gldns_b64_pton_base ( char const * src , size_t srcsize , uint8_t * target , size_t targsize , int base64url ) { const uint8_t pad64 = 64 ; const char * s = src ; uint8_t in [ 4 ] ; size_t o = 0 , incount = 0 ; int check_padding = ( srcsize ) ? 0 : 1 ; while ( * s && ( check_padding || srcsize ) ) { char d = * s ++ ; srcsize -- ; if ( d <= ' ' && d >= ' ' ) d -= ' ' ; else if ( d <= ' ' && d >= ' ' ) d = d - ' ' + 26 ; else if ( d <= ' ' && d >= ' ' ) d = d - ' ' + 52 ; else if ( ! base64url && d == ' ' ) d = 62 ; else if ( base64url && d == ' ' ) d = 62 ; else if ( ! base64url && d == ' ' ) d = 63 ; else if ( base64url && d == ' ' ) d = 63 ; else if ( d == ' ' ) { if ( ! check_padding ) continue ; d = 64 ; } else continue ; in [ incount ++ ] = ( uint8_t ) d ; if ( incount != 4 && ( check_padding || srcsize ) ) continue ; assert ( ! check_padding || incount == 4 ) ; if ( ( incount == 2 || ( incount == 4 && in [ 3 ] == pad64 && in [ 2 ] == pad64 ) ) ) { if ( o + 1 > targsize ) return -1 ; target [ o ] = ( in [ 0 ] << 2 ) | ( ( in [ 1 ] & 0x30 ) >> 4 ) ; o += 1 ; break ; } else if ( incount == 3 || ( incount == 4 && in [ 3 ] == pad64 ) ) { if ( o + 2 > targsize ) return -1 ; target [ o ] = ( in [ 0 ] << 2 ) | ( ( in [ 1 ] & 0x30 ) >> 4 ) ; target [ o + 1 ] = ( ( in [ 1 ] & 0x0f ) << 4 ) | ( ( in [ 2 ] & 0x3c ) >> 2 ) ; o += 2 ; break ; } else { if ( incount != 4 || o + 3 > targsize ) return -1 ; target [ o ] = ( in [ 0 ] << 2 ) | ( ( in [ 1 ] & 0x30 ) >> 4 ) ; target [ o + 1 ] = ( ( in [ 1 ] & 0x0f ) << 4 ) | ( ( in [ 2 ] & 0x3c ) >> 2 ) ; target [ o + 2 ] = ( ( in [ 2 ] & 0x03 ) << 6 ) | in [ 3 ] ; o += 3 ; } incount = 0 ; } return ( int ) o ; }
padding not required if srcsize is set
padding not required if srcsize is set
/* is 64th in the b64 array */ /* skip any character that is not base64 */ /* conceptually we do:
		const char* b64 =      pad'=' is appended to array
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		const char* d = strchr(b64, *s++);
		and use d-b64;
		*/ /* work on block of 4, unless padding is not used and there are
		 * less than 4 chars left */ /* process whole block of 4 characters into 3 output bytes */ /* A B = = */ /* we are done */ /* A B C = */ /* we are done */ /* write xxxxxxyy yyyyzzzz zzwwwwww */