summaryrefslogtreecommitdiffstats
path: root/generic/tkUtil.c
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2018-08-11 21:33:05 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2018-08-11 21:33:05 (GMT)
commit09fba835c8c9d68042f6fef960c025be7c8c3641 (patch)
tree5e38501bee08c156d14cba01ccef64012a94521b /generic/tkUtil.c
parent1d1ca3a5dde2d56b10fab0dfa5f430b98760de8e (diff)
downloadtk-09fba835c8c9d68042f6fef960c025be7c8c3641.zip
tk-09fba835c8c9d68042f6fef960c025be7c8c3641.tar.gz
tk-09fba835c8c9d68042f6fef960c025be7c8c3641.tar.bz2
More preparation for TIP #494 compatibitly. Add 2 utility functions, which can retreive big strings and ByteArrays without length overflow.
Diffstat (limited to 'generic/tkUtil.c')
-rw-r--r--generic/tkUtil.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/generic/tkUtil.c b/generic/tkUtil.c
index 1db7684..9f2dab1 100644
--- a/generic/tkUtil.c
+++ b/generic/tkUtil.c
@@ -729,8 +729,8 @@ Tk_GetScrollInfoObj(
int *intPtr) /* Filled in with number of pages or lines to
* scroll, if any. */
{
- const char *arg = Tcl_GetString(objv[2]);
- size_t length = objv[2]->length;
+ size_t length;
+ const char *arg = TkGetStringFromObj(objv[2], &length);
#define ArgPfxEq(str) \
((arg[0] == str[0]) && !strncmp(arg, str, length))
@@ -753,8 +753,7 @@ Tk_GetScrollInfoObj(
return TK_SCROLL_ERROR;
}
- arg = Tcl_GetString(objv[4]);
- length = objv[4]->length;
+ arg = TkGetStringFromObj(objv[4], &length);
if (ArgPfxEq("pages")) {
return TK_SCROLL_PAGES;
} else if (ArgPfxEq("units")) {
@@ -1213,7 +1212,7 @@ TkSendVirtualEvent(
*---------------------------------------------------------------------------
*/
-int
+size_t
TkUtfToUniChar(
const char *src, /* The UTF-8 string. */
int *chPtr) /* Filled with the Tcl_UniChar represented by
@@ -1221,12 +1220,12 @@ TkUtfToUniChar(
{
Tcl_UniChar uniChar = 0;
- int len = Tcl_UtfToUniChar(src, &uniChar);
+ size_t len = Tcl_UtfToUniChar(src, &uniChar);
if ((uniChar & 0xfc00) == 0xd800) {
Tcl_UniChar high = uniChar;
/* This can only happen if Tcl is compiled with TCL_UTF_MAX=4,
* or when a high surrogate character is detected in UTF-8 form */
- int len2 = Tcl_UtfToUniChar(src+len, &uniChar);
+ size_t len2 = Tcl_UtfToUniChar(src+len, &uniChar);
if ((uniChar & 0xfc00) == 0xdc00) {
*chPtr = (((high & 0x3ff) << 10) | (uniChar & 0x3ff)) + 0x10000;
len += len2;
@@ -1257,9 +1256,9 @@ TkUtfToUniChar(
*---------------------------------------------------------------------------
*/
-int TkUniCharToUtf(int ch, char *buf)
+size_t TkUniCharToUtf(int ch, char *buf)
{
- int size = Tcl_UniCharToUtf(ch, buf);
+ size_t size = Tcl_UniCharToUtf(ch, buf);
if ((((unsigned)(ch - 0x10000) <= 0xFFFFF)) && (size < 4)) {
/* Hey, this is wrong, we must be running TCL_UTF_MAX==3
* The best thing we can do is spit out 2 surrogates */
@@ -1272,6 +1271,27 @@ int TkUniCharToUtf(int ch, char *buf)
#endif
+
+
+unsigned char *
+TkGetByteArrayFromObj(
+ Tcl_Obj *objPtr,
+ size_t *lengthPtr
+) {
+ int length;
+
+ unsigned char *result = Tcl_GetByteArrayFromObj(objPtr, &length);
+#ifdef TCL_WIDE_INT_IS_LONG
+ if (sizeof(TCL_HASH_TYPE) > sizeof(int)) {
+ /* 64-bit and TIP #494 situation: */
+ *lengthPtr = *(TCL_HASH_TYPE *) objPtr->internalRep.twoPtrValue.ptr1;
+ } else
+#endif
+ /* 32-bit or without TIP #494 */
+ *lengthPtr = (size_t) (unsigned) length;
+ return result;
+}
+
/*
* Local Variables:
* mode: c