summaryrefslogtreecommitdiffstats
path: root/modules/mahonia/utf16.go
blob: 3ec7b770288abbd0bbf4457fbcf629fd2c6097b4 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package mahonia

import (
	"unicode/utf16"
)

func init() {
	for i := 0; i < len(utf16Charsets); i++ {
		RegisterCharset(&utf16Charsets[i])
	}
}

var utf16Charsets = []Charset{
	{
		Name: "UTF-16",
		NewDecoder: func() Decoder {
			var decodeRune Decoder
			return func(p []byte) (c rune, size int, status Status) {
				if decodeRune == nil {
					// haven't read the BOM yet
					if len(p) < 2 {
						status = NO_ROOM
						return
					}

					switch {
					case p[0] == 0xfe && p[1] == 0xff:
						decodeRune = decodeUTF16beRune
						return 0, 2, STATE_ONLY
					case p[0] == 0xff && p[1] == 0xfe:
						decodeRune = decodeUTF16leRune
						return 0, 2, STATE_ONLY
					default:
						decodeRune = decodeUTF16beRune
					}
				}

				return decodeRune(p)
			}
		},
		NewEncoder: func() Encoder {
			wroteBOM := false
			return func(p []byte, c rune) (size int, status Status) {
				if !wroteBOM {
					if len(p) < 2 {
						status = NO_ROOM
						return
					}

					p[0] = 0xfe
					p[1] = 0xff
					wroteBOM = true
					return 2, STATE_ONLY
				}

				return encodeUTF16beRune(p, c)
			}
		},
	},
	{
		Name:       "UTF-16BE",
		NewDecoder: func() Decoder { return decodeUTF16beRune },
		NewEncoder: func() Encoder { return encodeUTF16beRune },
	},
	{
		Name:       "UTF-16LE",
		NewDecoder: func() Decoder { return decodeUTF16leRune },
		NewEncoder: func() Encoder { return encodeUTF16leRune },
	},
}

func decodeUTF16beRune(p []byte) (r rune, size int, status Status) {
	if len(p) < 2 {
		status = NO_ROOM
		return
	}

	c := rune(p[0])<<8 + rune(p[1])

	if utf16.IsSurrogate(c) {
		if len(p) < 4 {
			status = NO_ROOM
			return
		}

		c2 := rune(p[2])<<8 + rune(p[3])
		c = utf16.DecodeRune(c, c2)

		if c == 0xfffd {
			return c, 2, INVALID_CHAR
		} else {
			return c, 4, SUCCESS
		}
	}

	return c, 2, SUCCESS
}

func encodeUTF16beRune(p []byte, c rune) (size int, status Status) {
	if c < 0x10000 {
		if len(p) < 2 {
			status = NO_ROOM
			return
		}
		p[0] = byte(c >> 8)
		p[1] = byte(c)
		return 2, SUCCESS
	}

	if len(p) < 4 {
		status = NO_ROOM
		return
	}
	s1, s2 := utf16.EncodeRune(c)
	p[0] = byte(s1 >> 8)
	p[1] = byte(s1)
	p[2] = byte(s2 >> 8)
	p[3] = byte(s2)
	return 4, SUCCESS
}

func decodeUTF16leRune(p []byte) (r rune, size int, status Status) {
	if len(p) < 2 {
		status = NO_ROOM
		return
	}

	c := rune(p[1])<<8 + rune(p[0])

	if utf16.IsSurrogate(c) {
		if len(p) < 4 {
			status = NO_ROOM
			return
		}

		c2 := rune(p[3])<<8 + rune(p[2])
		c = utf16.DecodeRune(c, c2)

		if c == 0xfffd {
			return c, 2, INVALID_CHAR
		} else {
			return c, 4, SUCCESS
		}
	}

	return c, 2, SUCCESS
}

func encodeUTF16leRune(p []byte, c rune) (size int, status Status) {
	if c < 0x10000 {
		if len(p) < 2 {
			status = NO_ROOM
			return
		}
		p[1] = byte(c >> 8)
		p[0] = byte(c)
		return 2, SUCCESS
	}

	if len(p) < 4 {
		status = NO_ROOM
		return
	}
	s1, s2 := utf16.EncodeRune(c)
	p[1] = byte(s1 >> 8)
	p[0] = byte(s1)
	p[3] = byte(s2 >> 8)
	p[2] = byte(s2)
	return 4, SUCCESS
}