Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > f831237475ece7d1519c6812064707d4 > files > 73

libnfc-devel-1.4.2-1.fc14.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libnfc: nfc-mfclassic.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.3 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">libnfc&#160;<span id="projectnumber">1.4.2</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="dir_e1066e20c97c3d492c10ec6dd00ee654.html">examples</a>      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<h1>nfc-mfclassic.c</h1>  </div>
</div>
<div class="contents">
<a href="nfc-mfclassic_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*-</span>
<a name="l00002"></a>00002 <span class="comment"> * Public platform independent Near Field Communication (NFC) library examples</span>
<a name="l00003"></a>00003 <span class="comment"> * </span>
<a name="l00004"></a>00004 <span class="comment"> * Copyright (C) 2009, Roel Verdult</span>
<a name="l00005"></a>00005 <span class="comment"> * Copyright (C) 2010, Romuald Conty, Romain Tartière</span>
<a name="l00006"></a>00006 <span class="comment"> * </span>
<a name="l00007"></a>00007 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<a name="l00008"></a>00008 <span class="comment"> * modification, are permitted provided that the following conditions are met:</span>
<a name="l00009"></a>00009 <span class="comment"> *  1) Redistributions of source code must retain the above copyright notice,</span>
<a name="l00010"></a>00010 <span class="comment"> *  this list of conditions and the following disclaimer. </span>
<a name="l00011"></a>00011 <span class="comment"> *  2 )Redistributions in binary form must reproduce the above copyright</span>
<a name="l00012"></a>00012 <span class="comment"> *  notice, this list of conditions and the following disclaimer in the</span>
<a name="l00013"></a>00013 <span class="comment"> *  documentation and/or other materials provided with the distribution.</span>
<a name="l00014"></a>00014 <span class="comment"> *</span>
<a name="l00015"></a>00015 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot;</span>
<a name="l00016"></a>00016 <span class="comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<a name="l00017"></a>00017 <span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
<a name="l00018"></a>00018 <span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
<a name="l00019"></a>00019 <span class="comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
<a name="l00020"></a>00020 <span class="comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
<a name="l00021"></a>00021 <span class="comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
<a name="l00022"></a>00022 <span class="comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
<a name="l00023"></a>00023 <span class="comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
<a name="l00024"></a>00024 <span class="comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
<a name="l00025"></a>00025 <span class="comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00026"></a>00026 <span class="comment"> * </span>
<a name="l00027"></a>00027 <span class="comment"> * Note that this license only applies on the examples, NFC library itself is under LGPL</span>
<a name="l00028"></a>00028 <span class="comment"> *</span>
<a name="l00029"></a>00029 <span class="comment"> */</span>
<a name="l00030"></a>00030 
<a name="l00036"></a>00036 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
<a name="l00037"></a>00037 <span class="preprocessor"></span><span class="preprocessor">#  include &quot;config.h&quot;</span>
<a name="l00038"></a>00038 <span class="preprocessor">#endif // HAVE_CONFIG_H</span>
<a name="l00039"></a>00039 <span class="preprocessor"></span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;stdint.h&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;stddef.h&gt;</span>
<a name="l00044"></a>00044 <span class="preprocessor">#include &lt;stdbool.h&gt;</span>
<a name="l00045"></a>00045 
<a name="l00046"></a>00046 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00047"></a>00047 <span class="preprocessor">#include &lt;ctype.h&gt;</span>
<a name="l00048"></a>00048 
<a name="l00049"></a>00049 <span class="preprocessor">#include &lt;nfc/nfc.h&gt;</span>
<a name="l00050"></a>00050 
<a name="l00051"></a>00051 <span class="preprocessor">#include &quot;mifare.h&quot;</span>
<a name="l00052"></a>00052 <span class="preprocessor">#include &quot;<a class="code" href="nfc-utils_8h.html" title="Provide some examples shared functions like print, parity calculation, options parsing.">nfc-utils.h</a>&quot;</span>
<a name="l00053"></a>00053 
<a name="l00054"></a>00054 <span class="keyword">static</span> <a class="code" href="structnfc__device__t.html" title="NFC device information.">nfc_device_t</a> *pnd;
<a name="l00055"></a>00055 <span class="keyword">static</span> <a class="code" href="structnfc__target__t.html" title="NFC target structure.">nfc_target_t</a> nt;
<a name="l00056"></a>00056 <span class="keyword">static</span> mifare_param mp;
<a name="l00057"></a>00057 <span class="keyword">static</span> mifare_classic_tag mtKeys;
<a name="l00058"></a>00058 <span class="keyword">static</span> mifare_classic_tag mtDump;
<a name="l00059"></a>00059 <span class="keyword">static</span> <span class="keywordtype">bool</span> bUseKeyA;
<a name="l00060"></a>00060 <span class="keyword">static</span> <span class="keywordtype">bool</span> bUseKeyFile;
<a name="l00061"></a>00061 <span class="keyword">static</span> uint8_t uiBlocks;
<a name="l00062"></a>00062 <span class="keyword">static</span> byte_t keys[] = {
<a name="l00063"></a>00063   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
<a name="l00064"></a>00064   0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7,
<a name="l00065"></a>00065   0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
<a name="l00066"></a>00066   0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5,
<a name="l00067"></a>00067   0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd,
<a name="l00068"></a>00068   0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a,
<a name="l00069"></a>00069   0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
<a name="l00070"></a>00070   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00071"></a>00071   0xab, 0xcd, 0xef, 0x12, 0x34, 0x56
<a name="l00072"></a>00072 };
<a name="l00073"></a>00073 
<a name="l00074"></a>00074 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="structnfc__modulation__t.html" title="NFC modulation structure.">nfc_modulation_t</a> nmMifare = {
<a name="l00075"></a>00075   .nmt = NMT_ISO14443A,
<a name="l00076"></a>00076   .nbr = NBR_106,
<a name="l00077"></a>00077 };
<a name="l00078"></a>00078 
<a name="l00079"></a>00079 <span class="keyword">static</span> <span class="keywordtype">size_t</span> num_keys = <span class="keyword">sizeof</span> (keys) / 6;
<a name="l00080"></a>00080 
<a name="l00081"></a>00081 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00082"></a>00082 print_success_or_failure (<span class="keywordtype">bool</span> bFailure, uint32_t * uiBlockCounter)
<a name="l00083"></a>00083 {
<a name="l00084"></a>00084   printf (<span class="stringliteral">&quot;%c&quot;</span>, (bFailure) ? <span class="charliteral">&#39;x&#39;</span> : <span class="charliteral">&#39;.&#39;</span>);
<a name="l00085"></a>00085   <span class="keywordflow">if</span> (uiBlockCounter &amp;&amp; !bFailure)
<a name="l00086"></a>00086     *uiBlockCounter += (*uiBlockCounter &lt; 128) ? 4 : 16;
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088 
<a name="l00089"></a>00089 <span class="keyword">static</span>  <span class="keywordtype">bool</span>
<a name="l00090"></a>00090 is_first_block (uint32_t uiBlock)
<a name="l00091"></a>00091 {
<a name="l00092"></a>00092   <span class="comment">// Test if we are in the small or big sectors</span>
<a name="l00093"></a>00093   <span class="keywordflow">if</span> (uiBlock &lt; 128)
<a name="l00094"></a>00094     <span class="keywordflow">return</span> ((uiBlock) % 4 == 0);
<a name="l00095"></a>00095   <span class="keywordflow">else</span>
<a name="l00096"></a>00096     <span class="keywordflow">return</span> ((uiBlock) % 16 == 0);
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098 
<a name="l00099"></a>00099 <span class="keyword">static</span>  <span class="keywordtype">bool</span>
<a name="l00100"></a>00100 is_trailer_block (uint32_t uiBlock)
<a name="l00101"></a>00101 {
<a name="l00102"></a>00102   <span class="comment">// Test if we are in the small or big sectors</span>
<a name="l00103"></a>00103   <span class="keywordflow">if</span> (uiBlock &lt; 128)
<a name="l00104"></a>00104     <span class="keywordflow">return</span> ((uiBlock + 1) % 4 == 0);
<a name="l00105"></a>00105   <span class="keywordflow">else</span>
<a name="l00106"></a>00106     <span class="keywordflow">return</span> ((uiBlock + 1) % 16 == 0);
<a name="l00107"></a>00107 }
<a name="l00108"></a>00108 
<a name="l00109"></a>00109 <span class="keyword">static</span>  uint32_t
<a name="l00110"></a>00110 get_trailer_block (uint32_t uiFirstBlock)
<a name="l00111"></a>00111 {
<a name="l00112"></a>00112   <span class="comment">// Test if we are in the small or big sectors</span>
<a name="l00113"></a>00113   uint32_t trailer_block = 0;
<a name="l00114"></a>00114   <span class="keywordflow">if</span> (uiFirstBlock &lt; 128) {
<a name="l00115"></a>00115     trailer_block = uiFirstBlock + (3 - (uiFirstBlock % 4));
<a name="l00116"></a>00116   } <span class="keywordflow">else</span> {
<a name="l00117"></a>00117     trailer_block = uiFirstBlock + (15 - (uiFirstBlock % 16));
<a name="l00118"></a>00118   }
<a name="l00119"></a>00119   <span class="keywordflow">return</span> trailer_block;
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121 
<a name="l00122"></a>00122 <span class="keyword">static</span>  <span class="keywordtype">bool</span>
<a name="l00123"></a>00123 authenticate (uint32_t uiBlock)
<a name="l00124"></a>00124 {
<a name="l00125"></a>00125   mifare_cmd mc;
<a name="l00126"></a>00126   uint32_t uiTrailerBlock;
<a name="l00127"></a>00127   <span class="keywordtype">size_t</span>  key_index;
<a name="l00128"></a>00128 
<a name="l00129"></a>00129   <span class="comment">// Key file authentication.</span>
<a name="l00130"></a>00130   <span class="keywordflow">if</span> (bUseKeyFile) {
<a name="l00131"></a>00131     <span class="comment">// Set the authentication information (uid)</span>
<a name="l00132"></a>00132     memcpy (mp.mpa.abtUid, nt.nti.nai.abtUid, 4);
<a name="l00133"></a>00133 
<a name="l00134"></a>00134     <span class="comment">// Locate the trailer (with the keys) used for this sector</span>
<a name="l00135"></a>00135     uiTrailerBlock = get_trailer_block (uiBlock);
<a name="l00136"></a>00136 
<a name="l00137"></a>00137     <span class="comment">// Determin if we should use the a or the b key</span>
<a name="l00138"></a>00138     <span class="keywordflow">if</span> (bUseKeyA) {
<a name="l00139"></a>00139       mc = MC_AUTH_A;
<a name="l00140"></a>00140       memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6);
<a name="l00141"></a>00141     } <span class="keywordflow">else</span> {
<a name="l00142"></a>00142       mc = MC_AUTH_B;
<a name="l00143"></a>00143       memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, 6);
<a name="l00144"></a>00144     }
<a name="l00145"></a>00145 
<a name="l00146"></a>00146     <span class="comment">// Try to authenticate for the current sector</span>
<a name="l00147"></a>00147     <span class="keywordflow">if</span> (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &amp;mp))
<a name="l00148"></a>00148       <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00149"></a>00149   }
<a name="l00150"></a>00150   <span class="comment">// Auto authentication.</span>
<a name="l00151"></a>00151   <span class="keywordflow">else</span> {
<a name="l00152"></a>00152     <span class="comment">// Determin if we should use the a or the b key</span>
<a name="l00153"></a>00153     mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B;
<a name="l00154"></a>00154 
<a name="l00155"></a>00155     <span class="comment">// Set the authentication information (uid)</span>
<a name="l00156"></a>00156     memcpy (mp.mpa.abtUid, nt.nti.nai.abtUid, 4);
<a name="l00157"></a>00157 
<a name="l00158"></a>00158     <span class="keywordflow">for</span> (key_index = 0; key_index &lt; num_keys; key_index++) {
<a name="l00159"></a>00159       memcpy (mp.mpa.abtKey, keys + (key_index * 6), 6);
<a name="l00160"></a>00160       <span class="keywordflow">if</span> (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &amp;mp)) {
<a name="l00161"></a>00161         <span class="keywordflow">if</span> (bUseKeyA)
<a name="l00162"></a>00162           memcpy (mtKeys.amb[uiBlock].mbt.abtKeyA, &amp;mp.mpa.abtKey, 6);
<a name="l00163"></a>00163         <span class="keywordflow">else</span>
<a name="l00164"></a>00164           memcpy (mtKeys.amb[uiBlock].mbt.abtKeyB, &amp;mp.mpa.abtKey, 6);
<a name="l00165"></a>00165 
<a name="l00166"></a>00166         <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00167"></a>00167       }
<a name="l00168"></a>00168 
<a name="l00169"></a>00169       <a class="code" href="nfc_8c.html#acff0e43e51d7a2c8d750d09fa5265e16" title="Select a passive or emulated tag.">nfc_initiator_select_passive_target</a> (pnd, nmMifare, mp.mpa.abtUid, 4, NULL);
<a name="l00170"></a>00170     }
<a name="l00171"></a>00171   }
<a name="l00172"></a>00172 
<a name="l00173"></a>00173   <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00174"></a>00174 }
<a name="l00175"></a>00175 
<a name="l00176"></a>00176 <span class="keyword">static</span>  <span class="keywordtype">bool</span>
<a name="l00177"></a>00177 read_card (<span class="keywordtype">void</span>)
<a name="l00178"></a>00178 {
<a name="l00179"></a>00179   int32_t iBlock;
<a name="l00180"></a>00180   <span class="keywordtype">bool</span>    bFailure = <span class="keyword">false</span>;
<a name="l00181"></a>00181   uint32_t uiReadBlocks = 0;
<a name="l00182"></a>00182 
<a name="l00183"></a>00183   printf (<span class="stringliteral">&quot;Reading out %d blocks |&quot;</span>, uiBlocks + 1);
<a name="l00184"></a>00184 
<a name="l00185"></a>00185   <span class="comment">// Read the card from end to begin</span>
<a name="l00186"></a>00186   <span class="keywordflow">for</span> (iBlock = uiBlocks; iBlock &gt;= 0; iBlock--) {
<a name="l00187"></a>00187     <span class="comment">// Authenticate everytime we reach a trailer block</span>
<a name="l00188"></a>00188     <span class="keywordflow">if</span> (is_trailer_block (iBlock)) {
<a name="l00189"></a>00189       <span class="comment">// Skip this the first time, bFailure it means nothing (yet)</span>
<a name="l00190"></a>00190       <span class="keywordflow">if</span> (iBlock != uiBlocks)
<a name="l00191"></a>00191         print_success_or_failure (bFailure, &amp;uiReadBlocks);
<a name="l00192"></a>00192 
<a name="l00193"></a>00193       <span class="comment">// Show if the readout went well</span>
<a name="l00194"></a>00194       <span class="keywordflow">if</span> (bFailure) {
<a name="l00195"></a>00195         <span class="comment">// When a failure occured we need to redo the anti-collision</span>
<a name="l00196"></a>00196         <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#acff0e43e51d7a2c8d750d09fa5265e16" title="Select a passive or emulated tag.">nfc_initiator_select_passive_target</a> (pnd, nmMifare, NULL, 0, &amp;nt)) {
<a name="l00197"></a>00197           printf (<span class="stringliteral">&quot;!\nError: tag was removed\n&quot;</span>);
<a name="l00198"></a>00198           <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00199"></a>00199         }
<a name="l00200"></a>00200         bFailure = <span class="keyword">false</span>;
<a name="l00201"></a>00201       }
<a name="l00202"></a>00202 
<a name="l00203"></a>00203       fflush (stdout);
<a name="l00204"></a>00204 
<a name="l00205"></a>00205       <span class="comment">// Try to authenticate for the current sector</span>
<a name="l00206"></a>00206       <span class="keywordflow">if</span> (!authenticate (iBlock)) {
<a name="l00207"></a>00207         printf (<span class="stringliteral">&quot;!\nError: authentication failed for block 0x%02x\n&quot;</span>, iBlock);
<a name="l00208"></a>00208         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00209"></a>00209       }
<a name="l00210"></a>00210       <span class="comment">// Try to read out the trailer</span>
<a name="l00211"></a>00211       <span class="keywordflow">if</span> (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &amp;mp)) {
<a name="l00212"></a>00212         <span class="comment">// Copy the keys over from our key dump and store the retrieved access bits</span>
<a name="l00213"></a>00213         memcpy (mtDump.amb[iBlock].mbt.abtKeyA, mtKeys.amb[iBlock].mbt.abtKeyA, 6);
<a name="l00214"></a>00214         memcpy (mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4);
<a name="l00215"></a>00215         memcpy (mtDump.amb[iBlock].mbt.abtKeyB, mtKeys.amb[iBlock].mbt.abtKeyB, 6);
<a name="l00216"></a>00216       } <span class="keywordflow">else</span> {
<a name="l00217"></a>00217         printf (<span class="stringliteral">&quot;!\nError: unable to read trailer block 0x%02x\n&quot;</span>, iBlock);
<a name="l00218"></a>00218       }
<a name="l00219"></a>00219     } <span class="keywordflow">else</span> {
<a name="l00220"></a>00220       <span class="comment">// Make sure a earlier readout did not fail</span>
<a name="l00221"></a>00221       <span class="keywordflow">if</span> (!bFailure) {
<a name="l00222"></a>00222         <span class="comment">// Try to read out the data block</span>
<a name="l00223"></a>00223         <span class="keywordflow">if</span> (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &amp;mp)) {
<a name="l00224"></a>00224           memcpy (mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
<a name="l00225"></a>00225         } <span class="keywordflow">else</span> {
<a name="l00226"></a>00226           bFailure = <span class="keyword">true</span>;
<a name="l00227"></a>00227           printf (<span class="stringliteral">&quot;!\nError: unable to read block 0x%02x\n&quot;</span>, iBlock);
<a name="l00228"></a>00228           <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00229"></a>00229         }
<a name="l00230"></a>00230       }
<a name="l00231"></a>00231     }
<a name="l00232"></a>00232   }
<a name="l00233"></a>00233   print_success_or_failure (bFailure, &amp;uiReadBlocks);
<a name="l00234"></a>00234   printf (<span class="stringliteral">&quot;|\n&quot;</span>);
<a name="l00235"></a>00235   printf (<span class="stringliteral">&quot;Done, %d of %d blocks read.\n&quot;</span>, uiReadBlocks, uiBlocks + 1);
<a name="l00236"></a>00236   fflush (stdout);
<a name="l00237"></a>00237 
<a name="l00238"></a>00238   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00239"></a>00239 }
<a name="l00240"></a>00240 
<a name="l00241"></a>00241 <span class="keyword">static</span>  <span class="keywordtype">bool</span>
<a name="l00242"></a>00242 write_card (<span class="keywordtype">void</span>)
<a name="l00243"></a>00243 {
<a name="l00244"></a>00244   uint32_t uiBlock;
<a name="l00245"></a>00245   <span class="keywordtype">bool</span>    bFailure = <span class="keyword">false</span>;
<a name="l00246"></a>00246   uint32_t uiWriteBlocks = 0;
<a name="l00247"></a>00247 
<a name="l00248"></a>00248   printf (<span class="stringliteral">&quot;Writing %d blocks |&quot;</span>, uiBlocks + 1);
<a name="l00249"></a>00249 
<a name="l00250"></a>00250   <span class="comment">// Write the card from begin to end;</span>
<a name="l00251"></a>00251   <span class="keywordflow">for</span> (uiBlock = 0; uiBlock &lt;= uiBlocks; uiBlock++) {
<a name="l00252"></a>00252     <span class="comment">// Authenticate everytime we reach the first sector of a new block</span>
<a name="l00253"></a>00253     <span class="keywordflow">if</span> (is_first_block (uiBlock)) {
<a name="l00254"></a>00254       <span class="comment">// Skip this the first time, bFailure it means nothing (yet)</span>
<a name="l00255"></a>00255       <span class="keywordflow">if</span> (uiBlock != 0)
<a name="l00256"></a>00256         print_success_or_failure (bFailure, &amp;uiWriteBlocks);
<a name="l00257"></a>00257 
<a name="l00258"></a>00258       <span class="comment">// Show if the readout went well</span>
<a name="l00259"></a>00259       <span class="keywordflow">if</span> (bFailure) {
<a name="l00260"></a>00260         <span class="comment">// When a failure occured we need to redo the anti-collision</span>
<a name="l00261"></a>00261         <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#acff0e43e51d7a2c8d750d09fa5265e16" title="Select a passive or emulated tag.">nfc_initiator_select_passive_target</a> (pnd, nmMifare, NULL, 0, &amp;nt)) {
<a name="l00262"></a>00262           printf (<span class="stringliteral">&quot;!\nError: tag was removed\n&quot;</span>);
<a name="l00263"></a>00263           <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00264"></a>00264         }
<a name="l00265"></a>00265         bFailure = <span class="keyword">false</span>;
<a name="l00266"></a>00266       }
<a name="l00267"></a>00267 
<a name="l00268"></a>00268       fflush (stdout);
<a name="l00269"></a>00269 
<a name="l00270"></a>00270       <span class="comment">// Try to authenticate for the current sector</span>
<a name="l00271"></a>00271       <span class="keywordflow">if</span> (!authenticate (uiBlock)) {
<a name="l00272"></a>00272         printf (<span class="stringliteral">&quot;!\nError: authentication failed for block %02x\n&quot;</span>, uiBlock);
<a name="l00273"></a>00273         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00274"></a>00274       }
<a name="l00275"></a>00275     }
<a name="l00276"></a>00276 
<a name="l00277"></a>00277     <span class="keywordflow">if</span> (is_trailer_block (uiBlock)) {
<a name="l00278"></a>00278       <span class="comment">// Copy the keys over from our key dump and store the retrieved access bits</span>
<a name="l00279"></a>00279       memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbt.abtKeyA, 6);
<a name="l00280"></a>00280       memcpy (mp.mpd.abtData + 6, mtDump.amb[uiBlock].mbt.abtAccessBits, 4);
<a name="l00281"></a>00281       memcpy (mp.mpd.abtData + 10, mtDump.amb[uiBlock].mbt.abtKeyB, 6);
<a name="l00282"></a>00282 
<a name="l00283"></a>00283       <span class="comment">// Try to write the trailer</span>
<a name="l00284"></a>00284       <span class="keywordflow">if</span> (nfc_initiator_mifare_cmd (pnd, MC_WRITE, uiBlock, &amp;mp) == <span class="keyword">false</span>) {
<a name="l00285"></a>00285         printf (<span class="stringliteral">&quot;failed to write trailer block %d \n&quot;</span>, uiBlock);
<a name="l00286"></a>00286         bFailure = <span class="keyword">true</span>;
<a name="l00287"></a>00287       }
<a name="l00288"></a>00288     } <span class="keywordflow">else</span> {
<a name="l00289"></a>00289       <span class="comment">// The first block 0x00 is read only, skip this</span>
<a name="l00290"></a>00290       <span class="keywordflow">if</span> (uiBlock == 0)
<a name="l00291"></a>00291         <span class="keywordflow">continue</span>;
<a name="l00292"></a>00292 
<a name="l00293"></a>00293       <span class="comment">// Make sure a earlier write did not fail</span>
<a name="l00294"></a>00294       <span class="keywordflow">if</span> (!bFailure) {
<a name="l00295"></a>00295         <span class="comment">// Try to write the data block</span>
<a name="l00296"></a>00296         memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16);
<a name="l00297"></a>00297         <span class="keywordflow">if</span> (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, uiBlock, &amp;mp))
<a name="l00298"></a>00298           bFailure = <span class="keyword">true</span>;
<a name="l00299"></a>00299       }
<a name="l00300"></a>00300     }
<a name="l00301"></a>00301   }
<a name="l00302"></a>00302   print_success_or_failure (bFailure, &amp;uiWriteBlocks);
<a name="l00303"></a>00303   printf (<span class="stringliteral">&quot;|\n&quot;</span>);
<a name="l00304"></a>00304   printf (<span class="stringliteral">&quot;Done, %d of %d blocks written.\n&quot;</span>, uiWriteBlocks, uiBlocks + 1);
<a name="l00305"></a>00305   fflush (stdout);
<a name="l00306"></a>00306 
<a name="l00307"></a>00307   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00308"></a>00308 }
<a name="l00309"></a>00309 
<a name="l00310"></a>00310 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00311"></a>00311 mifare_classic_extract_payload (<span class="keyword">const</span> <span class="keywordtype">char</span> *abDump, <span class="keywordtype">char</span> *pbPayload)
<a name="l00312"></a>00312 {
<a name="l00313"></a>00313   uint8_t uiSectorIndex;
<a name="l00314"></a>00314   uint8_t uiBlockIndex;
<a name="l00315"></a>00315   <span class="keywordtype">size_t</span>  szDumpOffset;
<a name="l00316"></a>00316   <span class="keywordtype">size_t</span>  szPayloadIndex = 0;
<a name="l00317"></a>00317 
<a name="l00318"></a>00318   <span class="keywordflow">for</span> (uiSectorIndex = 1; uiSectorIndex &lt; 16; uiSectorIndex++) {
<a name="l00319"></a>00319     <span class="keywordflow">for</span> (uiBlockIndex = 0; uiBlockIndex &lt; 3; uiBlockIndex++) {
<a name="l00320"></a>00320       szDumpOffset = uiSectorIndex * 16 * 4 + uiBlockIndex * 16;
<a name="l00321"></a>00321 <span class="comment">//      for(uint8_t uiByteIndex=0; uiByteIndex&lt;16; uiByteIndex++) printf(&quot;%02x &quot;, abDump[szPayloadIndex+uiByteIndex]);</span>
<a name="l00322"></a>00322       memcpy (pbPayload + szPayloadIndex, abDump + szDumpOffset, 16);
<a name="l00323"></a>00323       szPayloadIndex += 16;
<a name="l00324"></a>00324     }
<a name="l00325"></a>00325   }
<a name="l00326"></a>00326 }
<a name="l00327"></a>00327 
<a name="l00328"></a>00328 <span class="keyword">typedef</span> <span class="keyword">enum</span> {
<a name="l00329"></a>00329   ACTION_READ,
<a name="l00330"></a>00330   ACTION_WRITE,
<a name="l00331"></a>00331   ACTION_EXTRACT,
<a name="l00332"></a>00332   ACTION_USAGE
<a name="l00333"></a>00333 } action_t;
<a name="l00334"></a>00334 
<a name="l00335"></a>00335 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00336"></a>00336 print_usage (<span class="keyword">const</span> <span class="keywordtype">char</span> *pcProgramName)
<a name="l00337"></a>00337 {
<a name="l00338"></a>00338   printf (<span class="stringliteral">&quot;Usage: &quot;</span>);
<a name="l00339"></a>00339   printf (<span class="stringliteral">&quot;%s r|w a|b &lt;dump.mfd&gt; [&lt;keys.mfd&gt;]\n&quot;</span>, pcProgramName);
<a name="l00340"></a>00340   printf (<span class="stringliteral">&quot;  r|w           - Perform read from (r) or write to (w) card\n&quot;</span>);
<a name="l00341"></a>00341   printf (<span class="stringliteral">&quot;  a|b           - Use A or B keys for action\n&quot;</span>);
<a name="l00342"></a>00342   printf (<span class="stringliteral">&quot;  &lt;dump.mfd&gt;    - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n&quot;</span>);
<a name="l00343"></a>00343   printf (<span class="stringliteral">&quot;  &lt;keys.mfd&gt;    - MiFare Dump (MFD) that contain the keys (optional)\n&quot;</span>);
<a name="l00344"></a>00344   printf (<span class="stringliteral">&quot;Or: &quot;</span>);
<a name="l00345"></a>00345   printf (<span class="stringliteral">&quot;%s x &lt;dump.mfd&gt; &lt;payload.bin&gt;\n&quot;</span>, pcProgramName);
<a name="l00346"></a>00346   printf (<span class="stringliteral">&quot;  x             - Extract payload (data blocks) from MFD\n&quot;</span>);
<a name="l00347"></a>00347   printf (<span class="stringliteral">&quot;  &lt;dump.mfd&gt;    - MiFare Dump (MFD) that contains wanted payload\n&quot;</span>);
<a name="l00348"></a>00348   printf (<span class="stringliteral">&quot;  &lt;payload.bin&gt; - Binary file where payload will be extracted\n&quot;</span>);
<a name="l00349"></a>00349 }
<a name="l00350"></a>00350 
<a name="l00351"></a>00351 <span class="keywordtype">int</span>
<a name="l00352"></a>00352 main (<span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span> *argv[])
<a name="l00353"></a>00353 {
<a name="l00354"></a>00354   <span class="keywordtype">bool</span>    b4K;
<a name="l00355"></a>00355   action_t atAction = ACTION_USAGE;
<a name="l00356"></a>00356   byte_t *pbtUID;
<a name="l00357"></a>00357   FILE   *pfKeys = NULL;
<a name="l00358"></a>00358   FILE   *pfDump = NULL;
<a name="l00359"></a>00359   <span class="keyword">const</span> <span class="keywordtype">char</span> *command = argv[1];
<a name="l00360"></a>00360 
<a name="l00361"></a>00361   <span class="keywordflow">if</span> (argc &lt; 3) {
<a name="l00362"></a>00362     print_usage (argv[0]);
<a name="l00363"></a>00363     exit (EXIT_FAILURE);
<a name="l00364"></a>00364   }
<a name="l00365"></a>00365 
<a name="l00366"></a>00366   <span class="keywordflow">if</span> (strcmp (command, <span class="stringliteral">&quot;r&quot;</span>) == 0) {
<a name="l00367"></a>00367     atAction = ACTION_READ;
<a name="l00368"></a>00368     bUseKeyA = tolower ((<span class="keywordtype">int</span>) ((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>) *(argv[2]))) == <span class="charliteral">&#39;a&#39;</span>;
<a name="l00369"></a>00369     bUseKeyFile = (argc &gt; 4);
<a name="l00370"></a>00370   } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (command, <span class="stringliteral">&quot;w&quot;</span>) == 0) {
<a name="l00371"></a>00371     atAction = ACTION_WRITE;
<a name="l00372"></a>00372     bUseKeyA = tolower ((<span class="keywordtype">int</span>) ((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>) *(argv[2]))) == <span class="charliteral">&#39;a&#39;</span>;
<a name="l00373"></a>00373     bUseKeyFile = (argc &gt; 4);
<a name="l00374"></a>00374   } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (command, <span class="stringliteral">&quot;x&quot;</span>) == 0) {
<a name="l00375"></a>00375     atAction = ACTION_EXTRACT;
<a name="l00376"></a>00376   }
<a name="l00377"></a>00377 
<a name="l00378"></a>00378   <span class="keywordflow">switch</span> (atAction) {
<a name="l00379"></a>00379   <span class="keywordflow">case</span> ACTION_USAGE:
<a name="l00380"></a>00380     print_usage (argv[0]);
<a name="l00381"></a>00381     exit (EXIT_FAILURE);
<a name="l00382"></a>00382     <span class="keywordflow">break</span>;
<a name="l00383"></a>00383   <span class="keywordflow">case</span> ACTION_READ:
<a name="l00384"></a>00384   <span class="keywordflow">case</span> ACTION_WRITE:
<a name="l00385"></a>00385     <span class="keywordflow">if</span> (argc &lt; 4) {
<a name="l00386"></a>00386       print_usage (argv[0]);
<a name="l00387"></a>00387       exit (EXIT_FAILURE);
<a name="l00388"></a>00388     }
<a name="l00389"></a>00389 
<a name="l00390"></a>00390     <span class="keywordflow">if</span> (bUseKeyFile) {
<a name="l00391"></a>00391       pfKeys = fopen (argv[4], <span class="stringliteral">&quot;rb&quot;</span>);
<a name="l00392"></a>00392       <span class="keywordflow">if</span> (pfKeys == NULL) {
<a name="l00393"></a>00393         printf (<span class="stringliteral">&quot;Could not open keys file: %s\n&quot;</span>, argv[4]);
<a name="l00394"></a>00394         exit (EXIT_FAILURE);
<a name="l00395"></a>00395       }
<a name="l00396"></a>00396       <span class="keywordflow">if</span> (fread (&amp;mtKeys, 1, <span class="keyword">sizeof</span> (mtKeys), pfKeys) != <span class="keyword">sizeof</span> (mtKeys)) {
<a name="l00397"></a>00397         printf (<span class="stringliteral">&quot;Could not read keys file: %s\n&quot;</span>, argv[4]);
<a name="l00398"></a>00398         fclose (pfKeys);
<a name="l00399"></a>00399         exit (EXIT_FAILURE);
<a name="l00400"></a>00400       }
<a name="l00401"></a>00401       fclose (pfKeys);
<a name="l00402"></a>00402     }
<a name="l00403"></a>00403 
<a name="l00404"></a>00404     <span class="keywordflow">if</span> (atAction == ACTION_READ) {
<a name="l00405"></a>00405       memset (&amp;mtDump, 0x00, <span class="keyword">sizeof</span> (mtDump));
<a name="l00406"></a>00406     } <span class="keywordflow">else</span> {
<a name="l00407"></a>00407       pfDump = fopen (argv[3], <span class="stringliteral">&quot;rb&quot;</span>);
<a name="l00408"></a>00408 
<a name="l00409"></a>00409       <span class="keywordflow">if</span> (pfDump == NULL) {
<a name="l00410"></a>00410         printf (<span class="stringliteral">&quot;Could not open dump file: %s\n&quot;</span>, argv[3]);
<a name="l00411"></a>00411         exit (EXIT_FAILURE);
<a name="l00412"></a>00412       }
<a name="l00413"></a>00413 
<a name="l00414"></a>00414       <span class="keywordflow">if</span> (fread (&amp;mtDump, 1, <span class="keyword">sizeof</span> (mtDump), pfDump) != <span class="keyword">sizeof</span> (mtDump)) {
<a name="l00415"></a>00415         printf (<span class="stringliteral">&quot;Could not read dump file: %s\n&quot;</span>, argv[3]);
<a name="l00416"></a>00416         fclose (pfDump);
<a name="l00417"></a>00417         exit (EXIT_FAILURE);
<a name="l00418"></a>00418       }
<a name="l00419"></a>00419       fclose (pfDump);
<a name="l00420"></a>00420     }
<a name="l00421"></a>00421     <span class="comment">// printf(&quot;Successfully opened required files\n&quot;);</span>
<a name="l00422"></a>00422 
<a name="l00423"></a>00423     <span class="comment">// Try to open the NFC reader</span>
<a name="l00424"></a>00424     pnd = <a class="code" href="nfc_8c.html#aff0c2c92d2fc81e5dc93150fc72a7de6" title="Connect to a NFC device.">nfc_connect</a> (NULL);
<a name="l00425"></a>00425     <span class="keywordflow">if</span> (pnd == NULL) {
<a name="l00426"></a>00426       printf (<span class="stringliteral">&quot;Error connecting NFC reader\n&quot;</span>);
<a name="l00427"></a>00427       exit (EXIT_FAILURE);
<a name="l00428"></a>00428     }
<a name="l00429"></a>00429 
<a name="l00430"></a>00430     <a class="code" href="nfc_8c.html#a1c05aadcf1b30e8d69969dfd74a793f2" title="Initialize NFC device as initiator (reader)">nfc_initiator_init</a> (pnd);
<a name="l00431"></a>00431 
<a name="l00432"></a>00432     <span class="comment">// Drop the field for a while</span>
<a name="l00433"></a>00433     <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#a6cd940508b34b99c6c94173f8300d069" title="Configure advanced NFC device settings.">nfc_configure</a> (pnd, <a class="code" href="nfc-types_8h.html#ab2cdef5e5ac453e9c60c9fe449cb311ea932832c243c9448924de6e21aa622d07">NDO_ACTIVATE_FIELD</a>, <span class="keyword">false</span>)) {
<a name="l00434"></a>00434       <a class="code" href="nfc_8c.html#a111b9182fc80b309b56009e9c6be7454" title="Display the PCD error a-la perror.">nfc_perror</a> (pnd, <span class="stringliteral">&quot;nfc_configure&quot;</span>);
<a name="l00435"></a>00435       exit (EXIT_FAILURE);
<a name="l00436"></a>00436     }
<a name="l00437"></a>00437     <span class="comment">// Let the reader only try once to find a tag</span>
<a name="l00438"></a>00438     <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#a6cd940508b34b99c6c94173f8300d069" title="Configure advanced NFC device settings.">nfc_configure</a> (pnd, <a class="code" href="nfc-types_8h.html#ab2cdef5e5ac453e9c60c9fe449cb311ead179fd877ff1ba540f0425fa3547825d">NDO_INFINITE_SELECT</a>, <span class="keyword">false</span>)) {
<a name="l00439"></a>00439       <a class="code" href="nfc_8c.html#a111b9182fc80b309b56009e9c6be7454" title="Display the PCD error a-la perror.">nfc_perror</a> (pnd, <span class="stringliteral">&quot;nfc_configure&quot;</span>);
<a name="l00440"></a>00440       exit (EXIT_FAILURE);
<a name="l00441"></a>00441     }
<a name="l00442"></a>00442     <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#a6cd940508b34b99c6c94173f8300d069" title="Configure advanced NFC device settings.">nfc_configure</a> (pnd, <a class="code" href="nfc-types_8h.html#ab2cdef5e5ac453e9c60c9fe449cb311ea34588e4870fcc10476151e27e8e97a74">NDO_HANDLE_CRC</a>, <span class="keyword">true</span>)) {
<a name="l00443"></a>00443       <a class="code" href="nfc_8c.html#a111b9182fc80b309b56009e9c6be7454" title="Display the PCD error a-la perror.">nfc_perror</a> (pnd, <span class="stringliteral">&quot;nfc_configure&quot;</span>);
<a name="l00444"></a>00444       exit (EXIT_FAILURE);
<a name="l00445"></a>00445     }
<a name="l00446"></a>00446     <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#a6cd940508b34b99c6c94173f8300d069" title="Configure advanced NFC device settings.">nfc_configure</a> (pnd, <a class="code" href="nfc-types_8h.html#ab2cdef5e5ac453e9c60c9fe449cb311ea7c1f6097b2d2a1670f580de332c41596">NDO_HANDLE_PARITY</a>, <span class="keyword">true</span>)) {
<a name="l00447"></a>00447       <a class="code" href="nfc_8c.html#a111b9182fc80b309b56009e9c6be7454" title="Display the PCD error a-la perror.">nfc_perror</a> (pnd, <span class="stringliteral">&quot;nfc_configure&quot;</span>);
<a name="l00448"></a>00448       exit (EXIT_FAILURE);
<a name="l00449"></a>00449     }
<a name="l00450"></a>00450     <span class="comment">// Enable field so more power consuming cards can power themselves up</span>
<a name="l00451"></a>00451     <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#a6cd940508b34b99c6c94173f8300d069" title="Configure advanced NFC device settings.">nfc_configure</a> (pnd, <a class="code" href="nfc-types_8h.html#ab2cdef5e5ac453e9c60c9fe449cb311ea932832c243c9448924de6e21aa622d07">NDO_ACTIVATE_FIELD</a>, <span class="keyword">true</span>)) {
<a name="l00452"></a>00452       <a class="code" href="nfc_8c.html#a111b9182fc80b309b56009e9c6be7454" title="Display the PCD error a-la perror.">nfc_perror</a> (pnd, <span class="stringliteral">&quot;nfc_configure&quot;</span>);
<a name="l00453"></a>00453       exit (EXIT_FAILURE);
<a name="l00454"></a>00454     }
<a name="l00455"></a>00455     <span class="comment">// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.</span>
<a name="l00456"></a>00456     <a class="code" href="nfc_8c.html#a6cd940508b34b99c6c94173f8300d069" title="Configure advanced NFC device settings.">nfc_configure</a> (pnd, <a class="code" href="nfc-types_8h.html#ab2cdef5e5ac453e9c60c9fe449cb311eae40c8c9da1c2e8d123948e03374a1c0c">NDO_AUTO_ISO14443_4</a>, <span class="keyword">false</span>);
<a name="l00457"></a>00457 
<a name="l00458"></a>00458     printf (<span class="stringliteral">&quot;Connected to NFC reader: %s\n&quot;</span>, pnd-&gt;<a class="code" href="structnfc__device__t.html#a3933f0bea196fedca0345a7f940dc85b">acName</a>);
<a name="l00459"></a>00459 
<a name="l00460"></a>00460     <span class="comment">// Try to find a MIFARE Classic tag</span>
<a name="l00461"></a>00461     <span class="keywordflow">if</span> (!<a class="code" href="nfc_8c.html#acff0e43e51d7a2c8d750d09fa5265e16" title="Select a passive or emulated tag.">nfc_initiator_select_passive_target</a> (pnd, nmMifare, NULL, 0, &amp;nt)) {
<a name="l00462"></a>00462       printf (<span class="stringliteral">&quot;Error: no tag was found\n&quot;</span>);
<a name="l00463"></a>00463       <a class="code" href="nfc_8c.html#a442dc68878c3a0bb7e3293c8fd96afb0" title="Disconnect from a NFC device.">nfc_disconnect</a> (pnd);
<a name="l00464"></a>00464       exit (EXIT_FAILURE);
<a name="l00465"></a>00465     }
<a name="l00466"></a>00466     <span class="comment">// Test if we are dealing with a MIFARE compatible tag</span>
<a name="l00467"></a>00467     <span class="keywordflow">if</span> ((nt.nti.nai.btSak &amp; 0x08) == 0) {
<a name="l00468"></a>00468       printf (<span class="stringliteral">&quot;Error: tag is not a MIFARE Classic card\n&quot;</span>);
<a name="l00469"></a>00469       <a class="code" href="nfc_8c.html#a442dc68878c3a0bb7e3293c8fd96afb0" title="Disconnect from a NFC device.">nfc_disconnect</a> (pnd);
<a name="l00470"></a>00470       exit (EXIT_FAILURE);
<a name="l00471"></a>00471     }
<a name="l00472"></a>00472 
<a name="l00473"></a>00473     <span class="keywordflow">if</span> (bUseKeyFile) {
<a name="l00474"></a>00474       <span class="comment">// Get the info from the key dump</span>
<a name="l00475"></a>00475       b4K = (mtKeys.amb[0].mbm.abtATQA[1] == 0x02);
<a name="l00476"></a>00476       pbtUID = mtKeys.amb[0].mbm.abtUID;
<a name="l00477"></a>00477 
<a name="l00478"></a>00478       <span class="comment">// Compare if key dump UID is the same as the current tag UID</span>
<a name="l00479"></a>00479       <span class="keywordflow">if</span> (memcmp (nt.nti.nai.abtUid, pbtUID, 4) != 0) {
<a name="l00480"></a>00480         printf (<span class="stringliteral">&quot;Expected MIFARE Classic %ck card with UID: %02x%02x%02x%02x\n&quot;</span>, b4K ? <span class="charliteral">&#39;4&#39;</span> : <span class="charliteral">&#39;1&#39;</span>, pbtUID[3], pbtUID[2],
<a name="l00481"></a>00481                 pbtUID[1], pbtUID[0]);
<a name="l00482"></a>00482       }
<a name="l00483"></a>00483     }
<a name="l00484"></a>00484     <span class="comment">// Get the info from the current tag</span>
<a name="l00485"></a>00485     pbtUID = nt.nti.nai.abtUid;
<a name="l00486"></a>00486     b4K = (nt.nti.nai.abtAtqa[1] == 0x02);
<a name="l00487"></a>00487     printf (<span class="stringliteral">&quot;Found MIFARE Classic %ck card with UID: %02x%02x%02x%02x\n&quot;</span>, b4K ? <span class="charliteral">&#39;4&#39;</span> : <span class="charliteral">&#39;1&#39;</span>, pbtUID[3], pbtUID[2],
<a name="l00488"></a>00488             pbtUID[1], pbtUID[0]);
<a name="l00489"></a>00489 
<a name="l00490"></a>00490     uiBlocks = (b4K) ? 0xff : 0x3f;
<a name="l00491"></a>00491 
<a name="l00492"></a>00492     <span class="keywordflow">if</span> (atAction == ACTION_READ) {
<a name="l00493"></a>00493       <span class="keywordflow">if</span> (read_card ()) {
<a name="l00494"></a>00494         printf (<span class="stringliteral">&quot;Writing data to file: %s ...&quot;</span>, argv[3]);
<a name="l00495"></a>00495         fflush (stdout);
<a name="l00496"></a>00496         pfDump = fopen (argv[3], <span class="stringliteral">&quot;wb&quot;</span>);
<a name="l00497"></a>00497   <span class="keywordflow">if</span> (pfDump == NULL) {
<a name="l00498"></a>00498       printf (<span class="stringliteral">&quot;Could not open dump file: %s\n&quot;</span>, argv[3]);
<a name="l00499"></a>00499       exit (EXIT_FAILURE);
<a name="l00500"></a>00500   }
<a name="l00501"></a>00501         <span class="keywordflow">if</span> (fwrite (&amp;mtDump, 1, <span class="keyword">sizeof</span> (mtDump), pfDump) != <span class="keyword">sizeof</span> (mtDump)) {
<a name="l00502"></a>00502           printf (<span class="stringliteral">&quot;\nCould not write to file: %s\n&quot;</span>, argv[3]);
<a name="l00503"></a>00503           exit (EXIT_FAILURE);
<a name="l00504"></a>00504         }
<a name="l00505"></a>00505         printf (<span class="stringliteral">&quot;Done.\n&quot;</span>);
<a name="l00506"></a>00506         fclose (pfDump);
<a name="l00507"></a>00507       }
<a name="l00508"></a>00508     } <span class="keywordflow">else</span> {
<a name="l00509"></a>00509       write_card ();
<a name="l00510"></a>00510     }
<a name="l00511"></a>00511 
<a name="l00512"></a>00512     <a class="code" href="nfc_8c.html#a442dc68878c3a0bb7e3293c8fd96afb0" title="Disconnect from a NFC device.">nfc_disconnect</a> (pnd);
<a name="l00513"></a>00513     <span class="keywordflow">break</span>;
<a name="l00514"></a>00514 
<a name="l00515"></a>00515   <span class="keywordflow">case</span> ACTION_EXTRACT:{
<a name="l00516"></a>00516       <span class="keyword">const</span> <span class="keywordtype">char</span> *pcDump = argv[2];
<a name="l00517"></a>00517       <span class="keyword">const</span> <span class="keywordtype">char</span> *pcPayload = argv[3];
<a name="l00518"></a>00518 
<a name="l00519"></a>00519       FILE   *pfDump = NULL;
<a name="l00520"></a>00520       FILE   *pfPayload = NULL;
<a name="l00521"></a>00521 
<a name="l00522"></a>00522       <span class="keywordtype">char</span>    abDump[4096];
<a name="l00523"></a>00523       <span class="keywordtype">char</span>    abPayload[4096];
<a name="l00524"></a>00524 
<a name="l00525"></a>00525       pfDump = fopen (pcDump, <span class="stringliteral">&quot;rb&quot;</span>);
<a name="l00526"></a>00526 
<a name="l00527"></a>00527       <span class="keywordflow">if</span> (pfDump == NULL) {
<a name="l00528"></a>00528         printf (<span class="stringliteral">&quot;Could not open dump file: %s\n&quot;</span>, pcDump);
<a name="l00529"></a>00529         exit (EXIT_FAILURE);
<a name="l00530"></a>00530       }
<a name="l00531"></a>00531 
<a name="l00532"></a>00532       <span class="keywordflow">if</span> (fread (abDump, 1, <span class="keyword">sizeof</span> (abDump), pfDump) != <span class="keyword">sizeof</span> (abDump)) {
<a name="l00533"></a>00533         printf (<span class="stringliteral">&quot;Could not read dump file: %s\n&quot;</span>, pcDump);
<a name="l00534"></a>00534         fclose (pfDump);
<a name="l00535"></a>00535         exit (EXIT_FAILURE);
<a name="l00536"></a>00536       }
<a name="l00537"></a>00537       fclose (pfDump);
<a name="l00538"></a>00538 
<a name="l00539"></a>00539       mifare_classic_extract_payload (abDump, abPayload);
<a name="l00540"></a>00540 
<a name="l00541"></a>00541       printf (<span class="stringliteral">&quot;Writing data to file: %s\n&quot;</span>, pcPayload);
<a name="l00542"></a>00542       pfPayload = fopen (pcPayload, <span class="stringliteral">&quot;wb&quot;</span>);
<a name="l00543"></a>00543       <span class="keywordflow">if</span> (pfPayload == NULL) {
<a name="l00544"></a>00544         printf (<span class="stringliteral">&quot;Could not open file %s for writting.\n&quot;</span>, pcPayload);
<a name="l00545"></a>00545         exit (EXIT_FAILURE);
<a name="l00546"></a>00546       }
<a name="l00547"></a>00547       <span class="keywordflow">if</span> (fwrite (abPayload, 1, <span class="keyword">sizeof</span> (abPayload), pfPayload) != <span class="keyword">sizeof</span> (abPayload)) {
<a name="l00548"></a>00548         printf (<span class="stringliteral">&quot;Could not write to file: %s\n&quot;</span>, pcPayload);
<a name="l00549"></a>00549         exit (EXIT_FAILURE);
<a name="l00550"></a>00550       }
<a name="l00551"></a>00551       fclose (pfPayload);
<a name="l00552"></a>00552       printf (<span class="stringliteral">&quot;Done, all bytes have been extracted!\n&quot;</span>);
<a name="l00553"></a>00553     }
<a name="l00554"></a>00554   };
<a name="l00555"></a>00555 
<a name="l00556"></a>00556   exit (EXIT_SUCCESS);
<a name="l00557"></a>00557 }
</pre></div></div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Feb 23 2011 for libnfc by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </small></address>
</body>
</html>