lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / gallery2 / codec.cxx
blobca4a14e57d7b75b5b72948bcdf72ccc0ca1d0cd6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/stream.hxx>
22 #include <tools/zcodec.hxx>
23 #include "codec.hxx"
24 #include <boost/scoped_array.hpp>
26 // - GalleryCodec -
29 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
30 rStm( rIOStm )
34 GalleryCodec::~GalleryCodec()
38 bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
40 const sal_uIntPtr nPos = rStm.Tell();
41 bool bRet;
42 sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
44 rStm.ReadUChar( cByte1 ).ReadUChar( cByte2 ).ReadUChar( cByte3 ).ReadUChar( cByte4 ).ReadUChar( cByte5 ).ReadUChar( cByte6 );
46 if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
48 rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
49 bRet = true;
51 else
53 rVersion = 0;
54 bRet = false;
57 rStm.Seek( nPos );
59 return bRet;
62 void GalleryCodec::Write( SvStream& rStmToWrite )
64 sal_uInt32 nPos, nCompSize;
66 rStmToWrite.Seek( STREAM_SEEK_TO_END );
67 const sal_uInt32 nSize = rStmToWrite.Tell();
68 rStmToWrite.Seek( 0UL );
70 rStm.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
71 rStm.WriteUInt32( nSize );
73 nPos = rStm.Tell();
74 rStm.SeekRel( 4UL );
76 ZCodec aCodec;
77 aCodec.BeginCompression();
78 aCodec.Compress( rStmToWrite, rStm );
79 aCodec.EndCompression();
81 nCompSize = rStm.Tell() - nPos - 4UL;
82 rStm.Seek( nPos );
83 rStm.WriteUInt32( nCompSize );
84 rStm.Seek( STREAM_SEEK_TO_END );
87 void GalleryCodec::Read( SvStream& rStmToRead )
89 sal_uInt32 nVersion = 0;
91 if( IsCoded( rStm, nVersion ) )
93 sal_uInt32 nCompressedSize, nUnCompressedSize;
95 rStm.SeekRel( 6 );
96 rStm.ReadUInt32( nUnCompressedSize ).ReadUInt32( nCompressedSize );
98 // decompress
99 if( 1 == nVersion )
101 boost::scoped_array<sal_uInt8> pCompressedBuffer(new sal_uInt8[ nCompressedSize ]);
102 rStm.Read( pCompressedBuffer.get(), nCompressedSize );
103 sal_uInt8* pInBuf = pCompressedBuffer.get();
104 boost::scoped_array<sal_uInt8> pOutBuf(new sal_uInt8[ nUnCompressedSize ]);
105 sal_uInt8* pTmpBuf = pOutBuf.get();
106 sal_uInt8* pLast = pOutBuf.get() + nUnCompressedSize - 1;
107 sal_uIntPtr nIndex = 0UL, nCountByte, nRunByte;
108 bool bEndDecoding = false;
112 nCountByte = *pInBuf++;
114 if ( !nCountByte )
116 nRunByte = *pInBuf++;
118 if ( nRunByte > 2 )
120 // filling absolutely
121 memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
122 pInBuf += nRunByte;
123 nIndex += nRunByte;
125 // note WORD alignment
126 if ( nRunByte & 1 )
127 pInBuf++;
129 else if ( nRunByte == 1 ) // End of the image
130 bEndDecoding = true;
132 else
134 const sal_uInt8 cVal = *pInBuf++;
136 memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
137 nIndex += nCountByte;
140 while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
142 rStmToRead.Write( pOutBuf.get(), nUnCompressedSize );
144 else if( 2 == nVersion )
146 ZCodec aCodec;
148 aCodec.BeginCompression();
149 aCodec.Decompress( rStm, rStmToRead );
150 aCodec.EndCompression();
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */