-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic32: dec: factor decoding loop into inline function
- Loading branch information
Showing
4 changed files
with
48 additions
and
31 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,36 +1,47 @@ | ||
// Read source 4 bytes at a time | ||
// Since we might be writing one byte more than needed, | ||
// we need to make sure there will still be some room | ||
// for one extra byte in o. | ||
// This will be the case if srclen > 0 when the loop | ||
// is exited | ||
while (srclen > 4) | ||
static inline void | ||
dec_loop_generic_32 (const uint8_t **s, size_t *slen, uint8_t **o, size_t *olen) | ||
{ | ||
const uint32_t str | ||
= base64_table_dec_d0[c[0]] | ||
| base64_table_dec_d1[c[1]] | ||
| base64_table_dec_d2[c[2]] | ||
| base64_table_dec_d3[c[3]]; | ||
if (*slen < 8) { | ||
return; | ||
} | ||
|
||
// Process blocks of 4 bytes per round. Because one extra zero byte is | ||
// written after the output, ensure that there will be at least 4 bytes | ||
// of input data left to cover the gap. (Two data bytes and up to two | ||
// end-of-string markers.) | ||
size_t rounds = (*slen - 4) / 4; | ||
|
||
*slen -= rounds * 4; // 4 bytes consumed per round | ||
*olen += rounds * 3; // 3 bytes produced per round | ||
|
||
do { | ||
const uint32_t str | ||
= base64_table_dec_d0[(*s)[0]] | ||
| base64_table_dec_d1[(*s)[1]] | ||
| base64_table_dec_d2[(*s)[2]] | ||
| base64_table_dec_d3[(*s)[3]]; | ||
|
||
#if BASE64_LITTLE_ENDIAN | ||
// LUTs for little-endian set Most Significant Bit | ||
// in case of invalid character | ||
if (str & UINT32_C(0x80000000)) { | ||
break; | ||
} | ||
|
||
// LUTs for little-endian set MSB in case of invalid character: | ||
if (str & UINT32_C(0x80000000)) { | ||
break; | ||
} | ||
#else | ||
// LUTs for big-endian set Least Significant Bit | ||
// in case of invalid character | ||
if (str & UINT32_C(1)) { | ||
break; | ||
} | ||
// LUTs for big-endian set LSB in case of invalid character: | ||
if (str & UINT32_C(1)) { | ||
break; | ||
} | ||
#endif | ||
// Store the output: | ||
memcpy(*o, &str, sizeof (str)); | ||
|
||
*s += 4; | ||
*o += 3; | ||
|
||
// Store: | ||
memcpy(o, &str, sizeof (str)); | ||
} while (--rounds > 0); | ||
|
||
c += 4; | ||
o += 3; | ||
outl += 3; | ||
srclen -= 4; | ||
// Adjust for any rounds that were skipped: | ||
*slen += rounds * 4; | ||
*olen -= rounds * 3; | ||
} |
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
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