@@ -32,6 +32,58 @@ ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength)
32
32
}
33
33
EXPORT_SYMBOL (ucs2_strsize );
34
34
35
+ /**
36
+ * ucs2_strscpy() - Copy a UCS2 string into a sized buffer.
37
+ *
38
+ * @dst: Pointer to the destination buffer where to copy the string to.
39
+ * @src: Pointer to the source buffer where to copy the string from.
40
+ * @count: Size of the destination buffer, in UCS2 (16-bit) characters.
41
+ *
42
+ * Like strscpy(), only for UCS2 strings.
43
+ *
44
+ * Copy the source string @src, or as much of it as fits, into the destination
45
+ * buffer @dst. The behavior is undefined if the string buffers overlap. The
46
+ * destination buffer @dst is always NUL-terminated, unless it's zero-sized.
47
+ *
48
+ * Return: The number of characters copied into @dst (excluding the trailing
49
+ * %NUL terminator) or -E2BIG if @count is 0 or @src was truncated due to the
50
+ * destination buffer being too small.
51
+ */
52
+ ssize_t ucs2_strscpy (ucs2_char_t * dst , const ucs2_char_t * src , size_t count )
53
+ {
54
+ long res ;
55
+
56
+ /*
57
+ * Ensure that we have a valid amount of space. We need to store at
58
+ * least one NUL-character.
59
+ */
60
+ if (count == 0 || WARN_ON_ONCE (count > INT_MAX / sizeof (* dst )))
61
+ return - E2BIG ;
62
+
63
+ /*
64
+ * Copy at most 'count' characters, return early if we find a
65
+ * NUL-terminator.
66
+ */
67
+ for (res = 0 ; res < count ; res ++ ) {
68
+ ucs2_char_t c ;
69
+
70
+ c = src [res ];
71
+ dst [res ] = c ;
72
+
73
+ if (!c )
74
+ return res ;
75
+ }
76
+
77
+ /*
78
+ * The loop above terminated without finding a NUL-terminator,
79
+ * exceeding the 'count': Enforce proper NUL-termination and return
80
+ * error.
81
+ */
82
+ dst [count - 1 ] = 0 ;
83
+ return - E2BIG ;
84
+ }
85
+ EXPORT_SYMBOL (ucs2_strscpy );
86
+
35
87
int
36
88
ucs2_strncmp (const ucs2_char_t * a , const ucs2_char_t * b , size_t len )
37
89
{
0 commit comments