r5298 jmb - /trunk/libparserutils/test/cscodec-utf16.c

netsurf at semichrome.net netsurf at semichrome.net
Tue Sep 9 18:44:28 BST 2008


Author: jmb
Date: Tue Sep  9 12:44:26 2008
New Revision: 5298

URL: http://source.netsurf-browser.org?rev=5298&view=rev
Log:
Fix test driver to be endian independent

Modified:
    trunk/libparserutils/test/cscodec-utf16.c

Modified: trunk/libparserutils/test/cscodec-utf16.c
URL: http://source.netsurf-browser.org/trunk/libparserutils/test/cscodec-utf16.c?rev=5298&r1=5297&r2=5298&view=diff
==============================================================================
--- trunk/libparserutils/test/cscodec-utf16.c (original)
+++ trunk/libparserutils/test/cscodec-utf16.c Tue Sep  9 12:44:26 2008
@@ -63,14 +63,18 @@
 	if (ctx.buflen == 0)
 		return 1;
 
-	ctx.buf = malloc(2 * ctx.buflen);
+	ctx.buf = malloc(ctx.buflen);
 	if (ctx.buf == NULL) {
-		printf("Failed allocating %u bytes\n",
-				(unsigned int) ctx.buflen);
+		printf("Failed allocating %zu bytes\n", ctx.buflen);
 		return 1;
 	}
 
-	ctx.exp = ctx.buf + ctx.buflen;
+	ctx.exp = malloc(ctx.buflen);
+	if (ctx.exp == NULL) {
+		printf("Failed allocating %zu bytes\n", ctx.buflen);
+		free(ctx.buf);
+		return 1;
+	}
 	ctx.explen = ctx.buflen;
 
 	ctx.buf[0] = '\0';
@@ -200,10 +204,14 @@
 					&& isxdigit(data[6]));
 				/* UTF-16 code is always host endian (different
 				   than UCS-32 !).  */
-				ctx->buf[ctx->bufused++]
-					= (hex2digit(data[5]) << 4) | hex2digit(data[6]);
-				ctx->buf[ctx->bufused++]
-					= (hex2digit(data[3]) << 4) | hex2digit(data[4]);
+				const uint16_t nCodePoint = 
+						(hex2digit(data[3]) << 12) | 
+						(hex2digit(data[4]) <<  8) | 
+						(hex2digit(data[5]) <<  4) | 
+						hex2digit(data[6]);
+				*((uint16_t *) (void *) (ctx->buf + ctx->bufused)) = 
+						nCodePoint;
+				ctx->bufused += 2;
 				data += sizeof ("&#xNNNN")-1;
 				datalen -= sizeof ("&#xNNNN")-1;
 			}
@@ -234,10 +242,9 @@
 					| (hex2digit(data[8]) << 8)
 					| (hex2digit(data[9]) << 4)
 					| hex2digit(data[10]));
-				ctx->exp[ctx->expused++] = (nCodePoint >>  0) & 0xFF;
-				ctx->exp[ctx->expused++] = (nCodePoint >>  8) & 0xFF;
-				ctx->exp[ctx->expused++] = (nCodePoint >> 16) & 0xFF;
-				ctx->exp[ctx->expused++] = (nCodePoint >> 24) & 0xFF;
+				*((uint32_t *) (void *) (ctx->exp + ctx->expused)) = 
+						nCodePoint;
+				ctx->expused += 4;
 				data += sizeof ("&#xXXXXYYYY")-1;
 				datalen -= sizeof ("&#xXXXXYYYY")-1;
 			}




More information about the netsurf-commits mailing list