PGM_STR(): New macro.
[bertos.git] / mware / crc.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief XModem-CRC16 algorithm (interface)
10  *
11  * \note This algorithm is incompatible with the CCITT-CRC16.
12  *
13  * This code is based on the article Copyright 1986 Stephen Satchell.
14  *
15  * Programmers may incorporate any or all code into their programs,
16  * giving proper credit within the source. Publication of the
17  * source routines is permitted so long as proper credit is given
18  * to Stephen Satchell, Satchell Evaluations and Chuck Forsberg,
19  * Omen Technology.
20  *
21  * \version $Id$
22  * \author Bernardo Innocenti <bernie@develer.com>
23  */
24
25 /*#*
26  *#* $Log$
27  *#* Revision 1.5  2005/04/11 19:10:28  bernie
28  *#* Include top-level headers from cfg/ subdir.
29  *#*
30  *#* Revision 1.4  2004/08/25 14:12:09  rasky
31  *#* Aggiornato il comment block dei log RCS
32  *#*
33  *#* Revision 1.3  2004/08/15 05:47:26  bernie
34  *#* updcrc16(): inline version of UPDCRC16(); Cleanup documentation.
35  *#*
36  *#* Revision 1.2  2004/06/03 11:27:09  bernie
37  *#* Add dual-license information.
38  *#*
39  *#* Revision 1.1  2004/06/03 08:58:16  bernie
40  *#* Import into DevLib
41  *#*
42  *#*/
43 #ifndef CRC_H
44 #define CRC_H
45
46 #include <cfg/compiler.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif /* __cplusplus */
51
52
53 /* CRC table */
54 extern const uint16_t crc16tab[256];
55
56
57 /*!
58  * \brief Compute the updated CRC16 value for one octet (macro version)
59  *
60  * \note This version is only intended for old/broken compilers.
61  *       Use the inline function in new code.
62  *
63  * \param c New octet (range 0-255)
64  * \param oldcrc Previous CRC16 value (referenced twice, beware of side effects)
65  */
66 #define UPDCRC16(c, oldcrc) (crc16tab[((oldcrc) >> 8) ^ ((unsigned char)(c))] ^ ((oldcrc) << 8))
67
68
69 #ifdef INLINE
70 /*!
71  * \brief Compute the updated CRC16 value for one octet (macro version)
72  */
73 INLINE uint16_t updcrc16(uint8_t c, uint16_t oldcrc)
74 {
75         return crc16tab[(oldcrc >> 8) ^ c] ^ (oldcrc << 8);
76 }
77 #endif // INLINE
78
79
80 /*!
81  * This function implements the CRC 16 calculation on a buffer.
82  *
83  * \param crc  Current CRC16 value.
84  * \param buf  The buffer to perform CRC calculation on.
85  * \param len  The length of the Buffer.
86  *
87  * \return The updated CRC16 value.
88  */
89 extern uint16_t crc16(uint16_t crc, const void *buf, size_t len);
90
91 #ifdef __cplusplus
92 }
93 #endif /* __cplusplus */
94
95 #endif /* CRC_H */