Coverage Report

Created: 2026-04-29 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwps/src/lib/WPSStringStream.cpp
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* libwps
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) 2009, 2011 Alonso Laurent (alonso@loria.fr)
11
 * Copyright (C) 2006, 2007 Andrew Ziem
12
 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
13
 * Copyright (C) 2004 Marc Maurer (uwog@uwog.net)
14
 * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
15
 *
16
 * For minor contributions see the git repository.
17
 *
18
 * Alternatively, the contents of this file may be used under the terms
19
 * of the GNU Lesser General Public License Version 2.1 or later
20
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21
 * applicable instead of those above.
22
 *
23
 * For further information visit http://libwps.sourceforge.net
24
 */
25
26
#include <cstring>
27
#include <vector>
28
29
#include <librevenge-stream/librevenge-stream.h>
30
31
#include "WPSStringStream.h"
32
33
//! internal data of a WPSStringStream
34
class WPSStringStreamPrivate
35
{
36
public:
37
  //! constructor
38
  WPSStringStreamPrivate(const unsigned char *data, unsigned dataSize);
39
  //! destructor
40
  ~WPSStringStreamPrivate();
41
  //! append some data at the end of the actual stream
42
  void append(const unsigned char *data, unsigned dataSize);
43
  //! the stream buffer
44
  std::vector<unsigned char> m_buffer;
45
  //! the stream offset
46
  long m_offset;
47
private:
48
  WPSStringStreamPrivate(const WPSStringStreamPrivate &) = delete;
49
  WPSStringStreamPrivate &operator=(const WPSStringStreamPrivate &) = delete;
50
};
51
52
WPSStringStreamPrivate::WPSStringStreamPrivate(const unsigned char *data, unsigned dataSize)
53
18.9k
  : m_buffer(0)
54
18.9k
  , m_offset(0)
55
18.9k
{
56
18.9k
  if (data && dataSize)
57
18.9k
  {
58
18.9k
    m_buffer.resize(dataSize);
59
18.9k
    std::memcpy(&m_buffer[0], data, dataSize);
60
18.9k
  }
61
18.9k
}
62
63
WPSStringStreamPrivate::~WPSStringStreamPrivate()
64
18.9k
{
65
18.9k
}
66
67
void WPSStringStreamPrivate::append(const unsigned char *data, unsigned dataSize)
68
213
{
69
213
  if (!dataSize) return;
70
213
  size_t actualSize=m_buffer.size();
71
213
  m_buffer.resize(actualSize+size_t(dataSize));
72
213
  std::memcpy(&m_buffer[actualSize], data, dataSize);
73
213
}
74
75
WPSStringStream::WPSStringStream(const unsigned char *data, const unsigned int dataSize) :
76
18.9k
  librevenge::RVNGInputStream(),
77
18.9k
  m_data(new WPSStringStreamPrivate(data, dataSize))
78
18.9k
{
79
18.9k
}
80
81
WPSStringStream::~WPSStringStream()
82
18.9k
{
83
18.9k
}
84
85
void WPSStringStream::append(const unsigned char *data, const unsigned int dataSize)
86
213
{
87
213
  if (m_data) m_data->append(data, dataSize);
88
213
}
89
90
const unsigned char *WPSStringStream::read(unsigned long numBytes, unsigned long &numBytesRead)
91
75.5M
{
92
75.5M
  numBytesRead = 0;
93
94
75.5M
  if (numBytes == 0 || !m_data)
95
33
    return nullptr;
96
97
75.5M
  long numBytesToRead;
98
99
75.5M
  if (static_cast<unsigned long>(m_data->m_offset)+numBytes < m_data->m_buffer.size())
100
75.5M
    numBytesToRead = long(numBytes);
101
6.58k
  else
102
6.58k
    numBytesToRead = long(m_data->m_buffer.size()) - m_data->m_offset;
103
104
75.5M
  numBytesRead = static_cast<unsigned long>(numBytesToRead); // about as paranoid as we can be..
105
106
75.5M
  if (numBytesToRead == 0)
107
1.87k
    return nullptr;
108
109
75.5M
  long oldOffset = m_data->m_offset;
110
75.5M
  m_data->m_offset += numBytesToRead;
111
112
75.5M
  return &m_data->m_buffer[size_t(oldOffset)];
113
114
75.5M
}
115
116
long WPSStringStream::tell()
117
21.2M
{
118
21.2M
  return m_data ? m_data->m_offset : 0;
119
21.2M
}
120
121
int WPSStringStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType)
122
14.4M
{
123
14.4M
  if (!m_data) return -1;
124
14.4M
  if (seekType == librevenge::RVNG_SEEK_CUR)
125
184k
    m_data->m_offset += offset;
126
14.2M
  else if (seekType == librevenge::RVNG_SEEK_SET)
127
14.2M
    m_data->m_offset = offset;
128
12.6k
  else if (seekType == librevenge::RVNG_SEEK_END)
129
12.6k
    m_data->m_offset = offset+long(m_data->m_buffer.size());
130
131
14.4M
  if (m_data->m_offset < 0)
132
0
  {
133
0
    m_data->m_offset = 0;
134
0
    return -1;
135
0
  }
136
14.4M
  if (long(m_data->m_offset) > long(m_data->m_buffer.size()))
137
212k
  {
138
212k
    m_data->m_offset = long(m_data->m_buffer.size());
139
212k
    return -1;
140
212k
  }
141
142
14.2M
  return 0;
143
14.4M
}
144
145
bool WPSStringStream::isEnd()
146
1.91M
{
147
1.91M
  if (!m_data || long(m_data->m_offset) >= long(m_data->m_buffer.size()))
148
1.10k
    return true;
149
150
1.91M
  return false;
151
1.91M
}
152
153
bool WPSStringStream::isStructured()
154
0
{
155
0
  return false;
156
0
}
157
158
unsigned WPSStringStream::subStreamCount()
159
0
{
160
0
  return 0;
161
0
}
162
163
const char *WPSStringStream::subStreamName(unsigned)
164
0
{
165
0
  return nullptr;
166
0
}
167
168
bool WPSStringStream::existsSubStream(const char *)
169
0
{
170
0
  return false;
171
0
}
172
173
librevenge::RVNGInputStream *WPSStringStream::getSubStreamById(unsigned)
174
0
{
175
0
  return nullptr;
176
0
}
177
178
librevenge::RVNGInputStream *WPSStringStream::getSubStreamByName(const char *)
179
0
{
180
0
  return nullptr;
181
0
}
182
183
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */