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