1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcrt/cfx_utf8decoder.h"
8 
9 CFX_UTF8Decoder::CFX_UTF8Decoder() = default;
10 
11 CFX_UTF8Decoder::~CFX_UTF8Decoder() = default;
12 
AppendCodePoint(uint32_t ch)13 void CFX_UTF8Decoder::AppendCodePoint(uint32_t ch) {
14   m_Buffer.AppendChar(static_cast<wchar_t>(ch));
15 }
16 
Input(uint8_t byte)17 void CFX_UTF8Decoder::Input(uint8_t byte) {
18   if (byte < 0x80) {
19     m_PendingBytes = 0;
20     m_Buffer.AppendChar(byte);
21   } else if (byte < 0xc0) {
22     if (m_PendingBytes == 0) {
23       return;
24     }
25     m_PendingBytes--;
26     m_PendingChar |= (byte & 0x3f) << (m_PendingBytes * 6);
27     if (m_PendingBytes == 0) {
28       AppendCodePoint(m_PendingChar);
29     }
30   } else if (byte < 0xe0) {
31     m_PendingBytes = 1;
32     m_PendingChar = (byte & 0x1f) << 6;
33   } else if (byte < 0xf0) {
34     m_PendingBytes = 2;
35     m_PendingChar = (byte & 0x0f) << 12;
36   } else if (byte < 0xf8) {
37     m_PendingBytes = 3;
38     m_PendingChar = (byte & 0x07) << 18;
39   } else if (byte < 0xfc) {
40     m_PendingBytes = 4;
41     m_PendingChar = (byte & 0x03) << 24;
42   } else if (byte < 0xfe) {
43     m_PendingBytes = 5;
44     m_PendingChar = (byte & 0x01) << 30;
45   } else {
46     m_PendingBytes = 0;
47   }
48 }
49