char array should be null terminated
[LibreOffice.git] / svx / source / gallery2 / codec.cxx
blobc8ffb25446facd069ab099d131c85c97d7513eab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
32 #include <tools/stream.hxx>
33 #include <tools/zcodec.hxx>
34 #include "codec.hxx"
36 // ----------------
37 // - GalleryCodec -
38 // ----------------
40 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
41 rStm( rIOStm )
45 // -----------------------------------------------------------------------------
47 GalleryCodec::~GalleryCodec()
51 // -----------------------------------------------------------------------------
53 sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
55 const sal_uIntPtr nPos = rStm.Tell();
56 sal_Bool bRet;
57 sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
59 rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
61 if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
63 rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
64 bRet = sal_True;
66 else
68 rVersion = 0;
69 bRet = sal_False;
72 rStm.Seek( nPos );
74 return bRet;
77 // -----------------------------------------------------------------------------
79 void GalleryCodec::Write( SvStream& rStmToWrite )
81 sal_uInt32 nPos, nCompSize;
83 rStmToWrite.Seek( STREAM_SEEK_TO_END );
84 const sal_uInt32 nSize = rStmToWrite.Tell();
85 rStmToWrite.Seek( 0UL );
87 rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
88 rStm << nSize;
90 nPos = rStm.Tell();
91 rStm.SeekRel( 4UL );
93 ZCodec aCodec;
94 aCodec.BeginCompression();
95 aCodec.Compress( rStmToWrite, rStm );
96 aCodec.EndCompression();
98 nCompSize = rStm.Tell() - nPos - 4UL;
99 rStm.Seek( nPos );
100 rStm << nCompSize;
101 rStm.Seek( STREAM_SEEK_TO_END );
104 // -----------------------------------------------------------------------------
106 void GalleryCodec::Read( SvStream& rStmToRead )
108 sal_uInt32 nVersion = 0;
110 if( IsCoded( rStm, nVersion ) )
112 sal_uInt32 nCompressedSize, nUnCompressedSize;
114 rStm.SeekRel( 6 );
115 rStm >> nUnCompressedSize >> nCompressedSize;
117 // decompress
118 if( 1 == nVersion )
120 sal_uInt8* pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
121 sal_uInt8* pInBuf = pCompressedBuffer;
122 sal_uInt8* pOutBuf = new sal_uInt8[ nUnCompressedSize ];
123 sal_uInt8* pTmpBuf = pOutBuf;
124 sal_uInt8* pLast = pOutBuf + nUnCompressedSize - 1;
125 sal_uIntPtr nIndex = 0UL, nCountByte, nRunByte;
126 sal_Bool bEndDecoding = sal_False;
130 nCountByte = *pInBuf++;
132 if ( !nCountByte )
134 nRunByte = *pInBuf++;
136 if ( nRunByte > 2 )
138 // absolutes Fuellen
139 memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
140 pInBuf += nRunByte;
141 nIndex += nRunByte;
143 // WORD-Alignment beachten
144 if ( nRunByte & 1 )
145 pInBuf++;
147 else if ( nRunByte == 1 ) // Ende des Bildes
148 bEndDecoding = sal_True;
150 else
152 const sal_uInt8 cVal = *pInBuf++;
154 memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
155 nIndex += nCountByte;
158 while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
160 rStmToRead.Write( pOutBuf, nUnCompressedSize );
162 delete[] pOutBuf;
163 delete[] pCompressedBuffer;
165 else if( 2 == nVersion )
167 ZCodec aCodec;
169 aCodec.BeginCompression();
170 aCodec.Decompress( rStm, rStmToRead );
171 aCodec.EndCompression();
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */