1 | /* $Id: crc32-zlib.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - CRC-32 on top of zlib (very fast).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/crc.h>
|
---|
33 |
|
---|
34 | #include <zlib.h>
|
---|
35 |
|
---|
36 | /** @todo Check if we can't just use the zlib code directly here. */
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Deal with blocks that are too big for the uInt type.
|
---|
40 | */
|
---|
41 | static uint32_t rtCrc32ProcessTooBig(uint32_t uCRC32, const void *pv, size_t cb)
|
---|
42 | {
|
---|
43 | const Bytef *pb = (const Bytef *)pv;
|
---|
44 | do
|
---|
45 | {
|
---|
46 | uInt const cbChunk = cb <= ~(uInt)0 ? (uInt)cb : ~(uInt)0;
|
---|
47 | uCRC32 = crc32(uCRC32, pb, cbChunk);
|
---|
48 | pb += cbChunk;
|
---|
49 | cb -= cbChunk;
|
---|
50 | } while (!cb);
|
---|
51 | return uCRC32;
|
---|
52 | }
|
---|
53 |
|
---|
54 | RTDECL(uint32_t) RTCrc32(const void *pv, register size_t cb)
|
---|
55 | {
|
---|
56 | uint32_t uCrc = crc32(0, NULL, 0);
|
---|
57 | if (RT_UNLIKELY((uInt)cb == cb))
|
---|
58 | uCrc = crc32(uCrc, (const Bytef *)pv, (uInt)cb);
|
---|
59 | else
|
---|
60 | uCrc = rtCrc32ProcessTooBig(uCrc, pv, cb);
|
---|
61 | return uCrc;
|
---|
62 | }
|
---|
63 | RT_EXPORT_SYMBOL(RTCrc32);
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(uint32_t) RTCrc32Start(void)
|
---|
67 | {
|
---|
68 | return crc32(0, NULL, 0);
|
---|
69 | }
|
---|
70 | RT_EXPORT_SYMBOL(RTCrc32Start);
|
---|
71 |
|
---|
72 |
|
---|
73 | RTDECL(uint32_t) RTCrc32Process(uint32_t uCRC32, const void *pv, size_t cb)
|
---|
74 | {
|
---|
75 | if (RT_UNLIKELY((uInt)cb == cb))
|
---|
76 | uCRC32 = crc32(uCRC32, (const Bytef *)pv, (uInt)cb);
|
---|
77 | else
|
---|
78 | uCRC32 = rtCrc32ProcessTooBig(uCRC32, pv, cb);
|
---|
79 | return uCRC32;
|
---|
80 | }
|
---|
81 | RT_EXPORT_SYMBOL(RTCrc32Process);
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(uint32_t) RTCrc32Finish(uint32_t uCRC32)
|
---|
85 | {
|
---|
86 | return uCRC32;
|
---|
87 | }
|
---|
88 | RT_EXPORT_SYMBOL(RTCrc32Finish);
|
---|
89 |
|
---|