From 4ed84e2b659fe552ede472e6ad19ae31e38dc835 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Sat, 6 Dec 2008 11:13:39 +0100 Subject: [PATCH] Implement CRC functions --- src/crc13239.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/crc13239.h | 27 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/crc13239.c create mode 100644 src/crc13239.h diff --git a/src/crc13239.c b/src/crc13239.c new file mode 100644 index 0000000..cd65441 --- /dev/null +++ b/src/crc13239.c @@ -0,0 +1,47 @@ +/* + * Copyright 2008, Tollef Fog Heen + * + * Licensed under the Apache License, Version 2.0 (the "License"); you + * may not use this file except in compliance with the License. You + * may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + * + * ISO 13239 CRC 16 implementation + */ + +#include "crc13239.h" + +void crc_init(unsigned short *c) { + *c = 0xffff; +} + +void crc_update(unsigned short *c, unsigned char val) { + int i, j; + m_crc ^= val; + for (i = 0; i < 8; i++) { + j = m_crc & 1; + m_crc >>= 1; + if (j) m_crc ^= 0x8408; + } + +} + +unsigned short crc_get_full(unsigned char *data, size_t len, bool onecomp) { + unsigned short c; + crc_init(&c); + while (bcnt--) + crc_update(&c, *bp++); + return onecomp ? ~c : c; +} + +bool crc_check(unsigned short *c, char *crc) { + return (((c & 0xff) == crc[0]) && + ((c >> 8) == crc[1])); +} diff --git a/src/crc13239.h b/src/crc13239.h new file mode 100644 index 0000000..266a8e0 --- /dev/null +++ b/src/crc13239.h @@ -0,0 +1,27 @@ +/* + * Copyright 2008, Tollef Fog Heen + * + * Licensed under the Apache License, Version 2.0 (the "License"); you + * may not use this file except in compliance with the License. You + * may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + * + */ + +#ifndef _CRC13239_H +#define _CRC13239_H +#define CRC_OK_RESIDUE 0xf0b8 + +void crc_init(unsigned short *c); +void crc_update(unsigned short *c, unsigned char *val); +unsigned short crc_get_full(unsigned char *data, size_t len, bool onecomp); +bool crc_check(unsigned short *c, char *crc); + +#endif /* _CRC13239_H */ -- 2.39.5