3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
20 * ====================================================================
23 * @file svn_checksum.h
24 * @brief Subversion checksum routines
27 #ifndef SVN_CHECKSUM_H
28 #define SVN_CHECKSUM_H
30 #include <apr.h> /* for apr_size_t */
31 #include <apr_pools.h> /* for apr_pool_t */
33 #include "svn_types.h" /* for svn_boolean_t, svn_error_t */
37 #endif /* __cplusplus */
41 * Various types of checksums.
45 typedef enum svn_checksum_kind_t
47 /** The checksum is (or should be set to) an MD5 checksum. */
50 /** The checksum is (or should be set to) a SHA1 checksum. */
52 } svn_checksum_kind_t;
55 * A generic checksum representation.
59 typedef struct svn_checksum_t
61 /** The bytes of the checksum. */
62 const unsigned char *digest;
64 /** The type of the checksum. This should never be changed by consumers
66 svn_checksum_kind_t kind;
70 * Opaque type for creating checksums of data.
72 typedef struct svn_checksum_ctx_t svn_checksum_ctx_t;
74 /** Return a new checksum structure of type @a kind, initialized to the all-
75 * zeros value, allocated in @a pool.
80 svn_checksum_create(svn_checksum_kind_t kind,
83 /** Set @a checksum->digest to all zeros, which, by convention, matches
84 * all other checksums.
89 svn_checksum_clear(svn_checksum_t *checksum);
91 /** Compare checksums @a checksum1 and @a checksum2. If their kinds do not
92 * match or if neither is all zeros, and their content does not match, then
93 * return FALSE; else return TRUE.
98 svn_checksum_match(const svn_checksum_t *checksum1,
99 const svn_checksum_t *checksum2);
103 * Return a deep copy of @a checksum, allocated in @a pool. If @a
104 * checksum is NULL then NULL is returned.
109 svn_checksum_dup(const svn_checksum_t *checksum,
113 /** Return the hex representation of @a checksum, allocating the string
119 svn_checksum_to_cstring_display(const svn_checksum_t *checksum,
123 /** Return the hex representation of @a checksum, allocating the
124 * string in @a pool. If @a checksum->digest is all zeros (that is,
125 * 0, not '0') then return NULL. In 1.7+, @a checksum may be NULL
126 * and NULL will be returned in that case.
129 * @note Passing NULL for @a checksum in 1.6 will cause a segfault.
132 svn_checksum_to_cstring(const svn_checksum_t *checksum,
136 /** Return a serialized representation of @a checksum, allocated in
137 * @a result_pool. Temporary allocations are performed in @a scratch_pool.
139 * Note that @a checksum may not be NULL.
144 svn_checksum_serialize(const svn_checksum_t *checksum,
145 apr_pool_t *result_pool,
146 apr_pool_t *scratch_pool);
149 /** Return @a checksum from the serialized format at @a data. The checksum
150 * will be allocated in @a result_pool, with any temporary allocations
151 * performed in @a scratch_pool.
156 svn_checksum_deserialize(const svn_checksum_t **checksum,
158 apr_pool_t *result_pool,
159 apr_pool_t *scratch_pool);
162 /** Parse the hex representation @a hex of a checksum of kind @a kind and
163 * set @a *checksum to the result, allocating in @a pool.
165 * If @a hex is @c NULL or is the all-zeros checksum, then set @a *checksum
171 svn_checksum_parse_hex(svn_checksum_t **checksum,
172 svn_checksum_kind_t kind,
177 * Return in @a *checksum the checksum of type @a kind for the bytes beginning
178 * at @a data, and going for @a len. @a *checksum is allocated in @a pool.
183 svn_checksum(svn_checksum_t **checksum,
184 svn_checksum_kind_t kind,
191 * Return in @a pool a newly allocated checksum populated with the checksum
192 * of type @a kind for the empty string.
197 svn_checksum_empty_checksum(svn_checksum_kind_t kind,
202 * Create a new @c svn_checksum_ctx_t structure, allocated from @a pool for
203 * calculating checksums of type @a kind. @see svn_checksum_final()
208 svn_checksum_ctx_create(svn_checksum_kind_t kind,
212 * Update the checksum represented by @a ctx, with @a len bytes starting at
218 svn_checksum_update(svn_checksum_ctx_t *ctx,
224 * Finalize the checksum used when creating @a ctx, and put the resultant
225 * checksum in @a *checksum, allocated in @a pool.
230 svn_checksum_final(svn_checksum_t **checksum,
231 const svn_checksum_ctx_t *ctx,
236 * Return the digest size of @a checksum.
241 svn_checksum_size(const svn_checksum_t *checksum);
244 * Return @c TRUE iff @a checksum matches the checksum for the empty
250 svn_checksum_is_empty_checksum(svn_checksum_t *checksum);
254 * Return an error of type #SVN_ERR_CHECKSUM_MISMATCH for @a actual and
255 * @a expected checksums which do not match. Use @a fmt, and the following
256 * parameters to populate the error message.
258 * @note This function does not actually check for the mismatch, it just
259 * constructs the error.
261 * @a scratch_pool is used for temporary allocations; the returned error
262 * will be allocated in its own pool (as is typical).
267 svn_checksum_mismatch_err(const svn_checksum_t *expected,
268 const svn_checksum_t *actual,
269 apr_pool_t *scratch_pool,
272 __attribute__ ((format(printf, 4, 5)));
276 #endif /* __cplusplus */
278 #endif /* SVN_CHECKSUM_H */