@@ -92,14 +92,15 @@ void cass_uuid_string(CassUuid uuid, char* output) {
92
92
size_t pos = 0 ;
93
93
char encoded[16 ];
94
94
cass::encode_uuid (encoded, uuid);
95
+ static const char half_byte_to_hex[] = { ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' ,
96
+ ' 8' , ' 9' , ' a' , ' b' , ' c' , ' d' , ' e' , ' f' };
95
97
for (size_t i = 0 ; i < 16 ; ++i) {
96
- char buf[3 ] = { ' \0 ' };
97
- sprintf (buf, " %02x" , static_cast <uint8_t >(encoded[i]));
98
98
if (i == 4 || i == 6 || i == 8 || i == 10 ) {
99
99
output[pos++] = ' -' ;
100
100
}
101
- output[pos++] = buf[0 ];
102
- output[pos++] = buf[1 ];
101
+ uint8_t byte = static_cast <uint8_t >(encoded[i]);
102
+ output[pos++] = half_byte_to_hex[(byte >> 4 ) & 0x0F ];
103
+ output[pos++] = half_byte_to_hex[byte & 0x0F ];
103
104
}
104
105
output[pos] = ' \0 ' ;
105
106
}
@@ -120,19 +121,37 @@ CassError cass_uuid_from_string_n(const char* str,
120
121
const char * pos = str;
121
122
char buf[16 ];
122
123
124
+ static const char hex_to_half_byte[256 ] = {
125
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
126
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
127
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
128
+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , -1 , -1 , -1 , -1 , -1 , -1 ,
129
+ -1 , 10 , 11 , 12 , 13 , 14 , 15 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
130
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
131
+ -1 , 10 , 11 , 12 , 13 , 14 , 15 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
132
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
133
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
134
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
135
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
136
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
137
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
138
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
139
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
140
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
141
+ };
142
+
123
143
if (str == NULL || str_length != 36 ) {
124
144
return CASS_ERROR_LIB_BAD_PARAMS;
125
145
}
126
146
127
147
for (size_t i = 0 ; i < 16 ; ++i) {
128
148
if (*pos == ' -' ) pos++;
129
- unsigned int byte ;
130
- intptr_t bytes_left = str - pos;
131
- if (bytes_left >= 2 || ! isxdigit (*pos) || ! isxdigit (*(pos + 1 ))) {
149
+ uint8_t p0 = static_cast < uint8_t >(pos[ 0 ]) ;
150
+ uint8_t p1 = static_cast < uint8_t >( pos[ 1 ]) ;
151
+ if (hex_to_half_byte[p0] == - 1 || hex_to_half_byte[p1] == - 1 ) {
132
152
return CASS_ERROR_LIB_BAD_PARAMS;
133
153
}
134
- sscanf (pos, " %2x" , &byte);
135
- buf[i] = static_cast <char >(byte);
154
+ buf[i] = (hex_to_half_byte[p0] << 4 ) + hex_to_half_byte[p1];
136
155
pos += 2 ;
137
156
}
138
157
0 commit comments