Coverage Report

Created: 2026-08-31 06:56

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