diff options
author | Paul Kocialkowski | 2016-01-27 20:54:50 +0100 |
---|---|---|
committer | Paul Kocialkowski | 2016-01-27 20:58:23 +0100 |
commit | d036034922795d8af1d8cefc5bf466882262782f (patch) | |
tree | e92be65561abdaa453b98246188135c85e110cc0 /src/hdlc.c | |
parent | 2e31eedce5bc7d2fc37f8cd6be1a7177dd7f6da7 (diff) |
Use fixed-size stdint types and sys types
Generic C types only guarantee a minimum size, not a fixed size, so it is
necessary to use stdint types for fixed-size variables.
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
Diffstat (limited to 'src/hdlc.c')
-rw-r--r-- | src/hdlc.c | 94 |
1 files changed, 48 insertions, 46 deletions
@@ -17,14 +17,16 @@ #include <stdlib.h> #include <stdio.h> +#include <stdint.h> #include <string.h> #include <endian.h> +#include <sys/types.h> #include "lg-downloader.h" #include "hdlc.h" /* Lookup table for CCITT-16 CRC with generator polynomial 0x8408. */ -static const unsigned short hdlc_crc_table[] = { +static const uint16_t hdlc_crc_table[] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, @@ -59,26 +61,26 @@ static const unsigned short hdlc_crc_table[] = { 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 }; -static unsigned short hdlc_crc_step(unsigned short crc, unsigned char data) +static uint16_t hdlc_crc_step(uint16_t crc, uint8_t data) { return ((crc >> 8) ^ hdlc_crc_table[(crc ^ data) & 0xff]); } -static unsigned short hdlc_crc_final(unsigned short crc) +static uint16_t hdlc_crc_final(uint16_t crc) { return crc ^ HDLC_CRC_SEED; } -static unsigned int hdlc_escape_size(const void *data, unsigned int size) +static size_t hdlc_escape_size(const void *data, size_t size) { - unsigned int length; - unsigned char *p; - unsigned int i; + size_t length; + uint8_t *p; + size_t i; if (data == NULL || size == 0) return 0; - p = (unsigned char *) data; + p = (uint8_t *) data; length = size; for (i = 0; i < size; i++) @@ -88,18 +90,18 @@ static unsigned int hdlc_escape_size(const void *data, unsigned int size) return length; } -static int hdlc_escape(const void *data, unsigned int size, void *buffer) +static int hdlc_escape(const void *data, size_t size, void *buffer) { - unsigned char *p; - unsigned char *q; - unsigned int i; - unsigned int j; + uint8_t *p; + uint8_t *q; + size_t i; + size_t j; if (buffer == NULL) return -1; - p = (unsigned char *) data; - q = (unsigned char *) buffer; + p = (uint8_t *) data; + q = (uint8_t *) buffer; for (i = 0, j = 0; i < size; i++) { if (p[i] == HDLC_FLAG || p[i] == HDLC_ESCAPE) { @@ -113,19 +115,19 @@ static int hdlc_escape(const void *data, unsigned int size, void *buffer) return 0; } -static int hdlc_unescape_size(const void *data, unsigned int size) +static int hdlc_unescape_size(const void *data, size_t size) { if (data == NULL || size == 0) return 0; - unsigned int length; - unsigned char *p; - unsigned int i; + size_t length; + uint8_t *p; + size_t i; if (data == NULL || size == 0) return 0; - p = (unsigned char *) data; + p = (uint8_t *) data; length = size; for (i = 0; i < size; i++) @@ -135,18 +137,18 @@ static int hdlc_unescape_size(const void *data, unsigned int size) return length; } -static int hdlc_unescape(const void *data, unsigned int size, void *buffer) +static int hdlc_unescape(const void *data, size_t size, void *buffer) { - unsigned char *p; - unsigned char *q; - unsigned int i; - unsigned int j; + uint8_t *p; + uint8_t *q; + size_t i; + size_t j; if (buffer == NULL) return -1; - p = (unsigned char *) data; - q = (unsigned char *) buffer; + p = (uint8_t *) data; + q = (uint8_t *) buffer; for (i = 0, j = 0; i < size; i++) { if (p[i] == HDLC_ESCAPE && i < (size - 1)) @@ -158,14 +160,14 @@ static int hdlc_unescape(const void *data, unsigned int size, void *buffer) return 0; } -int hdlc_send(struct context *context, const void *data, unsigned int size) +int hdlc_send(struct context *context, const void *data, size_t size) { void *buffer = NULL; - unsigned int length; - unsigned short crc; - unsigned char *p; - unsigned short *q; - unsigned int i; + size_t length; + uint16_t crc; + uint8_t *p; + uint16_t *q; + size_t i; int rc; if (data == NULL || size == 0) @@ -180,7 +182,7 @@ int hdlc_send(struct context *context, const void *data, unsigned int size) /* CRC */ crc = HDLC_CRC_SEED; - p = (unsigned char *) data; + p = (uint8_t *) data; /* CRC is calculated from unescaped data */ for (i = 0; i < size; i++) @@ -205,7 +207,7 @@ int hdlc_send(struct context *context, const void *data, unsigned int size) buffer = calloc(1, length); - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; *p = HDLC_FLAG; p += HDLC_HEAD_SIZE; @@ -216,12 +218,12 @@ int hdlc_send(struct context *context, const void *data, unsigned int size) p += (length - HDLC_HEAD_SIZE - HDLC_TAIL_SIZE); - q = (unsigned short *) p; + q = (uint16_t *) p; *q = htole16(crc); q++; - p = (unsigned char *) q; + p = (uint8_t *) q; *p++ = HDLC_FLAG; @@ -246,15 +248,15 @@ complete: return rc; } -int hdlc_recv(struct context *context, void *data, unsigned int size) +int hdlc_recv(struct context *context, void *data, size_t size) { void *contents = NULL; void *buffer = NULL; - unsigned int length; - unsigned int count; - unsigned short crc; - unsigned char *p; - unsigned int i; + size_t length; + size_t count; + uint16_t crc; + uint8_t *p; + size_t i; int rc; if (data == NULL || size == 0) @@ -273,7 +275,7 @@ int hdlc_recv(struct context *context, void *data, unsigned int size) buffer = calloc(1, length); count = 0; - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; /* USB recv */ @@ -298,7 +300,7 @@ int hdlc_recv(struct context *context, void *data, unsigned int size) /* Check */ check: - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; /* Check for bare minimum size and flag */ if (count < (HDLC_TAIL_SIZE + 1) || p[count - 1] != HDLC_FLAG) { @@ -324,7 +326,7 @@ check: /* CRC */ crc = HDLC_CRC_SEED; - p = (unsigned char *) contents; + p = (uint8_t *) contents; for (i = 0; i < length; i++) crc = hdlc_crc_step(crc, *p++); |