Blame SOURCES/0035-ucs2-document-things-a-little-better.patch

b15ea1
From 6b2b7e8803027b7089a853af0f47b53a7d20e950 Mon Sep 17 00:00:00 2001
b15ea1
From: Peter Jones <pjones@redhat.com>
b15ea1
Date: Tue, 18 Jun 2019 13:12:39 -0400
b15ea1
Subject: [PATCH 35/86] ucs2: document things a little better
b15ea1
b15ea1
Signed-off-by: Peter Jones <pjones@redhat.com>
b15ea1
---
b15ea1
 src/ucs2.h | 135 +++++++++++++++++++++++++++++++++++++++--------------
b15ea1
 1 file changed, 100 insertions(+), 35 deletions(-)
b15ea1
b15ea1
diff --git a/src/ucs2.h b/src/ucs2.h
b15ea1
index 478de23b23f..3f8a41d8ccc 100644
b15ea1
--- a/src/ucs2.h
b15ea1
+++ b/src/ucs2.h
b15ea1
@@ -22,11 +22,20 @@
b15ea1
 #define ev_bits(val, mask, shift) \
b15ea1
 	(((val) & ((mask) << (shift))) >> (shift))
b15ea1
 
b15ea1
+/*
b15ea1
+ * ucs2len(): Count the number of characters in a UCS-2 string.
b15ea1
+ * s: a UCS-2 string
b15ea1
+ * limit: the maximum number of uint16_t bytepairs to examine
b15ea1
+ *
b15ea1
+ * returns the number of characters before NUL is found (i.e., excluding
b15ea1
+ * the NUL character).  If limit is non-negative, no character index above
b15ea1
+ * limit will be accessed, and the maximum return value is limit.
b15ea1
+ */
b15ea1
 static inline size_t UNUSED
b15ea1
-ucs2len(const void *vs, ssize_t limit)
b15ea1
+ucs2len(const void *s, ssize_t limit)
b15ea1
 {
b15ea1
 	ssize_t i;
b15ea1
-	const uint8_t *s8 = vs;
b15ea1
+	const uint8_t *s8 = s;
b15ea1
 
b15ea1
 	for (i = 0;
b15ea1
 	     i < (limit >= 0 ? limit : i+1) && !(s8[0] == 0 && s8[1] == 0);
b15ea1
@@ -35,6 +44,15 @@ ucs2len(const void *vs, ssize_t limit)
b15ea1
 	return i;
b15ea1
 }
b15ea1
 
b15ea1
+/*
b15ea1
+ * ucs2size(): count the number of bytes in use by a UCS-2 string.
b15ea1
+ * s: a UCS-2 string
b15ea1
+ * limit: the maximum number of uint16_t bytepairs to examine
b15ea1
+ *
b15ea1
+ * returns the number of bytes, including NUL, in the UCS-2 string s.  If
b15ea1
+ * limit is non-negative, no character index above limit will be accessed,
b15ea1
+ * and the maximum return value is limit.
b15ea1
+ */
b15ea1
 static inline size_t UNUSED
b15ea1
 ucs2size(const void *s, ssize_t limit)
b15ea1
 {
b15ea1
@@ -46,6 +64,18 @@ ucs2size(const void *s, ssize_t limit)
b15ea1
 	return rc;
b15ea1
 }
b15ea1
 
b15ea1
+/*
b15ea1
+ * utf8len(): Count the number of characters in a UTF-8 string.
b15ea1
+ * s: a UTF-8 string
b15ea1
+ * limit: the maximum number of bytes to examine
b15ea1
+ *
b15ea1
+ * returns the number of UTF-8 charters before NUL is found (i.e.,
b15ea1
+ * excluding the NUL character).  If limit is non-negative, no character
b15ea1
+ * index above limit will be accessed, and the maximum return value is
b15ea1
+ * limit.
b15ea1
+ *
b15ea1
+ * Caveat: only good up to 3-byte sequences.
b15ea1
+ */
b15ea1
 static inline size_t UNUSED NONNULL(1)
b15ea1
 utf8len(const unsigned char *s, ssize_t limit)
b15ea1
 {
b15ea1
@@ -63,6 +93,15 @@ utf8len(const unsigned char *s, ssize_t limit)
b15ea1
 	return j;
b15ea1
 }
b15ea1
 
b15ea1
+/*
b15ea1
+ * utf8size(): count the number of bytes in use by a UTF-8 string.
b15ea1
+ * s: a UTF-8 string
b15ea1
+ * limit: the maximum number of bytes to examine
b15ea1
+ *
b15ea1
+ * returns the number of bytes, including NUL, in the UTF-8 string s.
b15ea1
+ * If limit is non-negative, no character index above limit will be
b15ea1
+ * accessed, and the maximum return value is limit.
b15ea1
+ */
b15ea1
 static inline size_t UNUSED NONNULL(1)
b15ea1
 utf8size(const unsigned char *s, ssize_t limit)
b15ea1
 {
b15ea1
@@ -72,68 +111,94 @@ utf8size(const unsigned char *s, ssize_t limit)
b15ea1
 	return ret;
b15ea1
 }
b15ea1
 
b15ea1
+/*
b15ea1
+ * ucs2_to_utf8(): convert UCS-2 to UTF-8
b15ea1
+ * s: the UCS-2 string
b15ea1
+ * limit: the maximum number of characters to copy from s, including the
b15ea1
+ *	  NUL terminator, or -1 for no limit.
b15ea1
+ *
b15ea1
+ * returns an allocated string, into which at most limit - 1 characters of
b15ea1
+ * UTF-8 are translated from UCS-2.  The return value is *always*
b15ea1
+ * NUL-terminated.
b15ea1
+ */
b15ea1
 static inline unsigned char * UNUSED
b15ea1
-ucs2_to_utf8(const void * const voidchars, ssize_t limit)
b15ea1
+ucs2_to_utf8(const void * const s, ssize_t limit)
b15ea1
 {
b15ea1
 	ssize_t i, j;
b15ea1
-	unsigned char *ret;
b15ea1
-	const uint16_t * const chars = voidchars;
b15ea1
+	unsigned char *out, *ret;
b15ea1
+	const uint16_t * const chars = s;
b15ea1
 
b15ea1
 	if (limit < 0)
b15ea1
 		limit = ucs2len(chars, -1);
b15ea1
-	ret = malloc(limit * 6 + 1);
b15ea1
-	if (!ret)
b15ea1
+	out = malloc(limit * 6 + 1);
b15ea1
+	if (!out)
b15ea1
 		return NULL;
b15ea1
-	memset(ret, 0, limit * 6 +1);
b15ea1
+	memset(out, 0, limit * 6 +1);
b15ea1
 
b15ea1
 	for (i=0, j=0; chars[i] && i < (limit >= 0 ? limit : i+1); i++,j++) {
b15ea1
 		if (chars[i] <= 0x7f) {
b15ea1
-			ret[j] = chars[i];
b15ea1
+			out[j] = chars[i];
b15ea1
 		} else if (chars[i] > 0x7f && chars[i] <= 0x7ff) {
b15ea1
-			ret[j++] = 0xc0 | ev_bits(chars[i], 0x1f, 6);
b15ea1
-			ret[j]   = 0x80 | ev_bits(chars[i], 0x3f, 0);
b15ea1
+			out[j++] = 0xc0 | ev_bits(chars[i], 0x1f, 6);
b15ea1
+			out[j]   = 0x80 | ev_bits(chars[i], 0x3f, 0);
b15ea1
 #if 1
b15ea1
 		} else if (chars[i] > 0x7ff) {
b15ea1
-			ret[j++] = 0xe0 | ev_bits(chars[i], 0xf, 12);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
-			ret[j]   = 0x80| ev_bits(chars[i], 0x3f, 0);
b15ea1
+			out[j++] = 0xe0 | ev_bits(chars[i], 0xf, 12);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
+			out[j]   = 0x80| ev_bits(chars[i], 0x3f, 0);
b15ea1
 		}
b15ea1
 #else
b15ea1
 		} else if (chars[i] > 0x7ff && chars[i] < 0x10000) {
b15ea1
-			ret[j++] = 0xe0 | ev_bits(chars[i], 0xf, 12);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
-			ret[j]   = 0x80| ev_bits(chars[i], 0x3f, 0);
b15ea1
+			out[j++] = 0xe0 | ev_bits(chars[i], 0xf, 12);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
+			out[j]   = 0x80| ev_bits(chars[i], 0x3f, 0);
b15ea1
 		} else if (chars[i] > 0xffff && chars[i] < 0x200000) {
b15ea1
-			ret[j++] = 0xf0 | ev_bits(chars[i], 0x7, 18);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 12);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
-			ret[j]   = 0x80| ev_bits(chars[i], 0x3f, 0);
b15ea1
+			out[j++] = 0xf0 | ev_bits(chars[i], 0x7, 18);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 12);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
+			out[j]   = 0x80| ev_bits(chars[i], 0x3f, 0);
b15ea1
 		} else if (chars[i] > 0x1fffff && chars[i] < 0x4000000) {
b15ea1
-			ret[j++] = 0xf8 | ev_bits(chars[i], 0x3, 24);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 18);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 12);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
-			ret[j]   = 0x80 | ev_bits(chars[i], 0x3f, 0);
b15ea1
+			out[j++] = 0xf8 | ev_bits(chars[i], 0x3, 24);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 18);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 12);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
+			out[j]   = 0x80 | ev_bits(chars[i], 0x3f, 0);
b15ea1
 		} else if (chars[i] > 0x3ffffff) {
b15ea1
-			ret[j++] = 0xfc | ev_bits(chars[i], 0x1, 30);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 24);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 18);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 12);
b15ea1
-			ret[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
-			ret[j]   = 0x80 | ev_bits(chars[i], 0x3f, 0);
b15ea1
+			out[j++] = 0xfc | ev_bits(chars[i], 0x1, 30);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 24);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 18);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 12);
b15ea1
+			out[j++] = 0x80 | ev_bits(chars[i], 0x3f, 6);
b15ea1
+			out[j]   = 0x80 | ev_bits(chars[i], 0x3f, 0);
b15ea1
 		}
