Coverage Report

Created: 2026-09-14 06:53

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