/src/libreoffice/oox/source/helper/binaryinputstream.cxx
Line | Count | Source |
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 | | #include <oox/helper/binaryinputstream.hxx> |
21 | | |
22 | | #include <com/sun/star/io/XInputStream.hpp> |
23 | | #include <com/sun/star/io/XSeekable.hpp> |
24 | | #include <string.h> |
25 | | #include <algorithm> |
26 | | #include <vector> |
27 | | #include <rtl/ustrbuf.hxx> |
28 | | #include <osl/diagnose.h> |
29 | | #include <oox/helper/binaryoutputstream.hxx> |
30 | | |
31 | | namespace oox { |
32 | | |
33 | | using namespace ::com::sun::star::io; |
34 | | using namespace ::com::sun::star::uno; |
35 | | |
36 | | namespace { |
37 | | |
38 | | const sal_Int32 INPUTSTREAM_BUFFERSIZE = 0x8000; |
39 | | |
40 | | } // namespace |
41 | | |
42 | | OUString BinaryInputStream::readNulUnicodeArray() |
43 | 0 | { |
44 | 0 | OUStringBuffer aBuffer; |
45 | 0 | for (;;) |
46 | 0 | { |
47 | 0 | sal_uInt16 nChar = readuInt16(); |
48 | 0 | if ( mbEof || (nChar == 0) ) break; |
49 | 0 | aBuffer.append( static_cast< sal_Unicode >( nChar ) ); |
50 | 0 | } |
51 | 0 | return aBuffer.makeStringAndClear(); |
52 | 0 | } |
53 | | |
54 | | OString BinaryInputStream::readCharArray( sal_Int32 nChars ) |
55 | 116 | { |
56 | 116 | if( nChars <= 0 ) |
57 | 25 | return OString(); |
58 | | |
59 | 91 | ::std::vector< sal_uInt8 > aBuffer; |
60 | 91 | sal_Int32 nCharsRead = readArray( aBuffer, nChars ); |
61 | 91 | if( nCharsRead <= 0 ) |
62 | 0 | return OString(); |
63 | | |
64 | 91 | aBuffer.resize( static_cast< size_t >( nCharsRead ) ); |
65 | | // NUL characters are replaced by question marks. |
66 | 91 | ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' ); |
67 | | |
68 | 91 | return OString(reinterpret_cast<char*>(aBuffer.data()), nCharsRead); |
69 | 91 | } |
70 | | |
71 | | OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc ) |
72 | 116 | { |
73 | 116 | return OStringToOUString( readCharArray( nChars ), eTextEnc ); |
74 | 116 | } |
75 | | |
76 | | OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars ) |
77 | 10.5k | { |
78 | 10.5k | if( nChars <= 0 ) |
79 | 10.0k | return OUString(); |
80 | | |
81 | 466 | ::std::vector< sal_uInt16 > aBuffer; |
82 | 466 | sal_Int32 nCharsRead = readArray( aBuffer, nChars ); |
83 | 466 | if( nCharsRead <= 0 ) |
84 | 0 | return OUString(); |
85 | | |
86 | 466 | aBuffer.resize( static_cast< size_t >( nCharsRead ) ); |
87 | | // don't allow nul chars |
88 | 466 | ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' ); |
89 | | |
90 | 466 | OUStringBuffer aStringBuffer; |
91 | 466 | aStringBuffer.ensureCapacity( nCharsRead ); |
92 | 466 | for (auto const& elem : aBuffer) |
93 | 50.1k | aStringBuffer.append( static_cast< sal_Unicode >(elem) ); |
94 | 466 | return aStringBuffer.makeStringAndClear(); |
95 | 466 | } |
96 | | |
97 | | OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed ) |
98 | 36 | { |
99 | 36 | return bCompressed ? |
100 | | // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH |
101 | 36 | readCharArrayUC( nChars, RTL_TEXTENCODING_ISO_8859_1 ) : |
102 | 36 | readUnicodeArray( nChars ); |
103 | 36 | } |
104 | | |
105 | | void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm ) |
106 | 1.71k | { |
107 | 1.71k | sal_Int64 nBytes = SAL_MAX_INT64; |
108 | 1.71k | sal_Int32 nBufferSize = INPUTSTREAM_BUFFERSIZE; |
109 | 1.71k | StreamDataSequence aBuffer( nBufferSize ); |
110 | 3.42k | while( nBytes > 0 ) |
111 | 1.71k | { |
112 | 1.71k | sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize ); |
113 | 1.71k | sal_Int32 nBytesRead = readData( aBuffer, nReadSize ); |
114 | 1.71k | rOutStrm.writeData( aBuffer ); |
115 | 1.71k | if( nReadSize == nBytesRead ) |
116 | 0 | nBytes -= nReadSize; |
117 | 1.71k | else |
118 | 1.71k | nBytes = 0; |
119 | 1.71k | } |
120 | 1.71k | } |
121 | | |
122 | | BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) : |
123 | 193k | BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ), |
124 | 193k | BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ), |
125 | 193k | maBuffer( INPUTSTREAM_BUFFERSIZE ), |
126 | 193k | mxInStrm( rxInStrm ), |
127 | 193k | mbAutoClose( bAutoClose && rxInStrm.is() ) |
128 | 193k | { |
129 | 193k | mbEof = !mxInStrm.is(); |
130 | 193k | } Unexecuted instantiation: oox::BinaryXInputStream::BinaryXInputStream(com::sun::star::uno::Reference<com::sun::star::io::XInputStream> const&, bool) oox::BinaryXInputStream::BinaryXInputStream(com::sun::star::uno::Reference<com::sun::star::io::XInputStream> const&, bool) Line | Count | Source | 123 | 193k | BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ), | 124 | 193k | BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ), | 125 | 193k | maBuffer( INPUTSTREAM_BUFFERSIZE ), | 126 | 193k | mxInStrm( rxInStrm ), | 127 | 193k | mbAutoClose( bAutoClose && rxInStrm.is() ) | 128 | 193k | { | 129 | 193k | mbEof = !mxInStrm.is(); | 130 | 193k | } |
|
131 | | |
132 | | BinaryXInputStream::~BinaryXInputStream() |
133 | 193k | { |
134 | 193k | close(); |
135 | 193k | } |
136 | | |
137 | | void BinaryXInputStream::close() |
138 | 193k | { |
139 | 193k | OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" ); |
140 | 193k | if( mxInStrm.is() ) try |
141 | 192k | { |
142 | 192k | mxInStrm->closeInput(); |
143 | 192k | } |
144 | 192k | catch( Exception& ) |
145 | 192k | { |
146 | 219 | OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" ); |
147 | 219 | } |
148 | 193k | mxInStrm.clear(); |
149 | 193k | mbAutoClose = false; |
150 | 193k | BinaryXSeekableStream::close(); |
151 | 193k | } |
152 | | |
153 | | sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ ) |
154 | 183k | { |
155 | 183k | sal_Int32 nRet = 0; |
156 | 183k | if( !mbEof && (nBytes > 0) ) try |
157 | 120k | { |
158 | 120k | nRet = mxInStrm->readBytes( orData, nBytes ); |
159 | 120k | mbEof = nRet != nBytes; |
160 | 120k | } |
161 | 120k | catch( Exception& ) |
162 | 120k | { |
163 | 8 | mbEof = true; |
164 | 8 | } |
165 | 183k | return nRet; |
166 | 183k | } |
167 | | |
168 | | sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize ) |
169 | 192k | { |
170 | 192k | sal_Int32 nRet = 0; |
171 | 192k | if( !mbEof && (nBytes > 0) ) |
172 | 40.9k | { |
173 | 40.9k | sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE ); |
174 | 40.9k | sal_uInt8* opnMem = static_cast< sal_uInt8* >( opMem ); |
175 | 81.8k | while( !mbEof && (nBytes > 0) ) |
176 | 40.9k | { |
177 | 40.9k | sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize ); |
178 | 40.9k | sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize ); |
179 | 40.9k | if( nBytesRead > 0 ) |
180 | 25.7k | memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) ); |
181 | 40.9k | opnMem += nBytesRead; |
182 | 40.9k | nBytes -= nBytesRead; |
183 | 40.9k | nRet += nBytesRead; |
184 | 40.9k | } |
185 | 40.9k | } |
186 | 192k | return nRet; |
187 | 192k | } |
188 | | |
189 | | void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ ) |
190 | 36.9k | { |
191 | 36.9k | if( !mbEof ) try |
192 | 36.8k | { |
193 | 36.8k | mxInStrm->skipBytes( nBytes ); |
194 | 36.8k | } |
195 | 36.8k | catch( Exception& ) |
196 | 36.8k | { |
197 | 36 | mbEof = true; |
198 | 36 | } |
199 | 36.9k | } |
200 | | |
201 | | SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) : |
202 | 395 | BinaryStreamBase( true ), |
203 | 395 | SequenceSeekableStream( rData ) |
204 | 395 | { |
205 | 395 | } Unexecuted instantiation: oox::SequenceInputStream::SequenceInputStream(com::sun::star::uno::Sequence<signed char> const&) oox::SequenceInputStream::SequenceInputStream(com::sun::star::uno::Sequence<signed char> const&) Line | Count | Source | 202 | 395 | BinaryStreamBase( true ), | 203 | 395 | SequenceSeekableStream( rData ) | 204 | 395 | { | 205 | 395 | } |
|
206 | | |
207 | | sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ ) |
208 | 0 | { |
209 | 0 | sal_Int32 nReadBytes = 0; |
210 | 0 | if( !mbEof ) |
211 | 0 | { |
212 | 0 | nReadBytes = getMaxBytes( nBytes ); |
213 | 0 | orData.realloc( nReadBytes ); |
214 | 0 | if( nReadBytes > 0 ) |
215 | 0 | memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) ); |
216 | 0 | mnPos += nReadBytes; |
217 | 0 | mbEof = nReadBytes < nBytes; |
218 | 0 | } |
219 | 0 | return nReadBytes; |
220 | 0 | } |
221 | | |
222 | | sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ ) |
223 | 90 | { |
224 | 90 | sal_Int32 nReadBytes = 0; |
225 | 90 | if( !mbEof ) |
226 | 90 | { |
227 | 90 | nReadBytes = getMaxBytes( nBytes ); |
228 | 90 | if( nReadBytes > 0 ) |
229 | 90 | memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) ); |
230 | 90 | mnPos += nReadBytes; |
231 | 90 | mbEof = nReadBytes < nBytes; |
232 | 90 | } |
233 | 90 | return nReadBytes; |
234 | 90 | } |
235 | | |
236 | | void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ ) |
237 | 0 | { |
238 | 0 | if( !mbEof ) |
239 | 0 | { |
240 | 0 | sal_Int32 nSkipBytes = getMaxBytes( nBytes ); |
241 | 0 | mnPos += nSkipBytes; |
242 | 0 | mbEof = nSkipBytes < nBytes; |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | | RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) : |
247 | 0 | BinaryStreamBase( rInStrm.isSeekable() ), |
248 | 0 | mpInStrm( &rInStrm ), |
249 | 0 | mnStartPos( rInStrm.tell() ), |
250 | 0 | mnRelPos( 0 ) |
251 | 0 | { |
252 | 0 | sal_Int64 nRemaining = rInStrm.getRemaining(); |
253 | 0 | mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize; |
254 | 0 | mbEof = mbEof || rInStrm.isEof() || (mnSize < 0); |
255 | 0 | } Unexecuted instantiation: oox::RelativeInputStream::RelativeInputStream(oox::BinaryInputStream&, long) Unexecuted instantiation: oox::RelativeInputStream::RelativeInputStream(oox::BinaryInputStream&, long) |
256 | | |
257 | | sal_Int64 RelativeInputStream::size() const |
258 | 0 | { |
259 | 0 | return mpInStrm ? mnSize : -1; |
260 | 0 | } |
261 | | |
262 | | sal_Int64 RelativeInputStream::tell() const |
263 | 0 | { |
264 | 0 | return mpInStrm ? mnRelPos : -1; |
265 | 0 | } |
266 | | |
267 | | void RelativeInputStream::seek( sal_Int64 nPos ) |
268 | 0 | { |
269 | 0 | if( mpInStrm && isSeekable() && (mnStartPos >= 0) ) |
270 | 0 | { |
271 | 0 | mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize ); |
272 | 0 | mpInStrm->seek( mnStartPos + mnRelPos ); |
273 | 0 | mbEof = (mnRelPos != nPos) || mpInStrm->isEof(); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | void RelativeInputStream::close() |
278 | 0 | { |
279 | 0 | mpInStrm = nullptr; |
280 | 0 | mbEof = true; |
281 | 0 | } |
282 | | |
283 | | sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize ) |
284 | 0 | { |
285 | 0 | sal_Int32 nReadBytes = 0; |
286 | 0 | if( !mbEof ) |
287 | 0 | { |
288 | 0 | sal_Int32 nMaxBytes = getMaxBytes( nBytes ); |
289 | 0 | nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize ); |
290 | 0 | mnRelPos += nReadBytes; |
291 | 0 | mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof(); |
292 | 0 | } |
293 | 0 | return nReadBytes; |
294 | 0 | } |
295 | | |
296 | | sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize ) |
297 | 0 | { |
298 | 0 | sal_Int32 nReadBytes = 0; |
299 | 0 | if( !mbEof ) |
300 | 0 | { |
301 | 0 | sal_Int32 nMaxBytes = getMaxBytes( nBytes ); |
302 | 0 | nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize ); |
303 | 0 | mnRelPos += nReadBytes; |
304 | 0 | mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof(); |
305 | 0 | } |
306 | 0 | return nReadBytes; |
307 | 0 | } |
308 | | |
309 | | void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize ) |
310 | 0 | { |
311 | 0 | if( !mbEof ) |
312 | 0 | { |
313 | 0 | sal_Int32 nSkipBytes = getMaxBytes( nBytes ); |
314 | 0 | mpInStrm->skip( nSkipBytes, nAtomSize ); |
315 | 0 | mnRelPos += nSkipBytes; |
316 | 0 | mbEof = nSkipBytes < nBytes; |
317 | 0 | } |
318 | 0 | } |
319 | | |
320 | | } // namespace oox |
321 | | |
322 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |