2014-05-14 09:55:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Freie Universität Berlin.
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
2011-10-05 15:38:16 +02:00
|
|
|
#include "serialnumber.h"
|
|
|
|
|
2013-06-21 04:00:45 +02:00
|
|
|
int serial_add8(uint8_t s, uint8_t n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (n > 127) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return -1;
|
2013-06-21 04:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t sum = s + n;
|
|
|
|
return (uint8_t)(sum % 256);
|
2011-10-05 15:38:16 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 04:00:45 +02:00
|
|
|
int serial_add16(uint16_t s, uint16_t n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (n > 32767) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return -1;
|
2013-06-21 04:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t sum = s + n;
|
|
|
|
return (uint16_t)(sum % 65536);
|
2011-10-05 15:38:16 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 04:00:45 +02:00
|
|
|
int serial_add32(uint32_t s, uint32_t n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (n > 2147483647) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return -1;
|
2013-06-21 04:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t sum = s + n;
|
|
|
|
return (uint32_t)(sum % 4294967296);
|
2011-10-05 15:38:16 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 04:00:45 +02:00
|
|
|
serial_comp_res_t serial_comp8(uint8_t s1, uint8_t s2)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (s1 == s2) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return EQUAL;
|
2013-06-21 04:00:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 < 128) || (s1 > s2 && s1 - s2 > 128)) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return LESS;
|
|
|
|
}
|
2013-06-21 04:00:45 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 > 128) || (s1 > s2 && s1 - s2 < 128)) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return GREATER;
|
|
|
|
}
|
2013-06-21 04:00:45 +02:00
|
|
|
|
2011-10-05 15:38:16 +02:00
|
|
|
return UNDEF;
|
|
|
|
}
|
|
|
|
|
2013-06-21 04:00:45 +02:00
|
|
|
serial_comp_res_t serial_comp16(uint16_t s1, uint16_t s2)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (s1 == s2) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return EQUAL;
|
2013-06-21 04:00:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 < 32768) || (s1 > s2 && s1 - s2 > 32768)) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return LESS;
|
|
|
|
}
|
2013-06-21 04:00:45 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 > 32768) || (s1 > s2 && s1 - s2 < 32768)) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return GREATER;
|
|
|
|
}
|
2013-06-21 04:00:45 +02:00
|
|
|
|
2011-10-05 15:38:16 +02:00
|
|
|
return UNDEF;
|
|
|
|
}
|
|
|
|
|
2013-06-21 04:00:45 +02:00
|
|
|
serial_comp_res_t serial_comp32(uint32_t s1, uint32_t s2)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (s1 == s2) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return EQUAL;
|
2013-06-21 04:00:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 < 2147483648) || (s1 > s2 && s1 - s2 > 2147483648)) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return LESS;
|
|
|
|
}
|
2013-06-21 04:00:45 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if ((s1 < s2 && s1 - s2 > 2147483648) || (s1 > s2 && s1 - s2 < 2147483648)) {
|
2011-10-05 15:38:16 +02:00
|
|
|
return GREATER;
|
|
|
|
}
|
2013-06-21 04:00:45 +02:00
|
|
|
|
2011-10-05 15:38:16 +02:00
|
|
|
return UNDEF;
|
|
|
|
}
|