Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2011-09-15 15:49:36 +0400
committerCampbell Barton <ideasman42@gmail.com>2011-09-15 15:49:36 +0400
commit9648c6016b35a72aa23395f5d200e342df16490b (patch)
treee9e5184d1039db277ff45ca162b621f2bad8b861 /source/blender/blenlib
parent86d05b31446d8960679ece640089474afe5577d9 (diff)
fix [#28658] python can assign non utf8 and crash because of string lenth limits.
add BLI_strncpy_utf8() which which ensures there are no partially copied UTF8 characters, limited by the buffer size.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_string.h1
-rw-r--r--source/blender/blenlib/intern/string_utf8.c40
2 files changed, 41 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index be77e18c24b..c53ce9dced5 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -144,6 +144,7 @@ void BLI_ascii_strtoupper(char *str, int len);
/* string_utf8.c - may move these into their own header some day - campbell */
+char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy);
int BLI_utf8_invalid_byte(const char *str, int length);
int BLI_utf8_invalid_strip(char *str, int length);
diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c
index 8f7e4518e03..5c37d3003e4 100644
--- a/source/blender/blenlib/intern/string_utf8.c
+++ b/source/blender/blenlib/intern/string_utf8.c
@@ -141,3 +141,43 @@ int BLI_utf8_invalid_strip(char *str, int length)
return tot;
}
+
+
+/* compatible with BLI_strncpy, but esnure no partial utf8 chars */
+
+/* array copied from glib's glib's gutf8.c,
+ * note: this looks to be at odd's with 'trailingBytesForUTF8',
+ * need to find out what gives here! - campbell */
+static const size_t utf8_skip_data[256] = {
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
+};
+
+char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy)
+{
+ char *dst_r= dst;
+ size_t utf8_size;
+
+ /* note: currently we dont attempt to deal with invalid utf8 chars */
+
+ while(*src != '\0' && (utf8_size= utf8_skip_data[*src]) < maxncpy) {
+ maxncpy -= utf8_size;
+ switch(utf8_size) {
+ case 6: *dst ++ = *src ++;
+ case 5: *dst ++ = *src ++;
+ case 4: *dst ++ = *src ++;
+ case 3: *dst ++ = *src ++;
+ case 2: *dst ++ = *src ++;
+ case 1: *dst ++ = *src ++;
+ }
+ }
+ *dst= '\0';
+ return dst_r;
+}
+