pdf: simplify EncryptionHashTransporter
[LibreOffice.git] / svx / source / gallery2 / codec.cxx
blob76cf35c74023b0ce22a2a464e28f1497a0bc607e
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 <sal/log.hxx>
22 #include <tools/stream.hxx>
23 #include <tools/zcodec.hxx>
24 #include "codec.hxx"
25 #include <memory>
28 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
29 rStm( rIOStm )
33 bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
35 const sal_uInt64 nPos = rStm.Tell();
36 bool bRet;
37 sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
39 rStm.ReadUChar( cByte1 ).ReadUChar( cByte2 ).ReadUChar( cByte3 ).ReadUChar( cByte4 ).ReadUChar( cByte5 ).ReadUChar( cByte6 );
41 if (rStm.good() && cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
43 rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
44 bRet = true;
46 else
48 rVersion = 0;
49 bRet = false;
52 rStm.Seek( nPos );
54 return bRet;
57 void GalleryCodec::Write( SvStream& rStmToWrite )
59 sal_uInt32 nPos, nCompSize;
61 const sal_uInt32 nSize = rStmToWrite.TellEnd();
62 rStmToWrite.Seek( 0 );
64 rStm.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
65 rStm.WriteUInt32( nSize );
67 nPos = rStm.Tell();
68 rStm.SeekRel( 4 );
70 ZCodec aCodec;
71 aCodec.BeginCompression();
72 aCodec.Compress( rStmToWrite, rStm );
73 aCodec.EndCompression();
75 nCompSize = rStm.Tell() - nPos - 4;
76 rStm.Seek( nPos );
77 rStm.WriteUInt32( nCompSize );
78 rStm.Seek( STREAM_SEEK_TO_END );
81 void GalleryCodec::Read( SvStream& rStmToRead )
83 sal_uInt32 nVersion = 0;
85 if( !IsCoded( rStm, nVersion ) )
86 return;
88 rStm.SeekRel( 14 );
90 // decompress
91 if( 1 == nVersion )
93 SAL_WARN("svx", "staroffice binary file formats are no longer supported inside the gallery!");
95 else if( 2 == nVersion )
97 ZCodec aCodec;
99 aCodec.BeginCompression();
100 aCodec.Decompress( rStm, rStmToRead );
101 aCodec.EndCompression();
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */