75
/**************************************************************************
76
Converts a raw binary data to a '\0'-terminated hex string. The output is
77
truncated if there is not enough space in "hex", make sure "hex_size" is at
78
least (2 * raw_size + 1) if you do not want this to happen. Returns the
79
actual number of characters written to "hex" (including the '\0'). */
84
/* out: number of chars written */
85
const void* raw, /* in: raw data */
86
ulint raw_size, /* in: "raw" length in bytes */
87
char* hex, /* out: hex string */
88
ulint hex_size) /* in: "hex" size in bytes */
91
#ifdef WORDS_BIGENDIAN
93
#define MK_UINT16(a, b) (((uint16) (a)) << 8 | (uint16) (b))
95
#define UINT16_GET_A(u) ((unsigned char) ((u) >> 8))
96
#define UINT16_GET_B(u) ((unsigned char) ((u) & 0xFF))
98
#else /* WORDS_BIGENDIAN */
100
#define MK_UINT16(a, b) (((uint16) (b)) << 8 | (uint16) (a))
102
#define UINT16_GET_A(u) ((unsigned char) ((u) & 0xFF))
103
#define UINT16_GET_B(u) ((unsigned char) ((u) >> 8))
105
#endif /* WORDS_BIGENDIAN */
107
#define MK_ALL_UINT16_WITH_A(a) \
125
static const uint16 hex_map[256] = {
126
MK_ALL_UINT16_WITH_A('0'),
127
MK_ALL_UINT16_WITH_A('1'),
128
MK_ALL_UINT16_WITH_A('2'),
129
MK_ALL_UINT16_WITH_A('3'),
130
MK_ALL_UINT16_WITH_A('4'),
131
MK_ALL_UINT16_WITH_A('5'),
132
MK_ALL_UINT16_WITH_A('6'),
133
MK_ALL_UINT16_WITH_A('7'),
134
MK_ALL_UINT16_WITH_A('8'),
135
MK_ALL_UINT16_WITH_A('9'),
136
MK_ALL_UINT16_WITH_A('A'),
137
MK_ALL_UINT16_WITH_A('B'),
138
MK_ALL_UINT16_WITH_A('C'),
139
MK_ALL_UINT16_WITH_A('D'),
140
MK_ALL_UINT16_WITH_A('E'),
141
MK_ALL_UINT16_WITH_A('F')
143
const unsigned char* rawc;
148
rawc = (const unsigned char*) raw;
155
if (hex_size <= 2 * raw_size) {
157
read_bytes = hex_size / 2;
158
write_bytes = hex_size;
161
read_bytes = raw_size;
162
write_bytes = 2 * raw_size + 1;
165
#define LOOP_READ_BYTES(ASSIGN) \
166
for (i = 0; i < read_bytes; i++) { \
172
if (ut_align_offset(hex, 2) == 0) {
175
*(uint16*) hex = hex_map[*rawc]
180
*hex = UINT16_GET_A(hex_map[*rawc]);
181
*(hex + 1) = UINT16_GET_B(hex_map[*rawc])
185
if (hex_size <= 2 * raw_size && hex_size % 2 == 0) {
195
/***********************************************************************
196
Adds single quotes to the start and end of string and escapes any quotes
197
by doubling them. Returns the number of bytes that were written to "buf"
198
(including the terminating '\0'). If buf_size is too small then the
199
trailing bytes from "str" are discarded. */
204
/* out: number of bytes
206
const char* str, /* in: string */
207
ulint str_len, /* in: string length in bytes */
208
char* buf, /* out: output buffer */
209
ulint buf_size) /* in: output buffer size
244
for (str_i = 0; str_i < str_len; str_i++) {
248
if (buf_size - buf_i == 2) {
258
if (UNIV_UNLIKELY(buf_size - buf_i < 4)) {
270
if (UNIV_UNLIKELY(buf_size - buf_i < 4)) {