1/*
2 * Copyright (c) 2012 Apple, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _CC_COMMON_CRC_
25#define _CC_COMMON_CRC_
26
27#include <sys/cdefs.h>
28#if !defined(COMMON_NUMERICS_H)
29#include <CommonNumerics/CommonNumerics.h>
30#endif
31#include <Availability.h>
32#include <stdint.h>
33
34__BEGIN_DECLS
35
36typedef struct _CNCRCRef_t *CNCRCRef;
37
38enum {
39    kCN_CRC_8 = 10,
40    kCN_CRC_8_ICODE = 11,
41    kCN_CRC_8_ITU = 12,
42    kCN_CRC_8_ROHC = 13,
43    kCN_CRC_8_WCDMA = 14,
44    kCN_CRC_16 = 20,
45    kCN_CRC_16_CCITT_TRUE = 21,
46    kCN_CRC_16_CCITT_FALSE = 22,
47    kCN_CRC_16_USB = 23,
48    kCN_CRC_16_XMODEM = 24,
49    kCN_CRC_16_DECT_R = 25,
50    kCN_CRC_16_DECT_X = 26,
51    kCN_CRC_16_ICODE = 27,
52    kCN_CRC_16_VERIFONE = 28,
53    kCN_CRC_16_A = 29,
54    kCN_CRC_16_B = 30,
55    kCN_CRC_16_Fletcher = 31,
56    kCN_CRC_32_Adler = 40,
57    kCN_CRC_32 = 41,
58    kCN_CRC_32_CASTAGNOLI = 42,
59    kCN_CRC_32_BZIP2 = 43,
60    kCN_CRC_32_MPEG_2 = 44,
61    kCN_CRC_32_POSIX = 45,
62    kCN_CRC_32_XFER = 46,
63    kCN_CRC_64_ECMA_182 = 60,
64};
65typedef uint32_t CNcrc;
66
67/*!
68 @function   CNCRC
69 @abstract   One-shot CRC function.
70
71 @param      algorithm  Designates the CRC algorithm to use.
72 @param      in         The data to be checksummed.
73 @param      len        The length of the data to be checksummed.
74 @param      result     The resulting checksum.
75
76 @result     Possible error returns are kCNParamError and kCNUnimplemented.
77 */
78
79CNStatus
80CNCRC(CNcrc algorithm, const void *in, size_t len, uint64_t *result)
81__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
82
83/*!
84 @function   CNCRCInit
85 @abstract   Initialize a CNCRCRef.
86
87 @param      algorithm  Designates the CRC algorithm to use.
88 @param      crcRef     The resulting CNCRCRef.
89
90 @result     Possible error returns are kCNParamError, kCNMemoryFailure and kCNUnimplemented.
91 */
92
93CNStatus
94CNCRCInit(CNcrc algorithm, CNCRCRef *crcRef)
95__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
96
97/*!
98 @function   CNCRCRelease
99 @abstract   Release a CNCRCRef.
100
101 @param      crcRef     The CNCRCRef to release.
102
103 @result     kCNSuccess is always returned.
104 */
105
106CNStatus
107CNCRCRelease(CNCRCRef crcRef)
108__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
109
110/*!
111 @function   CNCRCUpdate
112 @abstract   Process data through the CRC function.  This can be called multiple times
113             with a valid crcRef created using CNCRCInit().
114
115 @param      crcRef     The CNCRCRef to use.
116 @param      in         The data to be checksummed.
117 @param      len        The length of the data to be checksummed.
118
119 @result     Possible error return is kCNParamError.
120 */
121
122CNStatus
123CNCRCUpdate(CNCRCRef crcRef, const void *in, size_t len)
124__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
125
126/*!
127 @function   CNCRCFinal
128 @abstract   Process remaining data through the CRC function and return the resulting checksum.
129 @param      crcRef     The CNCRCRef to use.
130 @param      result     The resulting checksum.
131
132 @result     Possible error return is kCNParamError.
133 */
134
135CNStatus
136CNCRCFinal(CNCRCRef crcRef, uint64_t *result)
137__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
138
139/*!
140 @function   CNCRCWeakTest
141 @abstract   Perform a "weak" test of a checksum.
142 @param      algorithm  Designates the CRC algorithm to test.
143
144 @result     Possible error return is kCNFailure.
145 */
146
147CNStatus
148CNCRCWeakTest(CNcrc algorithm)
149__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
150
151CNStatus
152CNCRCDumpTable(CNcrc algorithm)
153__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0);
154
155
156__END_DECLS
157
158#endif /* _CC_COMMON_CRC_ */
159