-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic64: enc: factor encoding loop into inline function
- Loading branch information
Showing
3 changed files
with
48 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,43 @@ | ||
// 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) | ||
static inline void | ||
enc_loop_generic_64 (const uint8_t **s, size_t *slen, uint8_t **o, size_t *olen) | ||
{ | ||
uint64_t str; | ||
|
||
// Load string: | ||
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 = BASE64_HTOBE64(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++ = base64_table_enc[(str >> 58) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 52) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 46) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 40) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 34) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 28) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 22) & 0x3F]; | ||
*o++ = base64_table_enc[(str >> 16) & 0x3F]; | ||
|
||
c += 6; // 6 bytes of input | ||
outl += 8; // 8 bytes of output | ||
srclen -= 6; | ||
if (*slen < 8) { | ||
return; | ||
} | ||
|
||
// Process blocks of 6 bytes at a time. Because blocks are loaded 8 | ||
// bytes at a time, ensure that there will be at least 2 remaining | ||
// bytes after the last round, so that the final read will not pass | ||
// beyond the bounds of the input buffer: | ||
size_t rounds = (*slen - 2) / 6; | ||
|
||
*slen -= rounds * 6; // 6 bytes consumed per round | ||
*olen += rounds * 8; // 8 bytes produced per round | ||
|
||
do { | ||
uint64_t src; | ||
|
||
// Load input: | ||
memcpy(&src, *s, sizeof (src)); | ||
|
||
// 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: | ||
src = BASE64_HTOBE64(src); | ||
|
||
// 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)++ = base64_table_enc[(src >> 58) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 52) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 46) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 40) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 34) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 28) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 22) & 0x3F]; | ||
*(*o)++ = base64_table_enc[(src >> 16) & 0x3F]; | ||
|
||
*s += 6; | ||
|
||
} while (--rounds > 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters