Debian Codegraph @

src/lib/QXPBlockParser.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
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
10 #include "QXPBlockParser.h"
11
12 #include <librevenge-stream/librevenge-stream.h>
13 #include <algorithm>
14 #include <cassert>
15 #include <memory>
16 #include <set>
17 #include <vector>
18 #include <iterator>
19
20 #include "QXPHeader.h"
21 #include "QXPMemoryStream.h"
22 #include "libqxp_utils.h"
23
24 namespace libqxp
25 {
26
27 using librevenge::RVNGInputStream;
28 using std::make_shared;
29 using std::vector;
30
31 namespace
32 {
33
34 unsigned long getLength(RVNGInputStream *const input)
35 {
36 assert(input);
37 const unsigned long pos = input->tell();
38 seek(input, 0);
39 const unsigned long len = getRemainingLength(input);
40 seek(input, pos);
41 return len;
42 }
43
44 }
45
46 QXPBlockParser::QXPBlockParser(const std::shared_ptr<RVNGInputStream> &input, const std::shared_ptr<QXPHeader> &header)
47 : m_input(input)
48 , m_header(header)
49 , be(header->isBigEndian())
50 , m_length(getLength(m_input.get()))
51 , m_blockLength(256)
52 , m_lastBlock(m_length > 0 ? m_length / m_blockLength + 1 : 0)
53 {
54 }
55
56 std::shared_ptr<RVNGInputStream> QXPBlockParser::getBlock(const uint32_t index)
57 {
58 if (index > 0 && index <= m_lastBlock)
59 {
60 seek(m_input, (index - 1) * m_blockLength);
61 unsigned long bytes = 0;
62 auto block = m_input->read(m_blockLength, bytes);
63 if (bytes > 0)
64 return make_shared<QXPMemoryStream>(block, bytes);
65 }
66 return nullptr;
67 }
68
69 std::shared_ptr<RVNGInputStream> QXPBlockParser::getChain(const uint32_t index)
70 {
71 bool bigIdx = m_header->version() >= QXPVersion::QXP_31_MAC;
72
73 vector<unsigned char> chain;
74 bool isBig = false;
75 uint32_t next = index;
76 try
77 {
78 std::set<uint32_t> visited;
79
80 while (next > 0 && next <= m_lastBlock)
81 {
82 seek(m_input, (next - 1) * m_blockLength);
83 uint16_t count = isBig ? readU16(m_input, be) : 1;
84 if (count > m_lastBlock - next)
85 count = m_lastBlock - next;
86 bool stop = false;
87
88 // Cycle/overlap detection
89 // If this is a big block, we read data up to the previously read
90 // block and only then stop.
91 for (uint32_t i = next - 1; i < next - 1 + count; ++i)
92 {
93 stop = !visited.insert(i).second;
94 if (stop)
95 count = uint16_t(i - next - 1);
96 }
97 if (count == 0)
98 break;
99
100 uint32_t len = (next - 1 + count) * m_blockLength - (bigIdx ? 4 : 2) - m_input->tell();
101 unsigned long bytes = 0;
102 auto block = m_input->read(len, bytes);
103 if (bool(block) && bytes > 0)
104 std::copy(block, block + bytes, std::back_inserter(chain));
105
106 if (stop || bytes < len) // A cycle was detected or we're at the end already
107 break;
108
109 const int32_t nextVal = bigIdx ? readS32(m_input, be) : readS16(m_input, be);
110 isBig = nextVal < 0;
111 next = abs(nextVal);
112 }
113 }
114 catch (...)
115 {
116 // Just retrieve what's possible
117 }
118 return make_shared<QXPMemoryStream>(chain.data(), chain.size());
119 }
120
121 }
122
123 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */