summaryrefslogtreecommitdiff
path: root/easytool/mifare_classic.c
blob: 5f21fd5452ef7a001183199503b6121db962d704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "mifare_classic.h"

void mfcl_parse_acc_bits(struct acc_bits_parsed *abp, uint8_t *acc_bits)
{
	uint8_t c1, c2, c3;
	uint8_t block;

	memset(abp, 0, sizeof(*abp));

	c1 = acc_bits[1] >> 4;
	c2 = acc_bits[2] & 0xf;
	c3 = acc_bits[2] >> 4;

	printf("C1 = %x, C2 = %x, C3 = %x\n", c1, c2, c3);

	for (block = 0; block < 4; block++) {
		uint8_t testbit = 1 << block;
		if (c1 & testbit)
			abp->block[block] |= ABP_C1;
		if (c2 & testbit)
			abp->block[block] |= ABP_C2;
		if (c3 & testbit)
			abp->block[block] |= ABP_C3;
	}
}

/* apply a delta (positive or negative) to a Mifare Classic VALUE block */
int mfcl_update_value_block(struct mfcl_value_block *mvb, int32_t delta)
{
	int64_t sum = mvb->value + delta;

	if (sum > 0xffffffff || sum < 0)
		return -ERANGE;

	mvb->value = sum;
	mvb->value_backup = mvb->value;
	mvb->value_inv = ~sum;

	return 0;
}
personal git repositories of Harald Welte. Your mileage may vary