• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
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 #include <utility>
10 
CFX_UTF8Decoder(ByteStringView input)11 CFX_UTF8Decoder::CFX_UTF8Decoder(ByteStringView input) {
12   for (char c : input) {
13     ProcessByte(c);
14   }
15 }
16 
17 CFX_UTF8Decoder::~CFX_UTF8Decoder() = default;
18 
TakeResult()19 WideString CFX_UTF8Decoder::TakeResult() {
20   return std::move(m_Buffer);
21 }
22 
AppendCodePoint(uint32_t ch)23 void CFX_UTF8Decoder::AppendCodePoint(uint32_t ch) {
24   m_Buffer += static_cast<wchar_t>(ch);
25 }
26 
ProcessByte(uint8_t byte)27 void CFX_UTF8Decoder::ProcessByte(uint8_t byte) {
28   if (byte < 0x80) {
29     m_PendingBytes = 0;
30     AppendCodePoint(byte);
31   } else if (byte < 0xc0) {
32     if (m_PendingBytes == 0) {
33       return;
34     }
35     m_PendingBytes--;
36     m_PendingChar |= (byte & 0x3f) << (m_PendingBytes * 6);
37     if (m_PendingBytes == 0) {
38       AppendCodePoint(m_PendingChar);
39     }
40   } else if (byte < 0xe0) {
41     m_PendingBytes = 1;
42     m_PendingChar = (byte & 0x1f) << 6;
43   } else if (byte < 0xf0) {
44     m_PendingBytes = 2;
45     m_PendingChar = (byte & 0x0f) << 12;
46   } else if (byte < 0xf8) {
47     m_PendingBytes = 3;
48     m_PendingChar = (byte & 0x07) << 18;
49   } else if (byte < 0xfc) {
50     m_PendingBytes = 4;
51     m_PendingChar = (byte & 0x03) << 24;
52   } else if (byte < 0xfe) {
53     m_PendingBytes = 5;
54     m_PendingChar = (byte & 0x01) << 30;
55   } else {
56     m_PendingBytes = 0;
57   }
58 }
59