Skip to content
Tcl Source Code Artifact [60594daee0]
Login

Artifact [60594daee0]

Artifact 60594daee0def40c68962ac3ade07847cd7b945b:

Attachment "utf-8-nullbyte.patch" to ticket [949905ffff] added by rmax on 2004-05-07 22:57:48.
? utf-8-nullbyte.patch
Index: tclEncoding.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclEncoding.c,v
retrieving revision 1.21
diff -u -r1.21 tclEncoding.c
--- tclEncoding.c	5 May 2004 18:28:39 -0000	1.21
+++ tclEncoding.c	7 May 2004 15:57:14 -0000
@@ -225,6 +225,16 @@
 			    CONST char *src, int srcLen, int flags,
 			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
 			    int *srcReadPtr, int *dstWrotePtr,
+			    int *dstCharsPtr, int nullbyte));
+static int		UtfIntToUtfExtProc _ANSI_ARGS_((ClientData clientData,
+			    CONST char *src, int srcLen, int flags,
+			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
+			    int *srcReadPtr, int *dstWrotePtr,
+			    int *dstCharsPtr));
+static int		UtfExtToUtfIntProc _ANSI_ARGS_((ClientData clientData,
+			    CONST char *src, int srcLen, int flags,
+			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
+			    int *srcReadPtr, int *dstWrotePtr,
 			    int *dstCharsPtr));
 static int		TclFindEncodings _ANSI_ARGS_((CONST char *argv0));
 
@@ -272,8 +282,8 @@
     systemEncoding	= Tcl_GetEncoding(NULL, type.encodingName);
 
     type.encodingName	= "utf-8";
-    type.toUtfProc	= UtfToUtfProc;
-    type.fromUtfProc    = UtfToUtfProc;
+    type.toUtfProc	= UtfExtToUtfIntProc;
+    type.fromUtfProc	= UtfIntToUtfExtProc;
     type.freeProc	= NULL;
     type.nullSize	= 1;
     type.clientData	= NULL;
@@ -1775,6 +1785,43 @@
     return result;
 }
 
+
+static int 
+UtfIntToUtfExtProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
+	     srcReadPtr, dstWrotePtr, dstCharsPtr)
+    ClientData clientData;
+    CONST char *src;
+    int srcLen;
+    int flags;
+    Tcl_EncodingState *statePtr;
+    char *dst;
+    int dstLen;
+    int *srcReadPtr;
+    int *dstWrotePtr;
+    int *dstCharsPtr;
+{
+    return UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
+			srcReadPtr, dstWrotePtr, dstCharsPtr, 1);
+}
+
+static int 
+UtfExtToUtfIntProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
+	     srcReadPtr, dstWrotePtr, dstCharsPtr)
+    ClientData clientData;
+    CONST char *src;
+    int srcLen;
+    int flags;
+    Tcl_EncodingState *statePtr;
+    char *dst;
+    int dstLen;
+    int *srcReadPtr;
+    int *dstWrotePtr;
+    int *dstCharsPtr;
+{
+    return UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
+			srcReadPtr, dstWrotePtr, dstCharsPtr, 0);
+}
+
 /*
  *-------------------------------------------------------------------------
  *
@@ -1795,7 +1842,7 @@
 
 static int 
 UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
-	srcReadPtr, dstWrotePtr, dstCharsPtr)
+	     srcReadPtr, dstWrotePtr, dstCharsPtr, nullbyte)
     ClientData clientData;	/* Not used. */
     CONST char *src;		/* Source string in UTF-8. */
     int srcLen;			/* Source string length in bytes. */
@@ -1852,8 +1899,17 @@
 	    result = TCL_CONVERT_NOSPACE;
 	    break;
 	}
-	if (UCHAR(*src) < 0x80) {
+	if (UCHAR(*src) < 0x80 && UCHAR(*src) > 0) {
 	    *dst++ = *src++;
+	} else if (nullbyte == 1 &&
+		   UCHAR(*src) == 0xc0 &&
+		   UCHAR(*(src+1)) == 0x80) {
+	    /*
+	     * On writing, embedded nulls get translated from the
+	     * internal representation to real null bytes.
+	     */
+	    *dst++ = 0;
+	    src += 2;
 	} else {
 	    src += Tcl_UtfToUniChar(src, &ch);
 	    dst += Tcl_UniCharToUtf(ch, dst);