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
9void CFX_UTF8Decoder::Clear() {
10  m_Buffer.Clear();
11  m_PendingBytes = 0;
12}
13
14void CFX_UTF8Decoder::AppendCodePoint(uint32_t ch) {
15  m_Buffer.AppendChar(static_cast<wchar_t>(ch));
16}
17
18void CFX_UTF8Decoder::Input(uint8_t byte) {
19  if (byte < 0x80) {
20    m_PendingBytes = 0;
21    m_Buffer.AppendChar(byte);
22  } else if (byte < 0xc0) {
23    if (m_PendingBytes == 0) {
24      return;
25    }
26    m_PendingBytes--;
27    m_PendingChar |= (byte & 0x3f) << (m_PendingBytes * 6);
28    if (m_PendingBytes == 0) {
29      AppendCodePoint(m_PendingChar);
30    }
31  } else if (byte < 0xe0) {
32    m_PendingBytes = 1;
33    m_PendingChar = (byte & 0x1f) << 6;
34  } else if (byte < 0xf0) {
35    m_PendingBytes = 2;
36    m_PendingChar = (byte & 0x0f) << 12;
37  } else if (byte < 0xf8) {
38    m_PendingBytes = 3;
39    m_PendingChar = (byte & 0x07) << 18;
40  } else if (byte < 0xfc) {
41    m_PendingBytes = 4;
42    m_PendingChar = (byte & 0x03) << 24;
43  } else if (byte < 0xfe) {
44    m_PendingBytes = 5;
45    m_PendingChar = (byte & 0x01) << 30;
46  }
47}
48