1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 GalleryCodec::GalleryCodec( SvStream
& rIOStm
) :
32 bool GalleryCodec::IsCoded( SvStream
& rStm
, sal_uInt32
& rVersion
)
34 const sal_uInt64 nPos
= rStm
.Tell();
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 );
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
);
70 aCodec
.BeginCompression();
71 aCodec
.Compress( rStmToWrite
, rStm
);
72 aCodec
.EndCompression();
74 nCompSize
= rStm
.Tell() - nPos
- 4;
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
) )
87 sal_uInt32 nCompressedSize
, nUnCompressedSize
;
90 rStm
.ReadUInt32( nUnCompressedSize
).ReadUInt32( nCompressedSize
);
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
++;
110 nRunByte
= *pInBuf
++;
114 // filling absolutely
115 memcpy( &pTmpBuf
[ nIndex
], pInBuf
, nRunByte
);
119 // note WORD alignment
123 else if ( nRunByte
== 1 ) // End of the image
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
)
142 aCodec
.BeginCompression();
143 aCodec
.Decompress( rStm
, rStmToRead
);
144 aCodec
.EndCompression();
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */