xref: /PHP-5.3/ext/mysqlnd/mysqlnd_charset.c (revision bc11e6fd)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2013 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Georg Richter <georg@mysql.com>                             |
16   |          Andrey Hristov <andrey@mysql.com>                           |
17   |          Ulf Wendel <uwendel@mysql.com>                              |
18   +----------------------------------------------------------------------+
19 */
20 #include "php.h"
21 #include "php_globals.h"
22 #include "mysqlnd.h"
23 #include "mysqlnd_priv.h"
24 #include "mysqlnd_debug.h"
25 
26 /* {{{ utf8 functions */
check_mb_utf8mb3_sequence(const char * start,const char * end)27 static unsigned int check_mb_utf8mb3_sequence(const char *start, const char *end)
28 {
29 	zend_uchar	c;
30 
31 	if (start >= end) {
32 		return 0;
33 	}
34 
35 	c = (zend_uchar) start[0];
36 
37 	if (c < 0x80) {
38 		return 1;		/* single byte character */
39 	}
40 	if (c < 0xC2) {
41 		return 0;		/* invalid mb character */
42 	}
43 	if (c < 0xE0) {
44 		if (start + 2 > end) {
45 			return 0;	/* too small */
46 		}
47 		if (!(((zend_uchar)start[1] ^ 0x80) < 0x40)) {
48 			return 0;
49 		}
50 		return 2;
51 	}
52 	if (c < 0xF0) {
53 		if (start + 3 > end) {
54 			return 0;	/* too small */
55 		}
56 		if (!(((zend_uchar)start[1] ^ 0x80) < 0x40 && ((zend_uchar)start[2] ^ 0x80) < 0x40 &&
57 			(c >= 0xE1 || (zend_uchar)start[1] >= 0xA0))) {
58 			return 0;	/* invalid utf8 character */
59 		}
60 		return 3;
61 	}
62 	return 0;
63 }
64 
65 
check_mb_utf8_sequence(const char * start,const char * end)66 static unsigned int check_mb_utf8_sequence(const char *start, const char *end)
67 {
68 	zend_uchar	c;
69 
70 	if (start >= end) {
71 		return 0;
72 	}
73 
74 	c = (zend_uchar) start[0];
75 
76 	if (c < 0x80) {
77 		return 1;		/* single byte character */
78 	}
79 	if (c < 0xC2) {
80 		return 0;		/* invalid mb character */
81 	}
82 	if (c < 0xE0) {
83 		if (start + 2 > end) {
84 			return 0;	/* too small */
85 		}
86 		if (!(((zend_uchar)start[1] ^ 0x80) < 0x40)) {
87 			return 0;
88 		}
89 		return 2;
90 	}
91 	if (c < 0xF0) {
92 		if (start + 3 > end) {
93 			return 0;	/* too small */
94 		}
95 		if (!(((zend_uchar)start[1] ^ 0x80) < 0x40 && ((zend_uchar)start[2] ^ 0x80) < 0x40 &&
96 			(c >= 0xE1 || (zend_uchar)start[1] >= 0xA0))) {
97 			return 0;	/* invalid utf8 character */
98 		}
99 		return 3;
100 	}
101 	if (c < 0xF5) {
102 		if (start + 4 > end) { /* We need 4 characters */
103 			return 0;	/* too small */
104 		}
105 
106 		/*
107 		  UTF-8 quick four-byte mask:
108 		  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
109 		  Encoding allows to encode U+00010000..U+001FFFFF
110 
111 		  The maximum character defined in the Unicode standard is U+0010FFFF.
112 		  Higher characters U+00110000..U+001FFFFF are not used.
113 
114 		  11110000.10010000.10xxxxxx.10xxxxxx == F0.90.80.80 == U+00010000 (min)
115 		  11110100.10001111.10111111.10111111 == F4.8F.BF.BF == U+0010FFFF (max)
116 
117 		  Valid codes:
118 		  [F0][90..BF][80..BF][80..BF]
119 		  [F1][80..BF][80..BF][80..BF]
120 		  [F2][80..BF][80..BF][80..BF]
121 		  [F3][80..BF][80..BF][80..BF]
122 		  [F4][80..8F][80..BF][80..BF]
123 		*/
124 
125 		if (!(((zend_uchar)start[1] ^ 0x80) < 0x40 &&
126 			((zend_uchar)start[2] ^ 0x80) < 0x40 &&
127 			((zend_uchar)start[3] ^ 0x80) < 0x40 &&
128 				(c >= 0xf1 || (zend_uchar)start[1] >= 0x90) &&
129 				(c <= 0xf3 || (zend_uchar)start[1] <= 0x8F)))
130 		{
131 			return 0;	/* invalid utf8 character */
132 		}
133 		return 4;
134 	}
135 	return 0;
136 }
137 
check_mb_utf8mb3_valid(const char * start,const char * end)138 static unsigned int check_mb_utf8mb3_valid(const char *start, const char *end)
139 {
140 	unsigned int len = check_mb_utf8mb3_sequence(start, end);
141 	return (len > 1)? len:0;
142 }
143 
check_mb_utf8_valid(const char * start,const char * end)144 static unsigned int check_mb_utf8_valid(const char *start, const char *end)
145 {
146 	unsigned int len = check_mb_utf8_sequence(start, end);
147 	return (len > 1)? len:0;
148 }
149 
150 
mysqlnd_mbcharlen_utf8mb3(unsigned int utf8)151 static unsigned int mysqlnd_mbcharlen_utf8mb3(unsigned int utf8)
152 {
153 	if (utf8 < 0x80) {
154 		return 1;		/* single byte character */
155 	}
156 	if (utf8 < 0xC2) {
157 		return 0;		/* invalid multibyte header */
158 	}
159 	if (utf8 < 0xE0) {
160 		return 2;		/* double byte character */
161 	}
162 	if (utf8 < 0xF0) {
163 		return 3;		/* triple byte character */
164 	}
165 	return 0;
166 }
167 
168 
mysqlnd_mbcharlen_utf8(unsigned int utf8)169 static unsigned int mysqlnd_mbcharlen_utf8(unsigned int utf8)
170 {
171 	if (utf8 < 0x80) {
172 		return 1;		/* single byte character */
173 	}
174 	if (utf8 < 0xC2) {
175 		return 0;		/* invalid multibyte header */
176 	}
177 	if (utf8 < 0xE0) {
178 		return 2;		/* double byte character */
179 	}
180 	if (utf8 < 0xF0) {
181 		return 3;		/* triple byte character */
182 	}
183 	if (utf8 < 0xF8) {
184 		return 4;		/* four byte character */
185 	}
186 	return 0;
187 }
188 /* }}} */
189 
190 
191 /* {{{ big5 functions */
192 #define valid_big5head(c)	(0xA1 <= (unsigned int)(c) && (unsigned int)(c) <= 0xF9)
193 #define valid_big5tail(c)	((0x40 <= (unsigned int)(c) && (unsigned int)(c) <= 0x7E) || \
194 							(0xA1 <= (unsigned int)(c) && (unsigned int)(c) <= 0xFE))
195 
196 #define isbig5code(c,d) (isbig5head(c) && isbig5tail(d))
197 
check_mb_big5(const char * start,const char * end)198 static unsigned int check_mb_big5(const char *start, const char *end)
199 {
200 	return (valid_big5head(*(start)) && (end - start) > 1 && valid_big5tail(*(start + 1)) ? 2 : 0);
201 }
202 
203 
mysqlnd_mbcharlen_big5(unsigned int big5)204 static unsigned int mysqlnd_mbcharlen_big5(unsigned int big5)
205 {
206 	return (valid_big5head(big5)) ? 2 : 1;
207 }
208 /* }}} */
209 
210 
211 /* {{{ cp932 functions */
212 #define valid_cp932head(c) ((0x81 <= (c) && (c) <= 0x9F) || (0xE0 <= (c) && c <= 0xFC))
213 #define valid_cp932tail(c) ((0x40 <= (c) && (c) <= 0x7E) || (0x80 <= (c) && c <= 0xFC))
214 
215 
check_mb_cp932(const char * start,const char * end)216 static unsigned int check_mb_cp932(const char *start, const char *end)
217 {
218 	return (valid_cp932head((zend_uchar)start[0]) && (end - start >  1) &&
219 			valid_cp932tail((zend_uchar)start[1])) ? 2 : 0;
220 }
221 
222 
mysqlnd_mbcharlen_cp932(unsigned int cp932)223 static unsigned int mysqlnd_mbcharlen_cp932(unsigned int cp932)
224 {
225 	return (valid_cp932head((zend_uchar)cp932)) ? 2 : 1;
226 }
227 /* }}} */
228 
229 
230 /* {{{ euckr functions */
231 #define valid_euckr(c)	((0xA1 <= (zend_uchar)(c) && (zend_uchar)(c) <= 0xFE))
232 
check_mb_euckr(const char * start,const char * end)233 static unsigned int check_mb_euckr(const char *start, const char *end)
234 {
235 	if (end - start <= 1) {
236 		return 0;	/* invalid length */
237 	}
238 	if (*(zend_uchar *)start < 0x80) {
239 		return 0;	/* invalid euckr character */
240 	}
241 	if (valid_euckr(start[1])) {
242 		return 2;
243 	}
244 	return 0;
245 }
246 
247 
mysqlnd_mbcharlen_euckr(unsigned int kr)248 static unsigned int mysqlnd_mbcharlen_euckr(unsigned int kr)
249 {
250 	return (valid_euckr(kr)) ? 2 : 1;
251 }
252 /* }}} */
253 
254 
255 /* {{{ eucjpms functions */
256 #define valid_eucjpms(c) 		(((c) & 0xFF) >= 0xA1 && ((c) & 0xFF) <= 0xFE)
257 #define valid_eucjpms_kata(c)	(((c) & 0xFF) >= 0xA1 && ((c) & 0xFF) <= 0xDF)
258 #define valid_eucjpms_ss2(c)	(((c) & 0xFF) == 0x8E)
259 #define valid_eucjpms_ss3(c)	(((c) & 0xFF) == 0x8F)
260 
check_mb_eucjpms(const char * start,const char * end)261 static unsigned int check_mb_eucjpms(const char *start, const char *end)
262 {
263 	if (*((zend_uchar *)start) < 0x80) {
264 		return 0;	/* invalid eucjpms character */
265 	}
266 	if (valid_eucjpms(start[0]) && (end - start) > 1 && valid_eucjpms(start[1])) {
267 		return 2;
268 	}
269 	if (valid_eucjpms_ss2(start[0]) && (end - start) > 1 && valid_eucjpms_kata(start[1])) {
270 		return 2;
271 	}
272 	if (valid_eucjpms_ss3(start[0]) && (end - start) > 2 && valid_eucjpms(start[1]) &&
273 		valid_eucjpms(start[2])) {
274 		return 2;
275 	}
276 	return 0;
277 }
278 
279 
mysqlnd_mbcharlen_eucjpms(unsigned int jpms)280 static unsigned int mysqlnd_mbcharlen_eucjpms(unsigned int jpms)
281 {
282 	if (valid_eucjpms(jpms) || valid_eucjpms_ss2(jpms)) {
283 		return 2;
284 	}
285 	if (valid_eucjpms_ss3(jpms)) {
286 		return 3;
287 	}
288 	return 1;
289 }
290 /* }}} */
291 
292 
293 /* {{{ gb2312 functions */
294 #define valid_gb2312_head(c)	(0xA1 <= (zend_uchar)(c) && (zend_uchar)(c) <= 0xF7)
295 #define valid_gb2312_tail(c)	(0xA1 <= (zend_uchar)(c) && (zend_uchar)(c) <= 0xFE)
296 
297 
check_mb_gb2312(const char * start,const char * end)298 static unsigned int check_mb_gb2312(const char *start, const char *end)
299 {
300 	return (valid_gb2312_head((unsigned int)start[0]) && end - start > 1 &&
301 			valid_gb2312_tail((unsigned int)start[1])) ? 2 : 0;
302 }
303 
304 
mysqlnd_mbcharlen_gb2312(unsigned int gb)305 static unsigned int mysqlnd_mbcharlen_gb2312(unsigned int gb)
306 {
307 	return (valid_gb2312_head(gb)) ? 2 : 1;
308 }
309 /* }}} */
310 
311 
312 /* {{{ gbk functions */
313 #define valid_gbk_head(c)	(0x81<=(zend_uchar)(c) && (zend_uchar)(c)<=0xFE)
314 #define valid_gbk_tail(c)	((0x40<=(zend_uchar)(c) && (zend_uchar)(c)<=0x7E) || (0x80<=(zend_uchar)(c) && (zend_uchar)(c)<=0xFE))
315 
check_mb_gbk(const char * start,const char * end)316 static unsigned int check_mb_gbk(const char *start, const char *end)
317 {
318 	return (valid_gbk_head(start[0]) && (end) - (start) > 1 && valid_gbk_tail(start[1])) ? 2 : 0;
319 }
320 
mysqlnd_mbcharlen_gbk(unsigned int gbk)321 static unsigned int mysqlnd_mbcharlen_gbk(unsigned int gbk)
322 {
323 	return (valid_gbk_head(gbk) ? 2 : 1);
324 }
325 /* }}} */
326 
327 
328 /* {{{  functions */
329 #define valid_sjis_head(c)	((0x81 <= (c) && (c) <= 0x9F) || (0xE0 <= (c) && (c) <= 0xFC))
330 #define valid_sjis_tail(c)	((0x40 <= (c) && (c) <= 0x7E) || (0x80 <= (c) && (c) <= 0xFC))
331 
332 
check_mb_sjis(const char * start,const char * end)333 static unsigned int check_mb_sjis(const char *start, const char *end)
334 {
335 	return (valid_sjis_head((zend_uchar)start[0]) && (end - start) > 1 && valid_sjis_tail((zend_uchar)start[1])) ? 2 : 0;
336 }
337 
338 
mysqlnd_mbcharlen_sjis(unsigned int sjis)339 static unsigned int mysqlnd_mbcharlen_sjis(unsigned int sjis)
340 {
341 	return (valid_sjis_head((zend_uchar)sjis)) ? 2 : 1;
342 }
343 /* }}} */
344 
345 
346 /* {{{ ucs2 functions */
check_mb_ucs2(const char * start __attribute ((unused)),const char * end __attribute ((unused)))347 static unsigned int check_mb_ucs2(const char *start __attribute((unused)), const char *end __attribute((unused)))
348 {
349 	return 2; /* always 2 */
350 }
351 
mysqlnd_mbcharlen_ucs2(unsigned int ucs2 __attribute ((unused)))352 static unsigned int mysqlnd_mbcharlen_ucs2(unsigned int ucs2 __attribute((unused)))
353 {
354 	return 2; /* always 2 */
355 }
356 /* }}} */
357 
358 
359 /* {{{ ujis functions */
360 #define valid_ujis(c)     	((0xA1 <= ((c)&0xFF) && ((c)&0xFF) <= 0xFE))
361 #define valid_ujis_kata(c)  ((0xA1 <= ((c)&0xFF) && ((c)&0xFF) <= 0xDF))
362 #define valid_ujis_ss2(c) 	(((c)&0xFF) == 0x8E)
363 #define valid_ujis_ss3(c) 	(((c)&0xFF) == 0x8F)
364 
check_mb_ujis(const char * start,const char * end)365 static unsigned int check_mb_ujis(const char *start, const char *end)
366 {
367 	if (*(zend_uchar*)start < 0x80) {
368 		return 0;	/* invalid ujis character */
369 	}
370 	if (valid_ujis(*(start)) && valid_ujis(*((start)+1))) {
371 		return 2;
372 	}
373 	if (valid_ujis_ss2(*(start)) && valid_ujis_kata(*((start)+1))) {
374 		return 2;
375 	}
376 	if (valid_ujis_ss3(*(start)) && (end-start) > 2 && valid_ujis(*((start)+1)) && valid_ujis(*((start)+2))) {
377 		return 3;
378 	}
379 	return 0;
380 }
381 
382 
mysqlnd_mbcharlen_ujis(unsigned int ujis)383 static unsigned int mysqlnd_mbcharlen_ujis(unsigned int ujis)
384 {
385 	return (valid_ujis(ujis)? 2: valid_ujis_ss2(ujis)? 2: valid_ujis_ss3(ujis)? 3: 1);
386 }
387 /* }}} */
388 
389 
390 
391 /* {{{ utf16 functions */
392 #define UTF16_HIGH_HEAD(x)  ((((zend_uchar) (x)) & 0xFC) == 0xD8)
393 #define UTF16_LOW_HEAD(x)   ((((zend_uchar) (x)) & 0xFC) == 0xDC)
394 
check_mb_utf16(const char * start,const char * end)395 static unsigned int check_mb_utf16(const char *start, const char *end)
396 {
397 	if (start + 2 > end) {
398 		return 0;
399 	}
400 
401 	if (UTF16_HIGH_HEAD(*start)) {
402 		return (start + 4 <= end) && UTF16_LOW_HEAD(start[2]) ? 4 : 0;
403 	}
404 
405 	if (UTF16_LOW_HEAD(*start)) {
406 		return 0;
407 	}
408 	return 2;
409 }
410 
411 
mysqlnd_mbcharlen_utf16(unsigned int utf16)412 static uint mysqlnd_mbcharlen_utf16(unsigned int utf16)
413 {
414   return UTF16_HIGH_HEAD(utf16) ? 4 : 2;
415 }
416 /* }}} */
417 
418 
419 /* {{{ utf32 functions */
420 static uint
check_mb_utf32(const char * start __attribute ((unused)),const char * end __attribute ((unused)))421 check_mb_utf32(const char *start __attribute((unused)), const char *end __attribute((unused)))
422 {
423 	return 4;
424 }
425 
426 
427 static uint
mysqlnd_mbcharlen_utf32(unsigned int utf32 __attribute ((unused)))428 mysqlnd_mbcharlen_utf32(unsigned int utf32 __attribute((unused)))
429 {
430 	return 4;
431 }
432 /* }}} */
433 
434 /*
435   The server compiles sometimes the full utf-8 (the mb4) as utf8m4, and the old as utf8,
436   for BC reasons. Sometimes, utf8mb4 is just utf8 but the old charsets are utf8mb3.
437   Change easily now, with a macro, could be made compilastion dependable.
438 */
439 
440 #define UTF8_MB4 "utf8mb4"
441 #define UTF8_MB3 "utf8"
442 
443 /* {{{ mysqlnd_charsets */
444 const MYSQLND_CHARSET mysqlnd_charsets[] =
445 {
446 	{   1, "big5","big5_chinese_ci", 1, 2, "", mysqlnd_mbcharlen_big5, check_mb_big5},
447 	{   3, "dec8", "dec8_swedisch_ci", 1, 1, "", NULL, NULL},
448 	{   4, "cp850", "cp850_general_ci", 1, 1, "", NULL, NULL},
449 	{   6, "hp8", "hp8_english_ci", 1, 1, "", NULL, NULL},
450 	{   7, "koi8r", "koi8r_general_ci", 1, 1, "", NULL, NULL},
451 	{   8, "latin1", "latin1_swedish_ci", 1, 1, "", NULL, NULL},
452 	{   5, "latin1", "latin1_german_ci", 1, 1, "", NULL, NULL}, /* should be after 0x9 because swedish_ci is the default collation */
453 	{   9, "latin2", "latin2_general_ci", 1, 1, "", NULL, NULL},
454 	{   2, "latin2", "latin2_czech_cs", 1, 1, "", NULL, NULL}, /* should be after 0x9 because general_ci is the default collation */
455 	{  10, "swe7", "swe7_swedish_ci", 1, 1, "", NULL, NULL},
456 	{  11, "ascii", "ascii_general_ci", 1, 1, "", NULL, NULL},
457 	{  12, "ujis", "ujis_japanese_ci", 1, 3, "", mysqlnd_mbcharlen_ujis, check_mb_ujis},
458 	{  13, "sjis", "sjis_japanese_ci", 1, 2, "", mysqlnd_mbcharlen_sjis, check_mb_sjis},
459 	{  16, "hebrew", "hebrew_general_ci", 1, 1, "", NULL, NULL},
460 	{  17, "filename", "filename", 1, 5, "", NULL, NULL},
461 	{  18, "tis620", "tis620_thai_ci", 1, 1, "", NULL, NULL},
462 	{  19, "euckr", "euckr_korean_ci", 1, 2, "", mysqlnd_mbcharlen_euckr, check_mb_euckr},
463 	{  21, "latin2", "latin2_hungarian_ci", 1, 1, "", NULL, NULL},
464 	{  27, "latin2", "latin2_croatian_ci", 1, 1, "", NULL, NULL},
465 	{  22, "koi8u", "koi8u_general_ci", 1, 1, "", NULL, NULL},
466 	{  24, "gb2312", "gb2312_chinese_ci", 1, 2, "", mysqlnd_mbcharlen_gb2312, check_mb_gb2312},
467 	{  25, "greek", "greek_general_ci", 1, 1, "", NULL, NULL},
468 	{  26, "cp1250", "cp1250_general_ci", 1, 1, "", NULL, NULL},
469 	{  28, "gbk", "gbk_chinese_ci", 1, 2, "", mysqlnd_mbcharlen_gbk, check_mb_gbk},
470 	{  30, "latin5", "latin5_turkish_ci", 1, 1, "", NULL, NULL},
471 	{  31, "latin1", "latin1_german2_ci", 1, 1, "", NULL, NULL},
472 	{  15, "latin1", "latin1_danish_ci", 1, 1, "", NULL, NULL},
473 	{  32, "armscii8", "armscii8_general_ci", 1, 1, "", NULL, NULL},
474 	{  33, UTF8_MB3, UTF8_MB3"_general_ci", 1, 3, "UTF-8 Unicode", mysqlnd_mbcharlen_utf8mb3,  check_mb_utf8mb3_valid},
475 	{  35, "ucs2", "ucs2_general_ci", 2, 2, "UCS-2 Unicode", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
476 	{  36, "cp866", "cp866_general_ci", 1, 1, "", NULL, NULL},
477 	{  37, "keybcs2", "keybcs2_general_ci", 1, 1, "", NULL, NULL},
478 	{  38, "macce", "macce_general_ci", 1, 1, "", NULL, NULL},
479 	{  39, "macroman", "macroman_general_ci", 1, 1, "", NULL, NULL},
480 	{  40, "cp852", "cp852_general_ci", 1, 1, "", NULL, NULL},
481 	{  41, "latin7", "latin7_general_ci", 1, 1, "", NULL, NULL},
482 	{  20, "latin7", "latin7_estonian_cs", 1, 1, "", NULL, NULL},
483 	{  57, "cp1256", "cp1256_general_ci", 1, 1, "", NULL, NULL},
484 	{  59, "cp1257", "cp1257_general_ci", 1, 1, "", NULL, NULL},
485 	{  63, "binary", "binary", 1, 1, "", NULL, NULL},
486 	{  97, "eucjpms", "eucjpms_japanese_ci", 1, 3, "", mysqlnd_mbcharlen_eucjpms, check_mb_eucjpms},
487 	{  29, "cp1257", "cp1257_lithunian_ci", 1, 1, "", NULL, NULL},
488 	{  31, "latin1", "latin1_german2_ci", 1, 1, "", NULL, NULL},
489 	{  34, "cp1250", "cp1250_czech_cs", 1, 1, "", NULL, NULL},
490 	{  42, "latin7", "latin7_general_cs", 1, 1, "", NULL, NULL},
491 	{  43, "macce", "macce_bin", 1, 1, "", NULL, NULL},
492 	{  44, "cp1250", "cp1250_croatian_ci", 1, 1, "", NULL, NULL},
493 	{  45, UTF8_MB4, UTF8_MB4"_general_ci", 1, 3, "UTF-8 Unicode", mysqlnd_mbcharlen_utf8,  check_mb_utf8_valid},
494 	{  46, UTF8_MB4, UTF8_MB4"_bin", 1, 3, "UTF-8 Unicode", mysqlnd_mbcharlen_utf8,  check_mb_utf8_valid},
495 	{  47, "latin1", "latin1_bin", 1, 1, "", NULL, NULL},
496 	{  48, "latin1", "latin1_general_ci", 1, 1, "", NULL, NULL},
497 	{  49, "latin1", "latin1_general_cs", 1, 1, "", NULL, NULL},
498 	{  51, "cp1251", "cp1251_general_ci", 1, 1, "", NULL, NULL},
499 	{  14, "cp1251", "cp1251_bulgarian_ci", 1, 1, "", NULL, NULL},
500 	{  23, "cp1251", "cp1251_ukrainian_ci", 1, 1, "", NULL, NULL},
501 	{  50, "cp1251", "cp1251_bin", 1, 1, "", NULL, NULL},
502 	{  52, "cp1251", "cp1251_general_cs", 1, 1, "", NULL, NULL},
503 	{  53, "macroman", "macroman_bin", 1, 1, "", NULL, NULL},
504 	{  54, "utf16", "utf16_general_ci", 2, 4, "UTF-16 Unicode", mysqlnd_mbcharlen_utf16, check_mb_utf16},
505 	{  55, "utf16", "utf16_bin", 2, 4, "UTF-16 Unicode", mysqlnd_mbcharlen_utf16, check_mb_utf16},
506 	{  58, "cp1257", "cp1257_bin", 1, 1, "", NULL, NULL},
507 #ifdef USED_TO_BE_SO_BEFORE_MYSQL_5_5
508 	{  60, "armascii8", "armascii8_bin", 1, 1, "", NULL, NULL},
509 #endif
510 /*55*/{  60, "utf32", "utf32_general_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
511 /*55*/{  61, "utf32", "utf32_bin", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
512 	{  65, "ascii", "ascii_bin", 1, 1, "", NULL, NULL},
513 	{  66, "cp1250", "cp1250_bin", 1, 1, "", NULL, NULL},
514 	{  67, "cp1256", "cp1256_bin", 1, 1, "", NULL, NULL},
515 	{  68, "cp866", "cp866_bin", 1, 1, "", NULL, NULL},
516 	{  69, "dec8", "dec8_bin", 1, 1, "", NULL, NULL},
517 	{  70, "greek", "greek_bin", 1, 1, "", NULL, NULL},
518 	{  71, "hebew", "hebrew_bin", 1, 1, "", NULL, NULL},
519 	{  72, "hp8", "hp8_bin", 1, 1, "", NULL, NULL},
520 	{  73, "keybcs2", "keybcs2_bin", 1, 1, "", NULL, NULL},
521 	{  74, "koi8r", "koi8r_bin", 1, 1, "", NULL, NULL},
522 	{  75, "koi8u", "koi8u_bin", 1, 1, "", NULL, NULL},
523 	{  77, "latin2", "latin2_bin", 1, 1, "", NULL, NULL},
524 	{  78, "latin5", "latin5_bin", 1, 1, "", NULL, NULL},
525 	{  79, "latin7", "latin7_bin", 1, 1, "", NULL, NULL},
526 	{  80, "cp850", "cp850_bin", 1, 1, "", NULL, NULL},
527 	{  81, "cp852", "cp852_bin", 1, 1, "", NULL, NULL},
528 	{  82, "swe7", "swe7_bin", 1, 1, "", NULL, NULL},
529 	{  83, UTF8_MB3, UTF8_MB3"_bin", 1, 3, "UTF-8 Unicode", mysqlnd_mbcharlen_utf8mb3,  check_mb_utf8mb3_valid},
530 	{  84, "big5", "big5_bin", 1, 2, "", mysqlnd_mbcharlen_big5, check_mb_big5},
531 	{  85, "euckr", "euckr_bin", 1, 2, "", mysqlnd_mbcharlen_euckr, check_mb_euckr},
532 	{  86, "gb2312", "gb2312_bin", 1, 2, "", mysqlnd_mbcharlen_gb2312, check_mb_gb2312},
533 	{  87, "gbk", "gbk_bin", 1, 2, "", mysqlnd_mbcharlen_gbk, check_mb_gbk},
534 	{  88, "sjis", "sjis_bin", 1, 2, "", mysqlnd_mbcharlen_sjis, check_mb_sjis},
535 	{  89, "tis620", "tis620_bin", 1, 1, "", NULL, NULL},
536 	{  90, "ucs2", "ucs2_bin", 2, 2, "UCS-2 Unicode", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
537 	{  91, "ujis", "ujis_bin", 1, 3, "", mysqlnd_mbcharlen_ujis, check_mb_ujis},
538 	{  92, "geostd8", "geostd8_general_ci", 1, 1, "", NULL, NULL},
539 	{  93, "geostd8", "geostd8_bin", 1, 1, "", NULL, NULL},
540 	{  94, "latin1", "latin1_spanish_ci", 1, 1, "", NULL, NULL},
541 	{  95, "cp932", "cp932_japanese_ci", 1, 2, "", mysqlnd_mbcharlen_cp932, check_mb_cp932},
542 	{  96, "cp932", "cp932_bin", 1, 2, "", mysqlnd_mbcharlen_cp932, check_mb_cp932},
543 	{  97, "eucjpms", "eucjpms_japanese_ci", 1, 3, "", mysqlnd_mbcharlen_eucjpms, check_mb_eucjpms},
544 	{  98, "eucjpms", "eucjpms_bin", 1, 3, "", mysqlnd_mbcharlen_eucjpms, check_mb_eucjpms},
545 	{  99, "cp1250", "cp1250_polish_ci", 1, 1, "", NULL, NULL},
546 	{ 128, "ucs2", "ucs2_unicode_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
547 	{ 129, "ucs2", "ucs2_icelandic_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
548 	{ 130, "ucs2", "ucs2_latvian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
549 	{ 131, "ucs2", "ucs2_romanian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
550 	{ 132, "ucs2", "ucs2_slovenian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
551 	{ 133, "ucs2", "ucs2_polish_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
552 	{ 134, "ucs2", "ucs2_estonian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
553 	{ 135, "ucs2", "ucs2_spanish_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
554 	{ 136, "ucs2", "ucs2_swedish_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
555 	{ 137, "ucs2", "ucs2_turkish_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
556 	{ 138, "ucs2", "ucs2_czech_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
557 	{ 139, "ucs2", "ucs2_danish_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
558 	{ 140, "ucs2", "ucs2_lithunian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
559 	{ 141, "ucs2", "ucs2_slovak_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
560 	{ 142, "ucs2", "ucs2_spanish2_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
561 	{ 143, "ucs2", "ucs2_roman_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
562 	{ 144, "ucs2", "ucs2_persian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
563 	{ 145, "ucs2", "ucs2_esperanto_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
564 	{ 146, "ucs2", "ucs2_hungarian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
565 	{ 147, "ucs2", "ucs2_sinhala_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
566 	{ 148, "ucs2", "ucs2_german2_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
567 	{ 149, "ucs2", "ucs2_croatian_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
568 	{ 150, "ucs2", "ucs2_unicode_520_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
569 	{ 151, "ucs2", "ucs2_vietnamese_ci", 2, 2, "", mysqlnd_mbcharlen_ucs2, check_mb_ucs2},
570 
571 /*56*/{160, "utf32", "utf32_unicode_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
572 /*56*/{161, "utf32", "utf32_icelandic_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
573 /*56*/{162, "utf32", "utf32_latvian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
574 /*56*/{163, "utf32", "utf32_romanian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
575 /*56*/{164, "utf32", "utf32_slovenian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
576 /*56*/{165, "utf32", "utf32_polish_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
577 /*56*/{166, "utf32", "utf32_estonian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
578 /*56*/{167, "utf32", "utf32_spanish_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
579 /*56*/{168, "utf32", "utf32_swedish_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
580 /*56*/{169, "utf32", "utf32_turkish_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
581 /*56*/{170, "utf32", "utf32_czech_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
582 /*56*/{171, "utf32", "utf32_danish_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
583 /*56*/{172, "utf32", "utf32_lithuanian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
584 /*56*/{173, "utf32", "utf32_slovak_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
585 /*56*/{174, "utf32", "utf32_spanish2_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
586 /*56*/{175, "utf32", "utf32_roman_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
587 /*56*/{176, "utf32", "utf32_persian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
588 /*56*/{177, "utf32", "utf32_esperanto_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
589 /*56*/{178, "utf32", "utf32_hungarian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
590 /*56*/{179, "utf32", "utf32_sinhala_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
591 /*56*/{180, "utf32", "utf32_german2_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
592 /*56*/{181, "utf32", "utf32_croatian_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
593 /*56*/{182, "utf32", "utf32_unicode_520_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
594 /*56*/{183, "utf32", "utf32_vietnamese_ci", 4, 4, "UTF-32 Unicode", mysqlnd_mbcharlen_utf32, check_mb_utf32},
595 
596 	{ 192, UTF8_MB3, UTF8_MB3"_general_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
597 	{ 193, UTF8_MB3, UTF8_MB3"_icelandic_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
598 	{ 194, UTF8_MB3, UTF8_MB3"_latvian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3,  check_mb_utf8mb3_valid},
599 	{ 195, UTF8_MB3, UTF8_MB3"_romanian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
600 	{ 196, UTF8_MB3, UTF8_MB3"_slovenian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
601 	{ 197, UTF8_MB3, UTF8_MB3"_polish_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
602 	{ 198, UTF8_MB3, UTF8_MB3"_estonian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
603 	{ 199, UTF8_MB3, UTF8_MB3"_spanish_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
604 	{ 200, UTF8_MB3, UTF8_MB3"_swedish_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
605 	{ 201, UTF8_MB3, UTF8_MB3"_turkish_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
606 	{ 202, UTF8_MB3, UTF8_MB3"_czech_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
607 	{ 203, UTF8_MB3, UTF8_MB3"_danish_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid },
608 	{ 204, UTF8_MB3, UTF8_MB3"_lithunian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid },
609 	{ 205, UTF8_MB3, UTF8_MB3"_slovak_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
610 	{ 206, UTF8_MB3, UTF8_MB3"_spanish2_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
611 	{ 207, UTF8_MB3, UTF8_MB3"_roman_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
612 	{ 208, UTF8_MB3, UTF8_MB3"_persian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
613 	{ 209, UTF8_MB3, UTF8_MB3"_esperanto_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
614 	{ 210, UTF8_MB3, UTF8_MB3"_hungarian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
615 	{ 211, UTF8_MB3, UTF8_MB3"_sinhala_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
616 	{ 211, UTF8_MB3, UTF8_MB3"_german2_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
617 	{ 213, UTF8_MB3, UTF8_MB3"_croatian_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
618 	{ 214, UTF8_MB3, UTF8_MB3"_unicode_520_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
619 	{ 215, UTF8_MB3, UTF8_MB3"_vietnamese_ci", 1, 3, "", mysqlnd_mbcharlen_utf8mb3, check_mb_utf8mb3_valid},
620 
621 	{ 224, UTF8_MB4, UTF8_MB4"_unicode_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
622 	{ 225, UTF8_MB4, UTF8_MB4"_icelandic_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
623 	{ 226, UTF8_MB4, UTF8_MB4"_latvian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
624 	{ 227, UTF8_MB4, UTF8_MB4"_romanian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
625 	{ 228, UTF8_MB4, UTF8_MB4"_slovenian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
626 	{ 229, UTF8_MB4, UTF8_MB4"_polish_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
627 	{ 230, UTF8_MB4, UTF8_MB4"_estonian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
628 	{ 231, UTF8_MB4, UTF8_MB4"_spanish_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
629 	{ 232, UTF8_MB4, UTF8_MB4"_swedish_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
630 	{ 233, UTF8_MB4, UTF8_MB4"_turkish_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
631 	{ 234, UTF8_MB4, UTF8_MB4"_czech_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
632 	{ 235, UTF8_MB4, UTF8_MB4"_danish_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
633 	{ 236, UTF8_MB4, UTF8_MB4"_lithuanian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
634 	{ 237, UTF8_MB4, UTF8_MB4"_slovak_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
635 	{ 238, UTF8_MB4, UTF8_MB4"_spanish2_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
636 	{ 239, UTF8_MB4, UTF8_MB4"_roman_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
637 	{ 240, UTF8_MB4, UTF8_MB4"_persian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
638 	{ 241, UTF8_MB4, UTF8_MB4"_esperanto_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
639 	{ 242, UTF8_MB4, UTF8_MB4"_hungarian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
640 	{ 243, UTF8_MB4, UTF8_MB4"_sinhala_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
641 	{ 244, UTF8_MB4, UTF8_MB4"_german2_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
642 	{ 245, UTF8_MB4, UTF8_MB4"_croatian_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
643 	{ 246, UTF8_MB4, UTF8_MB4"_unicode_520_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
644 	{ 247, UTF8_MB4, UTF8_MB4"_vietnamese_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
645 
646 	{ 254, UTF8_MB3, UTF8_MB3"_general_cs", 1, 3, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
647 	{   0, NULL, NULL, 0, 0, NULL, NULL, NULL}
648 };
649 /* }}} */
650 
651 
652 /* {{{ mysqlnd_find_charset_nr */
mysqlnd_find_charset_nr(unsigned int charsetnr)653 PHPAPI const MYSQLND_CHARSET * mysqlnd_find_charset_nr(unsigned int charsetnr)
654 {
655 	const MYSQLND_CHARSET * c = mysqlnd_charsets;
656 
657 	do {
658 		if (c->nr == charsetnr) {
659 			return c;
660 		}
661 		++c;
662 	} while (c[0].nr != 0);
663 	return NULL;
664 }
665 /* }}} */
666 
667 
668 /* {{{ mysqlnd_find_charset_name */
mysqlnd_find_charset_name(const char * const name)669 PHPAPI const MYSQLND_CHARSET * mysqlnd_find_charset_name(const char * const name)
670 {
671 	const MYSQLND_CHARSET *c = mysqlnd_charsets;
672 
673 	do {
674 		if (!strcasecmp(c->name, name)) {
675 			return c;
676 		}
677 		++c;
678 	} while (c[0].nr != 0);
679 	return NULL;
680 }
681 /* }}} */
682 
683 
684 /* {{{ mysqlnd_cset_escape_quotes */
mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset,char * newstr,const char * escapestr,size_t escapestr_len TSRMLS_DC)685 PHPAPI ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset, char *newstr,
686 										const char * escapestr, size_t escapestr_len TSRMLS_DC)
687 {
688 	const char 	*newstr_s = newstr;
689 	const char 	*newstr_e = newstr + 2 * escapestr_len;
690 	const char 	*end = escapestr + escapestr_len;
691 	zend_bool	escape_overflow = FALSE;
692 
693 	DBG_ENTER("mysqlnd_cset_escape_quotes");
694 
695 	for (;escapestr < end; escapestr++) {
696 		unsigned int len = 0;
697 		/* check unicode characters */
698 
699 		if (cset->char_maxlen > 1 && (len = cset->mb_valid(escapestr, end))) {
700 
701 			/* check possible overflow */
702 			if ((newstr + len) > newstr_e) {
703 				escape_overflow = TRUE;
704 				break;
705 			}
706 			/* copy mb char without escaping it */
707 			while (len--) {
708 				*newstr++ = *escapestr++;
709 			}
710 			escapestr--;
711 			continue;
712 		}
713 		if (*escapestr == '\'') {
714 			if (newstr + 2 > newstr_e) {
715 				escape_overflow = TRUE;
716 				break;
717 			}
718 			*newstr++ = '\'';
719 			*newstr++ = '\'';
720 		} else {
721 			if (newstr + 1 > newstr_e) {
722 				escape_overflow = TRUE;
723 				break;
724 			}
725 			*newstr++ = *escapestr;
726 		}
727 	}
728 	*newstr = '\0';
729 
730 	if (escape_overflow) {
731 		DBG_RETURN((ulong)~0);
732 	}
733 	DBG_RETURN((ulong)(newstr - newstr_s));
734 }
735 /* }}} */
736 
737 
738 /* {{{ mysqlnd_cset_escape_slashes */
mysqlnd_cset_escape_slashes(const MYSQLND_CHARSET * const cset,char * newstr,const char * escapestr,size_t escapestr_len TSRMLS_DC)739 PHPAPI ulong mysqlnd_cset_escape_slashes(const MYSQLND_CHARSET * const cset, char *newstr,
740 										 const char * escapestr, size_t escapestr_len TSRMLS_DC)
741 {
742 	const char 	*newstr_s = newstr;
743 	const char 	*newstr_e = newstr + 2 * escapestr_len;
744 	const char 	*end = escapestr + escapestr_len;
745 	zend_bool	escape_overflow = FALSE;
746 
747 	DBG_ENTER("mysqlnd_cset_escape_slashes");
748 	DBG_INF_FMT("charset=%s", cset->name);
749 
750 	for (;escapestr < end; escapestr++) {
751 		char esc = '\0';
752 		unsigned int len = 0;
753 
754 		/* check unicode characters */
755 		if (cset->char_maxlen > 1 && (len = cset->mb_valid(escapestr, end))) {
756 			/* check possible overflow */
757 			if ((newstr + len) > newstr_e) {
758 				escape_overflow = TRUE;
759 				break;
760 			}
761 			/* copy mb char without escaping it */
762 			while (len--) {
763 				*newstr++ = *escapestr++;
764 			}
765 			escapestr--;
766 			continue;
767 		}
768 		if (cset->char_maxlen > 1 && cset->mb_charlen(*escapestr) > 1) {
769 			esc = *escapestr;
770 		} else {
771 			switch (*escapestr) {
772 				case 0:
773 					esc = '0';
774 					break;
775 				case '\n':
776 					esc = 'n';
777 					break;
778 				case '\r':
779 					esc = 'r';
780 					break;
781 				case '\\':
782 				case '\'':
783 				case '"':
784 					esc = *escapestr;
785 					break;
786 				case '\032':
787 					esc = 'Z';
788 					break;
789 			}
790 		}
791 		if (esc) {
792 			if (newstr + 2 > newstr_e) {
793 				escape_overflow = TRUE;
794 				break;
795 			}
796 			/* copy escaped character */
797 			*newstr++ = '\\';
798 			*newstr++ = esc;
799 		} else {
800 			if (newstr + 1 > newstr_e) {
801 				escape_overflow = TRUE;
802 				break;
803 			}
804 			/* copy non escaped character */
805 			*newstr++ = *escapestr;
806 		}
807 	}
808 	*newstr = '\0';
809 
810 	if (escape_overflow) {
811 		DBG_RETURN((ulong)~0);
812 	}
813 	DBG_RETURN((ulong)(newstr - newstr_s));
814 }
815 /* }}} */
816 
817 /*
818  * Local variables:
819  * tab-width: 4
820  * c-basic-offset: 4
821  * End:
822  * vim600: noet sw=4 ts=4 fdm=marker
823  * vim<600: noet sw=4 ts=4
824  */
825