File: unicode.c

package info (click to toggle)
kernel-source-2.4.27 2.4.27-10sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 191,224 kB
  • ctags: 610,077
  • sloc: ansic: 3,299,602; asm: 164,708; makefile: 10,962; sh: 3,725; perl: 2,273; yacc: 1,651; cpp: 820; lex: 752; tcl: 577; awk: 251; lisp: 218; sed: 79
file content (126 lines) | stat: -rw-r--r-- 2,356 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *  linux/fs/hfsplus/unicode.c
 *
 * Copyright (C) 2001
 * Brad Boyer (flar@allandria.com)
 * (C) 2003 Ardis Technologies <roman@ardistech.com>
 *
 * Handler routines for unicode strings
 */

#include <linux/types.h>
#include <linux/nls.h>
#include "hfsplus_fs.h"
#include "hfsplus_raw.h"

/* Fold the case of a unicode char, given the 16 bit value */
/* Returns folded char, or 0 if ignorable */
static inline u16 case_fold(u16 c)
{
        u16 tmp;

        tmp = case_fold_table[(c>>8)];
        if (tmp)
                tmp = case_fold_table[tmp + (c & 0xFF)];
        else
                tmp = c;
        return tmp;
}

/* Compare unicode strings, return values like normal strcmp */
int hfsplus_unistrcmp(const hfsplus_unistr *s1, const hfsplus_unistr *s2)
{
	u16 len1, len2, c1, c2;
	const hfsplus_unichr *p1, *p2;

	len1 = be16_to_cpu(s1->length);
	len2 = be16_to_cpu(s2->length);
	p1 = s1->unicode;
	p2 = s2->unicode;

	while (1) {
		c1 = c2 = 0;

		while (len1 && !c1) {
			c1 = case_fold(be16_to_cpu(*p1));
			p1++;
			len1--;
		}
		while (len2 && !c2) {
			c2 = case_fold(be16_to_cpu(*p2));
			p2++;
			len2--;
		}

		if (c1 != c2)
			return (c1 < c2) ? -1 : 1;
		if (!c1 && !c2)
			return 0;
	}
}

int hfsplus_uni2asc(const hfsplus_unistr *ustr, char *astr, int *len)
{
	const hfsplus_unichr *ip;
	u8 *op;
	u16 ustrlen, cc;
	int size, tmp;

	op = astr;
	ip = ustr->unicode;
	ustrlen = be16_to_cpu(ustr->length);
	tmp = *len;
	while (ustrlen > 0 && tmp > 0) {
		cc = be16_to_cpu(*ip);
		if (!cc || cc > 0x7f) {
			size = utf8_wctomb(op, cc ? cc : 0x2400, tmp);
			if (size == -1) {
				/* ignore */
			} else {
				op += size;
				tmp -= size;
			}
		} else {
			*op++ = (u8) cc;
			tmp--;
		}
		ip++;
		ustrlen--;
	}
	*len = (char *)op - astr;
	if (ustrlen)
		return -ENAMETOOLONG;
	return 0;
}

int hfsplus_asc2uni(hfsplus_unistr *ustr, const char *astr, int len)
{
	int tmp;
	wchar_t c;
	u16 outlen = 0;

	while (outlen <= HFSPLUS_MAX_STRLEN && len > 0) {
		if (*astr & 0x80) {
			tmp = utf8_mbtowc(&c, astr, len);
			if (tmp < 0) {
				astr++;
				len--;
				continue;
			} else {
				astr += tmp;
				len -= tmp;
			}
			if (c == 0x2400)
				c = 0;
		} else {
			c = *astr++;
			len--;
		}
		ustr->unicode[outlen] = cpu_to_be16(c);
		outlen++;
	}
	ustr->length = cpu_to_be16(outlen);
	if (len > 0)
		return -ENAMETOOLONG;
	return 0;
}