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 
32 bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
33 {
34  const sal_uInt64 nPos = rStm.Tell();
35  bool bRet;
36  sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
37 
38  rStm.ReadUChar( cByte1 ).ReadUChar( cByte2 ).ReadUChar( cByte3 ).ReadUChar( cByte4 ).ReadUChar( cByte5 ).ReadUChar( cByte6 );
39 
40  if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
41  {
42  rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
43  bRet = true;
44  }
45  else
46  {
47  rVersion = 0;
48  bRet = false;
49  }
50 
51  rStm.Seek( nPos );
52 
53  return bRet;
54 }
55 
56 void GalleryCodec::Write( SvStream& rStmToWrite )
57 {
58  sal_uInt32 nPos, nCompSize;
59 
60  const sal_uInt32 nSize = rStmToWrite.TellEnd();
61  rStmToWrite.Seek( 0 );
62 
63  rStm.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
64  rStm.WriteUInt32( nSize );
65 
66  nPos = rStm.Tell();
67  rStm.SeekRel( 4 );
68 
69  ZCodec aCodec;
70  aCodec.BeginCompression();
71  aCodec.Compress( rStmToWrite, rStm );
72  aCodec.EndCompression();
73 
74  nCompSize = rStm.Tell() - nPos - 4;
75  rStm.Seek( nPos );
76  rStm.WriteUInt32( nCompSize );
78 }
79 
80 void GalleryCodec::Read( SvStream& rStmToRead )
81 {
82  sal_uInt32 nVersion = 0;
83 
84  if( !IsCoded( rStm, nVersion ) )
85  return;
86 
87  sal_uInt32 nCompressedSize, nUnCompressedSize;
88 
89  rStm.SeekRel( 6 );
90  rStm.ReadUInt32( nUnCompressedSize ).ReadUInt32( nCompressedSize );
91 
92  // decompress
93  if( 1 == nVersion )
94  {
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;
103 
104  do
105  {
106  nCountByte = *pInBuf++;
107 
108  if ( !nCountByte )
109  {
110  nRunByte = *pInBuf++;
111 
112  if ( nRunByte > 2 )
113  {
114  // filling absolutely
115  memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
116  pInBuf += nRunByte;
117  nIndex += nRunByte;
118 
119  // note WORD alignment
120  if ( nRunByte & 1 )
121  pInBuf++;
122  }
123  else if ( nRunByte == 1 ) // End of the image
124  bEndDecoding = true;
125  }
126  else
127  {
128  const sal_uInt8 cVal = *pInBuf++;
129 
130  memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
131  nIndex += nCountByte;
132  }
133  }
134  while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
135 
136  rStmToRead.WriteBytes(pOutBuf.get(), nUnCompressedSize);
137  }
138  else if( 2 == nVersion )
139  {
140  ZCodec aCodec;
141 
142  aCodec.BeginCompression();
143  aCodec.Decompress( rStm, rStmToRead );
144  aCodec.EndCompression();
145  }
146 }
147 
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvStream & rStm
Definition: codec.hxx:29
sal_Int32 nIndex
virtual sal_uInt64 TellEnd()
#define STREAM_SEEK_TO_END
sal_uInt64 Seek(sal_uInt64 nPos)
tools::Long Decompress(SvStream &rIStm, SvStream &rOStm)
void Read(SvStream &rStmToRead)
Definition: codec.cxx:80
sal_uInt64 SeekRel(sal_Int64 nPos)
SvStream & WriteUInt32(sal_uInt32 nUInt32)
void Compress(SvStream &rIStm, SvStream &rOStm)
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
std::size_t WriteBytes(const void *pData, std::size_t nSize)
void BeginCompression(int nCompressLevel=ZCODEC_DEFAULT_COMPRESSION, bool gzLib=false)
tools::Long EndCompression()
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:56
sal_uInt64 Tell() const
SvStream & WriteChar(char nChar)
static bool IsCoded(SvStream &rStm, sal_uInt32 &rVersion)
Definition: codec.cxx:32
sal_uInt16 nPos