From: Tollef Fog Heen Date: Sat, 6 Dec 2008 10:13:39 +0000 (+0100) Subject: Implement CRC functions X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4ed84e2b659fe552ede472e6ad19ae31e38dc835;p=yubiyubo Implement CRC functions --- 4ed84e2b659fe552ede472e6ad19ae31e38dc835 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 */