^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) International Business Machines Corp., 2000-2004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "jfs_incore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "jfs_filsys.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "jfs_unicode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "jfs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * NAME: jfs_strfromUCS()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * FUNCTION: Convert little-endian unicode string to character string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int jfs_strfromUCS_le(char *to, const __le16 * from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int len, struct nls_table *codepage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int outlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int warn_again = 5; /* Only warn up to 5 times total */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int warn = !!warn_again; /* once per string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (codepage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) for (i = 0; (i < len) && from[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) charlen =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) codepage->uni2char(le16_to_cpu(from[i]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) &to[outlen],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) NLS_MAX_CHARSET_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (charlen > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) outlen += charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) to[outlen++] = '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) for (i = 0; (i < len) && from[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (unlikely(le16_to_cpu(from[i]) & 0xff00)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) to[i] = '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (unlikely(warn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) warn--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) warn_again--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) "non-latin1 character 0x%x found in JFS file name\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) le16_to_cpu(from[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) "mount with iocharset=utf8 to access\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) to[i] = (char) (le16_to_cpu(from[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) outlen = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) to[outlen] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * NAME: jfs_strtoUCS()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * FUNCTION: Convert character string to unicode string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int jfs_strtoUCS(wchar_t * to, const unsigned char *from, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct nls_table *codepage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (codepage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) for (i = 0; len && *from; i++, from += charlen, len -= charlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) charlen = codepage->char2uni(from, len, &to[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (charlen < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) jfs_err("jfs_strtoUCS: char2uni returned %d.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) charlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) jfs_err("charset = %s, char = 0x%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) codepage->charset, *from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) for (i = 0; (i < len) && from[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) to[i] = (wchar_t) from[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) to[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * NAME: get_UCSname()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * FUNCTION: Allocate and translate to unicode string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int get_UCSname(struct component_name * uniName, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct nls_table *nls_tab = JFS_SBI(dentry->d_sb)->nls_tab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int length = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (length > JFS_NAME_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) uniName->name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kmalloc_array(length + 1, sizeof(wchar_t), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (uniName->name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) uniName->namlen = jfs_strtoUCS(uniName->name, dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) length, nls_tab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (uniName->namlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(uniName->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return uniName->namlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }