LibreOffice Module oox (master)  1
binaryinputstream.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice 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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_OOX_HELPER_BINARYINPUTSTREAM_HXX
21 #define INCLUDED_OOX_HELPER_BINARYINPUTSTREAM_HXX
22 
23 #include <cstddef>
24 #include <memory>
25 #include <vector>
26 
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <oox/dllapi.h>
30 #include <oox/helper/helper.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/textenc.h>
33 #include <rtl/ustring.hxx>
34 #include <sal/types.h>
35 
36 namespace com::sun::star {
37  namespace io { class XInputStream; }
38 }
39 
40 namespace oox {
41 
42 class BinaryOutputStream;
43 
44 
50 {
51 public:
62  virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
63 
74  virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
75 
83  virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
84 
88  template< typename Type >
89  [[nodiscard]]
90  Type readValue();
91 
92  [[nodiscard]]
93  sal_Int8 readInt8() { return readValue<sal_Int8>(); }
94  [[nodiscard]]
95  sal_uInt8 readuInt8() { return readValue<sal_uInt8>(); }
96  [[nodiscard]]
97  sal_Int16 readInt16() { return readValue<sal_Int16>(); }
98  [[nodiscard]]
99  sal_uInt16 readuInt16() { return readValue<sal_uInt16>(); }
100  [[nodiscard]]
101  sal_Int32 readInt32() { return readValue<sal_Int32>(); }
102  [[nodiscard]]
103  sal_uInt32 readuInt32() { return readValue<sal_uInt32>(); }
104  [[nodiscard]]
105  sal_Int64 readInt64() { return readValue<sal_Int64>(); }
106  [[nodiscard]]
107  float readFloat() { return readValue<float>(); }
108  [[nodiscard]]
109  double readDouble() { return readValue<double>(); }
110  [[nodiscard]]
111  unsigned char readuChar() { return readValue<unsigned char>(); }
112 
124  template< typename Type >
125  sal_Int32 readArray( Type* opnArray, sal_Int32 nElemCount );
126 
139  template< typename Type >
140  sal_Int32 readArray( ::std::vector< Type >& orVector, sal_Int32 nElemCount );
141 
144  OUString readNulUnicodeArray();
145 
152  OString readCharArray( sal_Int32 nChars );
153 
163  OUString readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc );
164 
171  OUString readUnicodeArray( sal_Int32 nChars );
172 
184  OUString readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed );
185 
188  void copyToStream( BinaryOutputStream& rOutStrm );
189 
190 protected:
194 
195 private:
196  BinaryInputStream( BinaryInputStream const& ) = delete;
197  BinaryInputStream& operator=( BinaryInputStream const& ) = delete;
198 };
199 
200 typedef std::shared_ptr< BinaryInputStream > BinaryInputStreamRef;
201 
202 
203 template< typename Type >
205 {
206  Type ornValue = Type();
207  readMemory( &ornValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
209  return ornValue;
210 }
211 
212 template< typename Type >
213 sal_Int32 BinaryInputStream::readArray( Type* opnArray, sal_Int32 nElemCount )
214 {
215  sal_Int32 nRet = 0;
216  if( !mbEof )
217  {
218  sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
219  nRet = readMemory( opnArray, nReadSize, sizeof( Type ) ) / sizeof( Type );
220  ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nRet ) );
221  }
222  return nRet;
223 }
224 
225 template< typename Type >
226 sal_Int32 BinaryInputStream::readArray( ::std::vector< Type >& orVector, sal_Int32 nElemCount )
227 {
228  orVector.resize( static_cast< size_t >( nElemCount ) );
229  return orVector.empty() ? 0 : readArray(orVector.data(), nElemCount);
230 }
231 
232 
238 {
239 public:
250  explicit BinaryXInputStream(
251  const css::uno::Reference< css::io::XInputStream >& rxInStrm,
252  bool bAutoClose );
253 
254  virtual ~BinaryXInputStream() override;
255 
258  virtual void close() override;
259 
262  virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
263 
266  virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
267 
270  virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
271 
272 private:
274  css::uno::Reference< css::io::XInputStream >
276  bool mbAutoClose;
277 };
278 
279 
285 {
286 public:
294  explicit SequenceInputStream( const StreamDataSequence& rData );
295 
298  virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
299 
302  virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
303 
306  virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
307 
308 private:
310  sal_Int32 getMaxBytes( sal_Int32 nBytes ) const
311  { return getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, mpData->getLength() - mnPos ); }
312 };
313 
314 
328 {
329 public:
336  explicit RelativeInputStream(
337  BinaryInputStream& rInStrm,
338  sal_Int64 nSize );
339 
342  virtual sal_Int64 size() const override;
343 
345  virtual sal_Int64 tell() const override;
346 
349  virtual void seek( sal_Int64 nPos ) override;
350 
352  virtual void close() override;
353 
357  virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
358 
362  virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
363 
366  virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
367 
368 private:
370  sal_Int32 getMaxBytes( sal_Int32 nBytes ) const
371  { return getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, mnSize - mnRelPos ); }
372 
373 private:
375  sal_Int64 mnStartPos;
376  sal_Int64 mnRelPos;
377  sal_Int64 mnSize;
378 };
379 
380 
381 } // namespace oox
382 
383 #endif
384 
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
Base class for binary stream classes.
virtual void close() override
Closes the input stream but not the wrapped stream.
Base class for binary input and output streams wrapping a UNO stream, seekable via the com...
Wraps a UNO input stream and provides convenient access functions.
exports com.sun.star. io
signed char sal_Int8
StreamDataSequence maBuffer
Data buffer used in readMemory() function.
virtual sal_Int32 readData(StreamDataSequence &orData, sal_Int32 nBytes, size_t nAtomSize=1) override
Reads nBytes bytes to the passed sequence.
std::shared_ptr< BinaryInputStream > BinaryInputStreamRef
const ContentProperties & rData
virtual sal_Int64 tell() const override
Returns the current relative stream position.
virtual sal_Int32 readMemory(void *opMem, sal_Int32 nBytes, size_t nAtomSize=1)=0
Derived classes implement reading nBytes bytes to the (preallocated!) memory buffer opMem...
Interface for binary output stream classes.
BinaryInputStream * mpInStrm
css::uno::Reference< css::io::XInputStream > mxInStrm
Reference to the input stream.
Wraps a BinaryInputStream and provides access to a specific part of the stream data.
virtual sal_Int32 readMemory(void *opMem, sal_Int32 nBytes, size_t nAtomSize=1) override
Reads nBytes bytes to the (existing) buffer opMem.
Base class for binary input and output streams wrapping a StreamDataSequence, which is always seekabl...
Interface for binary input stream classes.
RelativeInputStream(BinaryInputStream &rInStrm, sal_Int64 nSize)
Constructs the wrapper object for the passed stream.
css::uno::Sequence< sal_Int8 > StreamDataSequence
#define SAL_MAX_INT32
virtual void seek(sal_Int64 nPos) override
Seeks the stream to the passed relative position, if the wrapped stream is seekable.
sal_Int32 getMaxBytes(sal_Int32 nBytes) const
Returns the number of bytes available in the sequence for the passed byte count.
bool mbEof
End of stream flag.
static void convertLittleEndianArray(Type *, size_t)
Definition: helper.hxx:241
bool close
BinaryInputStream()
This dummy default c'tor will never call the c'tor of the virtual base class BinaryStreamBase as this...
virtual void skip(sal_Int32 nBytes, size_t nAtomSize=1) override
Seeks the stream forward by the passed number of bytes.
virtual sal_Int64 size() const override
Returns the size of the data block in the wrapped stream offered by this wrapper. ...
unsigned char sal_uInt8
sal_Int32 readArray(Type *opnArray, sal_Int32 nElemCount)
Reads a (preallocated!) C array of values from the stream.
bool mbAutoClose
True = automatically close stream on destruction.
Wraps a StreamDataSequence and provides convenient access functions.
#define OOX_DLLPUBLIC
Definition: dllapi.h:28
Type readValue()
Reads a value from the stream and converts it to platform byte order.
sal_Int32 getMaxBytes(sal_Int32 nBytes) const
Returns the number of bytes available in the sequence for the passed byte count.
sal_uInt16 nPos
static void convertLittleEndian(Type &)
Definition: helper.hxx:238