xref: /aoo41x/main/package/source/zipapi/CRC32.cxx (revision a3872823)
1*a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a3872823SAndrew Rist  * distributed with this work for additional information
6*a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9*a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
13*a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a3872823SAndrew Rist  * software distributed under the License is distributed on an
15*a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a3872823SAndrew Rist  * specific language governing permissions and limitations
18*a3872823SAndrew Rist  * under the License.
19*a3872823SAndrew Rist  *
20*a3872823SAndrew Rist  *************************************************************/
21*a3872823SAndrew Rist 
22*a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <CRC32.hxx>
27cdf0e10cSrcweir #ifndef _ZLIB_H
28cdf0e10cSrcweir #ifdef SYSTEM_ZLIB
29cdf0e10cSrcweir #include <zlib.h>
30cdf0e10cSrcweir #else
31cdf0e10cSrcweir #include <external/zlib/zlib.h>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <PackageConstants.hxx>
35cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace rtl;
38cdf0e10cSrcweir using namespace com::sun::star::uno;
39cdf0e10cSrcweir using namespace com::sun::star::io;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /** A class to compute the CRC32 value of a data stream
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir 
CRC32()44cdf0e10cSrcweir CRC32::CRC32()
45cdf0e10cSrcweir : nCRC(0)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir }
~CRC32()48cdf0e10cSrcweir CRC32::~CRC32()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir }
reset()51cdf0e10cSrcweir void SAL_CALL CRC32::reset()
52cdf0e10cSrcweir 	throw(RuntimeException)
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	nCRC=0;
55cdf0e10cSrcweir }
getValue()56cdf0e10cSrcweir sal_Int32 SAL_CALL CRC32::getValue()
57cdf0e10cSrcweir 	throw(RuntimeException)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	return nCRC & 0xFFFFFFFFL;
60cdf0e10cSrcweir }
61cdf0e10cSrcweir /** Update CRC32 with specified sequence of bytes
62cdf0e10cSrcweir  */
updateSegment(const Sequence<sal_Int8> & b,sal_Int32 off,sal_Int32 len)63cdf0e10cSrcweir void SAL_CALL CRC32::updateSegment(const Sequence< sal_Int8 > &b,
64cdf0e10cSrcweir 									sal_Int32 off,
65cdf0e10cSrcweir 									sal_Int32 len)
66cdf0e10cSrcweir 		throw(RuntimeException)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir 	nCRC = crc32(nCRC, (const unsigned char*)b.getConstArray()+off, len );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir /** Update CRC32 with specified sequence of bytes
71cdf0e10cSrcweir  */
update(const Sequence<sal_Int8> & b)72cdf0e10cSrcweir void SAL_CALL CRC32::update(const Sequence< sal_Int8 > &b)
73cdf0e10cSrcweir 		throw(RuntimeException)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 	nCRC = crc32(nCRC, (const unsigned char*)b.getConstArray(),b.getLength());
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
updateStream(Reference<XInputStream> & xStream)78cdf0e10cSrcweir sal_Int32 SAL_CALL CRC32::updateStream( Reference < XInputStream > & xStream )
79cdf0e10cSrcweir 	throw ( RuntimeException )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	sal_Int32 nLength, nTotal = 0;
82cdf0e10cSrcweir 	Sequence < sal_Int8 > aSeq ( n_ConstBufferSize );
83cdf0e10cSrcweir 	do
84cdf0e10cSrcweir 	{
85cdf0e10cSrcweir 		nLength = xStream->readBytes ( aSeq, n_ConstBufferSize );
86cdf0e10cSrcweir 		updateSegment ( aSeq, 0, nLength );
87cdf0e10cSrcweir 		nTotal += nLength;
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 	while ( nLength == n_ConstBufferSize );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	return nTotal;
92cdf0e10cSrcweir }
93