diff --git a/libfreefare/freefare.h b/libfreefare/freefare.h index c6bae69..e0cc22c 100644 --- a/libfreefare/freefare.h +++ b/libfreefare/freefare.h @@ -92,7 +92,17 @@ int mifare_classic_get_data_block_permission (MifareTag tag, const MifareClass int mifare_classic_format_sector (MifareTag tag, const MifareClassicBlockNumber block); char *mifare_classic_get_uid (MifareTag tag); -void mifare_classic_trailer_block (MifareClassicBlock *block, const MifareClassicKey key_a, const uint8_t ab_0, const uint8_t ab_1, const uint8_t ab_2, const uint8_t ab_tb, const uint8_t gpb, const MifareClassicKey key_b); +void mifare_classic_trailer_block (MifareClassicBlock *block, const MifareClassicKey key_a, uint8_t ab_0, uint8_t ab_1, uint8_t ab_2, uint8_t ab_tb, const uint8_t gpb, const MifareClassicKey key_b); + +#define C_000 0 +#define C_001 1 +#define C_010 2 +#define C_011 3 +#define C_100 4 +#define C_101 5 +#define C_110 6 +#define C_111 7 +#define C_DEFAULT 255 /* MIFARE Classic Access Bits */ #define MCAB_R 0x8 diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index b205d94..a845641 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -118,4 +118,10 @@ struct mifare_ultralight_tag { #define MIFARE_CLASSIC(tag) ((struct mifare_classic_tag *) tag) #define MIFARE_ULTRALIGHT(tag) ((struct mifare_ultralight_tag *) tag) +/* + * Access bits manipulation macros + */ +#define DB_AB(ab) ((ab == C_DEFAULT) ? C_000 : ab) +#define TB_AB(ab) ((ab == C_DEFAULT) ? C_100 : ab) + #endif /* !__FREEFARE_INTERNAL_H__ */ diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index b624439..8590ee9 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -709,10 +709,15 @@ mifare_classic_last_sector_block (MifareClassicBlockNumber block) * Generates a MIFARE trailer block. */ void -mifare_classic_trailer_block (MifareClassicBlock *block, const MifareClassicKey key_a, const uint8_t ab_0, const uint8_t ab_1, const uint8_t ab_2, const uint8_t ab_tb, const uint8_t gpb, const MifareClassicKey key_b) +mifare_classic_trailer_block (MifareClassicBlock *block, const MifareClassicKey key_a, uint8_t ab_0, uint8_t ab_1, uint8_t ab_2, uint8_t ab_tb, const uint8_t gpb, const MifareClassicKey key_b) { union mifare_classic_block *b = (union mifare_classic_block *)block; // *((union mifare_classic_block *)(&block)); + ab_0 = DB_AB(ab_0); + ab_1 = DB_AB(ab_1); + ab_2 = DB_AB(ab_2); + ab_tb = TB_AB(ab_tb); + memcpy (b->trailer.key_a, key_a, sizeof (MifareClassicKey)); uint32_t access_bits = ((((( ab_0 & 0x4) >> 2) << 8) | (((ab_0 & 0x2) >> 1) << 4) | (ab_0 & 0x1)) |