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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/** @file
Implementation of translation upon VT-UTF8.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Terminal.h"
/**
Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,
and insert them into Unicode FIFO.
@param TerminalDevice The terminal device.
**/
VOID
VTUTF8RawDataToUnicode (
IN TERMINAL_DEV *TerminalDevice
)
{
UTF8_CHAR Utf8Char;
UINT8 ValidBytes;
UINT16 UnicodeChar;
ValidBytes = 0;
//
// pop the raw data out from the raw fifo,
// and translate it into unicode, then push
// the unicode into unicode fifo, until the raw fifo is empty.
//
while (!IsRawFiFoEmpty (TerminalDevice) && !IsUnicodeFiFoFull (TerminalDevice)) {
GetOneValidUtf8Char (TerminalDevice, &Utf8Char, &ValidBytes);
if ((ValidBytes < 1) || (ValidBytes > 3)) {
continue;
}
Utf8ToUnicode (Utf8Char, ValidBytes, (CHAR16 *)&UnicodeChar);
UnicodeFiFoInsertOneKey (TerminalDevice, UnicodeChar);
}
}
/**
Get one valid VT-UTF8 characters set from Raw Data FIFO.
@param Utf8Device The terminal device.
@param Utf8Char Returned valid VT-UTF8 characters set.
@param ValidBytes The count of returned VT-VTF8 characters.
If ValidBytes is zero, no valid VT-UTF8 returned.
**/
VOID
GetOneValidUtf8Char (
IN TERMINAL_DEV *Utf8Device,
OUT UTF8_CHAR *Utf8Char,
OUT UINT8 *ValidBytes
)
{
UINT8 Temp;
UINT8 Index;
BOOLEAN FetchFlag;
Temp = 0;
Index = 0;
FetchFlag = TRUE;
//
// if no valid Utf8 char is found in the RawFiFo,
// then *ValidBytes will be zero.
//
*ValidBytes = 0;
while (!IsRawFiFoEmpty (Utf8Device)) {
RawFiFoRemoveOneKey (Utf8Device, &Temp);
switch (*ValidBytes) {
case 0:
if ((Temp & 0x80) == 0) {
//
// one-byte utf8 char
//
*ValidBytes = 1;
Utf8Char->Utf8_1 = Temp;
FetchFlag = FALSE;
} else if ((Temp & 0xe0) == 0xc0) {
//
// two-byte utf8 char
//
*ValidBytes = 2;
Utf8Char->Utf8_2[1] = Temp;
} else if ((Temp & 0xf0) == 0xe0) {
//
// three-byte utf8 char
//
*ValidBytes = 3;
Utf8Char->Utf8_3[2] = Temp;
Index++;
} else {
//
// reset *ValidBytes to zero, let valid utf8 char search restart
//
*ValidBytes = 0;
}
break;
case 2:
//
// two-byte utf8 char go on
//
if ((Temp & 0xc0) == 0x80) {
Utf8Char->Utf8_2[0] = Temp;
FetchFlag = FALSE;
} else {
*ValidBytes = 0;
}
break;
case 3:
//
// three-byte utf8 char go on
//
if ((Temp & 0xc0) == 0x80) {
if (Index == 1) {
Utf8Char->Utf8_3[1] = Temp;
Index++;
} else {
Utf8Char->Utf8_3[0] = Temp;
FetchFlag = FALSE;
}
} else {
//
// reset *ValidBytes and Index to zero, let valid utf8 char search restart
//
*ValidBytes = 0;
Index = 0;
}
break;
default:
break;
}
if (!FetchFlag) {
break;
}
}
return;
}
/**
Translate VT-UTF8 characters into one Unicode character.
UTF8 Encoding Table
Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
@param Utf8Char VT-UTF8 character set needs translating.
@param ValidBytes The count of valid VT-UTF8 characters.
@param UnicodeChar Returned unicode character.
**/
VOID
Utf8ToUnicode (
IN UTF8_CHAR Utf8Char,
IN UINT8 ValidBytes,
OUT CHAR16 *UnicodeChar
)
{
UINT8 UnicodeByte0;
UINT8 UnicodeByte1;
UINT8 Byte0;
UINT8 Byte1;
UINT8 Byte2;
*UnicodeChar = 0;
//
// translate utf8 code to unicode, in terminal standard,
// up to 3 bytes utf8 code is supported.
//
switch (ValidBytes) {
case 1:
//
// one-byte utf8 code
//
*UnicodeChar = (UINT16)Utf8Char.Utf8_1;
break;
case 2:
//
// two-byte utf8 code
//
Byte0 = Utf8Char.Utf8_2[0];
Byte1 = Utf8Char.Utf8_2[1];
UnicodeByte0 = (UINT8)((Byte1 << 6) | (Byte0 & 0x3f));
UnicodeByte1 = (UINT8)((Byte1 >> 2) & 0x07);
*UnicodeChar = (UINT16)(UnicodeByte0 | (UnicodeByte1 << 8));
break;
case 3:
//
// three-byte utf8 code
//
Byte0 = Utf8Char.Utf8_3[0];
Byte1 = Utf8Char.Utf8_3[1];
Byte2 = Utf8Char.Utf8_3[2];
UnicodeByte0 = (UINT8)((Byte1 << 6) | (Byte0 & 0x3f));
UnicodeByte1 = (UINT8)((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
*UnicodeChar = (UINT16)(UnicodeByte0 | (UnicodeByte1 << 8));
default:
break;
}
return;
}
/**
Translate one Unicode character into VT-UTF8 characters.
UTF8 Encoding Table
Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
@param Unicode Unicode character need translating.
@param Utf8Char Return VT-UTF8 character set.
@param ValidBytes The count of valid VT-UTF8 characters. If
ValidBytes is zero, no valid VT-UTF8 returned.
**/
VOID
UnicodeToUtf8 (
IN CHAR16 Unicode,
OUT UTF8_CHAR *Utf8Char,
OUT UINT8 *ValidBytes
)
{
UINT8 UnicodeByte0;
UINT8 UnicodeByte1;
//
// translate unicode to utf8 code
//
UnicodeByte0 = (UINT8)Unicode;
UnicodeByte1 = (UINT8)(Unicode >> 8);
if (Unicode < 0x0080) {
Utf8Char->Utf8_1 = (UINT8)(UnicodeByte0 & 0x7f);
*ValidBytes = 1;
} else if (Unicode < 0x0800) {
//
// byte sequence: high -> low
// Utf8_2[0], Utf8_2[1]
//
Utf8Char->Utf8_2[1] = (UINT8)((UnicodeByte0 & 0x3f) + 0x80);
Utf8Char->Utf8_2[0] = (UINT8)((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x1f) + 0xc0);
*ValidBytes = 2;
} else {
//
// byte sequence: high -> low
// Utf8_3[0], Utf8_3[1], Utf8_3[2]
//
Utf8Char->Utf8_3[2] = (UINT8)((UnicodeByte0 & 0x3f) + 0x80);
Utf8Char->Utf8_3[1] = (UINT8)((((UnicodeByte1 << 2) + (UnicodeByte0 >> 6)) & 0x3f) + 0x80);
Utf8Char->Utf8_3[0] = (UINT8)(((UnicodeByte1 >> 4) & 0x0f) + 0xe0);
*ValidBytes = 3;
}
}
/**
Check if input string is valid VT-UTF8 string.
@param TerminalDevice The terminal device.
@param WString The input string.
@retval EFI_SUCCESS If all input characters are valid.
**/
EFI_STATUS
VTUTF8TestString (
IN TERMINAL_DEV *TerminalDevice,
IN CHAR16 *WString
)
{
//
// to utf8, all kind of characters are supported.
//
return EFI_SUCCESS;
}
|