---
name: "mad"
section: "3"
platform: "linux"
package: "libfreefare-doc"
version: "0.4.0-2.3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mad/3"
---

# mad(3)

Mifare Application Directory (MAD) Manipulation Functions

## NAME

**mad\_new**, **mad\_read**, **mad\_write**, **mad\_get\_version**, **mad\_set\_version**, **mad\_get\_card\_publisher\_sector**, **mad\_set\_card\_publisher\_sector**, **mad\_get\_aid**, **mad\_set\_aid**, **mad\_free**, – Mifare Application Directory (MAD) Manipulation Functions

## LIBRARY

Mifare card manipulation library (libfreefare, -lfreefare)

## SYNOPSIS

**#include \<freefare.h\>**

*Mad*\

**mad\_new**(*uint8\_t version*);

*Mad*\

**mad\_read**(*MifareTag tag*);

*int*\

**mad\_write**(*MifareTag tag*, *Mad mad*, *MifareClassicKey key\_b\_sector\_00*, *MifareClassicKey key\_b\_sector\_10*);

*int*\

**mad\_get\_version**(*Mad mad*);

*void*\

**mad\_set\_version**(*Mad mad*, *uint8\_t version*);

*MifareClassicSectorNumber*\

**mad\_get\_card\_publisher\_sector**(*Mad mad*);

*int*\

**mad\_set\_card\_publisher\_sector**(*Mad mad*, *MifareClassicSectorNumber cps*);

*int*\

**mad\_get\_aid**(*Mad mad*, *MifareClassicSectorNumber sector*, *MadAid \*aid*);

*int*\

**mad\_set\_aid**(*Mad mad*, *MifareClassicSectorNumber sector*, *MadAid aid*);

*void*\

**mad\_free**(*Mad mad*);

## DESCRIPTION

Mifare Application Directories (MAD) can be easily manipulated using the **mad\_\***() set of functions.

A *mad* can be loaded form a Mifare Classic *tag* using **mad\_read**() or generated from scratch using **mad\_new**() and providing the *version* of the *mad* to generate. After using a *mad*, the memory can be reclaimed using **mad\_free**().

A *mad* can be written to a Mifare Classic *tag* using **mad\_write**() and providing the B keys required for writing to sectors 0, *key\_b\_sector\_00,* and the one required for writing to sectors 10, *key\_b\_sector\_10*. When writing a MAD version 1 *mad*, the *key\_b\_sector\_10*.

The *version* of a MAD *mad* can be read using **mad\_get\_version**() and changed using **mad\_set\_version**().

The card publisher sector number *cps* can be read read using **mad\_get\_card\_publisher\_sector**() and writen using **mad\_set\_card\_publisher\_sector**().

Sectors allocation in the *mad* is handled by the **mad\_get\_aid**() and **mad\_set\_aid**() functions.  These functions fill-in or read the Application Identifier, *aid* for the given *sector*.

## RETURN VALUES

The **mad\_new**() function return *NULL* on failure and allocates memory that has to be freed using **mad\_free**() on success. Unless stated otherwise, all other functions return a value greater than or equal to *0* on success or *-1* on failure.

## SEE ALSO

[freefare(3)](/freefare/3.md), [mifare\_application(3)](/mifare_application/3.md), [mifare\_classic(3)](/mifare_classic/3.md)

## AUTHORS

Romain Tartiere ⟨romain@blogreen.org⟩ Romuald Conty ⟨romuald@libnfc.org⟩
