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