]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/Vtutf8.c
1.Review the comments;
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / Vtutf8.c
1 /** @file
2 Implementation of translation upon VT-UTF8.
3
4 Copyright (c) 2006, Intel Corporation. <BR>
5 All rights reserved. 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 if ((Temp & 0xc0) == 0x80) {
129
130 Utf8Char->Utf8_2[0] = Temp;
131
132 FetchFlag = FALSE;
133
134 } else {
135
136 *ValidBytes = 0;
137 }
138 break;
139
140 case 3:
141 if ((Temp & 0xc0) == 0x80) {
142
143 Utf8Char->Utf8_3[2 - Index] = Temp;
144 Index++;
145 if (Index == 3) {
146 FetchFlag = FALSE;
147 }
148 } else {
149
150 *ValidBytes = 0;
151 Index = 0;
152 }
153 break;
154
155 default:
156 break;
157 }
158
159 if (!FetchFlag) {
160 break;
161 }
162 }
163
164 return ;
165 }
166
167 /**
168 Translate VT-UTF8 characters into one Unicode character.
169
170 UTF8 Encoding Table
171 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
172 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
173 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
174 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
175
176
177 @param Utf8Char VT-UTF8 character set needs translating.
178 @param ValidBytes The count of valid VT-UTF8 characters.
179 @param UnicodeChar Returned unicode character.
180
181 **/
182 VOID
183 Utf8ToUnicode (
184 IN UTF8_CHAR Utf8Char,
185 IN UINT8 ValidBytes,
186 OUT CHAR16 *UnicodeChar
187 )
188 {
189 UINT8 UnicodeByte0;
190 UINT8 UnicodeByte1;
191 UINT8 Byte0;
192 UINT8 Byte1;
193 UINT8 Byte2;
194
195 *UnicodeChar = 0;
196
197 //
198 // translate utf8 code to unicode, in terminal standard,
199 // up to 3 bytes utf8 code is supported.
200 //
201 switch (ValidBytes) {
202 case 1:
203 //
204 // one-byte utf8 code
205 //
206 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;
207 break;
208
209 case 2:
210 //
211 // two-byte utf8 code
212 //
213 Byte0 = Utf8Char.Utf8_2[0];
214 Byte1 = Utf8Char.Utf8_2[1];
215
216 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
217 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);
218 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
219 break;
220
221 case 3:
222 //
223 // three-byte utf8 code
224 //
225 Byte0 = Utf8Char.Utf8_3[0];
226 Byte1 = Utf8Char.Utf8_3[1];
227 Byte2 = Utf8Char.Utf8_3[2];
228
229 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
230 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
231 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
232
233 default:
234 break;
235 }
236
237 return ;
238 }
239
240 /**
241 Translate one Unicode character into VT-UTF8 characters.
242
243 UTF8 Encoding Table
244 Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
245 0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
246 8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
247 12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
248
249
250 @param Unicode Unicode character need translating.
251 @param Utf8Char Return VT-UTF8 character set.
252 @param ValidBytes The count of valid VT-UTF8 characters. If
253 ValidBytes is zero, no valid VT-UTF8 returned.
254
255 **/
256 VOID
257 UnicodeToUtf8 (
258 IN CHAR16 Unicode,
259 OUT UTF8_CHAR *Utf8Char,
260 OUT UINT8 *ValidBytes
261 )
262 {
263 UINT8 UnicodeByte0;
264 UINT8 UnicodeByte1;
265 //
266 // translate unicode to utf8 code
267 //
268 UnicodeByte0 = (UINT8) Unicode;
269 UnicodeByte1 = (UINT8) (Unicode >> 8);
270
271 if (Unicode < 0x0080) {
272
273 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);
274 *ValidBytes = 1;
275
276 } else if (Unicode < 0x0800) {
277 //
278 // byte sequence: high -> low
279 // Utf8_2[0], Utf8_2[1]
280 //
281 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
282 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
283
284 *ValidBytes = 2;
285
286 } else {
287 //
288 // byte sequence: high -> low
289 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
290 //
291 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
292 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
293 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
294
295 *ValidBytes = 3;
296 }
297 }
298
299
300 /**
301 Check if input string is valid VT-UTF8 string.
302
303 @param TerminalDevice The terminal device.
304 @param WString The input string.
305
306 @retval EFI_SUCCESS If all input characters are valid.
307
308 **/
309 EFI_STATUS
310 VTUTF8TestString (
311 IN TERMINAL_DEV *TerminalDevice,
312 IN CHAR16 *WString
313 )
314 {
315 //
316 // to utf8, all kind of characters are supported.
317 //
318 return EFI_SUCCESS;
319 }