Coverage Report

Created: 2026-04-12 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/transform.cpp
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#include <log4cxx/logstring.h>
19
#include <log4cxx/helpers/transform.h>
20
#include <log4cxx/helpers/transcoder.h>
21
#include <log4cxx/helpers/widelife.h>
22
#include <functional>
23
24
using namespace LOG4CXX_NS;
25
using namespace LOG4CXX_NS::helpers;
26
27
namespace
28
{
29
using CharProcessor = std::function<void(LogString&, int)>;
30
31
// Allowable XML 1.0 characters are:
32
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
33
void appendValidCharacters(LogString& buf, const LogString& input, CharProcessor handler = {})
34
0
{
35
0
  static const unsigned int specials[] =
36
0
    { 0x22 /* " */
37
0
    , 0x26 /* & */
38
0
    , 0x3C /* < */
39
0
    , 0x3E /* > */
40
0
    , 0x00
41
0
    };
42
0
  auto start = input.begin();
43
0
  for (auto nextCodePoint = start; input.end() != nextCodePoint; )
44
0
  {
45
0
    auto lastCodePoint = nextCodePoint;
46
0
    auto ch = Transcoder::decode(input, nextCodePoint);
47
0
    if (nextCodePoint == lastCodePoint) // failed to decode input?
48
0
      nextCodePoint = input.end();
49
0
    else if ((0x20 <= ch && ch <= 0xD7FF) &&
50
0
      specials[0] != ch &&
51
0
      specials[1] != ch &&
52
0
      specials[2] != ch &&
53
0
      specials[3] != ch)
54
0
    {
55
0
      continue;
56
0
    }
57
0
    else if ((0x9 == ch || 0xA == ch || 0xD == ch) ||
58
0
        (0xE000 <= ch && ch <= 0xFFFD) ||
59
0
        (0x10000 <= ch && ch <= 0x10FFFF))
60
0
    {
61
0
      continue;
62
0
    }
63
64
0
    if (start != lastCodePoint)
65
0
      buf.append(start, lastCodePoint);
66
0
    start = nextCodePoint;
67
0
    switch (ch)
68
0
    {
69
0
      case 0: // Do not output a NUL character
70
0
        break;
71
0
      case 0x22:
72
0
        buf.append(LOG4CXX_STR("&quot;"));
73
0
        break;
74
75
0
      case 0x26:
76
0
        buf.append(LOG4CXX_STR("&amp;"));
77
0
        break;
78
79
0
      case 0x3C:
80
0
        buf.append(LOG4CXX_STR("&lt;"));
81
0
        break;
82
83
0
      case 0x3E:
84
0
        buf.append(LOG4CXX_STR("&gt;"));
85
0
        break;
86
87
0
      case 0xFFFF: // invalid sequence
88
0
        Transform::appendCharacterReference(buf, 0xFFFD); // The Unicode replacement character
89
0
        break;
90
91
0
      default:
92
0
        if (handler)
93
0
          handler(buf, ch);
94
0
        break;
95
0
    }
96
0
  }
97
0
  buf.append(start, input.end());
98
0
}
99
100
} // namespace
101
102
void Transform::appendEscapingCDATA(
103
  LogString& buf, const LogString& input)
104
0
{
105
0
  static const LogString CDATA_END(LOG4CXX_STR("]]>"));
106
0
  const LogString::size_type CDATA_END_LEN = 3;
107
0
  static const LogString CDATA_EMBEDED_END(LOG4CXX_STR("]]&gt;<![CDATA["));
108
0
  auto start = input.begin();
109
0
  for (auto nextCodePoint = start; input.end() != nextCodePoint; )
110
0
  {
111
0
    bool cdataEnd = false;
112
0
    auto lastCodePoint = nextCodePoint;
113
0
    auto ch = Transcoder::decode(input, nextCodePoint);
114
0
    if (nextCodePoint == lastCodePoint) // failed to decode input?
115
0
    {
116
0
      nextCodePoint = input.end();
117
0
      ch = 0xFFFD; // The Unicode replacement character
118
0
    }
119
0
    else if (CDATA_END[0] == ch && input.end() != nextCodePoint)
120
0
    {
121
0
      lastCodePoint = nextCodePoint;
122
0
      if (CDATA_END[1] != Transcoder::decode(input, nextCodePoint) ||
123
0
        input.end() == nextCodePoint ||
124
0
        CDATA_END[2] != Transcoder::decode(input, nextCodePoint))
125
0
      {
126
0
        nextCodePoint = lastCodePoint;
127
0
        continue;
128
0
      }
129
0
      lastCodePoint = nextCodePoint;
130
0
      cdataEnd = true;
131
0
    }
132
0
    else if ((0x20 <= ch && ch <= 0xD7FF) ||
133
0
        (0x9 == ch || 0xA == ch || 0xD == ch) ||
134
0
        (0xE000 <= ch && ch <= 0xFFFD) ||
135
0
        (0x10000 <= ch && ch <= 0x10FFFF))
136
0
    {
137
0
      continue;
138
0
    }
139
140
0
    if (start != lastCodePoint)
141
0
      buf.append(start, lastCodePoint);
142
0
    if (cdataEnd)
143
0
      buf.append(CDATA_EMBEDED_END);
144
0
    else if (0 != ch)
145
0
      appendCharacterReference(buf, ch);
146
0
    start = nextCodePoint;
147
0
  }
148
0
  buf.append(start, input.end());
149
0
}
150
151
void Transform::appendCharacterReference(LogString& buf, unsigned int ch)
152
0
{
153
0
  auto toHexDigit = [](int ch) -> int
154
0
  {
155
0
    return (10 <= ch ? (0x61 - 10) : 0x30) + ch;
156
0
  };
157
0
  buf.push_back('&');
158
0
  buf.push_back('#');
159
0
  buf.push_back('x');
160
0
  if (0xFFFFFFF < ch)
161
0
    buf.push_back(toHexDigit((ch & 0xF0000000) >> 28));
162
0
  if (0xFFFFFF < ch)
163
0
    buf.push_back(toHexDigit((ch & 0xF000000) >> 24));
164
0
  if (0xFFFFF < ch)
165
0
    buf.push_back(toHexDigit((ch & 0xF00000) >> 20));
166
0
  if (0xFFFF < ch)
167
0
    buf.push_back(toHexDigit((ch & 0xF0000) >> 16));
168
0
  if (0xFFF < ch)
169
0
    buf.push_back(toHexDigit((ch & 0xF000) >> 12));
170
0
  if (0xFF < ch)
171
0
    buf.push_back(toHexDigit((ch & 0xF00) >> 8));
172
0
  if (0xF < ch)
173
0
    buf.push_back(toHexDigit((ch & 0xF0) >> 4));
174
0
  buf.push_back(toHexDigit(ch & 0xF));
175
0
  buf.push_back(';');
176
0
}
177
178
void Transform::appendEscapingTags(LogString& buf, const LogString& input)
179
0
{
180
0
  appendValidCharacters(buf, input, appendCharacterReference);
181
0
}
182
183
void Transform::appendLegalCharacters(LogString& buf, const LogString& input)
184
0
{
185
0
  appendValidCharacters(buf, input);
186
0
}