]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/TerminalDxe/vtutf8.c
apply for doxgen format.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / vtutf8.c
1 /**@file
2 Implementation translation among different code tyies.
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 VOID
18 VTUTF8RawDataToUnicode (
19 IN TERMINAL_DEV *TerminalDevice
20 )
21 {
22 UTF8_CHAR Utf8Char;
23 UINT8 ValidBytes;
24 UINT16 UnicodeChar;
25
26 ValidBytes = 0;
27 //
28 // pop the raw data out from the raw fifo,
29 // and translate it into unicode, then push
30 // the unicode into unicode fifo, until the raw fifo is empty.
31 //
32 while (!IsRawFiFoEmpty (TerminalDevice)) {
33
34 GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);
35
36 if (ValidBytes < 1 || ValidBytes > 3) {
37 continue;
38 }
39
40 Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *) &UnicodeChar);
41
42 UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);
43 }
44 }
45
46 VOID
47 GetOneValidUtf8Char (
48 IN TERMINAL_DEV *Utf8Device,
49 OUT UTF8_CHAR *Utf8Char,
50 OUT UINT8 *ValidBytes
51 )
52 {
53 UINT8 Temp;
54 UINT8 Index;
55 BOOLEAN FetchFlag;
56
57 Temp = 0;
58 Index = 0;
59 FetchFlag = TRUE;
60
61 //
62 // if no valid Utf8 char is found in the RawFiFo,
63 // then *ValidBytes will be zero.
64 //
65 *ValidBytes = 0;
66
67 while (!IsRawFiFoEmpty (Utf8Device)) {
68
69 RawFiFoRemoveOneKey (Utf8Device, &Temp);
70
71 switch (*ValidBytes) {
72
73 case 0:
74 if ((Temp & 0x80) == 0) {
75 //
76 // one-byte utf8 char
77 //
78 *ValidBytes = 1;
79
80 Utf8Char->Utf8_1 = Temp;
81
82 FetchFlag = FALSE;
83
84 } else if ((Temp & 0xe0) == 0xc0) {
85 //
86 // two-byte utf8 char
87 //
88 *ValidBytes = 2;
89
90 Utf8Char->Utf8_2[1] = Temp;
91
92 } else if ((Temp & 0xf0) == 0xe0) {
93 //
94 // three-byte utf8 char
95 //
96 *ValidBytes = 3;
97
98 Utf8Char->Utf8_3[2] = Temp;
99
100 Index++;
101
102 } else {
103 //
104 // reset *ValidBytes to zero, let valid utf8 char search restart
105 //
106 *ValidBytes = 0;
107 }
108
109 break;
110
111 case 2:
112 if ((Temp & 0xc0) == 0x80) {
113
114 Utf8Char->Utf8_2[0] = Temp;
115
116 FetchFlag = FALSE;
117
118 } else {
119
120 *ValidBytes = 0;
121 }
122 break;
123
124 case 3:
125 if ((Temp & 0xc0) == 0x80) {
126
127 Utf8Char->Utf8_3[2 - Index] = Temp;
128 Index++;
129 if (Index == 3) {
130 FetchFlag = FALSE;
131 }
132 } else {
133
134 *ValidBytes = 0;
135 Index = 0;
136 }
137 break;
138
139 default:
140 break;
141 }
142
143 if (!FetchFlag) {
144 break;
145 }
146 }
147
148 return ;
149 }
150
151 VOID
152 Utf8ToUnicode (
153 IN UTF8_CHAR Utf8Char,
154 IN UINT8 ValidBytes,
155 OUT CHAR16 *UnicodeChar
156 )
157 {
158 UINT8 UnicodeByte0;
159 UINT8 UnicodeByte1;
160 UINT8 Byte0;
161 UINT8 Byte1;
162 UINT8 Byte2;
163
164 *UnicodeChar = 0;
165
166 //
167 // translate utf8 code to unicode, in terminal standard,
168 // up to 3 bytes utf8 code is supported.
169 //
170 switch (ValidBytes) {
171 case 1:
172 //
173 // one-byte utf8 code
174 //
175 *UnicodeChar = (UINT16) Utf8Char.Utf8_1;
176 break;
177
178 case 2:
179 //
180 // two-byte utf8 code
181 //
182 Byte0 = Utf8Char.Utf8_2[0];
183 Byte1 = Utf8Char.Utf8_2[1];
184
185 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
186 UnicodeByte1 = (UINT8) ((Byte1 >> 2) & 0x07);
187 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
188 break;
189
190 case 3:
191 //
192 // three-byte utf8 code
193 //
194 Byte0 = Utf8Char.Utf8_3[0];
195 Byte1 = Utf8Char.Utf8_3[1];
196 Byte2 = Utf8Char.Utf8_3[2];
197
198 UnicodeByte0 = (UINT8) ((Byte1 << 6) | (Byte0 & 0x3f));
199 UnicodeByte1 = (UINT8) ((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
200 *UnicodeChar = (UINT16) (UnicodeByte0 | (UnicodeByte1 << 8));
201
202 default:
203 break;
204 }
205
206 return ;
207 }
208
209 VOID
210 UnicodeToUtf8 (
211 IN CHAR16 Unicode,
212 OUT UTF8_CHAR *Utf8Char,
213 OUT UINT8 *ValidBytes
214 )
215 {
216 UINT8 UnicodeByte0;
217 UINT8 UnicodeByte1;
218 //
219 // translate unicode to utf8 code
220 //
221 UnicodeByte0 = (UINT8) Unicode;
222 UnicodeByte1 = (UINT8) (Unicode >> 8);
223
224 if (Unicode < 0x0080) {
225
226 Utf8Char->Utf8_1 = (UINT8) (UnicodeByte0 & 0x7f);
227 *ValidBytes = 1;
228
229 } else if (Unicode < 0x0800) {
230 //
231 // byte sequence: high -> low
232 // Utf8_2[0], Utf8_2[1]
233 //
234 Utf8Char->Utf8_2[1] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
235 Utf8Char->Utf8_2[0] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
236
237 *ValidBytes = 2;
238
239 } else {
240 //
241 // byte sequence: high -> low
242 // Utf8_3[0], Utf8_3[1], Utf8_3[2]
243 //
244 Utf8Char->Utf8_3[2] = (UINT8) ((UnicodeByte0 & 0x3f) + 0x80);
245 Utf8Char->Utf8_3[1] = (UINT8) ((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
246 Utf8Char->Utf8_3[0] = (UINT8) (((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
247
248 *ValidBytes = 3;
249 }
250 }
251
252 EFI_STATUS
253 VTUTF8TestString (
254 IN TERMINAL_DEV *TerminalDevice,
255 IN CHAR16 *WString
256 )
257 {
258 //
259 // to utf8, all kind of characters are supported.
260 //
261 return EFI_SUCCESS;
262 }