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