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