]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / Vtutf8.c
1 /** @file
2 Implementation of translation upon VT-UTF8.
3
4 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Terminal.h"
16
17 /**
18 Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,
19 and insert them into Unicode FIFO.
20
21 @param TerminalDevice The terminal device.
22
23 **/
24 VOID
25 VTUTF8RawDataToUnicode (
26 IN TERMINAL_DEV *TerminalDevice
27 )
28 {
29 UTF8_CHAR Utf8Char;
30 UINT8 ValidBytes;
31 UINT16 UnicodeChar;
32
33 ValidBytes = 0;
34 //
35 // pop the raw data out from the raw fifo,
36 // and translate it into unicode, then push
37 // the unicode into unicode fifo, until the raw fifo is empty.
38 //
39 while (!IsRawFiFoEmpty (TerminalDevice)) {
40
41 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);
42
43 if (ValidBytes < 1 || ValidBytes > 3) {
44 continue;
45 }
46
47 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);
48
49 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);
50 }
51 }
52
53 /**
54 Get one valid VT-UTF8 characters set from Raw Data FIFO.
55
56 @param Utf8Device The terminal device.
57 @param Utf8Char Returned valid VT-UTF8 characters set.
58 @param ValidBytes The count of returned VT-VTF8 characters.
59 If ValidBytes is zero, no valid VT-UTF8 returned.
60
61 **/
62 VOID
63 GetOneValidUtf8Char (
64 IN TERMINAL_DEV *Utf8Device,
65 OUT UTF8_CHAR *Utf8Char,
66 OUT UINT8 *ValidBytes
67 )
68 {
69 UINT8 Temp;
70 UINT8 Index;
71 BOOLEAN FetchFlag;
72
73 Temp = 0;
74 Index = 0;
75 FetchFlag = TRUE;
76
77 //
78 // if no valid Utf8 char is found in the RawFiFo,
79 // then *ValidBytes will be zero.
80 //
81 *ValidBytes = 0;
82
83 while (!IsRawFiFoEmpty (Utf8Device)) {
84
85 RawFiFoRemoveOneKey (Utf8Device, &Temp);
86
87 switch (*ValidBytes) {
88
89 case 0:
90 if ((Temp & 0x80) == 0) {
91 //
92 // one-byte utf8 char
93 //
94 *ValidBytes = 1;
95
96 Utf8Char->Utf8_1 = Temp;
97
98 FetchFlag = FALSE;
99
100 } else if ((Temp & 0xe0) == 0xc0) {
101 //
102 // two-byte utf8 char
103 //
104 *ValidBytes = 2;
105
106 Utf8Char->Utf8_2[1] = Temp;
107
108 } else if ((Temp & 0xf0) == 0xe0) {
109 //
110 // three-byte utf8 char
111 //
112 *ValidBytes = 3;
113
114 Utf8Char->Utf8_3[2] = Temp;
115
116 Index++;
117
118 } else {
119 //
120 // reset *ValidBytes to zero, let valid utf8 char search restart
121 //
122 *ValidBytes = 0;
123 }
124
125 break;
126
127 case 2:
128 //
129 // two-byte utf8 char go on
130 //
131 if ((Temp & 0xc0) == 0x80) {
132
133 Utf8Char->Utf8_2[0] = Temp;
134
135 FetchFlag = FALSE;
136
137 } else {
138
139 *ValidBytes = 0;
140 }
141 break;
142
143 case 3:
144 //
145 // three-byte utf8 char go on
146 //
147 if ((Temp & 0xc0) == 0x80) {
148
149 Utf8Char->Utf8_3[2 - Index] = Temp;
150 Index++;
151 if (Index > 2) {
152 FetchFlag = FALSE;
153 }
154 } else {
155 //
156 // reset *ValidBytes and Index to zero, let valid utf8 char search restart
157 //
158 *ValidBytes = 0;
159 Index = 0;
160 }
161 break;
162
163 default:
164 break;
165 }
166
167 if (!FetchFlag) {
168 break;
169 }
170 }
171
172 return ;
173 }
174
175 /**
176 Translate VT-UTF8 characters into one Unicode character.
177
178 UTF8 Encoding Table
179 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
180 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
181 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
182 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
183
184
185 @param Utf8Char VT-UTF8 character set needs translating.
186 @param ValidBytes The count of valid VT-UTF8 characters.
187 @param UnicodeChar Returned unicode character.
188
189 **/
190 VOID
191 Utf8ToUnicode (
192 IN UTF8_CHAR Utf8Char,
193 IN UINT8 ValidBytes,
194 OUT CHAR16 *UnicodeChar
195 )
196 {
197 UINT8 UnicodeByte0;
198 UINT8 UnicodeByte1;
199 UINT8 Byte0;
200 UINT8 Byte1;
201 UINT8 Byte2;
202
203 *UnicodeChar = 0;
204
205 //
206 // translate utf8 code to unicode, in terminal standard,
207 // up to 3 bytes utf8 code is supported.
208 //
209 switch (ValidBytes) {
210 case 1:
211 //
212 // one-byte utf8 code
213 //
214 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;
215 break;
216
217 case 2:
218 //
219 // two-byte utf8 code
220 //
221 Byte0 = Utf8Char.Utf8_2[0];
222 Byte1 = Utf8Char.Utf8_2[1];
223
224 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
225 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);
226 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
227 break;
228
229 case 3:
230 //
231 // three-byte utf8 code
232 //
233 Byte0 = Utf8Char.Utf8_3[0];
234 Byte1 = Utf8Char.Utf8_3[1];
235 Byte2 = Utf8Char.Utf8_3[2];
236
237 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
238 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
239 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
240
241 default:
242 break;
243 }
244
245 return ;
246 }
247
248 /**
249 Translate one Unicode character into VT-UTF8 characters.
250
251 UTF8 Encoding Table
252 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
253 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
254 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
255 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
256
257
258 @param Unicode Unicode character need translating.
259 @param Utf8Char Return VT-UTF8 character set.
260 @param ValidBytes The count of valid VT-UTF8 characters. If
261 ValidBytes is zero, no valid VT-UTF8 returned.
262
263 **/
264 VOID
265 UnicodeToUtf8 (
266 IN CHAR16 Unicode,
267 OUT UTF8_CHAR *Utf8Char,
268 OUT UINT8 *ValidBytes
269 )
270 {
271 UINT8 UnicodeByte0;
272 UINT8 UnicodeByte1;
273 //
274 // translate unicode to utf8 code
275 //
276 UnicodeByte0 = (UINT8) Unicode;
277 UnicodeByte1 = (UINT8) (Unicode >> 8);
278
279 if (Unicode < 0x0080) {
280
281 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);
282 *ValidBytes = 1;
283
284 } else if (Unicode < 0x0800) {
285 //
286 // byte sequence: high -> low
287 // Utf8_2[0], Utf8_2[1]
288 //
289 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
290 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
291
292 *ValidBytes = 2;
293
294 } else {
295 //
296 // byte sequence: high -> low
297 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
298 //
299 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
300 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
301 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
302
303 *ValidBytes = 3;
304 }
305 }
306
307
308 /**
309 Check if input string is valid VT-UTF8 string.
310
311 @param TerminalDevice The terminal device.
312 @param WString The input string.
313
314 @retval EFI_SUCCESS If all input characters are valid.
315
316 **/
317 EFI_STATUS
318 VTUTF8TestString (
319 IN TERMINAL_DEV *TerminalDevice,
320 IN CHAR16 *WString
321 )
322 {
323 //
324 // to utf8, all kind of characters are supported.
325 //
326 return EFI_SUCCESS;
327 }