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 : // TODO: Read->RefreshBuffer-> React to changes from nBufActualLen
21 :
22 : #include <cstddef>
23 :
24 : #include <string.h>
25 : #include <stdio.h>
26 : #include <ctype.h>
27 : #include <stdlib.h>
28 :
29 : #include "boost/static_assert.hpp"
30 :
31 : #include <osl/endian.h>
32 :
33 : #include <comphelper/string.hxx>
34 :
35 : #define SWAPNIBBLES(c) \
36 : unsigned char nSwapTmp=c; \
37 : nSwapTmp <<= 4; \
38 : c >>= 4; \
39 : c |= nSwapTmp;
40 :
41 : #include <tools/debug.hxx>
42 : #include <tools/stream.hxx>
43 : #include <osl/thread.h>
44 : #include <algorithm>
45 :
46 : // !!! Do not inline if already the operators <<,>> are inline
47 10545 : inline static void SwapUShort( sal_uInt16& r )
48 10545 : { r = OSL_SWAPWORD(r); }
49 10248 : inline static void SwapShort( short& r )
50 10248 : { r = OSL_SWAPWORD(r); }
51 50133 : inline static void SwapULong( sal_uInt32& r )
52 50133 : { r = OSL_SWAPDWORD(r); }
53 19843 : inline static void SwapLongInt( sal_Int32& r )
54 19843 : { r = OSL_SWAPDWORD(r); }
55 :
56 0 : inline static void SwapUInt64( sal_uInt64& r )
57 : {
58 : union
59 : {
60 : sal_uInt64 n;
61 : sal_uInt32 c[2];
62 : } s;
63 :
64 0 : s.n = r;
65 0 : s.c[0] ^= s.c[1]; // swap the 32 bit words
66 0 : s.c[1] ^= s.c[0];
67 0 : s.c[0] ^= s.c[1];
68 : // swap the bytes in the words
69 0 : s.c[0] = OSL_SWAPDWORD(s.c[0]);
70 0 : s.c[1] = OSL_SWAPDWORD(s.c[1]);
71 0 : r = s.n;
72 0 : }
73 0 : inline static void SwapInt64( sal_Int64& r )
74 : {
75 : union
76 : {
77 : sal_Int64 n;
78 : sal_Int32 c[2];
79 : } s;
80 :
81 0 : s.n = r;
82 0 : s.c[0] ^= s.c[1]; // swap the 32 bit words
83 0 : s.c[1] ^= s.c[0];
84 0 : s.c[0] ^= s.c[1];
85 : // swap the bytes in the words
86 0 : s.c[0] = OSL_SWAPDWORD(s.c[0]);
87 0 : s.c[1] = OSL_SWAPDWORD(s.c[1]);
88 0 : r = s.n;
89 0 : }
90 :
91 : #ifdef UNX
92 0 : inline static void SwapFloat( float& r )
93 : {
94 : union
95 : {
96 : float f;
97 : sal_uInt32 c;
98 : } s;
99 :
100 0 : s.f = r;
101 0 : s.c = OSL_SWAPDWORD( s.c );
102 0 : r = s.f;
103 0 : }
104 :
105 0 : inline static void SwapDouble( double& r )
106 : {
107 : if( sizeof(double) != 8 )
108 : {
109 : DBG_ASSERT( false, "Can only swap 8-Byte-doubles\n" );
110 : }
111 : else
112 : {
113 : union
114 : {
115 : double d;
116 : sal_uInt32 c[2];
117 : } s;
118 :
119 0 : s.d = r;
120 0 : s.c[0] ^= s.c[1]; // swap 32-bit values in situ
121 0 : s.c[1] ^= s.c[0];
122 0 : s.c[0] ^= s.c[1];
123 0 : s.c[0] = OSL_SWAPDWORD(s.c[0]); // swap dword itself in situ
124 0 : s.c[1] = OSL_SWAPDWORD(s.c[1]);
125 0 : r = s.d;
126 : }
127 0 : }
128 : #endif
129 :
130 : //SDO
131 :
132 : #define READNUMBER_WITHOUT_SWAP(datatype,value) \
133 : if( bIoRead && sizeof(datatype)<=nBufFree) \
134 : { \
135 : for (std::size_t i = 0; i < sizeof(datatype); i++) \
136 : ((char *)&value)[i] = pBufPos[i]; \
137 : nBufActualPos += sizeof(datatype); \
138 : pBufPos += sizeof(datatype); \
139 : nBufFree -= sizeof(datatype); \
140 : } \
141 : else \
142 : { \
143 : Read( (char*)&value, sizeof(datatype) ); \
144 : } \
145 :
146 :
147 : #define WRITENUMBER_WITHOUT_SWAP(datatype,value) \
148 : if( bIoWrite && sizeof(datatype) <= nBufFree) \
149 : { \
150 : for (std::size_t i = 0; i < sizeof(datatype); i++) \
151 : pBufPos[i] = ((char *)&value)[i]; \
152 : nBufFree -= sizeof(datatype); \
153 : nBufActualPos += sizeof(datatype); \
154 : if( nBufActualPos > nBufActualLen ) \
155 : nBufActualLen = nBufActualPos; \
156 : pBufPos += sizeof(datatype); \
157 : bIsDirty = true; \
158 : } \
159 : else \
160 : { \
161 : Write( (char*)&value, sizeof(datatype) ); \
162 : } \
163 :
164 : // class SvLockBytes
165 :
166 30089 : void SvLockBytes::close()
167 : {
168 30089 : if (m_bOwner)
169 0 : delete m_pStream;
170 30089 : m_pStream = 0;
171 30089 : }
172 :
173 0 : TYPEINIT0(SvLockBytes);
174 :
175 : // virtual
176 1 : ErrCode SvLockBytes::ReadAt(sal_uInt64 const nPos, void * pBuffer, sal_Size nCount,
177 : sal_Size * pRead) const
178 : {
179 1 : if (!m_pStream)
180 : {
181 : OSL_FAIL("SvLockBytes::ReadAt(): Bad stream");
182 0 : return ERRCODE_NONE;
183 : }
184 :
185 1 : m_pStream->Seek(nPos);
186 1 : sal_Size nTheRead = m_pStream->Read(pBuffer, nCount);
187 1 : if (pRead)
188 1 : *pRead = nTheRead;
189 1 : return m_pStream->GetErrorCode();
190 : }
191 :
192 : // virtual
193 0 : ErrCode SvLockBytes::WriteAt(sal_uInt64 const nPos, const void * pBuffer, sal_Size nCount,
194 : sal_Size * pWritten)
195 : {
196 0 : if (!m_pStream)
197 : {
198 : OSL_FAIL("SvLockBytes::WriteAt(): Bad stream");
199 0 : return ERRCODE_NONE;
200 : }
201 :
202 0 : m_pStream->Seek(nPos);
203 0 : sal_Size nTheWritten = m_pStream->Write(pBuffer, nCount);
204 0 : if (pWritten)
205 0 : *pWritten = nTheWritten;
206 0 : return m_pStream->GetErrorCode();
207 : }
208 :
209 : // virtual
210 0 : ErrCode SvLockBytes::Flush() const
211 : {
212 0 : if (!m_pStream)
213 : {
214 : OSL_FAIL("SvLockBytes::Flush(): Bad stream");
215 0 : return ERRCODE_NONE;
216 : }
217 :
218 0 : m_pStream->Flush();
219 0 : return m_pStream->GetErrorCode();
220 : }
221 :
222 : // virtual
223 0 : ErrCode SvLockBytes::SetSize(sal_uInt64 const nSize)
224 : {
225 0 : if (!m_pStream)
226 : {
227 : OSL_FAIL("SvLockBytes::SetSize(): Bad stream");
228 0 : return ERRCODE_NONE;
229 : }
230 :
231 0 : m_pStream->SetStreamSize(nSize);
232 0 : return m_pStream->GetErrorCode();
233 : }
234 :
235 0 : ErrCode SvLockBytes::Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const
236 : {
237 0 : if (!m_pStream)
238 : {
239 : OSL_FAIL("SvLockBytes::Stat(): Bad stream");
240 0 : return ERRCODE_NONE;
241 : }
242 :
243 0 : if (pStat)
244 : {
245 0 : sal_uInt64 const nPos = m_pStream->Tell();
246 0 : pStat->nSize = m_pStream->Seek(STREAM_SEEK_TO_END);
247 0 : m_pStream->Seek(nPos);
248 : }
249 0 : return ERRCODE_NONE;
250 : }
251 :
252 : // class SvOpenLockBytes
253 :
254 0 : TYPEINIT1(SvOpenLockBytes, SvLockBytes);
255 :
256 : // class SvAsyncLockBytes
257 :
258 0 : TYPEINIT1(SvAsyncLockBytes, SvOpenLockBytes);
259 :
260 : // virtual
261 0 : ErrCode SvAsyncLockBytes::ReadAt(sal_uInt64 const nPos, void * pBuffer, sal_Size nCount,
262 : sal_Size * pRead) const
263 : {
264 0 : if (m_bTerminated)
265 0 : return SvOpenLockBytes::ReadAt(nPos, pBuffer, nCount, pRead);
266 : else
267 : {
268 : sal_Size nTheCount =
269 0 : std::min<sal_Size>(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
270 : ErrCode nError = SvOpenLockBytes::ReadAt(nPos, pBuffer, nTheCount,
271 0 : pRead);
272 0 : return !nCount || nTheCount == nCount || nError ? nError :
273 0 : ERRCODE_IO_PENDING;
274 : }
275 : }
276 :
277 : // virtual
278 0 : ErrCode SvAsyncLockBytes::WriteAt(sal_uInt64 const nPos, const void * pBuffer,
279 : sal_Size nCount, sal_Size * pWritten)
280 : {
281 0 : if (m_bTerminated)
282 0 : return SvOpenLockBytes::WriteAt(nPos, pBuffer, nCount, pWritten);
283 : else
284 : {
285 : sal_Size nTheCount =
286 0 : std::min<sal_Size>(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
287 : ErrCode nError = SvOpenLockBytes::WriteAt(nPos, pBuffer, nTheCount,
288 0 : pWritten);
289 0 : return !nCount || nTheCount == nCount || nError ? nError :
290 0 : ERRCODE_IO_PENDING;
291 : }
292 : }
293 :
294 : // virtual
295 0 : ErrCode SvAsyncLockBytes::FillAppend(const void * pBuffer, sal_Size nCount,
296 : sal_Size * pWritten)
297 : {
298 : sal_Size nTheWritten;
299 : ErrCode nError = SvOpenLockBytes::WriteAt(m_nSize, pBuffer, nCount,
300 0 : &nTheWritten);
301 0 : if (!nError)
302 0 : m_nSize += nTheWritten;
303 0 : if (pWritten)
304 0 : *pWritten = nTheWritten;
305 0 : return nError;
306 : }
307 :
308 : // virtual
309 0 : sal_uInt64 SvAsyncLockBytes::Seek(sal_uInt64 const nPos)
310 : {
311 : // check if a truncated STREAM_SEEK_TO_END was passed
312 : assert(nPos != SAL_MAX_UINT32);
313 0 : if (nPos != STREAM_SEEK_TO_END)
314 0 : m_nSize = nPos;
315 0 : return m_nSize;
316 : }
317 :
318 : // class SvStream
319 :
320 73953 : sal_Size SvStream::GetData( void* pData, sal_Size nSize )
321 : {
322 73953 : if( !GetError() )
323 : {
324 : DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
325 : sal_Size nRet;
326 73669 : nError = xLockBytes->ReadAt(m_nActPos, pData, nSize, &nRet);
327 73669 : m_nActPos += nRet;
328 73669 : return nRet;
329 : }
330 284 : else return 0;
331 : }
332 :
333 23071 : sal_Size SvStream::PutData( const void* pData, sal_Size nSize )
334 : {
335 23071 : if( !GetError() )
336 : {
337 : DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
338 : sal_Size nRet;
339 23070 : nError = xLockBytes->WriteAt(m_nActPos, pData, nSize, &nRet);
340 23070 : m_nActPos += nRet;
341 23070 : return nRet;
342 : }
343 1 : else return 0;
344 : }
345 :
346 227432 : sal_uInt64 SvStream::SeekPos(sal_uInt64 const nPos)
347 : {
348 : // check if a truncated STREAM_SEEK_TO_END was passed
349 : assert(nPos != SAL_MAX_UINT32);
350 227432 : if( !GetError() && nPos == STREAM_SEEK_TO_END )
351 : {
352 : DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
353 43035 : SvLockBytesStat aStat;
354 43035 : xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
355 43035 : m_nActPos = aStat.nSize;
356 : }
357 : else
358 184397 : m_nActPos = nPos;
359 227432 : return m_nActPos;
360 : }
361 :
362 32724 : void SvStream::FlushData()
363 : {
364 32724 : if( !GetError() )
365 : {
366 : DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
367 32518 : nError = xLockBytes->Flush();
368 : }
369 32724 : }
370 :
371 3295 : void SvStream::SetSize(sal_uInt64 const nSize)
372 : {
373 : DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
374 3295 : nError = xLockBytes->SetSize( nSize );
375 3295 : }
376 :
377 86770 : void SvStream::ImpInit()
378 : {
379 86770 : m_nActPos = 0;
380 86770 : nCompressMode = COMPRESSMODE_NONE;
381 86770 : eStreamCharSet = osl_getThreadTextEncoding();
382 86770 : nCryptMask = 0;
383 86770 : bIsEof = false;
384 : #if defined UNX
385 86770 : eLineDelimiter = LINEEND_LF; // UNIX-Format
386 : #else
387 : eLineDelimiter = LINEEND_CRLF; // DOS-Format
388 : #endif
389 :
390 86770 : SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
391 :
392 86770 : m_nBufFilePos = 0;
393 86770 : nBufActualPos = 0;
394 86770 : bIsDirty = false;
395 86770 : bIsConsistent = true;
396 86770 : bIsWritable = true;
397 :
398 86770 : pRWBuf = 0;
399 86770 : pBufPos = 0;
400 86770 : nBufSize = 0;
401 86770 : nBufActualLen = 0;
402 86770 : bIoRead = false;
403 86770 : bIoWrite = false;
404 86770 : nBufFree = 0;
405 :
406 86770 : eStreamMode = 0;
407 :
408 86770 : nVersion = 0;
409 :
410 86770 : ClearError();
411 86770 : }
412 :
413 30140 : SvStream::SvStream( SvLockBytes* pLockBytesP )
414 : {
415 30140 : ImpInit();
416 30140 : xLockBytes = pLockBytesP;
417 30140 : if( pLockBytesP ) {
418 30140 : const SvStream* pStrm = pLockBytesP->GetStream();
419 30140 : if( pStrm ) {
420 0 : SetError( pStrm->GetErrorCode() );
421 : }
422 : }
423 30140 : SetBufferSize( 256 );
424 30140 : }
425 :
426 56630 : SvStream::SvStream()
427 : {
428 56630 : ImpInit();
429 56630 : }
430 :
431 202594 : SvStream::~SvStream()
432 : {
433 86253 : if ( xLockBytes.Is() )
434 30088 : Flush();
435 :
436 86253 : if( pRWBuf )
437 67494 : delete[] pRWBuf;
438 116341 : }
439 :
440 111138 : void SvStream::ClearError()
441 : {
442 111138 : bIsEof = false;
443 111138 : nError = SVSTREAM_OK;
444 111138 : }
445 :
446 2696637 : void SvStream::SetError( sal_uInt32 nErrorCode )
447 : {
448 2696637 : if ( nError == SVSTREAM_OK )
449 2696356 : nError = nErrorCode;
450 2696637 : }
451 :
452 151193 : void SvStream::SetNumberFormatInt( sal_uInt16 nNewFormat )
453 : {
454 151193 : nNumberFormatInt = nNewFormat;
455 151193 : bSwap = false;
456 : #ifdef OSL_BIGENDIAN
457 : if( nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN )
458 : bSwap = true;
459 : #else
460 151193 : if( nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN )
461 5443 : bSwap = true;
462 : #endif
463 151193 : }
464 :
465 125056 : void SvStream::SetBufferSize( sal_uInt16 nBufferSize )
466 : {
467 125056 : sal_uInt64 const nActualFilePos = Tell();
468 125056 : bool bDontSeek = (pRWBuf == 0);
469 :
470 125056 : if( bIsDirty && bIsConsistent && bIsWritable ) // due to Windows NT: Access denied
471 26 : Flush();
472 :
473 125056 : if( nBufSize )
474 : {
475 35957 : delete[] pRWBuf;
476 35957 : m_nBufFilePos += nBufActualPos;
477 : }
478 :
479 125056 : pRWBuf = 0;
480 125056 : nBufActualLen = 0;
481 125056 : nBufActualPos = 0;
482 125056 : nBufSize = nBufferSize;
483 125056 : if( nBufSize )
484 103968 : pRWBuf = new sal_uInt8[ nBufSize ];
485 125056 : bIsConsistent = true;
486 125056 : pBufPos = pRWBuf;
487 125056 : bIoRead = bIoWrite = false;
488 125056 : if( !bDontSeek )
489 35957 : SeekPos( nActualFilePos );
490 125056 : }
491 :
492 9141 : void SvStream::ClearBuffer()
493 : {
494 9141 : nBufActualLen = 0;
495 9141 : nBufActualPos = 0;
496 9141 : m_nBufFilePos = 0;
497 9141 : pBufPos = pRWBuf;
498 9141 : bIsDirty = false;
499 9141 : bIsConsistent = true;
500 9141 : bIoRead = bIoWrite = false;
501 :
502 9141 : bIsEof = false;
503 9141 : }
504 :
505 8348 : void SvStream::ResetError()
506 : {
507 8348 : ClearError();
508 8348 : }
509 :
510 276 : bool SvStream::ReadByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
511 : sal_Int32 nMaxBytesToRead )
512 : {
513 276 : OString aStr;
514 276 : bool bRet = ReadLine( aStr, nMaxBytesToRead);
515 276 : rStr = OStringToOUString(aStr, eSrcCharSet);
516 276 : return bRet;
517 : }
518 :
519 603885 : bool SvStream::ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead )
520 : {
521 : sal_Char buf[256+1];
522 603885 : bool bEnd = false;
523 603885 : sal_uInt64 nOldFilePos = Tell();
524 603885 : sal_Char c = 0;
525 603885 : sal_Size nTotalLen = 0;
526 :
527 603885 : OStringBuffer aBuf(4096);
528 1810655 : while( !bEnd && !GetError() ) // Don't test for EOF as we
529 : // are reading block-wise!
530 : {
531 603928 : sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
532 603928 : if ( !nLen )
533 : {
534 1043 : if ( aBuf.isEmpty() )
535 : {
536 : // Exit on first block-read error
537 1040 : bIsEof = true;
538 1040 : rStr = OString();
539 1040 : return false;
540 : }
541 : else
542 3 : break;
543 : }
544 :
545 : sal_uInt16 j, n;
546 14943268 : for( j = n = 0; j < nLen ; ++j )
547 : {
548 14943214 : c = buf[j];
549 14943214 : if ( c == '\n' || c == '\r' )
550 : {
551 602831 : bEnd = true;
552 602831 : break;
553 : }
554 14340383 : if ( n < j )
555 0 : buf[n] = c;
556 14340383 : ++n;
557 : }
558 602885 : nTotalLen += j;
559 602885 : if (nTotalLen > static_cast<sal_Size>(nMaxBytesToRead))
560 : {
561 0 : n -= nTotalLen - nMaxBytesToRead;
562 0 : nTotalLen = nMaxBytesToRead;
563 0 : bEnd = true;
564 : }
565 602885 : if ( n )
566 541356 : aBuf.append(buf, n);
567 : }
568 :
569 602845 : if ( !bEnd && !GetError() && !aBuf.isEmpty() )
570 3 : bEnd = true;
571 :
572 602845 : nOldFilePos += nTotalLen;
573 602845 : if( Tell() > nOldFilePos )
574 602831 : nOldFilePos++;
575 602845 : Seek( nOldFilePos ); // Seek pointer due to BlockRead above
576 :
577 602845 : if ( bEnd && (c=='\r' || c=='\n') ) // Special treatment for DOS files
578 : {
579 : char cTemp;
580 602831 : sal_Size nLen = Read((char*)&cTemp , sizeof(cTemp) );
581 602831 : if ( nLen ) {
582 602261 : if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
583 602261 : Seek( nOldFilePos );
584 : }
585 : }
586 :
587 602845 : if ( bEnd )
588 602834 : bIsEof = false;
589 602845 : rStr = aBuf.makeStringAndClear();
590 602845 : return bEnd;
591 : }
592 :
593 4 : bool SvStream::ReadUniStringLine( OUString& rStr, sal_Int32 nMaxCodepointsToRead )
594 : {
595 : sal_Unicode buf[256+1];
596 4 : bool bEnd = false;
597 4 : sal_uInt64 nOldFilePos = Tell();
598 4 : sal_Unicode c = 0;
599 4 : sal_Size nTotalLen = 0;
600 :
601 : DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "ReadUniStringLine: swapping sizeof(sal_Unicode) not implemented" );
602 :
603 4 : OUStringBuffer aBuf(4096);
604 10 : while( !bEnd && !GetError() ) // Don't test for EOF as we
605 : // are reading block-wise!
606 : {
607 6 : sal_uInt16 nLen = (sal_uInt16)Read( (char*)buf, sizeof(buf)-sizeof(sal_Unicode) );
608 6 : nLen /= sizeof(sal_Unicode);
609 6 : if ( !nLen )
610 : {
611 4 : if ( aBuf.isEmpty() )
612 : {
613 : // exit on first BlockRead error
614 2 : bIsEof = true;
615 2 : rStr = OUString();
616 2 : return false;
617 : }
618 : else
619 2 : break;
620 : }
621 :
622 : sal_uInt16 j, n;
623 12 : for( j = n = 0; j < nLen ; ++j )
624 : {
625 10 : if ( bSwap )
626 0 : SwapUShort( buf[n] );
627 10 : c = buf[j];
628 10 : if ( c == '\n' || c == '\r' )
629 : {
630 0 : bEnd = true;
631 0 : break;
632 : }
633 : // erAck 26.02.01: Old behavior was no special treatment of '\0'
634 : // character here, but a following rStr+=c did ignore it. Is this
635 : // really intended? Or should a '\0' better terminate a line?
636 : // The nOldFilePos stuff wasn't correct then anyways.
637 10 : if ( c )
638 : {
639 10 : if ( n < j )
640 0 : buf[n] = c;
641 10 : ++n;
642 : }
643 : }
644 2 : nTotalLen += j;
645 2 : if (nTotalLen > static_cast<sal_Size>(nMaxCodepointsToRead))
646 : {
647 0 : n -= nTotalLen - nMaxCodepointsToRead;
648 0 : nTotalLen = nMaxCodepointsToRead;
649 0 : bEnd = true;
650 : }
651 2 : if ( n )
652 2 : aBuf.append( buf, n );
653 : }
654 :
655 2 : if ( !bEnd && !GetError() && !aBuf.isEmpty() )
656 2 : bEnd = true;
657 :
658 2 : nOldFilePos += nTotalLen * sizeof(sal_Unicode);
659 2 : if( Tell() > nOldFilePos )
660 0 : nOldFilePos += sizeof(sal_Unicode);
661 2 : Seek( nOldFilePos ); // seek due to BlockRead above
662 :
663 2 : if ( bEnd && (c=='\r' || c=='\n') ) // special treatment for DOS files
664 : {
665 : sal_Unicode cTemp;
666 0 : Read( (char*)&cTemp, sizeof(cTemp) );
667 0 : if ( bSwap )
668 0 : SwapUShort( cTemp );
669 0 : if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
670 0 : Seek( nOldFilePos );
671 : }
672 :
673 2 : if ( bEnd )
674 2 : bIsEof = false;
675 2 : rStr = aBuf.makeStringAndClear();
676 2 : return bEnd;
677 : }
678 :
679 280 : bool SvStream::ReadUniOrByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
680 : sal_Int32 nMaxCodepointsToRead )
681 : {
682 280 : if ( eSrcCharSet == RTL_TEXTENCODING_UNICODE )
683 4 : return ReadUniStringLine( rStr, nMaxCodepointsToRead );
684 : else
685 276 : return ReadByteStringLine( rStr, eSrcCharSet, nMaxCodepointsToRead );
686 : }
687 :
688 2 : OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStream)
689 : {
690 2 : OStringBuffer aOutput(256);
691 :
692 : sal_Char buf[ 256 + 1 ];
693 2 : bool bEnd = false;
694 2 : sal_uInt64 nFilePos = rStream.Tell();
695 :
696 6 : while( !bEnd && !rStream.GetError() )
697 : {
698 2 : sal_Size nLen = rStream.Read(buf, sizeof(buf)-1);
699 2 : if (!nLen)
700 0 : break;
701 :
702 2 : sal_Size nReallyRead = nLen;
703 2 : const sal_Char* pPtr = buf;
704 13 : while (nLen && *pPtr)
705 9 : ++pPtr, --nLen;
706 :
707 2 : bEnd = ( nReallyRead < sizeof(buf)-1 ) // read less than attempted to read
708 4 : || ( ( nLen > 0 ) // OR it is inside the block we read
709 0 : && ( 0 == *pPtr ) // AND found a string terminator
710 2 : );
711 :
712 2 : aOutput.append(buf, pPtr - buf);
713 : }
714 :
715 2 : nFilePos += aOutput.getLength();
716 2 : if (rStream.Tell() > nFilePos)
717 1 : rStream.Seek(nFilePos+1); // seek due to FileRead above
718 2 : return aOutput.makeStringAndClear();
719 : }
720 :
721 0 : OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStream, rtl_TextEncoding eEnc)
722 : {
723 : return OStringToOUString(
724 0 : read_zeroTerminated_uInt8s_ToOString(rStream), eEnc);
725 : }
726 :
727 : /** Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
728 : returned value is number of bytes written */
729 69115 : sal_Size write_uInt16s_FromOUString(SvStream& rStrm, const OUString& rStr,
730 : sal_Size nUnits)
731 : {
732 : DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "write_uInt16s_FromOUString: swapping sizeof(sal_Unicode) not implemented" );
733 : sal_Size nWritten;
734 69115 : if (!rStrm.IsEndianSwap())
735 69115 : nWritten = rStrm.Write( (char*)rStr.getStr(), nUnits * sizeof(sal_Unicode) );
736 : else
737 : {
738 0 : sal_Size nLen = nUnits;
739 : sal_Unicode aBuf[384];
740 0 : sal_Unicode* const pTmp = ( nLen > 384 ? new sal_Unicode[nLen] : aBuf);
741 0 : memcpy( pTmp, rStr.getStr(), nLen * sizeof(sal_Unicode) );
742 0 : sal_Unicode* p = pTmp;
743 0 : const sal_Unicode* const pStop = pTmp + nLen;
744 0 : while ( p < pStop )
745 : {
746 0 : SwapUShort( *p );
747 0 : p++;
748 : }
749 0 : nWritten = rStrm.Write( (char*)pTmp, nLen * sizeof(sal_Unicode) );
750 0 : if ( pTmp != aBuf )
751 0 : delete [] pTmp;
752 : }
753 69115 : return nWritten;
754 : }
755 :
756 10161 : bool SvStream::WriteUnicodeOrByteText( const OUString& rStr, rtl_TextEncoding eDestCharSet )
757 : {
758 10161 : if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
759 : {
760 10159 : write_uInt16s_FromOUString(*this, rStr, rStr.getLength());
761 10159 : return nError == SVSTREAM_OK;
762 : }
763 : else
764 : {
765 2 : OString aStr(OUStringToOString(rStr, eDestCharSet));
766 2 : write_uInt8s_FromOString(*this, aStr, aStr.getLength());
767 2 : return nError == SVSTREAM_OK;
768 : }
769 : }
770 :
771 0 : bool SvStream::WriteByteStringLine( const OUString& rStr, rtl_TextEncoding eDestCharSet )
772 : {
773 0 : return WriteLine(OUStringToOString(rStr, eDestCharSet));
774 : }
775 :
776 174427 : bool SvStream::WriteLine(const OString& rStr)
777 : {
778 174427 : Write(rStr.getStr(), rStr.getLength());
779 174427 : endl(*this);
780 174427 : return nError == SVSTREAM_OK;
781 : }
782 :
783 0 : bool SvStream::WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet )
784 : {
785 0 : if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
786 0 : WriteChar(ch);
787 : else
788 : {
789 0 : OString aStr(&ch, 1, eDestCharSet);
790 0 : Write(aStr.getStr(), aStr.getLength());
791 : }
792 0 : return nError == SVSTREAM_OK;
793 : }
794 :
795 0 : bool SvStream::StartWritingUnicodeText()
796 : {
797 0 : SetEndianSwap( false ); // write native format
798 : // BOM, Byte Order Mark, U+FEFF, see
799 : // http://www.unicode.org/faq/utf_bom.html#BOM
800 : // Upon read: 0xfeff(-257) => no swap; 0xfffe(-2) => swap
801 0 : WriteUInt16( 0xfeff );
802 0 : return nError == SVSTREAM_OK;
803 : }
804 :
805 8 : bool SvStream::StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet )
806 : {
807 8 : if (!( eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
808 : eReadBomCharSet == RTL_TEXTENCODING_UNICODE ||
809 3 : eReadBomCharSet == RTL_TEXTENCODING_UTF8))
810 3 : return true; // nothing to read
811 :
812 5 : bool bTryUtf8 = false;
813 : sal_uInt16 nFlag;
814 5 : sal_sSize nBack = sizeof(nFlag);
815 5 : this->ReadUInt16( nFlag );
816 5 : switch ( nFlag )
817 : {
818 : case 0xfeff :
819 : // native UTF-16
820 0 : if ( eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
821 : eReadBomCharSet == RTL_TEXTENCODING_UNICODE)
822 0 : nBack = 0;
823 0 : break;
824 : case 0xfffe :
825 : // swapped UTF-16
826 0 : if ( eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
827 : eReadBomCharSet == RTL_TEXTENCODING_UNICODE)
828 : {
829 0 : SetEndianSwap( !bSwap );
830 0 : nBack = 0;
831 : }
832 0 : break;
833 : case 0xefbb :
834 0 : if (nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN &&
835 0 : (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
836 : eReadBomCharSet == RTL_TEXTENCODING_UTF8))
837 0 : bTryUtf8 = true;
838 0 : break;
839 : case 0xbbef :
840 0 : if (nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN &&
841 0 : (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
842 : eReadBomCharSet == RTL_TEXTENCODING_UTF8))
843 0 : bTryUtf8 = true;
844 0 : break;
845 : default:
846 : ; // nothing
847 : }
848 5 : if (bTryUtf8)
849 : {
850 : unsigned char nChar;
851 0 : nBack += sizeof(nChar);
852 0 : this->ReadUChar( nChar );
853 0 : if (nChar == 0xbf)
854 0 : nBack = 0; // it is UTF-8
855 : }
856 5 : if (nBack)
857 5 : SeekRel( -nBack ); // no BOM, pure data
858 5 : return nError == SVSTREAM_OK;
859 : }
860 :
861 1010078 : sal_uInt64 SvStream::SeekRel(sal_Int64 const nPos)
862 : {
863 1010078 : sal_uInt64 nActualPos = Tell();
864 :
865 1010078 : if ( nPos >= 0 )
866 : {
867 947881 : if (SAL_MAX_UINT64 - nActualPos > static_cast<sal_uInt64>(nPos))
868 947881 : nActualPos += nPos;
869 : }
870 : else
871 : {
872 62197 : sal_uInt64 const nAbsPos = static_cast<sal_uInt64>(-nPos);
873 62197 : if ( nActualPos >= nAbsPos )
874 62197 : nActualPos -= nAbsPos;
875 : }
876 :
877 1010078 : pBufPos = pRWBuf + nActualPos;
878 1010078 : return Seek( nActualPos );
879 : }
880 :
881 5599941 : SvStream& SvStream::ReadUInt16(sal_uInt16& r)
882 : {
883 5599941 : sal_uInt16 n = 0;
884 5599941 : READNUMBER_WITHOUT_SWAP(sal_uInt16, n)
885 5599941 : if (good())
886 : {
887 5572867 : if (bSwap)
888 10545 : SwapUShort(n);
889 5572867 : r = n;
890 : }
891 5599941 : return *this;
892 : }
893 :
894 809121 : SvStream& SvStream::ReadUInt32(sal_uInt32& r)
895 : {
896 809121 : sal_uInt32 n = 0;
897 809121 : READNUMBER_WITHOUT_SWAP(sal_uInt32, n)
898 809121 : if (good())
899 : {
900 807754 : if (bSwap)
901 49141 : SwapULong(n);
902 807754 : r = n;
903 : }
904 809121 : return *this;
905 : }
906 :
907 0 : SvStream& SvStream::ReadUInt64(sal_uInt64& r)
908 : {
909 0 : sal_uInt64 n = 0;
910 0 : READNUMBER_WITHOUT_SWAP(sal_uInt64, n)
911 0 : if (good())
912 : {
913 0 : if (bSwap)
914 0 : SwapUInt64(n);
915 0 : r = n;
916 : }
917 0 : return *this;
918 : }
919 :
920 348793 : SvStream& SvStream::ReadInt16(sal_Int16& r)
921 : {
922 348793 : sal_Int16 n = 0;
923 348793 : READNUMBER_WITHOUT_SWAP(sal_Int16, n)
924 348793 : if (good())
925 : {
926 226063 : if (bSwap)
927 10248 : SwapShort(n);
928 226063 : r = n;
929 : }
930 348793 : return *this;
931 : }
932 :
933 840648 : SvStream& SvStream::ReadInt32(sal_Int32& r)
934 : {
935 840648 : sal_Int32 n = 0;
936 840648 : READNUMBER_WITHOUT_SWAP(sal_Int32, n)
937 840648 : if (good())
938 : {
939 817788 : if (bSwap)
940 19843 : SwapLongInt(n);
941 817788 : r = n;
942 : }
943 840648 : return *this;
944 : }
945 :
946 0 : SvStream& SvStream::ReadInt64(sal_Int64& r)
947 : {
948 0 : sal_Int64 n = 0;
949 0 : READNUMBER_WITHOUT_SWAP(sal_Int64, n)
950 0 : if (good())
951 : {
952 0 : if (bSwap)
953 0 : SwapInt64(n);
954 0 : r = n;
955 : }
956 0 : return *this;
957 : }
958 :
959 2368 : SvStream& SvStream::ReadSChar( signed char& r )
960 : {
961 3700 : if( (bIoRead || !bIsConsistent) &&
962 1332 : sizeof(signed char) <= nBufFree )
963 : {
964 1331 : r = *pBufPos;
965 1331 : nBufActualPos += sizeof(signed char);
966 1331 : pBufPos += sizeof(signed char);
967 1331 : nBufFree -= sizeof(signed char);
968 : }
969 : else
970 1037 : Read( (char*)&r, sizeof(signed char) );
971 2368 : return *this;
972 : }
973 :
974 : // Special treatment for Chars due to PutBack
975 :
976 1735456 : SvStream& SvStream::ReadChar( char& r )
977 : {
978 3390076 : if( (bIoRead || !bIsConsistent) &&
979 1654620 : sizeof(char) <= nBufFree )
980 : {
981 1654342 : r = *pBufPos;
982 1654342 : nBufActualPos += sizeof(char);
983 1654342 : pBufPos += sizeof(char);
984 1654342 : nBufFree -= sizeof(char);
985 : }
986 : else
987 81114 : Read( (char*)&r, sizeof(char) );
988 1735456 : return *this;
989 : }
990 :
991 5969155 : SvStream& SvStream::ReadUChar( unsigned char& r )
992 : {
993 11821071 : if( (bIoRead || !bIsConsistent) &&
994 5851916 : sizeof(char) <= nBufFree )
995 : {
996 5786902 : r = *pBufPos;
997 5786902 : nBufActualPos += sizeof(char);
998 5786902 : pBufPos += sizeof(char);
999 5786902 : nBufFree -= sizeof(char);
1000 : }
1001 : else
1002 182253 : Read( (char*)&r, sizeof(char) );
1003 5969155 : return *this;
1004 : }
1005 :
1006 207655 : SvStream& SvStream::ReadCharAsBool( bool& r )
1007 : {
1008 415310 : if( (bIoRead || !bIsConsistent) &&
1009 207655 : sizeof(char) <= nBufFree )
1010 : {
1011 : SAL_WARN_IF(
1012 : *pBufPos > 1, "tools.stream", unsigned(*pBufPos) << " not 0/1");
1013 207600 : r = *pBufPos != 0;
1014 207600 : nBufActualPos += sizeof(char);
1015 207600 : pBufPos += sizeof(char);
1016 207600 : nBufFree -= sizeof(char);
1017 : }
1018 : else
1019 : {
1020 : unsigned char c;
1021 55 : if (Read(&c, 1) == 1)
1022 : {
1023 : SAL_WARN_IF(c > 1, "tools.stream", unsigned(c) << " not 0/1");
1024 55 : r = c != 0;
1025 : }
1026 : }
1027 207655 : return *this;
1028 : }
1029 :
1030 6222 : SvStream& SvStream::ReadFloat(float& r)
1031 : {
1032 6222 : float n = 0;
1033 6222 : READNUMBER_WITHOUT_SWAP(float, n)
1034 6222 : if (good())
1035 : {
1036 : #if defined UNX
1037 6222 : if (bSwap)
1038 0 : SwapFloat(n);
1039 : #endif
1040 6222 : r = n;
1041 : }
1042 6222 : return *this;
1043 : }
1044 :
1045 7731 : SvStream& SvStream::ReadDouble(double& r)
1046 : {
1047 7731 : double n = 0;
1048 7731 : READNUMBER_WITHOUT_SWAP(double, n)
1049 7731 : if (good())
1050 : {
1051 : #if defined UNX
1052 7731 : if (bSwap)
1053 0 : SwapDouble(n);
1054 : #endif
1055 7731 : r = n;
1056 : }
1057 7731 : return *this;
1058 : }
1059 :
1060 27 : SvStream& SvStream::ReadStream( SvStream& rStream )
1061 : {
1062 27 : const sal_uInt32 cBufLen = 0x8000;
1063 27 : char* pBuf = new char[ cBufLen ];
1064 :
1065 : sal_uInt32 nCount;
1066 51 : do {
1067 51 : nCount = Read( pBuf, cBufLen );
1068 51 : rStream.Write( pBuf, nCount );
1069 : } while( nCount == cBufLen );
1070 :
1071 27 : delete[] pBuf;
1072 27 : return *this;
1073 : }
1074 :
1075 2912688 : SvStream& SvStream::WriteUInt16( sal_uInt16 v )
1076 : {
1077 2912688 : if( bSwap )
1078 0 : SwapUShort(v);
1079 2912688 : WRITENUMBER_WITHOUT_SWAP(sal_uInt16,v)
1080 2912688 : return *this;
1081 : }
1082 :
1083 868323 : SvStream& SvStream::WriteUInt32( sal_uInt32 v )
1084 : {
1085 868323 : if( bSwap )
1086 992 : SwapULong(v);
1087 868323 : WRITENUMBER_WITHOUT_SWAP(sal_uInt32,v)
1088 868323 : return *this;
1089 : }
1090 :
1091 563 : SvStream& SvStream::WriteUInt64( sal_uInt64 v )
1092 : {
1093 563 : if( bSwap )
1094 0 : SwapUInt64(v);
1095 563 : WRITENUMBER_WITHOUT_SWAP(sal_uInt64,v)
1096 563 : return *this;
1097 : }
1098 :
1099 69237 : SvStream& SvStream::WriteInt16( sal_Int16 v )
1100 : {
1101 69237 : if( bSwap )
1102 0 : SwapShort(v);
1103 69237 : WRITENUMBER_WITHOUT_SWAP(sal_Int16,v)
1104 69237 : return *this;
1105 : }
1106 :
1107 418370 : SvStream& SvStream::WriteInt32( sal_Int32 v )
1108 : {
1109 418370 : if( bSwap )
1110 0 : SwapLongInt(v);
1111 418370 : WRITENUMBER_WITHOUT_SWAP(sal_Int32,v)
1112 418370 : return *this;
1113 : }
1114 :
1115 0 : SvStream& SvStream::WriteInt64 (sal_Int64 v)
1116 : {
1117 0 : if( bSwap )
1118 0 : SwapInt64(v);
1119 0 : WRITENUMBER_WITHOUT_SWAP(sal_Int64,v)
1120 0 : return *this;
1121 : }
1122 :
1123 2828 : SvStream& SvStream::WriteSChar( signed char v )
1124 : {
1125 : //SDO
1126 2828 : if(bIoWrite && sizeof(signed char) <= nBufFree )
1127 : {
1128 2828 : *pBufPos = v;
1129 2828 : pBufPos++; // sizeof(char);
1130 2828 : nBufActualPos++;
1131 2828 : if( nBufActualPos > nBufActualLen ) // Append ?
1132 2828 : nBufActualLen = nBufActualPos;
1133 2828 : nBufFree--; // = sizeof(char);
1134 2828 : bIsDirty = true;
1135 : }
1136 : else
1137 0 : Write( (char*)&v, sizeof(signed char) );
1138 2828 : return *this;
1139 : }
1140 :
1141 : // Special treatment for Chars due to PutBack
1142 :
1143 812596 : SvStream& SvStream::WriteChar( char v )
1144 : {
1145 : //SDO
1146 812596 : if(bIoWrite && sizeof(char) <= nBufFree )
1147 : {
1148 794227 : *pBufPos = v;
1149 794227 : pBufPos++; // sizeof(char);
1150 794227 : nBufActualPos++;
1151 794227 : if( nBufActualPos > nBufActualLen ) // Append ?
1152 763721 : nBufActualLen = nBufActualPos;
1153 794227 : nBufFree--; // = sizeof(char);
1154 794227 : bIsDirty = true;
1155 : }
1156 : else
1157 18369 : Write( (char*)&v, sizeof(char) );
1158 812596 : return *this;
1159 : }
1160 :
1161 894880 : SvStream& SvStream::WriteUChar( unsigned char v )
1162 : {
1163 : //SDO
1164 894880 : if(bIoWrite && sizeof(char) <= nBufFree )
1165 : {
1166 374083 : *(unsigned char*)pBufPos = v;
1167 374083 : pBufPos++; // = sizeof(char);
1168 374083 : nBufActualPos++; // = sizeof(char);
1169 374083 : if( nBufActualPos > nBufActualLen ) // Append ?
1170 374083 : nBufActualLen = nBufActualPos;
1171 374083 : nBufFree--;
1172 374083 : bIsDirty = true;
1173 : }
1174 : else
1175 520797 : Write( (char*)&v, sizeof(char) );
1176 894880 : return *this;
1177 : }
1178 :
1179 6888 : SvStream& SvStream::WriteUInt8( sal_uInt8 v )
1180 : {
1181 6888 : return WriteUChar(v);
1182 : }
1183 :
1184 0 : SvStream& SvStream::WriteUnicode( sal_Unicode v )
1185 : {
1186 0 : return WriteUInt16(v);
1187 : }
1188 :
1189 162 : SvStream& SvStream::WriteFloat( float v )
1190 : {
1191 : #ifdef UNX
1192 162 : if( bSwap )
1193 0 : SwapFloat(v);
1194 : #endif
1195 162 : WRITENUMBER_WITHOUT_SWAP(float,v)
1196 162 : return *this;
1197 : }
1198 :
1199 4955 : SvStream& SvStream::WriteDouble ( const double& r )
1200 : {
1201 : #if defined UNX
1202 4955 : if( bSwap )
1203 : {
1204 0 : double nHelp = r;
1205 0 : SwapDouble(nHelp);
1206 0 : WRITENUMBER_WITHOUT_SWAP(double,nHelp)
1207 0 : return *this;
1208 : }
1209 : else
1210 : #endif
1211 : {
1212 4955 : WRITENUMBER_WITHOUT_SWAP(double,r);
1213 : }
1214 4955 : return *this;
1215 : }
1216 :
1217 1074382 : SvStream& SvStream::WriteCharPtr( const char* pBuf )
1218 : {
1219 1074382 : Write( pBuf, strlen( pBuf ) );
1220 1074382 : return *this;
1221 : }
1222 :
1223 29 : SvStream& SvStream::WriteStream( SvStream& rStream )
1224 : {
1225 29 : const sal_uInt32 cBufLen = 0x8000;
1226 29 : char* pBuf = new char[ cBufLen ];
1227 : sal_uInt32 nCount;
1228 136 : do {
1229 136 : nCount = rStream.Read( pBuf, cBufLen );
1230 136 : Write( pBuf, nCount );
1231 : } while( nCount == cBufLen );
1232 :
1233 29 : delete[] pBuf;
1234 29 : return *this;
1235 : }
1236 :
1237 118558 : OUString SvStream::ReadUniOrByteString( rtl_TextEncoding eSrcCharSet )
1238 : {
1239 : // read UTF-16 string directly from stream ?
1240 118558 : if (eSrcCharSet == RTL_TEXTENCODING_UNICODE)
1241 13356 : return read_uInt32_lenPrefixed_uInt16s_ToOUString(*this);
1242 105202 : return read_uInt16_lenPrefixed_uInt8s_ToOUString(*this, eSrcCharSet);
1243 : }
1244 :
1245 138663 : SvStream& SvStream::WriteUniOrByteString( const OUString& rStr, rtl_TextEncoding eDestCharSet )
1246 : {
1247 : // write UTF-16 string directly into stream ?
1248 138663 : if (eDestCharSet == RTL_TEXTENCODING_UNICODE)
1249 13371 : write_uInt32_lenPrefixed_uInt16s_FromOUString(*this, rStr);
1250 : else
1251 125292 : write_uInt16_lenPrefixed_uInt8s_FromOUString(*this, rStr, eDestCharSet);
1252 138663 : return *this;
1253 : }
1254 :
1255 4803229 : sal_Size SvStream::Read( void* pData, sal_Size nCount )
1256 : {
1257 4803229 : sal_Size nSaveCount = nCount;
1258 4803229 : if( !bIsConsistent )
1259 0 : RefreshBuffer();
1260 :
1261 4803229 : if( !pRWBuf )
1262 : {
1263 1437024 : nCount = GetData( (char*)pData,nCount);
1264 1437024 : if( nCryptMask )
1265 0 : EncryptBuffer(pData, nCount);
1266 1437024 : m_nBufFilePos += nCount;
1267 : }
1268 : else
1269 : {
1270 : // check if block is completely within buffer
1271 3366205 : bIoRead = true;
1272 3366205 : bIoWrite = false;
1273 3366205 : if( nCount <= (sal_Size)(nBufActualLen - nBufActualPos ) )
1274 : {
1275 : // => yes
1276 2926843 : memcpy(pData, pBufPos, (size_t) nCount);
1277 2926843 : nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1278 2926843 : pBufPos += nCount;
1279 2926843 : nBufFree = nBufFree - (sal_uInt16)nCount;
1280 : }
1281 : else
1282 : {
1283 439362 : if( bIsDirty ) // Does stream require a flush?
1284 : {
1285 266 : SeekPos(m_nBufFilePos);
1286 266 : if( nCryptMask )
1287 0 : CryptAndWriteBuffer(pRWBuf, nBufActualLen);
1288 : else
1289 266 : PutData( pRWBuf, nBufActualLen );
1290 266 : bIsDirty = false;
1291 : }
1292 :
1293 : // Does data block fit into buffer?
1294 439362 : if( nCount > nBufSize )
1295 : {
1296 : // => No! Thus read directly
1297 : // into target area without using the buffer
1298 :
1299 61660 : bIoRead = false;
1300 :
1301 61660 : SeekPos(m_nBufFilePos + nBufActualPos);
1302 61660 : nBufActualLen = 0;
1303 61660 : pBufPos = pRWBuf;
1304 61660 : nCount = GetData( (char*)pData, nCount );
1305 61660 : if( nCryptMask )
1306 0 : EncryptBuffer(pData, nCount);
1307 61660 : m_nBufFilePos += nCount;
1308 61660 : m_nBufFilePos += nBufActualPos;
1309 61660 : nBufActualPos = 0;
1310 : }
1311 : else
1312 : {
1313 : // => Yes. Fill buffer first, then copy to target area
1314 :
1315 377702 : m_nBufFilePos += nBufActualPos;
1316 377702 : SeekPos(m_nBufFilePos);
1317 :
1318 : // TODO: Typecast before GetData, sal_uInt16 nCountTmp
1319 377702 : sal_Size nCountTmp = GetData( pRWBuf, nBufSize );
1320 377702 : if( nCryptMask )
1321 0 : EncryptBuffer(pRWBuf, nCountTmp);
1322 377702 : nBufActualLen = (sal_uInt16)nCountTmp;
1323 377702 : if( nCount > nCountTmp )
1324 : {
1325 215738 : nCount = nCountTmp; // trim count back, EOF see below
1326 : }
1327 377702 : memcpy( pData, pRWBuf, (size_t)nCount );
1328 377702 : nBufActualPos = (sal_uInt16)nCount;
1329 377702 : pBufPos = pRWBuf + nCount;
1330 : }
1331 : }
1332 : }
1333 4803229 : bIsEof = false;
1334 4803229 : nBufFree = nBufActualLen - nBufActualPos;
1335 4803229 : if( nCount != nSaveCount && nError != ERRCODE_IO_PENDING )
1336 272948 : bIsEof = true;
1337 4803229 : if( nCount == nSaveCount && nError == ERRCODE_IO_PENDING )
1338 0 : nError = ERRCODE_NONE;
1339 4803229 : return nCount;
1340 : }
1341 :
1342 4800173 : sal_Size SvStream::Write( const void* pData, sal_Size nCount )
1343 : {
1344 4800173 : if( !nCount )
1345 65305 : return 0;
1346 4734868 : if( !bIsWritable )
1347 : {
1348 0 : SetError( ERRCODE_IO_CANTWRITE );
1349 0 : return 0;
1350 : }
1351 4734868 : if( !bIsConsistent )
1352 0 : RefreshBuffer(); // Remove changes in buffer through PutBack
1353 :
1354 4734868 : if( !pRWBuf )
1355 : {
1356 916744 : if( nCryptMask )
1357 0 : nCount = CryptAndWriteBuffer( pData, nCount );
1358 : else
1359 916744 : nCount = PutData( (char*)pData, nCount );
1360 916744 : m_nBufFilePos += nCount;
1361 916744 : return nCount;
1362 : }
1363 :
1364 3818124 : bIoRead = false;
1365 3818124 : bIoWrite = true;
1366 3818124 : if( nCount <= (sal_Size)(nBufSize - nBufActualPos) )
1367 : {
1368 3651661 : memcpy( pBufPos, pData, (size_t)nCount );
1369 3651661 : nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1370 : // Update length if buffer was updated
1371 3651661 : if( nBufActualPos > nBufActualLen )
1372 3566136 : nBufActualLen = nBufActualPos;
1373 :
1374 3651661 : pBufPos += nCount;
1375 3651661 : bIsDirty = true;
1376 : }
1377 : else
1378 : {
1379 : // Does stream require flushing?
1380 166463 : if( bIsDirty )
1381 : {
1382 136385 : SeekPos(m_nBufFilePos);
1383 136385 : if( nCryptMask )
1384 0 : CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1385 : else
1386 136385 : PutData( pRWBuf, nBufActualLen );
1387 136385 : bIsDirty = false;
1388 : }
1389 :
1390 : // Does data block fit into buffer?
1391 166463 : if( nCount > nBufSize )
1392 : {
1393 37587 : bIoWrite = false;
1394 37587 : m_nBufFilePos += nBufActualPos;
1395 37587 : nBufActualLen = 0;
1396 37587 : nBufActualPos = 0;
1397 37587 : pBufPos = pRWBuf;
1398 37587 : SeekPos(m_nBufFilePos);
1399 37587 : if( nCryptMask )
1400 0 : nCount = CryptAndWriteBuffer( pData, nCount );
1401 : else
1402 37587 : nCount = PutData( (char*)pData, nCount );
1403 37587 : m_nBufFilePos += nCount;
1404 : }
1405 : else
1406 : {
1407 : // Copy block to buffer
1408 128876 : memcpy( pRWBuf, pData, (size_t)nCount );
1409 :
1410 : // Mind the order!
1411 128876 : m_nBufFilePos += nBufActualPos;
1412 128876 : nBufActualPos = (sal_uInt16)nCount;
1413 128876 : pBufPos = pRWBuf + nCount;
1414 128876 : nBufActualLen = (sal_uInt16)nCount;
1415 128876 : bIsDirty = true;
1416 : }
1417 : }
1418 3818124 : nBufFree = nBufSize - nBufActualPos;
1419 3818124 : return nCount;
1420 : }
1421 :
1422 5621007 : sal_uInt64 SvStream::Seek(sal_uInt64 const nFilePos)
1423 : {
1424 5621007 : bIoRead = bIoWrite = false;
1425 5621007 : bIsEof = false;
1426 5621007 : if( !pRWBuf )
1427 : {
1428 875902 : m_nBufFilePos = SeekPos( nFilePos );
1429 : DBG_ASSERT(Tell() == m_nBufFilePos,"Out Of Sync!");
1430 875902 : return m_nBufFilePos;
1431 : }
1432 :
1433 : // Is seek position within buffer?
1434 4745105 : if (nFilePos >= m_nBufFilePos && nFilePos <= (m_nBufFilePos + nBufActualLen))
1435 : {
1436 3062966 : nBufActualPos = (sal_uInt16)(nFilePos - m_nBufFilePos);
1437 3062966 : pBufPos = pRWBuf + nBufActualPos;
1438 : // Update nBufFree to avoid crash upon PutBack
1439 3062966 : nBufFree = nBufActualLen - nBufActualPos;
1440 : }
1441 : else
1442 : {
1443 1682139 : if( bIsDirty && bIsConsistent)
1444 : {
1445 1336540 : SeekPos(m_nBufFilePos);
1446 1336540 : if( nCryptMask )
1447 0 : CryptAndWriteBuffer( pRWBuf, nBufActualLen );
1448 : else
1449 1336540 : PutData( pRWBuf, nBufActualLen );
1450 1336540 : bIsDirty = false;
1451 : }
1452 1682139 : nBufActualLen = 0;
1453 1682139 : nBufActualPos = 0;
1454 1682139 : pBufPos = pRWBuf;
1455 1682139 : m_nBufFilePos = SeekPos( nFilePos );
1456 : }
1457 : #ifdef OV_DEBUG
1458 : {
1459 : sal_uInt64 nDebugTemp = m_nBufFilePos + nBufActualPos;
1460 : DBG_ASSERT(Tell()==nDebugTemp,"Sync?");
1461 : }
1462 : #endif
1463 4745105 : return m_nBufFilePos + nBufActualPos;
1464 : }
1465 :
1466 : //STREAM_SEEK_TO_END in the some of the Seek backends is special cased to be
1467 : //efficient, in others e.g. SotStorageStream it's really horribly slow, and in
1468 : //those this should be overridden
1469 456 : sal_uInt64 SvStream::remainingSize()
1470 : {
1471 456 : sal_uInt64 const nCurr = Tell();
1472 456 : sal_uInt64 const nEnd = Seek(STREAM_SEEK_TO_END);
1473 456 : sal_uInt64 nMaxAvailable = nEnd-nCurr;
1474 456 : Seek(nCurr);
1475 456 : return nMaxAvailable;
1476 : }
1477 :
1478 123533 : void SvStream::Flush()
1479 : {
1480 123533 : if( bIsDirty && bIsConsistent )
1481 : {
1482 19699 : SeekPos(m_nBufFilePos);
1483 19699 : if( nCryptMask )
1484 0 : CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1485 : else
1486 19699 : if( PutData( pRWBuf, nBufActualLen ) != nBufActualLen )
1487 5 : SetError( SVSTREAM_WRITE_ERROR );
1488 19699 : bIsDirty = false;
1489 : }
1490 123533 : if( bIsWritable )
1491 109302 : FlushData();
1492 123533 : }
1493 :
1494 22 : void SvStream::RefreshBuffer()
1495 : {
1496 22 : if( bIsDirty && bIsConsistent )
1497 : {
1498 0 : SeekPos(m_nBufFilePos);
1499 0 : if( nCryptMask )
1500 0 : CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1501 : else
1502 0 : PutData( pRWBuf, nBufActualLen );
1503 0 : bIsDirty = false;
1504 : }
1505 22 : SeekPos(m_nBufFilePos);
1506 22 : nBufActualLen = (sal_uInt16)GetData( pRWBuf, nBufSize );
1507 22 : if( nBufActualLen && nError == ERRCODE_IO_PENDING )
1508 0 : nError = ERRCODE_NONE;
1509 22 : if( nCryptMask )
1510 0 : EncryptBuffer(pRWBuf, (sal_Size)nBufActualLen);
1511 22 : bIsConsistent = true;
1512 22 : bIoRead = bIoWrite = false;
1513 22 : }
1514 :
1515 56 : SvStream& SvStream::WriteNumber(sal_Int32 nInt32)
1516 : {
1517 : char buffer[12];
1518 56 : sal_Size nLen = sprintf(buffer, "%" SAL_PRIdINT32, nInt32);
1519 56 : Write(buffer, nLen);
1520 56 : return *this;
1521 : }
1522 :
1523 0 : SvStream& SvStream::WriteNumber(sal_uInt32 nUInt32)
1524 : {
1525 : char buffer[11];
1526 0 : sal_Size nLen = sprintf(buffer, "%" SAL_PRIuUINT32, nUInt32);
1527 0 : Write(buffer, nLen);
1528 0 : return *this;
1529 : }
1530 :
1531 : #define CRYPT_BUFSIZE 1024
1532 :
1533 : /// Encrypt and write
1534 0 : sal_Size SvStream::CryptAndWriteBuffer( const void* pStart, sal_Size nLen)
1535 : {
1536 : unsigned char pTemp[CRYPT_BUFSIZE];
1537 0 : unsigned char* pDataPtr = (unsigned char*)pStart;
1538 0 : sal_Size nCount = 0;
1539 : sal_Size nBufCount;
1540 0 : unsigned char nMask = nCryptMask;
1541 0 : do
1542 : {
1543 0 : if( nLen >= CRYPT_BUFSIZE )
1544 0 : nBufCount = CRYPT_BUFSIZE;
1545 : else
1546 0 : nBufCount = nLen;
1547 0 : nLen -= nBufCount;
1548 0 : memcpy( pTemp, pDataPtr, (sal_uInt16)nBufCount );
1549 : // **** Verschluesseln *****
1550 0 : for ( sal_uInt16 n=0; n < CRYPT_BUFSIZE; n++ )
1551 : {
1552 0 : unsigned char aCh = pTemp[n];
1553 0 : aCh ^= nMask;
1554 0 : SWAPNIBBLES(aCh)
1555 0 : pTemp[n] = aCh;
1556 : }
1557 : // *************************
1558 0 : nCount += PutData( (char*)pTemp, nBufCount );
1559 0 : pDataPtr += nBufCount;
1560 : }
1561 : while ( nLen );
1562 0 : return nCount;
1563 : }
1564 :
1565 0 : bool SvStream::EncryptBuffer(void* pStart, sal_Size nLen)
1566 : {
1567 0 : unsigned char* pTemp = (unsigned char*)pStart;
1568 0 : unsigned char nMask = nCryptMask;
1569 :
1570 0 : for ( sal_Size n=0; n < nLen; n++, pTemp++ )
1571 : {
1572 0 : unsigned char aCh = *pTemp;
1573 0 : SWAPNIBBLES(aCh)
1574 0 : aCh ^= nMask;
1575 0 : *pTemp = aCh;
1576 : }
1577 0 : return true;
1578 : }
1579 :
1580 10 : static unsigned char implGetCryptMask(const sal_Char* pStr, sal_Int32 nLen, long nVersion)
1581 : {
1582 10 : unsigned char nCryptMask = 0;
1583 :
1584 10 : if (!nLen)
1585 10 : return nCryptMask;
1586 :
1587 0 : if( nVersion <= SOFFICE_FILEFORMAT_31 )
1588 : {
1589 0 : while( nLen )
1590 : {
1591 0 : nCryptMask ^= *pStr;
1592 0 : pStr++;
1593 0 : nLen--;
1594 : }
1595 : }
1596 : else // BugFix #25888#
1597 : {
1598 0 : for( sal_uInt16 i = 0; i < nLen; i++ ) {
1599 0 : nCryptMask ^= pStr[i];
1600 0 : if( nCryptMask & 0x80 ) {
1601 0 : nCryptMask <<= 1;
1602 0 : nCryptMask++;
1603 : }
1604 : else
1605 0 : nCryptMask <<= 1;
1606 : }
1607 : }
1608 :
1609 0 : if( !nCryptMask )
1610 0 : nCryptMask = 67;
1611 :
1612 0 : return nCryptMask;
1613 : }
1614 :
1615 10 : void SvStream::SetCryptMaskKey(const OString& rCryptMaskKey)
1616 : {
1617 10 : m_aCryptMaskKey = rCryptMaskKey;
1618 : nCryptMask = implGetCryptMask(m_aCryptMaskKey.getStr(),
1619 10 : m_aCryptMaskKey.getLength(), GetVersion());
1620 10 : }
1621 :
1622 302 : void SvStream::SyncSvStream( sal_Size nNewStreamPos )
1623 : {
1624 302 : ClearBuffer();
1625 302 : SvStream::m_nBufFilePos = nNewStreamPos;
1626 302 : }
1627 :
1628 302 : void SvStream::SyncSysStream()
1629 : {
1630 302 : Flush();
1631 302 : SeekPos( Tell() );
1632 302 : }
1633 :
1634 3552 : bool SvStream::SetStreamSize(sal_uInt64 const nSize)
1635 : {
1636 : #ifdef DBG_UTIL
1637 : sal_uInt64 nFPos = Tell();
1638 : #endif
1639 3552 : sal_uInt16 nBuf = nBufSize;
1640 3552 : SetBufferSize( 0 );
1641 3552 : SetSize( nSize );
1642 3552 : SetBufferSize( nBuf );
1643 : DBG_ASSERT(Tell()==nFPos,"SetStreamSize failed");
1644 3552 : return (nError == 0);
1645 : }
1646 :
1647 377844 : SvStream& endl( SvStream& rStr )
1648 : {
1649 377844 : LineEnd eDelim = rStr.GetLineDelimiter();
1650 377844 : if ( eDelim == LINEEND_CR )
1651 0 : rStr.WriteChar('\r');
1652 377844 : else if( eDelim == LINEEND_LF )
1653 377844 : rStr.WriteChar('\n');
1654 : else
1655 0 : rStr.WriteChar('\r').WriteChar('\n');
1656 377844 : return rStr;
1657 : }
1658 :
1659 0 : SvStream& endlu( SvStream& rStrm )
1660 : {
1661 0 : switch ( rStrm.GetLineDelimiter() )
1662 : {
1663 : case LINEEND_CR :
1664 0 : rStrm.WriteUnicode('\r');
1665 0 : break;
1666 : case LINEEND_LF :
1667 0 : rStrm.WriteUnicode('\n');
1668 0 : break;
1669 : default:
1670 0 : rStrm.WriteUnicode('\r').WriteUnicode('\n');
1671 : }
1672 0 : return rStrm;
1673 : }
1674 :
1675 0 : SvStream& endlub( SvStream& rStrm )
1676 : {
1677 0 : if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
1678 0 : return endlu( rStrm );
1679 : else
1680 0 : return endl( rStrm );
1681 : }
1682 :
1683 15339 : SvMemoryStream::SvMemoryStream( void* pBuffer, sal_Size bufSize,
1684 15339 : StreamMode eMode )
1685 : {
1686 15339 : if( eMode & STREAM_WRITE )
1687 4203 : bIsWritable = true;
1688 : else
1689 11136 : bIsWritable = false;
1690 15339 : nEndOfData = bufSize;
1691 15339 : bOwnsData = false;
1692 15339 : pBuf = (sal_uInt8 *) pBuffer;
1693 15339 : nResize = 0L;
1694 15339 : nSize = bufSize;
1695 15339 : nPos = 0L;
1696 15339 : SetBufferSize( 0 );
1697 15339 : }
1698 :
1699 34533 : SvMemoryStream::SvMemoryStream( sal_Size nInitSize, sal_Size nResizeOffset )
1700 : {
1701 34533 : bIsWritable = true;
1702 34533 : bOwnsData = true;
1703 34533 : nEndOfData = 0L;
1704 34533 : nResize = nResizeOffset;
1705 34533 : nPos = 0;
1706 34533 : pBuf = 0;
1707 34533 : if( nResize != 0 && nResize < 16 )
1708 0 : nResize = 16;
1709 34533 : if( nInitSize && !AllocateMemory( nInitSize ) )
1710 : {
1711 0 : SetError( SVSTREAM_OUTOFMEMORY );
1712 0 : nSize = 0;
1713 : }
1714 : else
1715 34533 : nSize = nInitSize;
1716 34533 : SetBufferSize( 64 );
1717 34533 : }
1718 :
1719 103712 : SvMemoryStream::~SvMemoryStream()
1720 : {
1721 49872 : if( pBuf )
1722 : {
1723 42242 : if( bOwnsData )
1724 27741 : FreeMemory();
1725 : else
1726 14501 : Flush();
1727 : }
1728 53840 : }
1729 :
1730 9 : const void* SvMemoryStream::GetBuffer()
1731 : {
1732 9 : Flush();
1733 9 : return (const void*)GetData();
1734 : }
1735 :
1736 6857 : sal_uIntPtr SvMemoryStream::GetSize()
1737 : {
1738 6857 : Flush();
1739 6857 : sal_uInt64 const nTemp = Tell();
1740 6857 : sal_uInt64 const nLength = Seek( STREAM_SEEK_TO_END );
1741 6857 : Seek( nTemp );
1742 6857 : return nLength;
1743 : }
1744 :
1745 165 : void* SvMemoryStream::SetBuffer( void* pNewBuf, sal_Size nCount,
1746 : bool bOwnsDat, sal_Size nEOF )
1747 : {
1748 : void* pResult;
1749 165 : SetBufferSize( 0 ); // Buffering in der Basisklasse initialisieren
1750 165 : Seek( 0 );
1751 165 : if( bOwnsData )
1752 : {
1753 165 : pResult = 0;
1754 165 : if( pNewBuf != pBuf )
1755 165 : FreeMemory();
1756 : }
1757 : else
1758 0 : pResult = pBuf;
1759 :
1760 165 : pBuf = (sal_uInt8 *) pNewBuf;
1761 165 : nPos = 0;
1762 165 : nSize = nCount;
1763 165 : nResize = 0;
1764 165 : bOwnsData = bOwnsDat;
1765 :
1766 165 : if( nEOF > nCount )
1767 0 : nEOF = nCount;
1768 165 : nEndOfData = nEOF;
1769 :
1770 165 : ResetError();
1771 :
1772 165 : return pResult;
1773 : }
1774 :
1775 573703 : sal_Size SvMemoryStream::GetData( void* pData, sal_Size nCount )
1776 : {
1777 573703 : sal_Size nMaxCount = nEndOfData-nPos;
1778 573703 : if( nCount > nMaxCount )
1779 132844 : nCount = nMaxCount;
1780 573703 : memcpy( pData, pBuf+nPos, (size_t)nCount );
1781 573703 : nPos += nCount;
1782 573703 : return nCount;
1783 : }
1784 :
1785 1677311 : sal_Size SvMemoryStream::PutData( const void* pData, sal_Size nCount )
1786 : {
1787 1677311 : if( GetError() )
1788 0 : return 0L;
1789 :
1790 1677311 : sal_Size nMaxCount = nSize-nPos;
1791 :
1792 : // check for overflow
1793 1677311 : if( nCount > nMaxCount )
1794 : {
1795 8250 : if( nResize == 0 )
1796 : {
1797 : // copy as much as possible
1798 0 : nCount = nMaxCount;
1799 0 : SetError( SVSTREAM_OUTOFMEMORY );
1800 : }
1801 : else
1802 : {
1803 : long nNewResize;
1804 8250 : if( nSize && nSize > nResize )
1805 7317 : nNewResize = nSize;
1806 : else
1807 933 : nNewResize = nResize;
1808 :
1809 8250 : if( (nCount-nMaxCount) < nResize )
1810 : {
1811 : // lacking memory is smaller than nResize,
1812 : // resize accordingly
1813 5316 : if( !ReAllocateMemory( nNewResize) )
1814 : {
1815 0 : nCount = 0;
1816 0 : SetError( SVSTREAM_WRITE_ERROR );
1817 : }
1818 : }
1819 : else
1820 : {
1821 : // lacking memory is larger than nResize,
1822 : // resize by (nCoount-nMaxCount) + resize offset
1823 2934 : if( !ReAllocateMemory( nCount-nMaxCount+nNewResize ) )
1824 : {
1825 0 : nCount = 0;
1826 0 : SetError( SVSTREAM_WRITE_ERROR );
1827 : }
1828 : }
1829 : }
1830 : }
1831 : DBG_ASSERT(pBuf,"Possibly Reallocate failed");
1832 1677311 : memcpy( pBuf+nPos, pData, (size_t)nCount);
1833 :
1834 1677311 : nPos += nCount;
1835 1677311 : if( nPos > nEndOfData )
1836 924933 : nEndOfData = nPos;
1837 1677311 : return nCount;
1838 : }
1839 :
1840 3301296 : sal_uInt64 SvMemoryStream::SeekPos(sal_uInt64 const nNewPos)
1841 : {
1842 : // nEndOfData: First position in stream not allowed to read from
1843 : // nSize: Size of allocated buffer
1844 :
1845 : // check if a truncated STREAM_SEEK_TO_END was passed
1846 : assert(nNewPos != SAL_MAX_UINT32);
1847 3301296 : if( nNewPos < nEndOfData )
1848 1452717 : nPos = nNewPos;
1849 1848579 : else if( nNewPos == STREAM_SEEK_TO_END )
1850 24332 : nPos = nEndOfData;
1851 : else
1852 : {
1853 1824247 : if( nNewPos >= nSize ) // Does buffer need extension?
1854 : {
1855 1829 : if( nResize ) // Is extension possible?
1856 : {
1857 476 : long nDiff = (long)(nNewPos - nSize + 1);
1858 476 : nDiff += (long)nResize;
1859 476 : ReAllocateMemory( nDiff );
1860 476 : nPos = nNewPos;
1861 476 : nEndOfData = nNewPos;
1862 : }
1863 : else // Extension not possible, set pos to end of data
1864 : {
1865 : // SetError( SVSTREAM_OUTOFMEMORY );
1866 1353 : nPos = nEndOfData;
1867 : }
1868 : }
1869 : else // Expand buffer size
1870 : {
1871 1822418 : nPos = nNewPos;
1872 1822418 : nEndOfData = nNewPos;
1873 : }
1874 : }
1875 3301296 : return nPos;
1876 : }
1877 :
1878 74215 : void SvMemoryStream::FlushData()
1879 : {
1880 74215 : }
1881 :
1882 7082 : void SvMemoryStream::ResetError()
1883 : {
1884 7082 : SvStream::ClearError();
1885 7082 : }
1886 :
1887 26926 : bool SvMemoryStream::AllocateMemory( sal_Size nNewSize )
1888 : {
1889 26926 : pBuf = new sal_uInt8[nNewSize];
1890 26926 : return( pBuf != 0 );
1891 : }
1892 :
1893 : // (using Bozo algorithm)
1894 9698 : bool SvMemoryStream::ReAllocateMemory( long nDiff )
1895 : {
1896 9698 : bool bRetVal = false;
1897 9698 : long nTemp = (long)nSize;
1898 9698 : nTemp += nDiff;
1899 9698 : sal_Size nNewSize = (sal_Size)nTemp;
1900 :
1901 9698 : if( nNewSize )
1902 : {
1903 9698 : sal_uInt8* pNewBuf = new sal_uInt8[nNewSize];
1904 :
1905 9698 : if( pNewBuf )
1906 : {
1907 9698 : bRetVal = true; // Success!
1908 9698 : if( nNewSize < nSize ) // Are we shrinking?
1909 : {
1910 29 : memcpy( pNewBuf, pBuf, (size_t)nNewSize );
1911 29 : if( nPos > nNewSize )
1912 4 : nPos = 0L;
1913 29 : if( nEndOfData >= nNewSize )
1914 4 : nEndOfData = nNewSize-1L;
1915 : }
1916 : else
1917 : {
1918 9669 : memcpy( pNewBuf, pBuf, (size_t)nSize );
1919 : }
1920 :
1921 9698 : FreeMemory();
1922 :
1923 9698 : pBuf = pNewBuf;
1924 9698 : nSize = nNewSize;
1925 : }
1926 : }
1927 : else
1928 : {
1929 0 : bRetVal = true;
1930 0 : FreeMemory();
1931 0 : pBuf = 0;
1932 0 : nSize = 0;
1933 0 : nEndOfData = 0;
1934 0 : nPos = 0;
1935 : }
1936 :
1937 9698 : return bRetVal;
1938 : }
1939 :
1940 37604 : void SvMemoryStream::FreeMemory()
1941 : {
1942 37604 : delete[] pBuf;
1943 37604 : }
1944 :
1945 23 : void* SvMemoryStream::SwitchBuffer( sal_Size nInitSize, sal_Size nResizeOffset)
1946 : {
1947 23 : Flush();
1948 23 : if( !bOwnsData )
1949 0 : return 0;
1950 23 : Seek( STREAM_SEEK_TO_BEGIN );
1951 :
1952 23 : void* pRetVal = pBuf;
1953 23 : pBuf = 0;
1954 23 : nEndOfData = 0L;
1955 23 : nResize = nResizeOffset;
1956 23 : nPos = 0;
1957 :
1958 23 : if( nResize != 0 && nResize < 16 )
1959 0 : nResize = 16;
1960 :
1961 23 : ResetError();
1962 :
1963 23 : if( nInitSize && !AllocateMemory(nInitSize) )
1964 : {
1965 0 : SetError( SVSTREAM_OUTOFMEMORY );
1966 0 : nSize = 0;
1967 : }
1968 : else
1969 23 : nSize = nInitSize;
1970 :
1971 23 : SetBufferSize( 64 );
1972 23 : return pRetVal;
1973 : }
1974 :
1975 963 : void SvMemoryStream::SetSize(sal_uInt64 const nNewSize)
1976 : {
1977 963 : long nDiff = (long)nNewSize - (long)nSize;
1978 963 : ReAllocateMemory( nDiff );
1979 963 : }
1980 :
1981 0 : SvScriptStream::SvScriptStream(const OUString& rUrl):
1982 0 : mpProcess(NULL), mpHandle(NULL)
1983 : {
1984 : oslProcessError rc;
1985 : rc = osl_executeProcess_WithRedirectedIO(
1986 : rUrl.pData,
1987 : NULL, 0,
1988 : osl_Process_HIDDEN,
1989 : NULL,
1990 : NULL,
1991 : NULL, 0,
1992 : &mpProcess,
1993 0 : NULL, &mpHandle, NULL);
1994 0 : if (osl_Process_E_None != rc)
1995 : {
1996 0 : mpProcess = NULL;
1997 0 : mpHandle = NULL;
1998 : }
1999 0 : }
2000 :
2001 0 : SvScriptStream::~SvScriptStream()
2002 : {
2003 0 : if (mpProcess)
2004 : {
2005 0 : osl_terminateProcess(mpProcess);
2006 0 : osl_freeProcessHandle(mpProcess);
2007 : }
2008 0 : if (mpHandle)
2009 0 : osl_closeFile(mpHandle);
2010 0 : }
2011 :
2012 0 : bool SvScriptStream::ReadLine(OString &rStr, sal_Int32)
2013 : {
2014 0 : rStr = OString();
2015 0 : if (!good())
2016 0 : return false;
2017 :
2018 0 : OStringBuffer sBuf;
2019 0 : sal_Char aChar('\n');
2020 : sal_uInt64 nBytesRead;
2021 0 : while (osl_File_E_None == osl_readFile(mpHandle, &aChar, 1, &nBytesRead)
2022 0 : && nBytesRead == 1 && aChar != '\n')
2023 : {
2024 0 : sBuf.append( aChar );
2025 : }
2026 0 : rStr = sBuf.makeStringAndClear();
2027 0 : if (!rStr.isEmpty())
2028 0 : return true;
2029 :
2030 0 : return false;
2031 : }
2032 :
2033 0 : bool SvScriptStream::good() const
2034 : {
2035 0 : return mpHandle != NULL;
2036 : }
2037 :
2038 0 : TYPEINIT0 ( SvDataCopyStream )
2039 :
2040 0 : void SvDataCopyStream::Assign( const SvDataCopyStream& )
2041 : {
2042 0 : }
2043 :
2044 : //Create a OString of nLen bytes from rStream
2045 122250 : OString read_uInt8s_ToOString(SvStream& rStrm, sal_Size nLen)
2046 : {
2047 122250 : rtl_String *pStr = NULL;
2048 122250 : if (nLen)
2049 : {
2050 65283 : nLen = std::min(nLen, static_cast<sal_Size>(SAL_MAX_INT32));
2051 : //alloc a (ref-count 1) rtl_String of the desired length.
2052 : //rtl_String's buffer is uninitialized, except for null termination
2053 65283 : pStr = rtl_string_alloc(sal::static_int_cast<sal_Int32>(nLen));
2054 : SAL_WARN_IF(!pStr, "tools", "allocation failed");
2055 65283 : if (pStr)
2056 : {
2057 65283 : sal_Size nWasRead = rStrm.Read(pStr->buffer, nLen);
2058 65283 : if (nWasRead != nLen)
2059 : {
2060 : //on (typically unlikely) short read set length to what we could
2061 : //read, and null terminate. Excess buffer capacity remains of
2062 : //course, could create a (true) replacement OString if it matters.
2063 7 : pStr->length = sal::static_int_cast<sal_Int32>(nWasRead);
2064 7 : pStr->buffer[pStr->length] = 0;
2065 : }
2066 : }
2067 : }
2068 :
2069 : //take ownership of buffer and return, otherwise return empty string
2070 122250 : return pStr ? OString(pStr, SAL_NO_ACQUIRE) : OString();
2071 : }
2072 :
2073 : //Create a OUString of nLen sal_Unicodes from rStream
2074 81346 : OUString read_uInt16s_ToOUString(SvStream& rStrm, sal_Size nLen)
2075 : {
2076 81346 : rtl_uString *pStr = NULL;
2077 81346 : if (nLen)
2078 : {
2079 55012 : nLen = std::min(nLen, static_cast<sal_Size>(SAL_MAX_INT32));
2080 : //alloc a (ref-count 1) rtl_uString of the desired length.
2081 : //rtl_String's buffer is uninitialized, except for null termination
2082 55012 : pStr = rtl_uString_alloc(sal::static_int_cast<sal_Int32>(nLen));
2083 : SAL_WARN_IF(!pStr, "tools", "allocation failed");
2084 55012 : if (pStr)
2085 : {
2086 55012 : sal_Size nWasRead = rStrm.Read(pStr->buffer, nLen*2)/2;
2087 55012 : if (nWasRead != nLen)
2088 : {
2089 : //on (typically unlikely) short read set length to what we could
2090 : //read, and null terminate. Excess buffer capacity remains of
2091 : //course, could create a (true) replacement OUString if it matters.
2092 5 : pStr->length = sal::static_int_cast<sal_Int32>(nWasRead);
2093 5 : pStr->buffer[pStr->length] = 0;
2094 : }
2095 55012 : if (rStrm.IsEndianSwap())
2096 : {
2097 0 : for (sal_Int32 i = 0; i < pStr->length; ++i)
2098 0 : pStr->buffer[i] = OSL_SWAPWORD(pStr->buffer[i]);
2099 : }
2100 : }
2101 : }
2102 :
2103 : //take ownership of buffer and return, otherwise return empty string
2104 81346 : return pStr ? OUString(pStr, SAL_NO_ACQUIRE) : OUString();
2105 : }
2106 :
2107 : namespace
2108 : {
2109 61041 : template <typename T, typename O> T tmpl_convertLineEnd(const T &rIn, LineEnd eLineEnd)
2110 : {
2111 : // Determine linebreaks and compute length
2112 61041 : bool bConvert = false; // Needs conversion
2113 61041 : sal_Int32 nStrLen = rIn.getLength();
2114 61041 : sal_Int32 nLineEndLen = (eLineEnd == LINEEND_CRLF) ? 2 : 1;
2115 61041 : sal_Int32 nLen = 0; // Target length
2116 61041 : sal_Int32 i = 0; // Source counter
2117 :
2118 434062 : while (i < nStrLen)
2119 : {
2120 : // \r or \n causes linebreak
2121 311980 : if ( (rIn[i] == '\r') || (rIn[i] == '\n') )
2122 : {
2123 3491 : nLen = nLen + nLineEndLen;
2124 :
2125 : // If set already, skip expensive test
2126 3491 : if ( !bConvert )
2127 : {
2128 : // Muessen wir Konvertieren
2129 7685 : if ( ((eLineEnd != LINEEND_LF) && (rIn[i] == '\n')) ||
2130 0 : ((eLineEnd == LINEEND_CRLF) && (i+1) < nStrLen && (rIn[i+1] != '\n')) ||
2131 : ((eLineEnd == LINEEND_LF) &&
2132 4264 : ((rIn[i] == '\r') || ((i+1) < nStrLen && rIn[i+1] == '\r'))) ||
2133 : ((eLineEnd == LINEEND_CR) &&
2134 0 : ((rIn[i] == '\n') || ((i+1) < nStrLen && rIn[i+1] == '\n'))) )
2135 1204 : bConvert = true;
2136 : }
2137 :
2138 : // skip char if \r\n or \n\r
2139 3502 : if ( (i+1) < nStrLen && ((rIn[i+1] == '\r') || (rIn[i+1] == '\n')) &&
2140 11 : (rIn[i] != rIn[i+1]) )
2141 0 : ++i;
2142 : }
2143 : else
2144 308489 : ++nLen;
2145 311980 : ++i;
2146 : }
2147 :
2148 61041 : if (!bConvert)
2149 59837 : return rIn;
2150 :
2151 : // convert linebreaks, insert string
2152 1204 : O aNewData(nLen);
2153 1204 : i = 0;
2154 3902 : while (i < nStrLen)
2155 : {
2156 : // \r or \n causes linebreak
2157 1494 : if ( (rIn[i] == '\r') || (rIn[i] == '\n') )
2158 : {
2159 1274 : if ( eLineEnd == LINEEND_CRLF )
2160 : {
2161 0 : aNewData.append('\r');
2162 0 : aNewData.append('\n');
2163 : }
2164 : else
2165 : {
2166 1274 : if ( eLineEnd == LINEEND_CR )
2167 0 : aNewData.append('\r');
2168 : else
2169 1274 : aNewData.append('\n');
2170 : }
2171 :
2172 1274 : if ( (i+1) < nStrLen && ((rIn[i+1] == '\r') || (rIn[i+1] == '\n')) &&
2173 0 : (rIn[i] != rIn[i+1]) )
2174 0 : ++i;
2175 : }
2176 : else
2177 : {
2178 220 : aNewData.append(rIn[i]);
2179 : }
2180 :
2181 1494 : ++i;
2182 : }
2183 :
2184 1204 : return aNewData.makeStringAndClear();
2185 : }
2186 : }
2187 :
2188 84 : OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
2189 : {
2190 84 : return tmpl_convertLineEnd<OString, OStringBuffer>(rIn, eLineEnd);
2191 : }
2192 :
2193 60957 : OUString convertLineEnd(const OUString &rIn, LineEnd eLineEnd)
2194 : {
2195 60957 : return tmpl_convertLineEnd<OUString, OUStringBuffer>(rIn, eLineEnd);
2196 : }
2197 :
2198 13371 : sal_Size write_uInt32_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
2199 : const OUString &rStr)
2200 : {
2201 13371 : sal_Size nWritten = 0;
2202 13371 : sal_uInt32 nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<sal_uInt32>::max());
2203 : SAL_WARN_IF(static_cast<sal_Size>(nUnits) != static_cast<sal_Size>(rStr.getLength()),
2204 : "tools.stream",
2205 : "string too long for prefix count to fit in output type");
2206 13371 : rStrm.WriteUInt32(nUnits);
2207 13371 : if (rStrm.good())
2208 : {
2209 13371 : nWritten += sizeof(sal_uInt32);
2210 13371 : nWritten += write_uInt16s_FromOUString(rStrm, rStr, nUnits);
2211 : }
2212 13371 : return nWritten;
2213 : }
2214 :
2215 45585 : sal_Size write_uInt16_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
2216 : const OUString &rStr)
2217 : {
2218 45585 : sal_Size nWritten = 0;
2219 45585 : sal_uInt16 nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<sal_uInt16>::max());
2220 : SAL_WARN_IF(nUnits != rStr.getLength(),
2221 : "tools.stream",
2222 : "string too long for prefix count to fit in output type");
2223 45585 : rStrm.WriteUInt16(nUnits);
2224 45585 : if (rStrm.good())
2225 : {
2226 45585 : nWritten += sizeof(nUnits);
2227 45585 : nWritten += write_uInt16s_FromOUString(rStrm, rStr, nUnits);
2228 : }
2229 45585 : return nWritten;
2230 : }
2231 :
2232 193956 : sal_Size write_uInt16_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
2233 : const OString &rStr)
2234 : {
2235 193956 : sal_Size nWritten = 0;
2236 193956 : sal_uInt16 nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<sal_uInt16>::max());
2237 : SAL_WARN_IF(static_cast<sal_Size>(nUnits) != static_cast<sal_Size>(rStr.getLength()),
2238 : "tools.stream",
2239 : "string too long for sal_uInt16 count to fit in output type");
2240 193956 : rStrm.WriteUInt16( nUnits );
2241 193956 : if (rStrm.good())
2242 : {
2243 193956 : nWritten += sizeof(sal_uInt16);
2244 193956 : nWritten += write_uInt8s_FromOString(rStrm, rStr, nUnits);
2245 : }
2246 193956 : return nWritten;
2247 : }
2248 :
2249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|