]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/vtutf8.c
1. Sync the latest network stack. Add NetLibCreateIPv4DPathNode () in netlib library.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / vtutf8.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 vtutf8.c
15
16 Abstract:
17
18
19 Revision History
20 --*/
21
22 #include "Terminal.h"
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 VOID
54 GetOneValidUtf8Char (
55 IN TERMINAL_DEV *Utf8Device,
56 OUT UTF8_CHAR *Utf8Char,
57 OUT UINT8 *ValidBytes
58 )
59 {
60 UINT8 Temp;
61 UINT8 Index;
62 BOOLEAN FetchFlag;
63
64 Temp = 0;
65 Index = 0;
66 FetchFlag = TRUE;
67
68 //
69 // if no valid Utf8 char is found in the RawFiFo,
70 // then *ValidBytes will be zero.
71 //
72 *ValidBytes = 0;
73
74 while (!IsRawFiFoEmpty (Utf8Device)) {
75
76 RawFiFoRemoveOneKey (Utf8Device, &Temp);
77
78 switch (*ValidBytes) {
79
80 case 0:
81 if ((Temp & 0x80) == 0) {
82 //
83 // one-byte utf8 char
84 //
85 *ValidBytes = 1;
86
87 Utf8Char->Utf8_1 = Temp;
88
89 FetchFlag = FALSE;
90
91 } else if ((Temp & 0xe0) == 0xc0) {
92 //
93 // two-byte utf8 char
94 //
95 *ValidBytes = 2;
96
97 Utf8Char->Utf8_2[1] = Temp;
98
99 } else if ((Temp & 0xf0) == 0xe0) {
100 //
101 // three-byte utf8 char
102 //
103 *ValidBytes = 3;
104
105 Utf8Char->Utf8_3[2] = Temp;
106
107 Index++;
108
109 } else {
110 //
111 // reset *ValidBytes to zero, let valid utf8 char search restart
112 //
113 *ValidBytes = 0;
114 }
115
116 break;
117
118 case 2:
119 if ((Temp & 0xc0) == 0x80) {
120
121 Utf8Char->Utf8_2[0] = Temp;
122
123 FetchFlag = FALSE;
124
125 } else {
126
127 *ValidBytes = 0;
128 }
129 break;
130
131 case 3:
132 if ((Temp & 0xc0) == 0x80) {
133
134 Utf8Char->Utf8_3[2 - Index] = Temp;
135 Index++;
136 if (Index == 3) {
137 FetchFlag = FALSE;
138 }
139 } else {
140
141 *ValidBytes = 0;
142 Index = 0;
143 }
144 break;
145
146 default:
147 break;
148 }
149
150 if (!FetchFlag) {
151 break;
152 }
153 }
154
155 return ;
156 }
157
158 VOID
159 Utf8ToUnicode (
160 IN UTF8_CHAR Utf8Char,
161 IN UINT8 ValidBytes,
162 OUT CHAR16 *UnicodeChar
163 )
164 {
165 UINT8 UnicodeByte0;
166 UINT8 UnicodeByte1;
167 UINT8 Byte0;
168 UINT8 Byte1;
169 UINT8 Byte2;
170
171 *UnicodeChar = 0;
172
173 //
174 // translate utf8 code to unicode, in terminal standard,
175 // up to 3 bytes utf8 code is supported.
176 //
177 switch (ValidBytes) {
178 case 1:
179 //
180 // one-byte utf8 code
181 //
182 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;
183 break;
184
185 case 2:
186 //
187 // two-byte utf8 code
188 //
189 Byte0 = Utf8Char.Utf8_2[0];
190 Byte1 = Utf8Char.Utf8_2[1];
191
192 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
193 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);
194 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
195 break;
196
197 case 3:
198 //
199 // three-byte utf8 code
200 //
201 Byte0 = Utf8Char.Utf8_3[0];
202 Byte1 = Utf8Char.Utf8_3[1];
203 Byte2 = Utf8Char.Utf8_3[2];
204
205 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
206 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
207 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
208
209 default:
210 break;
211 }
212
213 return ;
214 }
215
216 VOID
217 UnicodeToUtf8 (
218 IN CHAR16 Unicode,
219 OUT UTF8_CHAR *Utf8Char,
220 OUT UINT8 *ValidBytes
221 )
222 {
223 UINT8 UnicodeByte0;
224 UINT8 UnicodeByte1;
225 //
226 // translate unicode to utf8 code
227 //
228 UnicodeByte0 = (UINT8) Unicode;
229 UnicodeByte1 = (UINT8) (Unicode >> 8);
230
231 if (Unicode < 0x0080) {
232
233 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);
234 *ValidBytes = 1;
235
236 } else if (Unicode < 0x0800) {
237 //
238 // byte sequence: high -> low
239 // Utf8_2[0], Utf8_2[1]
240 //
241 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
242 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
243
244 *ValidBytes = 2;
245
246 } else {
247 //
248 // byte sequence: high -> low
249 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
250 //
251 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
252 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
253 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
254
255 *ValidBytes = 3;
256 }
257 }
258
259 EFI_STATUS
260 VTUTF8TestString (
261 IN TERMINAL_DEV *TerminalDevice,
262 IN CHAR16 *WString
263 )
264 {
265 //
266 // to utf8, all kind of characters are supported.
267 //
268 return EFI_SUCCESS;
269 }