b15ea1
 #endif
b15ea1
 	}
b15ea1
-	ret[j] = '\0';
b15ea1
+	out[j++] = '\0';
b15ea1
+	ret = realloc(out, j);
b15ea1
+	if (!ret) {
b15ea1
+		free(out);
b15ea1
+		return NULL;
b15ea1
+	}
b15ea1
 	return ret;
b15ea1
 }
b15ea1
 
b15ea1
+/*
b15ea1
+ * utf8_to_ucs2(): convert UTF-8 to UCS-2
b15ea1
+ * s: the destination buffer to write to.
b15ea1
+ * size: the size of the allocation to write to
b15ea1
+ * terminate: whether or not to add a terminator to the string
b15ea1
+ * utf8: the utf8 source
b15ea1
+ *
b15ea1
+ * returns the number of characters written to s, including the NUL
b15ea1
+ * terminator if "terminate" is true, or -1 on error.  In the case of an
b15ea1
+ * error, the buffer will not be modified.
b15ea1
+ */
b15ea1
 static inline ssize_t UNUSED NONNULL(4)
b15ea1
-utf8_to_ucs2(void *ucs2void, ssize_t size, int terminate, const unsigned char *utf8)
b15ea1
+utf8_to_ucs2(void *s, ssize_t size, bool terminate, const unsigned char *utf8)
b15ea1
 {
b15ea1
 	ssize_t req;
b15ea1
 	ssize_t i, j;
b15ea1
-	uint16_t *ucs2 = ucs2void;
b15ea1
+	uint16_t *ucs2 = s;
b15ea1
 	uint16_t val16;
b15ea1
 
b15ea1
 	if (!ucs2 && size > 0) {
b15ea1
-- 
b15ea1
2.24.1
b15ea1