1
/*****************************************************************************
3
Copyright (C) 1994, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/*******************************************************************//**
20
@file include/ut0mem.ic
23
Created 5/30/1994 Heikki Tuuri
24
************************************************************************/
27
#include "mach0data.h"
29
/** Wrapper for memcpy(3). Copy memory area when the source and
30
target are not overlapping.
31
* @param dest in: copy to
32
* @param sour in: copy from
33
* @param n in: number of bytes to copy
37
ut_memcpy(void* dest, const void* sour, ulint n)
39
return(memcpy(dest, sour, n));
42
/** Wrapper for memmove(3). Copy memory area when the source and
43
target are overlapping.
44
* @param dest in: copy to
45
* @param sour in: copy from
46
* @param n in: number of bytes to copy
50
ut_memmove(void* dest, const void* sour, ulint n)
52
return(memmove(dest, sour, n));
55
/** Wrapper for memcmp(3). Compare memory areas.
56
* @param str1 in: first memory block to compare
57
* @param str2 in: second memory block to compare
58
* @param n in: number of bytes to compare
59
* @return negative, 0, or positive if str1 is smaller, equal,
60
or greater than str2, respectively. */
63
ut_memcmp(const void* str1, const void* str2, ulint n)
65
return(memcmp(str1, str2, n));
68
/** Wrapper for strcpy(3). Copy a NUL-terminated string.
69
* @param dest in: copy to
70
* @param sour in: copy from
74
ut_strcpy(char* dest, const char* sour)
76
return(strcpy(dest, sour));
79
/** Wrapper for strlen(3). Determine the length of a NUL-terminated string.
80
* @param str in: string
81
* @return length of the string in bytes, excluding the terminating NUL */
84
ut_strlen(const char* str)
89
/** Wrapper for strcmp(3). Compare NUL-terminated strings.
90
* @param str1 in: first string to compare
91
* @param str2 in: second string to compare
92
* @return negative, 0, or positive if str1 is smaller, equal,
93
or greater than str2, respectively. */
96
ut_strcmp(const char* str1, const char* str2)
98
return(strcmp(str1, str2));
101
/**********************************************************************//**
102
Compute strlen(ut_strcpyq(str, q)).
103
@return length of the string when quoted */
108
const char* str, /*!< in: null-terminated string */
109
char q) /*!< in: the quote character */
113
for (len = 0; *str; len++, str++) {
122
/**********************************************************************//**
123
Converts a raw binary data to a NUL-terminated hex string. The output is
124
truncated if there is not enough space in "hex", make sure "hex_size" is at
125
least (2 * raw_size + 1) if you do not want this to happen. Returns the
126
actual number of characters written to "hex" (including the NUL).
127
@return number of chars written */
132
const void* raw, /*!< in: raw data */
133
ulint raw_size, /*!< in: "raw" length in bytes */
134
char* hex, /*!< out: hex string */
135
ulint hex_size) /*!< in: "hex" size in bytes */
138
#ifdef WORDS_BIGENDIAN
140
#define MK_UINT16(a, b) (((uint16) (a)) << 8 | (uint16) (b))
142
#define UINT16_GET_A(u) ((unsigned char) ((u) >> 8))
143
#define UINT16_GET_B(u) ((unsigned char) ((u) & 0xFF))
145
#else /* WORDS_BIGENDIAN */
147
#define MK_UINT16(a, b) (((uint16) (b)) << 8 | (uint16) (a))
149
#define UINT16_GET_A(u) ((unsigned char) ((u) & 0xFF))
150
#define UINT16_GET_B(u) ((unsigned char) ((u) >> 8))
152
#endif /* WORDS_BIGENDIAN */
154
#define MK_ALL_UINT16_WITH_A(a) \
172
static const uint16 hex_map[256] = {
173
MK_ALL_UINT16_WITH_A('0'),
174
MK_ALL_UINT16_WITH_A('1'),
175
MK_ALL_UINT16_WITH_A('2'),
176
MK_ALL_UINT16_WITH_A('3'),
177
MK_ALL_UINT16_WITH_A('4'),
178
MK_ALL_UINT16_WITH_A('5'),
179
MK_ALL_UINT16_WITH_A('6'),
180
MK_ALL_UINT16_WITH_A('7'),
181
MK_ALL_UINT16_WITH_A('8'),
182
MK_ALL_UINT16_WITH_A('9'),
183
MK_ALL_UINT16_WITH_A('A'),
184
MK_ALL_UINT16_WITH_A('B'),
185
MK_ALL_UINT16_WITH_A('C'),
186
MK_ALL_UINT16_WITH_A('D'),
187
MK_ALL_UINT16_WITH_A('E'),
188
MK_ALL_UINT16_WITH_A('F')
190
const unsigned char* rawc;
195
rawc = (const unsigned char*) raw;
202
if (hex_size <= 2 * raw_size) {
204
read_bytes = hex_size / 2;
205
write_bytes = hex_size;
208
read_bytes = raw_size;
209
write_bytes = 2 * raw_size + 1;
212
#define LOOP_READ_BYTES(ASSIGN) \
213
for (i = 0; i < read_bytes; i++) { \
219
if (ut_align_offset(hex, 2) == 0) {
222
*(uint16*) hex = hex_map[*rawc]
227
*hex = UINT16_GET_A(hex_map[*rawc]);
228
*(hex + 1) = UINT16_GET_B(hex_map[*rawc])
232
if (hex_size <= 2 * raw_size && hex_size % 2 == 0) {
242
/*******************************************************************//**
243
Adds single quotes to the start and end of string and escapes any quotes
244
by doubling them. Returns the number of bytes that were written to "buf"
245
(including the terminating NUL). If buf_size is too small then the
246
trailing bytes from "str" are discarded.
247
@return number of bytes that were written */
252
const char* str, /*!< in: string */
253
ulint str_len, /*!< in: string length in bytes */
254
char* buf, /*!< out: output buffer */
255
ulint buf_size) /*!< in: output buffer size
290
for (str_i = 0; str_i < str_len; str_i++) {
294
if (buf_size - buf_i == 2) {
304
if (UNIV_UNLIKELY(buf_size - buf_i < 4)) {
316
if (UNIV_UNLIKELY(buf_size - buf_i < 4)) {