Skip to content

Commit 1e96b7c

Browse files
duanegLinus Torvalds
authored andcommitted
HFS+: refactor ASCII to unicode conversion routine for later reuse
The HFS+ filesystem is case-insensitive and does automatic unicode decomposition by default, but does not provide custom dentry operations. This can lead to multiple dentries being cached for lookups on a filename with varying case and/or character (de)composition. These patches add custom dentry hash and comparison operations for case-sensitive and/or automatically decomposing HFS+ filesystems. Unicode decomposition and case-folding are performed as required to ensure equivalent filenames are hashed to the same values and compare as equal. This patch: Refactor existing HFS+ ASCII to unicode string conversion routine to split out character conversion functionality. This will be reused by the custom dentry hash and comparison routines. This approach avoids unnecessary memory allocation compared to using the string conversion routine directly in the new functions. [akpm@linux-foundation.org: avoid use-of-uninitialised] Signed-off-by: Duane Griffin <duaneg@dghda.com> Signed-off-by: Roman Zippel <zippel@linux-m68k.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 29bc5b4 commit 1e96b7c

1 file changed

Lines changed: 62 additions & 45 deletions

File tree

‎fs/hfsplus/unicode.c‎

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -239,58 +239,75 @@ int hfsplus_uni2asc(struct super_block *sb, const struct hfsplus_unistr *ustr, c
239239
return res;
240240
}
241241

242-
int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr, const char *astr, int len)
242+
/*
243+
* Convert one or more ASCII characters into a single unicode character.
244+
* Returns the number of ASCII characters corresponding to the unicode char.
245+
*/
246+
static inline int asc2unichar(struct super_block *sb, const char *astr, int len,
247+
wchar_t *uc)
243248
{
244-
struct nls_table *nls = HFSPLUS_SB(sb).nls;
245-
int size, off, decompose;
249+
int size = HFSPLUS_SB(sb).nls->char2uni(astr, len, uc);
250+
if (size <= 0) {
251+
*uc = '?';
252+
size = 1;
253+
}
254+
switch (*uc) {
255+
case 0x2400:
256+
*uc = 0;
257+
break;
258+
case ':':
259+
*uc = '/';
260+
break;
261+
}
262+
return size;
263+
}
264+
265+
/* Decomposes a single unicode character. */
266+
static inline u16 *decompose_unichar(wchar_t uc, int *size)
267+
{
268+
int off;
269+
270+
off = hfsplus_decompose_table[(uc >> 12) & 0xf];
271+
if (off == 0 || off == 0xffff)
272+
return NULL;
273+
274+
off = hfsplus_decompose_table[off + ((uc >> 8) & 0xf)];
275+
if (!off)
276+
return NULL;
277+
278+
off = hfsplus_decompose_table[off + ((uc >> 4) & 0xf)];
279+
if (!off)
280+
return NULL;
281+
282+
off = hfsplus_decompose_table[off + (uc & 0xf)];
283+
*size = off & 3;
284+
if (*size == 0)
285+
return NULL;
286+
return hfsplus_decompose_table + (off / 4);
287+
}
288+
289+
int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr,
290+
const char *astr, int len)
291+
{
292+
int size, dsize, decompose;
293+
u16 *dstr, outlen = 0;
246294
wchar_t c;
247-
u16 outlen = 0;
248295

249296
decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
250-
251297
while (outlen < HFSPLUS_MAX_STRLEN && len > 0) {
252-
size = nls->char2uni(astr, len, &c);
253-
if (size <= 0) {
254-
c = '?';
255-
size = 1;
256-
}
257-
astr += size;
258-
len -= size;
259-
switch (c) {
260-
case 0x2400:
261-
c = 0;
262-
break;
263-
case ':':
264-
c = '/';
265-
break;
266-
}
267-
if (c >= 0xc0 && decompose) {
268-
off = hfsplus_decompose_table[(c >> 12) & 0xf];
269-
if (!off)
270-
goto done;
271-
if (off == 0xffff) {
272-
goto done;
273-
}
274-
off = hfsplus_decompose_table[off + ((c >> 8) & 0xf)];
275-
if (!off)
276-
goto done;
277-
off = hfsplus_decompose_table[off + ((c >> 4) & 0xf)];
278-
if (!off)
279-
goto done;
280-
off = hfsplus_decompose_table[off + (c & 0xf)];
281-
size = off & 3;
282-
if (!size)
283-
goto done;
284-
off /= 4;
285-
if (outlen + size > HFSPLUS_MAX_STRLEN)
298+
size = asc2unichar(sb, astr, len, &c);
299+
300+
if (decompose && (dstr = decompose_unichar(c, &dsize))) {
301+
if (outlen + dsize > HFSPLUS_MAX_STRLEN)
286302
break;
287303
do {
288-
ustr->unicode[outlen++] = cpu_to_be16(hfsplus_decompose_table[off++]);
289-
} while (--size > 0);
290-
continue;
291-
}
292-
done:
293-
ustr->unicode[outlen++] = cpu_to_be16(c);
304+
ustr->unicode[outlen++] = cpu_to_be16(*dstr++);
305+
} while (--dsize > 0);
306+
} else
307+
ustr->unicode[outlen++] = cpu_to_be16(c);
308+
309+
astr += size;
310+
len -= size;
294311
}
295312
ustr->length = cpu_to_be16(outlen);
296313
if (len > 0)

0 commit comments

Comments
 (0)