Coverage Report

Created: 2026-05-30 06:57

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<bool(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 = {}, bool handleValidCharacters = false)
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 (0xD800 <= ch && ch <= 0xDFFF)
50
0
    {
51
      // RFC 3629 ยง3 explicitly forbids surrogate-half values in UTF-8
52
0
      ch = 0xFFFF;
53
0
    }
54
0
    else if (((0x20 <= ch && ch <= 0xD7FF) &&
55
0
        specials[0] != ch &&
56
0
        specials[1] != ch &&
57
0
        specials[2] != ch &&
58
0
        specials[3] != ch) ||
59
0
      (0x9 == ch || 0xA == ch || 0xD == ch) ||
60
0
      (0xE000 <= ch && ch <= 0xFFFD) ||
61
0
      (0x10000 <= ch && ch <= 0x10FFFF))
62
0
    {
63
0
      LogString escaped;
64
0
      if (handleValidCharacters && handler && handler(escaped, ch))
65
0
      {
66
0
        if (start != lastCodePoint)
67
0
          buf.append(start, lastCodePoint);
68
0
        buf.append(escaped);
69
0
        start = nextCodePoint;
70
0
      }
71
0
      continue;
72
0
    }
73
74
0
    if (start != lastCodePoint)
75
0
      buf.append(start, lastCodePoint);
76
0
    start = nextCodePoint;
77
0
    switch (ch)
78
0
    {
79
0
      case 0: // Do not output a NUL character
80
0
        break;
81
0
      case 0x22:
82
0
        buf.append(LOG4CXX_STR("&quot;"));
83
0
        break;
84
85
0
      case 0x26:
86
0
        buf.append(LOG4CXX_STR("&amp;"));
87
0
        break;
88
89
0
      case 0x3C:
90
0
        buf.append(LOG4CXX_STR("&lt;"));
91
0
        break;
92
93
0
      case 0x3E:
94
0
        buf.append(LOG4CXX_STR("&gt;"));
95
0
        break;
96
97
0
      case 0xFFFF: // invalid sequence
98
0
        Transform::appendCharacterReference(buf, 0xFFFD); // The Unicode replacement character
99
0
        break;
100
101
0
      default:
102
0
        if (handler && !handler(buf, ch))
103
0
          Transform::appendCharacterReference(buf, ch);
104
0
        break;
105
0
    }
106
0
  }
107
0
  buf.append(start, input.end());
108
0
}
109
110
bool appendCharacterReferenceHandler(LogString& buf, int ch)
111
0
{
112
0
  Transform::appendCharacterReference(buf, ch);
113
0
  return true;
114
0
}
115
116
bool appendAttributeCharacterReference(LogString& buf, int ch)
117
0
{
118
0
  if (0x27 == ch || 0x9 == ch || 0xA == ch || 0xD == ch)
119
0
  {
120
0
    Transform::appendCharacterReference(buf, ch);
121
0
    return true;
122
0
  }
123
124
0
  return false;
125
0
}
126
127
} // namespace
128
129
void Transform::appendEscapingCDATA(
130
  LogString& buf, const LogString& input)
131
0
{
132
0
  static const LogString CDATA_END(LOG4CXX_STR("]]>"));
133
0
  const LogString::size_type CDATA_END_LEN = 3;
134
0
  static const LogString CDATA_EMBEDED_END(LOG4CXX_STR("]]&gt;<![CDATA["));
135
0
  auto start = input.begin();
136
0
  for (auto nextCodePoint = start; input.end() != nextCodePoint; )
137
0
  {
138
0
    bool cdataEnd = false;
139
0
    auto lastCodePoint = nextCodePoint;
140
0
    auto ch = Transcoder::decode(input, nextCodePoint);
141
0
    if (nextCodePoint == lastCodePoint) // failed to decode input?
142
0
    {
143
0
      nextCodePoint = input.end();
144
0
      ch = 0xFFFD; // The Unicode replacement character
145
0
    }
146
0
    else if (CDATA_END[0] == ch && input.end() != nextCodePoint)
147
0
    {
148
0
      lastCodePoint = nextCodePoint;
149
0
      if (CDATA_END[1] != Transcoder::decode(input, nextCodePoint) ||
150
0
        input.end() == nextCodePoint ||
151
0
        CDATA_END[2] != Transcoder::decode(input, nextCodePoint))
152
0
      {
153
0
        nextCodePoint = lastCodePoint;
154
0
        continue;
155
0
      }
156
0
      lastCodePoint = nextCodePoint;
157
0
      cdataEnd = true;
158
0
    }
159
0
    else if ((0x20 <= ch && ch <= 0xD7FF) ||
160
0
        (0x9 == ch || 0xA == ch || 0xD == ch) ||
161
0
        (0xE000 <= ch && ch <= 0xFFFD) ||
162
0
        (0x10000 <= ch && ch <= 0x10FFFF))
163
0
    {
164
0
      continue;
165
0
    }
166
167
0
    if (start != lastCodePoint)
168
0
      buf.append(start, lastCodePoint);
169
0
    if (cdataEnd)
170
0
      buf.append(CDATA_EMBEDED_END);
171
0
    else if (0 != ch)
172
0
      appendCharacterReference(buf, ch);
173
0
    start = nextCodePoint;
174
0
  }
175
0
  buf.append(start, input.end());
176
0
}
177
178
void Transform::appendCharacterReference(LogString& buf, unsigned int ch)
179
0
{
180
0
  auto toHexDigit = [](int ch) -> int
181
0
  {
182
0
    return (10 <= ch ? (0x61 - 10) : 0x30) + ch;
183
0
  };
184
0
  buf.push_back('&');
185
0
  buf.push_back('#');
186
0
  buf.push_back('x');
187
0
  if (0xFFFFFFF < ch)
188
0
    buf.push_back(toHexDigit((ch & 0xF0000000) >> 28));
189
0
  if (0xFFFFFF < ch)
190
0
    buf.push_back(toHexDigit((ch & 0xF000000) >> 24));
191
0
  if (0xFFFFF < ch)
192
0
    buf.push_back(toHexDigit((ch & 0xF00000) >> 20));
193
0
  if (0xFFFF < ch)
194
0
    buf.push_back(toHexDigit((ch & 0xF0000) >> 16));
195
0
  if (0xFFF < ch)
196
0
    buf.push_back(toHexDigit((ch & 0xF000) >> 12));
197
0
  if (0xFF < ch)
198
0
    buf.push_back(toHexDigit((ch & 0xF00) >> 8));
199
0
  if (0xF < ch)
200
0
    buf.push_back(toHexDigit((ch & 0xF0) >> 4));
201
0
  buf.push_back(toHexDigit(ch & 0xF));
202
0
  buf.push_back(';');
203
0
}
204
205
void Transform::appendEscapingTags(LogString& buf, const LogString& input)
206
0
{
207
0
  appendValidCharacters(buf, input, appendCharacterReferenceHandler);
208
0
}
209
210
void Transform::appendEscapingAttribute(LogString& buf, const LogString& input)
211
0
{
212
0
  appendValidCharacters(buf, input, appendAttributeCharacterReference, true);
213
0
}
214
215
void Transform::appendLegalCharacters(LogString& buf, const LogString& input)
216
0
{
217
0
  appendValidCharacters(buf, input);
218
0
}