LibreOffice Module svx (master)  1
codec.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <tools/stream.hxx>
22 #include <tools/zcodec.hxx>
23 #include "codec.hxx"
24 #include <memory>
25 
26 
28  rStm( rIOStm )
29 {
30 }
31 
33 {
34 }
35 
36 bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
37 {
38  const sal_uIntPtr nPos = rStm.Tell();
39  bool bRet;
40  sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
41 
42  rStm.ReadUChar( cByte1 ).ReadUChar( cByte2 ).ReadUChar( cByte3 ).ReadUChar( cByte4 ).ReadUChar( cByte5 ).ReadUChar( cByte6 );
43 
44  if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
45  {
46  rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
47  bRet = true;
48  }
49  else
50  {
51  rVersion = 0;
52  bRet = false;
53  }
54 
55  rStm.Seek( nPos );
56 
57  return bRet;
58 }
59 
60 void GalleryCodec::Write( SvStream& rStmToWrite )
61 {
62  sal_uInt32 nPos, nCompSize;
63 
64  const sal_uInt32 nSize = rStmToWrite.TellEnd();
65  rStmToWrite.Seek( 0 );
66 
67  rStm.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
68  rStm.WriteUInt32( nSize );
69 
70  nPos = rStm.Tell();
71  rStm.SeekRel( 4 );
72 
73  ZCodec aCodec;
74  aCodec.BeginCompression();
75  aCodec.Compress( rStmToWrite, rStm );
76  aCodec.EndCompression();
77 
78  nCompSize = rStm.Tell() - nPos - 4;
79  rStm.Seek( nPos );
80  rStm.WriteUInt32( nCompSize );
82 }
83 
84 void GalleryCodec::Read( SvStream& rStmToRead )
85 {
86  sal_uInt32 nVersion = 0;
87 
88  if( IsCoded( rStm, nVersion ) )
89  {
90  sal_uInt32 nCompressedSize, nUnCompressedSize;
91 
92  rStm.SeekRel( 6 );
93  rStm.ReadUInt32( nUnCompressedSize ).ReadUInt32( nCompressedSize );
94 
95  // decompress
96  if( 1 == nVersion )
97  {
98  std::unique_ptr<sal_uInt8[]> pCompressedBuffer(new sal_uInt8[ nCompressedSize ]);
99  rStm.ReadBytes(pCompressedBuffer.get(), nCompressedSize);
100  sal_uInt8* pInBuf = pCompressedBuffer.get();
101  std::unique_ptr<sal_uInt8[]> pOutBuf(new sal_uInt8[ nUnCompressedSize ]);
102  sal_uInt8* pTmpBuf = pOutBuf.get();
103  sal_uInt8* pLast = pOutBuf.get() + nUnCompressedSize - 1;
104  sal_uIntPtr nIndex = 0, nCountByte, nRunByte;
105  bool bEndDecoding = false;
106 
107  do
108  {
109  nCountByte = *pInBuf++;
110 
111  if ( !nCountByte )
112  {
113  nRunByte = *pInBuf++;
114 
115  if ( nRunByte > 2 )
116  {
117  // filling absolutely
118  memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
119  pInBuf += nRunByte;
120  nIndex += nRunByte;
121 
122  // note WORD alignment
123  if ( nRunByte & 1 )
124  pInBuf++;
125  }
126  else if ( nRunByte == 1 ) // End of the image
127  bEndDecoding = true;
128  }
129  else
130  {
131  const sal_uInt8 cVal = *pInBuf++;
132 
133  memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
134  nIndex += nCountByte;
135  }
136  }
137  while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
138 
139  rStmToRead.WriteBytes(pOutBuf.get(), nUnCompressedSize);
140  }
141  else if( 2 == nVersion )
142  {
143  ZCodec aCodec;
144 
145  aCodec.BeginCompression();
146  aCodec.Decompress( rStm, rStmToRead );
147  aCodec.EndCompression();
148  }
149  }
150 }
151 
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvStream & rStm
Definition: codec.hxx:31
sal_Int32 nIndex
virtual sal_uInt64 TellEnd()
~GalleryCodec()
Definition: codec.cxx:32
#define STREAM_SEEK_TO_END
sal_uInt64 Seek(sal_uInt64 nPos)
void Read(SvStream &rStmToRead)
Definition: codec.cxx:84
sal_uInt64 SeekRel(sal_Int64 nPos)
long EndCompression()
SvStream & WriteUInt32(sal_uInt32 nUInt32)
void Compress(SvStream &rIStm, SvStream &rOStm)
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
long Decompress(SvStream &rIStm, SvStream &rOStm)
std::size_t WriteBytes(const void *pData, std::size_t nSize)
void BeginCompression(int nCompressLevel=ZCODEC_DEFAULT_COMPRESSION, bool gzLib=false)
SvStream & ReadUChar(unsigned char &rChar)
sal_Int16 nVersion
GalleryCodec(SvStream &rIOStm)
Definition: codec.cxx:27
std::size_t ReadBytes(void *pData, std::size_t nSize)
unsigned char sal_uInt8
void Write(SvStream &rStmToWrite)
Definition: codec.cxx:60
sal_uInt64 Tell() const
SvStream & WriteChar(char nChar)
static bool IsCoded(SvStream &rStm, sal_uInt32 &rVersion)
Definition: codec.cxx:36
sal_uInt16 nPos