Skip to content

Commit 6270e83

Browse files
committed
refactor: make my_bit.h an independent header
Removed type aliases and macros defined in my_global.h from my_bit.h, making it possible to include it in plugins or external code without pulling in my_global.h
1 parent 246eb56 commit 6270e83

2 files changed

Lines changed: 55 additions & 93 deletions

File tree

include/my_bit.h

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
#ifndef MY_BIT_INCLUDED
1818
#define MY_BIT_INCLUDED
1919

20+
#ifdef __cplusplus
21+
extern "C" {
22+
#else
23+
#define constexpr
24+
#endif
25+
2026
/*
2127
Some useful bit functions
2228
*/
2329

24-
C_MODE_START
25-
26-
extern const uchar _my_bits_reverse_table[256];
30+
extern const unsigned char _my_bits_reverse_table[256];
2731

2832

2933
/*
@@ -43,42 +47,42 @@ extern const uchar _my_bits_reverse_table[256];
4347
Let's return 0 for the input 0, for the code simplicity.
4448
See the 000x branch. It covers both (1<<0) and 0.
4549
*/
46-
static inline CONSTEXPR uint my_bit_log2_hex_digit(uint8 value)
50+
static inline constexpr unsigned int my_bit_log2_hex_digit(unsigned char value)
4751
{
4852
return value & 0x0C ? /*1100*/ (value & 0x08 ? /*1000*/ 3 : /*0100*/ 2) :
4953
/*0010*/ (value & 0x02 ? /*0010*/ 1 : /*000x*/ 0);
5054
}
51-
static inline CONSTEXPR uint my_bit_log2_uint8(uint8 value)
55+
static inline constexpr unsigned int my_bit_log2_uint8(unsigned char value)
5256
{
53-
return value & 0xF0 ? my_bit_log2_hex_digit((uint8) (value >> 4)) + 4:
57+
return value & 0xF0 ? my_bit_log2_hex_digit((unsigned char) (value >> 4)) + 4:
5458
my_bit_log2_hex_digit(value);
5559
}
56-
static inline CONSTEXPR uint my_bit_log2_uint16(uint16 value)
60+
static inline constexpr unsigned int my_bit_log2_uint16(unsigned short value)
5761
{
58-
return value & 0xFF00 ? my_bit_log2_uint8((uint8) (value >> 8)) + 8 :
59-
my_bit_log2_uint8((uint8) value);
62+
return value & 0xFF00 ? my_bit_log2_uint8((unsigned char) (value >> 8)) + 8 :
63+
my_bit_log2_uint8((unsigned char) value);
6064
}
61-
static inline CONSTEXPR uint my_bit_log2_uint32(uint32 value)
65+
static inline constexpr unsigned int my_bit_log2_uint32(unsigned int value)
6266
{
6367
return value & 0xFFFF0000UL ?
64-
my_bit_log2_uint16((uint16) (value >> 16)) + 16 :
65-
my_bit_log2_uint16((uint16) value);
68+
my_bit_log2_uint16((unsigned short) (value >> 16)) + 16 :
69+
my_bit_log2_uint16((unsigned short) value);
6670
}
67-
static inline CONSTEXPR uint my_bit_log2_uint64(ulonglong value)
71+
static inline constexpr unsigned int my_bit_log2_uint64(unsigned long long int value)
6872
{
6973
return value & 0xFFFFFFFF00000000ULL ?
70-
my_bit_log2_uint32((uint32) (value >> 32)) + 32 :
71-
my_bit_log2_uint32((uint32) value);
74+
my_bit_log2_uint32((unsigned int) (value >> 32)) + 32 :
75+
my_bit_log2_uint32((unsigned int) value);
7276
}
73-
static inline CONSTEXPR uint my_bit_log2_size_t(size_t value)
77+
static inline constexpr unsigned int my_bit_log2_size_t(size_t value)
7478
{
7579
#ifdef __cplusplus
76-
static_assert(sizeof(size_t) <= sizeof(ulonglong),
77-
"size_t <= ulonglong is an assumption that needs to be fixed "
80+
static_assert(sizeof(size_t) <= sizeof(unsigned long long int),
81+
"size_t <= unsigned long long int is an assumption that needs to be fixed "
7882
"for this architecture. Please create an issue on "
7983
"https://jira.mariadb.org");
8084
#endif
81-
return my_bit_log2_uint64((ulonglong) value);
85+
return my_bit_log2_uint64((unsigned long long int) value);
8286
}
8387

8488

@@ -91,17 +95,17 @@ Count bits in 32bit integer
9195
9296
(Original code public domain).
9397
*/
94-
static inline uint my_count_bits_uint32(uint32 v)
98+
static inline unsigned int my_count_bits_uint32(unsigned int v)
9599
{
96100
v = v - ((v >> 1) & 0x55555555);
97101
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
98102
return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
99103
}
100104

101105

102-
static inline uint my_count_bits(ulonglong x)
106+
static inline unsigned int my_count_bits(unsigned long long int x)
103107
{
104-
return my_count_bits_uint32((uint32)x) + my_count_bits_uint32((uint32)(x >> 32));
108+
return my_count_bits_uint32((unsigned int)x) + my_count_bits_uint32((unsigned int)(x >> 32));
105109
}
106110

107111

@@ -126,7 +130,7 @@ static inline uint my_count_bits(ulonglong x)
126130
Comments shows how this works with 01100000000000000000000000001011
127131
*/
128132

129-
static inline uint32 my_round_up_to_next_power(uint32 v)
133+
static inline unsigned int my_round_up_to_next_power(unsigned int v)
130134
{
131135
v--; /* 01100000000000000000000000001010 */
132136
v|= v >> 1; /* 01110000000000000000000000001111 */
@@ -137,9 +141,9 @@ static inline uint32 my_round_up_to_next_power(uint32 v)
137141
return v+1; /* 10000000000000000000000000000000 */
138142
}
139143

140-
static inline uint32 my_clear_highest_bit(uint32 v)
144+
static inline unsigned int my_clear_highest_bit(unsigned int v)
141145
{
142-
uint32 w=v >> 1;
146+
unsigned int w=v >> 1;
143147
w|= w >> 1;
144148
w|= w >> 2;
145149
w|= w >> 4;
@@ -148,34 +152,34 @@ static inline uint32 my_clear_highest_bit(uint32 v)
148152
return v & w;
149153
}
150154

151-
static inline uint32 my_reverse_bits(uint32 key)
155+
static inline unsigned int my_reverse_bits(unsigned int key)
152156
{
153157
return
154-
((uint32)_my_bits_reverse_table[ key & 255] << 24) |
155-
((uint32)_my_bits_reverse_table[(key>> 8) & 255] << 16) |
156-
((uint32)_my_bits_reverse_table[(key>>16) & 255] << 8) |
157-
(uint32)_my_bits_reverse_table[(key>>24) ];
158+
((unsigned int)_my_bits_reverse_table[ key & 255] << 24) |
159+
((unsigned int)_my_bits_reverse_table[(key>> 8) & 255] << 16) |
160+
((unsigned int)_my_bits_reverse_table[(key>>16) & 255] << 8) |
161+
(unsigned int)_my_bits_reverse_table[(key>>24) ];
158162
}
159163

160164
/*
161165
a number with the n lowest bits set
162166
an overflow-safe version of (1 << n) - 1
163167
*/
164-
static inline uint64 my_set_bits(int n)
168+
static inline unsigned long long int my_set_bits(int n)
165169
{
166170
return (((1ULL << (n - 1)) - 1) << 1) | 1;
167171
}
168172

169173
/* Create a mask of the significant bits for the last byte (1,3,7,..255) */
170-
static inline uchar last_byte_mask(uint bits)
174+
static inline unsigned char last_byte_mask(unsigned int bits)
171175
{
172176
/* Get the number of used bits-1 (0..7) in the last byte */
173177
unsigned int const used = (bits - 1U) & 7U;
174178
/* Return bitmask for the significant bits */
175-
return (uchar) ((2U << used) - 1);
179+
return (unsigned char) ((2U << used) - 1);
176180
}
177181

178-
static inline uint my_bits_in_bytes(uint n)
182+
static inline unsigned int my_bits_in_bytes(unsigned int n)
179183
{
180184
return ((n + 7) / 8);
181185
}
@@ -188,7 +192,7 @@ static inline uint my_bits_in_bytes(uint n)
188192
Find the position of the first(least significant) bit set in
189193
the argument. Returns 64 if the argument was 0.
190194
*/
191-
static inline uint my_find_first_bit(ulonglong n)
195+
static inline unsigned int my_find_first_bit(unsigned long long int n)
192196
{
193197
if(!n)
194198
return 64;
@@ -197,9 +201,9 @@ static inline uint my_find_first_bit(ulonglong n)
197201
#elif defined(_MSC_VER)
198202
#if defined(_M_IX86)
199203
unsigned long bit;
200-
if( _BitScanForward(&bit, (uint)n))
204+
if( _BitScanForward(&bit, (unsigned int)n))
201205
return bit;
202-
_BitScanForward(&bit, (uint)(n>>32));
206+
_BitScanForward(&bit, (unsigned int)(n>>32));
203207
return bit + 32;
204208
#else
205209
unsigned long bit;
@@ -208,18 +212,23 @@ static inline uint my_find_first_bit(ulonglong n)
208212
#endif
209213
#else
210214
/* Generic case */
211-
uint shift= 0;
212-
static const uchar last_bit[16] = { 32, 0, 1, 0,
215+
unsigned int shift= 0;
216+
static const unsigned char last_bit[16] = { 32, 0, 1, 0,
213217
2, 0, 1, 0,
214218
3, 0, 1, 0,
215219
2, 0, 1, 0};
216-
uint bit;
220+
unsigned int bit;
217221
while ((bit = last_bit[(n >> shift) & 0xF]) == 32)
218222
shift+= 4;
219223
return shift+bit;
220224
#endif
221225
}
222-
C_MODE_END
226+
227+
#ifdef __cplusplus
228+
}
229+
#else
230+
#undef constexpr
231+
#endif
223232

224233
/*
225234
The helper function my_nlz(x) calculates the number of leading zeros
@@ -289,4 +298,4 @@ inline unsigned int my_nlz (unsigned long long x)
289298
}
290299
#endif
291300

292-
#endif /* MY_BIT_INCLUDED */
301+
#endif /* MY_BIT_INCLUDED */

plugin/auth_parsec/server_parsec.cc

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <mysqld_error.h>
2828
#include <mysql/service_thd.h>
2929
#include "scope.h"
30+
#include "my_bit.h"
3031
#include <cstring>
3132

3233
typedef unsigned char uchar;
@@ -43,54 +44,6 @@ constexpr parsec_iterations_t ITER_FACTOR_BASE_VAL= 10u;
4344
constexpr parsec_iterations_t ITER_BASE_VAL= 1u << ITER_FACTOR_BASE_VAL;
4445
constexpr parsec_iterations_t PARSEC_ITERATIONS_MAX= 1u << 31;
4546

46-
/*
47-
copied from my_bit.h and m_string.h because making a service
48-
will be an overkill
49-
*/
50-
typedef unsigned char uint8;
51-
typedef unsigned short uint16;
52-
typedef unsigned int uint32;
53-
54-
static inline uint32 parsec_round_up_to_next_power(uint32 v)
55-
{
56-
v--;
57-
v|= v >> 1;
58-
v|= v >> 2;
59-
v|= v >> 4;
60-
v|= v >> 8;
61-
v|= v >> 16;
62-
return v+1;
63-
}
64-
65-
66-
static inline constexpr uint32 parsec_bit_log2_hex_digit(uint8 value)
67-
{
68-
return value & 0x0C ? (value & 0x08 ? 3 : 2) :
69-
(value & 0x02 ? 1 : 0);
70-
}
71-
72-
73-
static inline constexpr uint32 parsec_bit_log2_uint8(uint8 value)
74-
{
75-
return value & 0xF0 ? parsec_bit_log2_hex_digit((uint8) (value >> 4)) + 4:
76-
parsec_bit_log2_hex_digit(value);
77-
}
78-
79-
80-
static inline constexpr uint32 parsec_bit_log2_uint16(uint16 value)
81-
{
82-
return value & 0xFF00 ? parsec_bit_log2_uint8((uint8) (value >> 8)) + 8 :
83-
parsec_bit_log2_uint8((uint8) value);
84-
}
85-
86-
87-
static inline constexpr uint32 parsec_bit_log2_uint32(uint32 value)
88-
{
89-
return value & 0xFFFF0000UL ?
90-
parsec_bit_log2_uint16((uint16) (value >> 16)) + 16 :
91-
parsec_bit_log2_uint16((uint16) value);
92-
}
93-
9447
static inline uchar base62_to_uchar(char c) {
9548
if (c >= '0' && c <= '9') return c - '0';
9649
if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
@@ -105,7 +58,7 @@ static void update_parsec_iterations(MYSQL_THD thd,
10558
struct st_mysql_sys_var *var, void *var_ptr, const void *save)
10659
{
10760
parsec_iterations_t iterations_user_input= *static_cast<const parsec_iterations_t *>(save);
108-
parsec_iterations_t iterations= parsec_round_up_to_next_power(iterations_user_input);
61+
parsec_iterations_t iterations= my_round_up_to_next_power(iterations_user_input);
10962
if (iterations != iterations_user_input)
11063
my_printf_error(ER_WRONG_VALUE_FOR_VAR, "parsec_iterations rounded up to %d",
11164
ME_WARNING, iterations);
@@ -262,7 +215,7 @@ int hash_password(const char *password, size_t password_length,
262215
MYSQL_THD thd = get_current_thd();
263216
Passwd_in_memory memory;
264217
memory.algorithm= 'P';
265-
memory.iterations= parsec_bit_log2_uint32(THDVAR(thd, iterations)) - ITER_FACTOR_BASE_VAL;
218+
memory.iterations= my_bit_log2_uint32(THDVAR(thd, iterations)) - ITER_FACTOR_BASE_VAL;
266219
my_random_bytes(memory.salt, sizeof(memory.salt));
267220

268221
uchar derived_key[PBKDF2_HASH_LENGTH];
@@ -291,7 +244,7 @@ int digest_to_binary(const char *hash, size_t hash_length,
291244
{
292245
auto stored= (Passwd_as_stored*)hash;
293246
auto memory= (Passwd_in_memory*)out;
294-
const uchar ITER_MAX_VAL= parsec_dig_vec_base62[parsec_bit_log2_uint32(PARSEC_ITERATIONS_MAX) - ITER_FACTOR_BASE_VAL];
247+
const uchar ITER_MAX_VAL= parsec_dig_vec_base62[my_bit_log2_uint32(PARSEC_ITERATIONS_MAX) - ITER_FACTOR_BASE_VAL];
295248
assert(ITER_MAX_VAL == 'L');
296249

297250
if (hash_length != sizeof (*stored) || *out_length < sizeof(*memory) ||

0 commit comments

Comments
 (0)