Coverage Report

Created: 2026-12-01 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/properties.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/properties.h>
20
#include <log4cxx/helpers/exception.h>
21
#include <log4cxx/helpers/bytebuffer.h>
22
#include <cctype>
23
#include <algorithm>
24
25
using namespace LOG4CXX_NS;
26
using namespace LOG4CXX_NS::helpers;
27
28
template <typename Char>
29
struct Properties::Less
30
{
31
  bool operator()(const std::basic_string<Char>& str1, const std::basic_string<Char>& str2) const
32
13.4M
  {
33
13.4M
    return std::lexicographical_compare
34
13.4M
      ( str1.begin(), str1.end()
35
13.4M
      , str2.begin(), str2.end()
36
13.4M
      , [](Char c1, Char c2)
37
268M
        {
38
268M
          return std::tolower(static_cast<unsigned char>(c1)) < std::tolower(static_cast<unsigned char>(c2));
39
268M
        }
log4cxx::helpers::Properties::Less<wchar_t>::operator()(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) const::{lambda(wchar_t, wchar_t)#1}::operator()(wchar_t, wchar_t) const
Line
Count
Source
37
151M
        {
38
151M
          return std::tolower(static_cast<unsigned char>(c1)) < std::tolower(static_cast<unsigned char>(c2));
39
151M
        }
log4cxx::helpers::Properties::Less<char>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const::{lambda(char, char)#1}::operator()(char, char) const
Line
Count
Source
37
116M
        {
38
116M
          return std::tolower(static_cast<unsigned char>(c1)) < std::tolower(static_cast<unsigned char>(c2));
39
116M
        }
40
13.4M
      );
41
13.4M
  }
log4cxx::helpers::Properties::Less<wchar_t>::operator()(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) const
Line
Count
Source
32
7.55M
  {
33
7.55M
    return std::lexicographical_compare
34
7.55M
      ( str1.begin(), str1.end()
35
7.55M
      , str2.begin(), str2.end()
36
7.55M
      , [](Char c1, Char c2)
37
7.55M
        {
38
7.55M
          return std::tolower(static_cast<unsigned char>(c1)) < std::tolower(static_cast<unsigned char>(c2));
39
7.55M
        }
40
7.55M
      );
41
7.55M
  }
log4cxx::helpers::Properties::Less<char>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Line
Count
Source
32
5.89M
  {
33
5.89M
    return std::lexicographical_compare
34
5.89M
      ( str1.begin(), str1.end()
35
5.89M
      , str2.begin(), str2.end()
36
5.89M
      , [](Char c1, Char c2)
37
5.89M
        {
38
5.89M
          return std::tolower(static_cast<unsigned char>(c1)) < std::tolower(static_cast<unsigned char>(c2));
39
5.89M
        }
40
5.89M
      );
41
5.89M
  }
42
};
43
44
class ISO_8859_1_CharIterator
45
{
46
  enum { BUFSIZE = 4096 };
47
  InputStreamPtr m_in;
48
  char           m_storage[BUFSIZE];
49
  ByteBuffer     m_buffer;
50
51
public: // ...structors
52
  ISO_8859_1_CharIterator(const InputStreamPtr& in)
53
5.39k
    : m_in(in)
54
5.39k
    , m_buffer(m_storage, BUFSIZE)
55
5.39k
  {
56
5.39k
    m_in->read(m_buffer);
57
5.39k
    m_buffer.flip(); // Change m_buffer to a collection of bytes to be processed
58
5.39k
  }
59
60
public: // Accessors
61
  /// The current item.
62
  /// @pre !off()
63
  uint32_t item() const
64
35.2M
  {
65
35.2M
    return static_cast<uint32_t>(*m_buffer.current());
66
35.2M
  }
67
68
  /// Is this before the first item or after the last item?
69
  bool off() const
70
35.3M
  {
71
35.3M
    return 0 == m_buffer.remaining();
72
35.3M
  }
73
74
public: // Modifiers
75
  /// Advance the iterator
76
  /// @pre !off()
77
  void forth()
78
35.2M
  {
79
35.2M
    m_buffer.increment_position(1);
80
35.2M
    if (0 == m_buffer.remaining())
81
12.1k
    {
82
12.1k
      m_buffer.clear();
83
12.1k
      m_in->read(m_buffer);
84
12.1k
      m_buffer.flip(); // Change m_buffer to a collection of bytes to be processed
85
12.1k
    }
86
35.2M
  }
87
};
88
89
class PropertyParser
90
{
91
    ISO_8859_1_CharIterator m_in;
92
  public:
93
    PropertyParser(const InputStreamPtr& in)
94
5.39k
      : m_in(in)
95
5.39k
    {}
96
97
    void loadInto(Properties& properties)
98
5.39k
    {
99
5.39k
      LogString key, element;
100
5.39k
      LexemType lexemType = BEGIN;
101
5.39k
      logchar c;
102
5.39k
      bool finished = false;
103
104
5.39k
      if (!get(c))
105
0
      {
106
0
        return;
107
0
      }
108
109
35.7M
      while (!finished)
110
35.7M
      {
111
35.7M
        if (1000 < key.size())
112
6
          throw IOException("property key exceeds 1000 characters");
113
35.7M
        if (10000 < element.size())
114
8
          throw IOException("property value exceeds 10000 characters");
115
35.7M
        switch (lexemType)
116
35.7M
        {
117
490k
          case BEGIN:
118
490k
            switch (c)
119
490k
            {
120
40.7k
              case 0x20: // ' '
121
42.5k
              case 0x09: // '\t'
122
76.8k
              case 0x0A: // '\n'
123
198k
              case 0x0D: // '\r'
124
198k
                if (!get(c))
125
113
                {
126
113
                  finished = true;
127
113
                }
128
129
198k
                break;
130
131
8.98k
              case 0x23: // '#'
132
9.69k
              case 0x21: // '!'
133
9.69k
                lexemType = COMMENT;
134
135
9.69k
                if (!get(c))
136
5
                {
137
5
                  finished = true;
138
5
                }
139
140
9.69k
                break;
141
142
282k
              default:
143
282k
                lexemType = KEY;
144
282k
                break;
145
490k
            }
146
147
490k
            break;
148
149
7.41M
          case KEY:
150
7.41M
            switch (c)
151
7.41M
            {
152
20.7k
              case 0x5C: // '\\'
153
20.7k
                lexemType = KEY_ESCAPE;
154
155
20.7k
                if (!get(c))
156
29
                {
157
29
                  finished = true;
158
29
                }
159
160
20.7k
                break;
161
162
4.04k
              case 0x09: // '\t'
163
45.1k
              case 0x20: // ' '
164
74.8k
              case 0x3A: // ':'
165
179k
              case 0x3D: // '='
166
179k
                lexemType = DELIMITER;
167
168
179k
                if (!get(c))
169
33
                {
170
33
                  finished = true;
171
33
                }
172
173
179k
                break;
174
175
67.2k
              case 0x0A:
176
102k
              case 0x0D:
177
                // key associated with an empty string element
178
102k
                properties.setProperty(key, LogString());
179
102k
                key.erase(key.begin(), key.end());
180
102k
                lexemType = BEGIN;
181
182
102k
                if (!get(c))
183
323
                {
184
323
                  finished = true;
185
323
                }
186
187
102k
                break;
188
189
7.11M
              default:
190
7.11M
                key.append(1, c);
191
192
7.11M
                if (!get(c))
193
990
                {
194
990
                  finished = true;
195
990
                }
196
197
7.11M
                break;
198
7.41M
            }
199
200
7.41M
            break;
201
202
7.41M
          case KEY_ESCAPE:
203
20.6k
            switch (c)
204
20.6k
            {
205
643
              case 0x74: // 't'
206
643
                key.append(1, 0x09);
207
643
                lexemType = KEY;
208
643
                break;
209
210
694
              case 0x6E: // 'n'
211
694
                key.append(1, 0x0A);
212
694
                lexemType = KEY;
213
694
                break;
214
215
3.15k
              case 0x72: // 'r'
216
3.15k
                key.append(1, 0x0D);
217
3.15k
                lexemType = KEY;
218
3.15k
                break;
219
220
1.62k
              case 0x0A: // '\n'
221
1.62k
                lexemType = KEY_CONTINUE;
222
1.62k
                break;
223
224
4.28k
              case 0x0D: // '\r'
225
4.28k
                lexemType = KEY_CONTINUE2;
226
4.28k
                break;
227
228
10.2k
              default:
229
10.2k
                key.append(1, c);
230
10.2k
                lexemType = KEY;
231
20.6k
            }
232
233
20.6k
            if (!get(c))
234
39
            {
235
39
              finished = true;
236
39
            }
237
238
20.6k
            break;
239
240
8.09k
          case KEY_CONTINUE:
241
8.09k
            switch (c)
242
8.09k
            {
243
1.63k
              case 0x20:  // ' '
244
2.19k
              case 0x09: //  '\t'
245
2.19k
                if (!get(c))
246
2
                {
247
2
                  finished = true;
248
2
                }
249
250
2.19k
                break;
251
252
5.89k
              default:
253
5.89k
                lexemType = KEY;
254
5.89k
                break;
255
8.09k
            }
256
257
8.09k
            break;
258
259
8.09k
          case KEY_CONTINUE2:
260
4.28k
            switch (c)
261
4.28k
            {
262
3.35k
              case 0x0A: // '\n'
263
3.35k
                if (!get(c))
264
1
                {
265
1
                  finished = true;
266
1
                }
267
268
3.35k
                lexemType = KEY_CONTINUE;
269
3.35k
                break;
270
271
926
              default:
272
926
                lexemType = KEY_CONTINUE;
273
926
                break;
274
4.28k
            }
275
276
4.28k
            break;
277
278
197k
          case DELIMITER:
279
197k
            switch (c)
280
197k
            {
281
2.51k
              case 0x09: // '\t'
282
11.4k
              case 0x20: // ' '
283
16.8k
              case 0x3A: // ':'
284
18.0k
              case 0x3D: // '='
285
18.0k
                if (!get(c))
286
19
                {
287
19
                  finished = true;
288
19
                }
289
290
18.0k
                break;
291
292
178k
              default:
293
178k
                lexemType = ELEMENT;
294
178k
                break;
295
197k
            }
296
297
197k
            break;
298
299
27.1M
          case ELEMENT:
300
27.1M
            switch (c)
301
27.1M
            {
302
89.7k
              case 0x5C: // '\\'
303
89.7k
                lexemType = ELEMENT_ESCAPE;
304
305
89.7k
                if (!get(c))
306
60
                {
307
60
                  finished = true;
308
60
                }
309
310
89.7k
                break;
311
312
155k
              case 0x0A: // '\n'
313
176k
              case 0x0D: // '\r'
314
                // key associated with an empty string element
315
176k
                properties.setProperty(key, element);
316
176k
                key.erase(key.begin(), key.end());
317
176k
                element.erase(element.begin(), element.end());
318
176k
                lexemType = BEGIN;
319
320
176k
                if (!get(c))
321
1.50k
                {
322
1.50k
                  finished = true;
323
1.50k
                }
324
325
176k
                break;
326
327
26.9M
              default:
328
26.9M
                element.append(1, c);
329
330
26.9M
                if (!get(c))
331
2.15k
                {
332
2.15k
                  finished = true;
333
2.15k
                }
334
335
26.9M
                break;
336
27.1M
            }
337
338
27.1M
            break;
339
340
27.1M
          case ELEMENT_ESCAPE:
341
89.6k
            switch (c)
342
89.6k
            {
343
3.32k
              case 0x74: // 't'
344
3.32k
                element.append(1, 0x09);
345
3.32k
                lexemType = ELEMENT;
346
3.32k
                break;
347
348
946
              case 0x6E: // 'n'
349
946
                element.append(1, 0x0A);
350
946
                lexemType = ELEMENT;
351
946
                break;
352
353
28.0k
              case 0x72: // 'r'
354
28.0k
                element.append(1, 0x0D);
355
28.0k
                lexemType = ELEMENT;
356
28.0k
                break;
357
358
2.52k
              case 0x0A: // '\n'
359
2.52k
                lexemType = ELEMENT_CONTINUE;
360
2.52k
                break;
361
362
786
              case 0x0D: // '\r'
363
786
                lexemType = ELEMENT_CONTINUE2;
364
786
                break;
365
366
54.0k
              default:
367
54.0k
                element.append(1, c);
368
54.0k
                lexemType = ELEMENT;
369
54.0k
                break;
370
89.6k
            }
371
372
89.6k
            if (!get(c))
373
61
            {
374
61
              finished = true;
375
61
            }
376
377
89.6k
            break;
378
379
8.88k
          case ELEMENT_CONTINUE:
380
8.88k
            switch (c)
381
8.88k
            {
382
5.11k
              case 0x20: // ' '
383
5.58k
              case 0x09: // '\t'
384
5.58k
                if (!get(c))
385
5
                {
386
5
                  finished = true;
387
5
                }
388
389
5.58k
                break;
390
391
3.30k
              default:
392
3.30k
                lexemType = ELEMENT;
393
3.30k
                break;
394
8.88k
            }
395
396
8.88k
            break;
397
398
8.88k
          case ELEMENT_CONTINUE2:
399
786
            switch (c)
400
786
            {
401
395
              case 0x0A: // '\n'
402
395
                if (!get(c))
403
1
                {
404
1
                  finished = true;
405
1
                }
406
407
395
                lexemType = ELEMENT_CONTINUE;
408
395
                break;
409
410
391
              default:
411
391
                lexemType = ELEMENT_CONTINUE;
412
391
                break;
413
786
            }
414
415
786
            break;
416
417
334k
          case COMMENT:
418
334k
            if (c == 0x0A || c == 0x0D)
419
9.66k
            {
420
9.66k
              lexemType = BEGIN;
421
9.66k
            }
422
423
334k
            if (!get(c))
424
37
            {
425
37
              finished = true;
426
37
            }
427
428
334k
            break;
429
35.7M
        }
430
35.7M
      }
431
432
5.37k
      if (!key.empty())
433
3.32k
      {
434
3.32k
        properties.setProperty(key, element);
435
3.32k
      }
436
5.37k
    }
437
438
  protected:
439
    bool get(logchar& c)
440
35.3M
    {
441
35.3M
      if (m_in.off())
442
5.37k
      {
443
5.37k
        c = 0;
444
5.37k
        return false;
445
5.37k
      }
446
447
35.2M
      c = static_cast<logchar>(m_in.item());
448
35.2M
      m_in.forth();
449
35.2M
      return true;
450
35.3M
    }
PropertyParser::get(wchar_t&)
Line
Count
Source
440
21.4M
    {
441
21.4M
      if (m_in.off())
442
4.61k
      {
443
4.61k
        c = 0;
444
4.61k
        return false;
445
4.61k
      }
446
447
21.4M
      c = static_cast<logchar>(m_in.item());
448
21.4M
      m_in.forth();
449
21.4M
      return true;
450
21.4M
    }
PropertyParser::get(char&)
Line
Count
Source
440
13.8M
    {
441
13.8M
      if (m_in.off())
442
761
      {
443
761
        c = 0;
444
761
        return false;
445
761
      }
446
447
13.8M
      c = static_cast<logchar>(m_in.item());
448
13.8M
      m_in.forth();
449
13.8M
      return true;
450
13.8M
    }
451
452
    typedef enum
453
    {
454
      BEGIN,
455
      KEY,
456
      KEY_ESCAPE,
457
      KEY_CONTINUE,
458
      KEY_CONTINUE2,
459
      DELIMITER,
460
      ELEMENT,
461
      ELEMENT_ESCAPE,
462
      ELEMENT_CONTINUE,
463
      ELEMENT_CONTINUE2,
464
      COMMENT
465
    }
466
    LexemType;
467
};
468
469
9
Properties::Properties() : properties(new PropertyMap())
470
9
{
471
9
}
472
473
Properties::Properties(const Properties& other)
474
6.42k
  : properties(new PropertyMap(*other.properties))
475
6.42k
{
476
6.42k
}
477
478
Properties& Properties::operator=(const Properties& other)
479
0
{
480
0
  delete this->properties;
481
0
  this->properties = new PropertyMap(*other.properties);
482
0
  return *this;
483
0
}
484
485
Properties::~Properties()
486
6.42k
{
487
6.42k
  delete properties;
488
6.42k
}
489
490
LogString Properties::setProperty(const LogString& key, const LogString& value)
491
282k
{
492
282k
  return put(key, value);
493
282k
}
log4cxx::helpers::Properties::setProperty(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
Line
Count
Source
491
179k
{
492
179k
  return put(key, value);
493
179k
}
log4cxx::helpers::Properties::setProperty(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
491
102k
{
492
102k
  return put(key, value);
493
102k
}
494
495
LogString Properties::put(const LogString& key, const LogString& value)
496
282k
{
497
282k
  LogString oldValue((*properties)[key]);
498
282k
  (*properties)[key] = value;
499
282k
  return oldValue;
500
282k
}
log4cxx::helpers::Properties::put(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
Line
Count
Source
496
179k
{
497
179k
  LogString oldValue((*properties)[key]);
498
179k
  (*properties)[key] = value;
499
179k
  return oldValue;
500
179k
}
log4cxx::helpers::Properties::put(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
496
102k
{
497
102k
  LogString oldValue((*properties)[key]);
498
102k
  (*properties)[key] = value;
499
102k
  return oldValue;
500
102k
}
501
502
LogString Properties::getProperty(const LogString& key) const
503
1.30M
{
504
1.30M
  return get(key);
505
1.30M
}
log4cxx::helpers::Properties::getProperty(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) const
Line
Count
Source
503
664k
{
504
664k
  return get(key);
505
664k
}
log4cxx::helpers::Properties::getProperty(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Line
Count
Source
503
637k
{
504
637k
  return get(key);
505
637k
}
506
507
LogString Properties::get(const LogString& key) const
508
1.30M
{
509
1.30M
  PropertyMap::const_iterator it = properties->find(key);
510
1.30M
  return (it != properties->end()) ? it->second : LogString();
511
1.30M
}
log4cxx::helpers::Properties::get(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) const
Line
Count
Source
508
664k
{
509
664k
  PropertyMap::const_iterator it = properties->find(key);
510
664k
  return (it != properties->end()) ? it->second : LogString();
511
664k
}
log4cxx::helpers::Properties::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Line
Count
Source
508
637k
{
509
637k
  PropertyMap::const_iterator it = properties->find(key);
510
637k
  return (it != properties->end()) ? it->second : LogString();
511
637k
}
512
513
void Properties::load(const InputStreamPtr& inStream)
514
5.39k
{
515
5.39k
  PropertyParser(inStream).loadInto(*this);
516
5.39k
}
517
518
std::vector<LogString> Properties::propertyNames() const
519
20.6k
{
520
20.6k
  std::vector<LogString> names;
521
20.6k
  names.reserve(properties->size());
522
523
20.6k
  for (auto const& item : *properties)
524
1.00M
  {
525
1.00M
    const LogString& key = item.first;
526
1.00M
    names.push_back(key);
527
1.00M
  }
528
529
20.6k
  return names;
530
20.6k
}
531
532
bool Properties::isEmpty() const
533
9.14k
{
534
9.14k
  return properties->empty();
535
9.14k
}