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 0 : OUString BinaryInputStream::readNulUnicodeArray()
46 : {
47 0 : OUStringBuffer aBuffer;
48 0 : for( sal_uInt16 nChar = readuInt16(); !mbEof && (nChar > 0); readValue( nChar ) )
49 0 : aBuffer.append( static_cast< sal_Unicode >( nChar ) );
50 0 : return aBuffer.makeStringAndClear();
51 : }
52 :
53 0 : OString BinaryInputStream::readCharArray( sal_Int32 nChars, bool bAllowNulChars )
54 : {
55 0 : if( nChars <= 0 )
56 0 : return OString();
57 :
58 0 : ::std::vector< sal_uInt8 > aBuffer;
59 0 : sal_Int32 nCharsRead = readArray( aBuffer, nChars );
60 0 : if( nCharsRead <= 0 )
61 0 : return OString();
62 :
63 0 : aBuffer.resize( static_cast< size_t >( nCharsRead ) );
64 0 : if( !bAllowNulChars )
65 0 : ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
66 :
67 0 : return OString( reinterpret_cast< sal_Char* >( &aBuffer.front() ), nCharsRead );
68 : }
69 :
70 0 : OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
71 : {
72 0 : 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 0 : 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 0 : readUnicodeArray( nChars, bAllowNulChars );
102 : }
103 :
104 0 : void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm, sal_Int64 nBytes, sal_Int32 nAtomSize )
105 : {
106 0 : if( nBytes > 0 )
107 : {
108 : // make buffer size a multiple of the passed atom size
109 0 : sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, (INPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
110 0 : StreamDataSequence aBuffer( nBufferSize );
111 0 : while( nBytes > 0 )
112 : {
113 0 : sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
114 0 : sal_Int32 nBytesRead = readData( aBuffer, nReadSize, nAtomSize );
115 0 : rOutStrm.writeData( aBuffer );
116 0 : if( nReadSize == nBytesRead )
117 0 : nBytes -= nReadSize;
118 : else
119 0 : nBytes = 0;
120 0 : }
121 : }
122 0 : }
123 :
124 :
125 :
126 0 : BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
127 0 : BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
128 : BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
129 : maBuffer( INPUTSTREAM_BUFFERSIZE ),
130 : mxInStrm( rxInStrm ),
131 0 : mbAutoClose( bAutoClose && rxInStrm.is() )
132 : {
133 0 : mbEof = !mxInStrm.is();
134 0 : }
135 :
136 0 : BinaryXInputStream::~BinaryXInputStream()
137 : {
138 0 : close();
139 0 : }
140 :
141 0 : void BinaryXInputStream::close()
142 : {
143 : OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
144 0 : if( mxInStrm.is() ) try
145 : {
146 0 : mxInStrm->closeInput();
147 : }
148 0 : catch( Exception& )
149 : {
150 : OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
151 : }
152 0 : mxInStrm.clear();
153 0 : mbAutoClose = false;
154 0 : BinaryXSeekableStream::close();
155 0 : }
156 :
157 0 : sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
158 : {
159 0 : sal_Int32 nRet = 0;
160 0 : if( !mbEof && (nBytes > 0) ) try
161 : {
162 0 : nRet = mxInStrm->readBytes( orData, nBytes );
163 0 : mbEof = nRet != nBytes;
164 : }
165 0 : catch( Exception& )
166 : {
167 0 : mbEof = true;
168 : }
169 0 : return nRet;
170 : }
171 :
172 0 : sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
173 : {
174 0 : sal_Int32 nRet = 0;
175 0 : if( !mbEof && (nBytes > 0) )
176 : {
177 0 : sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
178 0 : sal_uInt8* opnMem = reinterpret_cast< sal_uInt8* >( opMem );
179 0 : while( !mbEof && (nBytes > 0) )
180 : {
181 0 : sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
182 0 : sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
183 0 : if( nBytesRead > 0 )
184 0 : memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
185 0 : opnMem += nBytesRead;
186 0 : nBytes -= nBytesRead;
187 0 : nRet += nBytesRead;
188 : }
189 : }
190 0 : return nRet;
191 : }
192 :
193 0 : void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
194 : {
195 0 : if( !mbEof ) try
196 : {
197 0 : mxInStrm->skipBytes( nBytes );
198 : }
199 0 : catch( Exception& )
200 : {
201 0 : mbEof = true;
202 : }
203 0 : }
204 :
205 :
206 :
207 0 : SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
208 : BinaryStreamBase( true ),
209 0 : SequenceSeekableStream( rData )
210 : {
211 0 : }
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 0 : sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
229 : {
230 0 : sal_Int32 nReadBytes = 0;
231 0 : if( !mbEof )
232 : {
233 0 : nReadBytes = getMaxBytes( nBytes );
234 0 : if( nReadBytes > 0 )
235 0 : memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
236 0 : mnPos += nReadBytes;
237 0 : mbEof = nReadBytes < nBytes;
238 : }
239 0 : 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: */
|