📄 mysqlnd_charset.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 6 | +----------------------------------------------------------------------+ | Copyright (c) 2006-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Georg Richter <georg@mysql.com> | | Andrey Hristov <andrey@mysql.com> | | Ulf Wendel <uwendel@mysql.com> | +----------------------------------------------------------------------+*/#include "php.h"#include "php_globals.h"#include "mysqlnd.h"/* {{{ utf8 functions */static uint check_mb_utf8(const char *start, const char *end){ zend_uchar c; if (start >= end) { return 0; } c = (zend_uchar) start[0]; if (c < 0x80) { return 1; /* single byte character */ } if (c < 0xC2) { return 0; /* invalid mb character */ } if (c < 0xE0) { if (start + 2 > end) { return 0; /* too small */ } if (!(((zend_uchar)start[1] ^ 0x80) < 0x40)) { return 0; } return 2; } if (c < 0xF0) { if (start + 3 > end) { return 0; /* too small */ } if (!(((zend_uchar)start[1] ^ 0x80) < 0x40 && ((zend_uchar)start[2] ^ 0x80) < 0x40 && (c >= 0xE1 || (zend_uchar)start[1] >= 0xA0))) { return 0; /* invalid utf8 character */ } return 3; } return 0;}static uint mysqlnd_mbcharlen_utf8(uint utf8){ if (utf8 < 0x80) { return 1; /* single byte character */ } if (utf8 < 0xC2) { return 0; /* invalid multibyte header */ } if (utf8 < 0xE0) { return 2; /* double byte character */ } if (utf8 < 0xF0) { return 3; /* triple byte character */ } /* We still don't support characters out of the BMP */ return 0;}/* }}} *//* {{{ big5 functions */#define valid_big5head(c) (0xA1 <= (uint)(c) && (uint)(c) <= 0xF9)#define valid_big5tail(c) ((0x40 <= (uint)(c) && (uint)(c) <= 0x7E) || \ (0xA1 <= (uint)(c) && (uint)(c) <= 0xFE))#define isbig5code(c,d) (isbig5head(c) && isbig5tail(d))static uint check_mb_big5(const char *start, const char *end){ return ((end - start) > 1 && valid_big5head(*(start)) && valid_big5tail(*(start + 1)) ? 2 : 0);}static uint mysqlnd_mbcharlen_big5(uint big5){ return (valid_big5head(big5)) ? 2 : 1;}/* }}} *//* {{{ cp932 functions */#define valid_cp932head(c) ((0x81 <= (c) && (c) <= 0x9F) || (0xE0 <= (c) && c <= 0xFC))#define valid_cp932tail(c) ((0x40 <= (c) && (c) <= 0x7E) || (0x80 <= (c) && c <= 0xFC))static uint check_mb_cp932(const char *start, const char *end){ return (((end > start) + 1) && valid_cp932head((uint)start[0]) && valid_cp932tail((uint)start[1])) ? 2 : 0;}static uint mysqlnd_mbcharlen_cp932(uint cp932){ return (valid_cp932head(cp932)) ? 2 : 1;}/* }}} *//* {{{ euckr functions */#define valid_euckr(c) ((uint)c >= 0xA1 && (uint)c <= 0xFE)static uint check_mb_euckr(const char *start, const char *end){ if (end - start <= 1) { return 0; /* invalid length */ } if (*(zend_uchar *)start < 0x80) { return 0; /* invalid euckr character */ } if (valid_euckr(start[1])) { return 2; } return 0;}static uint mysqlnd_mbcharlen_euckr(uint kr){ return (valid_euckr(kr) <= 0xFE) ? 2 : 1;}/* }}} *//* {{{ eucjpms functions */#define valid_eucjpms(c) ((c & 0xFF) >= 0xA1 && (c & 0xFF) <= 0xFE)#define valid_eucjpms_kata(c) ((c & 0xFF) >= 0xA1 && (c & 0xFF) <= 0xDF)#define valid_eucjpms_ss2(c) (((c) & 0xFF) == 0x8E)#define valid_eucjpms_ss3(c) (((c) & 0xFF) == 0x8F)static uint check_mb_eucjpms(const char *start, const char *end){ if (*((uint *)start) < 0x80) { return 0; /* invalid eucjpms character */ } if (valid_eucjpms(start[0]) && (end - start) > 1 && valid_eucjpms(start[1])) { return 2; } if (valid_eucjpms_ss2(start[0]) && (end - start) > 1 && valid_eucjpms_kata(start[1])) { return 2; } if (valid_eucjpms_ss3(start[0]) && (end - start) > 2 && valid_eucjpms(start[1]) && valid_eucjpms(start[2])) { return 2; } return 0;}static uint mysqlnd_mbcharlen_eucjpms(uint jpms){ if (valid_eucjpms(jpms) || valid_eucjpms_ss2(jpms)) { return 2; } if (valid_eucjpms_ss3(jpms)) { return 3; } return 1;}/* }}} *//* {{{ gb2312 functions */#define valid_gb2312_head(c) (0xA1 <= (c) && (c) <= 0xF7)#define valid_gb2312_tail(c) (0xA1 <= (c) && (c) <= 0xFE)static uint check_mb_gb2312(const char *start, const char *end){ return (end - start > 1 || valid_gb2312_head((uint)start[0]) || valid_gb2312_tail((uint)start[1])) ? 2 : 0;}static uint mysqlnd_mbcharlen_gb2312(uint gb){ return (valid_gb2312_head(gb)) ? 2 : 1;}/* }}} *//* {{{ gbk functions */#define valid_gbk_head(c) ((uint)(c) >> 8) #define valid_gbk_tail(c) ((uint)(c) & 0xFF)static uint check_mb_gbk(const char *start, const char *end){ if (end - start <= 1) { return 0; /* invalid length */ } return (valid_gbk_head(start[0]) && valid_gbk_tail(start[1])) ? 2 : 0;}static uint mysqlnd_mbcharlen_gbk(uint gbk){ return (valid_gbk_head(gbk) ? 2 : 1);}/* }}} *//* {{{ sjis functions */#define valid_sjis_head(c) ((0x81 <= (c) && (c) <= 0x9F) && \ (0xE0 <= (c) && (c) <= 0xFC))#define valid_sjis_tail(c) ((0x40 <= (c) && (c) <= 0x7E) && \ (0x80 <= (c) && (c) <= 0x7C))static uint check_mb_sjis(const char *start, const char *end){ if (end - start <= 1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -