libnfc  1.7.0-rc7
nfc-utils.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * Additional contributors of this file:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * 1) Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2 )Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Note that this license only applies on the examples, NFC library itself is under LGPL
33  *
34  */
39 #include <nfc/nfc.h>
40 #include <err.h>
41 
42 #include "nfc-utils.h"
43 
44 uint8_t
45 oddparity(const uint8_t bt)
46 {
47  // cf http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
48  return (0x9669 >> ((bt ^(bt >> 4)) & 0xF)) & 1;
49 }
50 
51 void
52 oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
53 {
54  size_t szByteNr;
55  // Calculate the parity bits for the command
56  for (szByteNr = 0; szByteNr < szLen; szByteNr++) {
57  pbtPar[szByteNr] = oddparity(pbtData[szByteNr]);
58  }
59 }
60 
61 void
62 print_hex(const uint8_t *pbtData, const size_t szBytes)
63 {
64  size_t szPos;
65 
66  for (szPos = 0; szPos < szBytes; szPos++) {
67  printf("%02x ", pbtData[szPos]);
68  }
69  printf("\n");
70 }
71 
72 void
73 print_hex_bits(const uint8_t *pbtData, const size_t szBits)
74 {
75  uint8_t uRemainder;
76  size_t szPos;
77  size_t szBytes = szBits / 8;
78 
79  for (szPos = 0; szPos < szBytes; szPos++) {
80  printf("%02x ", pbtData[szPos]);
81  }
82 
83  uRemainder = szBits % 8;
84  // Print the rest bits
85  if (uRemainder != 0) {
86  if (uRemainder < 5)
87  printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
88  else
89  printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
90  }
91  printf("\n");
92 }
93 
94 void
95 print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
96 {
97  uint8_t uRemainder;
98  size_t szPos;
99  size_t szBytes = szBits / 8;
100 
101  for (szPos = 0; szPos < szBytes; szPos++) {
102  printf("%02x", pbtData[szPos]);
103  if (oddparity(pbtData[szPos]) != pbtDataPar[szPos]) {
104  printf("! ");
105  } else {
106  printf(" ");
107  }
108  }
109 
110  uRemainder = szBits % 8;
111  // Print the rest bits, these cannot have parity bit
112  if (uRemainder != 0) {
113  if (uRemainder < 5)
114  printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
115  else
116  printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
117  }
118  printf("\n");
119 }
120 
121 void
122 print_nfc_target(const nfc_target *pnt, bool verbose)
123 {
124  char *s;
125  str_nfc_target(&s, pnt, verbose);
126  printf("%s", s);
127  nfc_free(s);
128 }