crc16-xmodem.c

Simple implementation of crc16-xmodem
git clone git://git.finwo.net/lib/crc16-xmodem.c
Log | Files | Refs | README | LICENSE

crc16-xmodem.h (2093B)


      1 #ifndef __FINWO_CRC16_XMODEM_H__
      2 #define __FINWO_CRC16_XMODEM_H__
      3 
      4 /// crc16-xmodem
      5 /// =============
      6 ///
      7 /// Generate CRC-16/XMODEM codes consistently
      8 ///
      9 /// Why
     10 /// ---
     11 ///
     12 /// While a fairly simple task, I was implementing this throughout multiple projects
     13 /// of mine. Now if there's a bug, speed improvement to be had, or simple because
     14 /// the C standard changes, I want to be able to update it in 1 location
     15 /// instead of having to fix it in every individual project.
     16 ///
     17 /// TL;DR; I'm lazy, and want a single version of the code
     18 ///
     19 /// Usage
     20 /// -----
     21 ///
     22 /// ```c
     23 /// #include "finwo/crc16-xmodem.h"
     24 ///
     25 /// uint8_t message[] = "Hi!\0\0";
     26 ///
     27 /// // Output 12797 (0x31FD)
     28 /// uint16_t crc = crc16_xmodem(message, 4);
     29 /// crc16_xmodem_b(message, 4, &message[4]);
     30 ///
     31 /// // Now if you want to check if your message has travelled the network without errors:
     32 /// if (crc16_xmodem(message, 6) != 0) {
     33 ///   printf("Message got corrupted in transit\n");
     34 /// }
     35 /// ```
     36 ///
     37 /// Features
     38 /// --------
     39 ///
     40 /// - Consistent CRC-16/XMODEM implementation
     41 /// - ANSI C (C99)
     42 /// - Fast lookup-table based calculation
     43 ///
     44 /// API
     45 /// ---
     46 
     47 #include <stdint.h>
     48 #include <stddef.h>
     49 
     50 /// <details>
     51 ///   <summary>crc16_xmodem(data, length)</summary>
     52 ///
     53 ///   Calculate the CRC-16/XMODEM checksum of the given data
     54 ///
     55 /// ```c
     56 /// uint16_t crc16_xmodem(const uint8_t *data, size_t length);
     57 /// ```
     58 ///
     59 /// </details>
     60 uint16_t crc16_xmodem(const uint8_t *data, size_t length);
     61 
     62 /// <details>
     63 ///   <summary>crc16_xmodem_b(data, length, out)</summary>
     64 ///
     65 ///   Calculate the CRC-16/XMODEM checksum and write it as big-endian bytes
     66 ///
     67 /// ```c
     68 /// void crc16_xmodem_b(const uint8_t *data, size_t length, uint8_t *out);
     69 /// ```
     70 ///
     71 /// </details>
     72 void crc16_xmodem_b(const uint8_t *data, size_t length, uint8_t *out);
     73 
     74 /// Testing
     75 /// -------
     76 ///
     77 /// If you want to run the library's tests, simply run `make test` to compile
     78 /// the testing binary, and then `./test` to run the actual tests.
     79 ///
     80 /// License
     81 /// -------
     82 ///
     83 /// crc16-xmodem source code is available under the MIT-0 license.
     84 
     85 #endif