Best effort to create directories of SAL_LOG_FILE
[LibreOffice.git] / svx / source / gallery2 / codec.cxx
blob062c60dbecc6eb7c09bf5a008f22c0837fb734e9
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 <memory>
27 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
28 rStm( rIOStm )
32 bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
34 const sal_uInt64 nPos = rStm.Tell();
35 bool bRet;
36 sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
38 rStm.ReadUChar( cByte1 ).ReadUChar( cByte2 ).ReadUChar( cByte3 ).ReadUChar( cByte4 ).ReadUChar( cByte5 ).ReadUChar( cByte6 );
40 if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
42 rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
43 bRet = true;
45 else
47 rVersion = 0;
48 bRet = false;
51 rStm.Seek( nPos );
53 return bRet;
56 void GalleryCodec::Write( SvStream& rStmToWrite )
58 sal_uInt32 nPos, nCompSize;
60 const sal_uInt32 nSize = rStmToWrite.TellEnd();
61 rStmToWrite.Seek( 0 );
63 rStm.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
64 rStm.WriteUInt32( nSize );
66 nPos = rStm.Tell();
67 rStm.SeekRel( 4 );
69 ZCodec aCodec;
70 aCodec.BeginCompression();
71 aCodec.Compress( rStmToWrite, rStm );
72 aCodec.EndCompression();
74 nCompSize = rStm.Tell() - nPos - 4;
75 rStm.Seek( nPos );
76 rStm.WriteUInt32( nCompSize );
77 rStm.Seek( STREAM_SEEK_TO_END );
80 void GalleryCodec::Read( SvStream& rStmToRead )
82 sal_uInt32 nVersion = 0;
84 if( !IsCoded( rStm, nVersion ) )
85 return;
87 sal_uInt32 nCompressedSize, nUnCompressedSize;
89 rStm.SeekRel( 6 );
90 rStm.ReadUInt32( nUnCompressedSize ).ReadUInt32( nCompressedSize );
92 // decompress
93 if( 1 == nVersion )
95 std::unique_ptr<sal_uInt8[]> pCompressedBuffer(new sal_uInt8[ nCompressedSize ]);
96 rStm.ReadBytes(pCompressedBuffer.get(), nCompressedSize);
97 sal_uInt8* pInBuf = pCompressedBuffer.get();
98 std::unique_ptr<sal_uInt8[]> pOutBuf(new sal_uInt8[ nUnCompressedSize ]);
99 sal_uInt8* pTmpBuf = pOutBuf.get();
100 sal_uInt8* pLast = pOutBuf.get() + nUnCompressedSize - 1;
101 sal_uIntPtr nIndex = 0, nCountByte, nRunByte;
102 bool bEndDecoding = false;
106 nCountByte = *pInBuf++;
108 if ( !nCountByte )
110 nRunByte = *pInBuf++;
112 if ( nRunByte > 2 )
114 // filling absolutely
115 memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
116 pInBuf += nRunByte;
117 nIndex += nRunByte;
119 // note WORD alignment
120 if ( nRunByte & 1 )
121 pInBuf++;
123 else if ( nRunByte == 1 ) // End of the image
124 bEndDecoding = true;
126 else
128 const sal_uInt8 cVal = *pInBuf++;
130 memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
131 nIndex += nCountByte;
134 while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
136 rStmToRead.WriteBytes(pOutBuf.get(), nUnCompressedSize);
138 else if( 2 == nVersion )
140 ZCodec aCodec;
142 aCodec.BeginCompression();
143 aCodec.Decompress( rStm, rStmToRead );
144 aCodec.EndCompression();
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */