-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ctype: use non-locale-specific ctype.h
We also make these constant time, even though we're never distinguishing between bits of a secret using them. From that perspective, though, this is markedly better than the locale-specific table lookups in glibc, even though base64 characters span two cache lines and valid private keys must hit both. Co-authored-by: Samuel Neves <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Samuel Neves <[email protected]>
- Loading branch information
Showing
5 changed files
with
43 additions
and
14 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* | ||
* Copyright (C) 2015-2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved. | ||
* | ||
* Specialized constant-time ctype.h reimplementations that aren't locale-specific. | ||
*/ | ||
|
||
#ifndef CTYPE_H | ||
#define CTYPE_H | ||
|
||
#include <stdbool.h> | ||
|
||
static inline bool char_is_space(int c) | ||
{ | ||
unsigned char d = c - 9; | ||
return (0x80001FU >> (d & 31)) & (1U >> (d >> 5)); | ||
} | ||
|
||
static inline bool char_is_digit(int c) | ||
{ | ||
return (unsigned int)(('0' - 1 - c) & (c - ('9' + 1))) >> (sizeof(c) * 8 - 1); | ||
} | ||
|
||
static inline bool char_is_alpha(int c) | ||
{ | ||
return (unsigned int)(('a' - 1 - (c | 32)) & ((c | 32) - ('z' + 1))) >> (sizeof(c) * 8 - 1); | ||
} | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,14 @@ | |
* Copyright (C) 2015-2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved. | ||
*/ | ||
|
||
#include <ctype.h> | ||
#include <stdarg.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
#include <unistd.h> | ||
#include "ctype.h" | ||
|
||
static bool color_mode(void) | ||
{ | ||
|
@@ -46,7 +46,7 @@ static void filter_ansi(const char *fmt, va_list args) | |
if (str[i] == '\x1b' && str[i + 1] == '[') { | ||
str[i] = str[i + 1] = '\0'; | ||
for (j = i + 2; j < len; ++j) { | ||
if (isalpha(str[j])) | ||
if (char_is_alpha(str[j])) | ||
break; | ||
str[j] = '\0'; | ||
} | ||
|