aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkUtils.h
diff options
context:
space:
mode:
authorGravatar Hal Canary <halcanary@google.com>2017-01-11 15:53:25 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-01-11 21:27:29 +0000
commitd1c8e56423f4d1a879f3a7bcd24e2725d9b690a7 (patch)
tree7a054dc76e1818e3de7cfa553ff61ca511b13285 /src/core/SkUtils.h
parentf046e15347373c20e42b1a25ecd87cbdb84de146 (diff)
SkUTF8_CountUnichars(s,l) and SkUTF8_NextUnichar(s,l) now safe.
Theory: We will accept blobs of data as utf-8 text without validation, but when it comes time to process it: count code poits or convert to code points, be careful to check for errors. TODO: SkTypeface::charsToGlyphs() needs to take a length. Change-Id: Id8110ab43dbffce96faffdda1e0bdaa39cad40e4 Reviewed-on: https://skia-review.googlesource.com/6849 Commit-Queue: Hal Canary <halcanary@google.com> Reviewed-by: Herb Derby <herb@google.com>
Diffstat (limited to 'src/core/SkUtils.h')
-rw-r--r--src/core/SkUtils.h28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/core/SkUtils.h b/src/core/SkUtils.h
index 26f19e6906..e24dd52f45 100644
--- a/src/core/SkUtils.h
+++ b/src/core/SkUtils.h
@@ -9,6 +9,7 @@
#define SkUtils_DEFINED
#include "SkTypes.h"
+#include "SkMath.h"
/** Similar to memset(), but it assigns a 16bit value into the buffer.
@param buffer The memory to have value copied into it
@@ -58,7 +59,31 @@ inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
}
int SkUTF8_CountUnichars(const char utf8[]);
-int SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
+
+/** This function is safe: invalid UTF8 sequences will return -1; */
+int SkUTF8_CountUnicharsWithError(const char utf8[], size_t byteLength);
+
+/** This function is safe: invalid UTF8 sequences will return 0; */
+inline int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) {
+ return SkClampPos(SkUTF8_CountUnicharsWithError(utf8, byteLength));
+}
+
+/** This function is safe: invalid UTF8 sequences will return -1
+ * When -1 is returned, ptr is unchanged.
+ * Precondition: *ptr < end;
+ */
+SkUnichar SkUTF8_NextUnicharWithError(const char** ptr, const char* end);
+
+/** this version replaces invalid utf-8 sequences with code point U+FFFD. */
+inline SkUnichar SkUTF8_NextUnichar(const char** ptr, const char* end) {
+ SkUnichar val = SkUTF8_NextUnicharWithError(ptr, end);
+ if (val < 0) {
+ *ptr = end;
+ return 0xFFFD; // REPLACEMENT CHARACTER
+ }
+ return val;
+}
+
SkUnichar SkUTF8_ToUnichar(const char utf8[]);
SkUnichar SkUTF8_NextUnichar(const char**);
SkUnichar SkUTF8_PrevUnichar(const char**);
@@ -99,5 +124,4 @@ inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
}
return true;
}
-
#endif