]> git.ozlabs.org Git - ccan/blob - ccan/crcsync/crcsync.h
8ea33457b32b88fc7bf704d946fdb3ecad6bcc38
[ccan] / ccan / crcsync / crcsync.h
1 #ifndef CCAN_CRCSYNC_H
2 #define CCAN_CRCSYNC_H
3 #include <stdint.h>
4 #include <stddef.h>
5
6 /**
7  * crc_of_blocks - calculate the crc of the blocks.
8  * @data: pointer to the buffer to CRC
9  * @len: length of the buffer
10  * @blocksize: CRC of each block (final block may be shorter)
11  * @crcbits: the number of bits of crc you want (currently 32 maximum)
12  * @crc: the crcs (array will have (len + blocksize-1)/blocksize entries).
13  *
14  * Calculates the CRC of each block, and output the lower @crcbits to
15  * @crc array.
16  */
17 void crc_of_blocks(const void *data, size_t len, unsigned int blocksize,
18                    unsigned int crcbits, uint32_t crc[]);
19
20 /**
21  * crc_context_new - allocate and initialize state for crc_find_block
22  * @blocksize: the sie of each block
23  * @crcbits: the bits valid in the CRCs (<= 32)
24  * @crc: array of block crcs
25  * @num_crcs: number of block crcs
26  *
27  * Returns an allocated pointer to the structure for crc_find_block,
28  * or NULL.  Makes a copy of @crc and @num_crcs.
29  */
30 struct crc_context *crc_context_new(size_t blocksize, unsigned crcbits,
31                                     const uint32_t crc[], unsigned num_crcs);
32
33 /**
34  * crc_read_block - search for block matches in the buffer.
35  * @ctx: struct crc_context from crc_context_new.
36  * @result: unmatched bytecount, or crc which matched.
37  * @buf: pointer to bytes
38  * @buflen: length of buffer
39  *
40  * Returns the number of bytes of the buffer which have been digested,
41  * and sets @result either to a negagive number (== -crc_number - 1)
42  * to show that a block matched a crc, or zero or more to represent a
43  * length of unmatched data.
44  *
45  * Note that multiple lengths of unmatched data might be returned in a row:
46  * you'll probably want to merge them yourself.
47  */
48 size_t crc_read_block(struct crc_context *ctx, long *result,
49                       const void *buf, size_t buflen);
50
51 /**
52  * crc_read_flush - flush any remaining data from crc_read_block.
53  * @ctx: the context passed to crc_read_block.
54  *
55  * Matches the final data.  This can be used to create a boundary, or
56  * simply flush the final data.  Keep calling it until it returns 0.
57  */
58 long crc_read_flush(struct crc_context *ctx);
59
60 /**
61  * crc_context_free - free a context returned from crc_context_new.
62  * @ctx: the context returned from crc_context_new, or NULL.
63  */
64 void crc_context_free(struct crc_context *ctx);
65
66 #endif /* CCAN_CRCSYNC_H */