1/*
2 * librdkafka - Apache Kafka C library
3 *
4 * Copyright (c) 2018 Magnus Edenhill
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28/**
29 * \file rdcrc32.h
30 * Functions and types for CRC checks.
31 *
32 * Generated on Tue May 8 17:36:59 2012,
33 * by pycrc v0.7.10, http://www.tty1.net/pycrc/
34 *
35 * NOTE: Contains librd modifications:
36 * - rd_crc32() helper.
37 * - __RDCRC32___H__ define (was missing the '32' part).
38 *
39 * using the configuration:
40 * Width = 32
41 * Poly = 0x04c11db7
42 * XorIn = 0xffffffff
43 * ReflectIn = True
44 * XorOut = 0xffffffff
45 * ReflectOut = True
46 * Algorithm = table-driven
47 *****************************************************************************/
48#ifndef __RDCRC32___H__
49#define __RDCRC32___H__
50
51#include "rd.h"
52
53#include <stdlib.h>
54#include <stdint.h>
55
56#if WITH_ZLIB
57#include <zlib.h>
58#endif
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64
65/**
66 * The definition of the used algorithm.
67 *****************************************************************************/
68#define CRC_ALGO_TABLE_DRIVEN 1
69
70
71/**
72 * The type of the CRC values.
73 *
74 * This type must be big enough to contain at least 32 bits.
75 *****************************************************************************/
76typedef uint32_t rd_crc32_t;
77
78#if !WITH_ZLIB
79extern const rd_crc32_t crc_table[256];
80#endif
81
82
83/**
84 * Reflect all bits of a \a data word of \a data_len bytes.
85 *
86 * \param data The data word to be reflected.
87 * \param data_len The width of \a data expressed in number of bits.
88 * \return The reflected data.
89 *****************************************************************************/
90rd_crc32_t rd_crc32_reflect(rd_crc32_t data, size_t data_len);
91
92
93/**
94 * Calculate the initial crc value.
95 *
96 * \return The initial crc value.
97 *****************************************************************************/
98static RD_INLINE rd_crc32_t rd_crc32_init(void)
99{
100#if WITH_ZLIB
101 return crc32(0, NULL, 0);
102#else
103 return 0xffffffff;
104#endif
105}
106
107
108/**
109 * Update the crc value with new data.
110 *
111 * \param crc The current crc value.
112 * \param data Pointer to a buffer of \a data_len bytes.
113 * \param data_len Number of bytes in the \a data buffer.
114 * \return The updated crc value.
115 *****************************************************************************/
116 /**
117 * Update the crc value with new data.
118 *
119 * \param crc The current crc value.
120 * \param data Pointer to a buffer of \a data_len bytes.
121 * \param data_len Number of bytes in the \a data buffer.
122 * \return The updated crc value.
123 *****************************************************************************/
124static RD_INLINE RD_UNUSED
125rd_crc32_t rd_crc32_update(rd_crc32_t crc, const unsigned char *data, size_t data_len)
126{
127#if WITH_ZLIB
128 rd_assert(data_len <= UINT_MAX);
129 return crc32(crc, data, (uInt) data_len);
130#else
131 unsigned int tbl_idx;
132
133 while (data_len--) {
134 tbl_idx = (crc ^ *data) & 0xff;
135 crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff;
136
137 data++;
138 }
139 return crc & 0xffffffff;
140#endif
141}
142
143
144/**
145 * Calculate the final crc value.
146 *
147 * \param crc The current crc value.
148 * \return The final crc value.
149 *****************************************************************************/
150static RD_INLINE rd_crc32_t rd_crc32_finalize(rd_crc32_t crc)
151{
152#if WITH_ZLIB
153 return crc;
154#else
155 return crc ^ 0xffffffff;
156#endif
157}
158
159
160/**
161 * Wrapper for performing CRC32 on the provided buffer.
162 */
163static RD_INLINE rd_crc32_t rd_crc32 (const char *data, size_t data_len) {
164 return rd_crc32_finalize(rd_crc32_update(rd_crc32_init(),
165 (const unsigned char *)data,
166 data_len));
167}
168
169#ifdef __cplusplus
170} /* closing brace for extern "C" */
171#endif
172
173#endif /* __RDCRC32___H__ */
174