aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/base64/plain64/enc_uint64.c
blob: 3d5955af24d3d19d54d956643cee6a60f40f1e75 (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
28
29
30
31
// If we have 64-bit ints, pick off 6 bytes at a time for as long as we can,
// but ensure that there are at least 8 bytes available to avoid segfaulting:
while (srclen >= 8)
{
	// Load string:
	//uint64_t str = *(uint64_t *)c;
    uint64_t str;

    memcpy(&str, c, sizeof(str));

	// Reorder to 64-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_be64(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++ = plain64_base64_table_enc[(str >> 58) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 52) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 46) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 40) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 34) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 28) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 22) & 0x3F];
	*o++ = plain64_base64_table_enc[(str >> 16) & 0x3F];

	c += 6;		// 6 bytes of input
	outl += 8;	// 8 bytes of output
	srclen -= 6;
}