aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/base64/plain32/enc_uint32.c
blob: 9e117f060288c184a524856f55cd7a3827903e05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// If we have 32-bit ints, pick off 3 bytes at a time for as long as we can,
// but ensure that there are at least 4 bytes available to avoid segfaulting:
while (srclen >= 4)
{
	// Load string:
	//uint32_t str = *(uint32_t *)c; 
	uint32_t str; 

    memcpy(&str, c, sizeof(str)); 
 
	// Reorder to 32-bit big-endian, if not already in that format. The
	// workset must be in big-endian, otherwise the shifted bits do not
	// carry over properly among adjacent bytes:
	str = cpu_to_be32(str);

	// Shift input by 6 bytes each round and mask in only the lower 6 bits;
	// look up the character in the Base64 encoding table and write it to
	// the output location:
	*o++ = plain32_base64_table_enc[(str >> 26) & 0x3F];
	*o++ = plain32_base64_table_enc[(str >> 20) & 0x3F];
	*o++ = plain32_base64_table_enc[(str >> 14) & 0x3F];
	*o++ = plain32_base64_table_enc[(str >>  8) & 0x3F];

	c += 3;		// 3 bytes of input
	outl += 4;	// 4 bytes of output
	srclen -= 3;
}