Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/ucs2_string.h>
0003 #include <linux/module.h>
0004 
0005 /* Return the number of unicode characters in data */
0006 unsigned long
0007 ucs2_strnlen(const ucs2_char_t *s, size_t maxlength)
0008 {
0009         unsigned long length = 0;
0010 
0011         while (*s++ != 0 && length < maxlength)
0012                 length++;
0013         return length;
0014 }
0015 EXPORT_SYMBOL(ucs2_strnlen);
0016 
0017 unsigned long
0018 ucs2_strlen(const ucs2_char_t *s)
0019 {
0020         return ucs2_strnlen(s, ~0UL);
0021 }
0022 EXPORT_SYMBOL(ucs2_strlen);
0023 
0024 /*
0025  * Return the number of bytes is the length of this string
0026  * Note: this is NOT the same as the number of unicode characters
0027  */
0028 unsigned long
0029 ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength)
0030 {
0031         return ucs2_strnlen(data, maxlength/sizeof(ucs2_char_t)) * sizeof(ucs2_char_t);
0032 }
0033 EXPORT_SYMBOL(ucs2_strsize);
0034 
0035 int
0036 ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len)
0037 {
0038         while (1) {
0039                 if (len == 0)
0040                         return 0;
0041                 if (*a < *b)
0042                         return -1;
0043                 if (*a > *b)
0044                         return 1;
0045                 if (*a == 0) /* implies *b == 0 */
0046                         return 0;
0047                 a++;
0048                 b++;
0049                 len--;
0050         }
0051 }
0052 EXPORT_SYMBOL(ucs2_strncmp);
0053 
0054 unsigned long
0055 ucs2_utf8size(const ucs2_char_t *src)
0056 {
0057     unsigned long i;
0058     unsigned long j = 0;
0059 
0060     for (i = 0; src[i]; i++) {
0061         u16 c = src[i];
0062 
0063         if (c >= 0x800)
0064             j += 3;
0065         else if (c >= 0x80)
0066             j += 2;
0067         else
0068             j += 1;
0069     }
0070 
0071     return j;
0072 }
0073 EXPORT_SYMBOL(ucs2_utf8size);
0074 
0075 /*
0076  * copy at most maxlength bytes of whole utf8 characters to dest from the
0077  * ucs2 string src.
0078  *
0079  * The return value is the number of characters copied, not including the
0080  * final NUL character.
0081  */
0082 unsigned long
0083 ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength)
0084 {
0085     unsigned int i;
0086     unsigned long j = 0;
0087     unsigned long limit = ucs2_strnlen(src, maxlength);
0088 
0089     for (i = 0; maxlength && i < limit; i++) {
0090         u16 c = src[i];
0091 
0092         if (c >= 0x800) {
0093             if (maxlength < 3)
0094                 break;
0095             maxlength -= 3;
0096             dest[j++] = 0xe0 | (c & 0xf000) >> 12;
0097             dest[j++] = 0x80 | (c & 0x0fc0) >> 6;
0098             dest[j++] = 0x80 | (c & 0x003f);
0099         } else if (c >= 0x80) {
0100             if (maxlength < 2)
0101                 break;
0102             maxlength -= 2;
0103             dest[j++] = 0xc0 | (c & 0x7c0) >> 6;
0104             dest[j++] = 0x80 | (c & 0x03f);
0105         } else {
0106             maxlength -= 1;
0107             dest[j++] = c & 0x7f;
0108         }
0109     }
0110     if (maxlength)
0111         dest[j] = '\0';
0112     return j;
0113 }
0114 EXPORT_SYMBOL(ucs2_as_utf8);
0115 
0116 MODULE_LICENSE("GPL v2");