diff options
Diffstat (limited to 'easytool/easycard.c')
-rw-r--r-- | easytool/easycard.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/easytool/easycard.c b/easytool/easycard.c new file mode 100644 index 0000000..32c71f3 --- /dev/null +++ b/easytool/easycard.c @@ -0,0 +1,77 @@ +/* A reverse-engineered implementation of the EasyCard data format */ + +/* (C) 2010 by Harald Welte <laforge@gnumonks.org> + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/* System includes */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <string.h> +#include <time.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <netinet/in.h> + +/* libnfc includes */ +#include <nfc/nfc-types.h> +#include <nfc/mifaretag.h> + +#include "mifare_classic.h" + +/* Easycard specific includes */ +#include "easycard.h" + +time_t easy_timestamp2time(const uint8_t *easy_ts) +{ + return (easy_ts[2] << 16 | easy_ts[1] << 8 | easy_ts[0]) << 8; +} + +/* apply a telta (positive or negative) to a EasyCard log record */ +int easy_update_log_rec(struct easy_log_rec *elr, int16_t delta) +{ + int32_t sum = elr->amount + delta; + int32_t remaining = elr->remaining = delta; + + if ((sum < 0 || sum > 0xffff) || + (remaining < 0 || remaining > 0xffff)) + return -ERANGE; + + elr->amount = sum; + elr->remaining = remaining; + + return 0; +} + +static char tsbuf[64]; +char *easy_asc_timestamp(const uint8_t *timestamp) +{ + time_t t_time = easy_timestamp2time(timestamp); + struct tm *t_tm = gmtime(&t_time); + memset(tsbuf, 0, sizeof(tsbuf)); + snprintf(tsbuf, sizeof(tsbuf), "%4u-%02u-%02u %02u:%02u", + t_tm->tm_year+1900, t_tm->tm_mon+1, t_tm->tm_mday, + t_tm->tm_hour, t_tm->tm_min); + return tsbuf; +} + |