* xmlCheckUTF8: * @utf: Pointer to putative UTF-8 encoded string. * * Checks @utf for being valid UTF-8. @utf is assumed to be * null-terminated. This function is not super-strict, as it will * allow longer UTF-8 sequences than necessary. Note that Java is * capable of producing these sequences if provoked. Also note, this * routine checks for the 4-byte maximum size, but does not check for
| 926 | * Return value: true if @utf is valid. |
| 927 | **/ |
| 928 | int |
| 929 | xmlCheckUTF8(const unsigned char *utf) |
| 930 | { |
| 931 | int ix; |
| 932 | unsigned char c; |
| 933 | |
| 934 | if (utf == NULL) |
| 935 | return(0); |
| 936 | /* |
| 937 | * utf is a string of 1, 2, 3 or 4 bytes. The valid strings |
| 938 | * are as follows (in "bit format"): |
| 939 | * 0xxxxxxx valid 1-byte |
| 940 | * 110xxxxx 10xxxxxx valid 2-byte |
| 941 | * 1110xxxx 10xxxxxx 10xxxxxx valid 3-byte |
| 942 | * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx valid 4-byte |
| 943 | */ |
| 944 | while ((c = utf[0])) { /* string is 0-terminated */ |
| 945 | ix = 0; |
| 946 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 947 | ix = 1; |
| 948 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 949 | if ((utf[1] & 0xc0 ) != 0x80) |
| 950 | return 0; |
| 951 | ix = 2; |
| 952 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 953 | if (((utf[1] & 0xc0) != 0x80) || |
| 954 | ((utf[2] & 0xc0) != 0x80)) |
| 955 | return 0; |
| 956 | ix = 3; |
| 957 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 958 | if (((utf[1] & 0xc0) != 0x80) || |
| 959 | ((utf[2] & 0xc0) != 0x80) || |
| 960 | ((utf[3] & 0xc0) != 0x80)) |
| 961 | return 0; |
| 962 | ix = 4; |
| 963 | } else /* unknown encoding */ |
| 964 | return 0; |
| 965 | utf += ix; |
| 966 | } |
| 967 | return(1); |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * xmlUTF8Strsize: |
no outgoing calls
no test coverage detected