diff --git a/Makefile.am b/Makefile.am index de16f27..b294cb9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = libfreefare test examples +SUBDIRS = contrib libfreefare test examples pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libfreefare.pc diff --git a/configure.ac b/configure.ac index f2c80ed..4b899dd 100644 --- a/configure.ac +++ b/configure.ac @@ -13,6 +13,14 @@ AM_INIT_AUTOMAKE m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])]) +# Debug support (default:no) +AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debug output]),[enable_debug=$enableval],[enable_debug="no"]) +if test x"$enable_debug" = xyes; then + AC_CHECK_LIB([util], [hexdump], [has_libutil=yes], [has_libutil=no]) +fi +AM_CONDITIONAL(WITH_DEBUG, [test x"$enable_debug" = xyes]) +AM_CONDITIONAL(HAS_LIBUTIL, [test x"$has_libutil" = xyes]) + # Checks for typedefs, structures, and compiler characteristics. AC_C_INLINE AC_HEADER_STDBOOL @@ -69,6 +77,8 @@ if test x$ac_cv_enable_coverage = xyes; then fi AC_OUTPUT([Makefile + contrib/Makefile + contrib/libutil/Makefile examples/Makefile libfreefare/Makefile libfreefare.pc diff --git a/contrib/Makefile.am b/contrib/Makefile.am new file mode 100644 index 0000000..da38b23 --- /dev/null +++ b/contrib/Makefile.am @@ -0,0 +1,3 @@ +# $Id$ + +SUBDIRS = libutil diff --git a/contrib/libutil/Makefile.am b/contrib/libutil/Makefile.am new file mode 100644 index 0000000..1143567 --- /dev/null +++ b/contrib/libutil/Makefile.am @@ -0,0 +1,10 @@ +# $Id$ + +if WITH_DEBUG +if !HAS_LIBUTIL +noinst_LTLIBRARIES = libutil.la +noinst_HEADERS = libutil.h + +libutil_la_SOURCES = hexdump.c +endif # !HAS_LIBUTIL +endif # WITH_DEBUG diff --git a/contrib/libutil/hexdump.c b/contrib/libutil/hexdump.c new file mode 100644 index 0000000..b0372d9 --- /dev/null +++ b/contrib/libutil/hexdump.c @@ -0,0 +1,97 @@ +/*- + * Copyright (c) 1986, 1988, 1991, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 + * $Id$ + */ + +//#include +//__FBSDID("$FreeBSD: stable/8/lib/libutil/hexdump.c 180161 2008-07-01 22:30:57Z jhb $"); + +#include +#include +#include + +void +hexdump(const void *ptr, int length, const char *hdr, int flags) +{ + int i, j, k; + int cols; + const unsigned char *cp; + char delim; + + if ((flags & HD_DELIM_MASK) != 0) + delim = (flags & HD_DELIM_MASK) >> 8; + else + delim = ' '; + + if ((flags & HD_COLUMN_MASK) != 0) + cols = flags & HD_COLUMN_MASK; + else + cols = 16; + + cp = ptr; + for (i = 0; i < length; i+= cols) { + if (hdr != NULL) + printf("%s", hdr); + + if ((flags & HD_OMIT_COUNT) == 0) + printf("%04x ", i); + + if ((flags & HD_OMIT_HEX) == 0) { + for (j = 0; j < cols; j++) { + k = i + j; + if (k < length) + printf("%c%02x", delim, cp[k]); + else + printf(" "); + } + } + + if ((flags & HD_OMIT_CHARS) == 0) { + printf(" |"); + for (j = 0; j < cols; j++) { + k = i + j; + if (k >= length) + printf(" "); + else if (cp[k] >= ' ' && cp[k] <= '~') + printf("%c", cp[k]); + else + printf("."); + } + printf("|"); + } + printf("\n"); + } +} + diff --git a/contrib/libutil/libutil.h b/contrib/libutil/libutil.h new file mode 100644 index 0000000..74973bc --- /dev/null +++ b/contrib/libutil/libutil.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 1996 Peter Wemm . + * All rights reserved. + * Copyright (c) 2002 Networks Associates Technology, Inc. + * All rights reserved. + * + * Portions of this software were developed for the FreeBSD Project by + * ThinkSec AS and NAI Labs, the Security Research Division of Network + * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 + * ("CBOSS"), as part of the DARPA CHATS research program. + * + * Redistribution and use in source and binary forms, with or without + * modification, is permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD: stable/8/lib/libutil/libutil.h 185548 2008-12-02 06:50:26Z peter $ + * $Id$ + */ + +#ifndef _LIBUTIL_H_ +#define _LIBUTIL_H_ + +void hexdump(const void *ptr, int length, const char *hdr, int flags); + +/* hexdump(3) */ +#define HD_COLUMN_MASK 0xff +#define HD_DELIM_MASK 0xff00 +#define HD_OMIT_COUNT (1 << 16) +#define HD_OMIT_HEX (1 << 17) +#define HD_OMIT_CHARS (1 << 18) + +#endif /* !_LIBUTIL_H_ */ diff --git a/libfreefare/Makefile.am b/libfreefare/Makefile.am index 9567121..ee0b4a9 100644 --- a/libfreefare/Makefile.am +++ b/libfreefare/Makefile.am @@ -15,6 +15,16 @@ libfreefare_la_SOURCES = freefare.c \ desfire_error.c \ mifare_application.c \ tlv.c +libfreefare_la_LIBADD = + +if WITH_DEBUG +if HAS_LIBUTIL + libfreefare_la_LIBADD += -lutil +else # HAS_LIBUTIL + libfreefare_la_LIBADD += $(top_builddir)/contrib/libutil/libutil.la + AM_CFLAGS += -I$(top_builddir)/contrib/libutil/ -DWITH_DEBUG +endif # !HAS_LIBUTIL +endif # WITH_DEBUG libfreefare_la_HEADERS = freefare.h libfreefare_ladir = $(includedir) diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index 35df59f..e3ee48c 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -200,4 +200,10 @@ struct mifare_ultralight_tag { #define DB_AB(ab) ((ab == C_DEFAULT) ? C_000 : ab) #define TB_AB(ab) ((ab == C_DEFAULT) ? C_100 : ab) +#ifdef WITH_DEBUG +#define DEBUG_XFER(data, nbytes, hint) do { hexdump (data, nbytes, hint, 0); } while (0) +#else +#define DEBUG_XFER(data, nbytes, hint) do {} while (0) +#endif + #endif /* !__FREEFARE_INTERNAL_H__ */ diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index f733e6d..de038a9 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -44,6 +44,10 @@ #include #include +#ifdef WITH_DEBUG +# include "libutil.h" +#endif + #include #include "freefare_internal.h" @@ -179,8 +183,10 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ do { \ errno = 0; \ MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; \ + DEBUG_XFER (msg, __##msg##_n, "===> "); \ if (!(nfc_initiator_transceive_dep_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n))) \ return errno = EIO, -1; \ + DEBUG_XFER (res, __##res##_n, "<=== "); \ if ((1 == __##res##_n) && (OPERATION_OK != res[0]) && (ADDITIONAL_FRAME != res[0])) \ return MIFARE_DESFIRE (tag)->last_picc_error = res[0], -1; \ } while (0)