xref: /aoo41x/main/tools/source/stream/stream.cxx (revision f13410cf)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 
27 // ToDo:
28 //  - Read->RefreshBuffer->Auf Aenderungen von nBufActualLen reagieren
29 
30 #include <cstddef>
31 
32 #include <string.h>
33 #include <stdio.h>
34 #include <ctype.h>  // isspace
35 #include <stdlib.h> // strtol, _crotl
36 
37 #include "boost/static_assert.hpp"
38 
39 /*
40 #if defined( DBG_UTIL ) && (OSL_DEBUG_LEVEL > 1)
41 // prueft Synchronisation des Buffers nach allen Read, Write, Seek
42 #define OV_DEBUG
43 #endif
44 */
45 
46 #include <tools/solar.h>
47 
48 #if defined(BLC)
49 #define SWAPNIBBLES(c) c=_crotl(c,4);
50 #else
51 #define SWAPNIBBLES(c)      \
52 unsigned char nSwapTmp=c;   \
53 nSwapTmp <<= 4;             \
54 c >>= 4;                    \
55 c |= nSwapTmp;
56 #endif
57 
58 #include <tools/debug.hxx>
59 #define ENABLE_BYTESTRING_STREAM_OPERATORS
60 #include <tools/stream.hxx>
61 #include <osl/thread.h>
62 #include <algorithm>
63 
64 // -----------------------------------------------------------------------
65 
DBG_NAME(Stream)66 DBG_NAME( Stream )
67 
68 // -----------------------------------------------------------------------
69 
70 // sprintf Param-Mode
71 #define SPECIAL_PARAM_NONE 0        // Format-Str, Number
72 #define SPECIAL_PARAM_WIDTH 1       // Format-Str, Width, Number
73 #define SPECIAL_PARAM_PRECISION 2   // Format-Str, Precision, Number
74 #define SPECIAL_PARAM_BOTH 3        // Format-Str, Width, Precision, Number
75 
76 // -----------------------------------------------------------------------
77 
78 // !!! Nicht inline, wenn Operatoren <<,>> inline sind
79 inline static void SwapUShort( sal_uInt16& r )
80     {   r = SWAPSHORT(r);   }
SwapShort(short & r)81 inline static void SwapShort( short& r )
82     {   r = SWAPSHORT(r);   }
SwapLong(long & r)83 inline static void SwapLong( long& r )
84     {   r = SWAPLONG(r);   }
SwapULong(sal_uInt32 & r)85 inline static void SwapULong( sal_uInt32& r )
86     {   r = SWAPLONG(r);   }
SwapLongInt(int & r)87 inline static void SwapLongInt( int& r )
88     {   r = SWAPLONG(r);   }
SwapLongUInt(unsigned int & r)89 inline static void SwapLongUInt( unsigned int& r )
90     {   r = SWAPLONG(r);   }
91 #ifdef UNX
SwapFloat(float & r)92 inline static void SwapFloat( float& r )
93     {
94 		*((sal_uInt32*)(void*)&r) = SWAPLONG( *((sal_uInt32*)(void*)&r) );
95     }
SwapDouble(double & r)96 inline static void SwapDouble( double& r )
97     {
98         if( sizeof(double) != 8 )
99         {
100           DBG_ASSERT( sal_False, "Can only swap 8-Byte-doubles\n" );
101         }
102         else
103         {
104           sal_uInt32* c = (sal_uInt32*)(void*)&r;
105           c[0] ^= c[1]; // zwei 32-Bit-Werte in situ vertauschen
106           c[1] ^= c[0];
107           c[0] ^= c[1];
108           c[0] = SWAPLONG(c[0]); // und die beiden 32-Bit-Werte selbst in situ drehen
109           c[1] = SWAPLONG(c[1]);
110         }
111     }
112 #endif
113 
114 //SDO
115 
116 #define READNUMBER_WITHOUT_SWAP(datatype,value) \
117 {\
118 int tmp = eIOMode; \
119 if( (tmp == STREAM_IO_READ) && sizeof(datatype)<=nBufFree) \
120 {\
121     for (std::size_t i = 0; i < sizeof(datatype); i++)\
122         ((char *)&value)[i] = pBufPos[i];\
123     nBufActualPos += sizeof(datatype);\
124     pBufPos += sizeof(datatype);\
125     nBufFree -= sizeof(datatype);\
126 }\
127 else\
128     Read( (char*)&value, sizeof(datatype) );\
129 }
130 
131 #define WRITENUMBER_WITHOUT_SWAP(datatype,value) \
132 {\
133 int tmp = eIOMode; \
134 if( (tmp==STREAM_IO_WRITE) && sizeof(datatype) <= nBufFree)\
135 {\
136     for (std::size_t i = 0; i < sizeof(datatype); i++)\
137         pBufPos[i] = ((char *)&value)[i];\
138     nBufFree -= sizeof(datatype);\
139     nBufActualPos += sizeof(datatype);\
140     if( nBufActualPos > nBufActualLen )\
141         nBufActualLen = nBufActualPos;\
142     pBufPos += sizeof(datatype);\
143     bIsDirty = sal_True;\
144 }\
145 else\
146     Write( (char*)&value, sizeof(datatype) );\
147 }
148 
149 //============================================================================
150 //
151 //  class SvLockBytes
152 //
153 //============================================================================
154 
close()155 void SvLockBytes::close()
156 {
157     if (m_bOwner)
158         delete m_pStream;
159     m_pStream = 0;
160 }
161 
162 //============================================================================
163 TYPEINIT0(SvLockBytes);
164 
165 //============================================================================
166 // virtual
ReadAt(sal_Size nPos,void * pBuffer,sal_Size nCount,sal_Size * pRead) const167 ErrCode SvLockBytes::ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
168                             sal_Size * pRead) const
169 {
170     if (!m_pStream)
171     {
172         DBG_ERROR("SvLockBytes::ReadAt(): Bad stream");
173         return ERRCODE_NONE;
174     }
175 
176     m_pStream->Seek(nPos);
177     sal_Size nTheRead = m_pStream->Read(pBuffer, nCount);
178     if (pRead)
179         *pRead = nTheRead;
180     return m_pStream->GetErrorCode();
181 }
182 
183 //============================================================================
184 // virtual
WriteAt(sal_Size nPos,const void * pBuffer,sal_Size nCount,sal_Size * pWritten)185 ErrCode SvLockBytes::WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
186                              sal_Size * pWritten)
187 {
188     if (!m_pStream)
189     {
190         DBG_ERROR("SvLockBytes::WriteAt(): Bad stream");
191         return ERRCODE_NONE;
192     }
193 
194     m_pStream->Seek(nPos);
195     sal_Size nTheWritten = m_pStream->Write(pBuffer, nCount);
196     if (pWritten)
197         *pWritten = nTheWritten;
198     return m_pStream->GetErrorCode();
199 }
200 
201 //============================================================================
202 // virtual
Flush() const203 ErrCode SvLockBytes::Flush() const
204 {
205     if (!m_pStream)
206     {
207         DBG_ERROR("SvLockBytes::Flush(): Bad stream");
208         return ERRCODE_NONE;
209     }
210 
211     m_pStream->Flush();
212     return m_pStream->GetErrorCode();
213 }
214 
215 //============================================================================
216 // virtual
SetSize(sal_Size nSize)217 ErrCode SvLockBytes::SetSize(sal_Size nSize)
218 {
219     if (!m_pStream)
220     {
221         DBG_ERROR("SvLockBytes::SetSize(): Bad stream");
222         return ERRCODE_NONE;
223     }
224 
225     m_pStream->SetStreamSize(nSize);
226     return m_pStream->GetErrorCode();
227 }
228 
229 //============================================================================
LockRegion(sal_Size,sal_Size,LockType)230 ErrCode SvLockBytes::LockRegion(sal_Size, sal_Size, LockType)
231 {
232     DBG_ERROR("SvLockBytes::LockRegion(): Not implemented");
233     return ERRCODE_NONE;
234 }
235 
236 //============================================================================
237 
UnlockRegion(sal_Size,sal_Size,LockType)238 ErrCode SvLockBytes::UnlockRegion(sal_Size, sal_Size, LockType)
239 {
240     DBG_ERROR("SvLockBytes::UnlockRegion(): Not implemented");
241     return ERRCODE_NONE;
242 }
243 
244 //============================================================================
Stat(SvLockBytesStat * pStat,SvLockBytesStatFlag) const245 ErrCode SvLockBytes::Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const
246 {
247     if (!m_pStream)
248     {
249         DBG_ERROR("SvLockBytes::Stat(): Bad stream");
250         return ERRCODE_NONE;
251     }
252 
253     if (pStat)
254     {
255         sal_Size nPos = m_pStream->Tell();
256         pStat->nSize = m_pStream->Seek(STREAM_SEEK_TO_END);
257         m_pStream->Seek(nPos);
258     }
259     return ERRCODE_NONE;
260 }
261 
262 //============================================================================
263 //
264 //  class SvOpenLockBytes
265 //
266 //============================================================================
267 
268 TYPEINIT1(SvOpenLockBytes, SvLockBytes);
269 
270 //============================================================================
271 //
272 //  class SvAsyncLockBytes
273 //
274 //============================================================================
275 
276 TYPEINIT1(SvAsyncLockBytes, SvOpenLockBytes);
277 
278 //============================================================================
279 // virtual
ReadAt(sal_Size nPos,void * pBuffer,sal_Size nCount,sal_Size * pRead) const280 ErrCode SvAsyncLockBytes::ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
281                                  sal_Size * pRead) const
282 {
283     if (m_bTerminated)
284         return SvOpenLockBytes::ReadAt(nPos, pBuffer, nCount, pRead);
285     else
286     {
287         sal_Size nTheCount = std::min(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
288         ErrCode nError = SvOpenLockBytes::ReadAt(nPos, pBuffer, nTheCount,
289                                                  pRead);
290         return !nCount || nTheCount == nCount || nError ? nError :
291                                                           ERRCODE_IO_PENDING;
292     }
293 }
294 
295 //============================================================================
296 // virtual
WriteAt(sal_Size nPos,const void * pBuffer,sal_Size nCount,sal_Size * pWritten)297 ErrCode SvAsyncLockBytes::WriteAt(sal_Size nPos, const void * pBuffer,
298                                   sal_Size nCount, sal_Size * pWritten)
299 {
300     if (m_bTerminated)
301         return SvOpenLockBytes::WriteAt(nPos, pBuffer, nCount, pWritten);
302     else
303     {
304         sal_Size nTheCount = std::min(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
305         ErrCode nError = SvOpenLockBytes::WriteAt(nPos, pBuffer, nTheCount,
306                                                   pWritten);
307         return !nCount || nTheCount == nCount || nError ? nError :
308                                                           ERRCODE_IO_PENDING;
309     }
310 }
311 
312 //============================================================================
313 // virtual
FillAppend(const void * pBuffer,sal_Size nCount,sal_Size * pWritten)314 ErrCode SvAsyncLockBytes::FillAppend(const void * pBuffer, sal_Size nCount,
315                                      sal_Size * pWritten)
316 {
317     sal_Size nTheWritten;
318     ErrCode nError = SvOpenLockBytes::WriteAt(m_nSize, pBuffer, nCount,
319                                               &nTheWritten);
320     if (!nError)
321         m_nSize += nTheWritten;
322     if (pWritten)
323         *pWritten = nTheWritten;
324     return nError;
325 }
326 
327 //============================================================================
328 // virtual
Seek(sal_Size nPos)329 sal_Size SvAsyncLockBytes::Seek(sal_Size nPos)
330 {
331     if (nPos != STREAM_SEEK_TO_END)
332         m_nSize = nPos;
333     return m_nSize;
334 }
335 
336 //============================================================================
337 //
338 //  class SvStream
339 //
340 //============================================================================
341 
GetData(void * pData,sal_Size nSize)342 sal_Size SvStream::GetData( void* pData, sal_Size nSize )
343 {
344     if( !GetError() )
345     {
346         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
347         sal_Size nRet;
348         nError = xLockBytes->ReadAt( nActPos, pData, nSize, &nRet );
349         nActPos += nRet;
350         return nRet;
351     }
352     else return 0;
353 }
354 
SetLockBytes(SvLockBytesRef & rLB)355 ErrCode SvStream::SetLockBytes( SvLockBytesRef& rLB )
356 {
357     xLockBytes = rLB;
358     RefreshBuffer();
359     return ERRCODE_NONE;
360 }
361 
362 //========================================================================
363 
PutData(const void * pData,sal_Size nSize)364 sal_Size SvStream::PutData( const void* pData, sal_Size nSize )
365 {
366     if( !GetError() )
367     {
368         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
369         sal_Size nRet;
370         nError = xLockBytes->WriteAt( nActPos, pData, nSize, &nRet );
371         nActPos += nRet;
372         return nRet;
373     }
374     else return 0;
375 }
376 
377 //========================================================================
378 
SeekPos(sal_Size nPos)379 sal_Size SvStream::SeekPos( sal_Size nPos )
380 {
381     if( !GetError() && nPos == STREAM_SEEK_TO_END )
382     {
383         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
384         SvLockBytesStat aStat;
385         xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
386         nActPos = aStat.nSize;
387     }
388     else
389         nActPos = nPos;
390     return nActPos;
391 }
392 
393 //========================================================================
394 
FlushData()395 void SvStream::FlushData()
396 {
397     if( !GetError() )
398     {
399         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
400         nError = xLockBytes->Flush();
401     }
402 }
403 
404 //========================================================================
405 
SetSize(sal_Size nSize)406 void SvStream::SetSize( sal_Size nSize )
407 {
408     DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
409     nError = xLockBytes->SetSize( nSize );
410 }
411 
ImpInit()412 void SvStream::ImpInit()
413 {
414     nActPos             = 0;
415     nCompressMode       = COMPRESSMODE_NONE;
416     eStreamCharSet      = osl_getThreadTextEncoding();
417 //  eTargetCharSet      = osl_getThreadTextEncoding();
418     nCryptMask          = 0;
419     bIsEof              = sal_False;
420 #if defined UNX
421     eLineDelimiter      = LINEEND_LF;   // UNIX-Format
422 #else
423     eLineDelimiter      = LINEEND_CRLF; // DOS-Format
424 #endif
425 
426     SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
427 
428     nBufFilePos         = 0;
429     nBufActualPos       = 0;
430     bIsDirty            = sal_False;
431     bIsConsistent       = sal_True;
432     bIsWritable         = sal_True;
433 
434     pRWBuf              = 0;
435     pBufPos             = 0;
436     nBufSize            = 0;
437     nBufActualLen       = 0;
438     eIOMode             = STREAM_IO_DONTKNOW;
439     nBufFree            = 0;
440 
441     nRadix              = 10;
442     nPrecision          = 0;  // all significant digits
443     nWidth              = 0; // default width
444     cFiller             = ' ';
445     nJustification      = JUSTIFY_RIGHT;
446     eStreamMode         = 0;
447     CreateFormatString();
448 
449     nVersion           = 0;
450 
451     ClearError();
452 }
453 
454 /*************************************************************************
455 |*
456 |*    Stream::Stream()
457 |*
458 |*    Beschreibung      STREAM.SDW
459 |*    Ersterstellung    OV 08.06.94
460 |*    Letzte Aenderung  OV 08.06.94
461 |*
462 *************************************************************************/
463 
SvStream(SvLockBytes * pLockBytesP)464 SvStream::SvStream( SvLockBytes* pLockBytesP )
465 {
466     DBG_CTOR( Stream, NULL );
467 
468     ImpInit();
469     xLockBytes = pLockBytesP;
470     const SvStream* pStrm;
471     if( pLockBytesP ) {
472         pStrm = pLockBytesP->GetStream();
473         if( pStrm ) {
474             SetError( pStrm->GetErrorCode() );
475         }
476     }
477     SetBufferSize( 256 );
478 }
479 
SvStream()480 SvStream::SvStream()
481 {
482     DBG_CTOR( Stream, NULL );
483 
484     ImpInit();
485 }
486 
487 /*************************************************************************
488 |*
489 |*    Stream::~Stream()
490 |*
491 |*    Beschreibung      STREAM.SDW
492 |*    Ersterstellung    OV 08.06.94
493 |*    Letzte Aenderung  OV 08.06.94
494 |*
495 *************************************************************************/
496 
~SvStream()497 SvStream::~SvStream()
498 {
499     DBG_DTOR( Stream, NULL );
500 
501     if ( xLockBytes.Is() )
502         Flush();
503 
504     if( pRWBuf )
505         delete[] pRWBuf;
506 }
507 
508 /*************************************************************************
509 |*
510 |*    Stream::IsA()
511 |*
512 |*    Beschreibung      STREAM.SDW
513 |*    Ersterstellung    OV 08.06.94
514 |*    Letzte Aenderung  OV 08.06.94
515 |*
516 *************************************************************************/
517 
IsA() const518 sal_uInt16 SvStream::IsA() const
519 {
520     return (sal_uInt16)ID_STREAM;
521 }
522 
523 /*************************************************************************
524 |*
525 |*    Stream::ClearError()
526 |*
527 |*    Beschreibung      STREAM.SDW
528 |*    Ersterstellung    OV 08.06.94
529 |*    Letzte Aenderung  OV 08.06.94
530 |*
531 *************************************************************************/
532 
ClearError()533 void SvStream::ClearError()
534 {
535     bIsEof = sal_False;
536     nError = SVSTREAM_OK;
537 }
538 
539 /*************************************************************************
540 |*
541 |*    Stream::SetError()
542 |*
543 |*    Beschreibung      STREAM.SDW
544 |*    Ersterstellung    OV 08.06.94
545 |*    Letzte Aenderung  OV 08.06.94
546 |*
547 *************************************************************************/
548 
SetError(sal_uInt32 nErrorCode)549 void SvStream::SetError( sal_uInt32 nErrorCode )
550 {
551     if ( nError == SVSTREAM_OK )
552         nError = nErrorCode;
553 }
554 
555 
556 /*************************************************************************
557 |*
558 |*    Stream::SetNumberFormatInt()
559 |*
560 |*    Beschreibung      STREAM.SDW
561 |*    Ersterstellung    OV 08.06.94
562 |*    Letzte Aenderung  OV 08.06.94
563 |*
564 *************************************************************************/
565 
SetNumberFormatInt(sal_uInt16 nNewFormat)566 void SvStream::SetNumberFormatInt( sal_uInt16 nNewFormat )
567 {
568     nNumberFormatInt = nNewFormat;
569     bSwap = sal_False;
570 #ifdef OSL_BIGENDIAN
571     if( nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN )
572         bSwap = sal_True;
573 #else
574     if( nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN )
575         bSwap = sal_True;
576 #endif
577 }
578 
579 /*************************************************************************
580 |*
581 |*    Stream::SetBufferSize()
582 |*
583 |*    Beschreibung      STREAM.SDW
584 |*    Ersterstellung    OV 08.06.94
585 |*    Letzte Aenderung  OV 08.06.94
586 |*
587 *************************************************************************/
588 
SetBufferSize(sal_uInt16 nBufferSize)589 void SvStream::SetBufferSize( sal_uInt16 nBufferSize )
590 {
591     sal_Size nActualFilePos = Tell();
592     sal_Bool bDontSeek = (sal_Bool)(pRWBuf == 0);
593 
594     if( bIsDirty && bIsConsistent && bIsWritable )  // wg. Windows NT: Access denied
595         Flush();
596 
597     if( nBufSize )
598     {
599         delete[] pRWBuf;
600         nBufFilePos += nBufActualPos;
601     }
602 
603     pRWBuf          = 0;
604     nBufActualLen   = 0;
605     nBufActualPos   = 0;
606     nBufSize        = nBufferSize;
607     if( nBufSize )
608         pRWBuf = new sal_uInt8[ nBufSize ];
609     bIsConsistent   = sal_True;
610     pBufPos         = pRWBuf;
611     eIOMode = STREAM_IO_DONTKNOW;
612     if( !bDontSeek )
613         SeekPos( nActualFilePos );
614 }
615 
616 /*************************************************************************
617 |*
618 |*    Stream::ClearBuffer()
619 |*
620 |*    Beschreibung      STREAM.SDW
621 |*    Ersterstellung    OV 08.06.94
622 |*    Letzte Aenderung  OV 08.06.94
623 |*
624 *************************************************************************/
625 
ClearBuffer()626 void SvStream::ClearBuffer()
627 {
628     nBufActualLen   = 0;
629     nBufActualPos   = 0;
630     nBufFilePos     = 0;
631     pBufPos         = pRWBuf;
632     bIsDirty        = sal_False;
633     bIsConsistent   = sal_True;
634     eIOMode         = STREAM_IO_DONTKNOW;
635 
636     bIsEof          = sal_False;
637 }
638 
639 /*************************************************************************
640 |*
641 |*    Stream::ResetError()
642 |*
643 |*    Beschreibung      STREAM.SDW
644 |*    Ersterstellung    OV 08.06.94
645 |*    Letzte Aenderung  OV 08.06.94
646 |*
647 *************************************************************************/
648 
ResetError()649 void SvStream::ResetError()
650 {
651     ClearError();
652 }
653 
654 /*************************************************************************
655 |*
656 |*    Stream::ReadLine()
657 |*
658 |*    Beschreibung      STREAM.SDW
659 |*    Ersterstellung    OV 08.06.94
660 |*    Letzte Aenderung  OV 08.06.94
661 |*
662 *************************************************************************/
663 
ReadByteStringLine(::rtl::OUString & rStr,rtl_TextEncoding eSrcCharSet)664 sal_Bool SvStream::ReadByteStringLine( ::rtl::OUString& rStr, rtl_TextEncoding eSrcCharSet )
665 {
666     sal_Bool bRet;
667     ::rtl::OStringBuffer stringBuffer;
668 
669     bRet = ReadLine( stringBuffer );
670     rStr = ::rtl::OStringToOUString( stringBuffer.makeStringAndClear(), eSrcCharSet );
671     return bRet;
672 }
673 
ReadByteStringLine(String & rStr,rtl_TextEncoding eSrcCharSet)674 sal_Bool SvStream::ReadByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet )
675 {
676     sal_Bool bRet;
677     ByteString aStr;
678 
679     bRet = ReadLine(aStr);
680     rStr = UniString( aStr, eSrcCharSet );
681     return bRet;
682 }
683 
ReadLine(::rtl::OStringBuffer & rStr)684 sal_Bool SvStream::ReadLine( ::rtl::OStringBuffer& rStr )
685 {
686     sal_Char    buf[256+1];
687     sal_Bool    bEnd = sal_False;
688     sal_Size    nOldFilePos = Tell();
689     sal_Char    c = 0;
690     sal_Size    nTotalLen = 0;
691 
692     rStr.setLength( 0 );
693     while( !bEnd && !GetError() )   // !!! do not test for EOF,
694                                     // !!! because we read in blocks
695     {
696         sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
697         if ( !nLen )
698         {
699             if ( rStr.getLength() == 0 )
700             {
701                 // the very first block read failed -> abort
702                 bIsEof = sal_True;
703                 return sal_False;
704             }
705             else
706                 break;
707         }
708 
709         sal_uInt16 j, n;
710         for( j = n = 0; j < nLen ; ++j )
711         {
712             c = buf[j];
713             if ( c == '\n' || c == '\r' )
714             {
715                 bEnd = sal_True;
716                 break;
717             }
718             // erAck 26.02.01: Old behavior was no special treatment of '\0'
719             // character here, but a following rStr+=c did ignore it. Is this
720             // really intended? Or should a '\0' better terminate a line?
721             // The nOldFilePos stuff wasn't correct then anyways.
722             if ( c )
723             {
724                 if ( n < j )
725                     buf[n] = c;
726                 ++n;
727             }
728         }
729         if ( n )
730             rStr.append( buf, n );
731         nTotalLen += j;
732     }
733 
734     if ( !bEnd && !GetError() && rStr.getLength() )
735         bEnd = sal_True;
736 
737     nOldFilePos += nTotalLen;
738     if( Tell() > nOldFilePos )
739         nOldFilePos++;
740     Seek( nOldFilePos );  // seek because of the above BlockRead!
741 
742     if ( bEnd && (c=='\r' || c=='\n') )  // Special treatment of DOS files
743     {
744         char cTemp;
745         sal_Size nLen = Read((char*)&cTemp , sizeof(cTemp) );
746         if ( nLen ) {
747             if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
748                 Seek( nOldFilePos );
749         }
750     }
751 
752     if ( bEnd )
753         bIsEof = sal_False;
754     return bEnd;
755 }
756 
ReadLine(ByteString & rStr)757 sal_Bool SvStream::ReadLine( ByteString& rStr )
758 {
759     sal_Char    buf[256+1];
760     sal_Bool        bEnd        = sal_False;
761     sal_Size       nOldFilePos = Tell();
762     sal_Char    c           = 0;
763     sal_Size       nTotalLen   = 0;
764 
765     rStr.Erase();
766     while( !bEnd && !GetError() )   // !!! nicht auf EOF testen,
767                                     // !!! weil wir blockweise
768                                     // !!! lesen
769     {
770         sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
771         if ( !nLen )
772         {
773             if ( rStr.Len() == 0 )
774             {
775                 // der allererste Blockread hat fehlgeschlagen -> Abflug
776                 bIsEof = sal_True;
777                 return sal_False;
778             }
779             else
780                 break;
781         }
782 
783         sal_uInt16 j, n;
784         for( j = n = 0; j < nLen ; ++j )
785         {
786             c = buf[j];
787             if ( c == '\n' || c == '\r' )
788             {
789                 bEnd = sal_True;
790                 break;
791             }
792             // erAck 26.02.01: Old behavior was no special treatment of '\0'
793             // character here, but a following rStr+=c did ignore it. Is this
794             // really intended? Or should a '\0' better terminate a line?
795             // The nOldFilePos stuff wasn't correct then anyways.
796             if ( c )
797             {
798                 if ( n < j )
799                     buf[n] = c;
800                 ++n;
801             }
802         }
803         if ( n )
804             rStr.Append( buf, n );
805         nTotalLen += j;
806     }
807 
808     if ( !bEnd && !GetError() && rStr.Len() )
809         bEnd = sal_True;
810 
811     nOldFilePos += nTotalLen;
812     if( Tell() > nOldFilePos )
813         nOldFilePos++;
814     Seek( nOldFilePos );  // seeken wg. obigem BlockRead!
815 
816     if ( bEnd && (c=='\r' || c=='\n') )  // Sonderbehandlung DOS-Dateien
817     {
818         char cTemp;
819         sal_Size nLen = Read((char*)&cTemp , sizeof(cTemp) );
820         if ( nLen ) {
821             if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
822                 Seek( nOldFilePos );
823         }
824     }
825 
826     if ( bEnd )
827         bIsEof = sal_False;
828     return bEnd;
829 }
830 
ReadUniStringLine(String & rStr)831 sal_Bool SvStream::ReadUniStringLine( String& rStr )
832 {
833     sal_Unicode buf[256+1];
834     sal_Bool        bEnd        = sal_False;
835     sal_Size       nOldFilePos = Tell();
836     sal_Unicode c           = 0;
837     sal_Size       nTotalLen   = 0;
838 
839     DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "ReadUniStringLine: swapping sizeof(sal_Unicode) not implemented" );
840 
841     rStr.Erase();
842     while( !bEnd && !GetError() )   // !!! nicht auf EOF testen,
843                                     // !!! weil wir blockweise
844                                     // !!! lesen
845     {
846         sal_uInt16 nLen = (sal_uInt16)Read( (char*)buf, sizeof(buf)-sizeof(sal_Unicode) );
847         nLen /= sizeof(sal_Unicode);
848         if ( !nLen )
849         {
850             if ( rStr.Len() == 0 )
851             {
852                 // der allererste Blockread hat fehlgeschlagen -> Abflug
853                 bIsEof = sal_True;
854                 return sal_False;
855             }
856             else
857                 break;
858         }
859 
860         sal_uInt16 j, n;
861         for( j = n = 0; j < nLen ; ++j )
862         {
863             if ( bSwap )
864                 SwapUShort( buf[n] );
865             c = buf[j];
866             if ( c == '\n' || c == '\r' )
867             {
868                 bEnd = sal_True;
869                 break;
870             }
871             // erAck 26.02.01: Old behavior was no special treatment of '\0'
872             // character here, but a following rStr+=c did ignore it. Is this
873             // really intended? Or should a '\0' better terminate a line?
874             // The nOldFilePos stuff wasn't correct then anyways.
875             if ( c )
876             {
877                 if ( n < j )
878                     buf[n] = c;
879                 ++n;
880             }
881         }
882         if ( n )
883             rStr.Append( buf, n );
884         nTotalLen += j;
885     }
886 
887     if ( !bEnd && !GetError() && rStr.Len() )
888         bEnd = sal_True;
889 
890     nOldFilePos += nTotalLen * sizeof(sal_Unicode);
891     if( Tell() > nOldFilePos )
892         nOldFilePos += sizeof(sal_Unicode);
893     Seek( nOldFilePos );  // seeken wg. obigem BlockRead!
894 
895     if ( bEnd && (c=='\r' || c=='\n') )  // Sonderbehandlung DOS-Dateien
896     {
897         sal_Unicode cTemp;
898         Read( (char*)&cTemp, sizeof(cTemp) );
899         if ( bSwap )
900             SwapUShort( cTemp );
901         if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
902             Seek( nOldFilePos );
903     }
904 
905     if ( bEnd )
906         bIsEof = sal_False;
907     return bEnd;
908 }
909 
ReadUniOrByteStringLine(String & rStr,rtl_TextEncoding eSrcCharSet)910 sal_Bool SvStream::ReadUniOrByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet )
911 {
912     if ( eSrcCharSet == RTL_TEXTENCODING_UNICODE )
913         return ReadUniStringLine( rStr );
914     else
915         return ReadByteStringLine( rStr, eSrcCharSet );
916 }
917 
918 /*************************************************************************
919 |*
920 |*    Stream::ReadCString
921 |*
922 *************************************************************************/
923 
ReadCString(ByteString & rStr)924 sal_Bool SvStream::ReadCString( ByteString& rStr )
925 {
926     if( rStr.Len() )
927         rStr.Erase();
928 
929     sal_Char buf[ 256 + 1 ];
930     sal_Bool bEnd = sal_False;
931     sal_Size nFilePos = Tell();
932 
933     while( !bEnd && !GetError() )
934     {
935         sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
936 		sal_uInt16 nReallyRead = nLen;
937         if( !nLen )
938             break;
939 
940         const sal_Char* pPtr = buf;
941         while( *pPtr && nLen )
942             ++pPtr, --nLen;
943 
944         bEnd =	( nReallyRead < sizeof(buf)-1 )			// read less than attempted to read
945                 ||  (  ( nLen > 0 )                    // OR it is inside the block we read
946 					&&	( 0 == *pPtr )					//    AND found a string terminator
947                     );
948 
949         rStr.Append( buf, ::sal::static_int_cast< xub_StrLen >( pPtr - buf ) );
950     }
951 
952     nFilePos += rStr.Len();
953     if( Tell() > nFilePos )
954         nFilePos++;
955     Seek( nFilePos );  // seeken wg. obigem BlockRead!
956     return bEnd;
957 }
958 
ReadCString(String & rStr,rtl_TextEncoding eToEncode)959 sal_Bool SvStream::ReadCString( String& rStr, rtl_TextEncoding eToEncode )
960 {
961     ByteString sStr;
962     sal_Bool bRet = ReadCString( sStr );
963     rStr = String( sStr, eToEncode );
964     return bRet;
965 }
966 
967 
968 /*************************************************************************
969 |*
970 |*    Stream::WriteUnicodeText()
971 |*
972 *************************************************************************/
973 
WriteUnicodeText(const String & rStr)974 sal_Bool SvStream::WriteUnicodeText( const String& rStr )
975 {
976     DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "WriteUnicodeText: swapping sizeof(sal_Unicode) not implemented" );
977     if ( bSwap )
978     {
979         xub_StrLen nLen = rStr.Len();
980         sal_Unicode aBuf[384];
981         sal_Unicode* const pTmp = ( nLen > 384 ? new sal_Unicode[nLen] : aBuf);
982         memcpy( pTmp, rStr.GetBuffer(), nLen * sizeof(sal_Unicode) );
983         sal_Unicode* p = pTmp;
984         const sal_Unicode* const pStop = pTmp + nLen;
985         while ( p < pStop )
986         {
987             SwapUShort( *p );
988             p++;
989         }
990         Write( (char*)pTmp, nLen * sizeof(sal_Unicode) );
991         if ( pTmp != aBuf )
992             delete [] pTmp;
993     }
994     else
995         Write( (char*)rStr.GetBuffer(), rStr.Len() * sizeof(sal_Unicode) );
996     return nError == SVSTREAM_OK;
997 }
998 
WriteUnicodeOrByteText(const String & rStr,rtl_TextEncoding eDestCharSet)999 sal_Bool SvStream::WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet )
1000 {
1001     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1002         return WriteUnicodeText( rStr );
1003     else
1004     {
1005         ByteString aStr( rStr, eDestCharSet );
1006         Write( aStr.GetBuffer(), aStr.Len() );
1007         return nError == SVSTREAM_OK;
1008     }
1009 }
1010 
1011 /*************************************************************************
1012 |*
1013 |*    Stream::WriteLine()
1014 |*
1015 |*    Beschreibung      STREAM.SDW
1016 |*    Ersterstellung    OV 08.06.94
1017 |*    Letzte Aenderung  OV 08.06.94
1018 |*
1019 *************************************************************************/
1020 
WriteByteStringLine(const String & rStr,rtl_TextEncoding eDestCharSet)1021 sal_Bool SvStream::WriteByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet )
1022 {
1023     return WriteLine( ByteString( rStr, eDestCharSet ) );
1024 }
1025 
WriteLine(const ByteString & rStr)1026 sal_Bool SvStream::WriteLine( const ByteString& rStr )
1027 {
1028     Write( rStr.GetBuffer(), rStr.Len() );
1029     endl(*this);
1030     return nError == SVSTREAM_OK;
1031 }
1032 
WriteUniStringLine(const String & rStr)1033 sal_Bool SvStream::WriteUniStringLine( const String& rStr )
1034 {
1035     WriteUnicodeText( rStr );
1036     endlu(*this);
1037     return nError == SVSTREAM_OK;
1038 }
1039 
WriteUniOrByteStringLine(const String & rStr,rtl_TextEncoding eDestCharSet)1040 sal_Bool SvStream::WriteUniOrByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet )
1041 {
1042     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1043         return WriteUniStringLine( rStr );
1044     else
1045         return WriteByteStringLine( rStr, eDestCharSet );
1046 }
1047 
1048 /*************************************************************************
1049 |*
1050 |*    Stream::WriteLines()
1051 |*
1052 |*    Beschreibung      STREAM.SDW
1053 |*    Ersterstellung    OV 17.07.95
1054 |*    Letzte Aenderung  OV 17.07.95
1055 |*
1056 *************************************************************************/
1057 
WriteByteStringLines(const String & rStr,rtl_TextEncoding eDestCharSet)1058 sal_Bool SvStream::WriteByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet )
1059 {
1060     return WriteLines( ByteString( rStr, eDestCharSet ) );
1061 }
1062 
WriteLines(const ByteString & rStr)1063 sal_Bool SvStream::WriteLines( const ByteString& rStr )
1064 {
1065     ByteString aStr( rStr );
1066     aStr.ConvertLineEnd( eLineDelimiter );
1067     Write( aStr.GetBuffer(), aStr.Len() );
1068     endl( *this );
1069     return (sal_Bool)(nError == SVSTREAM_OK);
1070 }
1071 
WriteUniStringLines(const String & rStr)1072 sal_Bool SvStream::WriteUniStringLines( const String& rStr )
1073 {
1074     String aStr( rStr );
1075     aStr.ConvertLineEnd( eLineDelimiter );
1076     WriteUniStringLine( aStr );
1077     return nError == SVSTREAM_OK;
1078 }
1079 
WriteUniOrByteStringLines(const String & rStr,rtl_TextEncoding eDestCharSet)1080 sal_Bool SvStream::WriteUniOrByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet )
1081 {
1082     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1083         return WriteUniStringLines( rStr );
1084     else
1085         return WriteByteStringLines( rStr, eDestCharSet );
1086 }
1087 
1088 /*************************************************************************
1089 |*
1090 |*    Stream::WriteUniOrByteChar()
1091 |*
1092 *************************************************************************/
1093 
WriteUniOrByteChar(sal_Unicode ch,rtl_TextEncoding eDestCharSet)1094 sal_Bool SvStream::WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet )
1095 {
1096     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1097         *this << ch;
1098     else
1099     {
1100         ByteString aStr( ch, eDestCharSet );
1101         Write( aStr.GetBuffer(), aStr.Len() );
1102     }
1103     return nError == SVSTREAM_OK;
1104 }
1105 
1106 /*************************************************************************
1107 |*
1108 |*    Stream::StartWritingUnicodeText()
1109 |*
1110 *************************************************************************/
1111 
StartWritingUnicodeText()1112 sal_Bool SvStream::StartWritingUnicodeText()
1113 {
1114     SetEndianSwap( sal_False );     // write native format
1115     // BOM, Byte Order Mark, U+FEFF, see
1116     // http://www.unicode.org/faq/utf_bom.html#BOM
1117     // Upon read: 0xfeff(-257) => no swap; 0xfffe(-2) => swap
1118     *this << sal_uInt16( 0xfeff );
1119     return nError == SVSTREAM_OK;
1120 }
1121 
1122 /*************************************************************************
1123 |*
1124 |*    Stream::StartReadingUnicodeText()
1125 |*
1126 *************************************************************************/
1127 
StartReadingUnicodeText(rtl_TextEncoding eReadBomCharSet)1128 sal_Bool SvStream::StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet )
1129 {
1130     if (!(  eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1131             eReadBomCharSet == RTL_TEXTENCODING_UNICODE ||
1132             eReadBomCharSet == RTL_TEXTENCODING_UTF8))
1133         return sal_True;    // nothing to read
1134 
1135     bool bTryUtf8 = false;
1136     sal_uInt16 nFlag;
1137     sal_sSize nBack = sizeof(nFlag);
1138     *this >> nFlag;
1139     switch ( nFlag )
1140     {
1141         case 0xfeff :
1142             // native UTF-16
1143             if (    eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1144                     eReadBomCharSet == RTL_TEXTENCODING_UNICODE)
1145                 nBack = 0;
1146         break;
1147         case 0xfffe :
1148             // swapped UTF-16
1149             if (    eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1150                     eReadBomCharSet == RTL_TEXTENCODING_UNICODE)
1151             {
1152                 SetEndianSwap( !bSwap );
1153                 nBack = 0;
1154             }
1155         break;
1156         case 0xefbb :
1157             if (nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN &&
1158                     (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1159                      eReadBomCharSet == RTL_TEXTENCODING_UTF8))
1160                 bTryUtf8 = true;
1161         break;
1162         case 0xbbef :
1163             if (nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN &&
1164                     (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1165                      eReadBomCharSet == RTL_TEXTENCODING_UTF8))
1166                 bTryUtf8 = true;
1167         break;
1168         default:
1169             ;   // nothing
1170     }
1171     if (bTryUtf8)
1172     {
1173         sal_uChar nChar;
1174         nBack += sizeof(nChar);
1175         *this >> nChar;
1176         if (nChar == 0xbf)
1177             nBack = 0;      // it is UTF-8
1178     }
1179     if (nBack)
1180         SeekRel( -nBack );      // no BOM, pure data
1181     return nError == SVSTREAM_OK;
1182 }
1183 
1184 /*************************************************************************
1185 |*
1186 |*    Stream::ReadCsvLine()
1187 |*
1188 *************************************************************************/
1189 
1190 // Precondition: pStr is guaranteed to be non-NULL and points to a 0-terminated
1191 // array.
lcl_UnicodeStrChr(const sal_Unicode * pStr,sal_Unicode c)1192 inline const sal_Unicode* lcl_UnicodeStrChr( const sal_Unicode* pStr,
1193         sal_Unicode c )
1194 {
1195     while (*pStr)
1196     {
1197         if (*pStr == c)
1198             return pStr;
1199         ++pStr;
1200     }
1201     return 0;
1202 }
1203 
ReadCsvLine(String & rStr,sal_Bool bEmbeddedLineBreak,const String & rFieldSeparators,sal_Unicode cFieldQuote,sal_Bool bAllowBackslashEscape)1204 sal_Bool SvStream::ReadCsvLine( String& rStr, sal_Bool bEmbeddedLineBreak,
1205         const String& rFieldSeparators, sal_Unicode cFieldQuote,
1206         sal_Bool bAllowBackslashEscape)
1207 {
1208     ReadUniOrByteStringLine( rStr);
1209 
1210     if (bEmbeddedLineBreak)
1211     {
1212         const sal_Unicode* pSeps = rFieldSeparators.GetBuffer();
1213         xub_StrLen nLastOffset = 0;
1214         bool isQuoted = false;
1215         bool isFieldStarting = true;
1216         while (!IsEof() && rStr.Len() < STRING_MAXLEN)
1217         {
1218             bool wasQuote = false;
1219             bool bBackslashEscaped = false;
1220             const sal_Unicode *p;
1221             p = rStr.GetBuffer();
1222             p += nLastOffset;
1223             while (*p)
1224             {
1225                 if (isQuoted)
1226                 {
1227                     if (*p == cFieldQuote && !bBackslashEscaped)
1228                         wasQuote = !wasQuote;
1229                     else
1230                     {
1231                         if (bAllowBackslashEscape)
1232                         {
1233                             if (*p == '\\')
1234                                 bBackslashEscaped = !bBackslashEscaped;
1235                             else
1236                                 bBackslashEscaped = false;
1237                         }
1238                         if (wasQuote)
1239                         {
1240                             wasQuote = false;
1241                             isQuoted = false;
1242                             if (lcl_UnicodeStrChr( pSeps, *p ))
1243                                 isFieldStarting = true;
1244                         }
1245                     }
1246                 }
1247                 else
1248                 {
1249                     if (isFieldStarting)
1250                     {
1251                         isFieldStarting = false;
1252                         if (*p == cFieldQuote)
1253                             isQuoted = true;
1254                         else if (lcl_UnicodeStrChr( pSeps, *p ))
1255                             isFieldStarting = true;
1256                     }
1257                     else if (lcl_UnicodeStrChr( pSeps, *p ))
1258                         isFieldStarting = true;
1259                 }
1260                 ++p;
1261             }
1262 
1263             if (wasQuote)
1264                 isQuoted = false;
1265 
1266             if (isQuoted)
1267             {
1268                 nLastOffset = rStr.Len();
1269                 String aNext;
1270                 ReadUniOrByteStringLine( aNext);
1271                 rStr += sal_Unicode(_LF);
1272                 rStr += aNext;
1273             }
1274             else
1275                 break;
1276         }
1277     }
1278     return nError == SVSTREAM_OK;
1279 }
1280 
1281 /*************************************************************************
1282 |*
1283 |*    Stream::SeekRel()
1284 |*
1285 |*    Beschreibung      STREAM.SDW
1286 |*    Ersterstellung    OV 08.06.94
1287 |*    Letzte Aenderung  OV 08.06.94
1288 |*
1289 *************************************************************************/
1290 
SeekRel(sal_sSize nPos)1291 sal_Size SvStream::SeekRel( sal_sSize nPos )
1292 {
1293     sal_Size nActualPos = Tell();
1294 
1295 	if ( nPos >= 0 )
1296 	{
1297 		if ( SAL_MAX_SIZE - nActualPos > (sal_Size)nPos )
1298     		nActualPos += nPos;
1299 	}
1300 	else
1301 	{
1302 		sal_Size nAbsPos = (sal_Size)-nPos;
1303 		if ( nActualPos >= nAbsPos )
1304 			nActualPos -= nAbsPos;
1305 	}
1306 
1307     pBufPos = pRWBuf + nActualPos;
1308     return Seek( nActualPos );
1309 }
1310 
1311 /*************************************************************************
1312 |*
1313 |*    Stream::operator>>()
1314 |*
1315 |*    Beschreibung      STREAM.SDW
1316 |*    Ersterstellung    OV 08.06.94
1317 |*    Letzte Aenderung  OV 08.06.94
1318 |*
1319 *************************************************************************/
1320 
operator >>(sal_uInt16 & r)1321 SvStream& SvStream::operator >> ( sal_uInt16& r )
1322 {
1323     READNUMBER_WITHOUT_SWAP(sal_uInt16,r)
1324     if( bSwap )
1325         SwapUShort(r);
1326     return *this;
1327 }
1328 
operator >>(sal_uInt32 & r)1329 SvStream& SvStream::operator>> ( sal_uInt32& r )
1330 {
1331     READNUMBER_WITHOUT_SWAP(sal_uInt32,r)
1332     if( bSwap )
1333         SwapULong(r);
1334     return *this;
1335 }
1336 
operator >>(long & r)1337 SvStream& SvStream::operator >> ( long& r )
1338 {
1339 #if(SAL_TYPES_SIZEOFLONG != 4)
1340     int tmp = r;
1341     *this >> tmp;
1342     r = tmp;
1343 #else
1344     READNUMBER_WITHOUT_SWAP(long,r)
1345     if( bSwap )
1346         SwapLong(r);
1347 #endif
1348     return *this;
1349 }
1350 
operator >>(short & r)1351 SvStream& SvStream::operator >> ( short& r )
1352 {
1353     READNUMBER_WITHOUT_SWAP(short,r)
1354     if( bSwap )
1355         SwapShort(r);
1356     return *this;
1357 }
1358 
operator >>(int & r)1359 SvStream& SvStream::operator >> ( int& r )
1360 {
1361     READNUMBER_WITHOUT_SWAP(int,r)
1362     if( bSwap )
1363         SwapLongInt(r);
1364     return *this;
1365 }
1366 
operator >>(signed char & r)1367 SvStream& SvStream::operator>>( signed char& r )
1368 {
1369     if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1370         sizeof(signed char) <= nBufFree )
1371     {
1372         r = *pBufPos;
1373         nBufActualPos += sizeof(signed char);
1374         pBufPos += sizeof(signed char);
1375         nBufFree -= sizeof(signed char);
1376     }
1377     else
1378         Read( (char*)&r, sizeof(signed char) );
1379     return *this;
1380 }
1381 
1382 // Sonderbehandlung fuer Chars wegen PutBack
1383 
operator >>(char & r)1384 SvStream& SvStream::operator>>( char& r )
1385 {
1386     if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1387         sizeof(char) <= nBufFree )
1388     {
1389         r = *pBufPos;
1390         nBufActualPos += sizeof(char);
1391         pBufPos += sizeof(char);
1392         nBufFree -= sizeof(char);
1393     }
1394     else
1395         Read( (char*)&r, sizeof(char) );
1396     return *this;
1397 }
1398 
operator >>(unsigned char & r)1399 SvStream& SvStream::operator>>( unsigned char& r )
1400 {
1401     if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1402         sizeof(char) <= nBufFree )
1403     {
1404         r = *pBufPos;
1405         nBufActualPos += sizeof(char);
1406         pBufPos += sizeof(char);
1407         nBufFree -= sizeof(char);
1408     }
1409     else
1410         Read( (char*)&r, sizeof(char) );
1411     return *this;
1412 }
1413 
operator >>(float & r)1414 SvStream& SvStream::operator>>( float& r )
1415 {
1416     // Read( (char*)&r, sizeof(float) );
1417     READNUMBER_WITHOUT_SWAP(float,r)
1418 #if defined UNX
1419     if( bSwap )
1420       SwapFloat(r);
1421 #endif
1422     return *this;
1423 }
1424 
operator >>(double & r)1425 SvStream& SvStream::operator>>( double& r )
1426 {
1427     // Read( (char*)&r, sizeof(double) );
1428     READNUMBER_WITHOUT_SWAP(double,r)
1429 #if defined UNX
1430     if( bSwap )
1431       SwapDouble(r);
1432 #endif
1433     return *this;
1434 }
1435 
operator >>(SvStream & rStream)1436 SvStream& SvStream::operator>> ( SvStream& rStream )
1437 {
1438     const sal_uInt32 cBufLen = 0x8000;
1439     char* pBuf = new char[ cBufLen ];
1440 
1441     sal_uInt32 nCount;
1442     do {
1443         nCount = Read( pBuf, cBufLen );
1444         rStream.Write( pBuf, nCount );
1445     } while( nCount == cBufLen );
1446 
1447     delete[] pBuf;
1448     return *this;
1449 }
1450 
1451 /*************************************************************************
1452 |*
1453 |*    Stream::operator<<()
1454 |*
1455 |*    Beschreibung      STREAM.SDW
1456 |*    Ersterstellung    OV 08.06.94
1457 |*    Letzte Aenderung  OV 08.06.94
1458 |*
1459 *************************************************************************/
1460 
operator <<(sal_uInt16 v)1461 SvStream& SvStream::operator<< ( sal_uInt16 v )
1462 {
1463     if( bSwap )
1464         SwapUShort(v);
1465     WRITENUMBER_WITHOUT_SWAP(sal_uInt16,v)
1466     return *this;
1467 }
1468 
operator <<(sal_uInt32 v)1469 SvStream& SvStream::operator<<  ( sal_uInt32 v )
1470 {
1471     if( bSwap )
1472         SwapULong(v);
1473     WRITENUMBER_WITHOUT_SWAP(sal_uInt32,v)
1474     return *this;
1475 }
1476 
operator <<(long v)1477 SvStream& SvStream::operator<< ( long v )
1478 {
1479 #if(SAL_TYPES_SIZEOFLONG != 4)
1480     int tmp = v;
1481     *this << tmp;
1482 #else
1483     if( bSwap )
1484         SwapLong(v);
1485     WRITENUMBER_WITHOUT_SWAP(long,v)
1486 #endif
1487     return *this;
1488 }
1489 
operator <<(short v)1490 SvStream& SvStream::operator<<  ( short v )
1491 {
1492     if( bSwap )
1493         SwapShort(v);
1494     WRITENUMBER_WITHOUT_SWAP(short,v)
1495     return *this;
1496 }
1497 
operator <<(int v)1498 SvStream& SvStream::operator<<( int v )
1499 {
1500     if( bSwap )
1501         SwapLongInt( v );
1502     WRITENUMBER_WITHOUT_SWAP(int,v)
1503     return *this;
1504 }
1505 
operator <<(signed char v)1506 SvStream& SvStream::operator<<  ( signed char v )
1507 {
1508     //SDO
1509     int tmp = eIOMode;
1510     if(tmp == STREAM_IO_WRITE && sizeof(signed char) <= nBufFree )
1511     {
1512         *pBufPos = v;
1513         pBufPos++; // sizeof(char);
1514         nBufActualPos++;
1515         if( nBufActualPos > nBufActualLen )  // Append ?
1516             nBufActualLen = nBufActualPos;
1517         nBufFree--; // = sizeof(char);
1518         bIsDirty = sal_True;
1519     }
1520     else
1521         Write( (char*)&v, sizeof(signed char) );
1522     return *this;
1523 }
1524 
1525 // Sonderbehandlung fuer chars wegen PutBack
1526 
operator <<(char v)1527 SvStream& SvStream::operator<<  ( char v )
1528 {
1529     //SDO
1530     int tmp = eIOMode;
1531     if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
1532     {
1533         *pBufPos = v;
1534         pBufPos++; // sizeof(char);
1535         nBufActualPos++;
1536         if( nBufActualPos > nBufActualLen )  // Append ?
1537             nBufActualLen = nBufActualPos;
1538         nBufFree--; // = sizeof(char);
1539         bIsDirty = sal_True;
1540     }
1541     else
1542         Write( (char*)&v, sizeof(char) );
1543     return *this;
1544 }
1545 
operator <<(unsigned char v)1546 SvStream& SvStream::operator<<  ( unsigned char v )
1547 {
1548 //SDO
1549     int tmp = eIOMode;
1550     if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
1551     {
1552         *(unsigned char*)pBufPos = v;
1553         pBufPos++; // = sizeof(char);
1554         nBufActualPos++; // = sizeof(char);
1555         if( nBufActualPos > nBufActualLen )  // Append ?
1556             nBufActualLen = nBufActualPos;
1557         nBufFree--;
1558         bIsDirty = sal_True;
1559     }
1560     else
1561         Write( (char*)&v, sizeof(char) );
1562     return *this;
1563 }
1564 
operator <<(float v)1565 SvStream& SvStream::operator<< ( float v )
1566 {
1567 #ifdef UNX
1568     if( bSwap )
1569       SwapFloat(v);
1570 #endif
1571     WRITENUMBER_WITHOUT_SWAP(float,v)
1572     return *this;
1573 }
1574 
operator <<(const double & r)1575 SvStream& SvStream::operator<< ( const double& r )
1576 {
1577 //    Write( (char*)&r, sizeof( double ) );
1578 #if defined UNX
1579     if( bSwap )
1580     {
1581       double nHelp = r;
1582       SwapDouble(nHelp);
1583       WRITENUMBER_WITHOUT_SWAP(double,nHelp)
1584       return *this;
1585     }
1586     else
1587 #endif
1588     WRITENUMBER_WITHOUT_SWAP(double,r)
1589 
1590     return *this;
1591 }
1592 
operator <<(const char * pBuf)1593 SvStream& SvStream::operator<<  ( const char* pBuf )
1594 {
1595     Write( pBuf, strlen( pBuf ) );
1596     return *this;
1597 }
1598 
operator <<(const unsigned char * pBuf)1599 SvStream& SvStream::operator<<  ( const unsigned char* pBuf )
1600 {
1601     Write( (char*)pBuf, strlen( (char*)pBuf ) );
1602     return *this;
1603 }
1604 
operator <<(SvStream & rStream)1605 SvStream& SvStream::operator<< ( SvStream& rStream )
1606 {
1607     const sal_uInt32 cBufLen = 0x8000;
1608     char* pBuf = new char[ cBufLen ];
1609     sal_uInt32 nCount;
1610     do {
1611         nCount = rStream.Read( pBuf, cBufLen );
1612         Write( pBuf, nCount );
1613     } while( nCount == cBufLen );
1614 
1615     delete[] pBuf;
1616     return *this;
1617 }
1618 
1619 // -----------------------------------------------------------------------
1620 
ReadByteString(UniString & rStr,rtl_TextEncoding eSrcCharSet)1621 SvStream& SvStream::ReadByteString( UniString& rStr, rtl_TextEncoding eSrcCharSet )
1622 {
1623     // read UTF-16 string directly from stream ?
1624     if (eSrcCharSet == RTL_TEXTENCODING_UNICODE)
1625     {
1626         sal_uInt32 nLen;
1627         operator>> (nLen);
1628         if (nLen)
1629         {
1630             if (nLen > STRING_MAXLEN) {
1631                 SetError(SVSTREAM_GENERALERROR);
1632                 return *this;
1633             }
1634             sal_Unicode *pStr = rStr.AllocBuffer(
1635                 static_cast< xub_StrLen >(nLen));
1636             BOOST_STATIC_ASSERT(STRING_MAXLEN <= SAL_MAX_SIZE / 2);
1637             Read( pStr, nLen << 1 );
1638 
1639             if (bSwap)
1640                 for (sal_Unicode *pEnd = pStr + nLen; pStr < pEnd; pStr++)
1641                     SwapUShort(*pStr);
1642         }
1643         else
1644             rStr.Erase();
1645 
1646         return *this;
1647     }
1648 
1649     ByteString aStr;
1650     ReadByteString( aStr );
1651     rStr = UniString( aStr, eSrcCharSet );
1652     return *this;
1653 }
1654 
1655 // -----------------------------------------------------------------------
1656 
ReadByteString(ByteString & rStr)1657 SvStream& SvStream::ReadByteString( ByteString& rStr )
1658 {
1659     sal_uInt16 nLen = 0;
1660     operator>>( nLen );
1661     if( nLen )
1662     {
1663         char* pTmp = rStr.AllocBuffer( nLen );
1664         nLen = (sal_uInt16)Read( pTmp, nLen );
1665     }
1666     else
1667         rStr.Erase();
1668     return *this;
1669 }
1670 
1671 // -----------------------------------------------------------------------
1672 
WriteByteString(const UniString & rStr,rtl_TextEncoding eDestCharSet)1673 SvStream& SvStream::WriteByteString( const UniString& rStr, rtl_TextEncoding eDestCharSet )
1674 {
1675     // write UTF-16 string directly into stream ?
1676     if (eDestCharSet == RTL_TEXTENCODING_UNICODE)
1677     {
1678         sal_uInt32 nLen = rStr.Len();
1679         operator<< (nLen);
1680         if (nLen)
1681         {
1682             if (bSwap)
1683             {
1684                 const sal_Unicode *pStr = rStr.GetBuffer();
1685                 const sal_Unicode *pEnd = pStr + nLen;
1686 
1687                 for (; pStr < pEnd; pStr++)
1688                 {
1689                     sal_Unicode c = *pStr;
1690                     SwapUShort(c);
1691                     WRITENUMBER_WITHOUT_SWAP(sal_uInt16,c)
1692                 }
1693             }
1694             else
1695                 Write( rStr.GetBuffer(), nLen << 1 );
1696         }
1697 
1698         return *this;
1699     }
1700 
1701     return WriteByteString(ByteString( rStr, eDestCharSet ));
1702 }
1703 
1704 // -----------------------------------------------------------------------
1705 
WriteByteString(const ByteString & rStr)1706 SvStream& SvStream::WriteByteString( const ByteString& rStr)
1707 {
1708     sal_uInt16 nLen = rStr.Len();
1709     operator<< ( nLen );
1710     if( nLen != 0 )
1711         Write( rStr.GetBuffer(), nLen );
1712     return *this;
1713 }
1714 
1715 /*************************************************************************
1716 |*
1717 |*    Stream::Read()
1718 |*
1719 |*    Beschreibung      STREAM.SDW
1720 |*    Ersterstellung    OV 08.06.94
1721 |*    Letzte Aenderung  OV 08.06.94
1722 |*
1723 *************************************************************************/
1724 
Read(void * pData,sal_Size nCount)1725 sal_Size SvStream::Read( void* pData, sal_Size nCount )
1726 {
1727     sal_Size nSaveCount = nCount;
1728     if( !bIsConsistent )
1729         RefreshBuffer();
1730 
1731     if( !pRWBuf )
1732     {
1733         nCount = GetData( (char*)pData,nCount);
1734         if( nCryptMask )
1735             EncryptBuffer(pData, nCount);
1736         nBufFilePos += nCount;
1737     }
1738     else
1739     {
1740         // ist Block komplett im Puffer
1741         eIOMode = STREAM_IO_READ;
1742         if( nCount <= (sal_Size)(nBufActualLen - nBufActualPos ) )
1743         {
1744             // Ja!
1745             memcpy(pData, pBufPos, (size_t) nCount);
1746             nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1747             pBufPos += nCount;
1748             nBufFree = nBufFree - (sal_uInt16)nCount;
1749         }
1750         else
1751         {
1752             if( bIsDirty ) // Flushen ?
1753             {
1754                 SeekPos( nBufFilePos );
1755                 if( nCryptMask )
1756                     CryptAndWriteBuffer(pRWBuf, nBufActualLen);
1757                 else
1758                     PutData( pRWBuf, nBufActualLen );
1759                 bIsDirty = sal_False;
1760             }
1761 
1762             // passt der Datenblock in den Puffer ?
1763             if( nCount > nBufSize )
1764             {
1765                 // Nein! Deshalb ohne Umweg ueber den Puffer direkt
1766                 // in den Zielbereich einlesen
1767 
1768                 eIOMode = STREAM_IO_DONTKNOW;
1769 
1770                 SeekPos( nBufFilePos + nBufActualPos );
1771                 nBufActualLen = 0;
1772                 pBufPos       = pRWBuf;
1773                 nCount = GetData( (char*)pData, nCount );
1774                 if( nCryptMask )
1775                     EncryptBuffer(pData, nCount);
1776                 nBufFilePos += nCount;
1777                 nBufFilePos += nBufActualPos;
1778                 nBufActualPos = 0;
1779             }
1780             else
1781             {
1782                 // Der Datenblock passt komplett in den Puffer. Deshalb
1783                 // Puffer fuellen und dann die angeforderten Daten in den
1784                 // Zielbereich kopieren.
1785 
1786                 nBufFilePos += nBufActualPos;
1787                 SeekPos( nBufFilePos );
1788 
1789                 // TODO: Typecast vor GetData, sal_uInt16 nCountTmp
1790                 sal_Size nCountTmp = GetData( pRWBuf, nBufSize );
1791                 if( nCryptMask )
1792                     EncryptBuffer(pRWBuf, nCountTmp);
1793                 nBufActualLen = (sal_uInt16)nCountTmp;
1794                 if( nCount > nCountTmp )
1795                 {
1796                     nCount = nCountTmp;  // zurueckstutzen, Eof siehe unten
1797                 }
1798                 memcpy( pData, pRWBuf, (size_t)nCount );
1799                 nBufActualPos = (sal_uInt16)nCount;
1800                 pBufPos = pRWBuf + nCount;
1801             }
1802         }
1803     }
1804     bIsEof = sal_False;
1805     nBufFree = nBufActualLen - nBufActualPos;
1806     if( nCount != nSaveCount && nError != ERRCODE_IO_PENDING )
1807         bIsEof = sal_True;
1808     if( nCount == nSaveCount && nError == ERRCODE_IO_PENDING )
1809         nError = ERRCODE_NONE;
1810     return nCount;
1811 }
1812 
1813 /*************************************************************************
1814 |*
1815 |*    Stream::Write()
1816 |*
1817 |*    Beschreibung      STREAM.SDW
1818 |*    Ersterstellung    OV 08.06.94
1819 |*    Letzte Aenderung  OV 08.06.94
1820 |*
1821 *************************************************************************/
1822 
Write(const void * pData,sal_Size nCount)1823 sal_Size SvStream::Write( const void* pData, sal_Size nCount )
1824 {
1825     if( !nCount )
1826         return 0;
1827     if( !bIsWritable )
1828     {
1829         SetError( ERRCODE_IO_CANTWRITE );
1830         return 0;
1831     }
1832     if( !bIsConsistent )
1833         RefreshBuffer();   // Aenderungen des Puffers durch PutBack loeschen
1834 
1835     if( !pRWBuf )
1836     {
1837         if( nCryptMask )
1838             nCount = CryptAndWriteBuffer( pData, nCount );
1839         else
1840             nCount = PutData( (char*)pData, nCount );
1841         nBufFilePos += nCount;
1842         return nCount;
1843     }
1844 
1845     eIOMode = STREAM_IO_WRITE;
1846     if( nCount <= (sal_Size)(nBufSize - nBufActualPos) )
1847     {
1848         memcpy( pBufPos, pData, (size_t)nCount );
1849         nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1850         // wurde der Puffer erweitert ?
1851         if( nBufActualPos > nBufActualLen )
1852             nBufActualLen = nBufActualPos;
1853 
1854         pBufPos += nCount;
1855         bIsDirty = sal_True;
1856     }
1857     else
1858     {
1859         // Flushen ?
1860         if( bIsDirty )
1861         {
1862             SeekPos( nBufFilePos );
1863             if( nCryptMask )
1864                 CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1865             else
1866                 PutData( pRWBuf, nBufActualLen );
1867             bIsDirty = sal_False;
1868         }
1869 
1870         // passt der Block in den Puffer ?
1871         if( nCount > nBufSize )
1872         {
1873             eIOMode = STREAM_IO_DONTKNOW;
1874             nBufFilePos += nBufActualPos;
1875             nBufActualLen = 0;
1876             nBufActualPos = 0;
1877             pBufPos       = pRWBuf;
1878             SeekPos( nBufFilePos );
1879             if( nCryptMask )
1880                 nCount = CryptAndWriteBuffer( pData, nCount );
1881             else
1882                 nCount = PutData( (char*)pData, nCount );
1883             nBufFilePos += nCount;
1884         }
1885         else
1886         {
1887             // Block in Puffer stellen
1888             memcpy( pRWBuf, pData, (size_t)nCount );
1889 
1890             // Reihenfolge!
1891             nBufFilePos += nBufActualPos;
1892             nBufActualPos = (sal_uInt16)nCount;
1893             pBufPos = pRWBuf + nCount;
1894             nBufActualLen = (sal_uInt16)nCount;
1895             bIsDirty = sal_True;
1896         }
1897     }
1898     nBufFree = nBufSize - nBufActualPos;
1899     return nCount;
1900 }
1901 
1902 
1903 /*************************************************************************
1904 |*
1905 |*    Stream::Seek()
1906 |*
1907 |*    Beschreibung      STREAM.SDW
1908 |*    Ersterstellung    OV 08.06.94
1909 |*    Letzte Aenderung  OV 08.06.94
1910 |*
1911 *************************************************************************/
1912 
Seek(sal_Size nFilePos)1913 sal_Size SvStream::Seek( sal_Size nFilePos )
1914 {
1915     eIOMode = STREAM_IO_DONTKNOW;
1916 
1917     bIsEof = sal_False;
1918     if( !pRWBuf )
1919     {
1920         nBufFilePos = SeekPos( nFilePos );
1921         DBG_ASSERT(Tell()==nBufFilePos,"Out Of Sync!");
1922         return nBufFilePos;
1923     }
1924 
1925     // Ist Position im Puffer ?
1926     if( nFilePos >= nBufFilePos && nFilePos <= (nBufFilePos + nBufActualLen))
1927     {
1928         nBufActualPos = (sal_uInt16)(nFilePos - nBufFilePos);
1929         pBufPos = pRWBuf + nBufActualPos;
1930         // nBufFree korrigieren, damit wir nicht von einem
1931         // PutBack (ignoriert den StreamMode) getoetet werden
1932         nBufFree = nBufActualLen - nBufActualPos;
1933     }
1934     else
1935     {
1936         if( bIsDirty && bIsConsistent)
1937         {
1938             SeekPos( nBufFilePos );
1939             if( nCryptMask )
1940                 CryptAndWriteBuffer( pRWBuf, nBufActualLen );
1941             else
1942                 PutData( pRWBuf, nBufActualLen );
1943             bIsDirty = sal_False;
1944         }
1945         nBufActualLen = 0;
1946         nBufActualPos = 0;
1947         pBufPos       = pRWBuf;
1948         nBufFilePos = SeekPos( nFilePos );
1949     }
1950 #ifdef OV_DEBUG
1951     {
1952         sal_Size nDebugTemp = nBufFilePos + nBufActualPos;
1953         DBG_ASSERT(Tell()==nDebugTemp,"Sync?");
1954     }
1955 #endif
1956     return nBufFilePos + nBufActualPos;
1957 }
1958 
1959 /*************************************************************************
1960 |*
1961 |*    Stream::Flush()
1962 |*
1963 |*    Beschreibung      STREAM.SDW
1964 |*    Ersterstellung    OV 08.06.94
1965 |*    Letzte Aenderung  OV 08.06.94
1966 |*
1967 *************************************************************************/
1968 
Flush()1969 void SvStream::Flush()
1970 {
1971     if( bIsDirty && bIsConsistent )
1972     {
1973         SeekPos( nBufFilePos );
1974         if( nCryptMask )
1975             CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1976         else
1977             if( PutData( pRWBuf, nBufActualLen ) != nBufActualLen )
1978                 SetError( SVSTREAM_WRITE_ERROR );
1979         bIsDirty = sal_False;
1980     }
1981     if( bIsWritable )
1982         FlushData();
1983 }
1984 
1985 
1986 /*************************************************************************
1987 |*
1988 |*    Stream::PutBack()
1989 |*
1990 |*    Beschreibung      STREAM.SDW
1991 |*    Ersterstellung    OV 01.08.94
1992 |*    Letzte Aenderung  OV 01.08.94
1993 |*
1994 *************************************************************************/
1995 
1996 /*
1997     4 Faelle :
1998 
1999     1. Datenzeiger steht mitten im Puffer (nBufActualPos >= 1)
2000     2. Datenzeiger auf Position 0, Puffer ist voll
2001     3. Datenzeiger auf Position 0, Puffer ist teilweise gefuellt
2002     4. Datenzeiger auf Position 0, Puffer ist leer -> Fehler!
2003 */
2004 
PutBack(char aCh)2005 SvStream& SvStream::PutBack( char aCh )
2006 {
2007     // wenn kein Buffer oder Zurueckscrollen nicht moeglich -> Fehler
2008     if( !pRWBuf || !nBufActualLen || ( !nBufActualPos && !nBufFilePos ) )
2009     {
2010         // 4. Fall
2011         SetError( SVSTREAM_GENERALERROR );
2012         return *this;
2013     }
2014 
2015     // Flush() (Phys. Flushen aber nicht notwendig, deshalb selbst schreiben)
2016     if( bIsConsistent && bIsDirty  )
2017     {
2018         SeekPos( nBufFilePos );
2019         if( nCryptMask )
2020             CryptAndWriteBuffer( pRWBuf, nBufActualLen );
2021         else
2022             PutData( pRWBuf, nBufActualLen );
2023         bIsDirty = sal_False;
2024     }
2025     bIsConsistent = sal_False;  // Puffer enthaelt jetzt TRASH
2026     if( nBufActualPos )
2027     {
2028         // 1. Fall
2029         nBufActualPos--;
2030         pBufPos--;
2031         *pBufPos = aCh;
2032         nBufFree++;
2033     }
2034     else  // Puffer muss verschoben werden
2035     {
2036         // Ist Puffer am Anschlag ?
2037         if( nBufSize == nBufActualLen )
2038         {
2039             // 2. Fall
2040             memmove( pRWBuf+1, pRWBuf, nBufSize-1 );
2041             // nBufFree behaelt den Wert!
2042         }
2043         else
2044         {
2045             // 3. Fall -> Puffer vergroessern
2046             memmove( pRWBuf+1, pRWBuf, (sal_uInt16)nBufActualLen );
2047             nBufActualLen++;
2048             nBufFree++;
2049         }
2050         nBufFilePos--;
2051         *pRWBuf = aCh;
2052     }
2053     eIOMode = STREAM_IO_DONTKNOW;
2054     bIsEof = sal_False;
2055     return *this;
2056 }
2057 
2058 /*************************************************************************
2059 |*
2060 |*    Stream::EatWhite()
2061 |*
2062 |*    Beschreibung      STREAM.SDW
2063 |*    Ersterstellung    OV 01.08.94
2064 |*    Letzte Aenderung  OV 01.08.94
2065 |*
2066 *************************************************************************/
2067 
EatWhite()2068 void SvStream::EatWhite()
2069 {
2070     char aCh;
2071     Read(&aCh, sizeof(char) );
2072     while( !bIsEof && isspace((int)aCh) )  //( aCh == ' ' || aCh == '\t' ) )
2073         Read(&aCh, sizeof(char) );
2074     if( !bIsEof ) // konnte das letzte Char gelesen werden ?
2075         SeekRel( -1L );
2076 }
2077 
2078 /*************************************************************************
2079 |*
2080 |*    Stream::RefreshBuffer()
2081 |*
2082 |*    Beschreibung      STREAM.SDW
2083 |*    Ersterstellung    OV 01.08.94
2084 |*    Letzte Aenderung  OV 01.08.94
2085 |*
2086 *************************************************************************/
2087 
RefreshBuffer()2088 void SvStream::RefreshBuffer()
2089 {
2090     if( bIsDirty && bIsConsistent )
2091     {
2092         SeekPos( nBufFilePos );
2093         if( nCryptMask )
2094             CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
2095         else
2096             PutData( pRWBuf, nBufActualLen );
2097         bIsDirty = sal_False;
2098     }
2099     SeekPos( nBufFilePos );
2100     nBufActualLen = (sal_uInt16)GetData( pRWBuf, nBufSize );
2101     if( nBufActualLen && nError == ERRCODE_IO_PENDING )
2102         nError = ERRCODE_NONE;
2103     if( nCryptMask )
2104         EncryptBuffer(pRWBuf, (sal_Size)nBufActualLen);
2105     bIsConsistent = sal_True;
2106     eIOMode = STREAM_IO_DONTKNOW;
2107 }
2108 
2109 
2110 /*************************************************************************
2111 |*
2112 |*    Stream::CreateFormatString()
2113 |*
2114 |*    Beschreibung      Baut Formatstring zusammen
2115 |*    Ersterstellung    OV 08.06.94
2116 |*    Letzte Aenderung  OV 08.06.94
2117 |*
2118 *************************************************************************/
2119 
CreateFormatString()2120 void SvStream::CreateFormatString()
2121 {
2122     aFormatString = '%';
2123     nPrintfParams = SPECIAL_PARAM_NONE;
2124 
2125     if( nJustification )
2126     {
2127         aFormatString += '-';
2128     }
2129 
2130     if( nWidth )
2131     {
2132         if( cFiller != ' ' )
2133             aFormatString += '0';
2134         aFormatString += '*';
2135         nPrintfParams = SPECIAL_PARAM_WIDTH;
2136     }
2137 
2138     if( nPrecision )
2139     {
2140         aFormatString += ".*";
2141         if( nWidth )
2142             nPrintfParams = SPECIAL_PARAM_BOTH;
2143         else
2144             nPrintfParams = SPECIAL_PARAM_PRECISION;
2145     }
2146 }
2147 
2148 /*************************************************************************
2149 |*
2150 |*    Stream::ReadNumber()
2151 |*
2152 |*    Beschreibung      STREAM.SDW
2153 |*    Ersterstellung    OV 08.06.94
2154 |*    Letzte Aenderung  OV 08.06.94
2155 |*
2156 *************************************************************************/
2157 
2158 #define BUFSIZE_LONG 21  // log( 2 hoch 64 ) + 1
2159 
ReadNumber(long & rLong)2160 SvStream& SvStream::ReadNumber( long& rLong )
2161 {
2162     EatWhite();
2163     if( bIsEof || nError )
2164     {
2165         SetError( SVSTREAM_GENERALERROR );
2166         return *this;
2167     }
2168     sal_Size nFPtr = Tell();
2169     char buf[ BUFSIZE_LONG ];
2170     memset( buf, 0, BUFSIZE_LONG );
2171     sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2172     if( !nTemp || nError )
2173     {
2174         SetError( SVSTREAM_GENERALERROR );
2175         return *this;
2176     }
2177     char *pEndPtr;
2178     rLong = strtol( buf, &pEndPtr, (int)nRadix );
2179     nFPtr += ( (sal_Size)pEndPtr - (sal_Size)(&(buf[0])) );
2180     Seek( nFPtr );
2181     bIsEof = sal_False;
2182     return *this;
2183 }
2184 
ReadNumber(sal_uInt32 & rUInt32)2185 SvStream& SvStream::ReadNumber( sal_uInt32& rUInt32 )
2186 {
2187     EatWhite();
2188     if( bIsEof || nError )
2189     {
2190         SetError( SVSTREAM_GENERALERROR );
2191         return *this;
2192     }
2193     sal_Size nFPtr = Tell();
2194     char buf[ BUFSIZE_LONG ];
2195     memset( buf, 0, BUFSIZE_LONG );
2196     sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2197     if( !nTemp || nError )
2198     {
2199         SetError( SVSTREAM_GENERALERROR );
2200         return *this;
2201     }
2202     char *pEndPtr;
2203     rUInt32 = strtoul( buf, &pEndPtr, (int)nRadix );
2204     nFPtr += ( (sal_uIntPtr)pEndPtr - (sal_uIntPtr)buf );
2205     Seek( nFPtr );
2206     bIsEof = sal_False;
2207     return *this;
2208 }
2209 
ReadNumber(double & rDouble)2210 SvStream& SvStream::ReadNumber( double& rDouble )
2211 {
2212     EatWhite();
2213     if( bIsEof || nError )
2214     {
2215         SetError( SVSTREAM_GENERALERROR );
2216         return *this;
2217     }
2218     sal_Size nFPtr = Tell();
2219     char buf[ BUFSIZE_LONG ];
2220     memset( buf, 0, BUFSIZE_LONG );
2221     sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2222     if( !nTemp || nError )
2223     {
2224         SetError( SVSTREAM_GENERALERROR );
2225         return *this;
2226     }
2227     char *pEndPtr;
2228     rDouble = strtod( buf, &pEndPtr );
2229     nFPtr += ( (sal_Size)pEndPtr - (sal_Size)buf );
2230     Seek( nFPtr );
2231     bIsEof = sal_False;
2232     return *this;
2233 }
2234 
2235 
2236 /*************************************************************************
2237 |*
2238 |*    Stream::WriteNumber()
2239 |*
2240 |*    Beschreibung      STREAM.SDW
2241 |*    Ersterstellung    OV 08.06.94
2242 |*    Letzte Aenderung  OV 08.06.94
2243 |*
2244 *************************************************************************/
2245 
WriteNumber(long nLong)2246 SvStream& SvStream::WriteNumber( long nLong )
2247 {
2248     char buffer[256+12];
2249     char pType[] = "ld"; // Nicht static!
2250     if( nRadix == 16 )
2251         pType[1] = 'x';
2252     else if( nRadix == 8 )
2253         pType[1] = 'o';
2254     ByteString aFStr( aFormatString);
2255     aFStr += pType;
2256     int nLen;
2257     switch ( nPrintfParams )
2258     {
2259         case SPECIAL_PARAM_NONE :
2260             nLen = sprintf( buffer, aFStr.GetBuffer(), nLong );
2261             break;
2262         case SPECIAL_PARAM_WIDTH :
2263             nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, nLong );
2264             break;
2265         case SPECIAL_PARAM_PRECISION :
2266             nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision,nLong);
2267             break;
2268         default:
2269             nLen=sprintf(buffer, aFStr.GetBuffer(),nWidth,nPrecision,nLong);
2270     }
2271     Write( buffer, (long)nLen );
2272     return *this;
2273 }
2274 
WriteNumber(sal_uInt32 nUInt32)2275 SvStream& SvStream::WriteNumber( sal_uInt32 nUInt32 )
2276 {
2277     char buffer[256+12];
2278     char pType[] = "lu"; // Nicht static!
2279     if( nRadix == 16 )
2280         pType[1] = 'x';
2281     else if( nRadix == 8 )
2282         pType[1] = 'o';
2283     ByteString aFStr( aFormatString);
2284     aFStr += pType;
2285     int nLen;
2286     switch ( nPrintfParams )
2287     {
2288         case SPECIAL_PARAM_NONE :
2289             nLen = sprintf( buffer, aFStr.GetBuffer(), nUInt32 );
2290             break;
2291         case SPECIAL_PARAM_WIDTH :
2292             nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, nUInt32 );
2293             break;
2294         case SPECIAL_PARAM_PRECISION :
2295             nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision, nUInt32 );
2296             break;
2297         default:
2298             nLen=sprintf(buffer,aFStr.GetBuffer(),nWidth,nPrecision,nUInt32 );
2299     }
2300     Write( buffer, (long)nLen );
2301     return *this;
2302 }
2303 
2304 
WriteNumber(const double & rDouble)2305 SvStream& SvStream::WriteNumber( const double& rDouble )
2306 {
2307     char buffer[256+24];
2308     ByteString aFStr( aFormatString);
2309     aFStr += "lf";
2310     int nLen;
2311     switch ( nPrintfParams )
2312     {
2313         case SPECIAL_PARAM_NONE :
2314             nLen = sprintf( buffer, aFStr.GetBuffer(), rDouble );
2315             break;
2316         case SPECIAL_PARAM_WIDTH :
2317             nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, rDouble );
2318             break;
2319         case SPECIAL_PARAM_PRECISION :
2320             nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision, rDouble);
2321             break;
2322         default:
2323             nLen=sprintf(buffer, aFStr.GetBuffer(),nWidth,nPrecision,rDouble);
2324     }
2325     Write( buffer, (long)nLen );
2326     return *this;
2327 }
2328 
2329 /*************************************************************************
2330 |*
2331 |*    Stream::CryptAndWriteBuffer()
2332 |*
2333 |*    Beschreibung      Verschluesseln und Schreiben
2334 |*    Ersterstellung    OV 08.06.94
2335 |*    Letzte Aenderung  OV 08.06.94
2336 |*
2337 *************************************************************************/
2338 
2339 #define CRYPT_BUFSIZE 1024
2340 
CryptAndWriteBuffer(const void * pStart,sal_Size nLen)2341 sal_Size SvStream::CryptAndWriteBuffer( const void* pStart, sal_Size nLen)
2342 {
2343     unsigned char  pTemp[CRYPT_BUFSIZE];
2344     unsigned char* pDataPtr = (unsigned char*)pStart;
2345     sal_Size nCount = 0;
2346     sal_Size nBufCount;
2347     unsigned char nMask = nCryptMask;
2348     do
2349     {
2350         if( nLen >= CRYPT_BUFSIZE )
2351             nBufCount = CRYPT_BUFSIZE;
2352         else
2353             nBufCount = nLen;
2354         nLen -= nBufCount;
2355         memcpy( pTemp, pDataPtr, (sal_uInt16)nBufCount );
2356         // **** Verschluesseln *****
2357         for ( sal_uInt16 n=0; n < CRYPT_BUFSIZE; n++ )
2358         {
2359             unsigned char aCh = pTemp[n];
2360             aCh ^= nMask;
2361             SWAPNIBBLES(aCh)
2362             pTemp[n] = aCh;
2363         }
2364         // *************************
2365         nCount += PutData( (char*)pTemp, nBufCount );
2366         pDataPtr += nBufCount;
2367     }
2368     while ( nLen );
2369     return nCount;
2370 }
2371 
2372 /*************************************************************************
2373 |*
2374 |*    Stream::EncryptBuffer()
2375 |*
2376 |*    Beschreibung      Buffer entschluesseln
2377 |*    Ersterstellung    OV 08.06.94
2378 |*    Letzte Aenderung  OV 08.06.94
2379 |*
2380 *************************************************************************/
2381 
EncryptBuffer(void * pStart,sal_Size nLen)2382 sal_Bool SvStream::EncryptBuffer(void* pStart, sal_Size nLen)
2383 {
2384     unsigned char* pTemp = (unsigned char*)pStart;
2385     unsigned char nMask = nCryptMask;
2386 
2387     for ( sal_Size n=0; n < nLen; n++, pTemp++ )
2388     {
2389         unsigned char aCh = *pTemp;
2390         SWAPNIBBLES(aCh)
2391         aCh ^= nMask;
2392         *pTemp = aCh;
2393     }
2394     return sal_True;
2395 }
2396 
2397 /*************************************************************************
2398 |*
2399 |*    Stream::SetKey()
2400 |*
2401 |*    Beschreibung      STREAM.SDW
2402 |*    Ersterstellung    OV 08.06.94
2403 |*    Letzte Aenderung  OV 08.06.94
2404 |*
2405 *************************************************************************/
2406 
implGetCryptMask(const sal_Char * pStr,sal_Int32 nLen,long nVersion)2407 unsigned char implGetCryptMask(const sal_Char* pStr, sal_Int32 nLen, long nVersion)
2408 {
2409     unsigned char nCryptMask = 0;
2410 
2411     if (!nLen)
2412         return nCryptMask;
2413 
2414     if( nVersion <= SOFFICE_FILEFORMAT_31 )
2415     {
2416         while( nLen )
2417         {
2418             nCryptMask ^= *pStr;
2419             pStr++;
2420             nLen--;
2421         }
2422     }
2423     else // BugFix #25888#
2424     {
2425         for( sal_uInt16 i = 0; i < nLen; i++ ) {
2426             nCryptMask ^= pStr[i];
2427             if( nCryptMask & 0x80 ) {
2428                 nCryptMask <<= 1;
2429                 nCryptMask++;
2430             }
2431             else
2432                 nCryptMask <<= 1;
2433         }
2434     }
2435 
2436     if( !nCryptMask )
2437         nCryptMask = 67;
2438 
2439     return nCryptMask;
2440 }
2441 
SetKey(const ByteString & rKey)2442 void SvStream::SetKey( const ByteString& rKey )
2443 {
2444     aKey = rKey;
2445     nCryptMask = implGetCryptMask( aKey.GetBuffer(), aKey.Len(), GetVersion() );
2446 }
2447 
2448 /*************************************************************************
2449 |*
2450 |*    Stream::SyncSvStream()
2451 |*
2452 |*    Beschreibung      STREAM.SDW
2453 |*    Ersterstellung    OV 08.06.94
2454 |*    Letzte Aenderung  OV 08.06.94
2455 |*
2456 *************************************************************************/
2457 
SyncSvStream(sal_Size nNewStreamPos)2458 void SvStream::SyncSvStream( sal_Size nNewStreamPos )
2459 {
2460     ClearBuffer();
2461     SvStream::nBufFilePos = nNewStreamPos;
2462 }
2463 
2464 /*************************************************************************
2465 |*
2466 |*    Stream::SyncSysStream()
2467 |*
2468 |*    Beschreibung      STREAM.SDW
2469 |*    Ersterstellung    OV 08.06.94
2470 |*    Letzte Aenderung  OV 08.06.94
2471 |*
2472 *************************************************************************/
2473 
SyncSysStream()2474 void SvStream::SyncSysStream()
2475 {
2476     Flush();
2477     SeekPos( Tell() );
2478 }
2479 
2480 /*************************************************************************
2481 |*
2482 |*    Stream::SetStreamSize()
2483 |*
2484 |*    Beschreibung      STREAM.SDW
2485 |*    Ersterstellung    OV 08.06.94
2486 |*    Letzte Aenderung  OV 08.06.94
2487 |*
2488 *************************************************************************/
2489 
SetStreamSize(sal_Size nSize)2490 sal_Bool SvStream::SetStreamSize( sal_Size nSize )
2491 {
2492 #ifdef DBG_UTIL
2493     sal_Size nFPos = Tell();
2494 #endif
2495     sal_uInt16 nBuf = nBufSize;
2496     SetBufferSize( 0 );
2497     SetSize( nSize );
2498     SetBufferSize( nBuf );
2499     DBG_ASSERT(Tell()==nFPos,"SetStreamSize failed");
2500     return (sal_Bool)(nError == 0);
2501 }
2502 
2503 //============================================================================
2504 
AddMark(sal_Size)2505 void SvStream::AddMark( sal_Size )
2506 {
2507 }
2508 
2509 //============================================================================
2510 
RemoveMark(sal_Size)2511 void SvStream::RemoveMark( sal_Size )
2512 {
2513 }
2514 
2515 /*************************************************************************
2516 |*
2517 |*    endl()
2518 |*
2519 |*    Beschreibung      STREAM.SDW
2520 |*    Ersterstellung    OV 08.06.94
2521 |*    Letzte Aenderung  TH 13.11.96
2522 |*
2523 *************************************************************************/
2524 
endl(SvStream & rStr)2525 SvStream& endl( SvStream& rStr )
2526 {
2527     LineEnd eDelim = rStr.GetLineDelimiter();
2528     if ( eDelim == LINEEND_CR )
2529         rStr << _CR;
2530     else if( eDelim == LINEEND_LF )
2531         rStr << _LF;
2532     else
2533         rStr << _CR << _LF;
2534     return rStr;
2535 }
2536 
endlu(SvStream & rStrm)2537 SvStream& endlu( SvStream& rStrm )
2538 {
2539     switch ( rStrm.GetLineDelimiter() )
2540     {
2541         case LINEEND_CR :
2542             rStrm << sal_Unicode(_CR);
2543         break;
2544         case LINEEND_LF :
2545             rStrm << sal_Unicode(_LF);
2546         break;
2547         default:
2548             rStrm << sal_Unicode(_CR) << sal_Unicode(_LF);
2549     }
2550     return rStrm;
2551 }
2552 
endlub(SvStream & rStrm)2553 SvStream& endlub( SvStream& rStrm )
2554 {
2555     if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
2556         return endlu( rStrm );
2557     else
2558         return endl( rStrm );
2559 }
2560 
2561 /*************************************************************************
2562 |*
2563 |*    SvMemoryStream::SvMemoryStream()
2564 |*
2565 |*    Beschreibung      STREAM.SDW
2566 |*    Ersterstellung    OV 20.06.94
2567 |*    Letzte Aenderung  OV 20.06.94
2568 |*
2569 *************************************************************************/
2570 
SvMemoryStream(void * pBuffer,sal_Size bufSize,StreamMode eMode)2571 SvMemoryStream::SvMemoryStream( void* pBuffer, sal_Size bufSize,
2572                                 StreamMode eMode )
2573 {
2574     if( eMode & STREAM_WRITE )
2575         bIsWritable = sal_True;
2576     else
2577         bIsWritable = sal_False;
2578     nEndOfData  = bufSize;
2579     bOwnsData   = sal_False;
2580     pBuf        = (sal_uInt8 *) pBuffer;
2581     nResize     = 0L;
2582     nSize       = bufSize;
2583     nPos        = 0L;
2584     SetBufferSize( 0 );
2585 }
2586 
2587 /*************************************************************************
2588 |*
2589 |*    SvMemoryStream::SvMemoryStream()
2590 |*
2591 |*    Beschreibung      STREAM.SDW
2592 |*    Ersterstellung    OV 20.06.94
2593 |*    Letzte Aenderung  OV 20.06.94
2594 |*
2595 *************************************************************************/
2596 
SvMemoryStream(sal_Size nInitSize,sal_Size nResizeOffset)2597 SvMemoryStream::SvMemoryStream( sal_Size nInitSize, sal_Size nResizeOffset )
2598 {
2599     bIsWritable = sal_True;
2600     bOwnsData   = sal_True;
2601     nEndOfData  = 0L;
2602     nResize     = nResizeOffset;
2603     nPos        = 0;
2604     pBuf        = 0;
2605     if( nResize != 0 && nResize < 16 )
2606         nResize = 16;
2607     if( nInitSize && !AllocateMemory( nInitSize ) )
2608     {
2609         SetError( SVSTREAM_OUTOFMEMORY );
2610         nSize = 0;
2611     }
2612     else
2613         nSize = nInitSize;
2614     SetBufferSize( 64 );
2615 }
2616 
2617 /*************************************************************************
2618 |*
2619 |*    SvMemoryStream::~SvMemoryStream()
2620 |*
2621 |*    Beschreibung      STREAM.SDW
2622 |*    Ersterstellung    OV 20.06.94
2623 |*    Letzte Aenderung  OV 20.06.94
2624 |*
2625 *************************************************************************/
2626 
~SvMemoryStream()2627 SvMemoryStream::~SvMemoryStream()
2628 {
2629     if( pBuf )
2630     {
2631         if( bOwnsData )
2632             FreeMemory();
2633         else
2634             Flush();
2635     }
2636 }
2637 
2638 /*************************************************************************
2639 |*
2640 |*    SvMemoryStream::IsA()
2641 |*
2642 |*    Beschreibung      STREAM.SDW
2643 |*    Ersterstellung    OV 20.06.94
2644 |*    Letzte Aenderung  OV 20.06.94
2645 |*
2646 *************************************************************************/
2647 
IsA() const2648 sal_uInt16 SvMemoryStream::IsA() const
2649 {
2650     return (sal_uInt16)ID_MEMORYSTREAM;
2651 }
2652 
2653 /*************************************************************************
2654 |*
2655 |*    SvMemoryStream::SetBuffer()
2656 |*
2657 |*    Beschreibung      STREAM.SDW
2658 |*    Ersterstellung    OV 20.06.94
2659 |*    Letzte Aenderung  OV 20.06.94
2660 |*
2661 *************************************************************************/
2662 
SetBuffer(void * pNewBuf,sal_Size nCount,sal_Bool bOwnsDat,sal_Size nEOF)2663 void* SvMemoryStream::SetBuffer( void* pNewBuf, sal_Size nCount,
2664                                  sal_Bool bOwnsDat, sal_Size nEOF )
2665 {
2666     void* pResult;
2667     SetBufferSize( 0 ); // Buffering in der Basisklasse initialisieren
2668     Seek( 0 );
2669     if( bOwnsData )
2670     {
2671         pResult = 0;
2672         if( pNewBuf != pBuf )
2673             FreeMemory();
2674     }
2675     else
2676         pResult = pBuf;
2677 
2678     pBuf        = (sal_uInt8 *) pNewBuf;
2679     nPos        = 0;
2680     nSize       = nCount;
2681     nResize     = 0;
2682     bOwnsData   = bOwnsDat;
2683 
2684     if( nEOF > nCount )
2685         nEOF = nCount;
2686     nEndOfData = nEOF;
2687 
2688     ResetError();
2689 
2690     DBG_ASSERT( nEndOfData<STREAM_SEEK_TO_END,"Invalid EOF");
2691     return pResult;
2692 }
2693 
2694 /*************************************************************************
2695 |*
2696 |*    SvMemoryStream::GetData()
2697 |*
2698 |*    Beschreibung      STREAM.SDW
2699 |*    Ersterstellung    OV 20.06.94
2700 |*    Letzte Aenderung  OV 20.06.94
2701 |*
2702 *************************************************************************/
2703 
GetData(void * pData,sal_Size nCount)2704 sal_Size SvMemoryStream::GetData( void* pData, sal_Size nCount )
2705 {
2706     sal_Size nMaxCount = nEndOfData-nPos;
2707     if( nCount > nMaxCount )
2708         nCount = nMaxCount;
2709     memcpy( pData, pBuf+nPos, (size_t)nCount );
2710     nPos += nCount;
2711     return nCount;
2712 }
2713 
2714 /*************************************************************************
2715 |*
2716 |*    SvMemoryStream::PutData()
2717 |*
2718 |*    Beschreibung      STREAM.SDW
2719 |*    Ersterstellung    OV 20.06.94
2720 |*    Letzte Aenderung  OV 20.06.94
2721 |*
2722 *************************************************************************/
2723 
PutData(const void * pData,sal_Size nCount)2724 sal_Size SvMemoryStream::PutData( const void* pData, sal_Size nCount )
2725 {
2726     if( GetError() )
2727         return 0L;
2728 
2729     sal_Size nMaxCount = nSize-nPos;
2730 
2731     // auf Ueberlauf testen
2732     if( nCount > nMaxCount )
2733     {
2734         if( nResize == 0 )
2735         {
2736             // soviel wie moeglich rueberschaufeln
2737             nCount = nMaxCount;
2738             SetError( SVSTREAM_OUTOFMEMORY );
2739         }
2740         else
2741         {
2742             long nNewResize;
2743             if( nSize && nSize > nResize )
2744                 nNewResize = nSize;
2745             else
2746                 nNewResize = nResize;
2747 
2748             if( (nCount-nMaxCount) < nResize )
2749             {
2750                 // fehlender Speicher ist kleiner als Resize-Offset,
2751                 // deshalb um Resize-Offset vergroessern
2752                 if( !ReAllocateMemory( nNewResize) )
2753                 {
2754                     nCount = 0;
2755                     SetError( SVSTREAM_WRITE_ERROR );
2756                 }
2757             }
2758             else
2759             {
2760                 // fehlender Speicher ist groesser als Resize-Offset
2761                 // deshalb um Differenz+ResizeOffset vergroessern
2762                 if( !ReAllocateMemory( nCount-nMaxCount+nNewResize ) )
2763                 {
2764                     nCount = 0;
2765                     SetError( SVSTREAM_WRITE_ERROR );
2766                 }
2767             }
2768         }
2769     }
2770     DBG_ASSERT(pBuf,"Possibly Reallocate failed");
2771     memcpy( pBuf+nPos, pData, (size_t)nCount);
2772 
2773     nPos += nCount;
2774     if( nPos > nEndOfData )
2775         nEndOfData = nPos;
2776     return nCount;
2777 }
2778 
2779 /*************************************************************************
2780 |*
2781 |*    SvMemoryStream::SeekPos()
2782 |*
2783 |*    Beschreibung      STREAM.SDW
2784 |*    Ersterstellung    OV 20.06.94
2785 |*    Letzte Aenderung  OV 20.06.94
2786 |*
2787 *************************************************************************/
2788 
2789 // nEndOfData: Erste Position im Stream, die nicht gelesen werden darf
2790 // nSize: Groesse des allozierten Speichers
2791 
SeekPos(sal_Size nNewPos)2792 sal_Size SvMemoryStream::SeekPos( sal_Size nNewPos )
2793 {
2794     if( nNewPos < nEndOfData )
2795         nPos = nNewPos;
2796     else if( nNewPos == STREAM_SEEK_TO_END )
2797         nPos = nEndOfData;
2798     else
2799     {
2800         if( nNewPos >= nSize ) // muss Buffer vergroessert werden ?
2801         {
2802             if( nResize )  // ist vergroeseern erlaubt ?
2803             {
2804                 long nDiff = (long)(nNewPos - nSize + 1);
2805                 nDiff += (long)nResize;
2806                 ReAllocateMemory( nDiff );
2807                 nPos = nNewPos;
2808                 nEndOfData = nNewPos;
2809             }
2810             else  // vergroessern ist nicht erlaubt -> ans Ende setzen
2811             {
2812                 // SetError( SVSTREAM_OUTOFMEMORY );
2813                 nPos = nEndOfData;
2814             }
2815         }
2816         else  // gueltigen Bereich innerhalb des Buffers vergroessern
2817         {
2818             nPos = nNewPos;
2819             nEndOfData = nNewPos;
2820         }
2821     }
2822     return nPos;
2823 }
2824 
2825 /*************************************************************************
2826 |*
2827 |*    SvMemoryStream::FlushData()
2828 |*
2829 |*    Beschreibung      STREAM.SDW
2830 |*    Ersterstellung    OV 20.06.94
2831 |*    Letzte Aenderung  OV 20.06.94
2832 |*
2833 *************************************************************************/
2834 
FlushData()2835 void SvMemoryStream::FlushData()
2836 {
2837 }
2838 
2839 /*************************************************************************
2840 |*
2841 |*    SvMemoryStream::ResetError()
2842 |*
2843 |*    Beschreibung      STREAM.SDW
2844 |*    Ersterstellung    OV 20.06.94
2845 |*    Letzte Aenderung  OV 20.06.94
2846 |*
2847 *************************************************************************/
2848 
ResetError()2849 void SvMemoryStream::ResetError()
2850 {
2851     SvStream::ClearError();
2852 }
2853 
2854 /*************************************************************************
2855 |*
2856 |*    SvMemoryStream::AllocateMemory()
2857 |*
2858 |*    Beschreibung      STREAM.SDW
2859 |*    Ersterstellung    OV 20.06.94
2860 |*    Letzte Aenderung  OV 20.06.94
2861 |*
2862 *************************************************************************/
2863 
AllocateMemory(sal_Size nNewSize)2864 sal_Bool SvMemoryStream::AllocateMemory( sal_Size nNewSize )
2865 {
2866     pBuf = new sal_uInt8[nNewSize];
2867     return( pBuf != 0 );
2868 }
2869 
2870 /*************************************************************************
2871 |*
2872 |*    SvMemoryStream::ReAllocateMemory()   (Bozo-Algorithmus)
2873 |*
2874 |*    Beschreibung      STREAM.SDW
2875 |*    Ersterstellung    OV 20.06.94
2876 |*    Letzte Aenderung  OV 20.06.94
2877 |*
2878 *************************************************************************/
2879 
ReAllocateMemory(long nDiff)2880 sal_Bool SvMemoryStream::ReAllocateMemory( long nDiff )
2881 {
2882     sal_Bool bRetVal    = sal_False;
2883     long nTemp      = (long)nSize;
2884     nTemp           += nDiff;
2885     sal_Size nNewSize  = (sal_Size)nTemp;
2886 
2887     if( nNewSize )
2888     {
2889         sal_uInt8* pNewBuf   = new sal_uInt8[nNewSize];
2890 
2891         if( pNewBuf )
2892         {
2893             bRetVal = sal_True; // Success!
2894             if( nNewSize < nSize )      // Verkleinern ?
2895             {
2896                 memcpy( pNewBuf, pBuf, (size_t)nNewSize );
2897                 if( nPos > nNewSize )
2898                     nPos = 0L;
2899                 if( nEndOfData >= nNewSize )
2900                     nEndOfData = nNewSize-1L;
2901             }
2902             else
2903             {
2904                 memcpy( pNewBuf, pBuf, (size_t)nSize );
2905             }
2906 
2907             FreeMemory();
2908 
2909             pBuf  = pNewBuf;
2910             nSize = nNewSize;
2911         }
2912     }
2913     else
2914     {
2915         bRetVal = sal_True;
2916         FreeMemory();
2917         pBuf = 0;
2918         nSize = 0;
2919         nEndOfData = 0;
2920         nPos = 0;
2921     }
2922 
2923     return bRetVal;
2924 }
2925 
FreeMemory()2926 void SvMemoryStream::FreeMemory()
2927 {
2928     delete[] pBuf;
2929 }
2930 
2931 /*************************************************************************
2932 |*
2933 |*    SvMemoryStream::SwitchBuffer()
2934 |*
2935 |*    Beschreibung      STREAM.SDW
2936 |*    Ersterstellung    OV 26.07.94
2937 |*    Letzte Aenderung  OV 26.07.94
2938 |*
2939 *************************************************************************/
2940 
SwitchBuffer(sal_Size nInitSize,sal_Size nResizeOffset)2941 void* SvMemoryStream::SwitchBuffer( sal_Size nInitSize, sal_Size nResizeOffset)
2942 {
2943     Flush();
2944     if( !bOwnsData )
2945         return 0;
2946     Seek( STREAM_SEEK_TO_BEGIN );
2947 
2948     void* pRetVal = pBuf;
2949     pBuf          = 0;
2950     nEndOfData    = 0L;
2951     nResize       = nResizeOffset;
2952     nPos          = 0;
2953 
2954     if( nResize != 0 && nResize < 16 )
2955         nResize = 16;
2956 
2957     ResetError();
2958 
2959     if( nInitSize && !AllocateMemory(nInitSize) )
2960     {
2961         SetError( SVSTREAM_OUTOFMEMORY );
2962         nSize = 0;
2963     }
2964     else
2965         nSize = nInitSize;
2966 
2967     SetBufferSize( 64 );
2968     return pRetVal;
2969 }
2970 
SetSize(sal_Size nNewSize)2971 void SvMemoryStream::SetSize( sal_Size nNewSize )
2972 {
2973     long nDiff = (long)nNewSize - (long)nSize;
2974     ReAllocateMemory( nDiff );
2975 }
2976 
TYPEINIT0(SvDataCopyStream)2977 TYPEINIT0 ( SvDataCopyStream )
2978 
2979 void SvDataCopyStream::Assign( const SvDataCopyStream& )
2980 {
2981 }
2982