1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 *   Copyright (C) International Business Machines Corp., 2000-2002
4 *   Portions Copyright (C) Christoph Hellwig, 2001-2002
5 */
6#ifndef _H_JFS_UNICODE
7#define _H_JFS_UNICODE
8
9#include <linux/slab.h>
10#include <asm/byteorder.h>
11#include "../nls/nls_ucs2_data.h"
12#include "jfs_types.h"
13
14extern int get_UCSname(struct component_name *, struct dentry *);
15extern int jfs_strfromUCS_le(char *, const __le16 *, int, struct nls_table *);
16
17#define free_UCSname(COMP) kfree((COMP)->name)
18
19/*
20 * UniStrcpy:  Copy a string
21 */
22static inline wchar_t *UniStrcpy(wchar_t * ucs1, const wchar_t * ucs2)
23{
24	wchar_t *anchor = ucs1;	/* save the start of result string */
25
26	while ((*ucs1++ = *ucs2++));
27	return anchor;
28}
29
30
31
32/*
33 * UniStrncpy:  Copy length limited string with pad
34 */
35static inline __le16 *UniStrncpy_le(__le16 * ucs1, const __le16 * ucs2,
36				  size_t n)
37{
38	__le16 *anchor = ucs1;
39
40	while (n-- && *ucs2)	/* Copy the strings */
41		*ucs1++ = *ucs2++;
42
43	n++;
44	while (n--)		/* Pad with nulls */
45		*ucs1++ = 0;
46	return anchor;
47}
48
49/*
50 * UniStrncmp_le:  Compare length limited string - native to little-endian
51 */
52static inline int UniStrncmp_le(const wchar_t * ucs1, const __le16 * ucs2,
53				size_t n)
54{
55	if (!n)
56		return 0;	/* Null strings are equal */
57	while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
58		ucs1++;
59		ucs2++;
60	}
61	return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
62}
63
64/*
65 * UniStrncpy_to_le:  Copy length limited string with pad to little-endian
66 */
67static inline __le16 *UniStrncpy_to_le(__le16 * ucs1, const wchar_t * ucs2,
68				       size_t n)
69{
70	__le16 *anchor = ucs1;
71
72	while (n-- && *ucs2)	/* Copy the strings */
73		*ucs1++ = cpu_to_le16(*ucs2++);
74
75	n++;
76	while (n--)		/* Pad with nulls */
77		*ucs1++ = 0;
78	return anchor;
79}
80
81/*
82 * UniStrncpy_from_le:  Copy length limited string with pad from little-endian
83 */
84static inline wchar_t *UniStrncpy_from_le(wchar_t * ucs1, const __le16 * ucs2,
85					  size_t n)
86{
87	wchar_t *anchor = ucs1;
88
89	while (n-- && *ucs2)	/* Copy the strings */
90		*ucs1++ = __le16_to_cpu(*ucs2++);
91
92	n++;
93	while (n--)		/* Pad with nulls */
94		*ucs1++ = 0;
95	return anchor;
96}
97
98/*
99 * UniToupper:  Convert a unicode character to upper case
100 */
101static inline wchar_t UniToupper(wchar_t uc)
102{
103	const struct UniCaseRange *rp;
104
105	if (uc < sizeof(NlsUniUpperTable)) {	/* Latin characters */
106		return uc + NlsUniUpperTable[uc];	/* Use base tables */
107	} else {
108		rp = NlsUniUpperRange;	/* Use range tables */
109		while (rp->start) {
110			if (uc < rp->start)	/* Before start of range */
111				return uc;	/* Uppercase = input */
112			if (uc <= rp->end)	/* In range */
113				return uc + rp->table[uc - rp->start];
114			rp++;	/* Try next range */
115		}
116	}
117	return uc;		/* Past last range */
118}
119
120
121/*
122 * UniStrupr:  Upper case a unicode string
123 */
124static inline wchar_t *UniStrupr(wchar_t * upin)
125{
126	wchar_t *up;
127
128	up = upin;
129	while (*up) {		/* For all characters */
130		*up = UniToupper(*up);
131		up++;
132	}
133	return upin;		/* Return input pointer */
134}
135
136#endif				/* !_H_JFS_UNICODE */
137