Coverage Report

Created: 2026-09-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwpd/src/lib/WP5PrefixData.cpp
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* libwpd
3
 * Version: MPL 2.0 / LGPLv2.1+
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * Major Contributor(s):
10
 * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)
11
 *
12
 * For minor contributions see the git repository.
13
 *
14
 * Alternatively, the contents of this file may be used under the terms
15
 * of the GNU Lesser General Public License Version 2.1 or later
16
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
17
 * applicable instead of those above.
18
 *
19
 * For further information visit http://libwpd.sourceforge.net
20
 */
21
22
/* "This product is not manufactured, approved, or supported by
23
 * Corel Corporation or Corel Corporation Limited."
24
 */
25
26
#include <set>
27
28
#include "WP5PrefixData.h"
29
#include "WP5GeneralPacketIndex.h"
30
#include "WP5SpecialHeaderIndex.h"
31
#include "libwpd_internal.h"
32
33
WP5PrefixData::WP5PrefixData(librevenge::RVNGInputStream *input, WPXEncryption *encryption) :
34
0
  m_generalPacketData()
35
0
{
36
0
  std::vector<WP5GeneralPacketIndex> prefixIndexVector;
37
0
  std::set<long> readBlocks; // guard against a cycle in the block list
38
0
  int id = 0;
39
0
  while (true)
40
0
  {
41
0
    readBlocks.insert(input->tell());
42
0
    WP5SpecialHeaderIndex shi = WP5SpecialHeaderIndex(input, encryption);
43
44
0
    if ((shi.getType() != 0xfffb) || (shi.getNumOfIndexes() != 5) || (shi.getIndexBlockSize() != 50))
45
0
    {
46
0
      WPD_DEBUG_MSG(("WordPerfect: detected possible prefix data corruption, ignoring this and all following packets.\n"));
47
0
      break;
48
0
    }
49
50
0
    bool tmpFoundPossibleCorruption = false;
51
0
    for (int i=0; i<(shi.getNumOfIndexes()-1); i++)
52
0
    {
53
0
      WP5GeneralPacketIndex gpi = WP5GeneralPacketIndex(input, encryption, id);
54
0
      if ((gpi.getType() > 0x02ff) && (gpi.getType() < 0xfffb))
55
0
      {
56
0
        WPD_DEBUG_MSG(("WordPerfect: detected possible prefix data corruption, ignoring this and all following packets.\n"));
57
0
        tmpFoundPossibleCorruption = true;
58
0
        break;
59
0
      }
60
61
0
      if ((gpi.getType() != 0) && (gpi.getType() != 0xffff))
62
0
      {
63
0
        WPD_DEBUG_MSG(("WordPerfect: constructing general packet index 0x%x\n", id));
64
0
        prefixIndexVector.push_back(gpi);
65
0
        id++;
66
0
      }
67
0
    }
68
0
    if (tmpFoundPossibleCorruption)
69
0
      break;
70
71
0
    if (readBlocks.find(long(shi.getNextBlockOffset())) != readBlocks.end())
72
0
    {
73
0
      WPD_DEBUG_MSG(("WordPerfect: detected a cycle in the special header index list\n"));
74
0
      break;
75
0
    }
76
77
0
    if (shi.getNextBlockOffset() != 0)
78
0
      input->seek(shi.getNextBlockOffset(), librevenge::RVNG_SEEK_SET);
79
0
    else
80
0
      break;
81
0
  }
82
83
0
  std::vector<WP5GeneralPacketIndex>::iterator gpiIter;
84
0
  for (gpiIter = prefixIndexVector.begin(); gpiIter != prefixIndexVector.end(); ++gpiIter)
85
0
  {
86
0
    WPD_DEBUG_MSG(("WordPerfect: constructing general packet data %i\n", (*gpiIter).getID()));
87
0
    std::unique_ptr<WP5GeneralPacketData> generalPacketData{WP5GeneralPacketData::constructGeneralPacketData(input, encryption, &(*gpiIter))};
88
0
    if (generalPacketData)
89
0
    {
90
0
      m_generalPacketData[gpiIter->getType()] = std::move(generalPacketData);
91
0
    }
92
0
  }
93
0
}
94
95
WP5PrefixData::~WP5PrefixData()
96
0
{
97
0
}
98
99
const WP5GeneralPacketData *WP5PrefixData::getGeneralPacketData(const int type) const
100
0
{
101
0
  auto Iter = m_generalPacketData.find(type);
102
0
  if (Iter != m_generalPacketData.end())
103
0
    return Iter->second.get();
104
0
  else
105
0
    return nullptr;
106
0
}
107
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */