Last active
December 17, 2015 22:19
-
-
Save chergert/5681666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright 2013 10gen Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include <string.h> | |
| static void | |
| _bson_utf8_get_sequence (const char *utf8, | |
| unsigned char *seq_length, | |
| unsigned char *first_mask) | |
| { | |
| unsigned char c = *(const unsigned char *)utf8; | |
| unsigned char m; | |
| unsigned char n; | |
| /* | |
| * See the following[1] for a description of what the given multi-byte | |
| * sequences will be based on the bits set of the first byte. We also need | |
| * to mask the first byte based on that. All subsequent bytes are masked | |
| * against 0x3F. | |
| * | |
| * [1] http://www.joelonsoftware.com/articles/Unicode.html | |
| */ | |
| if ((c & 0x80) == 0) { | |
| n = 1; | |
| m = 0x7F; | |
| } else if ((c & 0xE0) == 0xC0) { | |
| n = 2; | |
| m = 0x1F; | |
| } else if ((c & 0xF0) == 0xE0) { | |
| n = 3; | |
| m = 0x0F; | |
| } else if ((c & 0xF8) == 0xF0) { | |
| n = 4; | |
| m = 0x07; | |
| } else if ((c & 0xFC) == 0xF8) { | |
| n = 5; | |
| m = 0x03; | |
| } else if ((c & 0xFE) == 0xFC) { | |
| n = 6; | |
| m = 0x01; | |
| } else { | |
| n = 0; | |
| m = 0; | |
| } | |
| *seq_length = n; | |
| *first_mask = m; | |
| } | |
| int | |
| _bson_utf8_validate (const char *utf8, | |
| size_t utf8_len, | |
| int allow_null) | |
| { | |
| unsigned char first_mask; | |
| unsigned char seq_length; | |
| int i; | |
| int j; | |
| for (i = 0; i < utf8_len; i += seq_length) { | |
| _bson_utf8_get_sequence(&utf8[i], &seq_length, &first_mask); | |
| if (!seq_length) { | |
| return 0; | |
| } | |
| for (j = i + 1; j < (i + seq_length); j++) { | |
| if ((utf8[j] & 0xC0) != 0x80) { | |
| return 0; | |
| } | |
| } | |
| if (!allow_null) { | |
| for (j = 0; j < seq_length; j++) { | |
| if (((i + j) > utf8_len) || !utf8[i + j]) { | |
| return 0; | |
| } | |
| } | |
| } | |
| } | |
| return 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright 2013 10gen Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #ifndef _BSON_RUBY_UTF8_H | |
| #define _BSON_RUBY_UTF8_H | |
| #include <unistd.h> | |
| /** | |
| * _bson_utf8_validate: | |
| * @utf8: A UTF-8 encoded string. | |
| * @utf8_len: The length of @utf8 in bytes. | |
| * @allow_null: 1 If '\0' is allowed within @utf8, excluding trailing \0. | |
| * | |
| * Validates that @utf8 is a valid UTF-8 string. | |
| * | |
| * If @allow_null is 1, then '\0' is allowed within @utf8_len bytes of @utf8. | |
| * Generally, this is bad practice since the main point of UTF-8 strings is | |
| * that they can be used with strlen() and friends. However, some languages | |
| * such as Python can send UTF-8 encoded strings with NUL's in them. | |
| * | |
| * Returns: 1 if @utf8 is valid UTF-8, otherwise 0. | |
| */ | |
| int | |
| _bson_utf8_validate (const char *utf8, | |
| size_t utf8_len, | |
| int allow_null); | |
| #endif /* _BSON_RUBY_UTF8_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment